Author: dcosse Date: 2014-07-31 10:26:29 +0200 (Thu, 31 Jul 2014) New Revision: 3878 Url: http://forge.chorem.org/projects/lima/repository/revisions/3878 Log: refs #1032 gestion des imports des financial statement non ordonn?\195?\169s Added: trunk/lima-business-api/src/main/java/org/chorem/lima/business/NotAllowedLabel.java Modified: trunk/lima-business-api/src/main/java/org/chorem/lima/business/api/FinancialStatementService.java trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/FinancialStatementServiceImpl.java trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/NewImportServiceImpl.java trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/csv/AbstractLimaModel.java trunk/lima-business/src/test/java/org/chorem/lima/business/NewImportExportServiceTest.java trunk/lima-business/src/test/resources/import/bcr_developed.csv Modified: trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/FinancialStatementServiceImpl.java =================================================================== --- trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/FinancialStatementServiceImpl.java 2014-07-30 22:18:28 UTC (rev 3877) +++ trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/FinancialStatementServiceImpl.java 2014-07-31 08:26:29 UTC (rev 3878) @@ -41,6 +41,7 @@ import org.chorem.lima.beans.ReportsDatas; import org.chorem.lima.business.AlreadyExistFinancialTransaction; import org.chorem.lima.business.LimaException; +import org.chorem.lima.business.NotAllowedLabel; import org.chorem.lima.business.api.AccountService; import org.chorem.lima.business.api.FinancialStatementService; import org.chorem.lima.business.api.ReportService; @@ -83,7 +84,10 @@ } }; - protected void validateNewFinancialStatement(FinancialStatement masterFinancialStatement, FinancialStatement financialStatement) throws AlreadyExistFinancialTransaction { + protected void validateNewFinancialStatement(FinancialStatement masterFinancialStatement, FinancialStatement financialStatement) throws AlreadyExistFinancialTransaction, NotAllowedLabel { + if (financialStatement.getLabel().contains("/")) { + throw new NotAllowedLabel(financialStatement.getLabel()); + } if (masterFinancialStatement != null) { Collection<FinancialStatement> masterSubFinancialStatements = masterFinancialStatement.getSubFinancialStatements(); if (masterSubFinancialStatements == null) { @@ -130,7 +134,7 @@ @Override public FinancialStatement createFinancialStatement(FinancialStatement masterFinancialStatement, - FinancialStatement financialStatement) throws AlreadyExistFinancialTransaction { + FinancialStatement financialStatement) throws AlreadyExistFinancialTransaction, NotAllowedLabel { validateNewFinancialStatement(masterFinancialStatement, financialStatement); Modified: trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/NewImportServiceImpl.java =================================================================== --- trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/NewImportServiceImpl.java 2014-07-30 22:18:28 UTC (rev 3877) +++ trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/NewImportServiceImpl.java 2014-07-31 08:26:29 UTC (rev 3878) @@ -34,6 +34,7 @@ import org.chorem.lima.business.LockedEntryBookException; import org.chorem.lima.business.LockedFinancialPeriodException; import org.chorem.lima.business.MoreOneUnlockFiscalPeriodException; +import org.chorem.lima.business.NotAllowedLabel; import org.chorem.lima.business.api.AccountService; import org.chorem.lima.business.api.EntryBookService; import org.chorem.lima.business.api.EntryService; @@ -228,7 +229,7 @@ return result; } - protected FinancialStatement returnFinancialStatement (FinancialStatement rootFinancialStatement, String subFinancialStatementLabel) throws AlreadyExistFinancialTransaction { + protected FinancialStatement returnFinancialStatement (FinancialStatement rootFinancialStatement, String subFinancialStatementLabel) throws AlreadyExistFinancialTransaction, NotAllowedLabel { Collection<FinancialStatement> subFinancialStatements = rootFinancialStatement.getSubFinancialStatements(); FinancialStatement targetedFinancialStatement = null; @@ -304,6 +305,11 @@ if (rootFinancialStatement == null) { rootFinancialStatement = financialStatement; rootFinancialStatement = financialStatementService.createFinancialStatement(null, rootFinancialStatement); + } else { + // in case it exist (not ordered import and previously created) values are bind to the previously created one excepted + // the sub financial statements + Binder<FinancialStatement, FinancialStatement> rootBinder = BinderFactory.newBinder(FinancialStatement.class, FinancialStatement.class); + rootBinder.copyExcluding(financialStatement, rootFinancialStatement, FinancialStatement.PROPERTY_SUB_FINANCIAL_STATEMENTS); } orderedFinancialStatements.put(rootFinancialStatement.getLabel(), rootFinancialStatement); } else { @@ -312,6 +318,7 @@ FinancialStatement rootFinancialStatement = orderedFinancialStatements.get(rootMasterName); + // case of not ordered import and subFinancialStatement is looking for it's master that has not been created yet if (rootFinancialStatement == null) { rootFinancialStatement = financialStatementService.newFinancialStatement(); rootFinancialStatement.setLabel(rootMasterName); @@ -319,27 +326,45 @@ orderedFinancialStatements.put(rootMasterName, rootFinancialStatement); } - // 0 is root - // explore branches + // explore branches to find the financialStatement's master one FinancialStatement branchesFinancialStatement = rootFinancialStatement; - for (int i = 1; i < masterNames.length; i++) { + for (int i = 1; i < masterNames.length; i++) {// 0 is root String masterName = masterNames[i]; branchesFinancialStatement = returnFinancialStatement(branchesFinancialStatement, masterName); } - // 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); + // 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; + if (branchesFinancialStatement != null && branchesFinancialStatement.getSubFinancialStatements() != null) { + for (FinancialStatement bfs : branchesFinancialStatement.getSubFinancialStatements()) { + if (bfs.getLabel().equals(financialStatement.getLabel())){ + Binder<FinancialStatement, FinancialStatement> rootBinder = BinderFactory.newBinder(FinancialStatement.class, FinancialStatement.class); + rootBinder.copyExcluding(bfs, financialStatement, FinancialStatement.PROPERTY_SUB_FINANCIAL_STATEMENTS); + alreadyCreated = true; + } + } + } - // replace modified root financial statement with new one - if (orderedFinancialStatements.get(targetedRootFinancialStatement.getLabel()) != null) { - orderedFinancialStatements.put(targetedRootFinancialStatement.getLabel(), targetedRootFinancialStatement); + // if necessary financial statement is created + if (!alreadyCreated) { + // 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); + + // replace modified root financial statement with new one + if (orderedFinancialStatements.get(targetedRootFinancialStatement.getLabel()) != null) { + orderedFinancialStatements.put(targetedRootFinancialStatement.getLabel(), targetedRootFinancialStatement); + } } + } result.increaseCreated(); lineIndex++; } catch (AlreadyExistFinancialTransaction e) { result.getException().addException(lineIndex, e); + } catch (NotAllowedLabel e) { + result.getException().addException(lineIndex, e); } } Modified: trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/csv/AbstractLimaModel.java =================================================================== --- trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/csv/AbstractLimaModel.java 2014-07-30 22:18:28 UTC (rev 3877) +++ trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/csv/AbstractLimaModel.java 2014-07-31 08:26:29 UTC (rev 3878) @@ -355,29 +355,6 @@ } }; - // TODO DCossé 29/07/14 is there a way to do sothing like that ? -// protected static final ValueParser<FinancialStatement> MASTER_FINANCIAL_STATEMENT_LABEL_TO_MASTER_FINANCIAL_STATEMENT_PARSER = new ValueParser<FinancialStatement>() { -// -// @Override -// public FinancialStatement parse(String value) { -// FinancialStatement result = null; -// if (StringUtils.isNotBlank(value)){ -// // look for master financial statement -// // the last financialStatement is the current one. -// int index = allFinancialStatements.size() - 2; -// while (index > 0) { -// FinancialStatement masterFinancialStatement = allFinancialStatements.get(index); -// if (StringUtils.isNotBlank(masterFinancialStatement.getLabel()) && masterFinancialStatement.getLabel().equals(value)) { -// result = masterFinancialStatement; -// break; -// } -// index --; -// } -// } -// return result; -// } -// }; - protected static final ValueFormatter<FinancialStatement> MASTER_FINANCIAL_STATEMENT_TO_MASTER_FINANCIAL_STATEMENT_LABEL_FORMATTER = new ValueFormatter<FinancialStatement>() { @Override Modified: trunk/lima-business/src/test/java/org/chorem/lima/business/NewImportExportServiceTest.java =================================================================== --- trunk/lima-business/src/test/java/org/chorem/lima/business/NewImportExportServiceTest.java 2014-07-30 22:18:28 UTC (rev 3877) +++ trunk/lima-business/src/test/java/org/chorem/lima/business/NewImportExportServiceTest.java 2014-07-31 08:26:29 UTC (rev 3878) @@ -7,6 +7,7 @@ import org.chorem.lima.entity.Account; import org.chorem.lima.entity.Entry; import org.chorem.lima.entity.EntryBook; +import org.chorem.lima.entity.FinancialStatement; import org.chorem.lima.entity.FinancialTransaction; import org.chorem.lima.entity.FiscalPeriod; import org.junit.Assert; @@ -17,6 +18,7 @@ import java.io.FileOutputStream; import java.io.InputStream; import java.nio.charset.Charset; +import java.util.Collection; import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; @@ -298,6 +300,7 @@ @Test public void testImportFiscalStatementsAsCSV() throws Exception { + // not ordered csv import file String bcr_developed = IOUtils.toString(ImportServiceImplTest.class.getResourceAsStream("/import/bcr_developed.csv")); ImportResult result; @@ -307,5 +310,11 @@ Assert.assertTrue(result.getException().getAllExceptionsByLine().isEmpty()); Assert.assertEquals(162, result.getNbCreated()); Assert.assertEquals(162, financialStatementService.getAllFinancialStatements().size()); + FinancialStatement actifImmobiliseStatement = financialStatementService.getFinancialStatementByLabel("ACTIF IMMOBILISÉ"); + Collection<FinancialStatement> subFinancialStatements = actifImmobiliseStatement.getSubFinancialStatements(); + Assert.assertEquals(3, subFinancialStatements.size()); + FinancialStatement bilanActifStatement = financialStatementService.getFinancialStatementByLabel("BILAN ACTIF"); + subFinancialStatements = bilanActifStatement.getSubFinancialStatements(); + Assert.assertEquals(6, subFinancialStatements.size()); } } Modified: trunk/lima-business/src/test/resources/import/bcr_developed.csv =================================================================== --- trunk/lima-business/src/test/resources/import/bcr_developed.csv 2014-07-30 22:18:28 UTC (rev 3877) +++ trunk/lima-business/src/test/resources/import/bcr_developed.csv 2014-07-31 08:26:29 UTC (rev 3878) @@ -1,7 +1,5 @@ label;header;accounts;debitAccounts;creditAccounts;provisionDeprecationAccounts;subAmount;headerAmount;masterFinancialStatement;financialStatementWay -BILAN ACTIF;O;;;;;O;N;;BOTH Capital Souscrit non appelé;N;109; ; ; ;N;N;BILAN ACTIF; -ACTIF IMMOBILISÉ;O; ; ; ; ;O;N;BILAN ACTIF;BOTH Immobilisations incorporelles;O;;;;;N;N;BILAN ACTIF/ACTIF IMMOBILISÉ;BOTH Frais d'établissement;N;201;;;2801;N;N;BILAN ACTIF/ACTIF IMMOBILISÉ/Immobilisations incorporelles; Frais de recherche et développement;N;203;;;2803;N;N;BILAN ACTIF/ACTIF IMMOBILISÉ/Immobilisations incorporelles; @@ -161,3 +159,5 @@ Reprises sur provisions et transferts de charges;N;"787, 797";;;;N;N;COMPTE DE RESULTAT/PRODUITS/Produits exceptionnels; Sur opérations en capital;N;"775, 777, 778";;;;N;N;COMPTE DE RESULTAT/PRODUITS/Produits exceptionnels; Sur opérations de gestion;N;771;;;;N;N;COMPTE DE RESULTAT/PRODUITS/Produits exceptionnels; +ACTIF IMMOBILISÉ;O; ; ; ; ;O;N;BILAN ACTIF;BOTH +BILAN ACTIF;O;;;;"2808, 2908";;N;;BOTH Added: trunk/lima-business-api/src/main/java/org/chorem/lima/business/NotAllowedLabel.java =================================================================== --- trunk/lima-business-api/src/main/java/org/chorem/lima/business/NotAllowedLabel.java (rev 0) +++ trunk/lima-business-api/src/main/java/org/chorem/lima/business/NotAllowedLabel.java 2014-07-31 08:26:29 UTC (rev 3878) @@ -0,0 +1,17 @@ +package org.chorem.lima.business; + +/** + * Created by davidcosse on 31/07/14. + */ +public class NotAllowedLabel extends LimaException { + + protected String label; + + public NotAllowedLabel(String label) { + this.label = label; + } + + public String getLabel() { + return label; + } +} Modified: trunk/lima-business-api/src/main/java/org/chorem/lima/business/api/FinancialStatementService.java =================================================================== --- trunk/lima-business-api/src/main/java/org/chorem/lima/business/api/FinancialStatementService.java 2014-07-30 22:18:28 UTC (rev 3877) +++ trunk/lima-business-api/src/main/java/org/chorem/lima/business/api/FinancialStatementService.java 2014-07-31 08:26:29 UTC (rev 3878) @@ -28,6 +28,7 @@ import org.chorem.lima.beans.FinancialStatementAmounts; import org.chorem.lima.business.AlreadyExistFinancialTransaction; import org.chorem.lima.business.LimaException; +import org.chorem.lima.business.NotAllowedLabel; import org.chorem.lima.entity.Account; import org.chorem.lima.entity.FinancialStatement; @@ -45,7 +46,7 @@ void createMasterFinacialStatements(FinancialStatement masterFinancialStatements); FinancialStatement createFinancialStatement(FinancialStatement masterFinancialStatement, - FinancialStatement financialStatement) throws AlreadyExistFinancialTransaction; + FinancialStatement financialStatement) throws AlreadyExistFinancialTransaction, NotAllowedLabel; void updateFinancialStatement(FinancialStatement financialStatement);