Author: dcosse Date: 2014-06-10 15:23:09 +0200 (Tue, 10 Jun 2014) New Revision: 3827 Url: http://forge.chorem.org/projects/lima/repository/revisions/3827 Log: #1032 utilisation de nuiton-csv migration en cours Added: trunk/lima-business-api/src/main/java/org/chorem/lima/business/api/NewExportService.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/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/ 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/AccountModel.java 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/csv/EntryModel.java trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/csv/FinancialTransactionModel.java trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/csv/FiscalPeriodModel.java trunk/lima-business/src/test/java/org/chorem/lima/business/NewExportServiceTest.java Modified: trunk/lima-business-api/src/main/java/org/chorem/lima/business/api/FinancialTransactionService.java trunk/lima-business/pom.xml trunk/lima-business/src/main/java/org/chorem/lima/LimaXAResource.java trunk/lima-business/src/main/java/org/chorem/lima/business/LimaConfig.java trunk/lima-business/src/main/java/org/chorem/lima/business/LimaInterceptor.java trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/EntryServiceImpl.java trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/FinancialTransactionServiceImpl.java trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/FiscalPeriodServiceImpl.java trunk/lima-business/src/main/java/org/chorem/lima/business/migration/DatabaseMigrationClass.java trunk/lima-business/src/main/java/org/chorem/lima/business/migration/MigrationV0V0_5.java trunk/lima-business/src/main/java/org/chorem/lima/business/migration/MigrationV0_6.java trunk/lima-business/src/main/java/org/chorem/lima/business/migration/MigrationV0_8.java trunk/lima-business/src/main/resources/i18n/lima-business_en_GB.properties trunk/lima-business/src/main/resources/i18n/lima-business_fr_FR.properties trunk/lima-business/src/test/java/org/chorem/lima/business/AbstractLimaTest.java trunk/lima-business/src/test/java/org/chorem/lima/business/AccountServiceImplTest.java trunk/lima-business/src/test/java/org/chorem/lima/business/EntryBookServiceImplTest.java trunk/lima-business/src/test/java/org/chorem/lima/business/FinancialPeriodServiceImplTest.java trunk/lima-business/src/test/java/org/chorem/lima/business/FinancialTransactionServiceImplTest.java trunk/lima-business/src/test/java/org/chorem/lima/business/FiscalPeriodServiceImplTest.java trunk/lima-business/src/test/java/org/chorem/lima/business/ImportServiceImplTest.java trunk/lima-business/src/test/java/org/chorem/lima/business/ReportServiceImplTest.java trunk/lima-business/src/test/java/org/chorem/lima/business/accountingrules/AccountServiceRuleFrTest.java trunk/lima-business/src/test/java/org/chorem/lima/business/accountingrules/TestAccountingRules.java trunk/lima-business/src/test/java/org/chorem/lima/entity/AccountDAOTest.java trunk/lima-business/src/test/java/org/chorem/lima/entity/FinancialTransactionDAOTest.java trunk/lima-callao/src/main/java/org/chorem/lima/DefaultServiceContext.java trunk/lima-callao/src/main/xmi/accounting-model.properties trunk/lima-swing/src/main/java/org/chorem/lima/ui/importexport/ImportExport.java trunk/lima-swing/src/main/java/org/chorem/lima/ui/ledger/LedgerViewHandler.java trunk/pom.xml Modified: trunk/lima-business/pom.xml =================================================================== --- trunk/lima-business/pom.xml 2014-06-03 09:36:17 UTC (rev 3826) +++ trunk/lima-business/pom.xml 2014-06-10 13:23:09 UTC (rev 3827) @@ -70,6 +70,10 @@ <artifactId>nuiton-config</artifactId> </dependency> <dependency> + <groupId>org.nuiton</groupId> + <artifactId>nuiton-csv</artifactId> + </dependency> + <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> </dependency> Modified: trunk/lima-business/src/main/java/org/chorem/lima/LimaXAResource.java =================================================================== --- trunk/lima-business/src/main/java/org/chorem/lima/LimaXAResource.java 2014-06-03 09:36:17 UTC (rev 3826) +++ trunk/lima-business/src/main/java/org/chorem/lima/LimaXAResource.java 2014-06-10 13:23:09 UTC (rev 3827) @@ -74,7 +74,7 @@ @Override public void end(Xid arg0, int arg1) throws XAException { /*try { - context.closeContext(); + context.close(); } catch (TopiaException ex) { throw new XAException(XAException.XA_HEURCOM); }*/ Modified: trunk/lima-business/src/main/java/org/chorem/lima/business/LimaConfig.java =================================================================== --- trunk/lima-business/src/main/java/org/chorem/lima/business/LimaConfig.java 2014-06-03 09:36:17 UTC (rev 3826) +++ trunk/lima-business/src/main/java/org/chorem/lima/business/LimaConfig.java 2014-06-10 13:23:09 UTC (rev 3827) @@ -31,6 +31,7 @@ import org.chorem.lima.business.accountingrules.FranceAccountingRules; import org.chorem.lima.business.migration.DatabaseMigrationClass; import org.chorem.lima.entity.LimaCallaoEntityEnum; +import org.nuiton.topia.migration.TopiaMigrationEngine; import org.nuiton.topia.persistence.TopiaConfigurationConstants; import org.nuiton.topia.migration.TopiaMigrationService; import org.nuiton.config.ApplicationConfig; @@ -65,9 +66,8 @@ // load default options super(Option.class, null, null, null); - // TODO DCossé 19/05/14 disabled until migration service is refactored. - //setOption("topia.service.migration", TopiaMigrationEngine.class.getName()); - setOption(TopiaMigrationService.MIGRATION_CALLBACK, DatabaseMigrationClass.class.getName()); + setOption("topia.service.migration", TopiaMigrationEngine.class.getName()); + setOption("topia.service.migration.callback", DatabaseMigrationClass.class.getName()); setOption(TopiaMigrationService.MIGRATION_SHOW_SQL, Boolean.TRUE.toString()); setOption(TopiaMigrationService.MIGRATION_SHOW_PROGRESSION, Boolean.TRUE.toString()); Modified: trunk/lima-business/src/main/java/org/chorem/lima/business/LimaInterceptor.java =================================================================== --- trunk/lima-business/src/main/java/org/chorem/lima/business/LimaInterceptor.java 2014-06-03 09:36:17 UTC (rev 3826) +++ trunk/lima-business/src/main/java/org/chorem/lima/business/LimaInterceptor.java 2014-06-10 13:23:09 UTC (rev 3827) @@ -82,7 +82,8 @@ // enter in service layer // interceptor will be called before each internal // ejb call, but opening a new transaction in not required - if (DAO_HELPER.get() == null) { + LimaCallaoTopiaDaoSupplier supplier = DAO_HELPER.get(); + if (supplier == null) { // maybe take care of TransactionAttributeTypes ? if (context.getTarget().getClass().getAnnotation(TransactionAttribute.class) != null || @@ -96,11 +97,6 @@ LimaCallaoTopiaApplicationContext rootContext = TopiaApplicationContextCache.getContext(config.getFlatOptions(), CREATE_CONTEXT_FUNCTION); - if (!schemaExistChecked && rootContext.isSchemaEmpty()) { - rootContext.createSchema(); - schemaExistChecked = true; - } - LimaCallaoTopiaPersistenceContext tx = rootContext.newPersistenceContext(); DAO_HELPER.set(tx); Modified: trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/EntryServiceImpl.java =================================================================== --- trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/EntryServiceImpl.java 2014-06-03 09:36:17 UTC (rev 3826) +++ trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/EntryServiceImpl.java 2014-06-10 13:23:09 UTC (rev 3827) @@ -33,6 +33,7 @@ import javax.ejb.Remote; import javax.ejb.Stateless; import javax.ejb.TransactionAttribute; +import java.util.Collection; /** * Created by davidcosse on 17/01/14. @@ -44,13 +45,14 @@ @Override public Entry createEntry(Entry entry) { - + Entry result; try { EntryTopiaDao entryDao = getDaoHelper().getEntryDao(); - entryDao.create(entry); + result = entryDao.create(entry); } catch (TopiaException e) { throw new LimaException("Can't create entry", e); } - return null; + return result; } + } Modified: trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/FinancialTransactionServiceImpl.java =================================================================== --- trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/FinancialTransactionServiceImpl.java 2014-06-03 09:36:17 UTC (rev 3826) +++ trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/FinancialTransactionServiceImpl.java 2014-06-10 13:23:09 UTC (rev 3827) @@ -100,7 +100,7 @@ @Override public FinancialTransaction createFinancialTransaction(FinancialTransaction financialtransaction) throws LimaException { - FinancialTransaction fTransaction; + FinancialTransaction result; AccountingRules accountingRules = LimaConfig.getInstance().getAccountingRules(); try { @@ -109,12 +109,12 @@ FinancialTransactionTopiaDao financialtransactionTopiaDao = getDaoHelper().getFinancialTransactionDao(); - fTransaction = financialtransactionTopiaDao.create(financialtransaction); + result = financialtransactionTopiaDao.create(financialtransaction); } catch (TopiaException ex) { throw new LimaException("Can't create financial transaction", ex); } - return fTransaction; + return result; } /** @@ -519,6 +519,13 @@ } @Override + public FinancialTransaction getFinancialTransactionWithId(String id) throws LimaException { + FinancialTransactionTopiaDao transactionTopiaDao = getDaoHelper().getFinancialTransactionDao(); + FinancialTransaction result = transactionTopiaDao.findByTopiaId(id); + return result; + } + + @Override public Entry createEntry(Entry entry) throws LimaException { AccountingRules accountingRules = LimaConfig.getInstance().getAccountingRules(); @@ -572,9 +579,10 @@ AccountingRules accountingRules = LimaConfig.getInstance().getAccountingRules(); try { + FinancialTransaction financialTransaction = entry.getFinancialTransaction(); + //check if the financial period is blocked - accountingRules.checkFinancialPeriodBlockedWithFinancialTransaction( - entry.getFinancialTransaction()); + accountingRules.checkFinancialPeriodBlockedWithFinancialTransaction(financialTransaction); EntryTopiaDao entryTopiaDao = getDaoHelper().getEntryDao(); Entry entryOld = entryTopiaDao.findByTopiaId(entry.getTopiaId()); 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-06-03 09:36:17 UTC (rev 3826) +++ trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/FiscalPeriodServiceImpl.java 2014-06-10 13:23:09 UTC (rev 3827) @@ -25,6 +25,7 @@ package org.chorem.lima.business.ejb; +import com.google.common.collect.Lists; import org.apache.commons.lang3.time.DateUtils; import org.chorem.lima.beans.BalanceTrial; import org.chorem.lima.beans.ReportsDatas; @@ -52,6 +53,7 @@ import org.chorem.lima.entity.FinancialPeriodTopiaDao; import org.chorem.lima.entity.FinancialTransaction; import org.chorem.lima.entity.FinancialTransactionImpl; +import org.chorem.lima.entity.FinancialTransactionTopiaDao; import org.chorem.lima.entity.FiscalPeriod; import org.chorem.lima.entity.FiscalPeriodTopiaDao; import org.nuiton.topia.persistence.TopiaException; @@ -104,6 +106,8 @@ try { FiscalPeriodTopiaDao fiscalPeriodTopiaDao = getDaoHelper().getFiscalPeriodDao(); + FinancialPeriodTopiaDao financialPeriodTopiaDao = getDaoHelper().getFinancialPeriodDao(); + ClosedPeriodicEntryBookTopiaDao closedPeriodicEntryBookTopiaDao = getDaoHelper().getClosedPeriodicEntryBookDao(); EntryBookTopiaDao entryBookTopiaDao = getDaoHelper().getEntryBookDao(); @@ -124,25 +128,28 @@ // create fiscalPeriod.addAllFinancialPeriod(financialPeriods); - result = fiscalPeriodTopiaDao.create(fiscalPeriod); //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(); + ClosedPeriodicEntryBook closedPeriodicEntryBook = new ClosedPeriodicEntryBookImpl(); // set entrybook closedPeriodicEntryBook.setEntryBook(entryBook); // set financial period closedPeriodicEntryBook.setFinancialPeriod(financialPeriod); - // create it - closedPeriodicEntryBookTopiaDao.create(closedPeriodicEntryBook); + + closedPeriodicEntryBooks.add(closedPeriodicEntryBook); } + financialPeriod.addAllEntryBookClosedPeriodicEntryBook(closedPeriodicEntryBooks); + } + result = fiscalPeriodTopiaDao.create(fiscalPeriod); + } catch (TopiaException ex) { throw new LimaException("Can't create period", ex); } Copied: trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/NewExportServiceImpl.java (from rev 3825, trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/ExportServiceImpl.java) =================================================================== --- trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/NewExportServiceImpl.java (rev 0) +++ trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/NewExportServiceImpl.java 2014-06-10 13:23:09 UTC (rev 3827) @@ -0,0 +1,231 @@ +/* + * #%L + * Lima business + * + * $Id: ExportServiceImpl.java 3781 2014-05-06 09:22:03Z dcosse $ + * $HeadURL: http://svn.chorem.org/lima/trunk/lima-business/src/main/java/org/chorem/lima... $ + * %% + * Copyright (C) 2008 - 2012 CodeLutin, Chatellier Eric + * %% + * 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% + */ + +package org.chorem.lima.business.ejb; + +import com.google.common.collect.Lists; +import org.apache.commons.io.FileUtils; +import org.chorem.lima.business.LimaException; +import org.chorem.lima.business.api.AccountService; +import org.chorem.lima.business.api.EntryBookService; +import org.chorem.lima.business.api.FinancialTransactionService; +import org.chorem.lima.business.api.NewExportService; +import org.chorem.lima.business.ejb.csv.AccountModel; +import org.chorem.lima.business.ejb.csv.EntryBookModel; +import org.chorem.lima.business.ejb.csv.EntryModel; +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.Export; + + +import javax.ejb.EJB; +import javax.ejb.Remote; +import javax.ejb.Stateless; +import javax.ejb.TransactionAttribute; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.nio.charset.Charset; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.List; +import java.util.zip.ZipEntry; +import java.util.zip.ZipOutputStream; + +import static org.nuiton.i18n.I18n.t; + +/** + * CSV import export service. + * + * @version $Revision: 3781 $ + * <p/> + * Last update : $Date: 2014-05-06 11:22:03 +0200 (mar. 06 mai 2014) $ + * By : $Author: dcosse $ + */ +@Stateless +@Remote(NewExportService.class) +@TransactionAttribute +public class NewExportServiceImpl extends AbstractLimaService implements NewExportService { + + protected final String DATE_PATTERN = "dd-MM-yyyy-HH:mm"; + + @EJB + private EntryBookService entryBookService; + + @EJB + private AccountService accountService; + + @EJB + private FinancialTransactionService financialTransactionService; + + @Override + public String exportAllAsCSV(String path, String charset) throws LimaException { + List<File> files = Lists.newArrayList(); + files.add(exportAccountsAsCSV(path + t("lima-business.document.accounts") + ".csv", charset)); + files.add(exportEntryBooksAsCSV(path + t("lima-business.document.entryBooks") + ".csv", charset)); + files.add(exportFiscalPeriodAsCSV(path + t("lima-business.document.fiscalPeriods") + ".csv", charset)); + files.add(exportFinancialTransactionsAsCSV(path + t("lima-business.document.financialTransactions") + ".csv", charset)); + files.add(exportEntriesAsCSV(path + t("lima-business.document.entries") + ".csv", charset)); + + SimpleDateFormat exportDate = new SimpleDateFormat(DATE_PATTERN); + String filePath = path + "LIMA-BACKUP-"+ exportDate.format(new Date()) +".zip"; + ZipOutputStream export = null; + try { + FileOutputStream out = new FileOutputStream(filePath); + export = new ZipOutputStream(out); + + for (File file : files) { + ZipEntry ze= new ZipEntry(file.getName()); + export.putNextEntry(ze); + int len; + byte[] buffer = new byte[1024]; + FileInputStream stream = new FileInputStream(file); + while ((len = stream.read(buffer)) > 0) { + export.write(buffer, 0, len); + } + stream.close(); + FileUtils.forceDelete(file); + } + + } catch (Exception e) { + throw new LimaException("Can't export All",e); + } finally { + if(export != null) { + //remember close it + try { + export.closeEntry(); + export.close(); + } catch (IOException e) { + // nothing to do + } + } + } + + return filePath; + } + + @Override + public File exportAccountsAsCSV(String path, String charset) throws LimaException { + File file; + try { + + AccountTopiaDao accountTopiaDao = getDaoHelper().getAccountDao(); + List<Account> entities = accountTopiaDao.findAll(); + + file = new File(path); + AccountModel model = new AccountModel(); + Export.exportToFile(model, entities, file, Charset.forName(charset)); + new FileInputStream(file); + + } catch (Exception ex) { + throw new LimaException("Can't export accounts", ex); + } + return file; + } + + @Override + public File exportEntryBooksAsCSV(String path, String charset) throws LimaException { + File file; + try { + + EntryBookTopiaDao entryBookTopiaDao = getDaoHelper().getEntryBookDao(); + List<EntryBook> entities = entryBookTopiaDao.findAll(); + + file = new File(path); + EntryBookModel model = new EntryBookModel(); + Export.exportToFile(model, entities, file, Charset.forName(charset)); + new FileInputStream(file); + + } catch (Exception ex) { + throw new LimaException("Can't export entry books", ex); + } + return file; + } + + @Override + public File exportFiscalPeriodAsCSV(String path, String charset) throws LimaException { + File file; + try { + FiscalPeriodTopiaDao dao = getDaoHelper().getFiscalPeriodDao(); + List<FiscalPeriod> entities = dao.findAll(); + + file = new File(path); + FiscalPeriodModel model = new FiscalPeriodModel(); + Export.exportToFile(model, entities, file, Charset.forName(charset)); + new FileInputStream(file); + } catch (Exception ex) { + throw new LimaException("Can't export financial transactions", ex); + } + return file; + } + + + @Override + public File exportFinancialTransactionsAsCSV(String path, String charset) throws LimaException { + File file; + try { + FinancialTransactionTopiaDao financialTransactionTopiaDao = + getDaoHelper().getFinancialTransactionDao(); + List<FinancialTransaction> entities = financialTransactionTopiaDao.findAll(); + + file = new File(path); + FinancialTransactionModel model = new FinancialTransactionModel(entryBookService); + Export.exportToFile(model, entities, file, Charset.forName(charset)); + new FileInputStream(file); + } catch (Exception ex) { + throw new LimaException("Can't export financial transactions", ex); + } + return file; + } + + @Override + public File exportEntriesAsCSV(String path, String charset) throws LimaException { + File file; + try { + EntryTopiaDao dao = getDaoHelper().getEntryDao(); + List<Entry> entities = dao.findAll(); + + file = new File(path); + EntryModel model = new EntryModel(accountService, financialTransactionService); + Export.exportToFile(model, entities, file, Charset.forName(charset)); + new FileInputStream(file); + } catch (Exception ex) { + throw new LimaException("Can't export financial transactions", ex); + } + return file; + } + +} Added: 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 (rev 0) +++ trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/NewImportServiceImpl.java 2014-06-10 13:23:09 UTC (rev 3827) @@ -0,0 +1,229 @@ +package org.chorem.lima.business.ejb; + +import com.google.common.base.Function; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import org.apache.commons.io.IOUtils; +import org.chorem.lima.business.LimaException; +import org.chorem.lima.business.api.AccountService; +import org.chorem.lima.business.api.EntryBookService; +import org.chorem.lima.business.api.FinancialTransactionService; +import org.chorem.lima.business.api.NewImportService; +import org.chorem.lima.business.ejb.csv.AccountModel; +import org.chorem.lima.business.ejb.csv.EntryBookModel; +import org.chorem.lima.business.ejb.csv.EntryModel; +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.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.FileInputStream; +import java.io.FileOutputStream; +import java.io.InputStream; +import java.util.Collection; +import java.util.List; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; + +import static org.nuiton.i18n.I18n.t; + +/** + * Created by davidcosse on 03/06/14. + */ +@Stateless +@Remote(NewImportService.class) +@TransactionAttribute +public class NewImportServiceImpl extends AbstractLimaService implements NewImportService { + + @EJB + private EntryBookService entryBookService; + + @EJB + private AccountService accountService; + + @EJB + private FinancialTransactionService financialTransactionService; + + @Override + public void importAccountAsCSV(String contents) throws LimaException { + InputStream contentStream = IOUtils.toInputStream(contents); + + 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); + } + } + dao.createAll(newAccounts); + } finally { + IOUtils.closeQuietly(contentStream); + } + + } + + @Override + public void importEntryBooksAsCSV(String contents) throws LimaException { + InputStream contentStream = IOUtils.toInputStream(contents); + try { + ImportModel<EntryBook> model = new EntryBookModel(); + + EntryBookTopiaDao dao = getDaoHelper().getEntryBookDao(); + Import<EntryBook> result = Import.newImport(model, contentStream); + dao.createAll(result); + } finally { + IOUtils.closeQuietly(contentStream); + } + } + + @Override + public void importFiscalPeriodsAsCSV(String contents) throws LimaException { + InputStream contentStream = IOUtils.toInputStream(contents); + try { + ImportModel<FiscalPeriod> model = new FiscalPeriodModel(); + + FiscalPeriodTopiaDao dao = getDaoHelper().getFiscalPeriodDao(); + Import<FiscalPeriod> result = Import.newImport(model, contentStream); + dao.createAll(result); + } finally { + IOUtils.closeQuietly(contentStream); + } + } + + @Override + public void importFinancialTransactionsAsCSV(String contents) throws LimaException { + // 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); + } finally { + IOUtils.closeQuietly(contentStream); + } + } + + 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 void importEntriesAsCSV(String contents) throws LimaException { + // import and save entries + InputStream contentStream = IOUtils.toInputStream(contents); + try { + FinancialTransactionTopiaDao financialTransactionTopiaDao = getDaoHelper().getFinancialTransactionDao(); + + ImportModel<Entry> model = new EntryModel(accountService, financialTransactionService); + Import<Entry> result = Import.newImport(model, contentStream); + + Collection<FinancialTransaction> financialTransactions = financialTransactionTopiaDao.findAll(); + ImmutableMap<String, FinancialTransaction> indexedFinancialTransactions = Maps.uniqueIndex(financialTransactions, GET_TOPIA_ID); + + for (Entry entry : result) { + FinancialTransaction financialTransaction = entry.getFinancialTransaction(); + financialTransaction = indexedFinancialTransactions.get(financialTransaction.getTopiaId()); + Collection<Entry> fEntries = financialTransaction.getEntry(); + if(fEntries == null) { + fEntries = Lists.newArrayList(); + financialTransaction.setEntry(fEntries); + } + fEntries.add(entry); + } + + financialTransactionTopiaDao.updateAll(indexedFinancialTransactions.values()); + + } finally { + IOUtils.closeQuietly(contentStream); + } + } + + @Override + public void importAllAsCSV(String path) throws LimaException { + FileInputStream contentStream; + ZipInputStream zipInputStream = null; + try { + contentStream = new FileInputStream(path); + zipInputStream = new ZipInputStream(contentStream); + + ZipEntry entry; + while ((entry = zipInputStream.getNextEntry()) != null) { + byte[] buffer = new byte[2048]; + FileOutputStream fileoutputstream = null; + + if (entry.getName().equalsIgnoreCase(t("lima-business.document.accounts") + ".csv")) { + fileoutputstream = new FileOutputStream(t("lima-business.document.accounts")); + } else if (entry.getName().equalsIgnoreCase(t("lima-business.document.entryBooks") + ".csv")) { + fileoutputstream = new FileOutputStream(t("lima-business.document.entryBooks")); + } else if (entry.getName().equalsIgnoreCase(t("lima-business.document.fiscalPeriods") + ".csv")) { + fileoutputstream = new FileOutputStream(t("lima-business.document.fiscalPeriods")); + } else if (entry.getName().equalsIgnoreCase(t("lima-business.document.financialTransactions") + ".csv")) { + fileoutputstream = new FileOutputStream(t("lima-business.document.financialTransactions")); + } else if (entry.getName().equalsIgnoreCase(t("lima-business.document.entries") + ".csv")) { + fileoutputstream = new FileOutputStream(t("lima-business.document.entries")); + } + int n; + + if (fileoutputstream != null) { + while ((n = zipInputStream.read(buffer, 0, 2048)) > -1) { + fileoutputstream.write(buffer, 0, n); + } + fileoutputstream.close(); + } + + zipInputStream.closeEntry(); + } + + InputStream transactionsStream = new FileInputStream(t("lima-business.document.financialTransactions")); + String transactionsStreamString = IOUtils.toString(transactionsStream); + importFinancialTransactionsAsCSV(transactionsStreamString); + + InputStream entryBooksStream = new FileInputStream(t("lima-business.document.entryBooks")); + String entryBooksStreamString = IOUtils.toString(entryBooksStream); + importEntryBooksAsCSV(entryBooksStreamString); + + InputStream fiscalPeriodsStream = new FileInputStream(t("lima-business.document.fiscalPeriods")); + String fiscalPeriodsStreamString = IOUtils.toString(fiscalPeriodsStream); + importFiscalPeriodsAsCSV(fiscalPeriodsStreamString); + + InputStream entriesStream = new FileInputStream(t("lima-business.document.entries")); + String entriesStreamString = IOUtils.toString(entriesStream); + importEntriesAsCSV(entriesStreamString); + + InputStream accountsStream = new FileInputStream(t("lima-business.document.accounts")); + String accountsStreamString = IOUtils.toString(accountsStream); + importAccountAsCSV(accountsStreamString); + + } catch (Exception e) { + throw new LimaException("Could not import", e); + } finally { + IOUtils.closeQuietly(zipInputStream); + } + } +} Added: 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 (rev 0) +++ trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/csv/AbstractLimaModel.java 2014-06-10 13:23:09 UTC (rev 3827) @@ -0,0 +1,298 @@ +package org.chorem.lima.business.ejb.csv; + +import com.google.common.base.Strings; +import org.apache.commons.lang3.StringUtils; +import org.chorem.lima.business.api.AccountService; +import org.chorem.lima.business.api.EntryBookService; +import org.chorem.lima.business.api.FinancialTransactionService; +import org.chorem.lima.entity.Account; +import org.chorem.lima.entity.EntryBook; +import org.chorem.lima.entity.FinancialTransaction; +import org.nuiton.csv.ValueFormatter; +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; +import java.util.Date; + +/** + * Created by davidcosse on 03/06/14. + */ +public abstract class AbstractLimaModel<E> extends AbstractImportModel<E> { + + + protected static EntryBookService entryBookService; + + protected static FinancialTransactionService financialTransactionService; + + protected static AccountService accountService; + + public AbstractLimaModel(char separator) { + super(separator); + } + + /** + * String to integer converter. + */ + protected static final ValueParser<Integer> INT_PARSER = new ValueParser<Integer>() { + @Override + public Integer parse(String value) throws ParseException { + int result = 0; + if (!Strings.isNullOrEmpty(value)) { + result = Integer.valueOf(value); + } + return result; + } + }; + + /** + * String to integer converter (null allowed). + */ + protected static final ValueParser<Integer> INTEGER_WITH_NULL_PARSER = new ValueParser<Integer>() { + @Override + public Integer parse(String value) throws ParseException { + Integer result = null; + if (!Strings.isNullOrEmpty(value)) { + result = Integer.valueOf(value); + } + return result; + } + }; + + /** + * String to double converter. + * Can handle , or . as decimal separator and ' ' as thousand separator + */ + protected static final ValueParser<Double> DOUBLE_PARSER = new ValueParser<Double>() { + @Override + public Double parse(String value) throws ParseException { + double result = 0; + if (!value.isEmpty()) { + // " " est un espace insécable, pas un " " + result = Double.valueOf(value.replace(',','.').replace(" ", "")); + } + return result; + } + }; + + /** + * String to double converter (null allowed). + * Can handle , or . as decimal separator and ' ' as thousand separator + */ + protected static final ValueParser<Double> DOUBLE_WITH_NULL_PARSER = new ValueParser<Double>() { + @Override + public Double parse(String value) throws ParseException { + Double result = null; + if (!value.isEmpty()) { + // " " est un espace insécable, pas un " " + result = Double.valueOf(value.replace(',','.').replace(" ", "")); + } + return result; + } + }; + + /** + * Zero to empty string converter. + */ + protected static final ValueParser<String> ZERO_TO_EMPTY_PARSER = new ValueParser<String>() { + @Override + public String parse(String value) throws ParseException { + String result = value.trim(); + result = result.replaceAll("\\s{2,}", " "); // replace multiple spaces + if (result.equals("0")) { + result = ""; + } + return result; + } + }; + + /** + * Date converter converter. + */ + protected static final ValueParser<Date> DATE_PARSER = new ValueParser<Date>() { + @Override + public Date parse(String value) throws ParseException { + Date result = null; + if (!Strings.isNullOrEmpty(value)) { + SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy"); + result = format.parse(value); + } + return result; + } + }; + + /** + * O/N boolean parser. + */ + protected static final ValueParser<Boolean> O_N_PARSER = new ValueParser<Boolean>() { + @Override + public Boolean parse(String value) throws ParseException { + Boolean result; + if ("O".equalsIgnoreCase(value)) { + result = Boolean.TRUE; + } else { + result = Boolean.FALSE; + } + return result; + } + }; + + protected static final ValueFormatter<Boolean> O_N_FORMATTER = new ValueFormatter<Boolean>() { + @Override + public String format(Boolean value) { + String result = value ? "O" : "N"; + return result; + } + }; + + protected static final ValueFormatter<Integer> INTEGER_FORMATTER = new ValueFormatter<Integer>() { + @Override + public String format(Integer value) { + String result; + if (value != null) { + result = String.valueOf(value); + } else { + result = ""; + } + return result; + } + }; + + protected static final ValueFormatter<Double> DOUBLE_FORMATTER = new ValueFormatter<Double>() { + @Override + public String format(Double value) { + String result; + if (value != null) { + result = String.valueOf(value); + } else { + result = ""; + } + return result; + } + }; + + protected static final ValueFormatter<Date> DATE_FORMATTER = new ValueFormatter<Date>() { + @Override + public String format(Date value) { + String result; + if (value != null) { + SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy"); + result = format.format(value); + } else { + result = ""; + } + return result; + } + }; + + protected static final ValueFormatter<EntryBook> ENTRY_BOOK_TO_ENTRY_BOOK_CODE_FORMATTER = new ValueFormatter<EntryBook>() { + @Override + public String format(EntryBook value) { + String result; + if (value != null) { + result = value.getCode(); + } else { + result = ""; + } + return result; + } + }; + + protected static final ValueParser<EntryBook> ENTRY_BOOK_CODE_TO_ENTRY_BOOK_PARSER = new ValueParser<EntryBook>() { + + @Override + public EntryBook parse(String value) { + EntryBook result; + if (StringUtils.isNotBlank(value)) { + result = entryBookService.getEntryBookByCode(value); + } else { + result = null; + } + return result; + } + }; + + protected static final ValueParser<BigDecimal> BIG_DECIMAL_WITH_NULL_PARSER = new ValueParser<BigDecimal>() { + @Override + public BigDecimal parse(String value) throws ParseException { + BigDecimal result = null; + if (!value.isEmpty()) { + // " " est un espace insécable, pas un " " + Double dval = Double.valueOf(value.replace(',', '.').replace(" ", "")); + result = BigDecimal.valueOf(dval); + } + return result; + } + }; + + protected static final ValueFormatter<BigDecimal> BIG_DECIMAL_FORMATTER = new ValueFormatter<BigDecimal>() { + @Override + public String format(BigDecimal value) { + String result; + if (value != null) { + result = String.valueOf(value); + } else { + result = ""; + } + return result; + } + }; + + protected static final ValueFormatter<Account> ACCOUNT_TO_ACCOUNT_NUMBER_FORMATTER = new ValueFormatter<Account>() { + @Override + public String format(Account value) { + String result; + if (value != null) { + result = value.getAccountNumber(); + } else { + result = ""; + } + return result; + } + }; + + protected static final ValueParser<Account> ACCOUNT_NUMBER_TO_ACCOUNT_TRANSACTION_PARSER = new ValueParser<Account>() { + + @Override + public Account parse(String value) { + Account result; + if (StringUtils.isNotBlank(value)) { + result = accountService.getAccountByNumber(value); + } else { + result = null; + } + return result; + } + }; + + protected static final ValueFormatter<FinancialTransaction> FINANCIAL_TRANSACTION_TO_FINANCIAL_TRANSACTION_ID_FORMATTER = new ValueFormatter<FinancialTransaction>() { + @Override + public String format(FinancialTransaction value) { + String result; + if (value != null) { + result = value.getTopiaId(); + } else { + result = ""; + } + return result; + } + }; + + protected static final ValueParser<FinancialTransaction> FINANCIAL_TRANSACTION_ID_TO_FINANCIAL_TRANSACTION_PARSER = new ValueParser<FinancialTransaction>() { + + @Override + public FinancialTransaction parse(String value) { + FinancialTransaction result; + if (StringUtils.isNotBlank(value)) { + result = financialTransactionService.getFinancialTransactionWithId(value); + } else { + result = null; + } + return result; + } + }; + +} Added: trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/csv/AccountModel.java =================================================================== --- trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/csv/AccountModel.java (rev 0) +++ trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/csv/AccountModel.java 2014-06-10 13:23:09 UTC (rev 3827) @@ -0,0 +1,34 @@ +package org.chorem.lima.business.ejb.csv; + +import org.chorem.lima.entity.Account; +import org.chorem.lima.entity.AccountImpl; +import org.nuiton.csv.ExportModel; +import org.nuiton.csv.ExportableColumn; +import org.nuiton.csv.ModelBuilder; + +/** + * Created by davidcosse on 03/06/14. + */ +public class AccountModel extends AbstractLimaModel<Account> implements ExportModel<Account> { + + public AccountModel() { + super(';'); + newMandatoryColumn("accountNumber", Account.PROPERTY_ACCOUNT_NUMBER); + newOptionalColumn("label", Account.PROPERTY_LABEL); + newOptionalColumn("thirdParty", Account.PROPERTY_THIRD_PARTY); + } + + @Override + public Iterable<ExportableColumn<Account, Object>> getColumnsForExport() { + ModelBuilder<Account> modelBuilder = new ModelBuilder<Account>(); + modelBuilder.newColumnForExport("accountNumber", Account.PROPERTY_ACCOUNT_NUMBER); + modelBuilder.newColumnForExport("label", Account.PROPERTY_LABEL); + modelBuilder.newColumnForExport("thirdParty", Account.PROPERTY_THIRD_PARTY); + return (Iterable) modelBuilder.getColumnsForExport(); + } + + @Override + public Account newEmptyInstance() { + return new AccountImpl(); + } +} Added: 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/csv/EntryBookModel.java (rev 0) +++ trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/csv/EntryBookModel.java 2014-06-10 13:23:09 UTC (rev 3827) @@ -0,0 +1,32 @@ +package org.chorem.lima.business.ejb.csv; + +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 EntryBookModel extends AbstractLimaModel<EntryBook> implements ExportModel<EntryBook> { + + public EntryBookModel() { + super(';'); + newMandatoryColumn("code", EntryBook.PROPERTY_CODE); + newOptionalColumn("label", EntryBook.PROPERTY_LABEL); + } + + @Override + public Iterable<ExportableColumn<EntryBook, Object>> getColumnsForExport() { + ModelBuilder<EntryBook> modelBuilder = new ModelBuilder<EntryBook>(); + modelBuilder.newColumnForExport("code", EntryBook.PROPERTY_CODE); + modelBuilder.newColumnForExport("label", EntryBook.PROPERTY_LABEL); + return (Iterable) modelBuilder.getColumnsForExport(); + } + + @Override + public EntryBook newEmptyInstance() { + return new EntryBookImpl(); + } +} Added: 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 (rev 0) +++ trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/csv/EntryModel.java 2014-06-10 13:23:09 UTC (rev 3827) @@ -0,0 +1,52 @@ +package org.chorem.lima.business.ejb.csv; + +import org.chorem.lima.business.api.AccountService; +import org.chorem.lima.business.api.FinancialTransactionService; +import org.chorem.lima.entity.Entry; +import org.chorem.lima.entity.EntryImpl; +import org.nuiton.csv.ExportModel; +import org.nuiton.csv.ExportableColumn; +import org.nuiton.csv.ModelBuilder; + +/** + * Created by davidcosse on 03/06/14. + */ +public class EntryModel extends AbstractLimaModel<Entry> implements ExportModel<Entry> { + + public EntryModel(AccountService accountService, FinancialTransactionService financialTransactionService) { + super(';'); + AbstractLimaModel.accountService = accountService; + AbstractLimaModel.financialTransactionService = financialTransactionService; + + newMandatoryColumn("account", Entry.PROPERTY_ACCOUNT, ACCOUNT_NUMBER_TO_ACCOUNT_TRANSACTION_PARSER); + newOptionalColumn("amount", Entry.PROPERTY_AMOUNT, BIG_DECIMAL_WITH_NULL_PARSER); + newOptionalColumn("debit", Entry.PROPERTY_DEBIT, O_N_PARSER); + newOptionalColumn("detail", Entry.PROPERTY_DETAIL); + newOptionalColumn("position", Entry.PROPERTY_POSITION); + 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); + } + + @Override + public Iterable<ExportableColumn<Entry, Object>> getColumnsForExport() { + ModelBuilder<Entry> modelBuilder = new ModelBuilder<Entry>(); + modelBuilder.newColumnForExport("account", Entry.PROPERTY_ACCOUNT, ACCOUNT_TO_ACCOUNT_NUMBER_FORMATTER); + modelBuilder.newColumnForExport("amount", Entry.PROPERTY_AMOUNT, BIG_DECIMAL_FORMATTER); + modelBuilder.newColumnForExport("debit", Entry.PROPERTY_DEBIT, O_N_FORMATTER); + modelBuilder.newColumnForExport("detail", Entry.PROPERTY_DETAIL); + modelBuilder.newColumnForExport("position", Entry.PROPERTY_POSITION); + modelBuilder.newColumnForExport("voucher", Entry.PROPERTY_VOUCHER); + modelBuilder.newColumnForExport("description", Entry.PROPERTY_DESCRIPTION); + modelBuilder.newColumnForExport("lettering", Entry.PROPERTY_LETTERING); + modelBuilder.newColumnForExport("financialTransaction", Entry.PROPERTY_FINANCIAL_TRANSACTION, FINANCIAL_TRANSACTION_TO_FINANCIAL_TRANSACTION_ID_FORMATTER); + + return (Iterable) modelBuilder.getColumnsForExport(); + } + + @Override + public Entry newEmptyInstance() { + return new EntryImpl(); + } +} Added: trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/csv/FinancialTransactionModel.java =================================================================== --- trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/csv/FinancialTransactionModel.java (rev 0) +++ trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/csv/FinancialTransactionModel.java 2014-06-10 13:23:09 UTC (rev 3827) @@ -0,0 +1,37 @@ +package org.chorem.lima.business.ejb.csv; + +import org.chorem.lima.business.api.EntryBookService; +import org.chorem.lima.entity.FinancialTransaction; +import org.chorem.lima.entity.FinancialTransactionImpl; +import org.nuiton.csv.ExportModel; +import org.nuiton.csv.ExportableColumn; +import org.nuiton.csv.ModelBuilder; + +/** + * Created by davidcosse on 03/06/14. + */ +public class FinancialTransactionModel extends AbstractLimaModel<FinancialTransaction> implements ExportModel<FinancialTransaction> { + + public FinancialTransactionModel(EntryBookService entryBookService) { + super(';'); + AbstractLimaModel.entryBookService = entryBookService; + newMandatoryColumn("id", FinancialTransaction.PROPERTY_TOPIA_ID); + newOptionalColumn("transactionDate", FinancialTransaction.PROPERTY_TRANSACTION_DATE, DATE_PARSER); + newOptionalColumn("entryBook", FinancialTransaction.PROPERTY_ENTRY_BOOK, ENTRY_BOOK_CODE_TO_ENTRY_BOOK_PARSER); + } + + @Override + public Iterable<ExportableColumn<FinancialTransaction, Object>> getColumnsForExport() { + ModelBuilder<FinancialTransaction> modelBuilder = new ModelBuilder<FinancialTransaction>(); + modelBuilder.newColumnForExport("id", FinancialTransaction.PROPERTY_TOPIA_ID); + modelBuilder.newColumnForExport("transactionDate", FinancialTransaction.PROPERTY_TRANSACTION_DATE, DATE_FORMATTER); + modelBuilder.newColumnForExport("entryBook", FinancialTransaction.PROPERTY_ENTRY_BOOK, ENTRY_BOOK_TO_ENTRY_BOOK_CODE_FORMATTER); + return (Iterable) modelBuilder.getColumnsForExport(); + } + + @Override + public FinancialTransactionImpl newEmptyInstance() { + return new FinancialTransactionImpl(); + } + +} Added: trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/csv/FiscalPeriodModel.java =================================================================== --- trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/csv/FiscalPeriodModel.java (rev 0) +++ trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/csv/FiscalPeriodModel.java 2014-06-10 13:23:09 UTC (rev 3827) @@ -0,0 +1,34 @@ +package org.chorem.lima.business.ejb.csv; + +import org.chorem.lima.entity.FiscalPeriod; +import org.chorem.lima.entity.FiscalPeriodImpl; +import org.nuiton.csv.ExportModel; +import org.nuiton.csv.ExportableColumn; +import org.nuiton.csv.ModelBuilder; + +/** + * Created by davidcosse on 03/06/14. + */ +public class FiscalPeriodModel extends AbstractLimaModel<FiscalPeriod> implements ExportModel<FiscalPeriod> { + + public FiscalPeriodModel() { + super(';'); + newMandatoryColumn("beginDate", FiscalPeriod.PROPERTY_BEGIN_DATE, DATE_PARSER); + newMandatoryColumn("endDate", FiscalPeriod.PROPERTY_END_DATE, DATE_PARSER); + newMandatoryColumn("locked", FiscalPeriod.PROPERTY_LOCKED, O_N_PARSER); + } + + @Override + public Iterable<ExportableColumn<FiscalPeriod, Object>> getColumnsForExport() { + ModelBuilder<FiscalPeriod> modelBuilder = new ModelBuilder<FiscalPeriod>(); + modelBuilder.newColumnForExport("beginDate", FiscalPeriod.PROPERTY_BEGIN_DATE, DATE_FORMATTER); + modelBuilder.newColumnForExport("endDate", FiscalPeriod.PROPERTY_END_DATE, DATE_FORMATTER); + modelBuilder.newColumnForExport("locked", FiscalPeriod.PROPERTY_LOCKED, O_N_FORMATTER); + return (Iterable) modelBuilder.getColumnsForExport(); + } + + @Override + public FiscalPeriod newEmptyInstance() { + return new FiscalPeriodImpl(); + } +} Modified: trunk/lima-business/src/main/java/org/chorem/lima/business/migration/DatabaseMigrationClass.java =================================================================== --- trunk/lima-business/src/main/java/org/chorem/lima/business/migration/DatabaseMigrationClass.java 2014-06-03 09:36:17 UTC (rev 3826) +++ trunk/lima-business/src/main/java/org/chorem/lima/business/migration/DatabaseMigrationClass.java 2014-06-10 13:23:09 UTC (rev 3827) @@ -26,10 +26,15 @@ import org.chorem.lima.entity.LimaCallaoTopiaPersistenceContext; import org.nuiton.topia.migration.TopiaMigrationCallbackByClassNG; +import org.nuiton.topia.persistence.TopiaApplicationContext; +import org.nuiton.topia.persistence.TopiaMigrationService; +import org.nuiton.topia.persistence.TopiaMigrationServiceException; +import org.nuiton.topia.persistence.TopiaService; import org.nuiton.topia.persistence.support.TopiaSqlSupport; import org.nuiton.util.Version; import java.util.List; +import java.util.Map; import java.util.Set; public class DatabaseMigrationClass extends TopiaMigrationCallbackByClassNG<LimaCallaoTopiaPersistenceContext> { Modified: trunk/lima-business/src/main/java/org/chorem/lima/business/migration/MigrationV0V0_5.java =================================================================== --- trunk/lima-business/src/main/java/org/chorem/lima/business/migration/MigrationV0V0_5.java 2014-06-03 09:36:17 UTC (rev 3826) +++ trunk/lima-business/src/main/java/org/chorem/lima/business/migration/MigrationV0V0_5.java 2014-06-10 13:23:09 UTC (rev 3827) @@ -32,6 +32,10 @@ public class MigrationV0V0_5 extends MigrationCallBackForVersion { + public MigrationV0V0_5() { + this.callBack = new DatabaseMigrationClass(); + } + @Override protected void prepareMigrationScript(TopiaPersistenceContext tx, List queries, Modified: trunk/lima-business/src/main/java/org/chorem/lima/business/migration/MigrationV0_6.java =================================================================== --- trunk/lima-business/src/main/java/org/chorem/lima/business/migration/MigrationV0_6.java 2014-06-03 09:36:17 UTC (rev 3826) +++ trunk/lima-business/src/main/java/org/chorem/lima/business/migration/MigrationV0_6.java 2014-06-10 13:23:09 UTC (rev 3827) @@ -47,6 +47,10 @@ /** Logger. */ private static final Log log = LogFactory.getLog(MigrationV0_6.class); + public MigrationV0_6() { + this.callBack = new DatabaseMigrationClass(); + } + @Override protected void prepareMigrationScript(TopiaPersistenceContext tx, List queries, Modified: trunk/lima-business/src/main/java/org/chorem/lima/business/migration/MigrationV0_8.java =================================================================== --- trunk/lima-business/src/main/java/org/chorem/lima/business/migration/MigrationV0_8.java 2014-06-03 09:36:17 UTC (rev 3826) +++ trunk/lima-business/src/main/java/org/chorem/lima/business/migration/MigrationV0_8.java 2014-06-10 13:23:09 UTC (rev 3827) @@ -22,6 +22,10 @@ /** Logger. */ private static final Log log = LogFactory.getLog(MigrationV0_8.class); + public MigrationV0_8() { + this.callBack = new DatabaseMigrationClass(); + } + @Override protected void prepareMigrationScript(TopiaPersistenceContext tx, List queries, Modified: trunk/lima-business/src/main/resources/i18n/lima-business_en_GB.properties =================================================================== --- trunk/lima-business/src/main/resources/i18n/lima-business_en_GB.properties 2014-06-03 09:36:17 UTC (rev 3826) +++ trunk/lima-business/src/main/resources/i18n/lima-business_en_GB.properties 2014-06-10 13:23:09 UTC (rev 3827) @@ -19,6 +19,7 @@ lima-business.defaultaccountingrules.missingentrybook=Can't block financialperiod / missing EntryBook in transactions lima-business.document.account= lima-business.document.accountnumber=Account N° +lima-business.document.accounts=Accounts lima-business.document.adress= lima-business.document.adresssuite= lima-business.document.amount= @@ -38,10 +39,14 @@ lima-business.document.dateformat=%1$tm/%1$te/%1$tY lima-business.document.debit=Debit lima-business.document.description=Description +lima-business.document.entries=Entries +lima-business.document.entryBooks=Entry books lima-business.document.entrybook=EntryBook lima-business.document.entrybooks= lima-business.document.entrybooktitle=%s\n%2$tB %2$tY +lima-business.document.financialTransactions=Financial transactions lima-business.document.financialstatement=FinancialStatement +lima-business.document.fiscalPeriods=Fiscal periods lima-business.document.generalentrybook=General EntryBook lima-business.document.grossamount=Gross Amount lima-business.document.label=Label Modified: trunk/lima-business/src/main/resources/i18n/lima-business_fr_FR.properties =================================================================== --- trunk/lima-business/src/main/resources/i18n/lima-business_fr_FR.properties 2014-06-03 09:36:17 UTC (rev 3826) +++ trunk/lima-business/src/main/resources/i18n/lima-business_fr_FR.properties 2014-06-10 13:23:09 UTC (rev 3827) @@ -19,6 +19,7 @@ lima-business.defaultaccountingrules.missingentrybook=Impossible de bloquer la période financière / il manque un journal dans une transaction (%1$te/%1$tm/%1$tY) lima-business.document.account=Compte lima-business.document.accountnumber=N° Compte +lima-business.document.accounts=Comptes lima-business.document.adress=Adresse lima-business.document.adresssuite=Adresse - suite lima-business.document.amount=Total @@ -39,10 +40,14 @@ lima-business.document.dateformat=%1$te/%1$tm/%1$tY lima-business.document.debit=Débit lima-business.document.description=Description +lima-business.document.entries=entrées +lima-business.document.entryBooks= lima-business.document.entrybook=Journal lima-business.document.entrybooks=Journaux lima-business.document.entrybooktitle=%s\n%2$tB %2$tY +lima-business.document.financialTransactions=transactions financières lima-business.document.financialstatement=Bilan et compte de résultat +lima-business.document.fiscalPeriods=Périodes ficales lima-business.document.generalentrybook=Journal Général lima-business.document.grossamount=Brut lima-business.document.label=Libellé 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-06-03 09:36:17 UTC (rev 3826) +++ trunk/lima-business/src/test/java/org/chorem/lima/business/AbstractLimaTest.java 2014-06-10 13:23:09 UTC (rev 3827) @@ -33,6 +33,7 @@ import java.util.Properties; import java.util.UUID; +import com.google.common.collect.Lists; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import org.apache.commons.logging.Log; @@ -41,10 +42,13 @@ import org.chorem.lima.business.accountingrules.TestAccountingRules; import org.chorem.lima.business.api.AccountService; import org.chorem.lima.business.api.EntryBookService; +import org.chorem.lima.business.api.EntryService; import org.chorem.lima.business.api.FinancialPeriodService; import org.chorem.lima.business.api.FinancialTransactionService; import org.chorem.lima.business.api.FiscalPeriodService; import org.chorem.lima.business.api.ImportService; +import org.chorem.lima.business.api.NewExportService; +import org.chorem.lima.business.api.NewImportService; import org.chorem.lima.business.api.ReportService; import org.chorem.lima.entity.Account; import org.chorem.lima.entity.AccountImpl; @@ -78,7 +82,7 @@ */ public abstract class AbstractLimaTest { - private static final Log log = LogFactory.getLog(AbstractLimaTest.class); + protected static final Log log = LogFactory.getLog(AbstractLimaTest.class); protected static final Function<Properties, LimaCallaoTopiaApplicationContext> CREATE_CONTEXT_FUNCTION = new Function<Properties, LimaCallaoTopiaApplicationContext>() { @Override public LimaCallaoTopiaApplicationContext apply(Properties input) { @@ -95,6 +99,9 @@ protected FiscalPeriodService fiscalPeriodService; protected ReportService reportService; protected ImportService importService; + protected EntryService entryService; + protected NewImportService newImportService; + protected NewExportService newExportService; /** * This is a before class method, but junit will fail to run inherited @@ -119,13 +126,19 @@ * @throws IOException */ protected void initServices() throws IOException { - accountService = LimaServiceFactory.getService(AccountService.class); - entryBookService = LimaServiceFactory.getService(EntryBookService.class); - financialPeriodService = LimaServiceFactory.getService(FinancialPeriodService.class); - financialTransactionService = LimaServiceFactory.getService(FinancialTransactionService.class); - fiscalPeriodService = LimaServiceFactory.getService(FiscalPeriodService.class); - reportService = LimaServiceFactory.getService(ReportService.class); - importService = LimaServiceFactory.getService(ImportService.class); + if(accountService == null) { + accountService = LimaServiceFactory.getService(AccountService.class); + entryBookService = LimaServiceFactory.getService(EntryBookService.class); + financialPeriodService = LimaServiceFactory.getService(FinancialPeriodService.class); + financialTransactionService = LimaServiceFactory.getService(FinancialTransactionService.class); + fiscalPeriodService = LimaServiceFactory.getService(FiscalPeriodService.class); + reportService = LimaServiceFactory.getService(ReportService.class); + importService = LimaServiceFactory.getService(ImportService.class); + entryService = LimaServiceFactory.getService(EntryService.class); + + newImportService = LimaServiceFactory.getService(NewImportService.class); + newExportService = LimaServiceFactory.getService(NewExportService.class); + } } /** @@ -162,7 +175,7 @@ String testDir = System.getProperty("java.io.tmpdir") + File.separator + "lima-business-" + UUID.randomUUID().toString(); testProperties.setProperty(Option.DATA_DIR.getKey(), testDir); testProperties.setProperty("hibernate.connection.url", "jdbc:h2:file:" + testDir + File.separator + "data"); - //testProperties.setProperty("hibernate.hbm2ddl.auto", "update"); + testProperties.setProperty("hibernate.hbm2ddl.auto", "update"); instance.setOptions(testProperties); instance.setOption("topia.persistence.classes", LimaCallaoEntityEnum.getImplementationClassesAsString()); @@ -179,7 +192,7 @@ } @After - public void cleanDatabase() throws IOException { + public void cleanDatabase() throws Exception { LimaConfig config = LimaConfig.getInstance(); FileUtils.deleteDirectory(config.getDataDir()); } @@ -197,7 +210,24 @@ } /** - * Create a basic database with some objects to reuse then in subtests. + * Method to use only for class that need a context to be tester. + * Only for DOA for now. + * + * @return a topia context + */ + protected LimaCallaoTopiaApplicationContext getTestContext() { + LimaConfig config = LimaConfig.getInstance(); + Properties options = config.getFlatOptions(); + + if (log.isDebugEnabled()) { + log.debug("Opening context to database : " + options.getProperty("hibernate.connection.url")); + } + LimaCallaoTopiaApplicationContext result = TopiaApplicationContextCache.getContext(options, CREATE_CONTEXT_FUNCTION); + return result; + } + + /** + * Create a basic database. * * Not called by default (<code>@Before</code>) to allow init test with * CSV or EBP import. @@ -205,23 +235,19 @@ * @throws LimaException * @throws ParseException */ - protected void initTestDatabase() throws LimaException, ParseException { + protected void initTestDatabase() throws Exception { - // clear database -// clearService.clearDatabase(); getTestContext().createSchema(); - initTestAccounts(); - initTestTransactions(); } /** * Create a basic account plan. - * - * @throws LimaException - * @throws ParseException + * + * @throws org.chorem.lima.business.LimaException + * @throws java.text.ParseException */ - protected void initTestAccounts() throws LimaException, ParseException { + protected void initTestWithAccounts() throws Exception { // creation d'un plan compatble de test Account classFinancier = new AccountImpl(); @@ -233,12 +259,12 @@ accountVmp.setAccountNumber("50"); accountVmp.setLabel("Valeurs mobilières de placement"); accountService.createAccount(accountVmp); - + Account accountPel = new AccountImpl(); accountPel.setAccountNumber("501"); accountPel.setLabel("Parts dans des entreprises liées"); accountService.createAccount(accountPel); - + Account accountAP = new AccountImpl(); accountAP.setAccountNumber("502"); accountAP.setLabel("Actions propres"); @@ -258,13 +284,48 @@ accountBanques.setAccountNumber("512"); accountBanques.setLabel("Banques"); accountService.createAccount(accountBanques); - + } + + /** + * Create some EntryBooks. + * + * @throws LimaException + * @throws ParseException + */ + protected void initTestWithEntryBooks() throws Exception { // creation d'un journal EntryBook journalDesVentes = new EntryBookImpl(); journalDesVentes.setLabel("Journal des ventes"); journalDesVentes.setCode("jdv"); entryBookService.createEntryBook(journalDesVentes); - + + EntryBook journalDesAchats = new EntryBookImpl(); + journalDesAchats.setLabel("Journal des achats"); + journalDesAchats.setCode("jda"); + entryBookService.createEntryBook(journalDesAchats); + + EntryBook myEntryBook = new EntryBookImpl(); + myEntryBook.setCode("JRN"); + myEntryBook.setLabel("MyJournal"); + entryBookService.createEntryBook(myEntryBook); + + } + + /** + * Create a FiscalPeriod with an EntryBook. + * + * @throws LimaException + * @throws ParseException + */ + protected void initTestWithFiscalPeriod() throws Exception { + initTestWithAccounts(); + + // creation d'un journal + EntryBook journalDesVentes = new EntryBookImpl(); + journalDesVentes.setLabel("Journal des ventes"); + journalDesVentes.setCode("jdv"); + entryBookService.createEntryBook(journalDesVentes); + // creation d'un exercice fiscal FiscalPeriod fiscalPeriod = new FiscalPeriodImpl(); fiscalPeriod.setBeginDate(df.parse("January 1, 2012")); @@ -273,16 +334,14 @@ } /** - * Create some transaction in previous accounts. - * - * @throws LimaException - * @throws ParseException + * Create FinancialTransaction with 2 entries + * @throws Exception */ - protected void initTestTransactions() throws LimaException, ParseException { - // add some transactions - // FIXME echatellier 20120221 fix test, following instance already in current method + protected void initTestWithFinancialTransaction() throws Exception { + + initTestWithFiscalPeriod(); + Account accountVmpVae = accountService.getAccountByNumber("511"); EntryBook journalDesVentes = entryBookService.getEntryBookByCode("jdv"); - Account accountVmpVae = accountService.getAccountByNumber("511"); FinancialTransaction transaction1 = new FinancialTransactionImpl(); transaction1.setTransactionDate(df.parse("April 4, 2012")); @@ -295,8 +354,8 @@ tr1Entry1.setFinancialTransaction(transaction1); tr1Entry1.setDescription("test desc"); tr1Entry1.setVoucher("voucher"); - financialTransactionService.createEntry(tr1Entry1); - + tr1Entry1 = financialTransactionService.createEntry(tr1Entry1); + Entry tr1Entry2 = new EntryImpl(); tr1Entry2.setAmount(BigDecimal.valueOf(42.0)); tr1Entry2.setDebit(true); @@ -304,23 +363,9 @@ tr1Entry2.setFinancialTransaction(transaction1); tr1Entry2.setDescription("test desc"); tr1Entry2.setVoucher("voucher"); - financialTransactionService.createEntry(tr1Entry2); - } + tr1Entry2 = financialTransactionService.createEntry(tr1Entry2); - /** - * Method to use only for class that need a context to be tester. - * Only for DOA for now. - * - * @return a topia context - */ - protected LimaCallaoTopiaApplicationContext getTestContext() { - LimaConfig config = LimaConfig.getInstance(); - Properties options = config.getFlatOptions(); - - if (log.isDebugEnabled()) { - log.debug("Opening context to database : " + options.getProperty("hibernate.connection.url")); - } - LimaCallaoTopiaApplicationContext result = TopiaApplicationContextCache.getContext(options, CREATE_CONTEXT_FUNCTION); - return result; + transaction1.setEntry(Lists.newArrayList(tr1Entry1, tr1Entry2)); + financialTransactionService.updateFinancialTransaction(transaction1); } } Modified: trunk/lima-business/src/test/java/org/chorem/lima/business/AccountServiceImplTest.java =================================================================== --- trunk/lima-business/src/test/java/org/chorem/lima/business/AccountServiceImplTest.java 2014-06-03 09:36:17 UTC (rev 3826) +++ trunk/lima-business/src/test/java/org/chorem/lima/business/AccountServiceImplTest.java 2014-06-10 13:23:09 UTC (rev 3827) @@ -47,7 +47,7 @@ @Before public void initTest() throws Exception { - initTestDatabase(); + initTestWithAccounts(); } /** Modified: trunk/lima-business/src/test/java/org/chorem/lima/business/EntryBookServiceImplTest.java =================================================================== --- trunk/lima-business/src/test/java/org/chorem/lima/business/EntryBookServiceImplTest.java 2014-06-03 09:36:17 UTC (rev 3826) +++ trunk/lima-business/src/test/java/org/chorem/lima/business/EntryBookServiceImplTest.java 2014-06-10 13:23:09 UTC (rev 3827) @@ -27,10 +27,16 @@ import org.chorem.lima.entity.EntryBook; import org.chorem.lima.entity.EntryBookImpl; +import org.chorem.lima.entity.FinancialTransaction; +import org.chorem.lima.entity.FinancialTransactionImpl; +import org.chorem.lima.entity.FiscalPeriod; +import org.chorem.lima.entity.FiscalPeriodImpl; import org.junit.Assert; import org.junit.Before; import org.junit.Test; +import java.text.ParseException; + /** * Tests pour la gestion des journaux. * <p/> @@ -45,7 +51,7 @@ @Before public void initTest() throws Exception { - initTestDatabase(); + initTestWithEntryBooks(); } /** @@ -74,9 +80,22 @@ * @throws LimaException */ @Test(expected=LimaBusinessException.class) - public void deleteUsedEntryBook() throws LimaException { + public void deleteUsedEntryBook() throws LimaException, ParseException { EntryBook entryBook = entryBookService.getAllEntryBooks().get(0); // VTE Assert.assertNotNull(entryBook); + + // make transaction used + // creation d'un exercice fiscal + FiscalPeriod fiscalPeriod = new FiscalPeriodImpl(); + fiscalPeriod.setBeginDate(df.parse("January 1, 2012")); + fiscalPeriod.setEndDate(df.parse("December 31, 2012")); + fiscalPeriodService.createFiscalPeriod(fiscalPeriod); + + FinancialTransaction financialTransaction = new FinancialTransactionImpl(); + financialTransaction.setTransactionDate(df.parse("January 1, 2012")); + financialTransaction.setEntryBook(entryBook); + financialTransactionService.createFinancialTransaction(financialTransaction); + entryBookService.removeEntryBook(entryBook); } @@ -88,13 +107,15 @@ */ @Test public void deleteNonUsedEntryBookTest() throws LimaException { + EntryBook myEntryBook = new EntryBookImpl(); myEntryBook.setCode("JRN"); myEntryBook.setLabel("MyJournal"); myEntryBook = entryBookService.createEntryBook(myEntryBook); + int nbEntryBooks = entryBookService.getAllEntryBooks().size(); entryBookService.removeEntryBook(myEntryBook); - Assert.assertEquals(1, entryBookService.getAllEntryBooks().size()); + Assert.assertEquals(nbEntryBooks -1, entryBookService.getAllEntryBooks().size()); } } \ No newline at end of file Modified: trunk/lima-business/src/test/java/org/chorem/lima/business/FinancialPeriodServiceImplTest.java =================================================================== --- trunk/lima-business/src/test/java/org/chorem/lima/business/FinancialPeriodServiceImplTest.java 2014-06-03 09:36:17 UTC (rev 3826) +++ trunk/lima-business/src/test/java/org/chorem/lima/business/FinancialPeriodServiceImplTest.java 2014-06-10 13:23:09 UTC (rev 3827) @@ -59,21 +59,23 @@ @Test public void blockClosedPeriodicEntryBookTest() throws Exception { + initTestWithFinancialTransaction(); + // find one closed to close LimaCallaoTopiaPersistenceContext context = getTestContext().newPersistenceContext(); ClosedPeriodicEntryBookTopiaDao dao = context.getClosedPeriodicEntryBookDao(); ClosedPeriodicEntryBook closedPeriodic = dao.findAll().get(0); - context.closeContext(); + context.close(); // block it Assert.assertFalse(closedPeriodic.isLocked()); - closedPeriodic = financialPeriodService.blockClosedPeriodicEntryBook(closedPeriodic); + financialPeriodService.blockClosedPeriodicEntryBook(closedPeriodic); // check it's blocked context = getTestContext().newPersistenceContext(); dao = context.getClosedPeriodicEntryBookDao(); closedPeriodic = dao.findAll().get(0); - context.closeContext(); + context.close(); Assert.assertFalse(closedPeriodic.isLocked()); } 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-06-03 09:36:17 UTC (rev 3826) +++ trunk/lima-business/src/test/java/org/chorem/lima/business/FinancialTransactionServiceImplTest.java 2014-06-10 13:23:09 UTC (rev 3827) @@ -53,8 +53,8 @@ public class FinancialTransactionServiceImplTest extends AbstractLimaTest { @Before - public void initTest() throws LimaException, ParseException { - initTestDatabase(); + public void initTest() throws Exception { + initTestWithFinancialTransaction(); } /** @@ -122,7 +122,7 @@ * @throws LimaException */ @Test - public void testGetUnbalancedTransactionNotAllGood() throws ParseException, LimaException { + public void testGetUnbalancedTransactionNotAllGood() throws Exception { EntryBook journalDesVentes = entryBookService.getEntryBookByCode("jdv"); Account accountVmpVae = accountService.getAccountByNumber("511"); Modified: trunk/lima-business/src/test/java/org/chorem/lima/business/FiscalPeriodServiceImplTest.java =================================================================== --- trunk/lima-business/src/test/java/org/chorem/lima/business/FiscalPeriodServiceImplTest.java 2014-06-03 09:36:17 UTC (rev 3826) +++ trunk/lima-business/src/test/java/org/chorem/lima/business/FiscalPeriodServiceImplTest.java 2014-06-10 13:23:09 UTC (rev 3827) @@ -50,7 +50,6 @@ @Before public void initTest() throws Exception { - initTestDatabase(); } /** 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-06-03 09:36:17 UTC (rev 3826) +++ trunk/lima-business/src/test/java/org/chorem/lima/business/ImportServiceImplTest.java 2014-06-10 13:23:09 UTC (rev 3827) @@ -55,8 +55,6 @@ */ protected void importEBPData() throws IOException, LimaException, ParseException { - getTestContext().createSchema(); - // create fiscal period (mandatory for import) FiscalPeriod fiscalPeriod = new FiscalPeriodImpl(); fiscalPeriod.setBeginDate(df.parse("January 1, 2012")); Added: trunk/lima-business/src/test/java/org/chorem/lima/business/NewExportServiceTest.java =================================================================== --- trunk/lima-business/src/test/java/org/chorem/lima/business/NewExportServiceTest.java (rev 0) +++ trunk/lima-business/src/test/java/org/chorem/lima/business/NewExportServiceTest.java 2014-06-10 13:23:09 UTC (rev 3827) @@ -0,0 +1,216 @@ +package org.chorem.lima.business; + +import com.google.common.collect.Lists; +import org.apache.commons.io.FileUtils; +import org.apache.commons.io.IOUtils; +import org.chorem.lima.entity.Account; +import org.chorem.lima.entity.Entry; +import org.chorem.lima.entity.EntryBook; +import org.chorem.lima.entity.FinancialTransaction; +import org.chorem.lima.entity.FiscalPeriod; +import org.junit.Assert; +import org.junit.Test; + +import java.io.File; +import java.io.FileInputStream; +import java.nio.charset.Charset; +import java.util.Collection; +import java.util.List; + +/** + * Created by davidcosse on 03/06/14. + */ +public class NewExportServiceTest extends AbstractLimaTest { + + @Test + public void testExportImportAccounts() throws Exception { + initTestWithFiscalPeriod(); + // make sure they are some accounts. + List<Account> accounts = accountService.getAllAccounts(); + Assert.assertTrue(accountService.getAllAccounts().size() > 0); + + + // export accounts + File file = newExportService.exportAccountsAsCSV("export-accounts.csv", Charset.defaultCharset().name()); + log.info("Account File path:" + file.getAbsolutePath()); + + // remove accounts + int nbEntities = accounts.size(); + for (Account account : accounts) { + accountService.removeAccount(account); + } + Assert.assertEquals(0, accountService.getAllAccounts().size()); + + // import accounts + FileInputStream contentStream = null; + try { + contentStream = new FileInputStream(file.getPath()); + String stream = IOUtils.toString(contentStream); + newImportService.importAccountAsCSV(stream); + + // make sure all account have been created + Assert.assertEquals(nbEntities, accountService.getAllAccounts().size()); + } finally { + IOUtils.closeQuietly(contentStream); + FileUtils.forceDelete(file); + } + + + } + + @Test + public void testExportImportEntryBooks() throws Exception { + initTestWithEntryBooks(); + File file = newExportService.exportEntryBooksAsCSV("export-entry-books.csv", Charset.defaultCharset().name()); + Assert.assertNotNull(file); + + List<EntryBook> entryBooks = entryBookService.getAllEntryBooks(); + int nbEntities = entryBooks.size(); + Assert.assertEquals(3, nbEntities); + + for (EntryBook entryBook : entryBooks) { + entryBookService.removeEntryBook(entryBook); + } + + Assert.assertEquals(0, entryBookService.getAllEntryBooks().size()); + + FileInputStream contentStream = null; + try { + contentStream = new FileInputStream(file.getPath()); + String stream = IOUtils.toString(contentStream); + newImportService.importEntryBooksAsCSV(stream); + } finally { + IOUtils.closeQuietly(contentStream); + } + + Assert.assertEquals(nbEntities, entryBookService.getAllEntryBooks().size()); + FileUtils.forceDelete(file); + } + + @Test + public void testExportImportFinancialTransactions() throws Exception { + initTestWithFinancialTransaction(); + + File file = newExportService.exportFinancialTransactionsAsCSV("export-financial-transactions.csv", Charset.defaultCharset().name()); + Assert.assertNotNull(file); + + 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(file.getPath()); + String stream = IOUtils.toString(contentStream); + newImportService.importFinancialTransactionsAsCSV(stream); + } finally { + IOUtils.closeQuietly(contentStream); + } + + Assert.assertEquals(nbEntities, financialTransactionService.getAllFinancialTransactions(df.parse("January 1, 2012"),df.parse("December 31, 2012")).size()); + FileUtils.forceDelete(file); + } + + @Test + public void testExportImportEntries() throws Exception { + initTestWithFinancialTransaction(); + + List<FinancialTransaction> financialTransactions = financialTransactionService.getAllFinancialTransactions(df.parse("April 4, 2012"),df.parse("December 31, 2012")); + List<Entry> entries = Lists.newArrayList(); + Assert.assertFalse(financialTransactions.isEmpty()); + for (FinancialTransaction financialTransaction : financialTransactions) { + entries.addAll(financialTransaction.getEntry()); + } + int nbEntities = entries.size(); + Assert.assertEquals(2,nbEntities); + + //test export + File file = newExportService.exportEntriesAsCSV("export-entries.csv", Charset.defaultCharset().name()); + Assert.assertNotNull(file); + + + for (Entry entry : entries) { + FinancialTransaction financialTransaction = entry.getFinancialTransaction(); + financialTransactionService.removeEntry(entry); + financialTransactionService.updateFinancialTransaction(financialTransaction); + } + + // check all entries have been cleared + entries.clear();// + + financialTransactions = financialTransactionService.getAllFinancialTransactions(df.parse("April 4, 2012"),df.parse("December 31, 2012")); + for (FinancialTransaction financialTransaction : financialTransactions) { + entries.addAll(financialTransaction.getEntry()); + } + Assert.assertTrue(entries.isEmpty()); + + // test import + FileInputStream contentStream = null; + try { + contentStream = new FileInputStream(file.getPath()); + String stream = IOUtils.toString(contentStream); + newImportService.importEntriesAsCSV(stream); + } finally { + IOUtils.closeQuietly(contentStream); + } + + // valid import + financialTransactions = financialTransactionService.getAllFinancialTransactions(df.parse("April 4, 2012"),df.parse("December 31, 2012")); + Assert.assertEquals(1, financialTransactions.size()); + for (FinancialTransaction financialTransaction : financialTransactions) { + entries.addAll(financialTransaction.getEntry()); + } + + Assert.assertEquals(nbEntities, entries.size()); + FileUtils.forceDelete(file); + } + + @Test + public void testExportImportFiscalPeriodsAsCSV() throws Exception { + initTestWithFiscalPeriod(); + File file = newExportService.exportFiscalPeriodAsCSV("export-fiscal-periods.csv", Charset.defaultCharset().name()); + Assert.assertNotNull(file); + + + List<FiscalPeriod> fiscalPeriods = fiscalPeriodService.getAllFiscalPeriods(); + int nbEntities = fiscalPeriods.size(); + Assert.assertEquals(1, nbEntities); + + initAbstractTest(); + + Assert.assertEquals(0, fiscalPeriodService.getAllFiscalPeriods().size()); + + FileInputStream contentStream = null; + try { + contentStream = new FileInputStream(file.getPath()); + String stream = IOUtils.toString(contentStream); + newImportService.importFiscalPeriodsAsCSV(stream); + } finally { + IOUtils.closeQuietly(contentStream); + } + + Assert.assertEquals(nbEntities, fiscalPeriodService.getAllFiscalPeriods().size()); + FileUtils.forceDelete(file); + } + + @Test + public void exportAllAsCSVTest() throws Exception { + initTestWithFinancialTransaction(); + String tmpDir = System.getProperty("java.io.tmpdir")+"/"; + String path = newExportService.exportAllAsCSV(tmpDir, "UTF-8"); + + initAbstractTest(); + + Collection<FinancialTransaction> financialTransactions = financialTransactionService.getAllFinancialTransactions(df.parse("January 1, 2012"),df.parse("December 31, 2012")); + Assert.assertTrue(financialTransactions.isEmpty()); + + newImportService.importAllAsCSV(path); + FileUtils.deleteDirectory(new File(tmpDir)); + } +} 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-06-03 09:36:17 UTC (rev 3826) +++ trunk/lima-business/src/test/java/org/chorem/lima/business/ReportServiceImplTest.java 2014-06-10 13:23:09 UTC (rev 3827) @@ -51,7 +51,7 @@ @Before public void initTest() throws Exception { - initTestDatabase(); + initTestWithFinancialTransaction(); } /** Modified: trunk/lima-business/src/test/java/org/chorem/lima/business/accountingrules/AccountServiceRuleFrTest.java =================================================================== --- trunk/lima-business/src/test/java/org/chorem/lima/business/accountingrules/AccountServiceRuleFrTest.java 2014-06-03 09:36:17 UTC (rev 3826) +++ trunk/lima-business/src/test/java/org/chorem/lima/business/accountingrules/AccountServiceRuleFrTest.java 2014-06-10 13:23:09 UTC (rev 3827) @@ -63,18 +63,4 @@ Assert.assertTrue(LimaConfig.getInstance().getAccountingRules() instanceof FranceAccountingRules); } - /** - * Création d'un compte dans le plan comptable. - * - * @throws LimaException - */ - @Ignore - @Test(expected=LimaException.class) - public void testCreateAccountWrongStart() throws LimaException { - // TODO DCossé 17/02/14 this test is no more valid as count with account number with letters are now accepted - // there is just a warning about it - Account myAccount = new AccountImpl(); - myAccount.setAccountNumber("10TEST"); - accountService.createAccount(myAccount); - } } Modified: trunk/lima-business/src/test/java/org/chorem/lima/business/accountingrules/TestAccountingRules.java =================================================================== --- trunk/lima-business/src/test/java/org/chorem/lima/business/accountingrules/TestAccountingRules.java 2014-06-03 09:36:17 UTC (rev 3826) +++ trunk/lima-business/src/test/java/org/chorem/lima/business/accountingrules/TestAccountingRules.java 2014-06-10 13:23:09 UTC (rev 3827) @@ -76,8 +76,7 @@ //check the new fiscal period adjoining the last Date dateLastFiscalPeriod = lastFiscalPeriod.getEndDate(); - dateLastFiscalPeriod = DateUtils. - addDays(dateLastFiscalPeriod, 1); + dateLastFiscalPeriod = DateUtils.addDays(dateLastFiscalPeriod, 1); dateLastFiscalPeriod = DateUtils.truncate(dateLastFiscalPeriod, Calendar.DATE); Date dateFiscalPeriod = fiscalPeriod.getBeginDate(); if (dateLastFiscalPeriod.compareTo(dateFiscalPeriod) != 0) { @@ -86,8 +85,7 @@ //We can create a new fiscal period meantime the last fiscal period was not locked //But not the ante periodfiscal - int unblockedFiscalPeriod = - fiscalPeriodDAO.findAllByLocked(false).size(); + int unblockedFiscalPeriod = fiscalPeriodDAO.forLockedEquals(false).findAll().size(); if (unblockedFiscalPeriod > 1) { throw new LimaBusinessException(t("lima-business.franceaccountingrules.antefiscalperiodnotblocked")); } Modified: trunk/lima-business/src/test/java/org/chorem/lima/entity/AccountDAOTest.java =================================================================== --- trunk/lima-business/src/test/java/org/chorem/lima/entity/AccountDAOTest.java 2014-06-03 09:36:17 UTC (rev 3826) +++ trunk/lima-business/src/test/java/org/chorem/lima/entity/AccountDAOTest.java 2014-06-10 13:23:09 UTC (rev 3827) @@ -52,9 +52,11 @@ * @throws TopiaException */ @Test - public void testStringToListAccounts() throws TopiaException { + public void testStringToListAccounts() throws Exception { LimaCallaoTopiaPersistenceContext context = getTestContext().newPersistenceContext(); + initTestWithAccounts(); + AccountTopiaDao accountDAO = context.getAccountDao(); List<Account> accounts = accountDAO.stringToListAccounts("50..511", false); @@ -63,6 +65,6 @@ accounts = accountDAO.stringToListAccounts("60..99", false); Assert.assertEquals(0, accounts.size()); - context.closeContext(); + context.close(); } } Modified: trunk/lima-business/src/test/java/org/chorem/lima/entity/FinancialTransactionDAOTest.java =================================================================== --- trunk/lima-business/src/test/java/org/chorem/lima/entity/FinancialTransactionDAOTest.java 2014-06-03 09:36:17 UTC (rev 3826) +++ trunk/lima-business/src/test/java/org/chorem/lima/entity/FinancialTransactionDAOTest.java 2014-06-10 13:23:09 UTC (rev 3827) @@ -53,8 +53,10 @@ * @throws LimaException */ @Test - public void testFindAllUnbalancedTransactions() throws TopiaException, LimaException { + public void testFindAllUnbalancedTransactions() throws Exception { + initTestWithFinancialTransaction(); + FinancialPeriod financialPeriod = financialPeriodService.getAllFinancialPeriods().get(0); EntryBook journalDesVentes = entryBookService.getEntryBookByCode("jdv"); Modified: trunk/lima-business-api/src/main/java/org/chorem/lima/business/api/FinancialTransactionService.java =================================================================== --- trunk/lima-business-api/src/main/java/org/chorem/lima/business/api/FinancialTransactionService.java 2014-06-03 09:36:17 UTC (rev 3826) +++ trunk/lima-business-api/src/main/java/org/chorem/lima/business/api/FinancialTransactionService.java 2014-06-10 13:23:09 UTC (rev 3827) @@ -56,6 +56,8 @@ void removeFinancialTransaction(FinancialTransaction financialtransaction) throws LimaException; + FinancialTransaction getFinancialTransactionWithId(String id) throws LimaException; + List<FinancialTransaction> getAllFinancialTransactions(FinancialPeriod period) throws LimaException; List<FinancialTransaction> getAllFinancialTransactions(Date beginDate, Date endDate) throws LimaException; Copied: trunk/lima-business-api/src/main/java/org/chorem/lima/business/api/NewExportService.java (from rev 3825, trunk/lima-business-api/src/main/java/org/chorem/lima/business/api/ExportService.java) =================================================================== --- trunk/lima-business-api/src/main/java/org/chorem/lima/business/api/NewExportService.java (rev 0) +++ trunk/lima-business-api/src/main/java/org/chorem/lima/business/api/NewExportService.java 2014-06-10 13:23:09 UTC (rev 3827) @@ -0,0 +1,56 @@ +/* + * #%L + * Lima business + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2008 - 2010 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% + */ + +package org.chorem.lima.business.api; + +import org.chorem.lima.business.LimaException; + +import java.io.File; + +/** + * Import export service. + * <p/> + * Currently import and export as XML. + * + * @author chatellier + * @version $Revision$ + * <p/> + * Last update : $Date$ + * By : $Author$ + */ +public interface NewExportService { + + String exportAllAsCSV(String path, String charset) throws LimaException; + + File exportAccountsAsCSV(String path, String charset) throws LimaException; + + File exportEntryBooksAsCSV(String path, String charset) throws LimaException; + + File exportFiscalPeriodAsCSV(String path, String charset) throws LimaException; + + File exportFinancialTransactionsAsCSV(String path, String charset) throws LimaException; + + File exportEntriesAsCSV(String path, String charset) throws LimaException; +} Added: 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 (rev 0) +++ trunk/lima-business-api/src/main/java/org/chorem/lima/business/api/NewImportService.java 2014-06-10 13:23:09 UTC (rev 3827) @@ -0,0 +1,21 @@ +package org.chorem.lima.business.api; + +import org.chorem.lima.business.LimaException; + +/** + * Created by davidcosse on 03/06/14. + */ +public interface NewImportService { + + void importAccountAsCSV(String contents) throws LimaException; + + void importEntryBooksAsCSV(String contents) throws LimaException; + + void importFiscalPeriodsAsCSV(String contents) throws LimaException; + + void importFinancialTransactionsAsCSV(String contents) throws LimaException; + + void importEntriesAsCSV(String contents) throws LimaException; + + void importAllAsCSV(String contents) throws LimaException; +} Modified: trunk/lima-callao/src/main/java/org/chorem/lima/DefaultServiceContext.java =================================================================== --- trunk/lima-callao/src/main/java/org/chorem/lima/DefaultServiceContext.java 2014-06-03 09:36:17 UTC (rev 3826) +++ trunk/lima-callao/src/main/java/org/chorem/lima/DefaultServiceContext.java 2014-06-10 13:23:09 UTC (rev 3827) @@ -46,7 +46,7 @@ // try to close non closed persistence context (false to not reopen it) AbstractTopiaPersistenceContext context = getPersistenceContext(false); if (context != null) { - context.closeContext(); + context.close(); } } @@ -97,11 +97,6 @@ } @Override - public void closeContext() { - - } - - @Override public boolean isClosed() { return false; } Modified: trunk/lima-callao/src/main/xmi/accounting-model.properties =================================================================== --- trunk/lima-callao/src/main/xmi/accounting-model.properties 2014-06-03 09:36:17 UTC (rev 3826) +++ trunk/lima-callao/src/main/xmi/accounting-model.properties 2014-06-10 13:23:09 UTC (rev 3827) @@ -61,3 +61,6 @@ org.chorem.lima.entity.VatStatement.attribute.subVatStatements.tagvalue.lazy=false org.chorem.lima.entity.VatStatement.attribute.masterVatStatement.tagvalue.lazy=false + +org.chorem.lima.entity.EntryBook.attribute.code.tagvalue.naturalId=true +org.chorem.lima.entity.EntryBook.attribute.code.tagvalue.notNull=true \ No newline at end of file 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-06-03 09:36:17 UTC (rev 3826) +++ trunk/lima-swing/src/main/java/org/chorem/lima/ui/importexport/ImportExport.java 2014-06-10 13:23:09 UTC (rev 3827) @@ -41,6 +41,7 @@ import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.StringWriter; +import java.nio.charset.Charset; import java.util.concurrent.ExecutionException; import javax.swing.JComboBox; @@ -50,6 +51,7 @@ import javax.swing.JPanel; import javax.swing.SwingWorker; +import com.google.common.base.Charsets; import com.google.common.base.Strings; import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringUtils; @@ -57,6 +59,7 @@ import org.apache.commons.logging.LogFactory; import org.chorem.lima.business.api.ExportService; import org.chorem.lima.business.api.ImportService; +import org.chorem.lima.business.api.NewExportService; import org.chorem.lima.business.utils.ImportExportEntityEnum; import org.chorem.lima.enums.EncodingEnum; import org.chorem.lima.enums.ImportExportEnum; @@ -86,6 +89,8 @@ protected ExportService exportService; + protected NewExportService newExportService; + private ImportExportWaitView waitView; public ImportExport(Component view) { @@ -94,6 +99,7 @@ //services importService = LimaServiceFactory.getService(ImportService.class); exportService = LimaServiceFactory.getService(ExportService.class); + newExportService = LimaServiceFactory.getService(NewExportService.class); //create the wait dialog panel waitView = new ImportExportWaitView(); @@ -113,13 +119,12 @@ */ public void importExport(ImportExportEnum importExportMethode, String file, boolean verbose) { final ImportExportEnum importExportMethodeF = importExportMethode; - encodingEnum = EncodingEnum.UTF8; + final Charset charset = Charsets.UTF_8; if (Strings.isNullOrEmpty(file)) { file = chooseFile(importExportMethode.getImportMode(), importExportMethode); } //if export cancel if (!Strings.isNullOrEmpty(file)) { - final EncodingEnum charset = encodingEnum; final String filePath = file; final Boolean verboseMode = verbose; final Boolean importMode = importExportMethode.getImportMode(); @@ -130,24 +135,23 @@ String result = ""; switch (importExportMethodeF) { case CSV_ALL_EXPORT: - datas = exportService.exportAsCSV(); - createFile(filePath, charset.getEncoding(), datas); + newExportService.exportAllAsCSV(filePath, charset.name()); break; case CSV_ACCOUNTCHARTS_EXPORT: datas = exportService.exportAccountsChartAsCSV(); - createFile(filePath, charset.getEncoding(), datas); + createFile(filePath, charset.name(), datas); break; case CSV_ENTRYBOOKS_EXPORT: datas = exportService.exportEntryBookChartAsCSV(); - createFile(filePath, charset.getEncoding(), datas); + createFile(filePath, charset.name(), datas); break; case CSV_FINANCIALSTATEMENTS_EXPORT: datas = exportService.exportFinancialStatementChartAsCSV(); - createFile(filePath, charset.getEncoding(), datas); + createFile(filePath, charset.name(), datas); break; case CSV_VAT_EXPORT: datas = exportService.exportVatStatementChartAsCSV(); - createFile(filePath, charset.getEncoding(), datas); + createFile(filePath, charset.name(), datas); break; case EBP_ACCOUNTCHARTS_EXPORT: //For windows ebp @@ -160,34 +164,34 @@ createFile(filePath, EncodingEnum.ISOLATIN1.getEncoding(), datas); break; case CSV_ALL_IMPORT: - datas = extractFile(filePath, charset.getEncoding()); + datas = extractFile(filePath, charset.name()); result = importService.importAllAsCSV(datas); break; case CSV_ACCOUNTCHARTS_IMPORT: - datas = extractFile(filePath, charset.getEncoding()); + datas = extractFile(filePath, charset.name()); result = importService.importAsCSV(datas, ImportExportEntityEnum.ACCOUNT); break; case CSV_ENTRYBOOKS_IMPORT: - datas = extractFile(filePath, charset.getEncoding()); + datas = extractFile(filePath, charset.name()); result = importService.importAsCSV(datas, ImportExportEntityEnum.ENTRYBOOK); break; case CSV_FINANCIALSTATEMENTS_IMPORT: - datas = extractFile(filePath, charset.getEncoding()); + datas = extractFile(filePath, charset.name()); result = importService.importAsCSV(datas, ImportExportEntityEnum.FINANCIALSTATEMENT); break; case CSV_VAT_IMPORT: - datas = extractFile(filePath, charset.getEncoding()); + datas = extractFile(filePath, charset.name()); result = importService.importAsCSV(datas, ImportExportEntityEnum.VATSTATEMENT); break; case CSV_ENTRIES_IMPORT: - datas = extractFile(filePath, charset.getEncoding()); + datas = extractFile(filePath, charset.name()); result = importService.importAsCSV(datas, ImportExportEntityEnum.ENTRY); break; case PDF_VAT_IMPORT: int response = JOptionPane.showConfirmDialog(waitView, t("lima.importexport.usevatpdf"), t("lima.common.confirmation"), JOptionPane.YES_NO_OPTION); - extractFile(filePath, charset.getEncoding()); + extractFile(filePath, charset.name()); result = importService.importAsPDF(filePath, ImportExportEntityEnum.VATPDF, response == JOptionPane.YES_OPTION); break; case EBP_ACCOUNTCHARTS_IMPORT: @@ -297,34 +301,41 @@ //Encoding option JComboBox comboBox = new JComboBox(EncodingEnum.descriptions()); - if (importExportMethode.getEncodingOption()) { - JPanel panel = new JPanel(); - panel.setLayout(new BorderLayout()); - panel.add(new JLabel(t("lima.importexport.choiceencoding")), BorderLayout.WEST); - panel.add(comboBox, BorderLayout.CENTER); - ((Container) chooser.getComponent(2)).add(panel, BorderLayout.SOUTH); - } - String approveButtonText; - if (importMode) { - chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); - approveButtonText = t("lima.ui.importexport.import"); - chooser.setDialogTitle(approveButtonText); - chooser.setApproveButtonText(approveButtonText); + if(importExportMethode.equals(ImportExportEnum.CSV_ALL_EXPORT)){ + } else { - approveButtonText = t("lima.ui.importexport.export"); - chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); - chooser.setDialogTitle(approveButtonText); - chooser.setApproveButtonText(approveButtonText); - } - if (chooser.showOpenDialog(viewComponent) == JFileChooser.APPROVE_OPTION) { - filePath = chooser.getSelectedFile().getAbsolutePath(); - if (importExportMethode.getEncodingOption()) { - encodingEnum = EncodingEnum.valueOfDescription((String) comboBox.getSelectedItem()); + JPanel panel = new JPanel(); + panel.setLayout(new BorderLayout()); + panel.add(new JLabel(t("lima.importexport.choiceencoding")), BorderLayout.WEST); + panel.add(comboBox, BorderLayout.CENTER); + ((Container) chooser.getComponent(2)).add(panel, BorderLayout.SOUTH); } + + String approveButtonText; + if (importMode) { + chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); + approveButtonText = t("lima.ui.importexport.import"); + chooser.setDialogTitle(approveButtonText); + chooser.setApproveButtonText(approveButtonText); + } else { + approveButtonText = t("lima.ui.importexport.export"); + chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); + chooser.setDialogTitle(approveButtonText); + chooser.setApproveButtonText(approveButtonText); + } + if (chooser.showOpenDialog(viewComponent) == JFileChooser.APPROVE_OPTION) { + filePath = chooser.getSelectedFile().getAbsolutePath(); + + if (importExportMethode.getEncodingOption()) { + encodingEnum = EncodingEnum.valueOfDescription((String) comboBox.getSelectedItem()); + } + } } + + return filePath; } @@ -362,30 +373,20 @@ * @return */ public String extractFile(String filePath, String charset) { - StringWriter sw = new StringWriter(); - BufferedReader in = null; + String result = null; + InputStream is = null; try { - InputStream is; + is = new FileInputStream(filePath); + result = IOUtils.toString(is, charset); - // FIXME echatellier 20101129 remove this hack and modify resources reading mecanism - if (filePath != null && filePath.indexOf(".jar!") > 0) { - String resourcesPath = filePath.substring(filePath.indexOf(".jar!") + 5); - is = ImportExport.class.getResourceAsStream(resourcesPath); - } else { - is = new FileInputStream(filePath); - } - - in = new BufferedReader(new InputStreamReader(is, charset)); - IOUtils.copy(in, sw); - in.close(); } catch (IOException eee) { if (log.isErrorEnabled()) { log.error("Can't read file " + filePath, eee); } } finally { - IOUtils.closeQuietly(in); + IOUtils.closeQuietly(is); } - return sw.toString(); + return result; } } Modified: trunk/lima-swing/src/main/java/org/chorem/lima/ui/ledger/LedgerViewHandler.java =================================================================== --- trunk/lima-swing/src/main/java/org/chorem/lima/ui/ledger/LedgerViewHandler.java 2014-06-03 09:36:17 UTC (rev 3826) +++ trunk/lima-swing/src/main/java/org/chorem/lima/ui/ledger/LedgerViewHandler.java 2014-06-10 13:23:09 UTC (rev 3827) @@ -108,7 +108,7 @@ /* @Override public void notifyMethod(String serviceName, String methodName) { if (serviceName.contains("FinancialTransaction") || - methodName.contains("importAccount") || + methodName.contains("importAccountAsCSV") || methodName.contains("importAll")) { refreshData(); } Modified: trunk/pom.xml =================================================================== --- trunk/pom.xml 2014-06-03 09:36:17 UTC (rev 3826) +++ trunk/pom.xml 2014-06-10 13:23:09 UTC (rev 3827) @@ -167,7 +167,8 @@ <nuitonConfigVersion>3.0-alpha-2</nuitonConfigVersion> <nuitonDecoratorVersion>3.0-alpha-3</nuitonDecoratorVersion> <nuitonProfilingVersion>2.7.1</nuitonProfilingVersion> - <eugeneVersion>2.9</eugeneVersion> + <nuitonCsvVersion>3.0-rc-1</nuitonCsvVersion> + <eugeneVersion>2.10</eugeneVersion> <nuitonI18nVersion>3.1</nuitonI18nVersion> <topiaVersion>3.0-SNAPSHOT</topiaVersion> @@ -323,6 +324,12 @@ </dependency> <dependency> + <groupId>org.nuiton</groupId> + <artifactId>nuiton-csv</artifactId> + <version>${nuitonCsvVersion}</version> + </dependency> + + <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <version>${h2Version}</version>