r3903 - trunk/lima-swing/src/main/java/org/chorem/lima/ui/importexport
Author: dcosse Date: 2014-08-06 17:56:15 +0200 (Wed, 06 Aug 2014) New Revision: 3903 Url: http://forge.chorem.org/projects/lima/repository/revisions/3903 Log: refs #1032 gestion des erreurs en cas de mauvais fichier en entr?\195?\169e Modified: trunk/lima-swing/src/main/java/org/chorem/lima/ui/importexport/ImportExport.java 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-08-06 14:27:15 UTC (rev 3902) +++ trunk/lima-swing/src/main/java/org/chorem/lima/ui/importexport/ImportExport.java 2014-08-06 15:56:15 UTC (rev 3903) @@ -31,6 +31,7 @@ import org.apache.commons.io.IOUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.chorem.lima.LimaConfig; import org.chorem.lima.LimaTechnicalException; import org.chorem.lima.business.ExportResult; import org.chorem.lima.business.ImportExportResults; @@ -40,7 +41,6 @@ import org.chorem.lima.business.exceptions.AlreadyExistFinancialStatement; import org.chorem.lima.business.exceptions.AlreadyExistVatStatementException; import org.chorem.lima.business.exceptions.BeginAfterEndFiscalPeriodException; -import org.chorem.lima.business.exceptions.ImportBackupException; import org.chorem.lima.business.exceptions.ImportFileException; import org.chorem.lima.business.exceptions.InvalidAccountNumberException; import org.chorem.lima.business.exceptions.LimaException; @@ -119,6 +119,7 @@ //services importService = LimaServiceFactory.getService(ImportService.class); exportService = LimaServiceFactory.getService(ExportService.class); + errorHelper = new ErrorHelper(LimaConfig.getInstance()); //create the wait dialog panel waitView = new ImportExportWaitView(); @@ -266,7 +267,7 @@ log.debug("importMode"); log.debug("importExportMethodeF : " + importExportMethodeF); for (ImportResult result : resultList) { - String importedEntity = result.getFromSource().getSimpleName(); + String importedEntity = result.getFromSource() == null ? "BACKUP" : result.getFromSource().getSimpleName(); log.debug(importedEntity + " nbCreated: " + result.getNbCreated()); log.debug(importedEntity + " nbUpdated: " + result.getNbUpdated()); log.debug(importedEntity + " nbIgnored: " + result.getNbIgnored()); @@ -292,14 +293,19 @@ message += t("lima.ui.importexport.import.nbIgnored", result.getNbIgnored())+"\n"; Map<Integer, LimaException> exceptionsByLine = result.getAllExceptionsByLine(); message = displayErrorMessage(message, exceptionsByLine); - message +="\n"; + // message is null if import failed + if (message == null) { + break; + } } - JOptionPane.showMessageDialog( - waitView, - message, - t("lima.ui.importexport.import"), - JOptionPane.INFORMATION_MESSAGE); + if (message != null) { + JOptionPane.showMessageDialog( + waitView, + message, + t("lima.ui.importexport.import"), + JOptionPane.INFORMATION_MESSAGE); + } } } else { List<ExportResult> exportResults = results.getExportResults(); @@ -354,7 +360,7 @@ Set<Integer> lines = exceptionsByLine.keySet(); for (Integer line : lines) { LimaException importException = exceptionsByLine.get(line); - message += t("lima.ui.importexport.import.line", line); + message = message + t("lima.ui.importexport.import.line", line); if (importException instanceof InvalidAccountNumberException) { message += t("lima.fiscalPeriod.franceAccountingRules.invalidAccountNumberException", ((InvalidAccountNumberException) importException).getAccountNumber())+"\n"; } else if (importException instanceof NotNumberAccountNumberException) { @@ -378,9 +384,11 @@ } else if (importException instanceof NoFiscalPeriodFoundException) { message += t("lima.import.entries.noFiscalPeriodFoundException")+"\n"; } else if (importException instanceof NoDataToImportException) { - message += t("lima.import.noDataToImportException")+"\n"; + errorHelper.showErrorMessage(t("lima.import.noDataToImportException")); + return null; } else if (importException instanceof ImportFileException){ - message += ((ImportFileException) importException).getDetailMessage(); + errorHelper.showErrorMessage(((ImportFileException) importException).getDetailMessage()); + return null; } else { message +=t("lima.import.unknownError"); } @@ -396,7 +404,7 @@ private String getFromSourceMessage(Class fromSource) { String message; if (fromSource == null) { - message = ""; + message = "BACKUP"+"\n"; } else if (fromSource.equals(Account.class)){ message = t("lima.ui.importexport.account")+"\n"; } else if (fromSource.equals(EntryBook.class)) { @@ -557,6 +565,8 @@ ZipInputStream zipInputStream = null; String tmpDir = System.getProperty("java.io.tmpdir")+"/"; FileInputStream inputStream = null; + // use to be sure to not load old streams. + String date = String.valueOf(new Date().getTime()); try { inputStream = new FileInputStream(filePath); @@ -566,7 +576,7 @@ while ((entry = zipInputStream.getNextEntry()) != null) { byte[] buffer = new byte[2048]; - FileOutputStream fileoutputstream = new FileOutputStream(tmpDir + entry.getName() + ".csv"); + FileOutputStream fileoutputstream = new FileOutputStream(tmpDir + entry.getName() + "-" + date +".csv"); int n; while ((n = zipInputStream.read(buffer, 0, 2048)) > -1) { @@ -584,30 +594,30 @@ } InputStream transactionsStream, entryBooksStream, fiscalPeriodsStream, entriesStream, accountsStream, identityStream; - if (result.getAllExceptionsByLine().isEmpty()) { + if (result.getAllExceptionsByLine() == null || result.getAllExceptionsByLine().isEmpty()) { try { - entryBooksStream = new FileInputStream(tmpDir + EntryBook.class.getSimpleName() +".csv"); + entryBooksStream = new FileInputStream(tmpDir + EntryBook.class.getSimpleName() + "-" + date + ".csv"); String entryBooksStreamString = IOUtils.toString(entryBooksStream); IOUtils.closeQuietly(entryBooksStream); // import - transactionsStream = new FileInputStream(tmpDir + FinancialTransaction.class.getSimpleName() +".csv"); + transactionsStream = new FileInputStream(tmpDir + FinancialTransaction.class.getSimpleName() + "-" + date + ".csv"); String transactionsStreamString = IOUtils.toString(transactionsStream); IOUtils.closeQuietly(transactionsStream); - fiscalPeriodsStream = new FileInputStream(tmpDir + FiscalPeriod.class.getSimpleName() +".csv"); + fiscalPeriodsStream = new FileInputStream(tmpDir + FiscalPeriod.class.getSimpleName() + "-" + date + ".csv"); String fiscalPeriodsStreamString = IOUtils.toString(fiscalPeriodsStream); IOUtils.closeQuietly(fiscalPeriodsStream); - accountsStream = new FileInputStream(tmpDir + Account.class.getSimpleName() +".csv"); + accountsStream = new FileInputStream(tmpDir + Account.class.getSimpleName() + "-" + date + ".csv"); String accountsStreamString = IOUtils.toString(accountsStream); IOUtils.closeQuietly(accountsStream); - entriesStream = new FileInputStream(tmpDir + Entry.class.getSimpleName() +".csv"); + entriesStream = new FileInputStream(tmpDir + Entry.class.getSimpleName() + "-" + date + ".csv"); String entriesStreamString = IOUtils.toString(entriesStream); IOUtils.closeQuietly(entriesStream); - identityStream = new FileInputStream(tmpDir + Identity.class.getSimpleName() + ".csv"); + identityStream = new FileInputStream(tmpDir + Identity.class.getSimpleName() + "-" + date + ".csv"); String identityStreamString = IOUtils.toString(identityStream); IOUtils.closeQuietly(identityStream); @@ -616,7 +626,7 @@ if(log.isInfoEnabled()) { log.info(ex); } - result.addException(new ImportBackupException(t("lima.ui.importexport.import.backupException"),ex)); + result.addInitException(new ImportFileException(t("lima.ui.importexport.import.extractFileError"))); } } return results;
participants (1)
-
dcosse@users.chorem.org