Author: echatellier Date: 2010-04-08 15:26:27 +0200 (Thu, 08 Apr 2010) New Revision: 2837 Log: Add transaction service methods Modified: trunk/lima-business/src/main/java/org/chorem/lima/business/TransactionService.java trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/TransactionServiceImpl.java Modified: trunk/lima-business/src/main/java/org/chorem/lima/business/TransactionService.java =================================================================== --- trunk/lima-business/src/main/java/org/chorem/lima/business/TransactionService.java 2010-04-08 12:29:15 UTC (rev 2836) +++ trunk/lima-business/src/main/java/org/chorem/lima/business/TransactionService.java 2010-04-08 13:26:27 UTC (rev 2837) @@ -21,6 +21,8 @@ import java.util.List; +import org.chorem.lima.entity.EntryBook; +import org.chorem.lima.entity.FinancialPeriod; import org.chorem.lima.entity.Transaction; /** @@ -34,11 +36,36 @@ */ public interface TransactionService { - //List<Transaction> getAllTransactions() throws LimaException; + /** + * Find all transaction (use carefully, can return a lot of results). + * + * @return all transaction + * @throws LimaException + */ + List<Transaction> getAllTransactions() throws LimaException; - //void createTransaction(Transaction transaction) throws LimaException; + /** + * Find all transaction for an entry book. + * + * @param entryBook entry book + * @return all transaction for entry book + * @throws LimaException + */ + List<Transaction> getAllTransactionsForEntryBook(EntryBook entryBook) throws LimaException; + + /** + * Find all transaction for an entry book and a financial period. + * + * @param entryBook entry book + * @param period financial period + * @return all transaction for entry book and period + * @throws LimaException + */ + List<Transaction> getAllTransactionsForEntryBookAndFinancialPeriod(EntryBook entryBook, FinancialPeriod period) throws LimaException; + + void createTransaction(Transaction transaction) throws LimaException; - //void updateTransaction(Transaction transaction) throws LimaException; + void updateTransaction(Transaction transaction) throws LimaException; - //void removeTransaction(Transaction transaction) throws LimaException; + void removeTransaction(Transaction transaction) throws LimaException; } Modified: trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/TransactionServiceImpl.java =================================================================== --- trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/TransactionServiceImpl.java 2010-04-08 12:29:15 UTC (rev 2836) +++ trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/TransactionServiceImpl.java 2010-04-08 13:26:27 UTC (rev 2837) @@ -19,13 +19,17 @@ package org.chorem.lima.business.ejb; +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.LimaConfig; +import org.chorem.lima.business.LimaException; import org.chorem.lima.business.TransactionService; import org.chorem.lima.entity.EntryBook; +import org.chorem.lima.entity.FinancialPeriod; import org.chorem.lima.entity.LimaCallaoDAOHelper; import org.chorem.lima.entity.Transaction; import org.chorem.lima.entity.TransactionDAO; @@ -45,7 +49,7 @@ * @author Rémi Chapelet */ @Stateless -public class TransactionServiceImpl implements TransactionService { +public class TransactionServiceImpl extends AbstractLimaService implements TransactionService { private static final Log log = LogFactory.getLog(TransactionServiceImpl.class); @@ -78,6 +82,7 @@ * @param journal * @return */ + @Override public void createTransaction(Transaction transaction /*Date entryDate, String voucherRef, String description, TimeSpan timeSpan, Journal journal*/) { /*String result = ServiceHelper.RESPOND_ERROR; @@ -227,153 +232,77 @@ return result; }*/ - /* - * Permet de modifier une transaction à partir d'une transaction DTO. - * Elle va rechercher le journal et le timespan correspondants. - * @param transactionDTO - * @return - * - public String modifyTransaction(TransactionDTO transactionDTO) { - // Recherche du journal - JournalServiceImpl journalServiceImpl = new JournalServiceImpl(); - Journal journal = journalServiceImpl - .searchJournalWithLabel(transactionDTO.getJournalDTO() - .getLabel()); - // Recherche du timeSpan - TimeSpan timeSpan = timeSpanServiceImpl - .searchTimeSpanByDate(transactionDTO.getTimeSpanDTO() - .getBeginTimeSpan()); - // Appel pour la modification - String result = modifyTransaction(transactionDTO.getId(), - transactionDTO.getEntryDate(), transactionDTO.getVoucherRef(), - transactionDTO.getDescription(), timeSpan, journal); - return result; - }*/ + /** + * Permet de retrouver la premiere transaction associée au journal. + * + * Utilisé par entrybook service pour savoir si un journal a des transactions + * associées et empecher la suppression. + * + * @param topiaTransaction context à utiliser + * @param entryBook journal + * @throws TopiaException + */ + protected Transaction findByEntryBook(TopiaContext topiaTransaction, EntryBook entryBook) throws TopiaException { - /* - * Recherche une transaction unique avec son identifiant TopiaId. - * @param topiaId - * @return - * - public Transaction searchTransactionWithTopiaId(String topiaId) { - Transaction transaction = null; - try { - // Acces BDD - TopiaContext topiaContext = rootContext.beginTransaction(); - // Chargement du DAO - TransactionDAO transactionDAO = LimaCallaoDAOHelper - .getTransactionDAO(topiaContext); - // Recherche - transaction = transactionDAO.findByTopiaId(topiaId); - // Fermeture BDD - topiaContext.closeContext(); - } catch (TopiaException e) { - log.error(e); - } - return transaction; - }*/ + TransactionDAO transactionDAO = LimaCallaoDAOHelper.getTransactionDAO(topiaTransaction); - /* - * Recherche une transaction unique à partir d'une transaction DTO. - * @param transactionDTO - * @return - * - public Transaction searchTransactionWithTopiaId( - TransactionDTO transactionDTO) { - Transaction transaction = searchTransactionWithTopiaId(transactionDTO - .getId()); - return transaction; - }*/ + TopiaQuery query = transactionDAO.createQuery(); + // entryBook is not visible, but column "entryBook" + // exist in transaction table + query.add("entryBook", entryBook); - /* - * Recherche une transaction dans la base de données, et renvoie celle-ci - * sous format DTO. Nous ne pouvons pas appeler la méthode searchTransactionWithTopiaId - * du fait que lors de la convertion, nous avons besoin du journal et du - * timeSpan. Or il ne faut donc pas de fermeture de connexion à la base de - * données pour celà. - * @param topiaId - * @return - * - public TransactionDTO searchTransactionDTO(String topiaId) { - TransactionDTO transactionDTO = null; - try { - // Acces BDD - TopiaContext topiaContext = rootContext.beginTransaction(); - // Chargement du DAO - TransactionDAO transactionDAO = LimaCallaoDAOHelper - .getTransactionDAO(topiaContext); - // Recherche - Transaction transaction = transactionDAO.findByTopiaId(topiaId); - // Converti la transaction en DTO - convertTransaction.setTransaction(rootContext); - transactionDTO = convertTransaction - .transactionEntityToDto(transaction); - // Fermeture BDD - topiaContext.closeContext(); - } catch (TopiaException e) { - log.error(e); - } - return transactionDTO; - }*/ + Transaction result = transactionDAO.findByQuery(query); + + return result; + } - /* - * Recherche toutes les transactions qui correspondent à un timespan. - * @param timeSpan - * @return - * - public List<Transaction> searchListTransactionWithTimeSpan(TimeSpan timeSpan) { - List<Transaction> listTransaction = null; - if (timeSpan != null) { - try { - // Acces BDD - TopiaContext topiaContext = rootContext.beginTransaction(); - // Chargement du DAO - TransactionDAO transactionDAO = LimaCallaoDAOHelper - .getTransactionDAO(topiaContext); - // Recherche - listTransaction = transactionDAO.findAllByTimeSpan(timeSpan); - // Fermeture BDD - topiaContext.closeContext(); - } catch (TopiaException e) { - log.error(e); - } - } - return listTransaction; - }*/ + @Override + public List<Transaction> getAllTransactions() throws LimaException { + + // find all with null entry book filter + + return getAllTransactionsForEntryBook(null); + } + + @Override + public List<Transaction> getAllTransactionsForEntryBook(EntryBook entryBook) throws LimaException { + + // find all with null period filter - /* - * Recherche les transactions DTO d'un timeSpan - * @param timeSpan - * @return - * - public List<TransactionDTO> searchListTransactionDTOWithTimeSpan( - TimeSpan timeSpan) { - List<TransactionDTO> listTransactionDTO = new ArrayList<TransactionDTO>(); - if (timeSpan != null) { - try { - // Acces BDD - TopiaContext topiaContext = rootContext.beginTransaction(); - // Chargement du DAO - TransactionDAO transactionDAO = LimaCallaoDAOHelper - .getTransactionDAO(topiaContext); - // Recherche - List<Transaction> listTransaction = transactionDAO - .findAllByTimeSpan(timeSpan); - // Converti la transaction en DTO - convertTransaction.setTransaction(rootContext); - for (Transaction transaction : listTransaction) { - TransactionDTO transactionDTO = convertTransaction - .transactionEntityToDto(transaction); - listTransactionDTO.add(transactionDTO); - } - // Fermeture BDD - topiaContext.closeContext(); - } catch (TopiaException e) { - log.error(e); + return getAllTransactionsForEntryBookAndFinancialPeriod(entryBook, null); + } + + @Override + public List<Transaction> getAllTransactionsForEntryBookAndFinancialPeriod(EntryBook entryBook, FinancialPeriod financialPeriod) throws LimaException { + + List<Transaction> transactions = null; + TopiaContext topiaTransaction = null; + try { + topiaTransaction = rootContext.beginTransaction(); + TransactionDAO transactionDAO = LimaCallaoDAOHelper.getTransactionDAO(topiaTransaction); + + // filter with topia query + TopiaQuery query = transactionDAO.createQuery(); + + if (entryBook != null) { + query.add("entryBook", entryBook); } + + if (financialPeriod != null) { + query.add("financialPeriod", financialPeriod); + } + + transactions = transactionDAO.findAllByQuery(query); } - return listTransactionDTO; - }*/ + catch (TopiaException ex) { + doCatch(topiaTransaction, ex, log); + } + finally { + doFinally(topiaTransaction, log); + } + + return transactions; + } /* * Recherche toutes les transactions appartenant à un même journal donné @@ -466,66 +395,46 @@ return result; }*/ - /* - * Permet d'effacer une transaction dans la base de données. - * ATTENTION : Elle supprime également toutes les entées comptables - * associées. Une transaction appartenant à un timeSpan bloqué ne peut être - * supprimée. - * @return - * - public String removeTransaction(String topiaId) { - String result = ServiceHelper.RESPOND_ERROR; + @Override + public void updateTransaction(Transaction transaction) throws LimaException { + + // TODO EC-20100408 add some checks ? (balance, status, etc... ? ) + + TopiaContext topiaTransaction = null; try { - // Acces BDD - TopiaContext topiaContext = rootContext.beginTransaction(); - // Chargement du DAO - TransactionDAO transactionDAO = LimaCallaoDAOHelper - .getTransactionDAO(topiaContext); - // Recherche de l'entry - Transaction transaction = transactionDAO.findByTopiaId(topiaId); - // Transaction doit exister - if (transaction != null) { - // Vérifie si la transaction est non bloquée. - if (!isTransactionBlocked(transaction)) { - // Supprime tous les entrys de la transactions - List<Entry> listEntry = entryServiceImpl - .searchEntryWithTransaction(transaction); - for (Entry entry : listEntry) { - result = removeEntry(entry.getTopiaId()); - } - // Supprime la transaction - transactionDAO.delete(transaction); - // Création BDD - topiaContext.commitTransaction(); - if (log.isInfoEnabled()) { - log.info("La transaction a été supprimée avec succès."); - } - result = ServiceHelper.RESPOND_SUCCESS; - } else { - // Transaction bloquée - result = ServiceHelper.TRANSACTION_TIMESPAN_BLOCKED; - } - } else { - result = ServiceHelper.TRANSACTION_NOT_EXIST; - } - // Fermeture BDD - topiaContext.closeContext(); - } catch (TopiaException e) { - log.error(e); + topiaTransaction = rootContext.beginTransaction(); + TransactionDAO transactionDAO = LimaCallaoDAOHelper.getTransactionDAO(topiaTransaction); + transactionDAO.update(transaction); } - return result; - }*/ + catch (TopiaException ex) { + doCatch(topiaTransaction, ex, log); + } + finally { + doFinally(topiaTransaction, log); + } - /* - * Supprime une transaction au format DTO. - * @param transactionDTO - * @return - * - public String removeTransaction(TransactionDTO transactionDTO) { - String result = removeTransaction(transactionDTO.getId()); - return result; - }*/ + } + @Override + public void removeTransaction(Transaction transaction) throws LimaException { + + // TODO EC-20100408 add some checks ? (balance, status, etc... ? ) + + TopiaContext topiaTransaction = null; + try { + topiaTransaction = rootContext.beginTransaction(); + TransactionDAO transactionDAO = LimaCallaoDAOHelper.getTransactionDAO(topiaTransaction); + transactionDAO.delete(transaction); + } + catch (TopiaException ex) { + doCatch(topiaTransaction, ex, log); + } + finally { + doFinally(topiaTransaction, log); + } + + } + /* * Permet d'ajouter une entrée comptable pour une transaction donnée. * ATTENTION : la transaction doit être NON bloquée. @@ -734,26 +643,4 @@ boolean isTransactionBalanced = debit == credit; return isTransactionBalanced; }*/ - - /** - * Permet de retrouver la premiere transaction associée au journal. - * - * @param topiaTransaction context à utiliser - * @param entryBook journal - * @throws TopiaException - */ - protected Transaction findByEntryBook(TopiaContext topiaTransaction, EntryBook entryBook) throws TopiaException { - - TransactionDAO transactionDAO = LimaCallaoDAOHelper.getTransactionDAO(topiaTransaction); - - TopiaQuery query = transactionDAO.createQuery(); - // entryBook is not visible, but column "entryBook" - // exist in transaction table - query.add("entryBook", entryBook); - - Transaction result = transactionDAO.findByQuery(query); - - return result; - } - } \ No newline at end of file