Java tutorial
/*$Id: LiseImportServiceH3Impl.java 13851 2009-05-23 15:44:33Z 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.hibernate3; import java.math.BigDecimal; import java.util.ArrayList; import java.util.Collection; import java.util.Currency; import java.util.Date; import java.util.List; import java.util.Map; import no.abmu.common.domain.Entity; import no.abmu.common.persistence.hibernate3.Hibernate3Util; 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.domain.ProductCustomerRelation; import no.abmu.lise.finders.consortium.ConsortiumFinderSpecification; import no.abmu.lise.finders.productcustomerrelation.ProductCustomerRelationFinderSpecification; import no.abmu.lise.service.AbstractLiseImportService; import no.abmu.lise.util.LiseImportExcelParser; import no.abmu.organisationregister.domain.ExtendedContactPerson; import no.abmu.organisationregister.domain.OrganisationUnit; import no.abmu.organisationregister.finders.contactperson.ContactPersonFinderSpecification; import no.abmu.organisationregister.service.OrganisationRegisterBaseService; import no.abmu.organisationregister.util.DataLoaderUtils; import no.abmu.util.string.StringUtil; import no.abmu.util.test.Assert; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.springframework.util.StopWatch; /** * <code>OrganisationUnitImportServiceH2Impl</code> implements interface * OrganisationUnitImportService. * * @author Jens Vindvad, Jens.Vindvad@abm-utvikling.no * @author Erlend verby; erlend.overby@hypatia.no * @author $Author: jens $ * @version $Rev: 13851 $ * $Date: 2009-05-23 17:44:33 +0200 (Sat, 23 May 2009) $ * copyright ABM-Utvikling * */ public class LiseImportServiceH3Impl extends AbstractLiseImportService { private static final Log logger = (Log) LogFactory.getLog(LiseImportServiceH3Impl.class); private SessionFactory sessionFactory; private boolean doRollback = true; public LiseImportServiceH3Impl() { } public OrganisationRegisterBaseService getOrganisationRegisterBaseService() { return baseService; } public SessionFactory getSessionFactory() { return sessionFactory; } public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } @Override public boolean importFromExcel(String fileName, String sheetName, boolean rollback) { Assert.checkRequiredArgument("fileName", fileName); Assert.checkRequiredArgument("sheetName", sheetName); LiseImportExcelParser excelParser = createLiseExcelParser(fileName, sheetName); excelParser.init(); excelParser.load(); String messageFromInputCheck = excelParser.checkInputValues(); if (StringUtil.notEmpty(messageFromInputCheck)) { // Currently just report do not stop. System.out.println(messageFromInputCheck); logger.warn(messageFromInputCheck); //return false; } excelParser.setSheetName("Sheet1"); if (!checkForLiseCustomerOrganisationUnits(baseService, excelParser)) { String errorMessage = "Not all customers were present in organisationRegister"; logger.error(errorMessage); throw new IllegalStateException(errorMessage); } String messageToUser = "All custommers were present good."; System.out.println(messageToUser); logger.info(messageToUser); if (!checkForLiseSupplierOrganisationUnits(baseService, excelParser)) { String errorMessage = "Not all suppliers were present in organisationRegister"; logger.warn(errorMessage); } logger.info("All suppliers were present good."); // First sheet 1 --> stateLevel 1 int stateLevel = 1; boolean wentOK = stateRun(excelParser, rollback, stateLevel); if (!wentOK || doRollback) { return wentOK; } // Second sheet 2 --> stateLevel 2 stateLevel++; wentOK = stateRun(excelParser, rollback, stateLevel); if (!wentOK || doRollback) { return wentOK; } // Third sheet 3 --> statelevel 3 stateLevel++; return stateRun(excelParser, rollback, stateLevel); } private boolean stateRun(LiseImportExcelParser excelParser, boolean rollback, int stateLevel) { boolean wentOK = false; try { Session session = sessionFactory.openSession(); wentOK = runInTransaction(session, excelParser, rollback, stateLevel); } catch (HibernateException e) { logger.error("Feil gikk ikke"); logger.error(e.toString()); } if (wentOK) { logger.info("Import went OK"); } else { logger.warn("There was some errors during import .... "); } try { sessionFactory.close(); } catch (HibernateException e) { logger.info("IMPORT FROM EXCEL, COULD NOT CLOSE SESSIONFACTORY ! " + e.toString()); e.printStackTrace(); } return wentOK; } private boolean runInTransaction(Session session, LiseImportExcelParser excelParser, boolean rollback, int stateLevel) { doRollback = rollback; boolean runInTransactionWentOk = false; Transaction transaction = session.beginTransaction(); try { runInTransactionWentOk = runInTransaction(session, excelParser, stateLevel); logger.debug("runInTransaction(" + runInTransactionWentOk + ") doRollback(" + doRollback + ") rollback(" + rollback + ")"); if (!runInTransactionWentOk) { logger.info("doing rollback (" + runInTransactionWentOk + ")"); transaction.rollback(); } else if (rollback || doRollback) { logger.info("doing rollback (" + rollback + ") do(" + doRollback + ")"); transaction.rollback(); } } catch (IllegalArgumentException e) { transaction.rollback(); throw e; } catch (IllegalStateException e) { transaction.rollback(); throw e; } catch (Throwable throwable) { throwable.printStackTrace(); logger.error("doing rollback ", throwable); transaction.rollback(); } finally { if (!transaction.wasRolledBack() && (!doRollback)) { logger.debug("Transaction Commit ! "); transaction.commit(); } session.close(); } return runInTransactionWentOk; } /* * @TODO implement import * Built on OrgUnitImporter * @return TRUE of everything went without any problems */ public boolean runInTransaction(Session session, LiseImportExcelParser excelParser, int stateLevel) { excelParser.load(); logger.debug("========== IMPORT SERVICE BASESERVICE IS(" + baseService + ")"); if (stateLevel == 1) { processSheetOne(session, excelParser); } else if (stateLevel == 2) { logger.info("Processing sheet two"); precessSheetTwo(session, excelParser); } else if (stateLevel == 3) { logger.info("Processing sheet tree"); processSheetThree(session, excelParser); } else { String errorMessage = "Unknown stateLevel ='" + stateLevel + "'."; logger.error(errorMessage); throw new IllegalArgumentException(errorMessage); } return true; } private void processSheetOne(Session session, LiseImportExcelParser excelParser) { List<Entity> entityStorList = new ArrayList<Entity>(); Consortium currentConsortium = null; Product currentProduct = null; String currentProductName = null; int productSaved = 0; int productCustomerRelationSaved = 0; int productCustomerRelationNotSaved = 0; int linkToCustomerContactPerson = 0; int linkToSupplierContactPerson = 0; int missingLinkToCustomerContactPerson = 0; int missingLinkToSupplierContactPerson = 0; int numberOfCustomerProductComment = 0; excelParser.setSheetName("Sheet1"); excelParser.load(); Date creationTime = new Date(); StopWatch stopWatch = new StopWatch(); stopWatch.start("processSheetOne"); logger.debug("Starting building the Domain model:"); for (; excelParser.hasNext(); excelParser.next()) { Consortium consortiumInLine = createConsortium(excelParser); Assert.assertNotNull("consortiumInLine", consortiumInLine); if (!consortiumInLine.equals(currentConsortium)) { // New current consortium currentConsortium = consortiumInLine; String logMessage = "Row " + excelParser.getCurrentRowNumber() + " in " + excelParser.getSheetName() + " new consortium ==> " + currentConsortium.getConsortiumName(); logger.info(logMessage); // Get supplier Map<String, String> supplierExcelColumnMap = excelParser.getConsortiumSupplierOrgUnitColumns(); OrganisationUnit supplier = getOrganisationUnit(session, excelParser, supplierExcelColumnMap); if (supplier != null) { currentConsortium.setSupplier(supplier); } else { String warningMessage = "Row " + excelParser.getCurrentRowNumber() + " in " + excelParser.getSheetName() + " did not find supplier ==> " + excelParser.getSupplierName(); logger.warn(warningMessage); } // Get supplier contact person String supContactPersonName = excelParser.getConsortiumSupplierContactPerson(); ExtendedContactPerson supplierContactPerson = findContactPerson(session, supContactPersonName); if (supplierContactPerson != null) { currentConsortium.setSupplierContactPerson(supplierContactPerson); linkToSupplierContactPerson++; } else { String warningMessage = "Row " + excelParser.getCurrentRowNumber() + " in " + excelParser.getSheetName() + " no supplier contact person ==> " + supContactPersonName; logger.warn(warningMessage); missingLinkToSupplierContactPerson++; } // Get abmu administrator contact person String abmuContactPersonName = excelParser.getConsortiumAbmuContactPerson(); ExtendedContactPerson abmuContactPerson = findContactPerson(session, abmuContactPersonName); if (abmuContactPerson != null) { currentConsortium.setAbmuContactPerson(abmuContactPerson); } else { String warningMessage = "Row " + excelParser.getCurrentRowNumber() + " in " + excelParser.getSheetName() + " no ABMU contact person ==> " + abmuContactPersonName; logger.warn(warningMessage); } entityStorList.add(currentConsortium); } Product productInLine = createProduct(excelParser); Assert.assertNotNull("productInLine", productInLine); Assert.assertNotNull("productInLine.getProductName()", productInLine.getProductName()); if (!productInLine.getProductName().equals(currentProductName)) { // New current product currentProduct = productInLine; currentProductName = currentProduct.getProductName(); String logMessage = "Row " + excelParser.getCurrentRowNumber() + " in " + excelParser.getSheetName() + " new product ==> " + currentProduct.getProductName(); logger.info(logMessage); currentConsortium.addProduct(currentProduct); productSaved++; } // Get customer Map<String, String> customerExcelColumnMap = excelParser.getRelationCustomerProductOrgUnitColumns(); OrganisationUnit customer = getOrganisationUnit(session, excelParser, customerExcelColumnMap); if (customer != null) { ProductCustomerRelation productCustomerRelation = new ProductCustomerRelation(currentProduct, customer); // Get customer contact person String contactPersonName = excelParser.getRcpContactPerson(); ExtendedContactPerson customerContactPerson = findContactPerson(session, contactPersonName); if (customerContactPerson != null) { productCustomerRelation.setCustomerContactPerson(customerContactPerson); linkToCustomerContactPerson++; } else { String warningMessage = "Row " + excelParser.getCurrentRowNumber() + " in " + excelParser.getSheetName() + "] no customer contact person ==> " + contactPersonName; logger.warn(warningMessage); missingLinkToCustomerContactPerson++; } currentProduct.addProductCustomerRelations(productCustomerRelation); productCustomerRelationSaved++; LiseComment customerProductComment = getCustomerProductComment(excelParser); if (customerProductComment != null) { customerProductComment.setCreated(creationTime); customerProductComment.setLastUpdated(creationTime); productCustomerRelation.addLiseComment(customerProductComment); numberOfCustomerProductComment++; } } else { String warnMessage = "Row " + excelParser.getCurrentRowNumber() + " in " + excelParser.getSheetName() + " no customer with name (" + excelParser.getSupplierName() + ")."; logger.warn(warnMessage); productCustomerRelationNotSaved++; } } creationTime = new Date(); // // Save // Hibernate3Util.saveEntities(session, entityStorList); stopWatch.stop(); logger.info(" ============================================================================= "); logger.info("Saved [" + entityStorList.size() + "] consortium."); logger.info("Saved [" + productSaved + "] products."); logger.info("Saved [" + productCustomerRelationSaved + "] productCustomerRelations."); logger.info("Saved [" + linkToSupplierContactPerson + "] link to supplier contactPerson."); logger.info("Saved [" + linkToCustomerContactPerson + "] link to customer contactPerson."); logger.info("Saved [" + numberOfCustomerProductComment + "] customer product comments."); logger.info("Missing [" + productCustomerRelationNotSaved + "] productCustomerRelations."); logger.info("Missing [" + missingLinkToSupplierContactPerson + "] link to supplier contactPerson."); logger.info("Missing [" + missingLinkToCustomerContactPerson + "] link to customer contactPerson."); logger.info("TransactionTime [" + stopWatch.getLastTaskTimeMillis() + "] in ms."); logger.info(" ============================================================================= "); } /* * For importing of the other sheets in the spreadsheet. * Sheet2 is for payments to the suppliers * */ private void precessSheetTwo(Session session, LiseImportExcelParser excelParser) { List<Entity> entityStorList = new ArrayList<Entity>(); int noConsortium = 0; int noInvoiceNumber = 0; excelParser.setSheetName("Sheet2"); excelParser.load(); Date creationTime = new Date(); StopWatch stopWatch = new StopWatch(); stopWatch.start("precessSheetTwo"); logger.debug("Starting building the Domain model:"); for (; excelParser.hasNext(); excelParser.next()) { Consortium consortiumFromDb = findConsortiumInDb(excelParser); if (consortiumFromDb != null) { Consortium consortiumInSession = (Consortium) session.get(Consortium.class, consortiumFromDb.getId()); String logMessage = "Row " + excelParser.getCurrentRowNumber() + " in " + excelParser.getSheetName() + " prosessing consortium ==> " + consortiumInSession.getConsortiumName(); logger.info(logMessage); PaymentToSupplier paymentToSupplier = createPaymentToSupplier(excelParser); if (paymentToSupplier.getLiseComment() != null) { paymentToSupplier.getLiseComment().setCreated(creationTime); paymentToSupplier.getLiseComment().setLastUpdated(creationTime); } if (StringUtil.isEmpty(paymentToSupplier.getInvoiceNumber())) { noInvoiceNumber++; String errorMessage = "Row " + excelParser.getCurrentRowNumber() + " no invoice number for consortium " + consortiumInSession.getConsortiumName(); logger.error(errorMessage); paymentToSupplier.setInvoiceNumber("DUMMY"); } consortiumInSession.addPaymentToSupplier(paymentToSupplier); entityStorList.add(consortiumInSession); } else { noConsortium++; } } creationTime = new Date(); // // Save // Hibernate3Util.saveEntities(session, entityStorList); stopWatch.stop(); logger.info(" ============================================================================= "); logger.info("Saving [" + entityStorList.size() + "] consortium with payment to supplier infomation."); logger.info("Payment to supplier with wissing [" + noInvoiceNumber + "] invoice number."); logger.info("Missing [" + noConsortium + "] consortium with payment to supplier infomation."); logger.info("TransactionTime [" + stopWatch.getLastTaskTimeMillis() + "] in ms."); logger.info(" ============================================================================= "); } private void processSheetThree(Session session, LiseImportExcelParser excelParser) { List<Entity> entityStorList = new ArrayList<Entity>(); int noProductCustomerRelation = 0; excelParser.setSheetName("Sheet3"); excelParser.load(); Date creationTime = new Date(); StopWatch stopWatch = new StopWatch(); stopWatch.start("processSheetThree"); for (; excelParser.hasNext(); excelParser.next()) { ProductCustomerRelation productCustomerFromDb = findProductCustomerRelInDb(excelParser); if (productCustomerFromDb != null) { ProductCustomerRelation productCustomerRelInSession = (ProductCustomerRelation) session .get(ProductCustomerRelation.class, productCustomerFromDb.getId()); InvoiceToCustomer invoiceToCustomer = createInvoiceToCustomer(excelParser); // Make sure we have currency on all invoices to customer if (invoiceToCustomer.getCurrency() == null) { Product product = productCustomerRelInSession.getProduct(); Consortium consortium = product.getConsortium(); invoiceToCustomer.setCurrency(consortium.getCurrency()); } // Setting up percentage currency buffer if (invoiceToCustomer.getCurrency().equals(Currency.getInstance("NOK"))) { invoiceToCustomer.setPercentageCurrencyBuffer(BigDecimal.valueOf(0)); } else { invoiceToCustomer.setPercentageCurrencyBuffer(BigDecimal.valueOf(1.75)); } // Be sure there are original amount if (invoiceToCustomer.getOriginalAmount() == null) { invoiceToCustomer.setOriginalAmount(BigDecimal.valueOf(0)); } if (invoiceToCustomer.getLiseComment() != null) { invoiceToCustomer.getLiseComment().setCreated(creationTime); invoiceToCustomer.getLiseComment().setLastUpdated(creationTime); } productCustomerRelInSession.addInvoicesToCustomer(invoiceToCustomer); entityStorList.add(productCustomerRelInSession); } else { noProductCustomerRelation++; } } creationTime = new Date(); // // Save // Hibernate3Util.saveEntities(session, entityStorList); stopWatch.stop(); logger.info(" ============================================================================= "); logger.info("Saving [" + entityStorList.size() + "] product customer relations with invoice information."); logger.info( "Missing [" + noProductCustomerRelation + "] product customer relations with invoice information."); logger.info("TransactionTime [" + stopWatch.getLastTaskTimeMillis() + "] in ms."); logger.info(" ============================================================================= "); } private ExtendedContactPerson findContactPerson(Session session, String personName) { ContactPersonFinderSpecification contactPersonFinderSpecification = new ContactPersonFinderSpecification(); contactPersonFinderSpecification.setContactPersonName(personName); int numPersons = baseService.findCount(contactPersonFinderSpecification); List<ExtendedContactPerson> contactPersons = (List<ExtendedContactPerson>) baseService .find(contactPersonFinderSpecification); int size = contactPersons.size(); if (size > 1) { logger.error("We found more than one contact person with name (" + personName + ")"); return null; } if (size == 0) { // logger.error("No contact person with name (" + personName +") in system!"); return null; } ExtendedContactPerson extendedContactPersonFromExcel = contactPersons.get(0); Long contactPersonId = extendedContactPersonFromExcel.getId(); return (ExtendedContactPerson) session.get(ExtendedContactPerson.class, contactPersonId); } private OrganisationUnit getOrganisationUnit(Session session, LiseImportExcelParser excelParser, Map<String, String> orgUnitExcelColumnMap) { OrganisationUnit orgUnitFromExcel = DataLoaderUtils.checkForAndGetOneAndOnlyOneOrganisationUnit(excelParser, baseService, orgUnitExcelColumnMap, true); if (orgUnitFromExcel == null) { return null; } Long orgUnitId = orgUnitFromExcel.getId(); if (orgUnitId == null) { return null; } return (OrganisationUnit) session.get(OrganisationUnit.class, orgUnitId); } private Consortium findConsortiumInDb(LiseImportExcelParser excelParser) { String consortiumName = excelParser.getConsortiumName(); Date agrementStartDate = excelParser.getConsortiumAgreementStartDate(); Date agrementEndDate = excelParser.getConsortiumAgreementEndDate(); if (agrementStartDate == null) { String warningMessage = "Row " + excelParser.getCurrentRowNumber() + " in " + excelParser.getSheetName() + " has no agreementStartDate"; logger.warn(warningMessage); return null; } ConsortiumFinderSpecification finderSpecification = new ConsortiumFinderSpecification(consortiumName); Collection<Consortium> consortiumCollection = baseService.find(finderSpecification); if (consortiumCollection.size() == 0) { String warningMessage = "Row " + excelParser.getCurrentRowNumber() + " in " + excelParser.getSheetName() + " no consortium in DB with name: " + consortiumName; logger.warn(warningMessage); return null; } for (Consortium consortium : consortiumCollection) { if (agrementStartDate.equals(consortium.getConsortiumAgreementStartDate())) { if (agrementEndDate == null) { if (consortium.getConsortiumAgreementEndDate() == null) { return consortium; } } else if (agrementEndDate.equals(consortium.getConsortiumAgreementEndDate())) { return consortium; } } } String warningMessage = "Row " + excelParser.getCurrentRowNumber() + " in " + excelParser.getSheetName() + " no consortium with name: '" + consortiumName + "', startDate: '" + agrementStartDate + "' and endDate: '" + agrementEndDate + "'."; logger.warn(warningMessage); return null; } private ProductCustomerRelation findProductCustomerRelInDb(LiseImportExcelParser excelParser) { String productName = excelParser.getProductName(); ProductCustomerRelationFinderSpecification finderSpecification = new ProductCustomerRelationFinderSpecification( productName); Collection<ProductCustomerRelation> productCustomerRelations = baseService.find(finderSpecification); if (productCustomerRelations.size() == 0) { String warningMessage = "Row " + excelParser.getCurrentRowNumber() + " in " + excelParser.getSheetName() + " no productCustomerRelation for product: '" + productName + "'"; logger.warn(warningMessage); return null; } String customerName = excelParser.getCustomerName(); for (ProductCustomerRelation productCustomerRelation : productCustomerRelations) { OrganisationUnit customer = productCustomerRelation.getCustomer(); if (customer.getName().getDefault().equals(customerName)) { return productCustomerRelation; } else if (customer.getName().getReference().equals(customerName)) { return productCustomerRelation; } } String warningMessage = "Row " + excelParser.getCurrentRowNumber() + " in " + excelParser.getSheetName() + " no productCustomerRelation in DB for customer: '" + customerName + "'."; logger.warn(warningMessage); return null; } }