Author: dcosse Date: 2014-06-10 18:17:01 +0200 (Tue, 10 Jun 2014) New Revision: 3828 Url: http://forge.chorem.org/projects/lima/repository/revisions/3828 Log: #1032 move file's creation to swing Modified: trunk/lima-business-api/src/main/java/org/chorem/lima/business/api/NewExportService.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/test/java/org/chorem/lima/business/NewExportServiceTest.java Modified: trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/NewExportServiceImpl.java =================================================================== --- trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/NewExportServiceImpl.java 2014-06-10 13:23:09 UTC (rev 3827) +++ trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/NewExportServiceImpl.java 2014-06-10 16:17:01 UTC (rev 3828) @@ -27,6 +27,7 @@ import com.google.common.collect.Lists; import org.apache.commons.io.FileUtils; +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; @@ -57,7 +58,6 @@ 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; @@ -93,17 +93,19 @@ @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; + String filePath; try { + List<File> files = Lists.newArrayList(); + files.add(exportAccountsFile(charset)); + files.add(exportEntryBooksFile(charset)); + files.add(exportFiscalPeriodFile(charset)); + files.add(exportFinancialTransactionsFile(charset)); + files.add(exportEntriesFile(charset)); + + SimpleDateFormat exportDate = new SimpleDateFormat(DATE_PATTERN); + filePath = path + "LIMA-BACKUP-"+ exportDate.format(new Date()) +".zip"; + FileOutputStream out = new FileOutputStream(filePath); export = new ZipOutputStream(out); @@ -123,109 +125,139 @@ } 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 - } - } + IOUtils.closeQuietly(export); } return filePath; } - @Override - public File exportAccountsAsCSV(String path, String charset) throws LimaException { - File file; - try { + protected File exportAccountsFile(String charset) throws Exception { + AccountTopiaDao accountTopiaDao = getDaoHelper().getAccountDao(); + List<Account> entities = accountTopiaDao.findAll(); - AccountTopiaDao accountTopiaDao = getDaoHelper().getAccountDao(); - List<Account> entities = accountTopiaDao.findAll(); + AccountModel model = new AccountModel(); + String tmpDir = System.getProperty("java.io.tmpdir")+"/"; - file = new File(path); - AccountModel model = new AccountModel(); - Export.exportToFile(model, entities, file, Charset.forName(charset)); - new FileInputStream(file); + File result = new File(tmpDir + "accounts.csv"); + Export.exportToFile(model, entities, result, Charset.forName(charset)); + return result; + } + @Override + public String exportAccountsStream(String charset) throws LimaException { + String result; + try { + File file = exportAccountsFile(charset); + FileInputStream inputStream = new FileInputStream(file); + result = IOUtils.toString(inputStream); } catch (Exception ex) { throw new LimaException("Can't export accounts", ex); } - return file; + return result; } - @Override - public File exportEntryBooksAsCSV(String path, String charset) throws LimaException { - File file; - try { + protected File exportEntryBooksFile(String charset) throws Exception { + EntryBookTopiaDao entryBookTopiaDao = getDaoHelper().getEntryBookDao(); + List<EntryBook> entities = entryBookTopiaDao.findAll(); - EntryBookTopiaDao entryBookTopiaDao = getDaoHelper().getEntryBookDao(); - List<EntryBook> entities = entryBookTopiaDao.findAll(); + String tmpDir = System.getProperty("java.io.tmpdir")+"/"; + File result = new File(tmpDir + "entryBooks.csv"); + EntryBookModel model = new EntryBookModel(); + Export.exportToFile(model, entities, result, Charset.forName(charset)); - file = new File(path); - EntryBookModel model = new EntryBookModel(); - Export.exportToFile(model, entities, file, Charset.forName(charset)); - new FileInputStream(file); + return result; + } + + @Override + public String exportEntryBooksStream(String charset) throws LimaException { + String result; + try { + File file = exportEntryBooksFile(charset); + FileInputStream inputStream = new FileInputStream(file); + result = IOUtils.toString(inputStream); } catch (Exception ex) { throw new LimaException("Can't export entry books", ex); } - return file; + return result; } + protected File exportFiscalPeriodFile(String charset) throws Exception { + + FiscalPeriodTopiaDao dao = getDaoHelper().getFiscalPeriodDao(); + List<FiscalPeriod> entities = dao.findAll(); + + String tmpDir = System.getProperty("java.io.tmpdir")+"/"; + File result = new File(tmpDir + "fiscalPeriod.csv"); + FiscalPeriodModel model = new FiscalPeriodModel(); + Export.exportToFile(model, entities, result, Charset.forName(charset)); + + return result; + } + @Override - public File exportFiscalPeriodAsCSV(String path, String charset) throws LimaException { - File file; + public String exportFiscalPeriodsStream(String charset) throws LimaException { + String result; 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); + File file = exportFiscalPeriodFile(charset); + FileInputStream inputStream = new FileInputStream(file); + result = IOUtils.toString(inputStream); } catch (Exception ex) { throw new LimaException("Can't export financial transactions", ex); } - return file; + return result; } + protected File exportFinancialTransactionsFile(String charset) throws Exception { + File result; + FinancialTransactionTopiaDao financialTransactionTopiaDao = + getDaoHelper().getFinancialTransactionDao(); + List<FinancialTransaction> entities = financialTransactionTopiaDao.findAll(); + + String tmpDir = System.getProperty("java.io.tmpdir")+"/"; + result = new File(tmpDir + "financialTransactions.csv"); + FinancialTransactionModel model = new FinancialTransactionModel(entryBookService); + Export.exportToFile(model, entities, result, Charset.forName(charset)); + Export.exportToFile(model, entities, result, Charset.forName(charset)); + return result; + } + @Override - public File exportFinancialTransactionsAsCSV(String path, String charset) throws LimaException { - File file; + public String exportFinancialTransactionsAsStream(String charset) throws LimaException { + String result; 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); + File file = exportFinancialTransactionsFile(charset); + FileInputStream inputStream = new FileInputStream(file); + result = IOUtils.toString(inputStream); } catch (Exception ex) { throw new LimaException("Can't export financial transactions", ex); } - return file; + return result; } + protected File exportEntriesFile(String charset) throws Exception { + EntryTopiaDao dao = getDaoHelper().getEntryDao(); + List<Entry> entities = dao.findAll(); + + String tmpDir = System.getProperty("java.io.tmpdir")+"/"; + File result = new File(tmpDir + "entries.csv"); + EntryModel model = new EntryModel(accountService, financialTransactionService); + Export.exportToFile(model, entities, result, Charset.forName(charset)); + return result; + } + @Override - public File exportEntriesAsCSV(String path, String charset) throws LimaException { - File file; + public String exportEntriesAsCSV(String charset) throws LimaException { + String result; 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); + File file = exportEntriesFile(charset); + FileInputStream inputStream = new FileInputStream(file); + result = IOUtils.toString(inputStream); } catch (Exception ex) { throw new LimaException("Can't export financial transactions", ex); } - return file; + return result; } } Modified: trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/NewImportServiceImpl.java =================================================================== --- trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/NewImportServiceImpl.java 2014-06-10 13:23:09 UTC (rev 3827) +++ trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/NewImportServiceImpl.java 2014-06-10 16:17:01 UTC (rev 3828) @@ -168,25 +168,29 @@ public void importAllAsCSV(String path) throws LimaException { FileInputStream contentStream; ZipInputStream zipInputStream = null; + + InputStream transactionsStream = null, entryBooksStream = null, fiscalPeriodsStream = null, entriesStream = null, accountsStream = null; try { contentStream = new FileInputStream(path); zipInputStream = new ZipInputStream(contentStream); + // unzip ZipEntry entry; + String tmpDir = System.getProperty("java.io.tmpdir")+"/"; 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")); + if (entry.getName().equalsIgnoreCase("accounts.csv")) { + fileoutputstream = new FileOutputStream(tmpDir + "accounts.csv"); + } else if (entry.getName().equalsIgnoreCase("entryBooks.csv")) { + fileoutputstream = new FileOutputStream(tmpDir + "entryBooks.csv"); + } else if (entry.getName().equalsIgnoreCase("fiscalPeriod.csv")) { + fileoutputstream = new FileOutputStream(tmpDir + "fiscalPeriods.csv"); + } else if (entry.getName().equalsIgnoreCase("financialTransactions.csv")) { + fileoutputstream = new FileOutputStream(tmpDir + "financialTransactions.csv"); + } else if (entry.getName().equalsIgnoreCase("entries.csv")) { + fileoutputstream = new FileOutputStream(tmpDir + "entries.csv"); } int n; @@ -200,23 +204,24 @@ zipInputStream.closeEntry(); } - InputStream transactionsStream = new FileInputStream(t("lima-business.document.financialTransactions")); + // import + transactionsStream = new FileInputStream(tmpDir + "financialTransactions.csv"); String transactionsStreamString = IOUtils.toString(transactionsStream); importFinancialTransactionsAsCSV(transactionsStreamString); - InputStream entryBooksStream = new FileInputStream(t("lima-business.document.entryBooks")); + entryBooksStream = new FileInputStream(tmpDir + "entryBooks.csv"); String entryBooksStreamString = IOUtils.toString(entryBooksStream); importEntryBooksAsCSV(entryBooksStreamString); - InputStream fiscalPeriodsStream = new FileInputStream(t("lima-business.document.fiscalPeriods")); + fiscalPeriodsStream = new FileInputStream(tmpDir + "fiscalPeriods.csv"); String fiscalPeriodsStreamString = IOUtils.toString(fiscalPeriodsStream); importFiscalPeriodsAsCSV(fiscalPeriodsStreamString); - InputStream entriesStream = new FileInputStream(t("lima-business.document.entries")); + entriesStream = new FileInputStream(tmpDir + "entries.csv"); String entriesStreamString = IOUtils.toString(entriesStream); importEntriesAsCSV(entriesStreamString); - InputStream accountsStream = new FileInputStream(t("lima-business.document.accounts")); + accountsStream = new FileInputStream(tmpDir + "accounts.csv"); String accountsStreamString = IOUtils.toString(accountsStream); importAccountAsCSV(accountsStreamString); @@ -224,6 +229,11 @@ throw new LimaException("Could not import", e); } finally { IOUtils.closeQuietly(zipInputStream); + IOUtils.closeQuietly(transactionsStream); + IOUtils.closeQuietly(entryBooksStream); + IOUtils.closeQuietly(fiscalPeriodsStream); + IOUtils.closeQuietly(entriesStream); + IOUtils.closeQuietly(accountsStream); } } } Modified: trunk/lima-business/src/test/java/org/chorem/lima/business/NewExportServiceTest.java =================================================================== --- trunk/lima-business/src/test/java/org/chorem/lima/business/NewExportServiceTest.java 2014-06-10 13:23:09 UTC (rev 3827) +++ trunk/lima-business/src/test/java/org/chorem/lima/business/NewExportServiceTest.java 2014-06-10 16:17:01 UTC (rev 3828) @@ -13,6 +13,8 @@ import java.io.File; import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.InputStream; import java.nio.charset.Charset; import java.util.Collection; import java.util.List; @@ -31,8 +33,11 @@ // export accounts - File file = newExportService.exportAccountsAsCSV("export-accounts.csv", Charset.defaultCharset().name()); - log.info("Account File path:" + file.getAbsolutePath()); + String tmpDir = System.getProperty("java.io.tmpdir")+"/"; + String export = newExportService.exportAccountsStream(Charset.defaultCharset().name()); + InputStream stream = IOUtils.toInputStream(export); + FileOutputStream res = new FileOutputStream(tmpDir + "export-accounts.csv"); + IOUtils.copy(stream, res); // remove accounts int nbEntities = accounts.size(); @@ -42,28 +47,29 @@ Assert.assertEquals(0, accountService.getAllAccounts().size()); // import accounts - FileInputStream contentStream = null; + InputStream contentStream = null; try { - contentStream = new FileInputStream(file.getPath()); - String stream = IOUtils.toString(contentStream); - newImportService.importAccountAsCSV(stream); + contentStream = new FileInputStream(tmpDir + "export-accounts.csv"); + String inportStream = IOUtils.toString(contentStream); + newImportService.importAccountAsCSV(inportStream); // 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); + String tmpDir = System.getProperty("java.io.tmpdir")+"/"; + String export = newExportService.exportEntryBooksStream(Charset.defaultCharset().name()); + InputStream stream = IOUtils.toInputStream(export); + FileOutputStream res = new FileOutputStream(tmpDir + "export-EntryBooks.csv"); + IOUtils.copy(stream, res); + List<EntryBook> entryBooks = entryBookService.getAllEntryBooks(); int nbEntities = entryBooks.size(); Assert.assertEquals(3, nbEntities); @@ -76,23 +82,25 @@ FileInputStream contentStream = null; try { - contentStream = new FileInputStream(file.getPath()); - String stream = IOUtils.toString(contentStream); - newImportService.importEntryBooksAsCSV(stream); + contentStream = new FileInputStream(tmpDir + "export-EntryBooks.csv"); + String inportStream = IOUtils.toString(contentStream); + newImportService.importEntryBooksAsCSV(inportStream); } 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); + String tmpDir = System.getProperty("java.io.tmpdir")+"/"; + String export = newExportService.exportFinancialTransactionsAsStream(Charset.defaultCharset().name()); + InputStream stream = IOUtils.toInputStream(export); + FileOutputStream res = new FileOutputStream(tmpDir + "export-financial-transactions.csv"); + IOUtils.copy(stream, res); List<FinancialTransaction> financialTransactions = financialTransactionService.getAllFinancialTransactions(df.parse("January 1, 2012"),df.parse("December 31, 2012")); int nbEntities = financialTransactions.size(); @@ -106,15 +114,14 @@ FileInputStream contentStream = null; try { - contentStream = new FileInputStream(file.getPath()); - String stream = IOUtils.toString(contentStream); - newImportService.importFinancialTransactionsAsCSV(stream); + contentStream = new FileInputStream(tmpDir + "export-financial-transactions.csv"); + String inportStream = IOUtils.toString(contentStream); + newImportService.importFinancialTransactionsAsCSV(inportStream); } finally { IOUtils.closeQuietly(contentStream); } Assert.assertEquals(nbEntities, financialTransactionService.getAllFinancialTransactions(df.parse("January 1, 2012"),df.parse("December 31, 2012")).size()); - FileUtils.forceDelete(file); } @Test @@ -131,10 +138,12 @@ Assert.assertEquals(2,nbEntities); //test export - File file = newExportService.exportEntriesAsCSV("export-entries.csv", Charset.defaultCharset().name()); - Assert.assertNotNull(file); + String tmpDir = System.getProperty("java.io.tmpdir")+"/"; + String export = newExportService.exportEntriesAsCSV(Charset.defaultCharset().name()); + InputStream stream = IOUtils.toInputStream(export); + FileOutputStream res = new FileOutputStream(tmpDir + "export-entries.csv"); + IOUtils.copy(stream, res); - for (Entry entry : entries) { FinancialTransaction financialTransaction = entry.getFinancialTransaction(); financialTransactionService.removeEntry(entry); @@ -153,9 +162,9 @@ // test import FileInputStream contentStream = null; try { - contentStream = new FileInputStream(file.getPath()); - String stream = IOUtils.toString(contentStream); - newImportService.importEntriesAsCSV(stream); + contentStream = new FileInputStream(tmpDir + "export-entries.csv"); + String inputStream = IOUtils.toString(contentStream); + newImportService.importEntriesAsCSV(inputStream); } finally { IOUtils.closeQuietly(contentStream); } @@ -168,15 +177,17 @@ } 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); + String tmpDir = System.getProperty("java.io.tmpdir")+"/"; + String export = newExportService.exportFiscalPeriodsStream(Charset.defaultCharset().name()); + InputStream stream = IOUtils.toInputStream(export); + FileOutputStream res = new FileOutputStream(tmpDir + "export-fiscal-periods.csv"); + IOUtils.copy(stream, res); List<FiscalPeriod> fiscalPeriods = fiscalPeriodService.getAllFiscalPeriods(); int nbEntities = fiscalPeriods.size(); @@ -188,15 +199,14 @@ FileInputStream contentStream = null; try { - contentStream = new FileInputStream(file.getPath()); - String stream = IOUtils.toString(contentStream); - newImportService.importFiscalPeriodsAsCSV(stream); + contentStream = new FileInputStream(tmpDir + "export-fiscal-periods.csv"); + String inputStream = IOUtils.toString(contentStream); + newImportService.importFiscalPeriodsAsCSV(inputStream); } finally { IOUtils.closeQuietly(contentStream); } Assert.assertEquals(nbEntities, fiscalPeriodService.getAllFiscalPeriods().size()); - FileUtils.forceDelete(file); } @Test Modified: 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/NewExportService.java 2014-06-10 13:23:09 UTC (rev 3827) +++ trunk/lima-business-api/src/main/java/org/chorem/lima/business/api/NewExportService.java 2014-06-10 16:17:01 UTC (rev 3828) @@ -44,13 +44,13 @@ String exportAllAsCSV(String path, String charset) throws LimaException; - File exportAccountsAsCSV(String path, String charset) throws LimaException; + String exportAccountsStream(String charset) throws LimaException; - File exportEntryBooksAsCSV(String path, String charset) throws LimaException; + String exportEntryBooksStream(String charset) throws LimaException; - File exportFiscalPeriodAsCSV(String path, String charset) throws LimaException; + String exportFiscalPeriodsStream(String charset) throws LimaException; - File exportFinancialTransactionsAsCSV(String path, String charset) throws LimaException; + String exportFinancialTransactionsAsStream(String charset) throws LimaException; - File exportEntriesAsCSV(String path, String charset) throws LimaException; + String exportEntriesAsCSV(String charset) throws LimaException; }