Author: jpepin Date: 2010-07-01 16:23:40 +0200 (Thu, 01 Jul 2010) New Revision: 2958 Url: http://chorem.org/repositories/revision/lima/2958 Log: D?\195?\169boguage export du PCG, ajout import PCG. Modified: trunk/lima-business/src/main/java/org/chorem/lima/business/AccountService.java trunk/lima-business/src/main/java/org/chorem/lima/business/ImportExportService.java trunk/lima-business/src/main/java/org/chorem/lima/business/accountingrules/FranceAccountingRules.java trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/AccountServiceImpl.java trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/ImportExportServiceImpl.java trunk/lima-callao/src/main/xmi/accounting.zargo trunk/lima-swing/src/main/java/org/chorem/lima/ui/account/AccountTreeTableModel.java trunk/lima-swing/src/main/java/org/chorem/lima/ui/account/AccountViewHandler.java Modified: trunk/lima-business/src/main/java/org/chorem/lima/business/AccountService.java =================================================================== --- trunk/lima-business/src/main/java/org/chorem/lima/business/AccountService.java 2010-07-01 13:16:53 UTC (rev 2957) +++ trunk/lima-business/src/main/java/org/chorem/lima/business/AccountService.java 2010-07-01 14:23:40 UTC (rev 2958) @@ -56,6 +56,7 @@ void updateAccount(Account account) throws LimaException; void removeAccount(Account account) throws LimaException; + void removeAllAccount() throws LimaException; List<Account> getAllChildrenAccounts(Account masterAccount, List<Account> accounts) throws LimaException; 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-01 13:16:53 UTC (rev 2957) +++ trunk/lima-business/src/main/java/org/chorem/lima/business/ImportExportService.java 2010-07-01 14:23:40 UTC (rev 2958) @@ -109,6 +109,8 @@ */ void exportAccountsChartAsCSV(String path) throws LimaException; + String importAccountsChartCSV(String path) throws LimaException; + /** * export entrybook chart as CSV. * Modified: trunk/lima-business/src/main/java/org/chorem/lima/business/accountingrules/FranceAccountingRules.java =================================================================== --- trunk/lima-business/src/main/java/org/chorem/lima/business/accountingrules/FranceAccountingRules.java 2010-07-01 13:16:53 UTC (rev 2957) +++ trunk/lima-business/src/main/java/org/chorem/lima/business/accountingrules/FranceAccountingRules.java 2010-07-01 14:23:40 UTC (rev 2958) @@ -89,6 +89,20 @@ } } + /** + * Rules to check before create subledger + */ + public void createSubLedgerRules(Account masterAccount, Account account) throws LimaException { + super.createSubLedgerRules(masterAccount, account); + + // check if the master account number is begin with 4 + if (!masterAccount.getAccountNumber().startsWith("4")){ + throw new LimaBusinessException( + "Master Account Number is not a thirdpart account" + + account.getAccountNumber()); + } + } + public void updateSubLedgerRules(Account account) throws LimaException { super.updateSubLedgerRules(account); } Modified: trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/AccountServiceImpl.java =================================================================== --- trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/AccountServiceImpl.java 2010-07-01 13:16:53 UTC (rev 2957) +++ trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/AccountServiceImpl.java 2010-07-01 14:23:40 UTC (rev 2958) @@ -108,12 +108,18 @@ account.getAccountNumber())); } + //create it accountDAO.create(account); + + Account masterAccountUpdate = null; + if (masterAccount != null){ + masterAccountUpdate = accountDAO.findByAccountNumber(masterAccount.getAccountNumber()); + } // check if parent account exist; - if (masterAccount != null) { - masterAccount.addSubAccounts(account); - accountDAO.update(masterAccount); + if (masterAccountUpdate != null) { + masterAccountUpdate.addSubAccounts(account); + accountDAO.update(masterAccountUpdate); } commitTransaction(transaction); @@ -151,12 +157,18 @@ account.getAccountNumber())); } + //create it accountDAO.create(account); + Account masterAccountUpdate = null; + if (masterAccount != null){ + masterAccountUpdate = accountDAO.findByAccountNumber(masterAccount.getAccountNumber()); + } + // check if the masteraccount exist; if (masterAccount != null) { - masterAccount.addSubLedgers(account); - accountDAO.update(masterAccount); + masterAccountUpdate.addSubLedgers(account); + accountDAO.update(masterAccountUpdate); } commitTransaction(transaction); @@ -330,6 +342,15 @@ } } + @Override + public void removeAllAccount() throws LimaException{ + List<Account> accounts = getChildrenAccounts(null); + for (Account account : accounts) { + removeAccount(account); + } + } + + /** * Permet de modifier un compte sur son label et son compte père. * 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-01 13:16:53 UTC (rev 2957) +++ trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/ImportExportServiceImpl.java 2010-07-01 14:23:40 UTC (rev 2958) @@ -21,14 +21,22 @@ import java.io.ByteArrayOutputStream; import java.io.File; +import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.OutputStream; +import java.util.Collection; +import java.util.Iterator; import java.util.List; +import java.util.TreeMap; +import javax.ejb.EJB; import javax.ejb.Stateless; import org.apache.commons.io.IOUtils; 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.business.AccountService; import org.chorem.lima.business.ImportExportService; import org.chorem.lima.business.ImportExportServiceLocal; import org.chorem.lima.business.LimaBusinessException; @@ -36,12 +44,13 @@ 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.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; /** @@ -61,6 +70,9 @@ private static final Log log = LogFactory.getLog(ImportExportServiceImpl.class); private TopiaContext rootContext; + + @EJB + AccountService accountService; public ImportExportServiceImpl() { LimaConfig config = LimaConfig.getInstance(); @@ -176,12 +188,17 @@ nextLine[2] = account.getThirdParty(); Account masterAccount = account.getMasterAccount(); Account generalLedger = account.getGeneralLedger(); + String masterAccountString = ""; + String generalLedgerString = ""; if (masterAccount != null){ - nextLine[3] = masterAccount.getAccountNumber(); + masterAccountString = masterAccount.getAccountNumber(); } + nextLine[3] = masterAccountString; if (generalLedger != null){ - nextLine[4] = generalLedger.getAccountNumber(); + generalLedgerString = generalLedger.getAccountNumber(); } + nextLine[4] = generalLedgerString; + // Ajoute la ligne au fichier csvWriter.writeNext(nextLine); } @@ -200,7 +217,104 @@ } } + @Override + public String importAccountsChartCSV(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(); // Date début période + log.debug(nextLine[0]); + if (!nextLine[0].equals("lima.accountschart")){ + throw new LimaBusinessException("The file is not an export csv file type accountschart"); + } + + AccountDAO accountDAO = LimaCallaoDAOHelper.getAccountDAO(topiaContext); + + TreeMap<String, AccountImport> accounts = new TreeMap<String, AccountImport>(); + + while ((nextLine = csvReader.readNext()) != null) { + String accountNumber = nextLine[0]; + String label = nextLine[1]; + String thirdParty = nextLine[2]; + String masterAccountNumber = nextLine[3]; + String generalLedgerNumber = nextLine[4]; + + //if exist, skip + if (accountDAO.findByAccountNumber(accountNumber) != null){ + result += "FAILED : The account " + accountNumber + " already exists !\n"; + } + else { + //create it + AccountImport account = new AccountImportImpl(); + account.setAccountNumber(accountNumber); + account.setLabel(label); + account.setThirdParty(thirdParty); + account.setMasterAccount(masterAccountNumber); + account.setGeneralLedger(generalLedgerNumber); + // put it in hashset + accounts.put(accountNumber, account); + } + } + + while (accounts.size() > 0){ + + log.debug(accounts.size()); + 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){ + log.debug("create it"); + log.debug(accountImport.getAccountNumber()); + //create it + Account account = new AccountImpl(); + account.setAccountNumber(accountImport.getAccountNumber()); + account.setLabel(accountImport.getLabel()); + account.setThirdParty(accountImport.getThirdParty()); + account.setMasterAccount(masterAccount); + account.setGeneralLedger(generalLedger); + + 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); + } + catch (IOException eeeIO) { + log.debug("Can't create new CSV Reader with file"+f+eeeIO); + } + finally { + doFinally(topiaContext, log); + } + return result; + } + + + @Override public void exportEntryBookChartAsCSV(byte[] data) throws LimaException { // TODO Auto-generated method stub Modified: trunk/lima-callao/src/main/xmi/accounting.zargo =================================================================== (Binary files differ) Modified: trunk/lima-swing/src/main/java/org/chorem/lima/ui/account/AccountTreeTableModel.java =================================================================== --- trunk/lima-swing/src/main/java/org/chorem/lima/ui/account/AccountTreeTableModel.java 2010-07-01 13:16:53 UTC (rev 2957) +++ trunk/lima-swing/src/main/java/org/chorem/lima/ui/account/AccountTreeTableModel.java 2010-07-01 14:23:40 UTC (rev 2958) @@ -235,7 +235,16 @@ } modelSupport.fireTreeStructureChanged(path); } + + /** + * Refresh accountschart. + * + */ + public void refreshTree() throws LimaException { + modelSupport.fireNewRoot(); + } + /** * Remove account. * 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-01 13:16:53 UTC (rev 2957) +++ trunk/lima-swing/src/main/java/org/chorem/lima/ui/account/AccountViewHandler.java 2010-07-01 14:23:40 UTC (rev 2958) @@ -24,6 +24,7 @@ import javax.swing.tree.TreePath; 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.LimaBusinessException; import org.chorem.lima.business.LimaException; @@ -54,12 +55,14 @@ protected AccountView view; + protected AccountService accountService; protected ImportExportService importExportService; protected AccountViewHandler(AccountView view) { this.view = view; // Gets factory service importExportService = LimaServiceFactory.getInstance().getImportExportService(); + accountService = LimaServiceFactory.getInstance().getAccountService(); } /** @@ -246,6 +249,11 @@ }; public void importexportAccount(){ + + JXTreeTable accountsTreeTable = view.getAccountsTreeTable(); + AccountTreeTableModel accountsTreeTableModel = + (AccountTreeTableModel)accountsTreeTable.getTreeTableModel(); + ImportExportForm importExportForm = new ImportExportForm(view); importExportForm.setLocationRelativeTo(view); importExportForm.setVisible(true); @@ -258,24 +266,43 @@ FileChooseView fileChooseView = new FileChooseView(view); JFileChooser chooser = fileChooseView.getChooser(); - if (mode.equals("import") || mode.equals("update")){ - chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); - } - else { - chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); - if (chooser.showOpenDialog(view) == JFileChooser.APPROVE_OPTION) { - String filePath = chooser.getSelectedFile().getAbsolutePath(); - log.debug(filePath); - try { - importExportService.exportAccountsChartAsCSV(filePath); - } catch (LimaException eee) { - if (log.isErrorEnabled()){ - log.error("Can't export this file", eee); - } - DialogHelper.showMessageDialog(eee.getMessage()); - } - } - } + + try { + if (mode.equals("import")){ + chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); + if (chooser.showOpenDialog(view) == JFileChooser.APPROVE_OPTION) { + String filePath = chooser.getSelectedFile().getAbsolutePath(); + log.debug(filePath); + accountService.removeAllAccount(); + String message = importExportService.importAccountsChartCSV(filePath); + DialogHelper.showMessageDialog(message); + } + + } + else if (mode.equals("update")){ + chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); + if (chooser.showOpenDialog(view) == JFileChooser.APPROVE_OPTION) { + String filePath = chooser.getSelectedFile().getAbsolutePath(); + log.debug(filePath); + String message = importExportService.importAccountsChartCSV(filePath); + accountsTreeTableModel.refreshTree(); + DialogHelper.showMessageDialog(message); + } + } + else { + chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); + if (chooser.showOpenDialog(view) == JFileChooser.APPROVE_OPTION) { + String filePath = chooser.getSelectedFile().getAbsolutePath(); + log.debug(filePath); + importExportService.exportAccountsChartAsCSV(filePath); + } + } + } catch (LimaException eee) { + if (log.isErrorEnabled()){ + log.error("Can't "+ mode +" this file", eee); + } + DialogHelper.showMessageDialog(eee.getMessage()); + } }