r3885 - in trunk: lima-business/src/main/java/org/chorem/lima/business/ejb lima-business/src/main/java/org/chorem/lima/business/ejb/csv lima-business/src/main/java/org/chorem/lima/business/ejb/ebp lima-business/src/test/java/org/chorem/lima/business lima-business-api/src/main/java/org/chorem/lima/business/api
Author: dcosse Date: 2014-07-31 18:36:13 +0200 (Thu, 31 Jul 2014) New Revision: 3885 Url: http://forge.chorem.org/projects/lima/repository/revisions/3885 Log: refs #1032 import des journaux depuis EBP Added: trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/ebp/EntryBookEBPModel.java Modified: trunk/lima-business-api/src/main/java/org/chorem/lima/business/api/ImportService.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/EntryBookServiceImpl.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/NewImportServiceImpl.java trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/csv/EntryModel.java trunk/lima-business/src/test/java/org/chorem/lima/business/ImportServiceImplTest.java trunk/lima-business/src/test/java/org/chorem/lima/business/NewImportExportServiceTest.java 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-31 15:40:32 UTC (rev 3884) +++ trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/EntryBookServiceImpl.java 2014-07-31 16:36:13 UTC (rev 3885) @@ -36,6 +36,8 @@ import org.chorem.lima.entity.EntryBookTopiaDao; import org.chorem.lima.entity.FinancialPeriod; import org.chorem.lima.entity.FinancialPeriodTopiaDao; +import org.nuiton.util.beans.Binder; +import org.nuiton.util.beans.BinderFactory; import javax.ejb.Remote; import javax.ejb.Stateless; @@ -62,7 +64,9 @@ boolean result; if (existingEntryBook != null) { result = true; - updateEntryBook(entryBook); + Binder binder = BinderFactory.newBinder(EntryBook.class, EntryBook.class); + binder.copy(entryBook, existingEntryBook, EntryBook.PROPERTY_LABEL); + updateEntryBook(existingEntryBook); } else { result = false; createNewEntryBook(entryBook); 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-31 15:40:32 UTC (rev 3884) +++ trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/ImportServiceImpl.java 2014-07-31 16:36:13 UTC (rev 3885) @@ -29,7 +29,6 @@ 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; import org.apache.pdfbox.exceptions.COSVisitorException; @@ -55,7 +54,6 @@ import org.chorem.lima.entity.Account; import org.chorem.lima.entity.AccountImpl; import org.chorem.lima.entity.EntryBook; -import org.chorem.lima.entity.EntryBookImpl; import org.chorem.lima.entity.Identity; import org.chorem.lima.entity.IdentityImpl; @@ -210,81 +208,6 @@ return result.toString(); } - @Override - public String importEntryBookFromEbp(String datas) throws ImportEbpException{ - 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 - - if (entryBookCodeIndex == null || entryBookLabelIndex == null) { - throw new ImportEbpException( - t("lima-business.import.noaccount")); - } - - // create entry book for each line - int count = 0; - String[] line = csvReader.readNext(); - while (line != null) { - 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(); - } - - if (log.isInfoEnabled()) { - long after = System.currentTimeMillis(); - log.info("Imported form EBP : " + count + " accounts in " + (after-before) + " ms"); - } - - } catch (IOException e) { - throw new ImportEbpException("Can't import", e); - } finally { - if (csvReader != null) { - try { - csvReader.close(); - } catch (IOException e) { - // on fait rien - } - } - } - - return result.toString(); - } - - // ################ IMPORT ################ /** 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-31 15:40:32 UTC (rev 3884) +++ trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/NewImportServiceImpl.java 2014-07-31 16:36:13 UTC (rev 3885) @@ -56,6 +56,7 @@ import org.chorem.lima.business.ejb.csv.FinancialTransactionModel; import org.chorem.lima.business.ejb.csv.FiscalPeriodModel; import org.chorem.lima.business.ejb.csv.VatStatementModel; +import org.chorem.lima.business.ejb.ebp.EntryBookEBPModel; import org.chorem.lima.business.ejb.ebp.EntryEBPModel; import org.chorem.lima.business.utils.EntryEBPComparator; import org.chorem.lima.business.utils.FiscalPeriodComparator; @@ -563,7 +564,7 @@ } @Override - public ImportResult importEntriesFromEbp(String datas) throws ImportEbpException { + public ImportResult importEntriesFromEbp(String datas) { ImportResult result = new ImportResult(); if (datas.isEmpty()) { @@ -736,4 +737,24 @@ } return result; } + + @Override + public ImportResult importEntryBookFromEbp(String datas) { + ImportResult result = new ImportResult(); + + ImportModel<EntryBook> model = new EntryBookEBPModel(); + + InputStream contentStream = IOUtils.toInputStream(datas); + Import<EntryBook> entryBooks = Import.newImport(model, contentStream); + + for (EntryBook entryEBP : entryBooks) { + boolean updated = entryBookService.createOrUpdateEntryBook(entryEBP); + if (updated) { + result.increaseUpdated(); + } else { + result.increaseCreated(); + } + } + return result; + } } Modified: trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/csv/EntryModel.java =================================================================== --- trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/csv/EntryModel.java 2014-07-31 15:40:32 UTC (rev 3884) +++ trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/csv/EntryModel.java 2014-07-31 16:36:13 UTC (rev 3885) @@ -49,7 +49,7 @@ newOptionalColumn("voucher", Entry.PROPERTY_VOUCHER); newOptionalColumn("description", Entry.PROPERTY_DESCRIPTION); newOptionalColumn("lettering", Entry.PROPERTY_LETTERING); - newOptionalColumn("financialTransaction", Entry.PROPERTY_FINANCIAL_TRANSACTION, FINANCIAL_TRANSACTION_ID_TO_FINANCIAL_TRANSACTION_PARSER); + newMandatoryColumn("financialTransaction", Entry.PROPERTY_FINANCIAL_TRANSACTION, FINANCIAL_TRANSACTION_ID_TO_FINANCIAL_TRANSACTION_PARSER); } @Override Copied: trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/ebp/EntryBookEBPModel.java (from rev 3883, trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/csv/EntryBookModel.java) =================================================================== --- trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/ebp/EntryBookEBPModel.java (rev 0) +++ trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/ebp/EntryBookEBPModel.java 2014-07-31 16:36:13 UTC (rev 3885) @@ -0,0 +1,76 @@ +package org.chorem.lima.business.ejb.ebp; + +/* + * #%L + * Lima :: business + * %% + * Copyright (C) 2008 - 2014 CodeLutin + * %% + * 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 3 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. + * + * You should have received a copy of the GNU General Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/gpl-3.0.html>. + * #L% + */ + +import org.chorem.lima.business.ejb.csv.AbstractLimaModel; +import org.chorem.lima.entity.EntryBook; +import org.chorem.lima.entity.EntryBookImpl; +import org.nuiton.csv.ExportModel; +import org.nuiton.csv.ExportableColumn; +import org.nuiton.csv.ModelBuilder; + +/** + * Created by davidcosse on 03/06/14. + */ +public class EntryBookEBPModel extends AbstractLimaModel<EntryBook> implements ExportModel<EntryBook> { + + public EntryBookEBPModel() { + super(','); + newMandatoryColumn("Code", EntryBook.PROPERTY_CODE); + newOptionalColumn("Libelle", EntryBook.PROPERTY_LABEL); + newIgnoredColumn("Type"); + newIgnoredColumn("TypePiece"); + newIgnoredColumn("NextPiece"); + newIgnoredColumn("CpteType"); + newIgnoredColumn("CpteCompte"); + newIgnoredColumn("Contact"); + newIgnoredColumn("RIBNomBanque"); + newIgnoredColumn("RIBAdrBanque"); + newIgnoredColumn("RIBAgence"); + newIgnoredColumn("RIBGuichet"); + newIgnoredColumn("RIBCompte"); + newIgnoredColumn("RIBCleRIB"); + newIgnoredColumn("Lcr"); + newIgnoredColumn("Preleve"); + newIgnoredColumn("MemoDateCreat"); + newIgnoredColumn("MemoDateModif"); + newIgnoredColumn("Devise"); + newIgnoredColumn("CompteTP"); + newIgnoredColumn("NumCptBqCH"); + newIgnoredColumn("NumClearing"); + newIgnoredColumn("bSaisieKM"); + } + + @Override + public Iterable<ExportableColumn<EntryBook, Object>> getColumnsForExport() { + ModelBuilder<EntryBook> modelBuilder = new ModelBuilder<EntryBook>(); + modelBuilder.newColumnForExport("Code", EntryBook.PROPERTY_CODE); + modelBuilder.newColumnForExport("Libelle", EntryBook.PROPERTY_LABEL); + return (Iterable) modelBuilder.getColumnsForExport(); + } + + @Override + public EntryBook newEmptyInstance() { + return new EntryBookImpl(); + } +} Modified: trunk/lima-business/src/test/java/org/chorem/lima/business/ImportServiceImplTest.java =================================================================== --- trunk/lima-business/src/test/java/org/chorem/lima/business/ImportServiceImplTest.java 2014-07-31 15:40:32 UTC (rev 3884) +++ trunk/lima-business/src/test/java/org/chorem/lima/business/ImportServiceImplTest.java 2014-07-31 16:36:13 UTC (rev 3885) @@ -66,28 +66,14 @@ // import files InputStream accountStream = null; - InputStream entryBookStream = null; - InputStream entriesStream = null; try { accountStream = ImportServiceImplTest.class.getResourceAsStream("/ebp/comptes.txt"); String accountData = IOUtils.toString(accountStream, "ISO-8859-1"); importService.importAccountsChartFromEbp(accountData); accountStream.close(); - - entryBookStream = ImportServiceImplTest.class.getResourceAsStream("/ebp/journaux.txt"); - String entryBookData = IOUtils.toString(entryBookStream, "ISO-8859-1"); - importService.importEntryBookFromEbp(entryBookData); - entryBookStream.close(); - -// entriesStream = ImportServiceImplTest.class.getResourceAsStream("/ebp/ecritures.txt"); -// String entriesData = IOUtils.toString(entriesStream, "ISO-8859-1"); -// importService.importEntriesFromEbp(entriesData); -// entriesStream.close(); } finally { IOUtils.closeQuietly(accountStream); - IOUtils.closeQuietly(entryBookStream); - IOUtils.closeQuietly(entriesStream); } } 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-31 15:40:32 UTC (rev 3884) +++ trunk/lima-business/src/test/java/org/chorem/lima/business/NewImportExportServiceTest.java 2014-07-31 16:36:13 UTC (rev 3885) @@ -30,6 +30,7 @@ import org.chorem.lima.entity.AccountImpl; import org.chorem.lima.entity.Entry; import org.chorem.lima.entity.EntryBook; +import org.chorem.lima.entity.EntryBookImpl; import org.chorem.lima.entity.FinancialStatement; import org.chorem.lima.entity.FinancialTransaction; import org.chorem.lima.entity.FiscalPeriod; @@ -40,6 +41,7 @@ import java.io.ByteArrayInputStream; import java.io.FileInputStream; import java.io.FileOutputStream; +import java.io.IOException; import java.io.InputStream; import java.nio.charset.Charset; import java.util.Collection; @@ -379,4 +381,25 @@ Assert.assertEquals(19, result.getNbCreated()); entriesStream.close(); } + + @Test + public void testImportEntryBooksFromEBP() throws IOException { + InputStream entryBookStream = NewImportExportServiceTest.class.getResourceAsStream("/ebp/journaux.txt"); + String entryBookData = IOUtils.toString(entryBookStream, "ISO-8859-1"); + entryBookStream.close(); + + // set somme already existing entryBooks + String [] entryBookCodes = {"AC","AN"}; + for (String entryBookCode : entryBookCodes) { + EntryBook entryBook = new EntryBookImpl(); + entryBook.setCode(entryBookCode); + entryBookService.createEntryBook(entryBook); + } + + ImportResult result = newImportService.importEntryBookFromEbp(entryBookData); + Assert.assertEquals(7, result.getNbCreated()); + Assert.assertEquals(2, result.getNbUpdated()); + EntryBook updatedEntryBook = entryBookService.getEntryBookByCode("AC"); + Assert.assertEquals("Achats de marchandises", updatedEntryBook.getLabel()); + } } Modified: trunk/lima-business-api/src/main/java/org/chorem/lima/business/api/ImportService.java =================================================================== --- trunk/lima-business-api/src/main/java/org/chorem/lima/business/api/ImportService.java 2014-07-31 15:40:32 UTC (rev 3884) +++ trunk/lima-business-api/src/main/java/org/chorem/lima/business/api/ImportService.java 2014-07-31 16:36:13 UTC (rev 3885) @@ -45,15 +45,6 @@ String importAccountsChartFromEbp(String data) throws ImportEbpException; /** - * Import entry books as EBP import. - * - * @param datas - * @return - * @throws LimaException - */ - String importEntryBookFromEbp(String datas) throws ImportEbpException; - - /** * Import content as CSV depending on import type. * * @param data import file content as string (remote service can't take File) 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-31 15:40:32 UTC (rev 3884) +++ trunk/lima-business-api/src/main/java/org/chorem/lima/business/api/NewImportService.java 2014-07-31 16:36:13 UTC (rev 3885) @@ -23,7 +23,6 @@ */ import org.chorem.lima.business.AlreadyExistAccountException; -import org.chorem.lima.business.ImportEbpException; import org.chorem.lima.business.ImportResult; import org.chorem.lima.business.InvalidAccountNumberException; @@ -48,5 +47,7 @@ List<ImportResult> importBackup(String entryBooks, String transactions, String fiscalPeriods, String accounts, String entries) throws AlreadyExistAccountException, InvalidAccountNumberException; - ImportResult importEntriesFromEbp(String datas) throws ImportEbpException; + ImportResult importEntriesFromEbp(String datas); + + ImportResult importEntryBookFromEbp(String datas); }
participants (1)
-
dcosse@users.chorem.org