View Javadoc

1   /*
2    * Copyright 2005 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.gl.dataaccess.impl;
17  
18  import java.util.Iterator;
19  
20  import org.apache.commons.lang.StringUtils;
21  import org.apache.ojb.broker.query.Criteria;
22  import org.apache.ojb.broker.query.QueryByCriteria;
23  import org.apache.ojb.broker.query.QueryFactory;
24  import org.kuali.ole.gl.GeneralLedgerConstants;
25  import org.kuali.ole.gl.businessobject.ExpenditureTransaction;
26  import org.kuali.ole.gl.businessobject.Transaction;
27  import org.kuali.ole.gl.dataaccess.ExpenditureTransactionDao;
28  import org.kuali.ole.sys.OLEPropertyConstants;
29  import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
30  
31  /**
32   * The OJB implmentation of ExpenditureTransactionDao
33   */
34  public class ExpenditureTransactionDaoOjb extends PlatformAwareDaoBaseOjb implements ExpenditureTransactionDao {
35      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ExpenditureTransactionDaoOjb.class);
36  
37      /**
38       * Constructs a ExpenditureTransactionDaoOjb instance
39       */
40      public ExpenditureTransactionDaoOjb() {
41          super();
42      }
43  
44      /**
45       * Queries the database to find the expenditure transaction in the database that would be affected if the given transaction is
46       * posted
47       * 
48       * @param t a transaction to find a related expenditure transaction for
49       * @return the expenditure transaction if found, null otherwise
50       * @see org.kuali.ole.gl.dataaccess.ExpenditureTransactionDao#getByTransaction(org.kuali.ole.gl.businessobject.Transaction)
51       */
52      public ExpenditureTransaction getByTransaction(Transaction t) {
53          LOG.debug("getByTransaction() started");
54  
55          Criteria crit = new Criteria();
56          crit.addEqualTo(OLEPropertyConstants.UNIVERSITY_FISCAL_YEAR, t.getUniversityFiscalYear());
57          crit.addEqualTo(OLEPropertyConstants.CHART_OF_ACCOUNTS_CODE, t.getChartOfAccountsCode());
58          crit.addEqualTo(OLEPropertyConstants.ACCOUNT_NUMBER, t.getAccountNumber());
59          crit.addEqualTo(OLEPropertyConstants.SUB_ACCOUNT_NUMBER, t.getSubAccountNumber());
60          crit.addEqualTo(OLEPropertyConstants.OBJECT_CODE, t.getFinancialObjectCode());
61          crit.addEqualTo(OLEPropertyConstants.SUB_OBJECT_CODE, t.getFinancialSubObjectCode());
62          crit.addEqualTo(OLEPropertyConstants.BALANCE_TYPE_CODE, t.getFinancialBalanceTypeCode());
63          crit.addEqualTo(OLEPropertyConstants.OBJECT_TYPE_CODE, t.getFinancialObjectTypeCode());
64          crit.addEqualTo(OLEPropertyConstants.UNIVERSITY_FISCAL_ACCOUNTING_PERIOD, t.getUniversityFiscalPeriodCode());
65          crit.addEqualTo(OLEPropertyConstants.PROJECT_CODE, t.getProjectCode());
66  
67          if (StringUtils.isBlank(t.getOrganizationReferenceId())) {
68              crit.addEqualTo(OLEPropertyConstants.ORGANIZATION_REFERENCE_ID, GeneralLedgerConstants.getDashOrganizationReferenceId());
69          }
70          else {
71              crit.addEqualTo(OLEPropertyConstants.ORGANIZATION_REFERENCE_ID, t.getOrganizationReferenceId());
72          }
73  
74          QueryByCriteria qbc = QueryFactory.newQuery(ExpenditureTransaction.class, crit);
75          return (ExpenditureTransaction) getPersistenceBrokerTemplate().getObjectByQuery(qbc);
76      }
77  
78      /**
79       * Fetches all expenditure transactions currently in the database
80       * 
81       * @return an Iterator with all expenditure transactions from the database
82       * @see org.kuali.ole.gl.dataaccess.ExpenditureTransactionDao#getAllExpenditureTransactions()
83       */
84      public Iterator getAllExpenditureTransactions() {
85          LOG.debug("getAllExpenditureTransactions() started");
86          try {
87              Criteria crit = new Criteria();
88              // We want them all so no criteria is added
89  
90              QueryByCriteria qbc = QueryFactory.newQuery(ExpenditureTransaction.class, crit);
91              return getPersistenceBrokerTemplate().getIteratorByQuery(qbc);
92          }
93          catch (Exception e) {
94              throw new RuntimeException(e);
95          }
96      }
97  
98      /**
99       * Deletes the given expenditure transaction
100      * 
101      * @param et the expenditure transaction that will be removed, as such, from the database
102      * @see org.kuali.ole.gl.dataaccess.ExpenditureTransactionDao#delete(org.kuali.ole.gl.businessobject.ExpenditureTransaction)
103      */
104     public void delete(ExpenditureTransaction et) {
105         LOG.debug("delete() started");
106 
107         getPersistenceBrokerTemplate().delete(et);
108     }
109 
110     /**
111      * Since expenditure transactions are temporary, just like flies that live for a mere day, this method removes all of the
112      * currently existing expenditure transactions from the database, all expenditure transactions having run through the poster and
113      * fulfilled their lifecycle
114      * 
115      * @see org.kuali.ole.gl.dataaccess.ExpenditureTransactionDao#deleteAllExpenditureTransactions()
116      */
117     public void deleteAllExpenditureTransactions() {
118         LOG.debug("deleteAllExpenditureTransactions() started");
119         try{
120             Iterator<ExpenditureTransaction> i = getAllExpenditureTransactions();
121             while (i.hasNext()) {
122                 ExpenditureTransaction et = i.next();
123                 if (LOG.isInfoEnabled()) {
124                     LOG.info("The following ExpenditureTransaction was deleted: " + et.toString());
125                 }
126                 delete(et);
127             }
128         }
129         catch (Exception e) {
130             throw new RuntimeException(e);
131         }   
132     }
133 }