Java tutorial
/*$Id: AbstractLiseImportService.java 12789 2009-01-29 07:28:36Z jens $*/ /* **************************************************************************** * * * (c) Copyright 2008 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.lise.service; import java.util.LinkedHashSet; import java.util.Map; import java.util.Set; import no.abmu.common.excel.ExcelParser; import no.abmu.lise.domain.Consortium; import no.abmu.lise.domain.InvoiceToCustomer; import no.abmu.lise.domain.LiseComment; import no.abmu.lise.domain.PaymentToSupplier; import no.abmu.lise.domain.Product; import no.abmu.lise.util.LiseImportExcelParser; import no.abmu.organisationregister.domain.OrganisationUnit; import no.abmu.organisationregister.service.OrganisationRegisterBaseService; import no.abmu.organisationregister.util.DataLoaderUtils; import no.abmu.util.test.Assert; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.util.StopWatch; /** * <code>AbstractLiseImportService</code> implements interface LiseImportService. * Based on AbstractOrgansiationUnitImportService * * @author Jens Vindvad, Jens.Vindvad@abm-utvikling.no * @author Erlend verby; erlend.overby@hypatia.no * @author $Author: jens $ * @version $Rev: 12789 $ * $Date: 2009-01-29 08:28:36 +0100 (Thu, 29 Jan 2009) $ * copyright ABM-Utvikling * */ public abstract class AbstractLiseImportService implements LiseImportService { private static final Log logger = (Log) LogFactory.getLog(AbstractLiseImportService.class); protected OrganisationRegisterBaseService baseService; public abstract boolean importFromExcel(String fileName, String sheetName, boolean doRollback); public void setOrganisationRegisterBaseService(OrganisationRegisterBaseService baseService) { this.baseService = baseService; } protected LiseImportExcelParser createLiseExcelParser(String fileName, String sheetName) { Assert.checkRequiredArgument("fileName", fileName); Assert.checkRequiredArgument("sheetName", sheetName); logger.info("Opening file [" + fileName + "] and using sheet [" + sheetName + "]"); LiseImportExcelParser excelParser = new LiseImportExcelParser(); excelParser.setOrganisationRegisterBaseService(getOrganisationRegisterBaseService()); excelParser.setExcelFileName(fileName); excelParser.setSheetName(sheetName); return excelParser; } protected boolean checkForLiseSupplierOrganisationUnits(OrganisationRegisterBaseService baseService, LiseImportExcelParser excelParser) { Map<String, String> excelColumnNames = excelParser.getConsortiumSupplierOrgUnitColumns(); boolean printWarningMessage = true; return checkOrganisationUnits(baseService, excelParser, excelColumnNames, printWarningMessage); } protected boolean checkForLiseCustomerOrganisationUnits(OrganisationRegisterBaseService baseService, LiseImportExcelParser excelParser) { Map<String, String> excelColumnNames = excelParser.getRelationCustomerProductOrgUnitColumns(); boolean printWarningMessage = true; return checkOrganisationUnits(baseService, excelParser, excelColumnNames, printWarningMessage); } protected Consortium createConsortium(LiseImportExcelParser excelParser) { return excelParser.createConsortium(); } protected Product createProduct(LiseImportExcelParser excelParser) { return excelParser.createProduct(); } protected PaymentToSupplier createPaymentToSupplier(LiseImportExcelParser excelParser) { return excelParser.createPaymentToSupplier(); } protected InvoiceToCustomer createInvoiceToCustomer(LiseImportExcelParser excelParser) { return excelParser.createInvoiceToCustomer(); } protected LiseComment getCustomerProductComment(LiseImportExcelParser excelParser) { return excelParser.getCustomerProductComment(); } protected LiseComment getPaymentToSupplierComment(LiseImportExcelParser excelParser) { return excelParser.getPaymentToSupplierComment(); } protected LiseComment getInvoiceToCustomerComment(LiseImportExcelParser excelParser) { return excelParser.getInvoiceToCustomerComment(); } private boolean checkOrganisationUnits(OrganisationRegisterBaseService baseService, ExcelParser excelParser, Map<String, String> excelColumnNames, boolean printWarningMessage) { StopWatch stopWatch = new StopWatch(); stopWatch.start(); int rowsWithOutMatch = 0; int numOfRowsWithData = excelParser.countNumberOfRowsInSheetBeforeStopTag(); // Get organisationUnit column name String organisationUnitExcelColumnName = excelColumnNames .get(DataLoaderUtils.KEY_ORGANISATIONUNIT_NAME_EXCEL_COLUMN); // First get a unique set of organisationUnit names Set<String> organisationUnitNames = new LinkedHashSet<String>(); Set<String> foundOrganisationUnitNames = new LinkedHashSet<String>(); Set<String> notFoundOrganisationUnitNames = new LinkedHashSet<String>(); excelParser.load(); for (; excelParser.hasNext(); excelParser.next()) { String orgUnitName = excelParser.getCellValueAsString(organisationUnitExcelColumnName); organisationUnitNames.add(orgUnitName); OrganisationUnit organisationUnit = DataLoaderUtils.checkForAndGetOneAndOnlyOneOrganisationUnit( excelParser, baseService, excelColumnNames, printWarningMessage); if (organisationUnit != null) { foundOrganisationUnitNames.add(orgUnitName); } else { notFoundOrganisationUnitNames.add(orgUnitName); rowsWithOutMatch++; } } // Report result in log. String resultToLog = "The processed excel sheet had " + numOfRowsWithData + " rows with " + organisationUnitNames.size() + " distinkt organisationUnitNames, of which " + foundOrganisationUnitNames.size() + " have exact match, and " + notFoundOrganisationUnitNames.size() + " do not have exact match. We have " + rowsWithOutMatch + " rows without match."; logger.info(resultToLog); // System.out.println(resultToLog); stopWatch.stop(); String timeMessage = "Testing for organisationUnit matches tok " + stopWatch.getTotalTimeMillis() + " ms."; logger.info(timeMessage); // System.out.println(timeMessage); return (rowsWithOutMatch == 0); } }