Author: dcosse Date: 2014-07-28 14:06:55 +0200 (Mon, 28 Jul 2014) New Revision: 3868 Url: http://forge.chorem.org/projects/lima/repository/revisions/3868 Log: refs #1032 utilisation des services metier propre aux entit?\195?\169es dans le service de migration Added: trunk/lima-business-api/src/main/java/org/chorem/lima/business/ImportException.java trunk/lima-business-api/src/main/java/org/chorem/lima/business/ImportResult.java Modified: trunk/lima-business-api/src/main/java/org/chorem/lima/business/api/AccountService.java trunk/lima-business-api/src/main/java/org/chorem/lima/business/api/EntryBookService.java trunk/lima-business-api/src/main/java/org/chorem/lima/business/api/FiscalPeriodService.java trunk/lima-business-api/src/main/java/org/chorem/lima/business/api/NewImportService.java trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/AccountServiceImpl.java trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/EntryBookServiceImpl.java trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/FiscalPeriodServiceImpl.java trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/ImportServiceImpl.java trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/NewExportServiceImpl.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/AbstractLimaTest.java trunk/lima-business/src/test/java/org/chorem/lima/business/FinancialTransactionServiceImplTest.java trunk/lima-business/src/test/java/org/chorem/lima/business/NewImportExportServiceTest.java trunk/lima-business/src/test/java/org/chorem/lima/business/ReportServiceImplTest.java trunk/lima-swing/src/main/java/org/chorem/lima/ui/importexport/ImportExport.java trunk/lima-swing/src/main/resources/i18n/lima-swing_en_GB.properties trunk/lima-swing/src/main/resources/i18n/lima-swing_fr_FR.properties Modified: trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/AccountServiceImpl.java =================================================================== --- trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/AccountServiceImpl.java 2014-07-25 08:47:33 UTC (rev 3867) +++ trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/AccountServiceImpl.java 2014-07-28 12:06:55 UTC (rev 3868) @@ -67,7 +67,22 @@ return result; } + @Override + public boolean createOrUbdateAccount(Account account) throws InvalidAccountNumberException { + // check if account number already exist + AccountTopiaDao accountDao = getDaoHelper().getAccountDao(); + boolean result = accountDao.forNaturalId(account.getAccountNumber().toUpperCase().trim()).exists(); + + if (result) { + // update + updateAccount(account); + } else { + createNewAccount(account); + } + return result; + } + /** * Permet de créer un nouveau compte dans le PCG de l'application. * @@ -77,6 +92,19 @@ @Override public Account createAccount(Account account) throws AlreadyExistAccountException, InvalidAccountNumberException { + // check if account number already exist + AccountTopiaDao accountDao = getDaoHelper().getAccountDao(); + + if (accountDao.forNaturalId(account.getAccountNumber()).exists()) { + throw new AlreadyExistAccountException(account.getAccountNumber()); + } + + Account result = createNewAccount(account); + + return result; + } + + protected Account createNewAccount(Account account) throws InvalidAccountNumberException { // check rules before create the account AccountingRules accountingRules = LimaConfig.getInstance().getAccountingRules(); accountingRules.createAccountRules(account); @@ -87,13 +115,9 @@ // check if account number already exist AccountTopiaDao accountDao = getDaoHelper().getAccountDao(); - if (accountDao.forNaturalId(account.getAccountNumber()).exists()) { - throw new AlreadyExistAccountException(account.getAccountNumber()); - } - //create it Account result = accountDao.create(account); - + return result; } Modified: trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/EntryBookServiceImpl.java =================================================================== --- trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/EntryBookServiceImpl.java 2014-07-25 08:47:33 UTC (rev 3867) +++ trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/EntryBookServiceImpl.java 2014-07-28 12:06:55 UTC (rev 3868) @@ -56,26 +56,45 @@ public class EntryBookServiceImpl extends AbstractLimaService implements EntryBookService { @Override + public boolean createOrUpdateEntryBook(EntryBook entryBook) { + EntryBookTopiaDao entryBookTopiaDao = getDaoHelper().getEntryBookDao(); + EntryBook existingEntryBook = entryBookTopiaDao.forCodeEquals(entryBook.getCode()).findUniqueOrNull(); + boolean result; + if (existingEntryBook != null) { + result = true; + updateEntryBook(entryBook); + } else { + result = false; + createNewEntryBook(entryBook); + } + return result; + } + + @Override public EntryBook createEntryBook(EntryBook entryBook) { - + EntryBook result = null; // check if entrybook with is name already exist EntryBookTopiaDao entryBookTopiaDao = getDaoHelper().getEntryBookDao(); - EntryBook result = entryBookTopiaDao.forCodeEquals(entryBook.getCode()).findUniqueOrNull(); - if (result != null) { + EntryBook alreadyExistingEntryBook = entryBookTopiaDao.forCodeEquals(entryBook.getCode()).findUniqueOrNull(); + if (alreadyExistingEntryBook != null) { log.error( t("lima-business.entrybook.entrybookalreadyexist", entryBook.getCode())); } else { // creation du EntryBook - result = entryBookTopiaDao.create(entryBook); - - createClosedPeriodicEntryBook(result); + result = createNewEntryBook(entryBook); } return result; } - @Override - public void createClosedPeriodicEntryBook(EntryBook entryBook) { + protected EntryBook createNewEntryBook(EntryBook entryBook) { + EntryBookTopiaDao entryBookTopiaDao = getDaoHelper().getEntryBookDao(); + EntryBook result = entryBookTopiaDao.create(entryBook); + createClosedPeriodicEntryBook(result); + return result; + } + + protected void createClosedPeriodicEntryBook(EntryBook entryBook) { //create ClosedPeriodicEntryBook for all unblocked financial period ClosedPeriodicEntryBookTopiaDao closedPeriodicEntryBookTopiaDao = getDaoHelper().getClosedPeriodicEntryBookDao(); @@ -127,7 +146,7 @@ // re-attach to current transaction EntryBookTopiaDao entryBookTopiaDao = getDaoHelper().getEntryBookDao(); - EntryBook localEntryBook = entryBookTopiaDao.findByTopiaId(entryBook.getTopiaId()); + EntryBook localEntryBook = entryBookTopiaDao.forTopiaIdEquals(entryBook.getTopiaId()).findUnique(); // delete all ClosedPeriodicEntryBook from this EntryBook ClosedPeriodicEntryBookTopiaDao closedPeriodicEntryBookTopiaDao = Modified: trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/FiscalPeriodServiceImpl.java =================================================================== --- trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/FiscalPeriodServiceImpl.java 2014-07-25 08:47:33 UTC (rev 3867) +++ trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/FiscalPeriodServiceImpl.java 2014-07-28 12:06:55 UTC (rev 3868) @@ -111,7 +111,6 @@ MoreOneUnlockFiscalPeriodException { FiscalPeriodTopiaDao fiscalPeriodTopiaDao = getDaoHelper().getFiscalPeriodDao(); - EntryBookTopiaDao entryBookTopiaDao = getDaoHelper().getEntryBookDao(); AccountingRules accountingRules = LimaConfig.getInstance().getAccountingRules(); @@ -124,51 +123,18 @@ endDate = DateUtils.addMilliseconds(endDate, -1); fiscalPeriod.setBeginDate(beginDate); fiscalPeriod.setEndDate(endDate); + createFiscalPeriodClosePeriodicEntryBooks(fiscalPeriod, accountingRules); - //check rules before create the account - List<FinancialPeriod> financialPeriods = accountingRules.createFiscalPeriodRules(fiscalPeriod); - - // create - fiscalPeriod.addAllFinancialPeriod(financialPeriods); - - //create all financial period - for (FinancialPeriod financialPeriod : financialPeriods) { - - List<ClosedPeriodicEntryBook> closedPeriodicEntryBooks = Lists.newArrayList(); - //create ClosedPeriodicEntryBook for all entrybook - for (EntryBook entryBook : entryBookTopiaDao.findAll()) { - //new closed periodic entrybook - ClosedPeriodicEntryBook closedPeriodicEntryBook = new ClosedPeriodicEntryBookImpl(); - // set entrybook - closedPeriodicEntryBook.setEntryBook(entryBook); - // set financial period - closedPeriodicEntryBook.setFinancialPeriod(financialPeriod); - - closedPeriodicEntryBooks.add(closedPeriodicEntryBook); - } - financialPeriod.addAllEntryBookClosedPeriodicEntryBook(closedPeriodicEntryBooks); - - } - FiscalPeriod result = fiscalPeriodTopiaDao.create(fiscalPeriod); return result; } - @Override - public void createFiscalPeriodsClosePeriodicEntryBooks(Collection<FiscalPeriod> fiscalPeriods) throws + protected void createFiscalPeriodClosePeriodicEntryBooks(FiscalPeriod fiscalPeriod, AccountingRules accountingRules) throws BeginAfterEndFiscalPeriodException, NotBeginNextDayOfLastFiscalPeriodException, MoreOneUnlockFiscalPeriodException { + EntryBookTopiaDao entryBookTopiaDao = getDaoHelper().getEntryBookDao(); - AccountingRules accountingRules = LimaConfig.getInstance().getAccountingRules(); - for (FiscalPeriod fiscalPeriod : fiscalPeriods) { - createFiscalPeriodClosePeriodicEntryBooks(fiscalPeriod, accountingRules, entryBookTopiaDao); - } - } - - protected void createFiscalPeriodClosePeriodicEntryBooks(FiscalPeriod fiscalPeriod, AccountingRules accountingRules, EntryBookTopiaDao entryBookTopiaDao) throws - BeginAfterEndFiscalPeriodException, NotBeginNextDayOfLastFiscalPeriodException, MoreOneUnlockFiscalPeriodException { - List<FinancialPeriod> financialPeriods; financialPeriods = accountingRules.createFiscalPeriodRules(fiscalPeriod); 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-07-25 08:47:33 UTC (rev 3867) +++ trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/ImportServiceImpl.java 2014-07-28 12:06:55 UTC (rev 3868) @@ -343,8 +343,7 @@ financialTransaction = new FinancialTransactionImpl(); financialTransaction.setEntryBook(entryBook); financialTransaction.setTransactionDate(dateEcr); - financialTransaction = financialTransactionService - .createFinancialTransaction(financialTransaction); + financialTransaction = financialTransactionService.createFinancialTransaction(financialTransaction); result.append(t( "lima-business.import.transactionadded", dateEcr, entryBook.getCode())); Modified: trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/NewExportServiceImpl.java =================================================================== --- trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/NewExportServiceImpl.java 2014-07-25 08:47:33 UTC (rev 3867) +++ trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/NewExportServiceImpl.java 2014-07-28 12:06:55 UTC (rev 3868) @@ -26,7 +26,6 @@ package org.chorem.lima.business.ejb; import com.google.common.collect.Lists; -import com.sun.org.apache.xpath.internal.operations.Bool; import org.apache.commons.codec.binary.Base64; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; @@ -59,8 +58,6 @@ import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.IOException; import java.nio.charset.Charset; import java.util.List; import java.util.zip.ZipEntry; @@ -79,17 +76,17 @@ @TransactionAttribute public class NewExportServiceImpl extends AbstractLimaService implements NewExportService { - protected final String DATE_PATTERN = "dd-MM-yyyy-HH:mm"; - @EJB - private EntryBookService entryBookService; + protected EntryBookService entryBookService; @EJB - private AccountService accountService; + protected AccountService accountService; @EJB - private FinancialTransactionService financialTransactionService; + protected FinancialTransactionService financialTransactionService; + public static final String JAVA_IO_TMPDIR = "java.io.tmpdir"; + @Override public String exportAllAsCSV(String charset) { ByteArrayOutputStream rstBao = new ByteArrayOutputStream(); @@ -132,7 +129,7 @@ List<Account> entities = accountTopiaDao.findAll(); AccountModel model = new AccountModel(); - String tmpDir = System.getProperty("java.io.tmpdir")+"/"; + String tmpDir = System.getProperty(JAVA_IO_TMPDIR)+"/"; File result = new File(tmpDir + "accounts.csv"); Export.exportToFile(model, entities, result, Charset.forName(charset)); @@ -156,7 +153,7 @@ EntryBookTopiaDao entryBookTopiaDao = getDaoHelper().getEntryBookDao(); List<EntryBook> entities = entryBookTopiaDao.findAll(); - String tmpDir = System.getProperty("java.io.tmpdir")+"/"; + String tmpDir = System.getProperty(JAVA_IO_TMPDIR)+"/"; File result = new File(tmpDir + "entryBooks.csv"); EntryBookModel model = new EntryBookModel(); Export.exportToFile(model, entities, result, Charset.forName(charset)); @@ -183,7 +180,7 @@ FiscalPeriodTopiaDao dao = getDaoHelper().getFiscalPeriodDao(); List<FiscalPeriod> entities = dao.findAll(); - String tmpDir = System.getProperty("java.io.tmpdir")+"/"; + String tmpDir = System.getProperty(JAVA_IO_TMPDIR)+"/"; File result = new File(tmpDir + "fiscalPeriod.csv"); FiscalPeriodModel model = new FiscalPeriodModel(); Export.exportToFile(model, entities, result, Charset.forName(charset)); @@ -211,7 +208,7 @@ getDaoHelper().getFinancialTransactionDao(); List<FinancialTransaction> entities = financialTransactionTopiaDao.findAll(); - String tmpDir = System.getProperty("java.io.tmpdir")+"/"; + String tmpDir = System.getProperty(JAVA_IO_TMPDIR)+"/"; result = new File(tmpDir + "financialTransactions.csv"); FinancialTransactionModel model = new FinancialTransactionModel(entryBookService); Export.exportToFile(model, entities, result, Charset.forName(charset)); @@ -236,7 +233,7 @@ EntryTopiaDao dao = getDaoHelper().getEntryDao(); List<Entry> entities = dao.findAll(); - String tmpDir = System.getProperty("java.io.tmpdir")+"/"; + String tmpDir = System.getProperty(JAVA_IO_TMPDIR)+"/"; File result = new File(tmpDir + "entries.csv"); EntryModel model = new EntryModel(accountService, financialTransactionService , humanReadable); Export.exportToFile(model, entities, result, Charset.forName(charset)); 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-25 08:47:33 UTC (rev 3867) +++ trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/NewImportServiceImpl.java 2014-07-28 12:06:55 UTC (rev 3868) @@ -22,13 +22,14 @@ * #L% */ -import com.google.common.base.Function; -import com.google.common.base.Preconditions; -import com.google.common.collect.Lists; import org.apache.commons.io.IOUtils; -import org.chorem.lima.business.BeginAfterEndFiscalPeriodException; +import org.chorem.lima.business.AlreadyExistAccountException; +import org.chorem.lima.business.FiscalPeriodException; +import org.chorem.lima.business.ImportResult; +import org.chorem.lima.business.InvalidAccountNumberException; +import org.chorem.lima.business.LockedEntryBookException; +import org.chorem.lima.business.LockedFinancialPeriodException; import org.chorem.lima.business.MoreOneUnlockFiscalPeriodException; -import org.chorem.lima.business.NotBeginNextDayOfLastFiscalPeriodException; import org.chorem.lima.business.api.AccountService; import org.chorem.lima.business.api.EntryBookService; import org.chorem.lima.business.api.EntryService; @@ -41,28 +42,21 @@ import org.chorem.lima.business.ejb.csv.FinancialTransactionModel; import org.chorem.lima.business.ejb.csv.FiscalPeriodModel; import org.chorem.lima.entity.Account; -import org.chorem.lima.entity.AccountTopiaDao; import org.chorem.lima.entity.Entry; import org.chorem.lima.entity.EntryBook; -import org.chorem.lima.entity.EntryBookTopiaDao; -import org.chorem.lima.entity.EntryTopiaDao; import org.chorem.lima.entity.FinancialTransaction; -import org.chorem.lima.entity.FinancialTransactionTopiaDao; import org.chorem.lima.entity.FiscalPeriod; -import org.chorem.lima.entity.FiscalPeriodTopiaDao; import org.nuiton.csv.Import; import org.nuiton.csv.ImportModel; -import org.nuiton.topia.persistence.TopiaEntity; import javax.ejb.EJB; import javax.ejb.Remote; import javax.ejb.Stateless; import javax.ejb.TransactionAttribute; import java.io.InputStream; +import java.util.ArrayList; import java.util.List; -import static org.nuiton.i18n.I18n.t; - /** * Created by davidcosse on 03/06/14. */ @@ -72,146 +66,154 @@ public class NewImportServiceImpl extends AbstractLimaService implements NewImportService { @EJB - private EntryBookService entryBookService; + protected EntryBookService entryBookService; @EJB - private AccountService accountService; + protected AccountService accountService; @EJB - private EntryService entryService; + protected FinancialTransactionService financialTransactionService; @EJB - private FinancialTransactionService financialTransactionService; + protected FiscalPeriodService fiscalPeriodService; @EJB - private FiscalPeriodService fiscalPeriodService; + protected EntryService entryService; - protected static final Function<TopiaEntity, String> GET_TOPIA_ID = new Function<TopiaEntity, String>() { - @Override - public String apply(TopiaEntity input) { - return input == null ? null : input.getTopiaId(); - } - }; - @Override - public String importAccountAsCSV(String contents) { + public ImportResult importAccountAsCSV(String contents) { InputStream contentStream = IOUtils.toInputStream(contents); + ImportResult result = new ImportResult(); try { ImportModel<Account> model = new AccountModel(); - AccountTopiaDao dao = getDaoHelper().getAccountDao(); - Import<Account> result = Import.newImport(model, contentStream); - - List<Account> newAccounts = Lists.newArrayList(); - Account oldAccount; - for (Account account : result) { - oldAccount = dao.forAccountNumberEquals(account.getAccountNumber()).findUniqueOrNull(); - if (oldAccount == null) { - newAccounts.add(account); + Import<Account> accounts = Import.newImport(model, contentStream); + // csv line index + int indexLine = 0; + boolean updated; + for (Account account : accounts) { + try { + updated = accountService.createOrUbdateAccount(account); + if (updated) { + result.increaseUpdated(); + } else { + result.increaseCreated(); + } + } catch (InvalidAccountNumberException e) { + result.getException().addException(indexLine, e); } + indexLine++; } - dao.createAll(newAccounts); } finally { IOUtils.closeQuietly(contentStream); } - return "SUCCES"; + return result; } @Override - public String importEntryBooksAsCSV(String contents) { + public ImportResult importEntryBooksAsCSV(String contents) { InputStream contentStream = IOUtils.toInputStream(contents); + ImportResult result = new ImportResult(); try { ImportModel<EntryBook> model = new EntryBookModel(); - EntryBookTopiaDao dao = getDaoHelper().getEntryBookDao(); - Import<EntryBook> result = Import.newImport(model, contentStream); - dao.createAll(result); - - for (EntryBook entryBook : result) { - entryBookService.createClosedPeriodicEntryBook(entryBook); + Import<EntryBook> entryBooks = Import.newImport(model, contentStream); + for (EntryBook entryBook : entryBooks) { + boolean updated = entryBookService.createOrUpdateEntryBook(entryBook); + if(updated) { + result.increaseUpdated(); + } else { + result.increaseCreated(); + } } } finally { IOUtils.closeQuietly(contentStream); } - return "SUCCES"; + return result; } @Override - public String importFiscalPeriodsAsCSV(String contents) { - StringBuilder result = new StringBuilder(); + public ImportResult importFiscalPeriodsAsCSV(String contents) { InputStream contentStream = IOUtils.toInputStream(contents); + ImportResult result = new ImportResult(); try { ImportModel<FiscalPeriod> model = new FiscalPeriodModel(); - FiscalPeriodTopiaDao dao = getDaoHelper().getFiscalPeriodDao(); - Import<FiscalPeriod> importedFPs = Import.newImport(model, contentStream); - List<FiscalPeriod> fiscalPeriods = Lists.newArrayList(importedFPs); + Import<FiscalPeriod> fiscalPeriods = Import.newImport(model, contentStream); + + int lineIndex = 0; try { - fiscalPeriodService.createFiscalPeriodsClosePeriodicEntryBooks(fiscalPeriods); - result.append(t("lima-business.import.fiscalperiodscloseperiodicentrybooks")); - } catch (BeginAfterEndFiscalPeriodException e) { - result.append("Can't import " + t("lima-business.import.FiscalPeriod.error.beginAfterEndFiscalPeriod")); + for (FiscalPeriod fiscalPeriod : fiscalPeriods) { + fiscalPeriodService.createFiscalPeriod(fiscalPeriod); + lineIndex++; + result.increaseCreated(); + } + } catch (FiscalPeriodException e) { + result.getException().addException(lineIndex, e); } catch (MoreOneUnlockFiscalPeriodException e) { - result.append("Can't import " + t("lima-business.import.FiscalPeriod.error.moreOneUnlockFiscalPeriod")); - } catch (NotBeginNextDayOfLastFiscalPeriodException e) { - result.append("Can't import " + t("lima-business.import.FiscalPeriod.error.notBeginNextDayOfLastFiscalPeriod")); + result.increaseIgnored(); } - dao.createAll(fiscalPeriods); } finally { IOUtils.closeQuietly(contentStream); } - return result.toString(); + return result; } - @Override - public String importFinancialTransactionsAsCSV(String contents) { + protected ImportResult importFinancialTransactionsAsCSV(String contents) { + ImportResult result = new ImportResult(); - EntryBookTopiaDao entryBookdao = getDaoHelper().getEntryBookDao(); - Preconditions.checkArgument(entryBookdao.count() > 0, "Les journaux doivent avoir été importé."); - // import and save FinancialTransactions InputStream contentStream = IOUtils.toInputStream(contents); try { ImportModel<FinancialTransaction> model = new FinancialTransactionModel(entryBookService); - FinancialTransactionTopiaDao dao = getDaoHelper().getFinancialTransactionDao(); - Import<FinancialTransaction> result = Import.newImport(model, contentStream); - dao.createAll(result); + Import<FinancialTransaction> financialTransactions = Import.newImport(model, contentStream); + + int lineIndex = 0; + for (FinancialTransaction financialTransaction : financialTransactions) { + try { + financialTransactionService.createFinancialTransaction(financialTransaction); + lineIndex++; + result.increaseCreated(); + } catch (LockedFinancialPeriodException e) { + result.getException().addException(lineIndex, e); + } catch (LockedEntryBookException e) { + result.getException().addException(lineIndex, e); + } + } + } finally { IOUtils.closeQuietly(contentStream); } - return "SUCCES"; + return result; } @Override - public String importEntriesAsCSV(String contents) { - AccountTopiaDao accountDao = getDaoHelper().getAccountDao(); - FinancialTransactionTopiaDao financialTransactionDao = getDaoHelper().getFinancialTransactionDao(); - - Preconditions.checkArgument(accountDao.count() > 0 && financialTransactionDao.count() > 0, "Les comptes et trasactions financières doivent avoir été importé."); - + public ImportResult importEntriesAsCSV(String contents) { // import and save entries InputStream contentStream = IOUtils.toInputStream(contents); + ImportResult result = new ImportResult(); try { ImportModel<Entry> model = new EntryModel(accountService, financialTransactionService, false); - Import<Entry> result = Import.newImport(model, contentStream); - - EntryTopiaDao dao = getDaoHelper().getEntryDao(); - dao.createAll(result); - + Import<Entry> entries = Import.newImport(model, contentStream); + for (Entry entry : entries) { + entryService.createEntry(entry); + result.increaseCreated(); + } } finally { IOUtils.closeQuietly(contentStream); } - return "SUCCES"; + return result; } @Override - public String importAllAsCSV(String entryBooks, String financialTransactions, String fiscalPeriods, String accounts, String entries){ + public List<ImportResult> importAllAsCSV(String entryBooks, String financialTransactions, String fiscalPeriods, String accounts, String entries) throws AlreadyExistAccountException, InvalidAccountNumberException { + List<ImportResult> results = new ArrayList<ImportResult>(); importAccountAsCSV(accounts); importEntryBooksAsCSV(entryBooks); importFiscalPeriodsAsCSV(fiscalPeriods); importFinancialTransactionsAsCSV(financialTransactions); importEntriesAsCSV(entries); - return "SUCCES"; + return results; } } 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-25 08:47:33 UTC (rev 3867) +++ trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/csv/AbstractLimaModel.java 2014-07-28 12:06:55 UTC (rev 3868) @@ -22,6 +22,7 @@ * #L% */ +import com.google.common.base.Preconditions; import com.google.common.base.Strings; import org.apache.commons.lang3.StringUtils; import org.chorem.lima.business.api.AccountService; @@ -34,7 +35,6 @@ import org.nuiton.csv.ValueParser; import org.nuiton.csv.ext.AbstractImportModel; -import javax.ejb.EJB; import java.math.BigDecimal; import java.text.ParseException; import java.text.SimpleDateFormat; @@ -311,12 +311,9 @@ @Override public FinancialTransaction parse(String value) { + Preconditions.checkArgument(!Strings.isNullOrEmpty(value)); FinancialTransaction result; - if (StringUtils.isNotBlank(value)) { - result = financialTransactionService.getFinancialTransactionWithId(value); - } else { - result = null; - } + result = financialTransactionService.getFinancialTransactionWithId(value); return result; } }; Modified: trunk/lima-business/src/test/java/org/chorem/lima/business/AbstractLimaTest.java =================================================================== --- trunk/lima-business/src/test/java/org/chorem/lima/business/AbstractLimaTest.java 2014-07-25 08:47:33 UTC (rev 3867) +++ trunk/lima-business/src/test/java/org/chorem/lima/business/AbstractLimaTest.java 2014-07-28 12:06:55 UTC (rev 3868) @@ -109,7 +109,6 @@ @Before public void initAbstractTest() throws Exception { setUpLocale(); -// LimaInterceptor.schemaExistChecked = false; Properties options = getTestConfiguration(); LimaConfig config = new LimaTestsConfig("/lima-test.properties", options); initServices(config); Modified: trunk/lima-business/src/test/java/org/chorem/lima/business/FinancialTransactionServiceImplTest.java =================================================================== --- trunk/lima-business/src/test/java/org/chorem/lima/business/FinancialTransactionServiceImplTest.java 2014-07-25 08:47:33 UTC (rev 3867) +++ trunk/lima-business/src/test/java/org/chorem/lima/business/FinancialTransactionServiceImplTest.java 2014-07-28 12:06:55 UTC (rev 3868) @@ -90,6 +90,7 @@ transaction1.setEntryBook(journalDesVentes); transaction1 = financialTransactionService.createFinancialTransaction(transaction1); + Entry tr1Entry1 = new EntryImpl(); tr1Entry1.setAmount(BigDecimal.valueOf(42.0)); tr1Entry1.setAccount(accountVmpVae); 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-25 08:47:33 UTC (rev 3867) +++ trunk/lima-business/src/test/java/org/chorem/lima/business/NewImportExportServiceTest.java 2014-07-28 12:06:55 UTC (rev 3868) @@ -53,10 +53,12 @@ try { contentStream = new FileInputStream(tmpDir + "export-accounts.csv"); String inportStream = IOUtils.toString(contentStream); - newImportService.importAccountAsCSV(inportStream); + ImportResult result = newImportService.importAccountAsCSV(inportStream); // make sure all account have been created Assert.assertEquals(nbEntities, accountService.getAllAccounts().size()); + Assert.assertEquals(nbEntities, result.getNbCreated()); + Assert.assertTrue(result.getException().getAllExceptionsByLine().isEmpty()); } finally { IOUtils.closeQuietly(contentStream); } @@ -83,50 +85,21 @@ Assert.assertEquals(0, entryBookService.getAllEntryBooks().size()); FileInputStream contentStream = null; + ImportResult result; try { contentStream = new FileInputStream(tmpDir + "export-EntryBooks.csv"); String inportStream = IOUtils.toString(contentStream); - newImportService.importEntryBooksAsCSV(inportStream); + result = newImportService.importEntryBooksAsCSV(inportStream); } finally { IOUtils.closeQuietly(contentStream); } Assert.assertEquals(nbEntities, entryBookService.getAllEntryBooks().size()); + Assert.assertEquals(nbEntities, result.getNbCreated()); + Assert.assertTrue(result.getException().getAllExceptionsByLine().isEmpty()); } @Test - public void testExportImportFinancialTransactions() throws Exception { - initTestWithFinancialTransaction(); - - String tmpDir = System.getProperty("java.io.tmpdir")+"/"; - String export = newExportService.exportFinancialTransactionsAsCSV(Charset.defaultCharset().name()); - InputStream stream = IOUtils.toInputStream(export); - FileOutputStream res = new FileOutputStream(tmpDir + "export-financial-transactions.csv"); - IOUtils.copy(stream, res); - - List<FinancialTransaction> financialTransactions = financialTransactionService.getAllFinancialTransactions(df.parse("January 1, 2012"),df.parse("December 31, 2012")); - int nbEntities = financialTransactions.size(); - Assert.assertEquals(1, nbEntities); - - for (FinancialTransaction financialTransaction : financialTransactions) { - financialTransactionService.removeFinancialTransaction(financialTransaction); - } - - Assert.assertEquals(0, financialTransactionService.getAllFinancialTransactions(df.parse("January 1, 2012"), df.parse("December 31, 2012")).size()); - - FileInputStream contentStream = null; - try { - contentStream = new FileInputStream(tmpDir + "export-financial-transactions.csv"); - String inportStream = IOUtils.toString(contentStream); - newImportService.importFinancialTransactionsAsCSV(inportStream); - } finally { - IOUtils.closeQuietly(contentStream); - } - - Assert.assertEquals(nbEntities, financialTransactionService.getAllFinancialTransactions(df.parse("January 1, 2012"),df.parse("December 31, 2012")).size()); - } - - @Test public void testExportImportEntries() throws Exception { initTestWithFinancialTransaction(); @@ -163,10 +136,11 @@ // test import FileInputStream contentStream = null; + ImportResult result; try { contentStream = new FileInputStream(tmpDir + "export-entries.csv"); String inputStream = IOUtils.toString(contentStream); - newImportService.importEntriesAsCSV(inputStream); + result = newImportService.importEntriesAsCSV(inputStream); } finally { IOUtils.closeQuietly(contentStream); } @@ -179,6 +153,8 @@ } Assert.assertEquals(nbEntities, entries.size()); + Assert.assertEquals(nbEntities, result.getNbCreated()); + Assert.assertTrue(result.getException().getAllExceptionsByLine().isEmpty()); } @Test @@ -192,24 +168,26 @@ IOUtils.copy(stream, res); List<FiscalPeriod> fiscalPeriods = fiscalPeriodService.getAllFiscalPeriods(); - int nbEntities = fiscalPeriods.size(); - Assert.assertEquals(1, nbEntities); + int nbFiscalPeriods = fiscalPeriods.size(); + Assert.assertEquals(1, nbFiscalPeriods); initAbstractTest(); Assert.assertEquals(0, fiscalPeriodService.getAllFiscalPeriods().size()); FileInputStream contentStream = null; + ImportResult result; try { contentStream = new FileInputStream(tmpDir + "export-fiscal-periods.csv"); String inputStream = IOUtils.toString(contentStream); - // TODO DCossé 22/07/14 traiter les messages de retour - newImportService.importFiscalPeriodsAsCSV(inputStream); + result = newImportService.importFiscalPeriodsAsCSV(inputStream); } finally { IOUtils.closeQuietly(contentStream); } - Assert.assertEquals(nbEntities, fiscalPeriodService.getAllFiscalPeriods().size()); + Assert.assertEquals(nbFiscalPeriods, fiscalPeriodService.getAllFiscalPeriods().size()); + Assert.assertEquals(nbFiscalPeriods, result.getNbCreated()); + Assert.assertTrue(result.getException().getAllExceptionsByLine().isEmpty()); } @Test @@ -223,11 +201,15 @@ initAbstractTest(); FileInputStream contentStream = null; + List<ImportResult> importResults; try { - importAllFromZipFile(tmpDir); + importResults = importAllFromZipFile(tmpDir); } finally { IOUtils.closeQuietly(contentStream); } + for (ImportResult importResult : importResults) { + Assert.assertTrue(importResult.getException().getAllExceptionsByLine().isEmpty()); + } } protected void createZipFile(String path, String zippedBase64Str) throws Exception { @@ -236,7 +218,7 @@ IOUtils.copy(inputStream, new FileOutputStream(path)); } - protected String importAllFromZipFile(String filePath) { + protected List<ImportResult> importAllFromZipFile(String filePath) { ZipInputStream zipInputStream = null; String tmpDir = System.getProperty("java.io.tmpdir")+"/"; FileInputStream inputStream = null; @@ -279,6 +261,7 @@ IOUtils.closeQuietly(inputStream); } InputStream transactionsStream, entryBooksStream, fiscalPeriodsStream, entriesStream, accountsStream; + List<ImportResult> results; try { entryBooksStream = new FileInputStream(tmpDir + "entryBooks.csv"); String entryBooksStreamString = IOUtils.toString(entryBooksStream); @@ -301,8 +284,7 @@ String accountsStreamString = IOUtils.toString(accountsStream); IOUtils.closeQuietly(accountsStream); - // TODO DCossé 23/07/14 should be done in one transaction on service side but it doesn't work now - newImportService.importAllAsCSV(entryBooksStreamString, transactionsStreamString, fiscalPeriodsStreamString, accountsStreamString, entriesStreamString); + results = newImportService.importAllAsCSV(entryBooksStreamString, transactionsStreamString, fiscalPeriodsStreamString, accountsStreamString, entriesStreamString); } catch (Exception ex) { if(log.isInfoEnabled()) { @@ -310,6 +292,6 @@ } throw new LimaTechnicalException("could not import files", ex); } - return "SUCCES"; + return results; } } Modified: trunk/lima-business/src/test/java/org/chorem/lima/business/ReportServiceImplTest.java =================================================================== --- trunk/lima-business/src/test/java/org/chorem/lima/business/ReportServiceImplTest.java 2014-07-25 08:47:33 UTC (rev 3867) +++ trunk/lima-business/src/test/java/org/chorem/lima/business/ReportServiceImplTest.java 2014-07-28 12:06:55 UTC (rev 3868) @@ -38,7 +38,9 @@ import org.junit.Test; import java.math.BigDecimal; +import java.util.ArrayList; import java.util.Date; +import java.util.List; /** * Test le service de génération des reports. @@ -92,7 +94,7 @@ transaction1.setTransactionDate(df.parse("April 5, 2012")); transaction1.setEntryBook(journalDesVentes); transaction1 = financialTransactionService.createFinancialTransaction(transaction1); - + Entry tr1Entry1 = new EntryImpl(); tr1Entry1.setAmount(BigDecimal.valueOf(54.0)); tr1Entry1.setAccount(accountVmpVae); Added: trunk/lima-business-api/src/main/java/org/chorem/lima/business/ImportException.java =================================================================== --- trunk/lima-business-api/src/main/java/org/chorem/lima/business/ImportException.java (rev 0) +++ trunk/lima-business-api/src/main/java/org/chorem/lima/business/ImportException.java 2014-07-28 12:06:55 UTC (rev 3868) @@ -0,0 +1,20 @@ +package org.chorem.lima.business; + +import java.util.HashMap; +import java.util.Map; +/** + * Created by davidcosse on 24/07/14. + */ +public class ImportException extends LimaException { + + protected Map<Integer, LimaException> allExceptions = new HashMap<>(); + + + public void addException(Integer indexLine, LimaException e) { + allExceptions.put(indexLine, e); + } + + public Map<Integer, LimaException> getAllExceptionsByLine() { + return allExceptions; + } +} Added: trunk/lima-business-api/src/main/java/org/chorem/lima/business/ImportResult.java =================================================================== --- trunk/lima-business-api/src/main/java/org/chorem/lima/business/ImportResult.java (rev 0) +++ trunk/lima-business-api/src/main/java/org/chorem/lima/business/ImportResult.java 2014-07-28 12:06:55 UTC (rev 3868) @@ -0,0 +1,52 @@ +package org.chorem.lima.business; + +import java.io.Serializable; + +/** + * Created by davidcosse on 24/07/14. + */ +public class ImportResult implements Serializable{ + + protected int nbCreated; + + protected int nbUpdated; + + protected int nbIgnored; + + protected ImportException exception; + + public ImportResult() { + this.exception = new ImportException(); + nbCreated = 0; + nbUpdated = 0; + nbIgnored = 0; + } + + public int getNbCreated() { + return nbCreated; + } + + public int getNbUpdated() { + return nbUpdated; + } + + public int getNbIgnored() { + return nbIgnored; + } + + public ImportException getException() { + return exception; + } + + public int increaseCreated() { + return nbCreated++; + } + + public int increaseUpdated() { + return nbUpdated++; + } + + public int increaseIgnored() { + return nbIgnored++; + } +} Modified: trunk/lima-business-api/src/main/java/org/chorem/lima/business/api/AccountService.java =================================================================== --- trunk/lima-business-api/src/main/java/org/chorem/lima/business/api/AccountService.java 2014-07-25 08:47:33 UTC (rev 3867) +++ trunk/lima-business-api/src/main/java/org/chorem/lima/business/api/AccountService.java 2014-07-28 12:06:55 UTC (rev 3868) @@ -87,6 +87,14 @@ List<Account> getAllSubAccounts(Account account); /** + * Create or update account + * @param account the account to create + * @return true if updated or false if created + * @throws InvalidAccountNumberException + */ + boolean createOrUbdateAccount(Account account) throws InvalidAccountNumberException; + + /** * Create new account. If {@code masterAccount} is not null, {@code account} * is added in {@code masterAccount}'s subAccounts. * Modified: trunk/lima-business-api/src/main/java/org/chorem/lima/business/api/EntryBookService.java =================================================================== --- trunk/lima-business-api/src/main/java/org/chorem/lima/business/api/EntryBookService.java 2014-07-25 08:47:33 UTC (rev 3867) +++ trunk/lima-business-api/src/main/java/org/chorem/lima/business/api/EntryBookService.java 2014-07-28 12:06:55 UTC (rev 3868) @@ -60,6 +60,13 @@ List<EntryBook> getAllEntryBooks(); /** + * Create or update the given entry book + * @param entryBook + * @return true if updated, false if created + */ + boolean createOrUpdateEntryBook(EntryBook entryBook); + + /** * Create new entry book. * * @param entryBook @@ -85,6 +92,4 @@ * @throws org.chorem.lima.business.UsedEntryBookException */ void removeEntryBook(EntryBook entryBook) throws UsedEntryBookException; - - void createClosedPeriodicEntryBook(EntryBook entryBook); } Modified: trunk/lima-business-api/src/main/java/org/chorem/lima/business/api/FiscalPeriodService.java =================================================================== --- trunk/lima-business-api/src/main/java/org/chorem/lima/business/api/FiscalPeriodService.java 2014-07-25 08:47:33 UTC (rev 3867) +++ trunk/lima-business-api/src/main/java/org/chorem/lima/business/api/FiscalPeriodService.java 2014-07-28 12:06:55 UTC (rev 3868) @@ -39,7 +39,6 @@ import org.chorem.lima.entity.EntryBook; import org.chorem.lima.entity.FiscalPeriod; -import java.util.Collection; import java.util.List; /** @@ -98,8 +97,4 @@ FiscalPeriod updateEndDate(FiscalPeriod fiscalPeriod); - void createFiscalPeriodsClosePeriodicEntryBooks(Collection<FiscalPeriod> fiscalPeriods) throws - BeginAfterEndFiscalPeriodException, - NotBeginNextDayOfLastFiscalPeriodException, - MoreOneUnlockFiscalPeriodException; } Modified: trunk/lima-business-api/src/main/java/org/chorem/lima/business/api/NewImportService.java =================================================================== --- trunk/lima-business-api/src/main/java/org/chorem/lima/business/api/NewImportService.java 2014-07-25 08:47:33 UTC (rev 3867) +++ trunk/lima-business-api/src/main/java/org/chorem/lima/business/api/NewImportService.java 2014-07-28 12:06:55 UTC (rev 3868) @@ -22,20 +22,24 @@ * #L% */ +import org.chorem.lima.business.AlreadyExistAccountException; +import org.chorem.lima.business.ImportResult; +import org.chorem.lima.business.InvalidAccountNumberException; + +import java.util.List; + /** * Created by davidcosse on 03/06/14. */ public interface NewImportService { - String importAccountAsCSV(String contents); + ImportResult importAccountAsCSV(String contents); - String importEntryBooksAsCSV(String contents); + ImportResult importEntryBooksAsCSV(String contents); - String importFiscalPeriodsAsCSV(String contents); + ImportResult importFiscalPeriodsAsCSV(String contents); - String importFinancialTransactionsAsCSV(String contents); + ImportResult importEntriesAsCSV(String contents); - String importEntriesAsCSV(String contents); - - String importAllAsCSV(String entryBooks, String transactions, String fiscalPeriods, String accounts, String entries); + List<ImportResult> importAllAsCSV(String entryBooks, String transactions, String fiscalPeriods, String accounts, String entries) throws AlreadyExistAccountException, InvalidAccountNumberException; } Modified: trunk/lima-swing/src/main/java/org/chorem/lima/ui/importexport/ImportExport.java =================================================================== --- trunk/lima-swing/src/main/java/org/chorem/lima/ui/importexport/ImportExport.java 2014-07-25 08:47:33 UTC (rev 3867) +++ trunk/lima-swing/src/main/java/org/chorem/lima/ui/importexport/ImportExport.java 2014-07-28 12:06:55 UTC (rev 3868) @@ -34,6 +34,7 @@ import org.apache.commons.logging.LogFactory; import org.chorem.lima.LimaTechnicalException; import org.chorem.lima.business.ImportEbpException; +import org.chorem.lima.business.ImportResult; import org.chorem.lima.business.api.ExportService; import org.chorem.lima.business.api.ImportService; import org.chorem.lima.business.api.NewExportService; @@ -66,6 +67,7 @@ import java.nio.charset.Charset; import java.text.SimpleDateFormat; import java.util.Date; +import java.util.List; import java.util.concurrent.ExecutionException; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; @@ -144,7 +146,9 @@ @Override protected String doInBackground() { String datas; + // TODO DCossé 24/07/14 remove it String result = ""; + ImportResult importResult; try { switch (importExportMethodeF) { case CSV_ALL_EXPORT: @@ -182,15 +186,20 @@ createFile(filePath, EncodingEnum.ISOLATIN1.getEncoding(), datas); break; case CSV_ALL_IMPORT: - result = importAllFromZipFile(filePath); + List<ImportResult> results = importAllFromZipFile(filePath); + result = "SUCCESS"; break; case CSV_ACCOUNTCHARTS_IMPORT: datas = extractFile(filePath, charset.name()); - result = newImportService.importAccountAsCSV(datas); + importResult = newImportService.importAccountAsCSV(datas); + // TODO DCossé 24/07/14 change result + result = "SUCCESS"; break; case CSV_ENTRYBOOKS_IMPORT: datas = extractFile(filePath, charset.name()); - result = newImportService.importEntryBooksAsCSV(datas); + importResult = newImportService.importEntryBooksAsCSV(datas); + // TODO DCossé 24/07/14 change result + result = "SUCCESS"; break; case CSV_FINANCIALSTATEMENTS_IMPORT: datas = extractFile(filePath, charset.name()); @@ -198,7 +207,9 @@ break; case CSV_ENTRIES_IMPORT: datas = extractFile(filePath, charset.name()); - result = newImportService.importEntriesAsCSV(datas); + importResult = newImportService.importEntriesAsCSV(datas); + // TODO DCossé 24/07/14 change result + result = "SUCCESS"; break; case CSV_VAT_IMPORT: datas = extractFile(filePath, charset.name()); @@ -419,7 +430,7 @@ } } - protected String importAllFromZipFile(String filePath) { + protected List<ImportResult> importAllFromZipFile(String filePath) { ZipInputStream zipInputStream = null; String tmpDir = System.getProperty("java.io.tmpdir")+"/"; FileInputStream inputStream = null; @@ -462,6 +473,7 @@ IOUtils.closeQuietly(inputStream); } InputStream transactionsStream, entryBooksStream, fiscalPeriodsStream, entriesStream, accountsStream; + List<ImportResult> results; try { entryBooksStream = new FileInputStream(tmpDir + "entryBooks.csv"); String entryBooksStreamString = IOUtils.toString(entryBooksStream); @@ -484,7 +496,7 @@ String entriesStreamString = IOUtils.toString(entriesStream); IOUtils.closeQuietly(entriesStream); - newImportService.importAllAsCSV(entryBooksStreamString, transactionsStreamString, fiscalPeriodsStreamString, accountsStreamString, entriesStreamString); + results = newImportService.importAllAsCSV(entryBooksStreamString, transactionsStreamString, fiscalPeriodsStreamString, accountsStreamString, entriesStreamString); } catch (Exception ex) { if(log.isInfoEnabled()) { @@ -492,6 +504,6 @@ } throw new LimaTechnicalException("could not import files", ex); } - return "SUCCES"; + return results; } } Modified: trunk/lima-swing/src/main/resources/i18n/lima-swing_en_GB.properties =================================================================== --- trunk/lima-swing/src/main/resources/i18n/lima-swing_en_GB.properties 2014-07-25 08:47:33 UTC (rev 3867) +++ trunk/lima-swing/src/main/resources/i18n/lima-swing_en_GB.properties 2014-07-28 12:06:55 UTC (rev 3868) @@ -242,6 +242,9 @@ lima.filter.entrybook= lima.filter.letter= lima.filter.voucher= +lima.financialStatements.check= +lima.financialStatements.check.nothing= +lima.financialStatements.check.warn= lima.financialstatement.accounts=Account list on debit and on credit lima.financialstatement.addfinancialStatementHeadererror=Can't add financialStatementHeader lima.financialstatement.check=Check accounts passing to movement Modified: trunk/lima-swing/src/main/resources/i18n/lima-swing_fr_FR.properties =================================================================== --- trunk/lima-swing/src/main/resources/i18n/lima-swing_fr_FR.properties 2014-07-25 08:47:33 UTC (rev 3867) +++ trunk/lima-swing/src/main/resources/i18n/lima-swing_fr_FR.properties 2014-07-28 12:06:55 UTC (rev 3868) @@ -226,6 +226,9 @@ lima.filter.entrybook=Ajouter un filtre sur les journaux lima.filter.letter=Ajouter un filtre sur les lettres lima.filter.voucher=Ajouter un filtre sur les pièces comptables +lima.financialStatements.check= +lima.financialStatements.check.nothing= +lima.financialStatements.check.warn= lima.financialstatement.accounts=Liste de comptes au crédit et au débit lima.financialstatement.addfinancialStatementHeadererror=Erreur lors de l'ajout de l'entête sur la déclaration financière lima.financialstatement.check=Vérification des comptes aux postes