Author: dcosse Date: 2014-09-29 15:16:00 +0200 (Mon, 29 Sep 2014) New Revision: 3928 Url: http://forge.chorem.org/projects/lima/repository/revisions/3928 Log: refs #1032 refactoring sur service d'import export Modified: trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/ImportServiceImpl.java Modified: trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/ImportServiceImpl.java =================================================================== --- trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/ImportServiceImpl.java 2014-09-29 12:36:53 UTC (rev 3927) +++ trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/ImportServiceImpl.java 2014-09-29 13:16:00 UTC (rev 3928) @@ -328,29 +328,32 @@ return results; } - protected FinancialStatement returnFinancialStatement (FinancialStatement rootFinancialStatement, String subFinancialStatementLabel) throws AlreadyExistFinancialStatement, NotAllowedLabelException { - Collection<FinancialStatement> subFinancialStatements = rootFinancialStatement.getSubFinancialStatements(); + protected FinancialStatement returnFinancialStatement (final FinancialStatement rootFinancialStatement, String subFinancialStatementLabel) throws AlreadyExistFinancialStatement, NotAllowedLabelException { FinancialStatement targetedFinancialStatement = null; + if (rootFinancialStatement != null) { + Collection<FinancialStatement> subFinancialStatements = rootFinancialStatement.getSubFinancialStatements(); - // look for financial statement from tree range - if (subFinancialStatements != null) { - for (FinancialStatement subFinancialStatement : subFinancialStatements) { - if(subFinancialStatement.getLabel().equals(subFinancialStatementLabel)){ - targetedFinancialStatement = subFinancialStatement; - break; + // look for financial statement from tree range + if (subFinancialStatements != null) { + for (FinancialStatement subFinancialStatement : subFinancialStatements) { + if(subFinancialStatement.getLabel().equals(subFinancialStatementLabel)){ + targetedFinancialStatement = subFinancialStatement; + break; + } } } + + // the target financialStatement has not been created yet so we create it now. + if (targetedFinancialStatement == null) { + // not found, we need to create it + targetedFinancialStatement = financialStatementService.newFinancialStatement(); + targetedFinancialStatement.setLabel(subFinancialStatementLabel); + // create targetedFinancialStatement and rootFinancialStatement if needed + targetedFinancialStatement = financialStatementService.createFinancialStatement(rootFinancialStatement, targetedFinancialStatement); + targetedFinancialStatement.getMasterFinancialStatement(); + } } - // the target financialStatement has not been created yet so we create it now. - if (targetedFinancialStatement == null) { - // not found, we need to create it - targetedFinancialStatement = financialStatementService.newFinancialStatement(); - targetedFinancialStatement.setLabel(subFinancialStatementLabel); - // create targetedFinancialStatement and rootFinancialStatement if needed - targetedFinancialStatement = financialStatementService.createFinancialStatement(rootFinancialStatement, targetedFinancialStatement); - targetedFinancialStatement.getMasterFinancialStatement(); - } return targetedFinancialStatement; } @@ -407,7 +410,7 @@ return results; } - protected void processFinancialStatementImport(ImportResult result, Map<String, FinancialStatement> orderedFinancialStatements, FinancialStatementImport financialStatementBean) { + protected void processFinancialStatementImport(ImportResult result, Map<String, FinancialStatement> orderedFinancialStatements, final FinancialStatementImport financialStatementBean) { Binder<FinancialStatementImport, FinancialStatement> binder = BinderFactory.newBinder(FinancialStatementImport.class, FinancialStatement.class); FinancialStatement financialStatement = financialStatementService.newFinancialStatement(); binder.copyExcluding(financialStatementBean, financialStatement, FinancialStatement.PROPERTY_MASTER_FINANCIAL_STATEMENT); @@ -437,8 +440,7 @@ // in case it exist (not ordered import and previously created) values are bind to the previously created one excepted // the sub financial statements - boolean alreadyCreated = false; - alreadyCreated = propagetChangesToExistingFinancialStatement(financialStatement, branchesFinancialStatement, alreadyCreated); + boolean alreadyCreated = propagetChangesToExistingFinancialStatement(financialStatement, branchesFinancialStatement, false); // if necessary financial statement is created if (!alreadyCreated) { @@ -452,7 +454,7 @@ } } - private FinancialStatement getRootFinancialStatement(Map<String, FinancialStatement> orderedFinancialStatements, FinancialStatementImport financialStatementBean, FinancialStatement financialStatement) throws AlreadyExistFinancialStatement, NotAllowedLabelException { + private FinancialStatement getRootFinancialStatement(Map<String, FinancialStatement> orderedFinancialStatements, final FinancialStatementImport financialStatementBean, final FinancialStatement financialStatement) throws AlreadyExistFinancialStatement, NotAllowedLabelException { FinancialStatement rootFinancialStatement = orderedFinancialStatements.get(financialStatementBean.getLabel()); if (rootFinancialStatement == null) { @@ -467,18 +469,22 @@ return rootFinancialStatement; } - private FinancialStatement getBrancheFinancialStatement(String[] masterNames, FinancialStatement branchesFinancialStatement) throws AlreadyExistFinancialStatement, NotAllowedLabelException { - for (int i = 1; i < masterNames.length; i++) {// 0 is root - String masterName = masterNames[i]; - branchesFinancialStatement = returnFinancialStatement(branchesFinancialStatement, masterName); + private FinancialStatement getBrancheFinancialStatement(String[] masterNames, final FinancialStatement branchesFinancialStatement) throws AlreadyExistFinancialStatement, NotAllowedLabelException { + FinancialStatement _branchesFinancialStatement = branchesFinancialStatement; + if (_branchesFinancialStatement != null) { + // 0 is root, start from 1 + for (int i = 1; i < masterNames.length; i++) { + String masterName = masterNames[i]; + _branchesFinancialStatement = returnFinancialStatement(_branchesFinancialStatement, masterName); + } } - return branchesFinancialStatement; + return _branchesFinancialStatement; } - protected void createRootFinancialStatement(Map<String, FinancialStatement> orderedFinancialStatements, FinancialStatement financialStatement, FinancialStatement branchesFinancialStatement) throws AlreadyExistFinancialStatement, NotAllowedLabelException { + protected void createRootFinancialStatement(Map<String, FinancialStatement> orderedFinancialStatements, final FinancialStatement financialStatement, final FinancialStatement branchesFinancialStatement) throws AlreadyExistFinancialStatement, NotAllowedLabelException { // if the master finacial statement has been modified then the current one is replace by the new one. - financialStatement = financialStatementService.createFinancialStatement(branchesFinancialStatement, financialStatement); - FinancialStatement targetedRootFinancialStatement = returnRootFinancialStatement(financialStatement); + FinancialStatement _financialStatement = financialStatementService.createFinancialStatement(branchesFinancialStatement, financialStatement); + FinancialStatement targetedRootFinancialStatement = returnRootFinancialStatement(_financialStatement); // replace modified root financial statement with new one if (orderedFinancialStatements.get(targetedRootFinancialStatement.getLabel()) != null) { @@ -486,7 +492,7 @@ } } - protected boolean propagetChangesToExistingFinancialStatement(FinancialStatement financialStatement, FinancialStatement branchesFinancialStatement, boolean alreadyCreated) { + protected boolean propagetChangesToExistingFinancialStatement(final FinancialStatement financialStatement, final FinancialStatement branchesFinancialStatement, boolean alreadyCreated) { if (branchesFinancialStatement != null && branchesFinancialStatement.getSubFinancialStatements() != null) { for (FinancialStatement bfs : branchesFinancialStatement.getSubFinancialStatements()) { if (bfs.getLabel().equals(financialStatement.getLabel())){ @@ -500,51 +506,59 @@ return alreadyCreated; } - protected FinancialStatement createRootFinancialStatement(Map<String, FinancialStatement> orderedFinancialStatements, String rootMasterName, FinancialStatement rootFinancialStatement) throws AlreadyExistFinancialStatement, NotAllowedLabelException { - if (rootFinancialStatement == null) { - rootFinancialStatement = financialStatementService.newFinancialStatement(); - rootFinancialStatement.setLabel(rootMasterName); - rootFinancialStatement = financialStatementService.createFinancialStatement(null, rootFinancialStatement); - orderedFinancialStatements.put(rootMasterName, rootFinancialStatement); + protected FinancialStatement createRootFinancialStatement(Map<String, FinancialStatement> orderedFinancialStatements, String rootMasterName, final FinancialStatement rootFinancialStatement) throws AlreadyExistFinancialStatement, NotAllowedLabelException { + FinancialStatement _rootFinancialStatement = rootFinancialStatement; + if (_rootFinancialStatement == null) { + _rootFinancialStatement = financialStatementService.newFinancialStatement(); + _rootFinancialStatement.setLabel(rootMasterName); + _rootFinancialStatement = financialStatementService.createFinancialStatement(null, _rootFinancialStatement); + orderedFinancialStatements.put(rootMasterName, _rootFinancialStatement); } - return rootFinancialStatement; + return _rootFinancialStatement; } - protected VatStatement returnVATStatement (VatStatement rootVATStatement, String subVATStatementLabel) throws AlreadyExistVatStatementException, NotAllowedLabelException { - Collection<VatStatement> subVatStatements = rootVATStatement.getSubVatStatements(); + protected VatStatement returnVATStatement (final VatStatement rootVATStatement, String subVATStatementLabel) throws AlreadyExistVatStatementException, NotAllowedLabelException { VatStatement targetedVATStatement = null; + if (rootVATStatement != null) { + Collection<VatStatement> subVatStatements = rootVATStatement.getSubVatStatements(); - // look for vatStatement from tree range - if (subVatStatements != null) { - for (VatStatement subVatStatement : subVatStatements) { - if(subVatStatement.getLabel().equals(subVATStatementLabel)){ - targetedVATStatement = subVatStatement; - break; + // look for vatStatement from tree range + if (subVatStatements != null) { + for (VatStatement subVatStatement : subVatStatements) { + if(subVatStatement.getLabel().equals(subVATStatementLabel)){ + targetedVATStatement = subVatStatement; + break; + } } } + + if (targetedVATStatement == null) { + // not found, we need to create it + targetedVATStatement = vatStatementService.newVatStatement(); + targetedVATStatement.setLabel(subVATStatementLabel); + // create targetedVATStatement and rootVATStatement if needed + targetedVATStatement = vatStatementService.createVatStatement(rootVATStatement, targetedVATStatement); + targetedVATStatement.getMasterVatStatement(); + } } - // - if (targetedVATStatement == null) { - // not found, we need to create it - targetedVATStatement = vatStatementService.newVatStatement(); - targetedVATStatement.setLabel(subVATStatementLabel); - // create targetedVATStatement and rootVATStatement if needed - targetedVATStatement = vatStatementService.createVatStatement(rootVATStatement, targetedVATStatement); - targetedVATStatement.getMasterVatStatement(); - } return targetedVATStatement; } - protected VatStatement returnRootVATStatement(VatStatement vatStatement) { + protected VatStatement returnRootVATStatement(final VatStatement vatStatement) { VatStatement rootVatStatement = null; - while (rootVatStatement == null) { - if (vatStatement.getMasterVatStatement() == null){ - rootVatStatement = vatStatement; - } else { - vatStatement = vatStatement.getMasterVatStatement(); + VatStatement _vatStatement = vatStatement; + + if (_vatStatement != null) { + while (rootVatStatement == null) { + if (_vatStatement.getMasterVatStatement() == null){ + rootVatStatement = _vatStatement; + } else { + _vatStatement = _vatStatement.getMasterVatStatement(); + } } } + return rootVatStatement; } @@ -581,7 +595,7 @@ return results; } - protected void importVatStatement(ImportResult result, Map<String, VatStatement> orderedVATStatements, VatStatementImport vatStatementBean) { + protected void importVatStatement(ImportResult result, Map<String, VatStatement> orderedVATStatements, final VatStatementImport vatStatementBean) { Binder<VatStatementImport, VatStatement> binder = BinderFactory.newBinder(VatStatementImport.class, VatStatement.class); VatStatement vatStatement = vatStatementService.newVatStatement(); binder.copyExcluding(vatStatementBean, vatStatement, VatStatement.PROPERTY_MASTER_VAT_STATEMENT); @@ -611,8 +625,7 @@ // in case it exist (not ordered import and previously created) values are bind to the previously created one excepted // the sub vatStatements - boolean alreadyCreated = false; - alreadyCreated = propagateVATChangeToBranche(vatStatement, branchesVATStatement, alreadyCreated); + boolean alreadyCreated = propagateVATChangeToBranche(vatStatement, branchesVATStatement, false); // if necessary vatStatement is created if (!alreadyCreated) { @@ -627,10 +640,10 @@ } } - private void refreshMasterVATStatement(Map<String, VatStatement> orderedVATStatements, VatStatement vatStatement, VatStatement branchesVATStatement) throws AlreadyExistVatStatementException, NotAllowedLabelException { + private void refreshMasterVATStatement(Map<String, VatStatement> orderedVATStatements, final VatStatement vatStatement, final VatStatement branchesVATStatement) throws AlreadyExistVatStatementException, NotAllowedLabelException { // if the master vatStatement has been modified then the current one is replace by the new one. - vatStatement = vatStatementService.createVatStatement(branchesVATStatement, vatStatement); - VatStatement targetedRootVATStatement = returnRootVATStatement(vatStatement); + VatStatement _vatStatement = vatStatementService.createVatStatement(branchesVATStatement, vatStatement); + VatStatement targetedRootVATStatement = returnRootVATStatement(_vatStatement); // replace modified root vatStatement with new one if (orderedVATStatements.get(targetedRootVATStatement.getLabel()) != null) { @@ -638,7 +651,7 @@ } } - private boolean propagateVATChangeToBranche(VatStatement vatStatement, VatStatement branchesVATStatement, boolean alreadyCreated) { + private boolean propagateVATChangeToBranche(final VatStatement vatStatement, final VatStatement branchesVATStatement, boolean alreadyCreated) { if (branchesVATStatement != null && branchesVATStatement.getSubVatStatements() != null) { for (VatStatement bfs : branchesVATStatement.getSubVatStatements()) { if (bfs.getLabel().equals(vatStatement.getLabel())){ @@ -652,36 +665,43 @@ return alreadyCreated; } - private VatStatement getBrancheVatStatement(String[] masterNames, VatStatement branchesVATStatement) throws AlreadyExistVatStatementException, NotAllowedLabelException { + private VatStatement getBrancheVatStatement(String[] masterNames, final VatStatement branchesVATStatement) throws AlreadyExistVatStatementException, NotAllowedLabelException { + VatStatement _branchesVATStatement = branchesVATStatement; for (int i = 1; i < masterNames.length; i++) {// 0 is root String masterName = masterNames[i]; - branchesVATStatement = returnVATStatement(branchesVATStatement, masterName); + _branchesVATStatement = returnVATStatement(_branchesVATStatement, masterName); } - return branchesVATStatement; + return _branchesVATStatement; } - private VatStatement createRootVATStatement(Map<String, VatStatement> orderedVATStatements, String rootMasterName, VatStatement rootVATStatement) throws AlreadyExistVatStatementException, NotAllowedLabelException { - if (rootVATStatement == null) { - rootVATStatement = vatStatementService.newVatStatement(); - rootVATStatement.setLabel(rootMasterName); - rootVATStatement = vatStatementService.createVatStatement(null, rootVATStatement); - orderedVATStatements.put(rootMasterName, rootVATStatement); + private VatStatement createRootVATStatement(Map<String, VatStatement> orderedVATStatements, String rootMasterName, final VatStatement rootVATStatement) throws AlreadyExistVatStatementException, NotAllowedLabelException { + VatStatement result = rootVATStatement; + if (result == null) { + result = vatStatementService.newVatStatement(); + result.setLabel(rootMasterName); + result = vatStatementService.createVatStatement(null, result); + orderedVATStatements.put(rootMasterName, result); } - return rootVATStatement; + return result; } - private VatStatement getRootVatStatement(Map<String, VatStatement> orderedVATStatements, VatStatementImport vatStatementBean, VatStatement vatStatement) throws AlreadyExistVatStatementException, NotAllowedLabelException { - VatStatement rootVATStatement = orderedVATStatements.get(vatStatementBean.getLabel()); + private VatStatement getRootVatStatement(Map<String, VatStatement> orderedVATStatements, final VatStatementImport vatStatementBean, final VatStatement vatStatement) throws AlreadyExistVatStatementException, NotAllowedLabelException { - if (rootVATStatement == null) { - rootVATStatement = vatStatement; - rootVATStatement = vatStatementService.createVatStatement(null, rootVATStatement); - } else { - // in case it exist (not ordered import and previously created) values are bind to the previously created one excepted - // the sub vatStatements - Binder<VatStatement, VatStatement> rootBinder = BinderFactory.newBinder(VatStatement.class, VatStatement.class); - rootBinder.copyExcluding(vatStatement, rootVATStatement, VatStatement.PROPERTY_SUB_VAT_STATEMENTS); + VatStatement rootVATStatement = null; + if (vatStatementBean != null) { + rootVATStatement = orderedVATStatements.get(vatStatementBean.getLabel()); + + if (rootVATStatement == null) { + rootVATStatement = vatStatement; + rootVATStatement = vatStatementService.createVatStatement(null, rootVATStatement); + } else { + // in case it exist (not ordered import and previously created) values are bind to the previously created one excepted + // the sub vatStatements + Binder<VatStatement, VatStatement> rootBinder = BinderFactory.newBinder(VatStatement.class, VatStatement.class); + rootBinder.copyExcluding(vatStatement, rootVATStatement, VatStatement.PROPERTY_SUB_VAT_STATEMENTS); + } } + return rootVATStatement; } @@ -805,7 +825,7 @@ return result; } - protected boolean validEntry(ImportResult importResult, Date dateEcr, Date fiscalPeriodsBiginDate, Date fiscalPeriodsEndingDate, Account account, String targetedAccount) { + protected boolean validEntry(ImportResult importResult, Date dateEcr, Date fiscalPeriodsBiginDate, Date fiscalPeriodsEndingDate, final Account account, String targetedAccount) { boolean result = true; // if entry date have fiscalperiod open if (dateEcr.compareTo(fiscalPeriodsBiginDate) < 0 @@ -898,7 +918,7 @@ return results; } - protected Entry createEntry(EntryEBP entryEBP, Account account) { + protected Entry createEntry(EntryEBP entryEBP, final Account account) { Entry entry; BigDecimal debit; entry = financialTransactionService.createNewEntry();