r3824 - in trunk: lima-business/src/main/java/org/chorem/lima/business/ejb lima-callao/src/main/xmi lima-swing/src/main/java/org/chorem/lima/ui/importexport
Author: dcosse Date: 2014-05-28 17:51:32 +0200 (Wed, 28 May 2014) New Revision: 3824 Url: http://forge.chorem.org/projects/lima/repository/revisions/3824 Log: refs #1032 refactoring sur Import Export Modified: trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/ImportServiceImpl.java trunk/lima-callao/src/main/xmi/accounting-model.zargo trunk/lima-swing/src/main/java/org/chorem/lima/ui/importexport/ImportExport.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-05-28 15:00:29 UTC (rev 3823) +++ trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/ImportServiceImpl.java 2014-05-28 15:51:32 UTC (rev 3824) @@ -50,6 +50,9 @@ import javax.ejb.Stateless; import javax.ejb.TransactionAttribute; +import com.google.common.base.Function; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; import org.apache.commons.lang3.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -57,8 +60,6 @@ import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.interactive.form.PDField; import org.chorem.lima.FinancialStatementWayEnum; -import org.chorem.lima.beans.AccountEBP; -import org.chorem.lima.beans.AccountEBPImpl; import org.chorem.lima.beans.ClosedPeriodicEntryBookImport; import org.chorem.lima.beans.ClosedPeriodicEntryBookImportImpl; import org.chorem.lima.beans.EntryEBP; @@ -124,6 +125,19 @@ private static final Log log = LogFactory.getLog(ImportServiceImpl.class); + protected static final Function<Account, String> GET_ACCOUNT_NUMBER = new Function<Account, String>() { + @Override + public String apply(Account input) { + return input.getAccountNumber(); + } + }; + + protected static final Function<EntryBook, String> GET_ENTRY_BOOK_CODE = new Function<EntryBook, String>() { + @Override + public String apply(EntryBook input) { + return input.getCode(); + } + }; protected final String DATE_PATTERN = "dd/MM/yyyy"; @EJB @@ -171,6 +185,18 @@ // use for logs long before = System.currentTimeMillis(); + List<Account> accounts = accountService.getAllAccounts(); + if (accounts == null) { + accounts = Lists.newArrayList(); + } + Map<String, Account> indexedAccounts = Maps.newHashMap(Maps.uniqueIndex(accounts, GET_ACCOUNT_NUMBER)); + + List<EntryBook> entryBooks = entryBookService.getAllEntryBooks(); + if (entryBooks == null) { + entryBooks = Lists.newArrayList(); + } + Map<String, EntryBook> indexedEntryBooks = Maps.newHashMap(Maps.uniqueIndex(entryBooks, GET_ENTRY_BOOK_CODE)); + SimpleDateFormat epbDateFormat = new SimpleDateFormat(DATE_PATTERN); StringBuilder result = new StringBuilder(); CSVReader csvReader = null; @@ -184,14 +210,14 @@ // check if file have a good header List<String> headEntry = new ArrayList<String>(); - headEntry.add("DatEcr"); - headEntry.add("Journal"); - headEntry.add("Compte"); - headEntry.add("Libelle"); - headEntry.add("Piece"); - headEntry.add("Debit"); - headEntry.add("Credit"); - headEntry.add("Lettre"); + headEntry.add(EntryEBP.PROPERTY_DAT_ECR); + headEntry.add(EntryEBP.PROPERTY_JOURNAL); + headEntry.add(EntryEBP.PROPERTY_COMPTE); + headEntry.add(EntryEBP.PROPERTY_LIBELLE); + headEntry.add(EntryEBP.PROPERTY_PIECE); + headEntry.add(EntryEBP.PROPERTY_DEBIT); + headEntry.add(EntryEBP.PROPERTY_CREDIT); + headEntry.add(EntryEBP.PROPERTY_LETTRE); if (!Arrays.asList(strat.getColumnMapping()).containsAll(headEntry)) { throw new LimaBusinessException( t("lima-business.import.ebpnoheader")); @@ -232,7 +258,7 @@ dateEcr = epbDateFormat.parse(entryEBP.getDatEcr()); // account loading - account = accountService.getAccountByNumber(entryEBP.getCompte()); + account = indexedAccounts.get(entryEBP.getCompte()); // if entry date have fiscalperiod open if (dateEcr.compareTo(fiscalPeriods.get(0).getBeginDate()) < 0 @@ -243,6 +269,7 @@ // if account not exist not export -> exception else if (account == null) { + //// TODO DCossé 28/05/14 this message is not displayed on user's error window. throw new LimaBusinessException(t( "lima-business.import.ebpmissingaccount", entryEBP.getCompte())); @@ -282,7 +309,7 @@ entryBookCode = entryEBP.getJournal(); // entryBook loading - entryBook = entryBookService.getEntryBookByCode(entryBookCode); + entryBook = indexedEntryBooks.get(entryBookCode); // if entrybook not exist create it ! if (entryBook == null) { @@ -344,43 +371,59 @@ @Override public String importAccountsChartFromEbp(String datas) throws LimaException { long before = System.currentTimeMillis(); + List<Account> accounts = accountService.getAllAccounts(); + if (accounts == null) { + accounts = Lists.newArrayList(); + } + Map<String, Account> indexedAccounts = Maps.newHashMap(Maps.uniqueIndex(accounts, GET_ACCOUNT_NUMBER)); + StringBuilder result = new StringBuilder(); CSVReader csvReader = null; try { csvReader = new CSVReader(new StringReader(datas)); - ColumnPositionMappingStrategy<AccountEBPImpl> strat = new ColumnPositionMappingStrategy<AccountEBPImpl>(); - strat.setType(AccountEBPImpl.class); - // read header to set strategy mapping - strat.setColumnMapping(csvReader.readNext()); - // check if file have a good header - List<String> headAccount = new ArrayList<String>(); - headAccount.add("Numero"); - headAccount.add("Intitule"); - if (!Arrays.asList(strat.getColumnMapping()).containsAll( - headAccount)) { + + // check header is valid + String[] headers = csvReader.readNext(); + String columnName; + Integer numIndex = null; + Integer intituleIndex = null; + for (int i = 0; i < headers.length; i++) { + columnName = headers[i]; + if (columnName.equalsIgnoreCase("Numero")) { + numIndex = i; + } else if (columnName.equalsIgnoreCase("Intitule")) { + intituleIndex = i; + } + } + + int nbAccountCreated = 0; + if (numIndex != null && intituleIndex != null) { + String[] line; + line = csvReader.readNext(); + while (line != null) { + String accountNumber = line[numIndex]; + String label = line[intituleIndex]; + Account account = indexedAccounts.get(accountNumber); + // an account can be created but not updated. + if (account == null) { + account = new AccountImpl(); + account.setAccountNumber(accountNumber); + account.setLabel(label); + accountService.createAccount(account); + result.append(t("lima-business.import.accountadded", + accountNumber, label)); + nbAccountCreated++; + } + line = csvReader.readNext(); + } + } else { throw new LimaBusinessException( t("lima-business.import.noaccount")); } - // FIXME echatellier 20120424 read csv line by line without - // creating beans instance - CsvToBean<AccountEBPImpl> csv = new CsvToBean<AccountEBPImpl>(); - List<AccountEBPImpl> list = csv.parse(strat, csvReader); - - for (AccountEBP accountEBP : list) { - String accountNumber = accountEBP.getNumero(); - String label = accountEBP.getIntitule(); - Account account = new AccountImpl(); - account.setAccountNumber(accountNumber); - account.setLabel(label); - accountService.createAccount(account); - result.append(t("lima-business.import.accountadded", - accountNumber, label)); - } - if (log.isInfoEnabled()) { long after = System.currentTimeMillis(); - log.info("Imported form EBP : " + list.size() + " accounts in " + log.info("Imported form EBP : " + nbAccountCreated + " accounts in " + (after - before) + " ms"); } @@ -402,15 +445,34 @@ public String importEntryBookFromEbp(String datas) throws LimaException { long before = System.currentTimeMillis(); + List<EntryBook> entryBooks = entryBookService.getAllEntryBooks(); + if (entryBooks == null) { + entryBooks = Lists.newArrayList(); + } + Map<String, EntryBook> indexedEntryBooks = Maps.newHashMap(Maps.uniqueIndex(entryBooks, GET_ENTRY_BOOK_CODE)); + StringBuilder result = new StringBuilder(); CSVReader csvReader = null; try { csvReader = new CSVReader(new StringReader(datas)); + String[] headers = csvReader.readNext(); + String columnName; + Integer entryBookCodeIndex = null; + Integer entryBookLabelIndex = null; + for (int i = 0; i < headers.length; i++) { + columnName = headers[i]; + if (columnName.equalsIgnoreCase("Code")) { + entryBookCodeIndex = i; + } else if (columnName.equalsIgnoreCase("Libelle")) { + entryBookLabelIndex = i; + } + } + // check if file have a good header - String[] headers = csvReader.readNext(); - if (!headers[0].equals("Code") || !headers[2].equals("Libelle")) { + + if (entryBookCodeIndex == null || entryBookLabelIndex == null) { throw new LimaBusinessException( t("lima-business.import.noaccount")); } @@ -419,12 +481,16 @@ int count = 0; String[] line = csvReader.readNext(); while (line != null) { - EntryBook entryBook = new EntryBookImpl(); - entryBook.setCode(StringUtils.trimToNull(line[0])); - entryBook.setLabel(StringUtils.trimToNull(line[2])); - entryBookService.createEntryBook(entryBook); - result.append(t("lima-business.import.accountadded", entryBook.getCode(), entryBook.getLabel())); - count++; + String entryBookCode = line[entryBookCodeIndex]; + EntryBook entryBook = indexedEntryBooks.get(entryBookCode); + if (entryBook == null) { + entryBook = new EntryBookImpl(); + entryBook.setCode(StringUtils.trimToNull(line[entryBookCodeIndex])); + entryBook.setLabel(StringUtils.trimToNull(line[entryBookLabelIndex])); + entryBookService.createEntryBook(entryBook); + result.append(t("lima-business.import.accountadded", entryBook.getCode(), entryBook.getLabel())); + count++; + } line = csvReader.readNext(); } Modified: trunk/lima-callao/src/main/xmi/accounting-model.zargo =================================================================== (Binary files differ) 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-05-28 15:00:29 UTC (rev 3823) +++ trunk/lima-swing/src/main/java/org/chorem/lima/ui/importexport/ImportExport.java 2014-05-28 15:51:32 UTC (rev 3824) @@ -52,6 +52,7 @@ import com.google.common.base.Strings; import org.apache.commons.io.IOUtils; +import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.chorem.lima.business.api.ExportService; @@ -224,7 +225,7 @@ // display result dialog if (verboseMode) { String result = get(); - if (result.equals(null)) { + if (StringUtils.isBlank(result)) { JOptionPane.showMessageDialog(viewComponent, t("lima.ui.importexport.importerror"), t("lima.ui.importexport.importtitle"), JOptionPane.ERROR_MESSAGE);
participants (1)
-
dcosse@users.chorem.org