Author: jpepin Date: 2010-07-05 19:07:51 +0200 (Mon, 05 Jul 2010) New Revision: 2963 Url: http://chorem.org/repositories/revision/lima/2963 Log: Split importexportservice (trop de m?\195?\169thodes) : en exportservice, et importservice + refactor. Added: trunk/lima-business/src/main/java/org/chorem/lima/business/ExportService.java trunk/lima-business/src/main/java/org/chorem/lima/business/ExportServiceLocal.java trunk/lima-business/src/main/java/org/chorem/lima/business/ImportService.java trunk/lima-business/src/main/java/org/chorem/lima/business/ImportServiceLocal.java trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/ExportServiceImpl.java trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/ImportServiceImpl.java Removed: trunk/lima-business/src/main/java/org/chorem/lima/business/ImportExportService.java trunk/lima-business/src/main/java/org/chorem/lima/business/ImportExportServiceLocal.java trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/ImportExportServiceImpl.java Modified: trunk/lima-callao/src/main/xmi/accounting.zargo trunk/lima-swing/src/main/java/org/chorem/lima/service/LimaServiceFactory.java trunk/lima-swing/src/main/java/org/chorem/lima/ui/account/AccountViewHandler.java trunk/lima-swing/src/main/java/org/chorem/lima/util/ImportExport.java Added: trunk/lima-business/src/main/java/org/chorem/lima/business/ExportService.java =================================================================== --- trunk/lima-business/src/main/java/org/chorem/lima/business/ExportService.java (rev 0) +++ trunk/lima-business/src/main/java/org/chorem/lima/business/ExportService.java 2010-07-05 17:07:51 UTC (rev 2963) @@ -0,0 +1,63 @@ +/* *##% Lima Business + * Copyright (C) 2008 - 2010 CodeLutin + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * ##%* + */ + +package org.chorem.lima.business; + +import javax.ejb.Remote; + +/** + * Import export service. + * + * Currently import and export as XML. + * + * @author chatellier + * @version $Revision: 2962 $ + * + * Last update : $Date: 2010-07-05 16:59:28 +0200 (lun., 05 juil. 2010) $ + * By : $Author: jpepin $ + */ +@Remote +public interface ExportService { + + + /** + * Get database export as CSV; + * + * @return export as byte array + * @throws LimaException + */ + public String exportAsCSV() throws LimaException; + + /** + * export entrybook chart as CSV. + */ + public String exportEntryBookChartAsCSV() throws LimaException; + + /** + * export financialstatement chart as CSV. + */ + public String exportFinancialStatementChartAsCSV() throws LimaException; + + /** + * export accounts chart as CSV. + */ + public String exportAccountsChartAsCSV() throws LimaException; + + +} Added: trunk/lima-business/src/main/java/org/chorem/lima/business/ExportServiceLocal.java =================================================================== --- trunk/lima-business/src/main/java/org/chorem/lima/business/ExportServiceLocal.java (rev 0) +++ trunk/lima-business/src/main/java/org/chorem/lima/business/ExportServiceLocal.java 2010-07-05 17:07:51 UTC (rev 2963) @@ -0,0 +1,38 @@ +/* *##% Lima Business + * Copyright (C) 2008 - 2010 CodeLutin + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * ##%* + */ + +package org.chorem.lima.business; + +import javax.ejb.Local; + +/** + * Import export service. + * + * Currently import and export as XML. + * + * @author chatellier + * @version $Revision: 2824 $ + * + * Last update : $Date: 2010-04-02 18:55:19 +0200 (ven. 02 avril 2010) $ + * By : $Author: echatellier $ + */ +@Local +public interface ExportServiceLocal extends ExportService { + +} Deleted: 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-05 14:59:28 UTC (rev 2962) +++ trunk/lima-business/src/main/java/org/chorem/lima/business/ImportExportService.java 2010-07-05 17:07:51 UTC (rev 2963) @@ -1,103 +0,0 @@ -/* *##% Lima Business - * Copyright (C) 2008 - 2010 CodeLutin - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - * ##%* - */ - -package org.chorem.lima.business; - -import javax.ejb.Remote; - -/** - * Import export service. - * - * Currently import and export as XML. - * - * @author chatellier - * @version $Revision$ - * - * Last update : $Date$ - * By : $Author$ - */ -@Remote -public interface ImportExportService { - - //########### INPORT - - /** - * Import data as EBP CSV export. - * - * @param data - * @throws LimaException - */ - public void importAsEbpCSV(byte[] data) throws LimaException; - - /** - * Import data as Sage Maestria export. - * - * @param data - * @throws LimaException - */ - public void importAsSage(byte[] data) throws LimaException; - - /** - * Import data as Ciel Compta export. - * - * @param data - * @throws LimaException - */ - public void importAsCiel(byte[] data) throws LimaException; - - /** - * import CSV. - * Return result log - */ - public String importCSV(String path) throws LimaException; - - public String importAccountsChartAsCSV(String path) throws LimaException; - - public String importFinancialStatementsChartAsCSV(String path) throws LimaException; - - public String importEntryBooksChartAsCSV(String path) throws LimaException; - - - //########### EXPORT - - /** - * Get database export as CSV; - * - * @return export as byte array - * @throws LimaException - */ - public String exportAsCSV() throws LimaException; - - /** - * export entrybook chart as CSV. - */ - public String exportEntryBookChartAsCSV() throws LimaException; - - /** - * export financialstatement chart as CSV. - */ - public String exportFinancialStatementChartAsCSV() throws LimaException; - - /** - * export accounts chart as CSV. - */ - public String exportAccountsChartAsCSV() throws LimaException; - - -} Deleted: trunk/lima-business/src/main/java/org/chorem/lima/business/ImportExportServiceLocal.java =================================================================== --- trunk/lima-business/src/main/java/org/chorem/lima/business/ImportExportServiceLocal.java 2010-07-05 14:59:28 UTC (rev 2962) +++ trunk/lima-business/src/main/java/org/chorem/lima/business/ImportExportServiceLocal.java 2010-07-05 17:07:51 UTC (rev 2963) @@ -1,38 +0,0 @@ -/* *##% Lima Business - * Copyright (C) 2008 - 2010 CodeLutin - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - * ##%* - */ - -package org.chorem.lima.business; - -import javax.ejb.Local; - -/** - * Import export service. - * - * Currently import and export as XML. - * - * @author chatellier - * @version $Revision: 2824 $ - * - * Last update : $Date: 2010-04-02 18:55:19 +0200 (ven. 02 avril 2010) $ - * By : $Author: echatellier $ - */ -@Local -public interface ImportExportServiceLocal extends ImportExportService { - -} Copied: trunk/lima-business/src/main/java/org/chorem/lima/business/ImportService.java (from rev 2962, trunk/lima-business/src/main/java/org/chorem/lima/business/ImportExportService.java) =================================================================== --- trunk/lima-business/src/main/java/org/chorem/lima/business/ImportService.java (rev 0) +++ trunk/lima-business/src/main/java/org/chorem/lima/business/ImportService.java 2010-07-05 17:07:51 UTC (rev 2963) @@ -0,0 +1,73 @@ +/* *##% Lima Business + * Copyright (C) 2008 - 2010 CodeLutin + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * ##%* + */ + +package org.chorem.lima.business; + +import javax.ejb.Remote; + +/** + * Import export service. + * + * Currently import and export as XML. + * + * @author chatellier + * @version $Revision$ + * + * Last update : $Date$ + * By : $Author$ + */ +@Remote +public interface ImportService { + + /** + * Import data as EBP CSV export. + * + * @param data + * @throws LimaException + */ + public void importAsEbpCSV(byte[] data) throws LimaException; + + /** + * Import data as Sage Maestria export. + * + * @param data + * @throws LimaException + */ + public void importAsSage(byte[] data) throws LimaException; + + /** + * Import data as Ciel Compta export. + * + * @param data + * @throws LimaException + */ + public void importAsCiel(byte[] data) throws LimaException; + + /** + * import CSV. + * Return result log + */ + public String importCSV(String path) throws LimaException; + + public String importAccountsChartAsCSV(String path) throws LimaException; + + public String importFinancialStatementsChartAsCSV(String path) throws LimaException; + + public String importEntryBooksChartAsCSV(String path) throws LimaException; +} Copied: trunk/lima-business/src/main/java/org/chorem/lima/business/ImportServiceLocal.java (from rev 2958, trunk/lima-business/src/main/java/org/chorem/lima/business/ImportExportServiceLocal.java) =================================================================== --- trunk/lima-business/src/main/java/org/chorem/lima/business/ImportServiceLocal.java (rev 0) +++ trunk/lima-business/src/main/java/org/chorem/lima/business/ImportServiceLocal.java 2010-07-05 17:07:51 UTC (rev 2963) @@ -0,0 +1,38 @@ +/* *##% Lima Business + * Copyright (C) 2008 - 2010 CodeLutin + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * ##%* + */ + +package org.chorem.lima.business; + +import javax.ejb.Local; + +/** + * Import export service. + * + * Currently import and export as XML. + * + * @author chatellier + * @version $Revision: 2824 $ + * + * Last update : $Date: 2010-04-02 18:55:19 +0200 (ven. 02 avril 2010) $ + * By : $Author: echatellier $ + */ +@Local +public interface ImportServiceLocal extends ImportService { + +} Added: trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/ExportServiceImpl.java =================================================================== --- trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/ExportServiceImpl.java (rev 0) +++ trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/ExportServiceImpl.java 2010-07-05 17:07:51 UTC (rev 2963) @@ -0,0 +1,493 @@ +/* *##% Lima Business + * Copyright (C) 2008 - 2010 CodeLutin + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * ##%* + */ + +package org.chorem.lima.business.ejb; + +import java.io.IOException; +import java.io.StringWriter; +import java.text.SimpleDateFormat; +import java.util.List; +import javax.ejb.Stateless; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.chorem.lima.business.ExportService; +import org.chorem.lima.business.ExportServiceLocal; +import org.chorem.lima.business.LimaConfig; +import org.chorem.lima.business.LimaException; +import org.chorem.lima.entity.Account; +import org.chorem.lima.entity.AccountDAO; +import org.chorem.lima.entity.ClosedPeriodicEntryBook; +import org.chorem.lima.entity.ClosedPeriodicEntryBookDAO; +import org.chorem.lima.entity.Entry; +import org.chorem.lima.entity.EntryBook; +import org.chorem.lima.entity.EntryBookDAO; +import org.chorem.lima.entity.EntryDAO; +import org.chorem.lima.entity.FinancialPeriod; +import org.chorem.lima.entity.FinancialPeriodDAO; +import org.chorem.lima.entity.FinancialStatement; +import org.chorem.lima.entity.FinancialStatementDAO; +import org.chorem.lima.entity.FinancialTransaction; +import org.chorem.lima.entity.FinancialTransactionDAO; +import org.chorem.lima.entity.FiscalPeriod; +import org.chorem.lima.entity.FiscalPeriodDAO; +import org.chorem.lima.entity.LimaCallaoDAOHelper; +import org.nuiton.topia.TopiaContext; +import org.nuiton.topia.TopiaContextFactory; +import org.nuiton.topia.TopiaException; +import org.nuiton.topia.TopiaNotFoundException; +import au.com.bytecode.opencsv.CSVWriter; + +/** + * CSV import export service. + * + * @version $Revision: 2962 $ + * + * Last update : $Date: 2010-07-05 16:59:28 +0200 (lun., 05 juil. 2010) $ + * By : $Author: jpepin $ + */ +@Stateless +public class ExportServiceImpl extends AbstractLimaService implements ExportService, ExportServiceLocal { + + private static final Log log = + LogFactory.getLog(ExportServiceImpl.class); + + private TopiaContext rootContext; + + protected CSVWriter csvWriter; + + protected SimpleDateFormat sdf; + + public ExportServiceImpl() { + LimaConfig config = LimaConfig.getInstance(); + try { + rootContext = TopiaContextFactory.getContext(config.getOptions()); + } catch (TopiaNotFoundException ex) { + if (log.isErrorEnabled()) { + log.error("Can't init topia context", ex); + } + } + sdf = new SimpleDateFormat("dd,MM,yyyy HH:mm:ss"); + } + + + /** + * Export integrality of database in CSV + */ + @Override + public String exportAsCSV() throws LimaException { + TopiaContext topiaContext = null; + StringWriter out = new StringWriter(); + + try { + topiaContext = beginTransaction(); + csvWriter = new CSVWriter(out, ';'); + exportAccountsChartAsCSV(topiaContext); + exportEntryBookChartAsCSV(topiaContext); + exportFinancialStatementChartAsCSV(topiaContext); + exportFiscalPeriodAsCSV(topiaContext); + exportFinancialPeriodAsCSV(topiaContext); + exportClosedPeriodicEntryBooksAsCSV(topiaContext); + exportFinancialTransactionsAsCSV(topiaContext); + exportEntriesAsCSV(topiaContext); + // 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",eeeIO); + } + finally { + doFinally(topiaContext, log); + } + return out.getBuffer().toString(); + } + + /** + * Remote methode call from UI. + */ + @Override + public String exportFinancialStatementChartAsCSV() throws LimaException { + TopiaContext topiaContext = null; + StringWriter out = new StringWriter(); + + try { + topiaContext = beginTransaction(); + csvWriter = new CSVWriter(out, ';'); + exportFinancialStatementChartAsCSV(topiaContext); + // 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",eeeIO); + } + finally { + doFinally(topiaContext, log); + } + return out.getBuffer().toString(); + } + + /** + * Local methode, export financialstatements from database + * structure : TYPE | Label | Header | Accounts | DebitAccounts + * | CreditAccounts | ProvisionDeprecationAccounts | SubAmount + * | HeaderAmount | MasterFinancialStatement + */ + public void exportFinancialStatementChartAsCSV(TopiaContext topiaContext) throws LimaException { + try { + String[] nextLine = new String[10]; + // Get all Financialstatements + FinancialStatementDAO financialStatementDAO = + LimaCallaoDAOHelper.getFinancialStatementDAO(topiaContext); + List<FinancialStatement> listFinancialStatements = + financialStatementDAO.findAll(); + // For all Financialstatements + for (FinancialStatement financialStatement : listFinancialStatements) { + nextLine[0] = "FNST"; + nextLine[1] = financialStatement.getLabel(); + nextLine[2] = new Boolean( + financialStatement.getHeader()).toString(); + nextLine[3] = financialStatement.getAccounts(); + nextLine[4] = financialStatement.getDebitAccounts(); + nextLine[5] = financialStatement.getCreditAccounts(); + nextLine[6] = financialStatement. + getProvisionDeprecationAccounts(); + nextLine[7] = new Boolean( + financialStatement.getSubAmount()).toString(); + nextLine[8] = new Boolean( + financialStatement.getHeaderAmount()).toString(); + FinancialStatement masterFinancialStatement = + financialStatement.getMasterFinancialStatement(); + String masterFinancialStatementString = ""; + if (masterFinancialStatement != null){ + masterFinancialStatementString = + masterFinancialStatement.getLabel(); + } + nextLine[9] = masterFinancialStatementString; + // Add line in file + csvWriter.writeNext(nextLine); + } + } + catch (TopiaException eeeTE){ + doCatch(topiaContext, eeeTE, log); + } + } + + /** + * Remote methode call from UI. + */ + @Override + public String exportEntryBookChartAsCSV() throws LimaException { + TopiaContext topiaContext = null; + StringWriter out = new StringWriter(); + + try { + topiaContext = beginTransaction(); + csvWriter = new CSVWriter(out, ';'); + exportEntryBookChartAsCSV(topiaContext); + // 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",eeeIO); + } + finally { + doFinally(topiaContext, log); + } + return out.getBuffer().toString(); + } + + + /** + * Local methode, export entrybooks from database + * Structure : TYPE | Code | Label | Type + */ + public void exportEntryBookChartAsCSV(TopiaContext topiaContext) throws LimaException { + try { + String[] nextLine = new String[4]; + // Get all entrybook + EntryBookDAO entryBookDAO = + LimaCallaoDAOHelper.getEntryBookDAO(topiaContext); + List<EntryBook> listEntryBook = entryBookDAO.findAll(); + // For all EntryBook + for (EntryBook entryBook : listEntryBook) { + nextLine[0] = "ENBK"; + nextLine[1] = entryBook.getCode(); + nextLine[2] = entryBook.getLabel(); + nextLine[3] = entryBook.getType(); + // Ajoute la ligne au fichier + csvWriter.writeNext(nextLine); + } + } + catch (TopiaException eeeTE){ + doCatch(topiaContext, eeeTE, log); + } + } + + /** + * Remote methode call from UI. + */ + @Override + public String exportAccountsChartAsCSV() throws LimaException { + TopiaContext topiaContext = null; + StringWriter out = new StringWriter(); + try { + topiaContext = beginTransaction(); + csvWriter = new CSVWriter(out, ';'); + exportAccountsChartAsCSV(topiaContext); + // 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",eeeIO); + } + finally { + doFinally(topiaContext, log); + } + return out.getBuffer().toString(); + } + + /** + * Local methode, export accounts from database + * Structure : TYPE | AccountNumber | Label | ThirdParty | MasteAccount | GeneralLedger + */ + public void exportAccountsChartAsCSV(TopiaContext topiaContext) throws LimaException { + try { + String[] nextLine = new String[6]; + // Récupère tous les comptes + AccountDAO accountDAO = + LimaCallaoDAOHelper.getAccountDAO(topiaContext); + List<Account> listAccount = accountDAO.findAll(); + // Pour tous les comptes + for (Account account : listAccount) { + nextLine[0] = "ACCN"; + nextLine[1] = account.getAccountNumber(); + nextLine[2] = account.getLabel(); + nextLine[3] = account.getThirdParty(); + Account masterAccount = account.getMasterAccount(); + Account generalLedger = account.getGeneralLedger(); + String masterAccountString = ""; + String generalLedgerString = ""; + if (masterAccount != null){ + masterAccountString = masterAccount.getAccountNumber(); + } + nextLine[4] = masterAccountString; + if (generalLedger != null){ + generalLedgerString = generalLedger.getAccountNumber(); + } + nextLine[5] = generalLedgerString; + + // Ajoute la ligne au fichier + csvWriter.writeNext(nextLine); + } + } + catch (TopiaException eeeTE){ + doCatch(topiaContext, eeeTE, log); + } + } + + /** + * Local methode, export financialtransactions from database + * Structue : TYPE | TransactionDate | AmountDebit | AmountCredit + * | FinancialPeriod BeginDate | FinancialPeriod EndDate | EntryBook Code + */ + public void exportFinancialTransactionsAsCSV(TopiaContext topiaContext) throws LimaException { + String[] nextLine = new String[7]; + // Get all financialtransactions + try { + FinancialTransactionDAO financialTransactionDAO = LimaCallaoDAOHelper.getFinancialTransactionDAO(topiaContext); + List<FinancialTransaction> listFinancialTransaction = financialTransactionDAO.findAll(); + // For all financialTransaction + for (FinancialTransaction financialTransaction : listFinancialTransaction) { + nextLine[0] = "FTRC"; + nextLine[1] = sdf.format(financialTransaction.getTransactionDate()); + nextLine[2] = new Double( + financialTransaction.getAmountDebit()).toString(); + nextLine[3] = new Double( + financialTransaction.getAmountCredit()).toString(); + nextLine[4] = sdf.format(financialTransaction.getFinancialPeriod(). + getBeginDate()); + nextLine[5] = sdf.format(financialTransaction.getFinancialPeriod(). + getEndDate()); + nextLine[6] = financialTransaction.getEntryBook().getCode(); + // Ajoute la ligne au fichier + csvWriter.writeNext(nextLine); + } + } + catch (TopiaException eeeTE){ + doCatch(topiaContext, eeeTE, log); + } + } + + /** + * Local methode, export entries from database + * Structure : TYPE | Description | Amount | Debit | Lettering | Detail | + * Voucher | Position | FinancialTransaction Date | FinancialTransaction Code | + * FinancialTransaction AmountDebit | FinancialTransaction AmountCredit + */ + public void exportEntriesAsCSV(TopiaContext topiaContext) throws LimaException { + try { + String[] nextLine = new String[12]; + // Get all Entry + EntryDAO entryDAO = LimaCallaoDAOHelper.getEntryDAO(topiaContext); + List<Entry> listEntry = entryDAO.findAll(); + // For all Entry + for (Entry entry : listEntry) { + nextLine[0] = "NTRY"; + nextLine[1] = entry.getDescription(); + nextLine[2] = new Double(entry.getAmount()).toString(); + nextLine[3] = new Boolean(entry.getDebit()).toString(); + nextLine[4] = entry.getLettering(); + nextLine[5] = entry.getVoucher(); + nextLine[6] = entry.getPosition(); + nextLine[7] = entry.getAccount().getAccountNumber(); + nextLine[8] = sdf.format(entry.getFinancialTransaction(). + getTransactionDate()); + nextLine[9] = entry.getFinancialTransaction(). + getEntryBook().getCode(); + nextLine[10] = new Double(entry.getFinancialTransaction() + .getAmountDebit()).toString(); + nextLine[11] = new Double(entry.getFinancialTransaction() + .getAmountCredit()).toString(); + + // Ajoute la ligne au fichier + csvWriter.writeNext(nextLine); + } + } + catch (TopiaException eeeTE){ + doCatch(topiaContext, eeeTE, log); + } + } + + /** + * Local methode, export fiscalperiods from database + * Structure : TYPE | BeginDate | EndDate | Locked + */ + public void exportFiscalPeriodAsCSV(TopiaContext topiaContext) throws LimaException { + try { + String[] nextLine = new String[5]; + // Get all fiscalperiod + FiscalPeriodDAO fiscalPeriodDAO = + LimaCallaoDAOHelper.getFiscalPeriodDAO(topiaContext); + List<FiscalPeriod> listFiscalPeriod = + fiscalPeriodDAO.findAll(); + // For all Entry + for (FiscalPeriod fiscalPeriod : listFiscalPeriod) { + nextLine[0] = "FSCP"; + nextLine[1] = sdf.format(fiscalPeriod.getBeginDate()); + nextLine[2] = sdf.format(fiscalPeriod.getEndDate()); + nextLine[3] = new Boolean(fiscalPeriod.getLocked()).toString(); + // Ajoute la ligne au fichier + csvWriter.writeNext(nextLine); + } + } + catch (TopiaException eeeTE){ + doCatch(topiaContext, eeeTE, log); + } + } + + /** + * Local methode, export financialperiods from database + * Structure : TYPE | BeginDate | EndDate | Locked | FiscalPeriod beginDate | FiscalPeriod endDate + */ + public void exportFinancialPeriodAsCSV(TopiaContext topiaContext) throws LimaException { + try { + String[] nextLine = new String[6]; + // Get all fiscalperiod + FinancialPeriodDAO financialPeriodDAO = + LimaCallaoDAOHelper.getFinancialPeriodDAO(topiaContext); + List<FinancialPeriod> listFinancialPeriod = + financialPeriodDAO.findAll(); + + FiscalPeriodDAO fiscalPeriodDAO = + LimaCallaoDAOHelper.getFiscalPeriodDAO(topiaContext); + + // For all Entry + for (FinancialPeriod financialPeriod : listFinancialPeriod) { + nextLine[0] = "FNCP"; + nextLine[1] = sdf.format(financialPeriod.getBeginDate()); + nextLine[2] = sdf.format(financialPeriod.getEndDate()); + nextLine[3] = new Boolean(financialPeriod.getLocked()).toString(); + FiscalPeriod fiscalPeriod = fiscalPeriodDAO. + findContainsFinancialPeriod(financialPeriod); + nextLine[4] = sdf.format(fiscalPeriod.getBeginDate()); + nextLine[5] = sdf.format(fiscalPeriod.getEndDate()); + // Ajoute la ligne au fichier + csvWriter.writeNext(nextLine); + } + } + catch (TopiaException eeeTE){ + doCatch(topiaContext, eeeTE, log); + } + } + + /** + * Local methode, export ClosedPeriodicEntryBooks from database + * Structure : TYPE | Locked | FinancialPeriod beginDate | FinancialPeriod endDate | EntryBook Code + */ + public void exportClosedPeriodicEntryBooksAsCSV(TopiaContext topiaContext) throws LimaException { + try { + String[] nextLine = new String[5]; + // Get all fiscalperiod + ClosedPeriodicEntryBookDAO closedPeriodicEntryBookDAO = + LimaCallaoDAOHelper.getClosedPeriodicEntryBookDAO(topiaContext); + List<ClosedPeriodicEntryBook> listClosedPeriodicEntryBook = + closedPeriodicEntryBookDAO.findAll(); + // For all Entry + for (ClosedPeriodicEntryBook closedPeriodicEntryBook : listClosedPeriodicEntryBook) { + nextLine[0] = "CPEB"; + nextLine[1] = new Boolean( + closedPeriodicEntryBook.getLocked()).toString(); + nextLine[2] = sdf.format(closedPeriodicEntryBook.getFinancialPeriod(). + getBeginDate()); + nextLine[3] =sdf.format(closedPeriodicEntryBook.getFinancialPeriod(). + getEndDate()); + nextLine[4] = closedPeriodicEntryBook.getEntryBook().getCode(); + // Ajoute la ligne au fichier + csvWriter.writeNext(nextLine); + } + } + catch (TopiaException eeeTE){ + doCatch(topiaContext, eeeTE, log); + } + } + + protected TopiaContext beginTransaction() throws TopiaException { + // basic check done, make check in database + // TODO move it into JTA + TopiaContext topiaTransaction; + topiaTransaction = rootContext.beginTransaction(); + log.trace("beginTransaction"+topiaTransaction); + return topiaTransaction; + } +} Deleted: 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-05 14:59:28 UTC (rev 2962) +++ trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/ImportExportServiceImpl.java 2010-07-05 17:07:51 UTC (rev 2963) @@ -1,947 +0,0 @@ -/* *##% Lima Business - * Copyright (C) 2008 - 2010 CodeLutin - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - * ##%* - */ - -package org.chorem.lima.business.ejb; - -import java.io.IOException; -import java.io.StringReader; -import java.io.StringWriter; -import java.util.Iterator; -import java.util.List; -import java.util.TreeMap; -import javax.ejb.EJB; -import javax.ejb.Stateless; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.chorem.lima.beans.AccountImport; -import org.chorem.lima.beans.AccountImportImpl; -import org.chorem.lima.beans.FinancialStatementImport; -import org.chorem.lima.beans.FinancialStatementImportImpl; -import org.chorem.lima.business.AccountServiceLocal; -import org.chorem.lima.business.EntryBookServiceLocal; -import org.chorem.lima.business.FinancialStatementServiceLocal; -import org.chorem.lima.business.FinancialTransactionServiceLocal; -import org.chorem.lima.business.ImportExportService; -import org.chorem.lima.business.ImportExportServiceLocal; -import org.chorem.lima.business.LimaConfig; -import org.chorem.lima.business.LimaException; -import org.chorem.lima.entity.Account; -import org.chorem.lima.entity.AccountDAO; -import org.chorem.lima.entity.AccountImpl; -import org.chorem.lima.entity.ClosedPeriodicEntryBook; -import org.chorem.lima.entity.ClosedPeriodicEntryBookDAO; -import org.chorem.lima.entity.Entry; -import org.chorem.lima.entity.EntryBook; -import org.chorem.lima.entity.EntryBookDAO; -import org.chorem.lima.entity.EntryBookImpl; -import org.chorem.lima.entity.EntryDAO; -import org.chorem.lima.entity.FinancialPeriod; -import org.chorem.lima.entity.FinancialPeriodDAO; -import org.chorem.lima.entity.FinancialStatement; -import org.chorem.lima.entity.FinancialStatementDAO; -import org.chorem.lima.entity.FinancialStatementImpl; -import org.chorem.lima.entity.FinancialTransaction; -import org.chorem.lima.entity.FiscalPeriod; -import org.chorem.lima.entity.FiscalPeriodDAO; -import org.chorem.lima.entity.LimaCallaoDAOHelper; -import org.nuiton.topia.TopiaContext; -import org.nuiton.topia.TopiaContextFactory; -import org.nuiton.topia.TopiaException; -import org.nuiton.topia.TopiaNotFoundException; -import au.com.bytecode.opencsv.CSVReader; -import au.com.bytecode.opencsv.CSVWriter; - -/** - * CSV import export service. - * - * @version $Revision$ - * - * Last update : $Date$ - * By : $Author$ - */ -@Stateless -public class ImportExportServiceImpl extends AbstractLimaService implements ImportExportService, ImportExportServiceLocal { - - private static final Log log = - LogFactory.getLog(ImportExportServiceImpl.class); - - private TopiaContext rootContext; - - @EJB - AccountServiceLocal accountService; - - @EJB - FinancialTransactionServiceLocal financialTransactionService; - - @EJB - FinancialStatementServiceLocal financialStatementService; - - @EJB - EntryBookServiceLocal entryBookService; - - protected CSVWriter csvWriter; - - public ImportExportServiceImpl() { - LimaConfig config = LimaConfig.getInstance(); - try { - rootContext = TopiaContextFactory.getContext(config.getOptions()); - } catch (TopiaNotFoundException ex) { - if (log.isErrorEnabled()) { - log.error("Can't init topia context", ex); - } - } - } - - //################ IMPORT THIRD PART ACCOUNTING SOFTWARE ################ - - - /* - * @see org.chorem.lima.business.ImportExportService#importAsEBPCSV(byte[]) - */ - @Override - public void importAsEbpCSV(byte[] data) throws LimaException { - - } - - @Override - public void importAsCiel(byte[] data) throws LimaException { - // TODO Auto-generated method stub - - } - - @Override - public void importAsSage(byte[] data) throws LimaException { - // TODO Auto-generated method stub - - } - - - //################ EXPORT ################ - - /** - * Export integrality of database in CSV - */ - @Override - public String exportAsCSV() throws LimaException { - TopiaContext topiaContext = null; - StringWriter out = new StringWriter(); - - try { - topiaContext = beginTransaction(); - csvWriter = new CSVWriter(out, ';'); - exportAccountsChartAsCSV(topiaContext); - exportEntryBookChartAsCSV(topiaContext); - exportFinancialStatementChartAsCSV(topiaContext); - exportFiscalPeriodAsCSV(topiaContext); - exportFinancialPeriodAsCSV(topiaContext); - exportClosedPeriodicEntryBooksAsCSV(topiaContext); - exportFinancialTransactionsAsCSV(topiaContext); - exportEntriesAsCSV(topiaContext); - // 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",eeeIO); - } - finally { - doFinally(topiaContext, log); - } - return out.getBuffer().toString(); - } - - /** - * Remote methode call from UI. - */ - @Override - public String exportFinancialStatementChartAsCSV() throws LimaException { - TopiaContext topiaContext = null; - StringWriter out = new StringWriter(); - - try { - topiaContext = beginTransaction(); - csvWriter = new CSVWriter(out, ';'); - exportFinancialStatementChartAsCSV(topiaContext); - // 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",eeeIO); - } - finally { - doFinally(topiaContext, log); - } - return out.getBuffer().toString(); - } - - /** - * Local methode, export financialstatements from database - * structure : TYPE | Label | Header | Accounts | DebitAccounts - * | CreditAccounts | ProvisionDeprecationAccounts | SubAmount - * | HeaderAmount | MasterFinancialStatement - */ - public void exportFinancialStatementChartAsCSV(TopiaContext topiaContext) throws LimaException { - try { - String[] nextLine = new String[10]; - // Get all Financialstatements - FinancialStatementDAO financialStatementDAO = - LimaCallaoDAOHelper.getFinancialStatementDAO(topiaContext); - List<FinancialStatement> listFinancialStatements = - financialStatementDAO.findAll(); - // For all Financialstatements - for (FinancialStatement financialStatement : listFinancialStatements) { - nextLine[0] = "FNST"; - nextLine[1] = financialStatement.getLabel(); - nextLine[2] = new Boolean( - financialStatement.getHeader()).toString(); - nextLine[3] = financialStatement.getAccounts(); - nextLine[4] = financialStatement.getDebitAccounts(); - nextLine[5] = financialStatement.getCreditAccounts(); - nextLine[6] = financialStatement. - getProvisionDeprecationAccounts(); - nextLine[7] = new Boolean( - financialStatement.getSubAmount()).toString(); - nextLine[8] = new Boolean( - financialStatement.getHeaderAmount()).toString(); - FinancialStatement masterFinancialStatement = - financialStatement.getMasterFinancialStatement(); - String masterFinancialStatementString = ""; - if (masterFinancialStatement != null){ - masterFinancialStatementString = - masterFinancialStatement.getLabel(); - } - nextLine[9] = masterFinancialStatementString; - // Add line in file - csvWriter.writeNext(nextLine); - } - } - catch (TopiaException eeeTE){ - doCatch(topiaContext, eeeTE, log); - } - } - - /** - * Remote methode call from UI. - */ - @Override - public String exportEntryBookChartAsCSV() throws LimaException { - TopiaContext topiaContext = null; - StringWriter out = new StringWriter(); - - try { - topiaContext = beginTransaction(); - csvWriter = new CSVWriter(out, ';'); - exportEntryBookChartAsCSV(topiaContext); - // 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",eeeIO); - } - finally { - doFinally(topiaContext, log); - } - return out.getBuffer().toString(); - } - - - /** - * Local methode, export entrybooks from database - * Structure : TYPE | Code | Label | Type - */ - public void exportEntryBookChartAsCSV(TopiaContext topiaContext) throws LimaException { - try { - String[] nextLine = new String[4]; - // Get all entrybook - EntryBookDAO entryBookDAO = - LimaCallaoDAOHelper.getEntryBookDAO(topiaContext); - List<EntryBook> listEntryBook = entryBookDAO.findAll(); - // For all EntryBook - for (EntryBook entryBook : listEntryBook) { - nextLine[0] = "ENBK"; - nextLine[1] = entryBook.getCode(); - nextLine[2] = entryBook.getLabel(); - nextLine[3] = entryBook.getType(); - // Ajoute la ligne au fichier - csvWriter.writeNext(nextLine); - } - } - catch (TopiaException eeeTE){ - doCatch(topiaContext, eeeTE, log); - } - } - - /** - * Remote methode call from UI. - */ - @Override - public String exportAccountsChartAsCSV() throws LimaException { - TopiaContext topiaContext = null; - StringWriter out = new StringWriter(); - try { - topiaContext = beginTransaction(); - csvWriter = new CSVWriter(out, ';'); - exportAccountsChartAsCSV(topiaContext); - // 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",eeeIO); - } - finally { - doFinally(topiaContext, log); - } - return out.getBuffer().toString(); - } - - /** - * Local methode, export accounts from database - * Structure : TYPE | AccountNumber | Label | ThirdParty | MasteAccount | GeneralLedger - */ - public void exportAccountsChartAsCSV(TopiaContext topiaContext) throws LimaException { - try { - String[] nextLine = new String[6]; - // Récupère tous les comptes - AccountDAO accountDAO = - LimaCallaoDAOHelper.getAccountDAO(topiaContext); - List<Account> listAccount = accountDAO.findAll(); - // Pour tous les comptes - for (Account account : listAccount) { - nextLine[0] = "ACCN"; - nextLine[1] = account.getAccountNumber(); - nextLine[2] = account.getLabel(); - nextLine[3] = account.getThirdParty(); - Account masterAccount = account.getMasterAccount(); - Account generalLedger = account.getGeneralLedger(); - String masterAccountString = ""; - String generalLedgerString = ""; - if (masterAccount != null){ - masterAccountString = masterAccount.getAccountNumber(); - } - nextLine[4] = masterAccountString; - if (generalLedger != null){ - generalLedgerString = generalLedger.getAccountNumber(); - } - nextLine[5] = generalLedgerString; - - // Ajoute la ligne au fichier - csvWriter.writeNext(nextLine); - } - } - catch (TopiaException eeeTE){ - doCatch(topiaContext, eeeTE, log); - } - } - - /** - * Local methode, export financialtransactions from database - * Structue : TYPE | TransactionDate | AmountDebit | AmountCredit - * | FinancialPeriod BeginDate | FinancialPeriod EndDate | EntryBook Code - */ - public void exportFinancialTransactionsAsCSV(TopiaContext topiaContext) throws LimaException { - String[] nextLine = new String[7]; - // Get all financialtransactions balanced - List<FinancialTransaction> listFinancialTransaction = - financialTransactionService.getAllFinancialTransactionsBalanced(topiaContext); - // For all balanced financialTransaction - for (FinancialTransaction financialTransaction : listFinancialTransaction) { - nextLine[0] = "FTRC"; - nextLine[1] = financialTransaction.getTransactionDate().toString(); - nextLine[2] = new Double( - financialTransaction.getAmountDebit()).toString(); - nextLine[3] = new Double( - financialTransaction.getAmountCredit()).toString(); - nextLine[4] = financialTransaction.getFinancialPeriod(). - getBeginDate().toString(); - nextLine[5] = financialTransaction.getFinancialPeriod(). - getEndDate().toString(); - nextLine[6] = financialTransaction.getEntryBook().getCode(); - // Ajoute la ligne au fichier - csvWriter.writeNext(nextLine); - } - } - - /** - * Local methode, export entries from database - * Structure : TYPE | Description | Amount | Debit | Lettering | Detail | - * Voucher | Position | FinancialTransaction Date | FinancialTransaction Code | - * FinancialTransaction AmountDebit | FinancialTransaction AmountCredit - */ - public void exportEntriesAsCSV(TopiaContext topiaContext) throws LimaException { - try { - String[] nextLine = new String[12]; - // Get all Entry - EntryDAO entryDAO = LimaCallaoDAOHelper.getEntryDAO(topiaContext); - List<Entry> listEntry = entryDAO.findAll(); - // For all Entry - for (Entry entry : listEntry) { - nextLine[0] = "NTRY"; - nextLine[1] = entry.getDescription(); - nextLine[2] = new Double(entry.getAmount()).toString(); - nextLine[3] = new Boolean(entry.getDebit()).toString(); - nextLine[4] = entry.getLettering(); - nextLine[5] = entry.getVoucher(); - nextLine[6] = entry.getPosition(); - nextLine[7] = entry.getAccount().getAccountNumber(); - nextLine[8] = entry.getFinancialTransaction(). - getTransactionDate().toString(); - nextLine[9] = entry.getFinancialTransaction(). - getEntryBook().getCode(); - nextLine[10] = new Double(entry.getFinancialTransaction() - .getAmountDebit()).toString(); - nextLine[11] = new Double(entry.getFinancialTransaction() - .getAmountCredit()).toString(); - - // Ajoute la ligne au fichier - csvWriter.writeNext(nextLine); - } - } - catch (TopiaException eeeTE){ - doCatch(topiaContext, eeeTE, log); - } - } - - /** - * Local methode, export fiscalperiods from database - * Structure : TYPE | BeginDate | EndDate | Locked - */ - public void exportFiscalPeriodAsCSV(TopiaContext topiaContext) throws LimaException { - try { - String[] nextLine = new String[5]; - // Get all fiscalperiod - FiscalPeriodDAO fiscalPeriodDAO = - LimaCallaoDAOHelper.getFiscalPeriodDAO(topiaContext); - List<FiscalPeriod> listFiscalPeriod = - fiscalPeriodDAO.findAll(); - // For all Entry - for (FiscalPeriod fiscalPeriod : listFiscalPeriod) { - nextLine[0] = "FSCP"; - nextLine[1] = fiscalPeriod.getBeginDate().toString(); - nextLine[2] = fiscalPeriod.getEndDate().toString(); - nextLine[3] = new Boolean(fiscalPeriod.getLocked()).toString(); - // Ajoute la ligne au fichier - csvWriter.writeNext(nextLine); - } - } - catch (TopiaException eeeTE){ - doCatch(topiaContext, eeeTE, log); - } - } - - /** - * Local methode, export financialperiods from database - * Structure : TYPE | BeginDate | EndDate | Locked | FiscalPeriod beginDate | FiscalPeriod endDate - */ - public void exportFinancialPeriodAsCSV(TopiaContext topiaContext) throws LimaException { - try { - String[] nextLine = new String[6]; - // Get all fiscalperiod - FinancialPeriodDAO financialPeriodDAO = - LimaCallaoDAOHelper.getFinancialPeriodDAO(topiaContext); - List<FinancialPeriod> listFinancialPeriod = - financialPeriodDAO.findAll(); - - FiscalPeriodDAO fiscalPeriodDAO = - LimaCallaoDAOHelper.getFiscalPeriodDAO(topiaContext); - - // For all Entry - for (FinancialPeriod financialPeriod : listFinancialPeriod) { - nextLine[0] = "FNCP"; - nextLine[1] = financialPeriod.getBeginDate().toString(); - nextLine[2] = financialPeriod.getEndDate().toString(); - nextLine[3] = new Boolean(financialPeriod.getLocked()).toString(); - FiscalPeriod fiscalPeriod = fiscalPeriodDAO. - findContainsFinancialPeriod(financialPeriod); - nextLine[4] = fiscalPeriod.getBeginDate().toString(); - nextLine[5] = fiscalPeriod.getEndDate().toString(); - // Ajoute la ligne au fichier - csvWriter.writeNext(nextLine); - } - } - catch (TopiaException eeeTE){ - doCatch(topiaContext, eeeTE, log); - } - } - - /** - * Local methode, export ClosedPeriodicEntryBooks from database - * Structure : TYPE | Locked | FinancialPeriod beginDate | FinancialPeriod endDate | EntryBook Code - */ - public void exportClosedPeriodicEntryBooksAsCSV(TopiaContext topiaContext) throws LimaException { - try { - String[] nextLine = new String[5]; - // Get all fiscalperiod - ClosedPeriodicEntryBookDAO closedPeriodicEntryBookDAO = - LimaCallaoDAOHelper.getClosedPeriodicEntryBookDAO(topiaContext); - List<ClosedPeriodicEntryBook> listClosedPeriodicEntryBook = - closedPeriodicEntryBookDAO.findAll(); - // For all Entry - for (ClosedPeriodicEntryBook closedPeriodicEntryBook : listClosedPeriodicEntryBook) { - nextLine[0] = "CPEB"; - nextLine[1] = new Boolean( - closedPeriodicEntryBook.getLocked()).toString(); - nextLine[2] = closedPeriodicEntryBook.getFinancialPeriod(). - getBeginDate().toString(); - nextLine[3] =closedPeriodicEntryBook.getFinancialPeriod(). - getEndDate().toString(); - nextLine[4] = closedPeriodicEntryBook.getEntryBook().getCode(); - // Ajoute la ligne au fichier - csvWriter.writeNext(nextLine); - } - } - catch (TopiaException eeeTE){ - doCatch(topiaContext, eeeTE, log); - } - } - - //################ IMPORT ################ - - - @Override - public String importCSV(String datas) throws LimaException { - String result = ""; - - //Accounts - TreeMap<String, AccountImport> accounts = - new TreeMap<String, AccountImport>(); - - //FinancialStatements - TreeMap<String, FinancialStatementImport> financialStatements = - new TreeMap<String, FinancialStatementImport>(); - - TopiaContext topiaContext = null; - try { - topiaContext = beginTransaction(); - - String[] nextLine = new String[1]; - CSVReader csvReader = new CSVReader(new StringReader(datas), ';'); - - - while ((nextLine = csvReader.readNext()) != null) { - String indice = nextLine[0]; - if (indice.equals("ACCN")){ - accounts = importAccountsChartsCSV( - nextLine, accounts, topiaContext); - } - else if (indice.equals("ENBK")){ - result+=importEntryBooksChartCSV( - nextLine, topiaContext); - } - else if (indice.equals("FNST")){ - financialStatements = importFinancialsStatementChartCSV( - nextLine, financialStatements, topiaContext); - } - else if (indice.equals("FSCP")){ - //result+= - } - else if (indice.equals("FNCP")){ - //result+= - } - else if (indice.equals("CPEB")){ - //result+= - } - else if (indice.equals("FTRC")){ - //result+= - } - else if (indice.equals("NTRY")){ - //result+= - } - } - - //create accounts - result+=createAccounts(accounts, topiaContext); - //create financialStatements - result+=createFinancialStatements(financialStatements, topiaContext); - } - catch (TopiaException eeeTE){ - doCatch(topiaContext, eeeTE, log); - } - catch (IOException eeeIO) { - log.debug("Can't create new CSV Reader",eeeIO); - } - finally { - doFinally(topiaContext, log); - } - return result; - } - - @Override - public String importEntryBooksChartAsCSV(String datas) throws LimaException{ - String result = ""; - - TopiaContext topiaContext = null; - try { - topiaContext = beginTransaction(); - - String[] nextLine = new String[1]; - CSVReader csvReader = new CSVReader(new StringReader(datas), ';'); - - while ((nextLine = csvReader.readNext()) != null) { - String indice = nextLine[0]; - if (indice.equals("ENBK")){ - result+=importEntryBooksChartCSV(nextLine, topiaContext); - } - } - } - catch (TopiaException eeeTE){ - doCatch(topiaContext, eeeTE, log); - } - catch (IOException eeeIO) { - log.debug("Can't create new CSV Reader",eeeIO); - } - finally { - doFinally(topiaContext, log); - } - return result; - } - - - @Override - public String importFinancialStatementsChartAsCSV(String datas) throws LimaException { - String result = ""; - - //FinancialStatements - TreeMap<String, FinancialStatementImport> financialStatements = - new TreeMap<String, FinancialStatementImport>(); - - TopiaContext topiaContext = null; - try { - topiaContext = beginTransaction(); - - String[] nextLine = new String[1]; - CSVReader csvReader = new CSVReader(new StringReader(datas), ';'); - - while ((nextLine = csvReader.readNext()) != null) { - String indice = nextLine[0]; - if (indice.equals("FNST")){ - financialStatements = importFinancialsStatementChartCSV( - nextLine, financialStatements, topiaContext); - } - } - - //create financialStatements - result+=createFinancialStatements(financialStatements, topiaContext); - } - catch (TopiaException eeeTE){ - doCatch(topiaContext, eeeTE, log); - } - catch (IOException eeeIO) { - log.debug("Can't create new CSV Reader",eeeIO); - } - finally { - doFinally(topiaContext, log); - } - return result; - } - - - @Override - public String importAccountsChartAsCSV(String datas) throws LimaException { - String result = ""; - - //Accounts - TreeMap<String, AccountImport> accounts = - new TreeMap<String, AccountImport>(); - - TopiaContext topiaContext = null; - try { - topiaContext = beginTransaction(); - - String[] nextLine = new String[1]; - CSVReader csvReader = new CSVReader(new StringReader(datas), ';'); - - - while ((nextLine = csvReader.readNext()) != null) { - String indice = nextLine[0]; - if (indice.equals("ACCN")){ - accounts = importAccountsChartsCSV( - nextLine, accounts, topiaContext); - } - } - - //create accounts - result+=createAccounts(accounts, topiaContext); - } - catch (TopiaException eeeTE){ - doCatch(topiaContext, eeeTE, log); - } - catch (IOException eeeIO) { - log.debug("Can't create new CSV Reader",eeeIO); - } - finally { - doFinally(topiaContext, log); - } - return result; - } - - public TreeMap<String, AccountImport> importAccountsChartsCSV(String[] nextLine, TreeMap<String, AccountImport> accounts, TopiaContext topiaContext) throws LimaException{ - - String accountNumber = nextLine[1]; - String label = nextLine[2]; - String thirdParty = nextLine[3]; - String masterAccountNumber = nextLine[4]; - String generalLedgerNumber = nextLine[5]; - - try { - AccountDAO accountDAO = - LimaCallaoDAOHelper.getAccountDAO(topiaContext); - - //if not exist, create it - if (accountDAO.findByAccountNumber(accountNumber) == null){ - //create it - AccountImport accountImport = new AccountImportImpl(); - accountImport.setAccountNumber(accountNumber); - accountImport.setLabel(label); - accountImport.setThirdParty(thirdParty); - accountImport.setMasterAccount(masterAccountNumber); - accountImport.setGeneralLedger(generalLedgerNumber); - // put it in hashset - accounts.put(accountNumber, accountImport); - } - /*else { - result += "FAILED : The account " - + accountNumber + " already exists !\n"; - }*/ - } - catch (TopiaException eeeTE){ - doCatch(topiaContext, eeeTE, log); - } - - return accounts; - } - - - public String createAccounts(TreeMap<String, AccountImport> accounts, TopiaContext topiaContext) throws LimaException { - String result = ""; - - try { - AccountDAO accountDAO = LimaCallaoDAOHelper.getAccountDAO(topiaContext); - - while (accounts.size() > 0){ - for (Iterator<AccountImport> itr = accounts.values().iterator(); itr.hasNext();){ - AccountImport accountImport = itr.next(); - String masterAccountNumber = accountImport.getMasterAccount(); - String generalLedgerNumber = accountImport.getGeneralLedger(); - Account masterAccount = - accountDAO.findByAccountNumber(masterAccountNumber); - Account generalLedger = - accountDAO.findByAccountNumber(generalLedgerNumber); - - if ((masterAccountNumber.equals("") && generalLedgerNumber.equals("")) - || masterAccount != null || generalLedger != null){ - //create it - Account account = new AccountImpl(); - account.setAccountNumber(accountImport.getAccountNumber()); - account.setLabel(accountImport.getLabel()); - account.setThirdParty(accountImport.getThirdParty()); - - if (generalLedger != null){ - accountService.createSubLedger(generalLedger, account); - } - else { - accountService.createAccount(masterAccount, account); - } - result += "SUCCESS : The account " + - accountImport.getAccountNumber() + " is created ! \n"; - itr.remove(); - } - else if (!accounts.containsKey(masterAccountNumber) - && !accounts.containsKey(generalLedgerNumber) ){ - result += "FAILED : The account " + - accountImport.getAccountNumber() + " have masterAccount : " + - masterAccountNumber + "which not exist \n"; - itr.remove(); - } - } - } - } - catch (TopiaException eeeTE){ - doCatch(topiaContext, eeeTE, log); - } - return result; - } - - - public String importEntryBooksChartCSV(String[] nextLine, TopiaContext topiaContext) throws LimaException { - String result =""; - try { - - EntryBookDAO entryBookDAO = - LimaCallaoDAOHelper.getEntryBookDAO(topiaContext); - - EntryBook entryBook = new EntryBookImpl(); - entryBook.setCode(nextLine[1]); - entryBook.setLabel(nextLine[2]); - entryBook.setType(nextLine[3]); - - //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); - } - return result; - } - - - public TreeMap<String, FinancialStatementImport> importFinancialsStatementChartCSV(String[] nextLine, TreeMap<String, FinancialStatementImport> financialStatements, TopiaContext topiaContext) throws LimaException { - - String label = nextLine[1]; - String header = nextLine[2]; - String accounts = nextLine[3]; - String debitAccounts = nextLine[4]; - String creditAccounts = nextLine[5]; - String provisionDeprecationAccounts = nextLine[6]; - String subAmount = nextLine[7]; - String headerAmount = nextLine[8]; - String masterFinancialStatement = nextLine[9]; - - try { - - FinancialStatementDAO financialStatementDAO = - LimaCallaoDAOHelper.getFinancialStatementDAO(topiaContext); - - //if exist, skip - if (financialStatementDAO.findByLabel(label) == null){ - //create it - FinancialStatementImport financialStatementImport = - new FinancialStatementImportImpl(); - financialStatementImport.setLabel(label); - financialStatementImport.setHeader(header); - financialStatementImport.setAccounts(accounts); - financialStatementImport.setCreditAccounts(creditAccounts); - financialStatementImport.setDebitAccounts(debitAccounts); - financialStatementImport.setHeaderAmount(headerAmount); - financialStatementImport.setProvisionDeprecationAccounts( - provisionDeprecationAccounts); - financialStatementImport.setSubAmount(subAmount); - financialStatementImport.setMasterFinancialStatement( - masterFinancialStatement); - - // put it in hashset - financialStatements.put(label, financialStatementImport); - } - /*else { - result += "FAILED : The financialstatement " - + label + " already exists !\n"; - }*/ - } - catch (TopiaException eeeTE){ - doCatch(topiaContext, eeeTE, log); - } - return financialStatements; - } - - - public String createFinancialStatements(TreeMap<String, FinancialStatementImport> financialStatements, TopiaContext topiaContext) throws LimaException { - String result = ""; - try { - - FinancialStatementDAO financialStatementDAO = - LimaCallaoDAOHelper.getFinancialStatementDAO(topiaContext); - - while (financialStatements.size() > 0){ - for (Iterator<FinancialStatementImport> itr = financialStatements.values().iterator(); itr.hasNext();){ - FinancialStatementImport financialStatementImport = itr.next(); - String masterFinancialStatementLabel = - financialStatementImport.getMasterFinancialStatement(); - FinancialStatement masterFinancialStatement = - financialStatementDAO.findByLabel( - masterFinancialStatementLabel); - - if (masterFinancialStatementLabel.equals("") - || masterFinancialStatement != null){ - //create it - FinancialStatement financialStatement = - new FinancialStatementImpl(); - financialStatement.setLabel( - financialStatementImport.getLabel()); - financialStatement.setHeader(Boolean.parseBoolean( - financialStatementImport.getHeader())); - financialStatement.setAccounts( - financialStatementImport.getAccounts()); - financialStatement.setDebitAccounts( - financialStatementImport.getDebitAccounts()); - financialStatement.setCreditAccounts( - financialStatementImport.getCreditAccounts()); - financialStatement.setProvisionDeprecationAccounts( - financialStatementImport. - getProvisionDeprecationAccounts()); - financialStatement.setSubAmount(Boolean.parseBoolean( - financialStatementImport.getSubAmount())); - financialStatement.setHeaderAmount(Boolean.parseBoolean( - financialStatementImport.getHeaderAmount())); - - financialStatementService.createFinancialStatement( - masterFinancialStatement, financialStatement); - - result += "SUCCESS : The financialStatement " + - financialStatementImport.getLabel() + " is created ! \n"; - itr.remove(); - } - else if (!financialStatements.containsKey(masterFinancialStatementLabel)){ - result += "FAILED : The financialStatement " + - financialStatementImport.getLabel() + - " have masterFinancialStatement : " + - masterFinancialStatementLabel + "which not exist \n"; - itr.remove(); - } - } - } - } - catch (TopiaException eeeTE){ - doCatch(topiaContext, eeeTE, log); - } - return result; - } - - - protected TopiaContext beginTransaction() throws TopiaException { - // basic check done, make check in database - // TODO move it into JTA - TopiaContext topiaTransaction; - topiaTransaction = rootContext.beginTransaction(); - log.trace("beginTransaction"+topiaTransaction); - return topiaTransaction; - } -} Copied: trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/ImportServiceImpl.java (from rev 2962, trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/ImportExportServiceImpl.java) =================================================================== --- trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/ImportServiceImpl.java (rev 0) +++ trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/ImportServiceImpl.java 2010-07-05 17:07:51 UTC (rev 2963) @@ -0,0 +1,692 @@ +/* *##% Lima Business + * Copyright (C) 2008 - 2010 CodeLutin + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * ##%* + */ + +package org.chorem.lima.business.ejb; + +import java.io.IOException; +import java.io.StringReader; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.Iterator; +import java.util.List; +import java.util.TreeMap; +import javax.ejb.EJB; +import javax.ejb.Stateless; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.chorem.lima.beans.AccountImport; +import org.chorem.lima.beans.AccountImportImpl; +import org.chorem.lima.beans.FinancialPeriodImport; +import org.chorem.lima.beans.FinancialPeriodImportImpl; +import org.chorem.lima.beans.FinancialStatementImport; +import org.chorem.lima.beans.FinancialStatementImportImpl; +import org.chorem.lima.business.AccountServiceLocal; +import org.chorem.lima.business.EntryBookServiceLocal; +import org.chorem.lima.business.FinancialStatementServiceLocal; +import org.chorem.lima.business.FinancialTransactionServiceLocal; +import org.chorem.lima.business.ImportService; +import org.chorem.lima.business.ImportServiceLocal; +import org.chorem.lima.business.LimaConfig; +import org.chorem.lima.business.LimaException; +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.FinancialPeriod; +import org.chorem.lima.entity.FinancialPeriodDAO; +import org.chorem.lima.entity.FinancialPeriodImpl; +import org.chorem.lima.entity.FinancialStatement; +import org.chorem.lima.entity.FinancialStatementDAO; +import org.chorem.lima.entity.FinancialStatementImpl; +import org.chorem.lima.entity.FiscalPeriod; +import org.chorem.lima.entity.FiscalPeriodDAO; +import org.chorem.lima.entity.FiscalPeriodImpl; +import org.chorem.lima.entity.LimaCallaoDAOHelper; +import org.nuiton.topia.TopiaContext; +import org.nuiton.topia.TopiaContextFactory; +import org.nuiton.topia.TopiaException; +import org.nuiton.topia.TopiaNotFoundException; +import au.com.bytecode.opencsv.CSVReader; +import au.com.bytecode.opencsv.CSVWriter; + +/** + * CSV import export service. + * + * @version $Revision$ + * + * Last update : $Date$ + * By : $Author$ + */ +@Stateless +public class ImportServiceImpl extends AbstractLimaService implements ImportService, ImportServiceLocal { + + private static final Log log = + LogFactory.getLog(ImportServiceImpl.class); + + private TopiaContext rootContext; + + @EJB + AccountServiceLocal accountService; + + @EJB + FinancialTransactionServiceLocal financialTransactionService; + + @EJB + FinancialStatementServiceLocal financialStatementService; + + @EJB + EntryBookServiceLocal entryBookService; + + protected CSVWriter csvWriter; + + protected SimpleDateFormat sdf; + + public ImportServiceImpl() { + LimaConfig config = LimaConfig.getInstance(); + try { + rootContext = TopiaContextFactory.getContext(config.getOptions()); + } catch (TopiaNotFoundException ex) { + if (log.isErrorEnabled()) { + log.error("Can't init topia context", ex); + } + } + sdf = new SimpleDateFormat("dd,MM,yyyy HH:mm:ss"); + } + + //################ IMPORT THIRD PART ACCOUNTING SOFTWARE ################ + + + /* + * @see org.chorem.lima.business.ImportExportService#importAsEBPCSV(byte[]) + */ + @Override + public void importAsEbpCSV(byte[] data) throws LimaException { + + } + + @Override + public void importAsCiel(byte[] data) throws LimaException { + // TODO Auto-generated method stub + + } + + @Override + public void importAsSage(byte[] data) throws LimaException { + // TODO Auto-generated method stub + + } + + + //################ IMPORT ################ + + + @Override + public String importCSV(String datas) throws LimaException { + String result = ""; + + //Accounts + TreeMap<String, AccountImport> accounts = + new TreeMap<String, AccountImport>(); + + //FinancialStatements + TreeMap<String, FinancialStatementImport> financialStatements = + new TreeMap<String, FinancialStatementImport>(); + + //FiscalPeriods + List<FiscalPeriod> fiscalPeriods = new ArrayList<FiscalPeriod>(); + + //FinancialPeriods + List<FinancialPeriodImport> financialPeriodImports = + new ArrayList<FinancialPeriodImport>(); + + + TopiaContext topiaContext = null; + try { + topiaContext = beginTransaction(); + + String[] nextLine = new String[1]; + CSVReader csvReader = new CSVReader(new StringReader(datas), ';'); + + + while ((nextLine = csvReader.readNext()) != null) { + String indice = nextLine[0]; + if (indice.equals("ACCN")){ + accounts = importAccountsChartsCSV( + nextLine, accounts, topiaContext); + } + else if (indice.equals("ENBK")){ + result += importEntryBooksChartCSV( + nextLine, topiaContext); + } + else if (indice.equals("FNST")){ + financialStatements = importFinancialsStatementChartCSV( + nextLine, financialStatements, topiaContext); + } + else if (indice.equals("FSCP")){ + fiscalPeriods = importFiscalPeriodCSV(nextLine, fiscalPeriods, topiaContext); + } + else if (indice.equals("FNCP")){ + financialPeriodImports = importFinancialPeriodChartCSV(nextLine, financialPeriodImports, topiaContext); + } + else if (indice.equals("CPEB")){ + //result+= + } + else if (indice.equals("FTRC")){ + //result+= + } + else if (indice.equals("NTRY")){ + //result+= + } + } + + //create accounts + result+=createAccounts(accounts, topiaContext); + //create financialStatements + result+=createFinancialStatements(financialStatements, topiaContext); + //create fiscalperiod + + //create financialperiod + result+=createFinancialPeriod(financialPeriodImports, topiaContext); + } + catch (TopiaException eeeTE){ + doCatch(topiaContext, eeeTE, log); + } + catch (IOException eeeIO) { + log.debug("Can't create new CSV Reader",eeeIO); + } + finally { + doFinally(topiaContext, log); + } + return result; + } + + @Override + public String importEntryBooksChartAsCSV(String datas) throws LimaException{ + String result = ""; + + TopiaContext topiaContext = null; + try { + topiaContext = beginTransaction(); + + String[] nextLine = new String[1]; + CSVReader csvReader = new CSVReader(new StringReader(datas), ';'); + + while ((nextLine = csvReader.readNext()) != null) { + String indice = nextLine[0]; + if (indice.equals("ENBK")){ + result+=importEntryBooksChartCSV(nextLine, topiaContext); + } + } + } + catch (TopiaException eeeTE){ + doCatch(topiaContext, eeeTE, log); + } + catch (IOException eeeIO) { + log.debug("Can't create new CSV Reader",eeeIO); + } + finally { + doFinally(topiaContext, log); + } + return result; + } + + + @Override + public String importFinancialStatementsChartAsCSV(String datas) throws LimaException { + String result = ""; + + //FinancialStatements + TreeMap<String, FinancialStatementImport> financialStatements = + new TreeMap<String, FinancialStatementImport>(); + + TopiaContext topiaContext = null; + try { + topiaContext = beginTransaction(); + + String[] nextLine = new String[1]; + CSVReader csvReader = new CSVReader(new StringReader(datas), ';'); + + while ((nextLine = csvReader.readNext()) != null) { + String indice = nextLine[0]; + if (indice.equals("FNST")){ + financialStatements = importFinancialsStatementChartCSV( + nextLine, financialStatements, topiaContext); + } + } + + //create financialStatements + result+=createFinancialStatements(financialStatements, topiaContext); + } + catch (TopiaException eeeTE){ + doCatch(topiaContext, eeeTE, log); + } + catch (IOException eeeIO) { + log.debug("Can't create new CSV Reader",eeeIO); + } + finally { + doFinally(topiaContext, log); + } + return result; + } + + + @Override + public String importAccountsChartAsCSV(String datas) throws LimaException { + String result = ""; + + //Accounts + TreeMap<String, AccountImport> accounts = + new TreeMap<String, AccountImport>(); + + TopiaContext topiaContext = null; + try { + topiaContext = beginTransaction(); + + String[] nextLine = new String[1]; + CSVReader csvReader = new CSVReader(new StringReader(datas), ';'); + + + while ((nextLine = csvReader.readNext()) != null) { + String indice = nextLine[0]; + if (indice.equals("ACCN")){ + accounts = importAccountsChartsCSV( + nextLine, accounts, topiaContext); + } + } + + //create accounts + result+=createAccounts(accounts, topiaContext); + } + catch (TopiaException eeeTE){ + doCatch(topiaContext, eeeTE, log); + } + catch (IOException eeeIO) { + log.debug("Can't create new CSV Reader",eeeIO); + } + finally { + doFinally(topiaContext, log); + } + return result; + } + + public TreeMap<String, AccountImport> importAccountsChartsCSV(String[] nextLine, TreeMap<String, AccountImport> accounts, TopiaContext topiaContext) throws LimaException{ + + String accountNumber = nextLine[1]; + String label = nextLine[2]; + String thirdParty = nextLine[3]; + String masterAccountNumber = nextLine[4]; + String generalLedgerNumber = nextLine[5]; + + try { + AccountDAO accountDAO = + LimaCallaoDAOHelper.getAccountDAO(topiaContext); + + //if not exist, create it + if (accountDAO.findByAccountNumber(accountNumber) == null){ + //create it + AccountImport accountImport = new AccountImportImpl(); + accountImport.setAccountNumber(accountNumber); + accountImport.setLabel(label); + accountImport.setThirdParty(thirdParty); + accountImport.setMasterAccount(masterAccountNumber); + accountImport.setGeneralLedger(generalLedgerNumber); + // put it in hashset + accounts.put(accountNumber, accountImport); + } + /*else { + result += "FAILED : The account " + + accountNumber + " already exists !\n"; + }*/ + } + catch (TopiaException eeeTE){ + doCatch(topiaContext, eeeTE, log); + } + + return accounts; + } + + + /** + * Import and create fiscal period + * Structure : TYPE | BeginDate | EndDate | Locked + */ + public List<FiscalPeriod> importFiscalPeriodCSV(String[] nextLine, List<FiscalPeriod> fiscalPeriods, TopiaContext topiaContext) throws LimaException { + try { + FiscalPeriodDAO fiscalPeriodDAO = + LimaCallaoDAOHelper.getFiscalPeriodDAO(topiaContext); + + FiscalPeriod fiscalPeriod = new FiscalPeriodImpl(); + Date beginDate = sdf.parse(nextLine[1]); + fiscalPeriod.setBeginDate(beginDate); + Date endDate = sdf.parse(nextLine[2]); + fiscalPeriod.setEndDate(endDate); + fiscalPeriod.setLocked(new Boolean(nextLine[3])); + + //if not exist, skip + if (fiscalPeriodDAO.findByNaturalId(beginDate, endDate) == null){ + fiscalPeriods.add(fiscalPeriod); + } + } + catch (ParseException eeePE) { + if(log.isDebugEnabled()){ + log.debug("Can't parse date", eeePE); + } + } + catch (TopiaException eeeTE){ + doCatch(topiaContext, eeeTE, log); + } + return fiscalPeriods; + } + + + public String importEntryBooksChartCSV(String[] nextLine, TopiaContext topiaContext) throws LimaException { + String result =""; + try { + + EntryBookDAO entryBookDAO = + LimaCallaoDAOHelper.getEntryBookDAO(topiaContext); + + EntryBook entryBook = new EntryBookImpl(); + entryBook.setCode(nextLine[1]); + entryBook.setLabel(nextLine[2]); + entryBook.setType(nextLine[3]); + + //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); + } + return result; + } + + + public TreeMap<String, FinancialStatementImport> importFinancialsStatementChartCSV(String[] nextLine, TreeMap<String, FinancialStatementImport> financialStatements, TopiaContext topiaContext) throws LimaException { + + String label = nextLine[1]; + String header = nextLine[2]; + String accounts = nextLine[3]; + String debitAccounts = nextLine[4]; + String creditAccounts = nextLine[5]; + String provisionDeprecationAccounts = nextLine[6]; + String subAmount = nextLine[7]; + String headerAmount = nextLine[8]; + String masterFinancialStatement = nextLine[9]; + + try { + + FinancialStatementDAO financialStatementDAO = + LimaCallaoDAOHelper.getFinancialStatementDAO(topiaContext); + + //if exist, skip + if (financialStatementDAO.findByLabel(label) == null){ + //create it + FinancialStatementImport financialStatementImport = + new FinancialStatementImportImpl(); + financialStatementImport.setLabel(label); + financialStatementImport.setHeader(header); + financialStatementImport.setAccounts(accounts); + financialStatementImport.setCreditAccounts(creditAccounts); + financialStatementImport.setDebitAccounts(debitAccounts); + financialStatementImport.setHeaderAmount(headerAmount); + financialStatementImport.setProvisionDeprecationAccounts( + provisionDeprecationAccounts); + financialStatementImport.setSubAmount(subAmount); + financialStatementImport.setMasterFinancialStatement( + masterFinancialStatement); + + // put it in hashset + financialStatements.put(label, financialStatementImport); + } + /*else { + result += "FAILED : The financialstatement " + + label + " already exists !\n"; + }*/ + } + catch (TopiaException eeeTE){ + doCatch(topiaContext, eeeTE, log); + } + return financialStatements; + } + + /** + * Structure : TYPE | BeginDate | EndDate | Locked | FiscalPeriod beginDate | FiscalPeriod endDate + */ + + public List<FinancialPeriodImport> importFinancialPeriodChartCSV(String[] nextLine, List<FinancialPeriodImport> financialPeriodImports, TopiaContext topiaContext) throws LimaException { + + String beginDate = nextLine[1]; + String endDate = nextLine[2]; + String locked = nextLine[3]; + String beginDateFiscalPeriod = nextLine[4]; + String endDateFiscalPeriod = nextLine[5]; + + try { + + FinancialPeriodDAO financialPeriodDAO = + LimaCallaoDAOHelper.getFinancialPeriodDAO(topiaContext); + + //if exist, skip + if (financialPeriodDAO.findByNaturalId(sdf.parse(beginDate), sdf.parse(endDate)) == null){ + //create it + FinancialPeriodImport financialPeriodImport = + new FinancialPeriodImportImpl(); + financialPeriodImport.setBeginDate(beginDate); + financialPeriodImport.setEndDate(endDate); + financialPeriodImport.setBeginDateFiscalPeriod(beginDateFiscalPeriod); + financialPeriodImport.setEndDateFiscalPeriod(endDateFiscalPeriod); + financialPeriodImport.setLocked(locked); + + // put it in list + financialPeriodImports.add(financialPeriodImport); + } + } + catch (ParseException eeePE) { + if(log.isDebugEnabled()){ + log.debug("Can't parse date", eeePE); + } + } + catch (TopiaException eeeTE){ + doCatch(topiaContext, eeeTE, log); + } + + return financialPeriodImports; + } + + + //################ CREATE ENTITY IN DB FOR IMPORT ################ + + + public String createFinancialStatements(TreeMap<String, FinancialStatementImport> financialStatements, TopiaContext topiaContext) throws LimaException { + String result = ""; + try { + + FinancialStatementDAO financialStatementDAO = + LimaCallaoDAOHelper.getFinancialStatementDAO(topiaContext); + + while (financialStatements.size() > 0){ + for (Iterator<FinancialStatementImport> itr = financialStatements.values().iterator(); itr.hasNext();){ + FinancialStatementImport financialStatementImport = itr.next(); + String masterFinancialStatementLabel = + financialStatementImport.getMasterFinancialStatement(); + FinancialStatement masterFinancialStatement = + financialStatementDAO.findByLabel( + masterFinancialStatementLabel); + + if (masterFinancialStatementLabel.equals("") + || masterFinancialStatement != null){ + //create it + FinancialStatement financialStatement = + new FinancialStatementImpl(); + financialStatement.setLabel( + financialStatementImport.getLabel()); + financialStatement.setHeader(Boolean.parseBoolean( + financialStatementImport.getHeader())); + financialStatement.setAccounts( + financialStatementImport.getAccounts()); + financialStatement.setDebitAccounts( + financialStatementImport.getDebitAccounts()); + financialStatement.setCreditAccounts( + financialStatementImport.getCreditAccounts()); + financialStatement.setProvisionDeprecationAccounts( + financialStatementImport. + getProvisionDeprecationAccounts()); + financialStatement.setSubAmount(Boolean.parseBoolean( + financialStatementImport.getSubAmount())); + financialStatement.setHeaderAmount(Boolean.parseBoolean( + financialStatementImport.getHeaderAmount())); + + financialStatementService.createFinancialStatement( + masterFinancialStatement, financialStatement); + + result += "SUCCESS : The financialStatement " + + financialStatementImport.getLabel() + " is created ! \n"; + itr.remove(); + } + else if (!financialStatements.containsKey(masterFinancialStatementLabel)){ + result += "FAILED : The financialStatement " + + financialStatementImport.getLabel() + + " have masterFinancialStatement : " + + masterFinancialStatementLabel + "which not exist \n"; + itr.remove(); + } + } + } + } + catch (TopiaException eeeTE){ + doCatch(topiaContext, eeeTE, log); + } + return result; + } + + public String createFinancialPeriod(List<FinancialPeriodImport> financialPeriodImports, TopiaContext topiaContext) throws LimaException{ + String result = ""; + try { + FiscalPeriodDAO fiscalPeriodDAO = + LimaCallaoDAOHelper.getFiscalPeriodDAO(topiaContext); + FinancialPeriodDAO financialPeriodDAO = + LimaCallaoDAOHelper.getFinancialPeriodDAO(topiaContext); + + for (FinancialPeriodImport financialPeriodImport : financialPeriodImports) { + //create financialPeriod + FinancialPeriod financialPeriod = new FinancialPeriodImpl(); + Date beginDate = sdf.parse(financialPeriodImport.getBeginDate()); + Date endDate = sdf.parse(financialPeriodImport.getEndDate()); + financialPeriod.setBeginDate(beginDate); + financialPeriod.setEndDate(endDate); + financialPeriod.setLocked(new Boolean(financialPeriodImport.getLocked())); + financialPeriodDAO.create(financialPeriod); + + //add to fiscalperiod + Date beginDateFiscalPeriod = + sdf.parse(financialPeriodImport.getBeginDateFiscalPeriod()); + Date endDateFiscalPeriod = + sdf.parse(financialPeriodImport.getEndDateFiscalPeriod()); + FiscalPeriod fiscalPeriod = + fiscalPeriodDAO.findByNaturalId(beginDateFiscalPeriod, endDateFiscalPeriod); + fiscalPeriod.addFinancialPeriod(financialPeriod); + + result += "SUCCESS : The financialPeriod " + + financialPeriodImport.getBeginDate()+"-"+financialPeriodImport.getEndDate() + " is created ! \n"; + } + } + catch (ParseException eeePE) { + if(log.isDebugEnabled()){ + log.debug("Can't parse date", eeePE); + } + } + catch (TopiaException eeeTE){ + doCatch(topiaContext, eeeTE, log); + } + return result; + } + + public String createAccounts(TreeMap<String, AccountImport> accounts, TopiaContext topiaContext) throws LimaException { + String result = ""; + + try { + AccountDAO accountDAO = LimaCallaoDAOHelper.getAccountDAO(topiaContext); + + while (accounts.size() > 0){ + for (Iterator<AccountImport> itr = accounts.values().iterator(); itr.hasNext();){ + AccountImport accountImport = itr.next(); + String masterAccountNumber = accountImport.getMasterAccount(); + String generalLedgerNumber = accountImport.getGeneralLedger(); + Account masterAccount = + accountDAO.findByAccountNumber(masterAccountNumber); + Account generalLedger = + accountDAO.findByAccountNumber(generalLedgerNumber); + + if ((masterAccountNumber.equals("") && generalLedgerNumber.equals("")) + || masterAccount != null || generalLedger != null){ + //create it + Account account = new AccountImpl(); + account.setAccountNumber(accountImport.getAccountNumber()); + account.setLabel(accountImport.getLabel()); + account.setThirdParty(accountImport.getThirdParty()); + + if (generalLedger != null){ + accountService.createSubLedger(generalLedger, account); + } + else { + accountService.createAccount(masterAccount, account); + } + result += "SUCCESS : The account " + + accountImport.getAccountNumber() + " is created ! \n"; + itr.remove(); + } + else if (!accounts.containsKey(masterAccountNumber) + && !accounts.containsKey(generalLedgerNumber) ){ + result += "FAILED : The account " + + accountImport.getAccountNumber() + " have masterAccount : " + + masterAccountNumber + "which not exist \n"; + itr.remove(); + } + } + } + } + catch (TopiaException eeeTE){ + doCatch(topiaContext, eeeTE, log); + } + return result; + } + + + protected TopiaContext beginTransaction() throws TopiaException { + // basic check done, make check in database + // TODO move it into JTA + TopiaContext topiaTransaction; + topiaTransaction = rootContext.beginTransaction(); + log.trace("beginTransaction"+topiaTransaction); + return topiaTransaction; + } +} Modified: trunk/lima-callao/src/main/xmi/accounting.zargo =================================================================== (Binary files differ) Modified: trunk/lima-swing/src/main/java/org/chorem/lima/service/LimaServiceFactory.java =================================================================== --- trunk/lima-swing/src/main/java/org/chorem/lima/service/LimaServiceFactory.java 2010-07-05 14:59:28 UTC (rev 2962) +++ trunk/lima-swing/src/main/java/org/chorem/lima/service/LimaServiceFactory.java 2010-07-05 17:07:51 UTC (rev 2963) @@ -33,10 +33,11 @@ import org.chorem.lima.LimaMain; import org.chorem.lima.business.AccountService; import org.chorem.lima.business.EntryBookService; +import org.chorem.lima.business.ExportService; import org.chorem.lima.business.FinancialPeriodService; import org.chorem.lima.business.FinancialStatementService; import org.chorem.lima.business.FiscalPeriodService; -import org.chorem.lima.business.ImportExportService; +import org.chorem.lima.business.ImportService; import org.chorem.lima.business.RecordService; import org.chorem.lima.business.FinancialTransactionService; import org.chorem.lima.business.ReportService; @@ -275,15 +276,15 @@ } /** - * Get financial statement service. + * Get import service. * */ - public ImportExportService getImportExportService() { + public ImportService getImportService() { - String lookupName = "ImportExportServiceImplRemote"; - ImportExportService ejbHome = null; + String lookupName = "ImportServiceImplRemote"; + ImportService ejbHome = null; try { - ejbHome = (ImportExportService)ctx.lookup(lookupName); + ejbHome = (ImportService)ctx.lookup(lookupName); } catch (NamingException eee) { if (log.isErrorEnabled()) { log.error("Can't lookup for service : " + lookupName, eee); @@ -292,7 +293,26 @@ return ejbHome; } + /** + * Get export service. + * + */ + public ExportService getExportService() { + + String lookupName = "ExportServiceImplRemote"; + ExportService ejbHome = null; + try { + ejbHome = (ExportService)ctx.lookup(lookupName); + } catch (NamingException eee) { + if (log.isErrorEnabled()) { + log.error("Can't lookup for service : " + lookupName, eee); + } + } + return ejbHome; + } + + /** * Get record service. * * @return record service proxy Modified: trunk/lima-swing/src/main/java/org/chorem/lima/ui/account/AccountViewHandler.java =================================================================== --- trunk/lima-swing/src/main/java/org/chorem/lima/ui/account/AccountViewHandler.java 2010-07-05 14:59:28 UTC (rev 2962) +++ trunk/lima-swing/src/main/java/org/chorem/lima/ui/account/AccountViewHandler.java 2010-07-05 17:07:51 UTC (rev 2963) @@ -25,7 +25,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.chorem.lima.business.AccountService; -import org.chorem.lima.business.ImportExportService; +import org.chorem.lima.business.ImportService; import org.chorem.lima.business.LimaBusinessException; import org.chorem.lima.business.LimaException; import org.chorem.lima.entity.Account; Modified: trunk/lima-swing/src/main/java/org/chorem/lima/util/ImportExport.java =================================================================== --- trunk/lima-swing/src/main/java/org/chorem/lima/util/ImportExport.java 2010-07-05 14:59:28 UTC (rev 2962) +++ trunk/lima-swing/src/main/java/org/chorem/lima/util/ImportExport.java 2010-07-05 17:07:51 UTC (rev 2963) @@ -19,15 +19,14 @@ package org.chorem.lima.util; -import java.io.BufferedInputStream; -import java.io.BufferedOutputStream; import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; +import java.io.FileReader; +import java.io.FileWriter; import java.io.IOException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.chorem.lima.business.ImportExportService; +import org.chorem.lima.business.ExportService; +import org.chorem.lima.business.ImportService; import org.chorem.lima.business.LimaException; import org.chorem.lima.service.LimaServiceFactory; import org.chorem.lima.ui.account.AccountViewHandler; @@ -38,10 +37,13 @@ private static final Log log = LogFactory.getLog(AccountViewHandler.class); - protected ImportExportService importExportService; + protected ImportService importService; + + protected ExportService exportService; public ImportExport() { - importExportService = LimaServiceFactory.getInstance().getImportExportService(); + importService = LimaServiceFactory.getInstance().getImportService(); + exportService = LimaServiceFactory.getInstance().getExportService(); } /** @@ -54,21 +56,21 @@ public void exportToCsvFile(String path, String exportMethode) throws LimaException { String result = ""; try { - BufferedOutputStream f = new BufferedOutputStream(new FileOutputStream(path)); + FileWriter f = new FileWriter(new File(path)); if (exportMethode.equals("exportEntryBookChart")){ - result = importExportService.exportEntryBookChartAsCSV(); + result = exportService.exportEntryBookChartAsCSV(); } else if (exportMethode.equals("exportAccountsChart")){ - result = importExportService.exportAccountsChartAsCSV(); + result = exportService.exportAccountsChartAsCSV(); } else if (exportMethode.equals("exportFinancialStatementsChart")){ - result = importExportService.exportFinancialStatementChartAsCSV(); + result = exportService.exportFinancialStatementChartAsCSV(); } else { - result = importExportService.exportAsCSV(); + result = exportService.exportAsCSV(); } - f.write(result.getBytes()); + f.write(result); f.flush(); f.close(); } @@ -93,20 +95,20 @@ String result = ""; byte[] buffer = new byte[(int) new File(path).length()]; try { - BufferedInputStream f = new BufferedInputStream(new FileInputStream(path)); - f.read(buffer); + FileReader f = new FileReader(new File(path)); + f.read(); if (importMethode.equals("importEntryBookChart")){ - result = importExportService.importEntryBooksChartAsCSV(new String(buffer)); + result = importService.importEntryBooksChartAsCSV(new String(buffer)); } else if (importMethode.equals("importAccountsChart")){ - result = importExportService.importAccountsChartAsCSV(new String(buffer)); + result = importService.importAccountsChartAsCSV(new String(buffer)); } else if (importMethode.equals("importFinancialStatementsChart")){ - result = importExportService.importFinancialStatementsChartAsCSV(new String(buffer)); + result = importService.importFinancialStatementsChartAsCSV(new String(buffer)); } else { - result = importExportService.importCSV(new String(buffer)); + result = importService.importCSV(new String(buffer)); } f.close(); }