View Javadoc
1   /*
2    * Copyright 2005-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  package org.kuali.ole.gl.dataaccess.impl;
17  
18  import java.util.ArrayList;
19  import java.util.Arrays;
20  import java.util.Iterator;
21  import java.util.List;
22  import java.util.Map;
23  
24  import org.apache.ojb.broker.query.Criteria;
25  import org.apache.ojb.broker.query.Query;
26  import org.apache.ojb.broker.query.QueryByCriteria;
27  import org.apache.ojb.broker.query.QueryFactory;
28  import org.apache.ojb.broker.query.ReportQueryByCriteria;
29  import org.kuali.ole.gl.OJBUtility;
30  import org.kuali.ole.gl.businessobject.Encumbrance;
31  import org.kuali.ole.gl.businessobject.Transaction;
32  import org.kuali.ole.gl.dataaccess.EncumbranceDao;
33  import org.kuali.ole.sys.OLEConstants;
34  import org.kuali.ole.sys.OLEPropertyConstants;
35  import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
36  
37  /**
38   * An OJB implementation of the EncumbranceDao
39   */
40  public class EncumbranceDaoOjb extends PlatformAwareDaoBaseOjb implements EncumbranceDao {
41      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(EncumbranceDaoOjb.class);
42  
43      /**
44       * Returns an encumbrance that would be affected by the given transaction
45       * 
46       * @param t the transaction to find the affected encumbrance for
47       * @return an Encumbrance that would be affected by the posting of the transaction, or null
48       * @see org.kuali.ole.gl.dataaccess.EncumbranceDao#getEncumbranceByTransaction(org.kuali.ole.gl.businessobject.Transaction)
49       */
50      public Encumbrance getEncumbranceByTransaction(Transaction t) {
51          LOG.debug("getEncumbranceByTransaction() started");
52  
53          Criteria crit = new Criteria();
54          crit.addEqualTo(OLEPropertyConstants.UNIVERSITY_FISCAL_YEAR, t.getUniversityFiscalYear());
55          crit.addEqualTo(OLEPropertyConstants.CHART_OF_ACCOUNTS_CODE, t.getChartOfAccountsCode());
56          crit.addEqualTo(OLEPropertyConstants.ACCOUNT_NUMBER, t.getAccountNumber());
57          crit.addEqualTo(OLEPropertyConstants.SUB_ACCOUNT_NUMBER, t.getSubAccountNumber());
58          crit.addEqualTo(OLEPropertyConstants.OBJECT_CODE, t.getFinancialObjectCode());
59          crit.addEqualTo(OLEPropertyConstants.SUB_OBJECT_CODE, t.getFinancialSubObjectCode());
60          crit.addEqualTo(OLEPropertyConstants.BALANCE_TYPE_CODE, t.getFinancialBalanceTypeCode());
61          crit.addEqualTo(OLEPropertyConstants.ENCUMBRANCE_DOCUMENT_TYPE_CODE, t.getFinancialDocumentTypeCode());
62          crit.addEqualTo(OLEPropertyConstants.ORIGIN_CODE, t.getFinancialSystemOriginationCode());
63          crit.addEqualTo(OLEPropertyConstants.DOCUMENT_NUMBER, t.getDocumentNumber());
64  
65          QueryByCriteria qbc = QueryFactory.newQuery(Encumbrance.class, crit);
66          return (Encumbrance) getPersistenceBrokerTemplate().getObjectByQuery(qbc);
67      }
68  
69      /**
70       * Returns an Iterator of all encumbrances that need to be closed for the fiscal year
71       * 
72       * @param fiscalYear a fiscal year to find encumbrances for
73       * @return an Iterator of encumbrances to close
74       * @see org.kuali.ole.gl.dataaccess.EncumbranceDao#getEncumbrancesToClose(java.lang.Integer)
75       */
76      public Iterator getEncumbrancesToClose(Integer fiscalYear) {
77  
78          Criteria criteria = new Criteria();
79          criteria.addEqualTo(OLEPropertyConstants.UNIVERSITY_FISCAL_YEAR, fiscalYear);
80  
81          QueryByCriteria query = new QueryByCriteria(Encumbrance.class, criteria);
82          query.addOrderByAscending(OLEPropertyConstants.CHART_OF_ACCOUNTS_CODE);
83          query.addOrderByAscending(OLEPropertyConstants.ACCOUNT_NUMBER);
84          query.addOrderByAscending(OLEPropertyConstants.SUB_ACCOUNT_NUMBER);
85          query.addOrderByAscending(OLEPropertyConstants.OBJECT_CODE);
86          query.addOrderByAscending(OLEPropertyConstants.SUB_OBJECT_CODE);
87          query.addOrderByAscending(OLEPropertyConstants.BALANCE_TYPE_CODE);
88  
89          return getPersistenceBrokerTemplate().getIteratorByQuery(query);
90      }
91  
92      /**
93       * Purges the database of all those encumbrances with the given chart and year 
94       * 
95       * @param chartOfAccountsCode the chart of accounts code purged encumbrances will have
96       * @param year the university fiscal year purged encumbrances will have
97       * @see org.kuali.ole.gl.dataaccess.EncumbranceDao#purgeYearByChart(java.lang.String, int)
98       */
99      public void purgeYearByChart(String chartOfAccountsCode, int year) {
100         LOG.debug("purgeYearByChart() started");
101 
102         Criteria criteria = new Criteria();
103         criteria.addEqualTo(OLEPropertyConstants.CHART, chartOfAccountsCode);
104         criteria.addLessThan(OLEPropertyConstants.UNIVERSITY_FISCAL_YEAR, new Integer(year));
105 
106         getPersistenceBrokerTemplate().deleteByQuery(new QueryByCriteria(Encumbrance.class, criteria));
107 
108         // This is required because if any deleted account balances are in the cache, deleteByQuery
109         // doesn't
110         // remove them from the cache so a future select will retrieve these deleted account
111         // balances from
112         // the cache and return them. Clearing the cache forces OJB to go to the database again.
113         getPersistenceBrokerTemplate().clearCache();
114     }
115 
116     /**
117      * fetch all encumbrance records from GL open encumbrance table
118      * 
119      * @return an Iterator with all encumbrances currently in the database
120      * @see org.kuali.ole.gl.dataaccess.EncumbranceDao#getAllEncumbrances()
121      */
122     public Iterator getAllEncumbrances() {
123         Criteria criteria = new Criteria();
124         QueryByCriteria query = QueryFactory.newQuery(Encumbrance.class, criteria);
125         return getPersistenceBrokerTemplate().getIteratorByQuery(query);
126     }
127 
128     /**
129      * group all encumbrances with/without the given document type code by fiscal year, chart, account, sub-account, object code,
130      * sub object code, and balance type code, and summarize the encumbrance amount and the encumbrance close amount.
131      * 
132      * @param documentTypeCode the given document type code
133      * @param included indicate if all encumbrances with the given document type are included in the results or not
134      * @return an Iterator of arrays of java.lang.Objects holding summarization data about qualifying encumbrances 
135      * @see org.kuali.ole.gl.dataaccess.EncumbranceDao#getSummarizedEncumbrances(String, boolean)
136      */
137     public Iterator getSummarizedEncumbrances(String documentTypeCode, boolean included) {
138         Criteria criteria = new Criteria();
139 
140         if (included) {
141             criteria.addEqualTo(OLEPropertyConstants.ENCUMBRANCE_DOCUMENT_TYPE_CODE, documentTypeCode);
142         }
143         else {
144             criteria.addNotEqualTo(OLEPropertyConstants.ENCUMBRANCE_DOCUMENT_TYPE_CODE, documentTypeCode);
145         }
146 
147         ReportQueryByCriteria query = QueryFactory.newReportQuery(Encumbrance.class, criteria);
148 
149         // set the selection attributes
150         List attributeList = buildAttributeList();
151         String[] attributes = (String[]) attributeList.toArray(new String[attributeList.size()]);
152         query.setAttributes(attributes);
153 
154         // add the group criteria into the selection statement
155         List groupByList = buildGroupByList();
156         String[] groupBy = (String[]) groupByList.toArray(new String[groupByList.size()]);
157         query.addGroupBy(groupBy);
158 
159         return getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(query);
160     }
161 
162     /**
163      * Queries the database to find all open encumbrances that qualify by the given keys
164      * 
165      * @param fieldValues the input fields and values
166      * @return a collection of open encumbrances
167      * @see org.kuali.ole.gl.dataaccess.EncumbranceDao#findOpenEncumbrance(java.util.Map)
168      */
169     public Iterator findOpenEncumbrance(Map fieldValues, boolean includeZeroEncumbrances) {
170         LOG.debug("findOpenEncumbrance() started");
171 
172         Query query = this.getOpenEncumbranceQuery(fieldValues, includeZeroEncumbrances);
173         OJBUtility.limitResultSize(query);
174         return getPersistenceBrokerTemplate().getIteratorByQuery(query);
175     }
176 
177     /**
178      * Counts the number of open encumbrances that have the keys given in the map
179      * 
180      * @param fieldValues the input fields and values
181      * @return the number of the open encumbrances
182      * @see org.kuali.ole.gl.dataaccess.EncumbranceDao#getOpenEncumbranceRecordCount(java.util.Map)
183      */
184     public Integer getOpenEncumbranceRecordCount(Map fieldValues, boolean includeZeroEncumbrances) {
185         LOG.debug("getOpenEncumbranceRecordCount() started");
186 
187         Query query = this.getOpenEncumbranceQuery(fieldValues, includeZeroEncumbrances);
188         return getPersistenceBrokerTemplate().getCount(query);
189     }
190 
191     /**
192      * build the query for encumbrance search
193      * 
194      * @param fieldValues a Map of values to use as keys for the query
195      * @param includeZeroEncumbrances should the query include encumbrances which have zeroed out?
196      * @return an OJB query
197      */
198     protected Query getOpenEncumbranceQuery(Map fieldValues, boolean includeZeroEncumbrances) {
199         Criteria criteria = OJBUtility.buildCriteriaFromMap(fieldValues, new Encumbrance());
200         criteria.addIn(OLEPropertyConstants.BALANCE_TYPE_CODE, Arrays.asList(OLEConstants.ENCUMBRANCE_BALANCE_TYPE));
201         if (!includeZeroEncumbrances) {
202             Criteria nonZeroEncumbranceCriteria = new Criteria();
203             nonZeroEncumbranceCriteria.addNotEqualToField(OLEPropertyConstants.ACCOUNT_LINE_ENCUMBRANCE_AMOUNT, OLEPropertyConstants.ACCOUNT_LINE_ENCUMBRANCE_CLOSED_AMOUNT);
204             criteria.addAndCriteria(nonZeroEncumbranceCriteria);
205         }
206         return QueryFactory.newQuery(Encumbrance.class, criteria);
207     }
208 
209     /**
210      * This method builds the atrribute list used by balance searching
211      * 
212      * @return a List of encumbrance attributes that need to be summed
213      */
214     protected List buildAttributeList() {
215         List attributeList = this.buildGroupByList();
216 
217         attributeList.add("sum(" + OLEPropertyConstants.ACCOUNT_LINE_ENCUMBRANCE_AMOUNT + ")");
218         attributeList.add("sum(" + OLEPropertyConstants.ACCOUNT_LINE_ENCUMBRANCE_CLOSED_AMOUNT + ")");
219 
220         return attributeList;
221     }
222 
223     /**
224      * This method builds group by attribute list used by balance searching
225      * 
226      * @return a List of encumbrance attributes to search on
227      */
228     protected List buildGroupByList() {
229         List attributeList = new ArrayList();
230 
231         attributeList.add(OLEPropertyConstants.UNIVERSITY_FISCAL_YEAR);
232         attributeList.add(OLEPropertyConstants.CHART_OF_ACCOUNTS_CODE);
233         attributeList.add(OLEPropertyConstants.ACCOUNT_NUMBER);
234         attributeList.add(OLEPropertyConstants.SUB_ACCOUNT_NUMBER);
235         attributeList.add(OLEPropertyConstants.OBJECT_CODE);
236         attributeList.add(OLEPropertyConstants.SUB_OBJECT_CODE);
237         attributeList.add(OLEPropertyConstants.BALANCE_TYPE_CODE);
238 
239         return attributeList;
240     }
241     
242     /**
243      * @see org.kuali.ole.gl.dataaccess.EncumbranceDao#findCountGreaterOrEqualThan(java.lang.Integer)
244      */
245     public Integer findCountGreaterOrEqualThan(Integer year) {
246         Criteria criteria = new Criteria();
247         criteria.addGreaterOrEqualThan(OLEPropertyConstants.UNIVERSITY_FISCAL_YEAR, year);
248         
249         ReportQueryByCriteria query = QueryFactory.newReportQuery(Encumbrance.class, criteria);
250         
251         return getPersistenceBrokerTemplate().getCount(query);
252     }
253 }