This is an automated email from the git hooks/post-receive script. New commit to branch feature/1194-default-file-loading in repository lima. See http://git.chorem.org/lima.git commit 22604dbf6887b8750d3610e4099bb7b2c4e3c303 Author: dcosse <cosse@codelutin.com> Date: Tue Mar 10 18:35:21 2015 +0100 fixes #1194 les fichiers sont retrouvés via leur URL et non plus via le file path --- .../org/chorem/lima/enums/AccountsChartEnum.java | 14 +- .../org/chorem/lima/enums/EntryBooksChartEnum.java | 11 +- .../lima/enums/FinancialStatementsChartEnum.java | 11 +- .../org/chorem/lima/enums/ImportExportEnum.java | 39 ++- .../chorem/lima/enums/VatStatementsChartEnum.java | 11 +- .../java/org/chorem/lima/ui/MainViewHandler.java | 2 +- .../chorem/lima/ui/account/AccountImportForm.css | 2 +- .../chorem/lima/ui/account/AccountViewHandler.java | 7 +- .../lima/ui/entrybook/EntryBookViewHandler.java | 2 +- .../FinancialStatementChartViewHandler.java | 2 +- .../chorem/lima/ui/importexport/ImportExport.java | 374 ++++++++++++--------- .../chorem/lima/ui/opening/CreateAccountsPanel.css | 2 +- .../org/chorem/lima/ui/opening/OpeningView.jaxx | 2 +- .../chorem/lima/ui/opening/OpeningViewHandler.java | 26 +- .../lima/ui/vatchart/VatChartViewHandler.java | 2 +- 15 files changed, 305 insertions(+), 202 deletions(-) diff --git a/lima-swing/src/main/java/org/chorem/lima/enums/AccountsChartEnum.java b/lima-swing/src/main/java/org/chorem/lima/enums/AccountsChartEnum.java index f45c2d7..4ec0a32 100644 --- a/lima-swing/src/main/java/org/chorem/lima/enums/AccountsChartEnum.java +++ b/lima-swing/src/main/java/org/chorem/lima/enums/AccountsChartEnum.java @@ -22,9 +22,11 @@ package org.chorem.lima.enums; +import java.net.URL; + public enum AccountsChartEnum { - IMPORT(""), IMPORTEBP(""), SHORTENED("pcg_shortened.csv"), BASE("pcg_base.csv"), DEVELOPED("pcg_developed.csv"); + IMPORT(""), IMPORT_EBP(""), SHORTENED("pcg_shortened.csv"), BASE("pcg_base.csv"), DEVELOPED("pcg_developed.csv"); private final String filePath; @@ -32,11 +34,9 @@ public enum AccountsChartEnum { this.filePath = filePath; } - public String getFilePath() { - String result = ""; - if (!filePath.equals("")) { - result = getClass().getResource("/import/" + filePath).getPath(); - } - return result; + public URL getDefaultFileURL() { + URL url = ImportExportEnum.getFileURL("/import/" + filePath); + return url; } + } diff --git a/lima-swing/src/main/java/org/chorem/lima/enums/EntryBooksChartEnum.java b/lima-swing/src/main/java/org/chorem/lima/enums/EntryBooksChartEnum.java index 70c24db..3a03314 100644 --- a/lima-swing/src/main/java/org/chorem/lima/enums/EntryBooksChartEnum.java +++ b/lima-swing/src/main/java/org/chorem/lima/enums/EntryBooksChartEnum.java @@ -22,6 +22,8 @@ package org.chorem.lima.enums; +import java.net.URL; + public enum EntryBooksChartEnum { IMPORT(""), DEFAULT("eb_default.csv"); @@ -32,11 +34,8 @@ public enum EntryBooksChartEnum { this.filePath = filePath; } - public String getFilePath() { - String result = ""; - if (!filePath.equals("")) { - result = getClass().getResource("/import/" + filePath).getPath(); - } - return result; + public URL getDefaultFileURL() { + URL url = ImportExportEnum.getFileURL("/import/" + filePath); + return url; } } diff --git a/lima-swing/src/main/java/org/chorem/lima/enums/FinancialStatementsChartEnum.java b/lima-swing/src/main/java/org/chorem/lima/enums/FinancialStatementsChartEnum.java index 383f058..b1df6bc 100644 --- a/lima-swing/src/main/java/org/chorem/lima/enums/FinancialStatementsChartEnum.java +++ b/lima-swing/src/main/java/org/chorem/lima/enums/FinancialStatementsChartEnum.java @@ -22,6 +22,8 @@ package org.chorem.lima.enums; +import java.net.URL; + public enum FinancialStatementsChartEnum { IMPORT(""), SHORTENED("bcr_shortened.csv"), BASE("bcr_base.csv"), DEVELOPED("bcr_developed.csv"); @@ -33,11 +35,8 @@ public enum FinancialStatementsChartEnum { } - public String getFilePath() { - String result = ""; - if (!filePath.equals("")) { - result = getClass().getResource("/import/" + filePath).getPath(); - } - return result; + public URL getDefaultFileUrl() { + URL url = ImportExportEnum.getFileURL("/import/" + filePath); + return url; } } diff --git a/lima-swing/src/main/java/org/chorem/lima/enums/ImportExportEnum.java b/lima-swing/src/main/java/org/chorem/lima/enums/ImportExportEnum.java index 1845c91..a7b4a1f 100644 --- a/lima-swing/src/main/java/org/chorem/lima/enums/ImportExportEnum.java +++ b/lima-swing/src/main/java/org/chorem/lima/enums/ImportExportEnum.java @@ -23,10 +23,19 @@ package org.chorem.lima.enums; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.chorem.lima.LimaTechnicalException; + +import java.io.File; +import java.net.MalformedURLException; +import java.net.URL; + /** first Boolean importMode define if it's a import (true) or an export (false) * second one to allow or not choce for encoding * */ -public enum ImportExportEnum { + public enum ImportExportEnum { + CSV_ALL_EXPORT(false, false), CSV_ALL_IMPORT(true, true), CSV_ACCOUNTCHARTS_EXPORT(false, true), CSV_ACCOUNTCHARTS_IMPORT(true, true), CSV_ENTRYBOOKS_EXPORT(false, true), CSV_ENTRYBOOKS_IMPORT(true, true), @@ -39,6 +48,8 @@ public enum ImportExportEnum { EBP_ACCOUNTCHARTS_IMPORT(true, true), EBP_ENTRIES_IMPORT(true, true), EBP_ENTRYBOOKS_IMPORT(true, true),EBP_ENTRYBOOKS_EXPORT(false, false); + private static final Log log = LogFactory.getLog(ImportExportEnum.class); + private final Boolean importMode; private final Boolean encodingOption; @@ -52,6 +63,32 @@ public enum ImportExportEnum { this.encodingOption = encodingOption; } + protected static URL getFileURL(String filePAth) { + URL result = ImportExportEnum.class.getResource(filePAth); + + if (result == null) { + File file = new File(filePAth); + if (log.isInfoEnabled()) { + log.info(String.format("Load %s from File path", filePAth)); + } + try { + result = file.toURI().toURL(); + } catch (MalformedURLException e) { + throw new LimaTechnicalException("Could not get url of file: " + file); + } + } else { + if (log.isInfoEnabled()) { + log.info(String.format("Load %s from resource path", filePAth)); + } + } + + if (result == null) { + throw new LimaTechnicalException(String.format("Could not load file: %s", filePAth)); + } + + return result; + } + /** * encoding choice * true if import or export methode let encoding option diff --git a/lima-swing/src/main/java/org/chorem/lima/enums/VatStatementsChartEnum.java b/lima-swing/src/main/java/org/chorem/lima/enums/VatStatementsChartEnum.java index 9f0a2d6..f66d246 100644 --- a/lima-swing/src/main/java/org/chorem/lima/enums/VatStatementsChartEnum.java +++ b/lima-swing/src/main/java/org/chorem/lima/enums/VatStatementsChartEnum.java @@ -21,6 +21,8 @@ */ package org.chorem.lima.enums; +import java.net.URL; + public enum VatStatementsChartEnum { IMPORT(""), SHORTENED("vat_shortened.csv"), BASE("vat_base.csv"), @@ -33,11 +35,8 @@ public enum VatStatementsChartEnum { } - public String getFilePath() { - String result = ""; - if (!filePath.equals("")) { - result = getClass().getResource("/import/" + filePath).getPath(); - } - return result; + public URL getDefaultFileUrl() { + URL url = ImportExportEnum.getFileURL("/import/" + filePath); + return url; } } diff --git a/lima-swing/src/main/java/org/chorem/lima/ui/MainViewHandler.java b/lima-swing/src/main/java/org/chorem/lima/ui/MainViewHandler.java index 1d66c5a..02fe187 100644 --- a/lima-swing/src/main/java/org/chorem/lima/ui/MainViewHandler.java +++ b/lima-swing/src/main/java/org/chorem/lima/ui/MainViewHandler.java @@ -483,7 +483,7 @@ public class MainViewHandler { ImportExportEnum type) { MainView mainView = getUI(rootContext); ImportExport importExport = new ImportExport(mainView); - importExport.importExport(type, "", true); + importExport.processImportExport(type, true); } public void loadURI(MainView ui) { diff --git a/lima-swing/src/main/java/org/chorem/lima/ui/account/AccountImportForm.css b/lima-swing/src/main/java/org/chorem/lima/ui/account/AccountImportForm.css index ca10320..ddaf141 100644 --- a/lima-swing/src/main/java/org/chorem/lima/ui/account/AccountImportForm.css +++ b/lima-swing/src/main/java/org/chorem/lima/ui/account/AccountImportForm.css @@ -54,7 +54,7 @@ #importebp { text : "lima.chart.accounts.import.ebp"; - value : "{AccountsChartEnum.IMPORTEBP}"; + value : "{AccountsChartEnum.IMPORT_EBP}"; buttonGroup : buttonGroup; } diff --git a/lima-swing/src/main/java/org/chorem/lima/ui/account/AccountViewHandler.java b/lima-swing/src/main/java/org/chorem/lima/ui/account/AccountViewHandler.java index 54e3122..005f1d9 100644 --- a/lima-swing/src/main/java/org/chorem/lima/ui/account/AccountViewHandler.java +++ b/lima-swing/src/main/java/org/chorem/lima/ui/account/AccountViewHandler.java @@ -467,14 +467,13 @@ public class AccountViewHandler implements ServiceListener { (AccountsChartEnum) value; //Import accounts chart switch (defaultAccountsChartEnum) { - case IMPORTEBP: - importExport.importExport(ImportExportEnum.EBP_ACCOUNTCHARTS_IMPORT, - defaultAccountsChartEnum.getFilePath(), false); + case IMPORT_EBP: + importExport.importExport(ImportExportEnum.EBP_ACCOUNTCHARTS_IMPORT, null, null, false); break; default: importExport.importExport(ImportExportEnum.CSV_ACCOUNTCHARTS_IMPORT, - defaultAccountsChartEnum.getFilePath(), false); + null, null, false); break; } } diff --git a/lima-swing/src/main/java/org/chorem/lima/ui/entrybook/EntryBookViewHandler.java b/lima-swing/src/main/java/org/chorem/lima/ui/entrybook/EntryBookViewHandler.java index 6e2c278..8f0073f 100644 --- a/lima-swing/src/main/java/org/chorem/lima/ui/entrybook/EntryBookViewHandler.java +++ b/lima-swing/src/main/java/org/chorem/lima/ui/entrybook/EntryBookViewHandler.java @@ -289,7 +289,7 @@ public class EntryBookViewHandler implements ServiceListener { EntryBooksChartEnum defaultEntryBooksEnum = (EntryBooksChartEnum) value; ImportExport importExport = new ImportExport(view); importExport.importExport(ImportExportEnum.CSV_ENTRYBOOKS_IMPORT, - defaultEntryBooksEnum.getFilePath(), true); + null, null, true); } } diff --git a/lima-swing/src/main/java/org/chorem/lima/ui/financialstatementchart/FinancialStatementChartViewHandler.java b/lima-swing/src/main/java/org/chorem/lima/ui/financialstatementchart/FinancialStatementChartViewHandler.java index eddc402..3e5bc1b 100644 --- a/lima-swing/src/main/java/org/chorem/lima/ui/financialstatementchart/FinancialStatementChartViewHandler.java +++ b/lima-swing/src/main/java/org/chorem/lima/ui/financialstatementchart/FinancialStatementChartViewHandler.java @@ -390,7 +390,7 @@ public class FinancialStatementChartViewHandler implements ServiceListener { (FinancialStatementsChartEnum) value; ImportExport importExport = new ImportExport(view); importExport.importExport(ImportExportEnum.CSV_FINANCIALSTATEMENTS_IMPORT, - defaultFinancialStatementsEnum.getFilePath(), true); + null, null, true); } } 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 1d13f18..afedfb3 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 @@ -73,6 +73,8 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStreamWriter; +import java.net.MalformedURLException; +import java.net.URL; import java.nio.charset.Charset; import java.rmi.server.ExportException; import java.text.SimpleDateFormat; @@ -131,159 +133,224 @@ public class ImportExport { * Call the appropriate method in business service. * * @param importExportChoice determine what to import/export and export type (CSV, EBP, PDF) - * @param path file path for import/export file, if null a dialog will ask for it + * @param exportPath file path for import/export file, if null a dialog will ask for it * @param verbose if true a dialog show result */ - public void importExport(final ImportExportEnum importExportChoice, String path, boolean verbose) { + public void importExport(final ImportExportEnum importExportChoice, String exportPath, URL importUrl, boolean verbose) { final Charset defaultCharset = Charsets.UTF_8; - String filePath = path; - if (Strings.isNullOrEmpty(filePath)) { - filePath = chooseFile(importExportChoice.getImportMode(), importExportChoice); + if (Strings.isNullOrEmpty(exportPath) && importUrl == null) { + exportPath = chooseFile(importExportChoice.getImportMode(), importExportChoice); + File file = new File(exportPath); + if (file.exists()) { + try { + importUrl = file.toURI().toURL(); + } catch (MalformedURLException e) { + throw new LimaTechnicalException("Can't open file " + exportPath); + } + } else { + throw new LimaTechnicalException("Can't open file from path" + exportPath); + } } - processImport(verbose, importExportChoice, defaultCharset, filePath); + processImportExport(verbose, importExportChoice, defaultCharset, exportPath, importUrl); } - protected void processImport(final boolean verboseMode, final ImportExportEnum importExportMethod, final Charset defaultCharset, final String filePath) { - //if export cancel - if (!Strings.isNullOrEmpty(filePath)) { - final Boolean importMode = importExportMethod.getImportMode(); - - new SwingWorker<ImportExportResults, Void>() { - - @Override - protected ImportExportResults doInBackground() { - ImportExportResults results = new ImportExportResults(); - switch (importExportMethod) { - //####################################### CSV ############################################## - - case CSV_ACCOUNTCHARTS_EXPORT: - results.pushExportResults(exportService.exportAccountsAsCSV(defaultCharset.name())); - createFile(filePath, defaultCharset.name(), results.getExportResults().get(0).getExportData()); - break; - - case CSV_ACCOUNTCHARTS_IMPORT: - String content = loadFile(filePath, defaultCharset.name()); - results.pushImportResults(importService.importAccountAsCSV(content)); - break; - case CSV_ENTRYBOOKS_EXPORT: - results.pushExportResults(exportService.exportEntryBooksAsCSV(defaultCharset.name())); - createFile(filePath, defaultCharset.name(), results.getExportResults().get(0).getExportData()); - break; - case CSV_ENTRYBOOKS_IMPORT: - content = loadFile(filePath, defaultCharset.name()); - results.pushImportResults(importService.importEntryBooksAsCSV(content)); - break; - case CSV_ENTRIES_EXPORT: - results.pushExportResults(exportService.exportEntriesAsCSV(defaultCharset.name(), true)); - createFile(filePath, defaultCharset.name(), results.getExportResults().get(0).getExportData()); - break; - case CSV_ENTRIES_IMPORT: - content = loadFile(filePath, defaultCharset.name()); - results.pushImportResults(importService.importEntriesAsCSV(content)); - break; - - case CSV_ALL_EXPORT: - results = exportBackup(filePath, defaultCharset.name()); - break; - case CSV_ALL_IMPORT: - results = importAllFromZipFile(filePath); - break; - - case CSV_VAT_EXPORT: - results.pushExportResults(exportService.exportVatStatements(defaultCharset.name())); - createFile(filePath, defaultCharset.name(), results.getExportResults().get(0).getExportData()); - break; - case CSV_VAT_IMPORT: - content = loadFile(filePath, defaultCharset.name()); - results.pushImportResults(importService.importVATStatementsAsCSV(content)); - break; - - case CSV_FINANCIALSTATEMENTS_EXPORT: - results.pushExportResults(exportService.exportFinancialStatements(defaultCharset.name())); - createFile(filePath, defaultCharset.name(), results.getExportResults().get(0).getExportData()); - break; - case CSV_FINANCIALSTATEMENTS_IMPORT: - content = loadFile(filePath, defaultCharset.name()); - results.pushImportResults(importService.importFinancialStatementsAsCSV(content)); - break; - - //####################################### EBP ############################################## - //For windows ebp so using encoding ISOLATIN1 - - case EBP_ACCOUNTCHARTS_EXPORT: - results.pushExportResults(exportService.exportAccountAsEbp(defaultCharset.name())); - createFile(filePath, EncodingEnum.ISOLATIN1.getEncoding(), results.getExportResults().get(0).getExportData()); - break; - case EBP_ACCOUNTCHARTS_IMPORT: - content = loadFile(filePath, EncodingEnum.ISOLATIN1.getEncoding()); - results.pushImportResults(importService.importAccountFromEbp(content)); - break; - - case EBP_ENTRYBOOKS_EXPORT: - results.pushExportResults(exportService.exportEntryBookAsEbp(defaultCharset.name())); - createFile(filePath, EncodingEnum.ISOLATIN1.getEncoding(), results.getExportResults().get(0).getExportData()); - break; - case EBP_ENTRYBOOKS_IMPORT: - content = loadFile(filePath, EncodingEnum.ISOLATIN1.getEncoding()); - results.pushImportResults(importService.importEntryBookFromEbp(content)); - break; - - case EBP_ENTRIES_EXPORT: - results.pushExportResults(exportService.exportEntriesAsEbp(defaultCharset.name())); - createFile(filePath, EncodingEnum.ISOLATIN1.getEncoding(), results.getExportResults().get(0).getExportData()); - break; - case EBP_ENTRIES_IMPORT: - content = loadFile(filePath, EncodingEnum.ISOLATIN1.getEncoding()); - results.pushImportResults(importService.importEntriesFromEbp(content)); - break; - default: - break; - } - return results; - } + public void processImportExport(final ImportExportEnum importExportChoice, boolean verbose) { + switch (importExportChoice) { + case CSV_ALL_EXPORT: + case CSV_ACCOUNTCHARTS_EXPORT: + case CSV_ENTRYBOOKS_EXPORT: + case CSV_ENTRIES_EXPORT: + case CSV_FINANCIALSTATEMENTS_EXPORT: + case CSV_VAT_EXPORT: + case PDF_VAT_EXPORT: + case EBP_ACCOUNTCHARTS_EXPORT: + case EBP_ENTRIES_EXPORT: + case EBP_ENTRYBOOKS_EXPORT: + processExport(importExportChoice, verbose); + break; + + case CSV_ALL_IMPORT: + case CSV_ACCOUNTCHARTS_IMPORT: + case CSV_ENTRYBOOKS_IMPORT: + case CSV_ENTRIES_IMPORT: + case CSV_FINANCIALSTATEMENTS_IMPORT: + case CSV_VAT_IMPORT: + case PDF_VAT_IMPORT: + case EBP_ACCOUNTCHARTS_IMPORT: + case EBP_ENTRIES_IMPORT: + case EBP_ENTRYBOOKS_IMPORT: + processImport(importExportChoice, verbose); + break; + } + } - @Override - protected void done() { - try { + protected void processImport(final ImportExportEnum importExportChoice, boolean verbose) { + final Charset defaultCharset = Charsets.UTF_8; + URL importFileUrl; + String importFilePath = chooseFile(importExportChoice.getImportMode(), importExportChoice); + File file = new File(importFilePath); + if (file.exists()) { + try { + importFileUrl = file.toURI().toURL(); + } catch (MalformedURLException e) { + throw new LimaTechnicalException("Can't open file " + importFilePath); + } + } else { + throw new LimaTechnicalException("Can't open file from path" + importFilePath); + } + log.info("Precessing import:" + importFileUrl.getPath() + " " + importFilePath + " " + importFileUrl.getFile()); + processImportExport(verbose, importExportChoice, defaultCharset, null, importFileUrl); + } - if (log.isDebugEnabled()) { - log.debug("importMode : " + importMode); - } + protected void processExport(final ImportExportEnum importExportChoice, boolean verbose) { + String exportFilePath = chooseFile(importExportChoice.getImportMode(), importExportChoice); + if (StringUtils.isNotBlank(exportFilePath)) { + log.info("Precessing export:" + exportFilePath); + processImportExport(verbose, importExportChoice, Charsets.UTF_8, exportFilePath, null); + } else { + throw new LimaTechnicalException("Targeted directory can not be found."); + } + } + + protected void processImportExport(final boolean verboseMode, final ImportExportEnum importExportMethod, final Charset defaultCharset, final String exportFilePath, final URL importURL) { + //if export cancel + final Boolean importMode = importExportMethod.getImportMode(); + + new SwingWorker<ImportExportResults, Void>() { + + @Override + protected ImportExportResults doInBackground() { + ImportExportResults results = new ImportExportResults(); + switch (importExportMethod) { + //####################################### CSV ############################################## + case CSV_ACCOUNTCHARTS_IMPORT: + String content = loadContent(importURL, defaultCharset.name()); + results.pushImportResults(importService.importAccountAsCSV(content)); + break; + case CSV_ENTRYBOOKS_IMPORT: + content = loadContent(importURL, defaultCharset.name()); + results.pushImportResults(importService.importEntryBooksAsCSV(content)); + break; + case CSV_ENTRIES_IMPORT: + content = loadContent(importURL, defaultCharset.name()); + results.pushImportResults(importService.importEntriesAsCSV(content)); + break; + case CSV_VAT_IMPORT: + content = loadContent(importURL, defaultCharset.name()); + results.pushImportResults(importService.importVATStatementsAsCSV(content)); + break; + case CSV_FINANCIALSTATEMENTS_IMPORT: + content = loadContent(importURL, defaultCharset.name()); + results.pushImportResults(importService.importFinancialStatementsAsCSV(content)); + break; + + case CSV_ACCOUNTCHARTS_EXPORT: + results.pushExportResults(exportService.exportAccountsAsCSV(defaultCharset.name())); + createFile(exportFilePath, defaultCharset.name(), results.getExportResults().get(0).getExportData()); + break; + case CSV_ENTRYBOOKS_EXPORT: + results.pushExportResults(exportService.exportEntryBooksAsCSV(defaultCharset.name())); + createFile(exportFilePath, defaultCharset.name(), results.getExportResults().get(0).getExportData()); + break; + case CSV_ENTRIES_EXPORT: + results.pushExportResults(exportService.exportEntriesAsCSV(defaultCharset.name(), true)); + createFile(exportFilePath, defaultCharset.name(), results.getExportResults().get(0).getExportData()); + break; + case CSV_ALL_EXPORT: + results = exportBackup(exportFilePath, defaultCharset.name()); + break; + case CSV_ALL_IMPORT: + results = importAllFromZipFile(importURL); + break; + case CSV_VAT_EXPORT: + results.pushExportResults(exportService.exportVatStatements(defaultCharset.name())); + createFile(exportFilePath, defaultCharset.name(), results.getExportResults().get(0).getExportData()); + break; + case CSV_FINANCIALSTATEMENTS_EXPORT: + results.pushExportResults(exportService.exportFinancialStatements(defaultCharset.name())); + createFile(exportFilePath, defaultCharset.name(), results.getExportResults().get(0).getExportData()); + break; + + //####################################### EBP ############################################## + //For windows ebp so using encoding ISOLATIN1 + case EBP_ACCOUNTCHARTS_IMPORT: + content = loadContent(importURL, EncodingEnum.ISOLATIN1.getEncoding()); + results.pushImportResults(importService.importAccountFromEbp(content)); + break; + + case EBP_ENTRYBOOKS_IMPORT: + content = loadContent(importURL, EncodingEnum.ISOLATIN1.getEncoding()); + results.pushImportResults(importService.importEntryBookFromEbp(content)); + break; + + case EBP_ENTRIES_IMPORT: + content = loadContent(importURL, EncodingEnum.ISOLATIN1.getEncoding()); + results.pushImportResults(importService.importEntriesFromEbp(content)); + break; + + case EBP_ACCOUNTCHARTS_EXPORT: + results.pushExportResults(exportService.exportAccountAsEbp(defaultCharset.name())); + createFile(exportFilePath, EncodingEnum.ISOLATIN1.getEncoding(), results.getExportResults().get(0).getExportData()); + break; + + case EBP_ENTRYBOOKS_EXPORT: + results.pushExportResults(exportService.exportEntryBookAsEbp(defaultCharset.name())); + createFile(exportFilePath, EncodingEnum.ISOLATIN1.getEncoding(), results.getExportResults().get(0).getExportData()); + break; + + case EBP_ENTRIES_EXPORT: + results.pushExportResults(exportService.exportEntriesAsEbp(defaultCharset.name())); + createFile(exportFilePath, EncodingEnum.ISOLATIN1.getEncoding(), results.getExportResults().get(0).getExportData()); + break; + + default: + break; + } + return results; + } - //hidde wait dialog panel - waitView.setVisible(false); + @Override + protected void done() { + try { - // display result dialog - if (verboseMode) { - ImportExportResults globalResult = get(); - if (importMode && globalResult == null) { - JOptionPane.showMessageDialog(viewComponent, t("lima.import.error"), - t("lima.import.title"), JOptionPane.ERROR_MESSAGE); + if (log.isDebugEnabled()) { + log.debug("importMode : " + importMode); + } + //hidde wait dialog panel + waitView.setVisible(false); + + // display result dialog + if (verboseMode) { + ImportExportResults globalResult = get(); + if (importMode && globalResult == null) { + JOptionPane.showMessageDialog(viewComponent, t("lima.import.error"), + t("lima.import.title"), JOptionPane.ERROR_MESSAGE); + + } else { + if (importMode) { + List<ImportResult> importResults = globalResult.getImportResults(); + if (importResults != null) { + ComputeImportResultMessage(importResults); + } } else { - if (importMode) { - List<ImportResult> importResults = globalResult.getImportResults(); - if (importResults != null) { - ComputeImportResultMessage(importResults); - } - } else { - List<ExportResult> exportResults = globalResult.getExportResults(); - if (exportResults != null) { - ComputeExportResultMessage(exportResults); - } + List<ExportResult> exportResults = globalResult.getExportResults(); + if (exportResults != null) { + ComputeExportResultMessage(exportResults); } } } - } catch (InterruptedException | ExecutionException ex) { - if (log.isErrorEnabled()) { - log.error("Can't get result message", ex); - } + } + } catch (InterruptedException | ExecutionException ex) { + if (log.isErrorEnabled()) { + log.error("Can't get result message", ex); } } - }.execute(); - waitView.setVisible(true); - } + } + }.execute(); + waitView.setVisible(true); + } protected void ComputeExportResultMessage(List<ExportResult> exportResults) { @@ -500,23 +567,25 @@ public class ImportExport { /** * Open csv file and get his data on a string. * - * @param filePath path to targeted file + * @param url url to targeted file * @param charset charset to use for import * @return file contents */ - protected String loadFile(String filePath, String charset) { + @Deprecated + protected String loadContent(URL url, String charset) { String result = null; - InputStream is = null; - try { - is = new FileInputStream(filePath); - result = IOUtils.toString(is, charset); - } catch (IOException eee) { - if (log.isErrorEnabled()) { - log.error("Can't read file " + filePath, eee); + if (url != null) { + try (InputStream inputStream = url.openStream()) { + result = IOUtils.toString(inputStream, charset); + if (log.isInfoEnabled()) { + log.info("Loading " + url.getPath()); + } + } catch (IOException e) { + if (log.isErrorEnabled()) { + log.error("Can't read file " + url.getPath(), e); + } } - } finally { - IOUtils.closeQuietly(is); } return result; @@ -568,24 +637,24 @@ public class ImportExport { return streamData; } - protected ImportExportResults importAllFromZipFile(String filePath) { + protected ImportExportResults importAllFromZipFile(URL url) { String tmpDir = System.getProperty("java.io.tmpdir")+"/"; String determinant = String.valueOf(new Date().getTime()); - ImportResult result = extractZipFile(filePath, tmpDir, determinant); + ImportResult result = extractZipFile(url, tmpDir, determinant); ImportExportResults results = processBackup(result, tmpDir, determinant); return results; } - protected ImportResult extractZipFile(String filePath, String tmpDir, String determinant) { + protected ImportResult extractZipFile(URL url, String tmpDir, String determinant) { ImportExportResults results = new ImportExportResults(); ImportResult result = results.createAddAndGetImportResult(null); ZipInputStream zipInputStream = null; - FileInputStream inputStream = null; + InputStream inputStream = null; // use to be sure to not load old streams. try { - inputStream = new FileInputStream(filePath); + inputStream = url.openStream(); zipInputStream = new ZipInputStream(inputStream); @@ -608,8 +677,9 @@ public class ImportExport { } } } catch (Exception e) { - result.addInitException(new ImportFileException(t("lima.import.error.extractFile"))); + result.addInitException(new ImportFileException(t("lima.import.error.extractFile", e))); log.error(e.getStackTrace()); + e.printStackTrace(); } finally { IOUtils.closeQuietly(zipInputStream); IOUtils.closeQuietly(inputStream); diff --git a/lima-swing/src/main/java/org/chorem/lima/ui/opening/CreateAccountsPanel.css b/lima-swing/src/main/java/org/chorem/lima/ui/opening/CreateAccountsPanel.css index aaa30f6..21b5c46 100644 --- a/lima-swing/src/main/java/org/chorem/lima/ui/opening/CreateAccountsPanel.css +++ b/lima-swing/src/main/java/org/chorem/lima/ui/opening/CreateAccountsPanel.css @@ -50,6 +50,6 @@ #importebp { text : "lima.chart.accounts.import.ebp"; - value : "{AccountsChartEnum.IMPORTEBP}"; + value : "{AccountsChartEnum.IMPORT_EBP}"; buttonGroup : buttonGroup; } \ No newline at end of file diff --git a/lima-swing/src/main/java/org/chorem/lima/ui/opening/OpeningView.jaxx b/lima-swing/src/main/java/org/chorem/lima/ui/opening/OpeningView.jaxx index feb964b..10968ae 100644 --- a/lima-swing/src/main/java/org/chorem/lima/ui/opening/OpeningView.jaxx +++ b/lima-swing/src/main/java/org/chorem/lima/ui/opening/OpeningView.jaxx @@ -54,7 +54,7 @@ <JPanel id='panel'> <JLabel id="welcomeLabel"/> <JButton id="importButton" - onActionPerformed="handler.importSave()"/> + onActionPerformed="handler.importBackup()"/> </JPanel> </cell> </row> diff --git a/lima-swing/src/main/java/org/chorem/lima/ui/opening/OpeningViewHandler.java b/lima-swing/src/main/java/org/chorem/lima/ui/opening/OpeningViewHandler.java index 717dd29..f56d2f4 100644 --- a/lima-swing/src/main/java/org/chorem/lima/ui/opening/OpeningViewHandler.java +++ b/lima-swing/src/main/java/org/chorem/lima/ui/opening/OpeningViewHandler.java @@ -74,9 +74,9 @@ public class OpeningViewHandler { } - public void importSave() { + public void importBackup() { ImportExport importExport = new ImportExport(view); - importExport.importExport(ImportExportEnum.CSV_ALL_IMPORT, "", true); + importExport.importExport(ImportExportEnum.CSV_ALL_IMPORT, null, null, true); view.dispose(); } @@ -112,29 +112,29 @@ public class OpeningViewHandler { (AccountsChartEnum) value; //Import accounts chart switch (defaultAccountsChartEnum) { - case IMPORTEBP: + case IMPORT_EBP: importExport.importExport(ImportExportEnum.EBP_ACCOUNTCHARTS_IMPORT, - defaultAccountsChartEnum.getFilePath(), false); + null, defaultAccountsChartEnum.getDefaultFileURL(), false); break; default: importExport.importExport(ImportExportEnum.CSV_ACCOUNTCHARTS_IMPORT, - defaultAccountsChartEnum.getFilePath(), false); + null, defaultAccountsChartEnum.getDefaultFileURL(), false); break; } //Import financialstatement switch (defaultAccountsChartEnum) { case SHORTENED: importExport.importExport(ImportExportEnum.CSV_FINANCIALSTATEMENTS_IMPORT, - FinancialStatementsChartEnum.SHORTENED.getFilePath(), false); + null, defaultAccountsChartEnum.getDefaultFileURL(), false); break; case DEVELOPED: importExport.importExport(ImportExportEnum.CSV_FINANCIALSTATEMENTS_IMPORT, - FinancialStatementsChartEnum.DEVELOPED.getFilePath(), false); + null, FinancialStatementsChartEnum.DEVELOPED.getDefaultFileUrl() , false); break; default: importExport.importExport(ImportExportEnum.CSV_FINANCIALSTATEMENTS_IMPORT, - FinancialStatementsChartEnum.BASE.getFilePath(), false); + null, FinancialStatementsChartEnum.BASE.getDefaultFileUrl(), false); break; } //Import vatstatement @@ -142,19 +142,19 @@ public class OpeningViewHandler { case SHORTENED: // TODO DCossé 31/07/14 why vat_base, default developped and shortened file have got same contents ? importExport.importExport(ImportExportEnum.CSV_VAT_IMPORT, - VatStatementsChartEnum.SHORTENED.getFilePath(), false); + null, VatStatementsChartEnum.SHORTENED.getDefaultFileUrl(), false); break; case BASE: importExport.importExport(ImportExportEnum.CSV_VAT_IMPORT, - VatStatementsChartEnum.BASE.getFilePath(), false); + null, VatStatementsChartEnum.BASE.getDefaultFileUrl(), false); break; case DEVELOPED: importExport.importExport(ImportExportEnum.CSV_VAT_IMPORT, - VatStatementsChartEnum.DEVELOPED.getFilePath(), false); + null, VatStatementsChartEnum.DEVELOPED.getDefaultFileUrl(), false); break; default: importExport.importExport(ImportExportEnum.CSV_VAT_IMPORT, - VatStatementsChartEnum.DEFAULT.getFilePath(), false); + null, VatStatementsChartEnum.DEFAULT.getDefaultFileUrl(), false); break; } @@ -170,7 +170,7 @@ public class OpeningViewHandler { case 3: if (ebPanel.getImportEntryBook().isSelected()) { importExport.importExport(ImportExportEnum.CSV_ENTRYBOOKS_IMPORT, - EntryBooksChartEnum.DEFAULT.getFilePath(), false); + null, EntryBooksChartEnum.DEFAULT.getDefaultFileURL(), false); } view.getEntrybooksIcon().setBorder(noBorder); view.getFiscalperiodsIcon().setBorder(BorderFactory.createLineBorder(green, 2)); diff --git a/lima-swing/src/main/java/org/chorem/lima/ui/vatchart/VatChartViewHandler.java b/lima-swing/src/main/java/org/chorem/lima/ui/vatchart/VatChartViewHandler.java index c1ab881..cc56408 100644 --- a/lima-swing/src/main/java/org/chorem/lima/ui/vatchart/VatChartViewHandler.java +++ b/lima-swing/src/main/java/org/chorem/lima/ui/vatchart/VatChartViewHandler.java @@ -228,7 +228,7 @@ public class VatChartViewHandler implements ServiceListener { (VatStatementsChartEnum) value; ImportExport importExport = new ImportExport(view); importExport.importExport(ImportExportEnum.CSV_VAT_IMPORT, - defaultVatStatementsEnum.getFilePath(), true); + null, null, true); refresh(); } } -- To stop receiving notification emails like this one, please contact chorem.org SCM administrator <admin+scm@chorem.org>.