View Javadoc
1   /*
2    * Copyright 2006 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  /*
17   * Created on Jan 11, 2006
18   *
19   */
20  package org.kuali.ole.gl.dataaccess.impl;
21  
22  import java.math.BigDecimal;
23  import java.util.Date;
24  import java.util.Iterator;
25  
26  import org.apache.ojb.broker.query.Criteria;
27  import org.apache.ojb.broker.query.QueryByCriteria;
28  import org.apache.ojb.broker.query.QueryFactory;
29  import org.apache.ojb.broker.query.ReportQueryByCriteria;
30  import org.kuali.ole.gl.businessobject.Entry;
31  import org.kuali.ole.gl.businessobject.Reversal;
32  import org.kuali.ole.gl.businessobject.Transaction;
33  import org.kuali.ole.gl.dataaccess.ReversalDao;
34  import org.kuali.ole.sys.OLEPropertyConstants;
35  import org.kuali.ole.sys.util.TransactionalServiceUtils;
36  import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
37  
38  /**
39   * An OJB implementation of the Reversal DAO
40   */
41  public class ReversalDaoOjb extends PlatformAwareDaoBaseOjb implements ReversalDao {
42      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ReversalDaoOjb.class);
43  
44      private final static String UNIVERISITY_FISCAL_YEAR = "universityFiscalYear";
45      private final static String CHART_OF_ACCOUNTS_CODE = "chartOfAccountsCode";
46      private final static String ACCOUNT_NUMBER = "accountNumber";
47      private final static String SUB_ACCOUNT_NUMBER = "subAccountNumber";
48      private final static String FINANCIAL_OBJECT_CODE = "financialObjectCode";
49      private final static String FINANCIAL_SUB_OBJECT_CODE = "financialSubObjectCode";
50      private final static String FINANCIAL_BALANCE_TYPE_CODE = "financialBalanceTypeCode";
51      private final static String FINANCIAL_OBJECT_TYPE_CODE = "financialObjectTypeCode";
52      private final static String UNIVERISTY_FISCAL_PERIOD_CODE = "universityFiscalPeriodCode";
53      private final static String FINANCIAL_DOCUMENT_TYPE_CODE = "financialDocumentTypeCode";
54      private final static String FINANCIAL_SYSTEM_ORIGINATION_CODE = "financialSystemOriginationCode";
55      private final static String MAX_CONSTANT = "max(documentNumber)";
56  
57      /**
58       * Constructs a ReversalDaoOjb instance
59       */
60      public ReversalDaoOjb() {
61          super();
62      }
63  
64      /**
65       * Find the maximum transactionLedgerEntrySequenceNumber in the entry table for a specific transaction. This is used to make
66       * sure that rows added have a unique primary key.
67       * 
68       * @param t a transaction to find the maximum sequence number for
69       * @return the max sequence number for the given transaction
70       */
71      public int getMaxSequenceNumber(Transaction t) {
72          LOG.debug("getSequenceNumber() ");
73  
74          Criteria crit = new Criteria();
75          crit.addEqualTo(UNIVERISITY_FISCAL_YEAR, t.getUniversityFiscalYear());
76          crit.addEqualTo(CHART_OF_ACCOUNTS_CODE, t.getChartOfAccountsCode());
77          crit.addEqualTo(ACCOUNT_NUMBER, t.getAccountNumber());
78          crit.addEqualTo(SUB_ACCOUNT_NUMBER, t.getSubAccountNumber());
79          crit.addEqualTo(FINANCIAL_OBJECT_CODE, t.getFinancialObjectCode());
80          crit.addEqualTo(FINANCIAL_SUB_OBJECT_CODE, t.getFinancialSubObjectCode());
81          crit.addEqualTo(FINANCIAL_BALANCE_TYPE_CODE, t.getFinancialBalanceTypeCode());
82          crit.addEqualTo(FINANCIAL_OBJECT_TYPE_CODE, t.getFinancialObjectTypeCode());
83          crit.addEqualTo(UNIVERISTY_FISCAL_PERIOD_CODE, t.getUniversityFiscalPeriodCode());
84          crit.addEqualTo(FINANCIAL_DOCUMENT_TYPE_CODE, t.getFinancialDocumentTypeCode());
85          crit.addEqualTo(FINANCIAL_SYSTEM_ORIGINATION_CODE, t.getFinancialSystemOriginationCode());
86          crit.addEqualTo(OLEPropertyConstants.DOCUMENT_NUMBER, t.getDocumentNumber());
87  
88          ReportQueryByCriteria q = QueryFactory.newReportQuery(Entry.class, crit);
89          q.setAttributes(new String[] { "max(transactionLedgerEntrySequenceNumber)" });
90  
91          Iterator iter = getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(q);
92          if (iter.hasNext()) {
93              Object[] data = (Object[]) TransactionalServiceUtils.retrieveFirstAndExhaustIterator(iter);
94              BigDecimal max = (BigDecimal) data[0]; // Don't know why OJB returns a BigDecimal, but it does
95  
96              if (max == null) {
97                  return 0;
98              }
99              else {
100                 return max.intValue();
101             }
102         }
103         else {
104             return 0;
105         }
106     }
107 
108     /**
109      * Fetches the reversal record that would affected by the posting of the given transaction
110      * 
111      * @param t the transaction to find the related reversal for
112      * @return the reversal affected by the given transaction
113      * @see org.kuali.ole.gl.dataaccess.ReversalDao#getByTransaction(org.kuali.ole.gl.businessobject.Transaction)
114      */
115     public Reversal getByTransaction(Transaction t) {
116         LOG.debug("getByTransaction() started");
117 
118         Criteria crit = new Criteria();
119         crit.addEqualTo(OLEPropertyConstants.FINANCIAL_DOCUMENT_REVERSAL_DATE, t.getFinancialDocumentReversalDate());
120         crit.addEqualTo(OLEPropertyConstants.UNIVERSITY_FISCAL_YEAR, t.getUniversityFiscalYear());
121         crit.addEqualTo(OLEPropertyConstants.CHART_OF_ACCOUNTS_CODE, t.getChartOfAccountsCode());
122         crit.addEqualTo(OLEPropertyConstants.ACCOUNT_NUMBER, t.getAccountNumber());
123         crit.addEqualTo(OLEPropertyConstants.SUB_ACCOUNT_NUMBER, t.getSubAccountNumber());
124         crit.addEqualTo(OLEPropertyConstants.FINANCIAL_OBJECT_CODE, t.getFinancialObjectCode());
125         crit.addEqualTo(OLEPropertyConstants.FINANCIAL_SUB_OBJECT_CODE, t.getFinancialSubObjectCode());
126         crit.addEqualTo(OLEPropertyConstants.FINANCIAL_BALANCE_TYPE_CODE, t.getFinancialBalanceTypeCode());
127         crit.addEqualTo(OLEPropertyConstants.FINANCIAL_OBJECT_TYPE_CODE, t.getFinancialObjectTypeCode());
128         crit.addEqualTo(OLEPropertyConstants.UNIVERSITY_FISCAL_PERIOD_CODE, t.getUniversityFiscalPeriodCode());
129         crit.addEqualTo(OLEPropertyConstants.FINANCIAL_DOCUMENT_TYPE_CODE, t.getFinancialDocumentTypeCode());
130         crit.addEqualTo(OLEPropertyConstants.FINANCIAL_SYSTEM_ORIGINATION_CODE, t.getFinancialSystemOriginationCode());
131         crit.addEqualTo(OLEPropertyConstants.DOCUMENT_NUMBER, t.getDocumentNumber());
132         crit.addEqualTo(OLEPropertyConstants.TRANSACTION_ENTRY_SEQUENCE_NUMBER, t.getTransactionLedgerEntrySequenceNumber());
133 
134         QueryByCriteria qbc = QueryFactory.newQuery(Reversal.class, crit);
135         return (Reversal) getPersistenceBrokerTemplate().getObjectByQuery(qbc);
136     }
137 
138     /**
139      * Fetches all reversals that have been set to reverse on or before the given date - that is to say,
140      * returns all the reversal records ready to be reversed!
141      * 
142      * @param before the date that reversals must reverse on or before
143      * @return an Iterator of reversal records to reverse
144      * @see org.kuali.ole.gl.dataaccess.ReversalDao#getByDate(java.util.Date)
145      */
146     public Iterator getByDate(Date before) {
147         LOG.debug("getByDate() started");
148 
149         Criteria crit = new Criteria();
150         crit.addLessOrEqualThan(OLEPropertyConstants.FINANCIAL_DOCUMENT_REVERSAL_DATE, new java.sql.Date(before.getTime()));
151 
152         QueryByCriteria qbc = QueryFactory.newQuery(Reversal.class, crit);
153         return getPersistenceBrokerTemplate().getIteratorByQuery(qbc);
154     }
155 
156     /**
157      * Deletes a reversal record
158      * 
159      * @param re reversal to delete
160      * @see org.kuali.ole.gl.dataaccess.ReversalDao#delete(org.kuali.ole.gl.businessobject.Reversal)
161      */
162     public void delete(Reversal re) {
163         LOG.debug("delete() started");
164 
165         getPersistenceBrokerTemplate().delete(re);
166     }
167 }