Author: echatellier Date: 2012-04-25 11:14:11 +0200 (Wed, 25 Apr 2012) New Revision: 3392 Url: http://chorem.org/repositories/revision/lima/3392 Log: Move queries to dao Modified: trunk/lima-business/src/main/java/org/chorem/lima/business/AccountingRules.java trunk/lima-business/src/main/java/org/chorem/lima/business/accountingrules/DefaultAccountingRules.java trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/FinancialPeriodServiceImpl.java trunk/lima-callao/src/main/java/org/chorem/lima/entity/FinancialTransactionDAO.java Modified: trunk/lima-business/src/main/java/org/chorem/lima/business/AccountingRules.java =================================================================== --- trunk/lima-business/src/main/java/org/chorem/lima/business/AccountingRules.java 2012-04-25 08:28:20 UTC (rev 3391) +++ trunk/lima-business/src/main/java/org/chorem/lima/business/AccountingRules.java 2012-04-25 09:14:11 UTC (rev 3392) @@ -95,5 +95,12 @@ */ void checkFinancialPeriodBlockedWithFinancialTransaction(FinancialTransaction financialTransaction, TopiaContext topiaContext) throws LimaException; + /** + * Check if a financial period can be closed. + * + * @param closedPeriodicEntryBook + * @param topiaContext + * @throws LimaException + */ void blockClosedPeriodicEntryBookRules(ClosedPeriodicEntryBook closedPeriodicEntryBook, TopiaContext topiaContext) throws LimaException; } Modified: trunk/lima-business/src/main/java/org/chorem/lima/business/accountingrules/DefaultAccountingRules.java =================================================================== --- trunk/lima-business/src/main/java/org/chorem/lima/business/accountingrules/DefaultAccountingRules.java 2012-04-25 08:28:20 UTC (rev 3391) +++ trunk/lima-business/src/main/java/org/chorem/lima/business/accountingrules/DefaultAccountingRules.java 2012-04-25 09:14:11 UTC (rev 3392) @@ -68,9 +68,6 @@ protected static final Log log = LogFactory.getLog(DefaultAccountingRules.class); - /** DAO helper not injected by EJB. */ - protected LimaDaoHelper daoHelper = new LimaDaoHelper(); - /** * Rules to check before create accounts. */ @@ -117,7 +114,7 @@ @Override public void removeAccountRules(Account account) throws LimaException { try { - EntryDAO entryDAO = daoHelper.getEntryDAO(); + EntryDAO entryDAO = LimaDaoHelper.getEntryDAO(); // Check if account have entries int nbentries = entryDAO.findAllByAccount(account).size(); if (nbentries != 0) { @@ -192,72 +189,33 @@ @Override public void blockClosedPeriodicEntryBookRules(ClosedPeriodicEntryBook closedPeriodicEntryBook, TopiaContext topiaContext) throws LimaException { - List<FinancialTransaction> result = null; - // Check if all financial transactions of closedperiodicentrybook are equilibrate try { FinancialTransactionDAO financialTransactionDAO = LimaCallaoDAOHelper.getFinancialTransactionDAO(topiaContext); - TopiaQuery query = financialTransactionDAO.createQuery("E"); - query.addWhere("E.amountCredit != E.amountDebit") - .addEquals(FinancialTransaction.PROPERTY_ENTRY_BOOK, - closedPeriodicEntryBook.getEntryBook()) - .addEquals(FinancialTransaction.PROPERTY_FINANCIAL_PERIOD, - closedPeriodicEntryBook.getFinancialPeriod()); - result = financialTransactionDAO.findAllByQuery(query); - + // Check if all financial transactions of closedperiodicentrybook are equilibrate + List<FinancialTransaction> result = financialTransactionDAO.getAllUnbalancedTransaction(closedPeriodicEntryBook.getFinancialPeriod(), + closedPeriodicEntryBook.getEntryBook()); if (result.size() > 0) { throw new LimaBusinessException(_("lima-business.defaultaccountingrules.blockerrorequillibrate")); } - } catch (TopiaException ex) { - doCatch(topiaContext, ex, log); - } - result = null; + + // Check if all financial transactions of this closedperiodicentrybook/financialPeriod are well filled in + result = financialTransactionDAO.getAllUnfilledTransaction(closedPeriodicEntryBook.getFinancialPeriod(), + closedPeriodicEntryBook.getEntryBook()); - // Check if all financial transactions of this closedperiodicentrybook/financialPeriod are well filled in - try { - FinancialTransactionDAO financialTransactionDAO = - LimaCallaoDAOHelper.getFinancialTransactionDAO(topiaContext); - - TopiaQuery query = financialTransactionDAO.createQuery("T"); - query.addDistinct() - .addOrder("T." + FinancialTransaction.TOPIA_CREATE_DATE) - .addFrom(FiscalPeriod.class, "F") - .addLeftJoin("T." + FinancialTransaction.PROPERTY_ENTRY, "E", true) - .addWhere("T.amountCredit != T.amountDebit OR E.account = null OR E.voucher = null OR E.voucher = '' OR E.description = null OR E.description = ''") - .addEquals(FinancialTransaction.PROPERTY_ENTRY_BOOK, - closedPeriodicEntryBook.getEntryBook()) - .addEquals(FinancialTransaction.PROPERTY_FINANCIAL_PERIOD, - closedPeriodicEntryBook.getFinancialPeriod()); - result = financialTransactionDAO.findAllByQuery(query); - if (result.size() > 0) { throw new LimaBusinessException(_("lima-business.defaultaccountingrules.missingelements")); } - } catch (TopiaException ex) { - doCatch(topiaContext, ex, log); - } + + // Check if all financial transactions have EntryBooks + result = financialTransactionDAO.getAllTransactionWithoutEntryBook(); - result = null; - - // Check if all financial transactions have EntryBooks - try { - FinancialTransactionDAO financialTransactionDAO = - LimaCallaoDAOHelper.getFinancialTransactionDAO(topiaContext); - - TopiaQuery query = financialTransactionDAO.createQuery("T"); - query.addDistinct() - .addOrder("T." + FinancialTransaction.TOPIA_CREATE_DATE) - .addFrom(FiscalPeriod.class, "F") - .addLeftJoin("T." + FinancialTransaction.PROPERTY_ENTRY, "E", true) - .addWhere("T.amountCredit != T.amountDebit OR E.account = null OR E.voucher = null OR E.voucher = '' OR E.description = null OR E.description = ''") - .addNull(FinancialTransaction.PROPERTY_ENTRY_BOOK); - result = financialTransactionDAO.findAllByQuery(query); - if (result.size() > 0) { throw new LimaBusinessException(_("lima-business.defaultaccountingrules.missingentrybook")); } + } catch (TopiaException ex) { doCatch(topiaContext, ex, log); } Modified: trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/FinancialPeriodServiceImpl.java =================================================================== --- trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/FinancialPeriodServiceImpl.java 2012-04-25 08:28:20 UTC (rev 3391) +++ trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/FinancialPeriodServiceImpl.java 2012-04-25 09:14:11 UTC (rev 3392) @@ -210,19 +210,19 @@ AccountingRules accountingRules = LimaConfig.getInstance().getAccountingRules(); try { - //check rules before create the account + // check rules before create the account accountingRules.blockClosedPeriodicEntryBookRules( closedPeriodicEntryBook, topiaContext); ClosedPeriodicEntryBookDAO closedPeriodicEntryBookDAO = LimaCallaoDAOHelper.getClosedPeriodicEntryBookDAO(topiaContext); - ClosedPeriodicEntryBook closedPeriodicEntryBook2 = - closedPeriodicEntryBookDAO.findByTopiaId( - closedPeriodicEntryBook.getTopiaId()); + //ClosedPeriodicEntryBook closedPeriodicEntryBook2 = + // closedPeriodicEntryBookDAO.findByTopiaId( + // closedPeriodicEntryBook.getTopiaId()); - closedPeriodicEntryBook2.setLocked(true); - closedPeriodicEntryBookDAO.update(closedPeriodicEntryBook2); + closedPeriodicEntryBook.setLocked(true); + closedPeriodicEntryBookDAO.update(closedPeriodicEntryBook); commitTransaction(topiaContext); } catch (TopiaException ex) { Modified: trunk/lima-callao/src/main/java/org/chorem/lima/entity/FinancialTransactionDAO.java =================================================================== --- trunk/lima-callao/src/main/java/org/chorem/lima/entity/FinancialTransactionDAO.java 2012-04-25 08:28:20 UTC (rev 3391) +++ trunk/lima-callao/src/main/java/org/chorem/lima/entity/FinancialTransactionDAO.java 2012-04-25 09:14:11 UTC (rev 3392) @@ -25,7 +25,10 @@ package org.chorem.lima.entity; +import java.util.List; + import org.nuiton.topia.TopiaException; +import org.nuiton.topia.framework.TopiaQuery; /** * Ajout de requetes specifiques aux financial transaction sur le DAO. @@ -51,4 +54,65 @@ return count.longValue(); } + /** + * Find all unbalanced transactions. + * + * @param period period + * @param entryBook entry book + * @return unbalanced transactions + * @throws TopiaException + */ + public List<FinancialTransaction> getAllUnbalancedTransaction(FinancialPeriod period, + EntryBook entryBook) throws TopiaException { + TopiaQuery query = createQuery("E"); + query.addWhere("E.amountCredit != E.amountDebit") + .addEquals(FinancialTransaction.PROPERTY_ENTRY_BOOK, entryBook) + .addEquals(FinancialTransaction.PROPERTY_FINANCIAL_PERIOD, period); + List<FinancialTransaction> result = findAllByQuery(query); + return result; + } + + /** + * Find all transaction where some field are not filled in. + * + * @param period period + * @param entryBook entry book + * @return unfilled transaction + * @throws TopiaException + */ + public List<FinancialTransaction> getAllUnfilledTransaction(FinancialPeriod period, + EntryBook entryBook) throws TopiaException { + TopiaQuery query = createQuery("T"); + query.addDistinct() + .addOrder("T." + FinancialTransaction.TOPIA_CREATE_DATE) + .addFrom(FiscalPeriod.class, "F") + .addLeftJoin("T." + FinancialTransaction.PROPERTY_ENTRY, "E", true) + .addWhere("T.amountCredit != T.amountDebit OR E.account = null OR E.voucher = null OR E.voucher = '' OR E.description = null OR E.description = ''") + .addEquals(FinancialTransaction.PROPERTY_ENTRY_BOOK, entryBook) + .addEquals(FinancialTransaction.PROPERTY_FINANCIAL_PERIOD, period); + List<FinancialTransaction> result = findAllByQuery(query); + return result; + } + + /** + * Find all transaction without entry book. + * + * TODO echatellier 20120425 same method as getAllUnfilledTransaction ? + * doesn't concern a period ? + * + * @return + * @throws TopiaException + */ + public List<FinancialTransaction> getAllTransactionWithoutEntryBook() throws TopiaException { + TopiaQuery query = createQuery("T"); + query.addDistinct() + .addOrder("T." + FinancialTransaction.TOPIA_CREATE_DATE) + .addFrom(FiscalPeriod.class, "F") + .addLeftJoin("T." + FinancialTransaction.PROPERTY_ENTRY, "E", true) + .addWhere("T.amountCredit != T.amountDebit OR E.account = null OR E.voucher = null OR E.voucher = '' OR E.description = null OR E.description = ''") + .addNull(FinancialTransaction.PROPERTY_ENTRY_BOOK); + List<FinancialTransaction> result = findAllByQuery(query); + return result; + } + } //FinancialTransactionDAO