View Javadoc
1   /*
2    * The Kuali Financial System, a comprehensive financial management system for higher education.
3    * 
4    * Copyright 2005-2014 The Kuali Foundation
5    * 
6    * This program is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU Affero General Public License as
8    * published by the Free Software Foundation, either version 3 of the
9    * License, or (at your option) any later version.
10   * 
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU Affero General Public License for more details.
15   * 
16   * You should have received a copy of the GNU Affero General Public License
17   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  package org.kuali.kfs.gl.dataaccess.impl;
20  
21  import java.math.BigDecimal;
22  import java.util.Iterator;
23  
24  import org.apache.ojb.broker.query.Criteria;
25  import org.apache.ojb.broker.query.QueryByCriteria;
26  import org.apache.ojb.broker.query.QueryFactory;
27  import org.apache.ojb.broker.query.ReportQueryByCriteria;
28  import org.kuali.kfs.gl.businessobject.Entry;
29  import org.kuali.kfs.gl.businessobject.Transaction;
30  import org.kuali.kfs.gl.dataaccess.EntryDao;
31  import org.kuali.kfs.gl.dataaccess.LedgerEntryBalancingDao;
32  import org.kuali.kfs.sys.KFSConstants;
33  import org.kuali.kfs.sys.KFSPropertyConstants;
34  import org.kuali.kfs.sys.util.TransactionalServiceUtils;
35  import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
36  import org.kuali.rice.krad.util.ObjectUtils;
37  
38  /**
39   * An OJB implementation of EntryDao
40   */
41  public class EntryDaoOjb extends PlatformAwareDaoBaseOjb implements EntryDao, LedgerEntryBalancingDao {
42      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(EntryDaoOjb.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      /**
59       * Constructs a EntryDaoOjb instance
60       */
61      public EntryDaoOjb() {
62          super();
63      }
64  
65      /**
66       * Find the maximum transactionLedgerEntrySequenceNumber in the entry table for a specific transaction. This is used to make
67       * sure that rows added have a unique primary key.
68       * 
69       * @param t the transaction to check
70       * @return the max sequence number
71       */
72      public int getMaxSequenceNumber(Transaction t) {
73          LOG.debug("getSequenceNumber() ");
74  
75          Criteria crit = new Criteria();
76          crit.addEqualTo(UNIVERISITY_FISCAL_YEAR, t.getUniversityFiscalYear());
77          crit.addEqualTo(CHART_OF_ACCOUNTS_CODE, t.getChartOfAccountsCode());
78          crit.addEqualTo(ACCOUNT_NUMBER, t.getAccountNumber());
79          crit.addEqualTo(SUB_ACCOUNT_NUMBER, t.getSubAccountNumber());
80          crit.addEqualTo(FINANCIAL_OBJECT_CODE, t.getFinancialObjectCode());
81          crit.addEqualTo(FINANCIAL_SUB_OBJECT_CODE, t.getFinancialSubObjectCode());
82          crit.addEqualTo(FINANCIAL_BALANCE_TYPE_CODE, t.getFinancialBalanceTypeCode());
83          crit.addEqualTo(FINANCIAL_OBJECT_TYPE_CODE, t.getFinancialObjectTypeCode());
84          crit.addEqualTo(UNIVERISTY_FISCAL_PERIOD_CODE, t.getUniversityFiscalPeriodCode());
85          crit.addEqualTo(FINANCIAL_DOCUMENT_TYPE_CODE, t.getFinancialDocumentTypeCode());
86          crit.addEqualTo(FINANCIAL_SYSTEM_ORIGINATION_CODE, t.getFinancialSystemOriginationCode());
87          crit.addEqualTo(KFSPropertyConstants.DOCUMENT_NUMBER, t.getDocumentNumber());
88  
89          ReportQueryByCriteria q = QueryFactory.newReportQuery(Entry.class, crit);
90          q.setAttributes(new String[] { "max(transactionLedgerEntrySequenceNumber)" });
91  
92          Iterator iter = getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(q);
93          // would this work better? max = (BigDecimal) getPersistenceBrokerTemplate().getObjectByQuery(q);
94          BigDecimal max = null;
95          while (iter.hasNext()) {
96              Object[] data = (Object[]) iter.next();
97              max = (BigDecimal) data[0]; // Don't know why OJB returns a BigDecimal, but it does
98          }
99          if (max == null) {
100             return 0;
101         }
102         else {
103             return max.intValue();
104         }
105     }
106 
107     /**
108      * Purge the entry table by chart/year
109      * 
110      * @param chart the chart of accounts code of entries to purge
111      * @param year the university fiscal year of entries to purge
112      */
113     public void purgeYearByChart(String chartOfAccountsCode, int year) {
114         LOG.debug("purgeYearByChart() started");
115 
116         Criteria criteria = new Criteria();
117         criteria.addEqualTo(CHART_OF_ACCOUNTS_CODE, chartOfAccountsCode);
118         criteria.addLessThan(UNIVERISITY_FISCAL_YEAR, new Integer(year));
119 
120         getPersistenceBrokerTemplate().deleteByQuery(new QueryByCriteria(Entry.class, criteria));
121 
122         // This is required because if any deleted rows are in the cache, deleteByQuery doesn't
123         // remove them from the cache so a future select will retrieve these deleted account balances from
124         // the cache and return them. Clearing the cache forces OJB to go to the database again.
125         getPersistenceBrokerTemplate().clearCache();
126     }
127     
128     /**
129      * @see org.kuali.kfs.gl.dataaccess.LedgerEntryBalancingDao#findEntryByGroup(java.lang.Integer, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
130      */
131     public Object[] findEntryByGroup(Integer universityFiscalYear, String chartOfAccountsCode, String financialObjectCode, String financialBalanceTypeCode, String universityFiscalPeriodCode, String transactionDebitCreditCode) {
132         Criteria criteria = new Criteria();
133         criteria.addEqualTo(KFSConstants.UNIVERSITY_FISCAL_YEAR_PROPERTY_NAME, universityFiscalYear);
134         criteria.addEqualTo(KFSConstants.CHART_OF_ACCOUNTS_CODE_PROPERTY_NAME, chartOfAccountsCode);
135         criteria.addEqualTo(KFSConstants.FINANCIAL_OBJECT_CODE_PROPERTY_NAME, financialObjectCode);
136         criteria.addEqualTo(KFSConstants.FINANCIAL_BALANCE_TYPE_CODE_PROPERTY_NAME, financialBalanceTypeCode);
137         criteria.addEqualTo(KFSConstants.UNIVERSITY_FISCAL_PERIOD_CODE_PROPERTY_NAME, universityFiscalPeriodCode);
138         criteria.addEqualTo(KFSConstants.TRANSACTION_DEBIT_CREDIT_CODE, transactionDebitCreditCode);
139 
140         ReportQueryByCriteria reportQuery = QueryFactory.newReportQuery(Entry.class, criteria);
141         reportQuery.setAttributes(new String[] { "count(*)", "sum(" + KFSConstants.TRANSACTION_LEDGER_ENTRY_AMOUNT + ")"});
142         reportQuery.addGroupBy(new String[] { KFSConstants.UNIVERSITY_FISCAL_YEAR_PROPERTY_NAME, KFSConstants.CHART_OF_ACCOUNTS_CODE_PROPERTY_NAME, KFSConstants.FINANCIAL_OBJECT_CODE_PROPERTY_NAME, KFSConstants.FINANCIAL_BALANCE_TYPE_CODE_PROPERTY_NAME, KFSConstants.UNIVERSITY_FISCAL_PERIOD_CODE_PROPERTY_NAME, KFSConstants.TRANSACTION_DEBIT_CREDIT_CODE});
143         
144         Iterator<Object[]> iterator = getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(reportQuery);
145         Object[] returnResult = TransactionalServiceUtils.retrieveFirstAndExhaustIterator(iterator);
146         
147         if (ObjectUtils.isNull(returnResult)) {
148             // Do nothing, we'll return null. Data wasn't found.
149         } else if (returnResult[0] instanceof BigDecimal) {
150             returnResult[0] = ((BigDecimal) returnResult[0]).intValue();
151         } else {
152             returnResult[0] = ((Long) returnResult[0]).intValue();
153         }
154         
155         return returnResult;
156     }
157     
158     /**
159      * @see org.kuali.kfs.gl.dataaccess.LedgerEntryBalancingDao#findCountGreaterOrEqualThan(java.lang.Integer)
160      */
161     public Integer findCountGreaterOrEqualThan(Integer year) {
162         Criteria criteria = new Criteria();
163         criteria.addGreaterOrEqualThan(KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR, year);
164         
165         ReportQueryByCriteria query = QueryFactory.newReportQuery(Entry.class, criteria);
166         
167         return getPersistenceBrokerTemplate().getCount(query);
168     }
169 }