no.abmu.abmstatistikk.annualstatistic.util.DataLoaderH2.java Source code

Java tutorial

Introduction

Here is the source code for no.abmu.abmstatistikk.annualstatistic.util.DataLoaderH2.java

Source

/*$Id: DataLoaderH2.java 12168 2008-12-08 10:43:46Z jens $*/
/*
 ****************************************************************************
 *                                                                          *
 *                   (c) Copyright 2005 ABM-utvikling                       *
 *                                                                          *
 * This program is free software; you can redistribute it and/or modify it  *
 * under the terms of the GNU General Public License as published by the    *
 * Free Software Foundation; either version 2 of the License, or (at your   *
 * option) any later version.                                               *
 *                                                                          *
 * This program is distributed in the hope that it will be useful, but      *
 * WITHOUT ANY WARRANTY; without even the implied warranty of               *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General *
 * Public License for more details. http://www.gnu.org/licenses/gpl.html    *
 *                                                                          *
 ****************************************************************************
 */
package no.abmu.abmstatistikk.annualstatistic.util;

import java.io.File;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.Set;

import no.abmu.abmstatistikk.annualstatistic.service.AnnualStatisticService;
import no.abmu.organisationregister.domain.OrganisationTypeNameConst;
import no.abmu.organisationregister.service.OrganisationUnitService;
import no.abmu.util.hibernate2.spring.ApplicationContextLoaderH2;

import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;

/**
 * Tool to import data into AnnualStatistic database.
 *
 * @author Henning Kulander
 * @author Thomas Oldervoll
 * @author $Author: jens $
 * @version $Rev: 12168 $
 * @date $Date: 2008-12-08 11:43:46 +0100 (Mon, 08 Dec 2008) $
 * @copyright ABM-Utvikling
 */
public class DataLoaderH2 {

    public static final String HIB_FILE_KEY = "hibernate.property.file";
    public static final String HIB_DIALECT_KEY = "hibernate.dialect";

    private static Logger log = Logger.getLogger(DataLoaderH2.class);

    private static String dialect;
    private static Exception initException;

    protected ApplicationContext context;

    private AnnualStatisticService asService;
    private OrganisationUnitService organisationUnitService;

    private boolean loadMuseum = false;
    private boolean loadFagbib = false;
    private boolean loadFolkebib = false;

    public DataLoaderH2(String[] args) {
        parseOpts(args);

        Properties properties = new Properties();
        String hibFile = System.getProperty(HIB_FILE_KEY, "conf/hibernate/hibernate.properties");
        try {
            properties.load(ClassLoader.getSystemClassLoader().getResourceAsStream(hibFile));
            dialect = (String) properties.get(HIB_DIALECT_KEY);
            if (dialect == null) {
                throw new IllegalAccessException("The file " + hibFile + " had no key " + HIB_DIALECT_KEY);
            }

        } catch (Exception e) {
            initException = e;
        }

        System.getProperties().setProperty("applicationContextConfig", "conf/spring/application-context.xml");
        System.getProperties().setProperty("config_locs_cp",
                "conf/spring/appContext-util.xml," + "conf/spring/appContext-configuration.xml,"
                        + "conf/spring/appContext-db-dataLayer.xml,"
                        + "conf/spring/appContext-service-orgRegister.xml,"
                        + "conf/spring/appContext-service-annualStatistic.xml,"
                        + "conf/spring/appContext-service-user.xml");
        /*
        +
            "conf/spring/appContext-security.xml"
            */
        System.getProperties().setProperty("config_locs", "");
        ApplicationContextLoaderH2.getInstance().init();

        context = ApplicationContextLoaderH2.getInstance().getApplicationContext();
        asService = (AnnualStatisticService) ApplicationContextLoaderH2.getInstance().getApplicationContext()
                .getBean("AnnualStatisticService");
        organisationUnitService = (OrganisationUnitService) ApplicationContextLoaderH2.getInstance()
                .getApplicationContext().getBean("organisationUnitService");
        if (organisationUnitService == null) {
            log.error("Couldn't find organisationUnitService bean");
        }
        loadData();
    }

    private void parseOpts(String[] args) {
        if (args == null) {
            return;
        }

        for (int i = 0; i < args.length; i++) {
            if ("-full".equals(args[i])) {
                loadMuseum = true;
                loadFagbib = true;
                loadFolkebib = true;
            } else if ("-museum".equals(args[i])) {
                loadMuseum = true;
            } else if ("-fagbib".equals(args[i])) {
                loadFagbib = true;
            } else if ("-folkebib".equals(args[i])) {
                loadFolkebib = true;
            }
        }
    }

    private void loadData() {
        loadFromExcel();
    }

    private void addMuseum(Set excelFiles) {
        excelFiles.add(new ExcelFileInfo(OrganisationTypeNameConst.MUSEUM, "museum",
                "src" + File.separator + "data" + File.separator + "museum.xls", "Ark1", 1, 3, 5));
    }

    private void addFagbib(Set excelFiles) {
        excelFiles.add(new ExcelFileInfo(OrganisationTypeNameConst.FAG_LIBRARY, "fagbib",
                "src" + File.separator + "data" + File.separator + "bibliotek_fag.xls", "Ark1", 0, 4, 0));
    }

    private void addFolkebib(Set excelFiles) {
        excelFiles.add(new ExcelFileInfo(OrganisationTypeNameConst.FYLKES_LIBRARY, "folkbib",
                "src" + File.separator + "data" + File.separator + "statistikk2003" + File.separator
                        + "statistikk2003_folkebibliotek_" + "fylkesbibliotek.xls",
                "Ark1", 0, 2, 1));
        excelFiles.add(new ExcelFileInfo(OrganisationTypeNameConst.MOBILE_LIBRARY, "folkbib",
                "src" + File.separator + "data" + File.separator + "statistikk2003" + File.separator
                        + "statistikk2003_folkebibliotek_" + "mobile.xls",
                "Ark1", 0, 2, 1));
        excelFiles.add(new ExcelFileInfo(OrganisationTypeNameConst.HIGH_SCHOOL_LIBRARY, "folkbib",
                "src" + File.separator + "data" + File.separator + "statistikk2003" + File.separator
                        + "statistikk2003_folkebibliotek_" + "videregaaendeskoler.xls",
                "Ark1", 0, 2, 1));
        excelFiles.add(new ExcelFileInfo(OrganisationTypeNameConst.FYLKE, "folkbib",
                "src" + File.separator + "data" + File.separator + "statistikk2003" + File.separator
                        + "statistikk2003_folkebibliotek_" + "fylkeskommuner_vgs.xls",
                "Ark1", 0, 2, 1));
        excelFiles.add(new ExcelFileInfo(OrganisationTypeNameConst.PUBLIC_LIBRARY, "folkbib",
                "src" + File.separator + "data" + File.separator + "statistikk2003" + File.separator
                        + "statistikk2003_folkebibliotek_" + "folkebibliotek.xls",
                "Ark1", 0, 2, 1));
        excelFiles.add(new ExcelFileInfo(OrganisationTypeNameConst.PRISON_LIBRARY, "folkbib",
                "src" + File.separator + "data" + File.separator + "statistikk2003" + File.separator
                        + "statistikk2003_folkebibliotek_" + "fengsel.xls",
                "Ark1", 1, 3, 1));
        excelFiles.add(new ExcelFileInfo(OrganisationTypeNameConst.HOSPITAL_LIBRARY, "folkbib",
                "src" + File.separator + "data" + File.separator + "statistikk2003" + File.separator
                        + "statistikk2003_folkebibliotek_" + "pasient.xls",
                "Ark1", 0, 2, 1));
        excelFiles.add(new ExcelFileInfo(OrganisationTypeNameConst.PRIMARY_SCHOOL_LIBRARY, "folkbib",
                "src" + File.separator + "data" + File.separator + "statistikk2003" + File.separator
                        + "statistikk2003_folkebibliotek_" + "grunnskoler.xls",
                "Ark1", 0, 2, 1));
        excelFiles.add(new ExcelFileInfo(OrganisationTypeNameConst.KOMMUNE, "folkbib",
                "src" + File.separator + "data" + File.separator + "statistikk2003" + File.separator
                        + "statistikk2003_folkebibliotek_" + "kommuner_grunnskoler.xls",
                "Ark1", 0, 2, 1));
    }

    /**
     * ExcelFileInfo.
     *
     */
    private class ExcelFileInfo {
        private String fileName;
        private String sheetName;
        private int headerLine;
        private String schemaShortName;
        private String organisationType;
        private short idColumn;
        private int firstDataLine;

        public ExcelFileInfo(String organisationType, String schemaShortName, String fileName, String sheetName,
                int headerLine, int firstDataLine, int idColumn) {
            this.organisationType = organisationType;
            this.schemaShortName = schemaShortName;
            this.fileName = fileName;
            this.sheetName = sheetName;
            this.headerLine = headerLine;
            this.firstDataLine = firstDataLine;
            this.idColumn = (short) idColumn;
        }

        public int hashCode() {
            return new HashCodeBuilder().append(fileName).append(organisationType).toHashCode();
        }
    }

    private void loadFromExcel() {
        Set excelFiles = new HashSet();
        if (loadMuseum) {
            addMuseum(excelFiles);
        }
        if (loadFagbib) {
            addFagbib(excelFiles);
        }
        if (loadFolkebib) {
            addFolkebib(excelFiles);
        }

        Iterator excelFileIterator = excelFiles.iterator();
        while (excelFileIterator.hasNext()) {
            ExcelFileInfo excelFileInfo = (ExcelFileInfo) excelFileIterator.next();

            ExcelWithLibraryInformationAndDataParser excelParser = new ExcelWithLibraryInformationAndDataParser();
            excelParser.setAnnualStatisticService(asService);
            excelParser.setOrganisationUnitService(organisationUnitService);
            log.debug("ParseExcel... handleRequestInternal called...");

            System.out.println("Loading " + excelFileInfo.fileName + ".");
            excelParser.setOrganisationType(excelFileInfo.organisationType);
            excelParser.setSchemaShortName(excelFileInfo.schemaShortName);
            excelParser.setSheetName(excelFileInfo.sheetName);
            excelParser.setOrganisationUnitIdColumnIdx(new Short(excelFileInfo.idColumn));
            excelParser.setFieldLine((int) excelFileInfo.headerLine);
            excelParser.setFirstDataLine(excelFileInfo.firstDataLine);
            excelParser.setExcelFileName(excelFileInfo.fileName);
            List orgUnits = excelParser.getOrganisationalUnits();

            log.info("Loaded " + orgUnits.size() + " organisation units from " + excelFileInfo.fileName);
        }
    }

    public static void main(String[] args) {
        DataLoaderH2 dataLoader = new DataLoaderH2(args);
    }

}