This is an automated email from the git hooks/post-receive script. New commit to branch feature/1224 in repository lima. See http://git.chorem.org/lima.git commit 0336a0b2d2d23fd8a8a924e2295b1d0f9d03fd4a Author: Sylvain Bavencoff <bavencoff@codelutin.com> Date: Tue May 19 11:22:48 2015 +0200 fixes #1224 : Ajout la date et le journal de la transaction ainssi qu'un id permettant de distinguer les transactions --- .../lima/business/ejb/ExportServiceImpl.java | 2 +- .../lima/business/ejb/ImportServiceImpl.java | 41 ++++++++- .../chorem/lima/business/ejb/csv/EntryModel.java | 98 +++++++++++++++++++--- .../lima/business/ImportExportServiceTest.java | 14 ++-- .../chorem/lima/ui/importexport/ImportExport.java | 2 +- 5 files changed, 130 insertions(+), 27 deletions(-) diff --git a/lima-business/src/main/java/org/chorem/lima/business/ejb/ExportServiceImpl.java b/lima-business/src/main/java/org/chorem/lima/business/ejb/ExportServiceImpl.java index 2c9e119..edc9740 100644 --- a/lima-business/src/main/java/org/chorem/lima/business/ejb/ExportServiceImpl.java +++ b/lima-business/src/main/java/org/chorem/lima/business/ejb/ExportServiceImpl.java @@ -186,7 +186,7 @@ public class ExportServiceImpl extends AbstractLimaService implements ExportServ EntryTopiaDao dao = getDaoHelper().getEntryDao(); List<Entry> entities = dao.findAll(); if (entities != null && !entities.isEmpty()) { - EntryModel model = new EntryModel(accountService, financialTransactionService , humanReadable); + EntryModel model = new EntryModel(accountService, entryBookService, financialTransactionService , humanReadable); try { exportResult.setExportData(Export.exportToString(model, entities, Charset.forName(charset), true)); } catch (Exception e) { diff --git a/lima-business/src/main/java/org/chorem/lima/business/ejb/ImportServiceImpl.java b/lima-business/src/main/java/org/chorem/lima/business/ejb/ImportServiceImpl.java index 766b126..b66e798 100644 --- a/lima-business/src/main/java/org/chorem/lima/business/ejb/ImportServiceImpl.java +++ b/lima-business/src/main/java/org/chorem/lima/business/ejb/ImportServiceImpl.java @@ -25,6 +25,7 @@ package org.chorem.lima.business.ejb; import com.google.common.base.Function; import com.google.common.collect.Maps; import com.google.common.collect.Ordering; +import com.google.common.collect.Sets; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; import org.chorem.lima.beans.EntryEBP; @@ -97,6 +98,7 @@ import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Set; import static org.nuiton.i18n.I18n.t; @@ -303,13 +305,46 @@ public class ImportServiceImpl extends AbstractLimaService implements ImportServ // import and save entries InputStream contentStream = IOUtils.toInputStream(contents); try { - ImportModel<Entry> model = new EntryModel(accountService, financialTransactionService, false); + ImportModel<Entry> model = new EntryModel(accountService, entryBookService, financialTransactionService, true); + Import<Entry> entries = Import.newImport(model, contentStream); + Set<FinancialTransaction> financialTransactions = Sets.newHashSet(); + for (Entry entry : entries) { + FinancialTransaction financialTransaction = entry.getFinancialTransaction(); + financialTransaction.addEntry(entry); + financialTransactions.add(financialTransaction); + result.increaseCreated(); + } + for (FinancialTransaction financialTransaction : financialTransactions) { + financialTransactionService.createFinancialTransaction(financialTransaction); + } + } catch (Exception e) { + result.addInitException(new ImportFileException(e.getMessage())); + results.setErrors(true); + } finally { + IOUtils.closeQuietly(contentStream); + } + + } + return results; + } + + protected ImportExportResults importEntriesAsCSVBackUp(String contents) { + ImportExportResults results = new ImportExportResults(); + ImportResult result = results.createAddAndGetImportResult(Entry.class); + + if (StringUtils.isBlank(contents)) { + result.addException(new NoDataToImportException()); + } else { + // import and save entries + InputStream contentStream = IOUtils.toInputStream(contents); + try { + ImportModel<Entry> model = new EntryModel(accountService, entryBookService, financialTransactionService, false); Import<Entry> entries = Import.newImport(model, contentStream); for (Entry entry : entries) { entryService.createEntry(entry); result.increaseCreated(); } - } catch (ImportRuntimeException e) { + } catch (Exception e) { result.addInitException(new ImportFileException(e.getMessage())); results.setErrors(true); } finally { @@ -757,7 +792,7 @@ public class ImportServiceImpl extends AbstractLimaService implements ImportServ return globalResult; } - subReport = importEntriesAsCSV(entries); + subReport = importEntriesAsCSVBackUp(entries); globalResult.pushImportResults(subReport); if (subReport.isErrors()){ return globalResult; diff --git a/lima-business/src/main/java/org/chorem/lima/business/ejb/csv/EntryModel.java b/lima-business/src/main/java/org/chorem/lima/business/ejb/csv/EntryModel.java index 7fd48d4..d99f696 100644 --- a/lima-business/src/main/java/org/chorem/lima/business/ejb/csv/EntryModel.java +++ b/lima-business/src/main/java/org/chorem/lima/business/ejb/csv/EntryModel.java @@ -24,37 +24,51 @@ package org.chorem.lima.business.ejb.csv; import com.google.common.base.Preconditions; import com.google.common.base.Strings; +import com.google.common.collect.Maps; 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.Entry; +import org.chorem.lima.entity.EntryBook; import org.chorem.lima.entity.EntryImpl; 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; import org.nuiton.csv.ValueFormatter; import org.nuiton.csv.ValueParser; -import java.text.SimpleDateFormat; +import java.util.Map; /** * Created by davidcosse on 03/06/14. */ public class EntryModel extends AbstractLimaModel<Entry> implements ExportModel<Entry> { + public static final String PROPERTY_ENTRY_BOOK = Entry.PROPERTY_FINANCIAL_TRANSACTION + "." + FinancialTransaction.PROPERTY_ENTRY_BOOK + "." + EntryBook.PROPERTY_CODE; + public static final String PROPERTY_DATE = Entry.PROPERTY_FINANCIAL_TRANSACTION + "." + FinancialTransaction.PROPERTY_TRANSACTION_DATE; + protected FinancialTransactionService financialTransactionService; protected AccountService accountService; + protected EntryBookService entryBookService; protected boolean humanReadable; - public EntryModel(AccountService accountService, FinancialTransactionService financialTransactionService, boolean humanReadable) { + public EntryModel(AccountService accountService, EntryBookService entryBookService, FinancialTransactionService financialTransactionService, boolean humanReadable) { super(';'); this.accountService = accountService; + this.entryBookService = entryBookService; this.financialTransactionService = financialTransactionService; this.humanReadable = humanReadable; - newMandatoryColumn("account", Entry.PROPERTY_ACCOUNT, ACCOUNT_NUMBER_TO_ACCOUNT_TRANSACTION_PARSER); + newMandatoryColumn("financialTransaction", Entry.PROPERTY_FINANCIAL_TRANSACTION, new FinancialTransactionParser()); + if (humanReadable) { + newMandatoryColumn("entryBook", Entry.PROPERTY_FINANCIAL_TRANSACTION + "." + FinancialTransaction.PROPERTY_ENTRY_BOOK, new EntryBookParser()); + newMandatoryColumn("date", PROPERTY_DATE, DATE_PARSER); + } + newMandatoryColumn("account", Entry.PROPERTY_ACCOUNT, new AccountParser()); newOptionalColumn("amount", Entry.PROPERTY_AMOUNT, BIG_DECIMAL_WITH_NULL_PARSER); newOptionalColumn("debit", Entry.PROPERTY_DEBIT, O_N_PARSER); newOptionalColumn("detail", Entry.PROPERTY_DETAIL); @@ -62,12 +76,16 @@ public class EntryModel extends AbstractLimaModel<Entry> implements ExportModel< newOptionalColumn("voucher", Entry.PROPERTY_VOUCHER); newOptionalColumn("description", Entry.PROPERTY_DESCRIPTION); newOptionalColumn("lettering", Entry.PROPERTY_LETTERING); - newMandatoryColumn("financialTransaction", Entry.PROPERTY_FINANCIAL_TRANSACTION, FINANCIAL_TRANSACTION_ID_TO_FINANCIAL_TRANSACTION_PARSER); } @Override public Iterable<ExportableColumn<Entry, Object>> getColumnsForExport() { ModelBuilder<Entry> modelBuilder = new ModelBuilder<>(); + modelBuilder.newColumnForExport("financialTransaction", Entry.PROPERTY_FINANCIAL_TRANSACTION, new financialTransactionFormatter()); + if (humanReadable) { + modelBuilder.newColumnForExport("entryBook", PROPERTY_ENTRY_BOOK); + modelBuilder.newColumnForExport("date", PROPERTY_DATE, DATE_FORMATTER); + } 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); @@ -76,7 +94,6 @@ public class EntryModel extends AbstractLimaModel<Entry> implements ExportModel< 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_FORMATTER); return (Iterable) modelBuilder.getColumnsForExport(); } @@ -86,18 +103,29 @@ public class EntryModel extends AbstractLimaModel<Entry> implements ExportModel< return new EntryImpl(); } - protected final ValueParser<FinancialTransaction> FINANCIAL_TRANSACTION_ID_TO_FINANCIAL_TRANSACTION_PARSER = new ValueParser<FinancialTransaction>() { + protected class FinancialTransactionParser implements ValueParser<FinancialTransaction> { + + Map<Integer, FinancialTransaction> transactionByNums = Maps.newHashMap(); @Override public FinancialTransaction parse(String value) { Preconditions.checkArgument(!Strings.isNullOrEmpty(value)); FinancialTransaction result; - result = financialTransactionService.getFinancialTransactionWithId(value); + if (humanReadable) { + int num = Integer.parseInt(value); + result = transactionByNums.get(num); + if (result == null) { + result = new FinancialTransactionImpl(); + transactionByNums.put(num, result); + } + } else { + result = financialTransactionService.getFinancialTransactionWithId(value); + } return result; } - }; + } - protected final ValueParser<Account> ACCOUNT_NUMBER_TO_ACCOUNT_TRANSACTION_PARSER = new ValueParser<Account>() { + protected class AccountParser implements ValueParser<Account> { @Override public Account parse(String value) { @@ -109,15 +137,59 @@ public class EntryModel extends AbstractLimaModel<Entry> implements ExportModel< } return result; } - }; + } + + protected class EntryBookParser implements ValueParser<EntryBook> { + + protected Map<String, EntryBook> entryBookByCode; + + public EntryBookParser() { + entryBookByCode = Maps.newHashMap(); + for (EntryBook entryBook : entryBookService.getAllEntryBooks()) { + entryBookByCode.put(entryBook.getCode(), entryBook); + } + } + + @Override + public EntryBook parse(String value) { + EntryBook result; + if (StringUtils.isNotBlank(value)) { + result = entryBookByCode.get(value); + } else { + result = null; + } + return result; + } + } + + protected class financialTransactionFormatter implements ValueFormatter<FinancialTransaction> { + + Map<FinancialTransaction, Integer> numByTransactions = Maps.newHashMap(); + + int nextNum = 0; - protected final ValueFormatter<FinancialTransaction> FINANCIAL_TRANSACTION_TO_FINANCIAL_TRANSACTION_FORMATTER = new ValueFormatter<FinancialTransaction>() { @Override public String format(FinancialTransaction value) { String result; if (value != null) { - SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATE_FORMAT); - result = humanReadable ? simpleDateFormat.format(value.getTransactionDate()) : value.getTopiaId(); + if (humanReadable) { + Integer num = numByTransactions.get(value); + + if (num == null) { + + num = nextNum++; + + numByTransactions.put(value, num); + + } + + result = num.toString(); + + } else { + + result = value.getTopiaId(); + + } } else { result = ""; } diff --git a/lima-business/src/test/java/org/chorem/lima/business/ImportExportServiceTest.java b/lima-business/src/test/java/org/chorem/lima/business/ImportExportServiceTest.java index 23ea2d9..8e342c6 100644 --- a/lima-business/src/test/java/org/chorem/lima/business/ImportExportServiceTest.java +++ b/lima-business/src/test/java/org/chorem/lima/business/ImportExportServiceTest.java @@ -172,25 +172,21 @@ public class ImportExportServiceTest extends AbstractLimaTest { //test export String tmpDir = System.getProperty("java.io.tmpdir")+"/"; - ImportExportResults export = exportService.exportEntriesAsCSV(Charset.defaultCharset().name(), false); + ImportExportResults export = exportService.exportEntriesAsCSV(Charset.defaultCharset().name(), true); InputStream stream = IOUtils.toInputStream(export.getExportResults().get(0).getExportData()); FileOutputStream res = new FileOutputStream(tmpDir + "export-entries.csv"); IOUtils.copy(stream, res); - for (Entry entry : entries) { - FinancialTransaction financialTransaction = entry.getFinancialTransaction(); - financialTransactionService.removeEntry(entry); - financialTransactionService.updateFinancialTransaction(financialTransaction); + // clear transaction + for (FinancialTransaction financialTransaction : financialTransactions) { + financialTransactionService.removeFinancialTransaction(financialTransaction); } // ake sure 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()); + Assert.assertTrue(financialTransactions.isEmpty()); // test import FileInputStream contentStream = null; diff --git a/lima-swing/src/main/java/org/chorem/lima/ui/importexport/ImportExport.java b/lima-swing/src/main/java/org/chorem/lima/ui/importexport/ImportExport.java index 164ec2d..a27af37 100644 --- a/lima-swing/src/main/java/org/chorem/lima/ui/importexport/ImportExport.java +++ b/lima-swing/src/main/java/org/chorem/lima/ui/importexport/ImportExport.java @@ -211,7 +211,7 @@ public class ImportExport { String exportFilePath = chooseFile(importExportChoice.getImportMode(), importExportChoice); if (exportFilePath != null) { // if null ==> cancel export File file = new File(exportFilePath); - if (file.exists()) { + if (file.getParentFile().exists()) { log.info("Precessing export:" + exportFilePath); processImportExport(verbose, importExportChoice, Charsets.UTF_8, exportFilePath, null); } else { -- To stop receiving notification emails like this one, please contact chorem.org SCM administrator <admin+scm@chorem.org>.