Author: jpepin Date: 2010-07-02 12:31:31 +0200 (Fri, 02 Jul 2010) New Revision: 2961 Url: http://chorem.org/repositories/revision/lima/2961 Log: Import / Export des journaux Modified: trunk/lima-business/src/main/java/org/chorem/lima/business/ImportExportService.java trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/ImportExportServiceImpl.java trunk/lima-swing/src/main/java/org/chorem/lima/ui/entrybook/EntryBookTableModel.java trunk/lima-swing/src/main/java/org/chorem/lima/ui/entrybook/EntryBookView.jaxx trunk/lima-swing/src/main/java/org/chorem/lima/ui/entrybook/EntryBookViewHandler.java trunk/lima-swing/src/main/java/org/chorem/lima/ui/financialstatementchart/FinancialStatementChartView.jaxx trunk/lima-swing/src/main/resources/i18n/lima-swing-en_GB.properties trunk/lima-swing/src/main/resources/i18n/lima-swing-fr_FR.properties Modified: trunk/lima-business/src/main/java/org/chorem/lima/business/ImportExportService.java =================================================================== --- trunk/lima-business/src/main/java/org/chorem/lima/business/ImportExportService.java 2010-07-02 09:46:14 UTC (rev 2960) +++ trunk/lima-business/src/main/java/org/chorem/lima/business/ImportExportService.java 2010-07-02 10:31:31 UTC (rev 2961) @@ -120,7 +120,7 @@ /** * import entrybook chart CSV. */ - void importEntryBookChartCSV(String path) throws LimaException; + String importEntryBookChartCSV(String path) throws LimaException; /** * export financialstatement chart as CSV. Modified: trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/ImportExportServiceImpl.java =================================================================== --- trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/ImportExportServiceImpl.java 2010-07-02 09:46:14 UTC (rev 2960) +++ trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/ImportExportServiceImpl.java 2010-07-02 10:31:31 UTC (rev 2961) @@ -40,6 +40,7 @@ import org.chorem.lima.beans.FinancialStatementImport; import org.chorem.lima.beans.FinancialStatementImportImpl; import org.chorem.lima.business.AccountService; +import org.chorem.lima.business.EntryBookService; import org.chorem.lima.business.FinancialStatementService; import org.chorem.lima.business.ImportExportService; import org.chorem.lima.business.ImportExportServiceLocal; @@ -49,6 +50,9 @@ import org.chorem.lima.entity.Account; import org.chorem.lima.entity.AccountDAO; import org.chorem.lima.entity.AccountImpl; +import org.chorem.lima.entity.EntryBook; +import org.chorem.lima.entity.EntryBookDAO; +import org.chorem.lima.entity.EntryBookImpl; import org.chorem.lima.entity.FinancialStatement; import org.chorem.lima.entity.FinancialStatementDAO; import org.chorem.lima.entity.FinancialStatementImpl; @@ -85,6 +89,9 @@ @EJB FinancialStatementService financialStatementService; + + @EJB + EntryBookService entryBookService; public ImportExportServiceImpl() { LimaConfig config = LimaConfig.getInstance(); @@ -331,15 +338,91 @@ @Override public void exportEntryBookChartAsCSV(String path) throws LimaException { + File f = new File(path); + TopiaContext topiaContext = null; + try { + topiaContext = beginTransaction(); + CSVWriter csvWriter = new CSVWriter(new FileWriter(f), ';'); + String[] nextLine = new String[3]; + // Get all entrybook + EntryBookDAO entryBookDAO = LimaCallaoDAOHelper.getEntryBookDAO(topiaContext); + + List<EntryBook> listEntryBook = entryBookDAO.findAll(); + nextLine[0] = "lima.entrybooks"; + csvWriter.writeNext(nextLine); + // For all EntryBook + for (EntryBook entryBook : listEntryBook) { + nextLine[0] = entryBook.getCode(); + nextLine[1] = entryBook.getLabel(); + nextLine[2] = entryBook.getType(); + // Ajoute la ligne au fichier + csvWriter.writeNext(nextLine); + } + // Write cache in file + csvWriter.flush(); + csvWriter.close(); + } + catch (TopiaException eeeTE){ + doCatch(topiaContext, eeeTE, log); + } + catch (IOException eeeIO) { + log.debug("Can't create new CSV Writer with file"+f+eeeIO); + } + finally { + doFinally(topiaContext, log); + } - } @Override - public void importEntryBookChartCSV(String path) throws LimaException { - // TODO Auto-generated method stub - + public String importEntryBookChartCSV(String path) throws LimaException { + File f = new File(path); + String result = ""; + + TopiaContext topiaContext = null; + try { + topiaContext = beginTransaction(); + String[] nextLine = new String[1]; + CSVReader csvReader = new CSVReader(new FileReader(f), ';'); + + nextLine = csvReader.readNext(); // File Type + if (!nextLine[0].equals("lima.entrybooks")){ + throw new LimaBusinessException( + "The file is not an export csv file type entrybooks"); + } + + EntryBookDAO entryBookDAO = LimaCallaoDAOHelper.getEntryBookDAO(topiaContext); + + while ((nextLine = csvReader.readNext()) != null) { + EntryBook entryBook = new EntryBookImpl(); + entryBook.setCode(nextLine[0]); + entryBook.setLabel(nextLine[1]); + entryBook.setType(nextLine[2]); + + //if exist, skip + if (entryBookDAO.findByCode(entryBook.getCode()) != null){ + result += "FAILED : The entrybook " + + entryBook.getLabel() + " already exists !\n"; + } + else { + //create it + entryBookService.createEntryBook(entryBook); + result += "SUCCESS : The financialStatement " + + entryBook.getLabel() + " is created ! \n"; + } + } + } + catch (TopiaException eeeTE){ + doCatch(topiaContext, eeeTE, log); + } + catch (IOException eeeIO) { + log.debug("Can't create new CSV Reader with file"+f+eeeIO); + } + finally { + doFinally(topiaContext, log); + } + return result; } @Override @@ -351,14 +434,14 @@ topiaContext = beginTransaction(); CSVWriter csvWriter = new CSVWriter(new FileWriter(f), ';'); String[] nextLine = new String[9]; - // Récupère tous les comptes + // Get all Financialstatements FinancialStatementDAO financialStatementDAO = LimaCallaoDAOHelper.getFinancialStatementDAO(topiaContext); List<FinancialStatement> listFinancialStatements = financialStatementDAO.findAll(); nextLine[0] = "lima.financialstatementschart"; csvWriter.writeNext(nextLine); - // Pour tous les comptes + // For all Financialstatements for (FinancialStatement financialStatement : listFinancialStatements) { nextLine[0] = financialStatement.getLabel(); nextLine[1] = new Boolean( @@ -380,7 +463,7 @@ masterFinancialStatement.getLabel(); } nextLine[8] = masterFinancialStatementString; - // Ajoute la ligne au fichier + // Add line in file csvWriter.writeNext(nextLine); } // Write cache in file Modified: trunk/lima-swing/src/main/java/org/chorem/lima/ui/entrybook/EntryBookTableModel.java =================================================================== --- trunk/lima-swing/src/main/java/org/chorem/lima/ui/entrybook/EntryBookTableModel.java 2010-07-02 09:46:14 UTC (rev 2960) +++ trunk/lima-swing/src/main/java/org/chorem/lima/ui/entrybook/EntryBookTableModel.java 2010-07-02 10:31:31 UTC (rev 2961) @@ -132,6 +132,11 @@ return false; } + + public void refreshTable(){ + fireTableDataChanged(); + } + /** * @param entryBook * @throws LimaException Modified: trunk/lima-swing/src/main/java/org/chorem/lima/ui/entrybook/EntryBookView.jaxx =================================================================== --- trunk/lima-swing/src/main/java/org/chorem/lima/ui/entrybook/EntryBookView.jaxx 2010-07-02 09:46:14 UTC (rev 2960) +++ trunk/lima-swing/src/main/java/org/chorem/lima/ui/entrybook/EntryBookView.jaxx 2010-07-02 10:31:31 UTC (rev 2961) @@ -27,7 +27,7 @@ ]]> </script> <row> - <cell fill="both" weightx="1" weighty="1" rows='4'> + <cell fill="both" weightx="1" weighty="1" rows='5'> <JScrollPane> <org.jdesktop.swingx.JXTable id="entryBooksTable" rowHeight="24" model="{new org.chorem.lima.ui.entrybook.EntryBookTableModel()}" @@ -57,4 +57,10 @@ onActionPerformed="getHandler().deleteEntryBook()"/> </cell> </row> + <row> + <cell fill="horizontal"> + <JButton id="importexportButton" text="lima.common.importexport" + onActionPerformed="getHandler().importexportEntryBook()"/> + </cell> + </row> </Table> \ No newline at end of file Modified: trunk/lima-swing/src/main/java/org/chorem/lima/ui/entrybook/EntryBookViewHandler.java =================================================================== --- trunk/lima-swing/src/main/java/org/chorem/lima/ui/entrybook/EntryBookViewHandler.java 2010-07-02 09:46:14 UTC (rev 2960) +++ trunk/lima-swing/src/main/java/org/chorem/lima/ui/entrybook/EntryBookViewHandler.java 2010-07-02 10:31:31 UTC (rev 2961) @@ -20,16 +20,23 @@ import static org.nuiton.i18n.I18n._; +import javax.swing.JFileChooser; import javax.swing.JOptionPane; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.chorem.lima.business.ImportExportService; import org.chorem.lima.business.LimaException; import org.chorem.lima.entity.EntryBook; import org.chorem.lima.entity.EntryBookImpl; +import org.chorem.lima.service.LimaServiceFactory; +import org.chorem.lima.ui.ImportExportForm; +import org.chorem.lima.ui.account.AccountTreeTableModel; import org.chorem.lima.util.DialogHelper; import org.chorem.lima.util.ErrorHelper; +import org.chorem.lima.util.FileChooseView; import org.jdesktop.swingx.JXTable; +import org.jdesktop.swingx.JXTreeTable; /** * Handler for entry book view. @@ -46,8 +53,11 @@ protected EntryBookView view; + protected ImportExportService importExportService; + public EntryBookViewHandler(EntryBookView view) { this.view = view; + importExportService = LimaServiceFactory.getInstance().getImportExportService(); } public void addEntryBook() { @@ -137,4 +147,49 @@ DialogHelper.showMessageDialog(eee.getMessage()); } } + + +public void importexportEntryBook(){ + + JXTable entryBookTable = view.getEntryBooksTable(); + EntryBookTableModel entryBookTableModel = + (EntryBookTableModel)entryBookTable.getModel(); + + ImportExportForm importExportForm = new ImportExportForm(view); + importExportForm.setLocationRelativeTo(view); + importExportForm.setVisible(true); + + Object value = importExportForm.getRadioButtons().getSelectedValue(); + // if action confirmed + if (value != null){ + String mode = (String) value; + FileChooseView fileChooseView = new FileChooseView(view); + + JFileChooser chooser = fileChooseView.getChooser(); + + try { + if (mode.equals("import")){ + chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); + if (chooser.showOpenDialog(view) == JFileChooser.APPROVE_OPTION) { + String filePath = chooser.getSelectedFile().getAbsolutePath(); + String message = importExportService.importEntryBookChartCSV(filePath); + entryBookTableModel.refreshTable(); + DialogHelper.showMessageDialog(message); + } + } + else { + chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); + if (chooser.showOpenDialog(view) == JFileChooser.APPROVE_OPTION) { + String filePath = chooser.getSelectedFile().getAbsolutePath(); + importExportService.exportEntryBookChartAsCSV(filePath); + } + } + } catch (LimaException eee) { + if (log.isErrorEnabled()){ + log.error("Can't "+ mode +" this file", eee); + } + DialogHelper.showMessageDialog(eee.getMessage()); + } + } + } } Modified: trunk/lima-swing/src/main/java/org/chorem/lima/ui/financialstatementchart/FinancialStatementChartView.jaxx =================================================================== --- trunk/lima-swing/src/main/java/org/chorem/lima/ui/financialstatementchart/FinancialStatementChartView.jaxx 2010-07-02 09:46:14 UTC (rev 2960) +++ trunk/lima-swing/src/main/java/org/chorem/lima/ui/financialstatementchart/FinancialStatementChartView.jaxx 2010-07-02 10:31:31 UTC (rev 2961) @@ -64,7 +64,7 @@ enabled="{isSelectedRow()}"/> </cell> </row> - <row> + <row> <cell fill="horizontal"> <JButton id="importexportButton" text="lima.common.importexport" onActionPerformed="getHandler().importexportFinancialStatement()"/> Modified: trunk/lima-swing/src/main/resources/i18n/lima-swing-en_GB.properties =================================================================== --- trunk/lima-swing/src/main/resources/i18n/lima-swing-en_GB.properties 2010-07-02 09:46:14 UTC (rev 2960) +++ trunk/lima-swing/src/main/resources/i18n/lima-swing-en_GB.properties 2010-07-02 10:31:31 UTC (rev 2961) @@ -33,7 +33,7 @@ lima.common.add= lima.common.addSubLedger= lima.common.cancel= -lima.common.importexport= +lima.common.importexport=Import/Export lima.common.ok= lima.common.print= lima.common.quit= @@ -93,9 +93,8 @@ lima.import.all.csv=Import all datas (CSV) lima.import.all.csv.ebp=Import all datas (EBP) lima.import.journal=Import journal -lima.importexport.export= -lima.importexport.import= -lima.importexport.update= +lima.importexport.export=Export +lima.importexport.import=Import lima.init.closed=Lima closed at %1$s lima.init.errorclosing= lima.lettering.add= Modified: trunk/lima-swing/src/main/resources/i18n/lima-swing-fr_FR.properties =================================================================== --- trunk/lima-swing/src/main/resources/i18n/lima-swing-fr_FR.properties 2010-07-02 09:46:14 UTC (rev 2960) +++ trunk/lima-swing/src/main/resources/i18n/lima-swing-fr_FR.properties 2010-07-02 10:31:31 UTC (rev 2961) @@ -33,7 +33,7 @@ lima.common.add=Ajout Compte G\u00E9n\u00E9ral lima.common.addSubLedger=Ajouter Compte Tiers lima.common.cancel=Annuler -lima.common.importexport= +lima.common.importexport=Importer/Exporter lima.common.ok=OK lima.common.print= lima.common.quit=Quitter @@ -93,9 +93,8 @@ lima.import.all.csv=Importer une nouvelle base (CSV) lima.import.all.csv.ebp=Importer des donn\u00E9es de EBP lima.import.journal=Importer le journal -lima.importexport.export= -lima.importexport.import= -lima.importexport.update= +lima.importexport.export=Export +lima.importexport.import=Import lima.init.closed=Lima ferm\u00E9 \u00E0 %1$s lima.init.errorclosing= lima.lettering.add=Ajouter une lettre