View Javadoc

1   /*
2    * Copyright 2007 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.sys.dataaccess.impl;
17  
18  import java.math.BigDecimal;
19  import java.util.ArrayList;
20  import java.util.Arrays;
21  import java.util.Collection;
22  import java.util.HashMap;
23  import java.util.Iterator;
24  import java.util.List;
25  import java.util.Map;
26  
27  import org.apache.commons.lang.StringUtils;
28  import org.apache.ojb.broker.query.Criteria;
29  import org.apache.ojb.broker.query.QueryByCriteria;
30  import org.apache.ojb.broker.query.QueryFactory;
31  import org.apache.ojb.broker.query.ReportQueryByCriteria;
32  import org.kuali.ole.coa.businessobject.Account;
33  import org.kuali.ole.gl.OJBUtility;
34  import org.kuali.ole.gl.businessobject.Balance;
35  import org.kuali.ole.gl.businessobject.Encumbrance;
36  import org.kuali.ole.sys.OLEConstants;
37  import org.kuali.ole.sys.OLEPropertyConstants;
38  import org.kuali.ole.sys.businessobject.GeneralLedgerPendingEntry;
39  import org.kuali.ole.sys.businessobject.SystemOptions;
40  import org.kuali.ole.sys.dataaccess.GeneralLedgerPendingEntryDao;
41  import org.kuali.ole.sys.util.TransactionalServiceUtils;
42  import org.kuali.rice.core.api.util.type.KualiDecimal;
43  import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
44  import org.kuali.rice.kns.lookup.LookupUtils;
45  
46  /**
47   *
48   *
49   */
50  public class GeneralLedgerPendingEntryDaoOjb extends PlatformAwareDaoBaseOjb implements GeneralLedgerPendingEntryDao {
51      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(GeneralLedgerPendingEntryDaoOjb.class);
52  
53      protected final static String TRANSACTION_LEDGER_ENTRY_SEQUENCE_NUMBER = "transactionLedgerEntrySequenceNumber";
54      protected final static String FINANCIAL_DOCUMENT_APPROVED_CODE = "financialDocumentApprovedCode";
55      protected final static String ACCOUNT_NUMBER = "accountNumber";
56      protected final static String DOC_NUMBER = "documentNumber";
57      protected final static String CHART_OF_ACCOUNTS_CODE = "chartOfAccountsCode";
58      protected final static String CHART_FINANCIAL_CASH_OBJECT_CODE = "chart.financialCashObjectCode";
59      protected final static String OBJECT_TYPE_FIN_OBJECT_TYPE_DEBITCREDIT_CD = "objectType.finObjectTypeDebitcreditCd";
60  
61      /**
62       * @see org.kuali.module.gl.dao.GeneralLedgerPendingEntryDao#getTransactionSummary(java.lang.Integer, java.lang.String,
63       *      java.lang.String, java.util.Collection, java.util.Collection, java.lang.String, boolean)
64       */
65      @Override
66      public KualiDecimal getTransactionSummary(Integer universityFiscalYear, String chartOfAccountsCode, String accountNumber, Collection objectTypeCodes, Collection balanceTypeCodes, String acctSufficientFundsFinObjCd, boolean isYearEnd) {
67          LOG.debug("getTransactionSummary() started");
68  
69          Criteria criteria = new Criteria();
70          criteria.addEqualTo(OLEConstants.UNIVERSITY_FISCAL_YEAR_PROPERTY_NAME, universityFiscalYear);
71          criteria.addEqualTo(OLEConstants.CHART_OF_ACCOUNTS_CODE_PROPERTY_NAME, chartOfAccountsCode);
72          criteria.addEqualTo(OLEConstants.ACCOUNT_NUMBER_PROPERTY_NAME, accountNumber);
73          criteria.addEqualTo("acctSufficientFundsFinObjCd", acctSufficientFundsFinObjCd);
74          criteria.addIn(OLEConstants.FINANCIAL_BALANCE_TYPE_CODE_PROPERTY_NAME, balanceTypeCodes);
75          criteria.addIn("financialObjectTypeCode", objectTypeCodes);
76  
77          if (isYearEnd) {
78              criteria.addLike("financialDocumentTypeCode", "YE%");
79          }
80          else {
81              criteria.addNotLike("financialDocumentTypeCode", "YE%");
82          }
83  
84          Collection status = new ArrayList();
85          status.add(OLEConstants.DocumentStatusCodes.CANCELLED);
86          status.add(OLEConstants.DocumentStatusCodes.DISAPPROVED);
87  
88          criteria.addNotIn(OLEConstants.DOCUMENT_HEADER_PROPERTY_NAME + "." + OLEConstants.DOCUMENT_HEADER_DOCUMENT_STATUS_CODE_PROPERTY_NAME, status);
89  
90          ReportQueryByCriteria reportQuery = QueryFactory.newReportQuery(this.getEntryClass(), criteria);
91          reportQuery.setAttributes(new String[] { "sum(" + OLEConstants.TRANSACTION_LEDGER_ENTRY_AMOUNT + ")" });
92  
93          KualiDecimal rv = null;
94          Iterator iterator = getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(reportQuery);
95          if (iterator.hasNext()) {
96              rv = (KualiDecimal) ((Object[]) TransactionalServiceUtils.retrieveFirstAndExhaustIterator(iterator))[0];
97          }
98          return (rv == null) ? KualiDecimal.ZERO : rv;
99      }
100 
101     /**
102      * @see org.kuali.module.gl.dao.GeneralLedgerPendingEntryDao#getTransactionSummary(java.lang.Integer, java.lang.String,
103      *      java.lang.String, java.util.Collection, java.util.Collection, boolean, boolean)
104      */
105     @Override
106     public KualiDecimal getTransactionSummary(Integer universityFiscalYear, String chartOfAccountsCode, String accountNumber, Collection objectTypeCodes, Collection balanceTypeCodes, String acctSufficientFundsFinObjCd, boolean isDebit, boolean isYearEnd) {
107         if ( LOG.isDebugEnabled() ) {
108             LOG.debug("getTransactionSummary(): " + universityFiscalYear + " / " + chartOfAccountsCode + " / " + accountNumber + " / " + objectTypeCodes + " / " + balanceTypeCodes + " / " + acctSufficientFundsFinObjCd + " / " + isDebit + " / " + isYearEnd );
109         }
110 
111         Criteria criteria = new Criteria();
112         criteria.addEqualTo(OLEConstants.UNIVERSITY_FISCAL_YEAR_PROPERTY_NAME, universityFiscalYear);
113         criteria.addEqualTo(OLEConstants.CHART_OF_ACCOUNTS_CODE_PROPERTY_NAME, chartOfAccountsCode);
114         criteria.addEqualTo(OLEConstants.ACCOUNT_NUMBER_PROPERTY_NAME, accountNumber);
115         criteria.addEqualTo("acctSufficientFundsFinObjCd", acctSufficientFundsFinObjCd);
116         criteria.addIn(OLEConstants.FINANCIAL_BALANCE_TYPE_CODE_PROPERTY_NAME, balanceTypeCodes);
117         criteria.addIn("financialObjectTypeCode", objectTypeCodes);
118 
119         if (isYearEnd) {
120             criteria.addLike("financialDocumentTypeCode", "YE%");
121         }
122         else {
123             criteria.addNotLike("financialDocumentTypeCode", "YE%");
124         }
125 
126         if (isDebit) {
127             criteria.addEqualTo(OLEConstants.TRANSACTION_DEBIT_CREDIT_CODE, OLEConstants.GL_DEBIT_CODE);
128         }
129         else {
130             criteria.addNotEqualTo(OLEConstants.TRANSACTION_DEBIT_CREDIT_CODE, OLEConstants.GL_DEBIT_CODE);
131         }
132 
133         Collection status = new ArrayList();
134         status.add(OLEConstants.DocumentStatusCodes.CANCELLED);
135         status.add(OLEConstants.DocumentStatusCodes.DISAPPROVED);
136 
137         criteria.addNotIn(OLEConstants.DOCUMENT_HEADER_PROPERTY_NAME + "." + OLEConstants.DOCUMENT_HEADER_DOCUMENT_STATUS_CODE_PROPERTY_NAME, status);
138 
139         ReportQueryByCriteria reportQuery = QueryFactory.newReportQuery(this.getEntryClass(), criteria);
140         reportQuery.setAttributes(new String[] { "sum(" + OLEConstants.TRANSACTION_LEDGER_ENTRY_AMOUNT + ")" });
141 
142         KualiDecimal rv = null;
143         Iterator iterator = getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(reportQuery);
144         if (iterator.hasNext()) {
145             rv = (KualiDecimal) ((Object[]) iterator.next())[0];
146         }
147         return (rv == null) ? KualiDecimal.ZERO : rv;
148     }
149 
150     /**
151      * @see org.kuali.module.gl.dao.GeneralLedgerPendingEntryDao#getTransactionSummary(java.util.Collection, java.lang.String,
152      *      java.lang.String, java.util.Collection, java.util.Collection, boolean)
153      */
154     @Override
155     public KualiDecimal getTransactionSummary(Collection universityFiscalYears, String chartOfAccountsCode, String accountNumber, Collection objectCodes, Collection balanceTypeCodes, boolean isDebit) {
156         LOG.debug("getTransactionSummary() started");
157 
158         Criteria criteria = new Criteria();
159         criteria.addIn(OLEConstants.UNIVERSITY_FISCAL_YEAR_PROPERTY_NAME, universityFiscalYears);
160         criteria.addEqualTo(OLEConstants.CHART_OF_ACCOUNTS_CODE_PROPERTY_NAME, chartOfAccountsCode);
161         criteria.addEqualTo(OLEConstants.ACCOUNT_NUMBER_PROPERTY_NAME, accountNumber);
162         criteria.addIn(OLEConstants.FINANCIAL_OBJECT_CODE_PROPERTY_NAME, objectCodes);
163         criteria.addIn(OLEConstants.FINANCIAL_BALANCE_TYPE_CODE_PROPERTY_NAME, balanceTypeCodes);
164 
165         if (isDebit) {
166             criteria.addEqualTo(OLEConstants.TRANSACTION_DEBIT_CREDIT_CODE, OLEConstants.GL_DEBIT_CODE);
167         }
168         else {
169             criteria.addNotEqualTo(OLEConstants.TRANSACTION_DEBIT_CREDIT_CODE, OLEConstants.GL_DEBIT_CODE);
170         }
171 
172         Collection status = new ArrayList();
173         status.add(OLEConstants.DocumentStatusCodes.CANCELLED);
174         status.add(OLEConstants.DocumentStatusCodes.DISAPPROVED);
175 
176         criteria.addNotIn(OLEConstants.DOCUMENT_HEADER_PROPERTY_NAME + "." + OLEConstants.DOCUMENT_HEADER_DOCUMENT_STATUS_CODE_PROPERTY_NAME, status);
177 
178         ReportQueryByCriteria reportQuery = QueryFactory.newReportQuery(this.getEntryClass(), criteria);
179         reportQuery.setAttributes(new String[] { "sum(" + OLEConstants.TRANSACTION_LEDGER_ENTRY_AMOUNT + ")" });
180 
181         KualiDecimal rv = null;
182         Iterator iterator = getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(reportQuery);
183         if (iterator.hasNext()) {
184             rv = (KualiDecimal) ((Object[]) TransactionalServiceUtils.retrieveFirstAndExhaustIterator(iterator))[0];
185         }
186         return (rv == null) ? KualiDecimal.ZERO : rv;
187     }
188 
189     /*
190      * (non-Javadoc)
191      * @see org.kuali.dao.GeneralLedgerPendingEntryDao#delete(Long)
192      */
193     @Override
194     public void delete(String documentHeaderId) {
195         LOG.debug("delete() started");
196 
197         if (documentHeaderId != null) {
198             Criteria criteria = new Criteria();
199             criteria.addEqualTo(OLEPropertyConstants.DOCUMENT_NUMBER, documentHeaderId);
200 
201             getPersistenceBrokerTemplate().deleteByQuery(QueryFactory.newQuery(this.getEntryClass(), criteria));
202             getPersistenceBrokerTemplate().clearCache();
203         }
204     }
205 
206     @Override
207     public void deleteByFinancialDocumentApprovedCode(String financialDocumentApprovedCode) {
208         LOG.debug("deleteByFinancialDocumentApprovedCode() started");
209 
210         Criteria criteria = new Criteria();
211         criteria.addEqualTo(FINANCIAL_DOCUMENT_APPROVED_CODE, financialDocumentApprovedCode);
212 
213         QueryByCriteria qbc = QueryFactory.newQuery(this.getEntryClass(), criteria);
214         getPersistenceBrokerTemplate().deleteByQuery(qbc);
215         getPersistenceBrokerTemplate().clearCache();
216     }
217 
218     /**
219      * @see org.kuali.module.gl.dao.GeneralLedgerPendingEntryDao#findApprovedPendingLedgerEntries()
220      */
221     @Override
222     public Iterator findApprovedPendingLedgerEntries() {
223         LOG.debug("findApprovedPendingLedgerEntries() started");
224 
225         // only process the document for which document status code is A (approved)
226         Criteria criteria = new Criteria();
227         criteria.addEqualTo("financialDocumentApprovedCode", OLEConstants.PENDING_ENTRY_APPROVED_STATUS_CODE.APPROVED);
228 
229         QueryByCriteria query = QueryFactory.newQuery(this.getEntryClass(), criteria);
230         return getPersistenceBrokerTemplate().getIteratorByQuery(query);
231     }
232 
233     /**
234      * @see org.kuali.module.gl.dao.GeneralLedgerPendingEntryDao#countPendingLedgerEntries(org.kuali.ole.coa.businessobject.Account)
235      */
236     @Override
237     public int countPendingLedgerEntries(Account account) {
238         LOG.debug("findPendingLedgerEntries(Account) started");
239 
240         Criteria criteria = new Criteria();
241         criteria.addEqualTo(ACCOUNT_NUMBER, account.getAccountNumber());
242         criteria.addEqualTo(CHART_OF_ACCOUNTS_CODE, account.getChartOfAccountsCode());
243 
244         ReportQueryByCriteria query = QueryFactory.newReportQuery(this.getEntryClass(), criteria);
245         query.setAttributes(new String[] { "count(*)" });
246         Iterator i = getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(query);
247         if (i.hasNext()) {
248             Object[] values = (Object[]) TransactionalServiceUtils.retrieveFirstAndExhaustIterator(i);
249             if (values[0] instanceof BigDecimal) {
250                 return ((BigDecimal) values[0]).intValue();
251             }
252             else {
253                 return ((Long) values[0]).intValue();
254             }
255         }
256         else {
257             return 0;
258         }
259     }
260 
261     /**
262      * @see org.kuali.module.gl.dao.GeneralLedgerPendingEntryDao#findPendingLedgerEntries(org.kuali.ole.gl.businessobject.Encumbrance,
263      *      boolean)
264      */
265     @Override
266     public Iterator findPendingLedgerEntries(Encumbrance encumbrance, boolean isApproved) {
267         LOG.debug("findPendingLedgerEntries(Encumbrance, boolean) started");
268 
269         // find pending ledger entry by the primary key fields of encumbrance
270         Criteria criteria = new Criteria();
271         criteria.addEqualTo(OLEPropertyConstants.UNIVERSITY_FISCAL_YEAR, encumbrance.getUniversityFiscalYear());
272         criteria.addEqualTo(OLEPropertyConstants.CHART_OF_ACCOUNTS_CODE, encumbrance.getChartOfAccountsCode());
273         criteria.addEqualTo(OLEPropertyConstants.ACCOUNT_NUMBER, encumbrance.getAccountNumber());
274         criteria.addEqualTo(OLEPropertyConstants.SUB_ACCOUNT_NUMBER, encumbrance.getSubAccountNumber());
275         criteria.addEqualTo(OLEPropertyConstants.FINANCIAL_OBJECT_CODE, encumbrance.getObjectCode());
276         criteria.addEqualTo(OLEPropertyConstants.FINANCIAL_SUB_OBJECT_CODE, encumbrance.getSubObjectCode());
277         criteria.addEqualTo(OLEPropertyConstants.FINANCIAL_BALANCE_TYPE_CODE, encumbrance.getBalanceTypeCode());
278         criteria.addEqualTo(OLEPropertyConstants.FINANCIAL_DOCUMENT_TYPE_CODE, encumbrance.getDocumentTypeCode());
279         criteria.addEqualTo(OLEPropertyConstants.ORGANIZATION_DOCUMENT_NUMBER, encumbrance.getDocumentNumber());
280 
281         // add the status codes into the criteria
282         this.addStatusCode(criteria, isApproved);
283 
284         // Criteria: (originCode=originationCode OR originCode=originationReferenceCode)
285         Criteria criteria1 = new Criteria();
286         Criteria criteria2 = new Criteria();
287         criteria1.addEqualTo(OLEPropertyConstants.FINANCIAL_SYSTEM_ORIGINATION_CODE, encumbrance.getOriginCode());
288         criteria2.addEqualTo(OLEPropertyConstants.REFERENCE_FINANCIAL_SYSTEM_ORIGINATION_CODE, encumbrance.getOriginCode());
289         criteria1.addOrCriteria(criteria2);
290 
291         // combine all criteria together
292         criteria.addAndCriteria(criteria1);
293 
294         QueryByCriteria query = QueryFactory.newQuery(this.getEntryClass(), criteria);
295         return getPersistenceBrokerTemplate().getIteratorByQuery(query);
296     }
297 
298     /**
299      * @see org.kuali.module.gl.dao.GeneralLedgerPendingEntryDao#findPendingLedgerEntries(org.kuali.ole.gl.businessobject.Balance,
300      *      boolean, boolean)
301      */
302     @Override
303     public Iterator findPendingLedgerEntries(Balance balance, boolean isApproved, boolean isConsolidated) {
304         LOG.debug("findPendingLedgerEntries(Balance, boolean, boolean) started");
305 
306         // find pending ledger entry by the primary key fields of balance
307         Criteria criteria = new Criteria();
308         criteria.addEqualTo(OLEPropertyConstants.UNIVERSITY_FISCAL_YEAR, balance.getUniversityFiscalYear());
309         criteria.addEqualTo(OLEPropertyConstants.CHART_OF_ACCOUNTS_CODE, balance.getChartOfAccountsCode());
310         criteria.addEqualTo(OLEPropertyConstants.ACCOUNT_NUMBER, balance.getAccountNumber());
311         criteria.addEqualTo(OLEPropertyConstants.FINANCIAL_OBJECT_CODE, balance.getObjectCode());
312         criteria.addEqualTo(OLEPropertyConstants.FINANCIAL_BALANCE_TYPE_CODE, balance.getBalanceTypeCode());
313 
314         if (!isConsolidated) {
315             criteria.addEqualTo(OLEPropertyConstants.SUB_ACCOUNT_NUMBER, balance.getSubAccountNumber());
316         }
317 
318         // add the status codes into the criteria
319         this.addStatusCode(criteria, isApproved);
320 
321         QueryByCriteria query = QueryFactory.newQuery(this.getEntryClass(), criteria);
322         return getPersistenceBrokerTemplate().getIteratorByQuery(query);
323     }
324 
325     /**
326      * @see org.kuali.ole.sys.dataaccess.GeneralLedgerPendingEntryDao#findPendingLedgerEntriesForEntry(java.util.Map, boolean,
327      *      java.lang.String, int)
328      */
329     @Override
330     public Iterator findPendingLedgerEntriesForEntry(Map fieldValues, boolean isApproved, String currentFiscalPeriodCode, int currentFY, List<String> encumbranceBalanceTypes) {
331         LOG.debug("findPendingLedgerEntriesForEntry started");
332 
333         Criteria criteria = buildCriteriaFromMap(fieldValues, new GeneralLedgerPendingEntry(), currentFiscalPeriodCode, currentFY, encumbranceBalanceTypes);
334 
335         // add the status codes into the criteria
336         this.addStatusCode(criteria, isApproved);
337 
338         LookupUtils.applySearchResultsLimit(this.getEntryClass(), criteria, getDbPlatform());
339 
340         QueryByCriteria query = QueryFactory.newQuery(this.getEntryClass(), criteria);
341         return getPersistenceBrokerTemplate().getIteratorByQuery(query);
342     }
343 
344     /**
345      * @see org.kuali.ole.sys.dataaccess.GeneralLedgerPendingEntryDao#findPendingLedgerEntriesForBalance(java.util.Map, boolean,
346      *      java.lang.String, int)
347      */
348     @Override
349     public Iterator findPendingLedgerEntriesForBalance(Map fieldValues, boolean isApproved, String currentFiscalPeriodCode, int currentFY, List<String> encumbranceBalanceTypes) {
350         LOG.debug("findPendingLedgerEntriesForBalance started");
351 
352         Criteria criteria = buildCriteriaFromMap(fieldValues, this.getEntryClassInstance(), currentFiscalPeriodCode, currentFY, encumbranceBalanceTypes);
353 
354         // add the status codes into the criteria
355         this.addStatusCode(criteria, isApproved);
356 
357         QueryByCriteria query = QueryFactory.newQuery(this.getEntryClass(), criteria);
358         return getPersistenceBrokerTemplate().getIteratorByQuery(query);
359     }
360 
361     /**
362      * @see org.kuali.ole.sys.dataaccess.GeneralLedgerPendingEntryDao#findPendingLedgerEntriesForCashBalance(java.util.Map, boolean,
363      *      java.lang.String, int)
364      */
365     @Override
366     public Iterator findPendingLedgerEntriesForCashBalance(Map fieldValues, boolean isApproved, String currentFiscalPeriodCode, int currentFiscalYear, List<String> encumbranceBalanceType) {
367         LOG.debug("findPendingLedgerEntriesForCashBalance started");
368 
369         Criteria criteria = buildCriteriaFromMap(fieldValues, this.getEntryClassInstance(), currentFiscalPeriodCode, currentFiscalYear, encumbranceBalanceType);
370         criteria.addEqualTo(OLEPropertyConstants.FINANCIAL_BALANCE_TYPE_CODE, "AC");
371         criteria.addEqualToField(OLEPropertyConstants.FINANCIAL_OBJECT_CODE, CHART_FINANCIAL_CASH_OBJECT_CODE);
372 
373         // add the status codes into the criteria
374         this.addStatusCode(criteria, isApproved);
375 
376         QueryByCriteria query = QueryFactory.newQuery(this.getEntryClass(), criteria);
377         return getPersistenceBrokerTemplate().getIteratorByQuery(query);
378     }
379 
380 
381     /**
382      * @see org.kuali.ole.sys.dataaccess.GeneralLedgerPendingEntryDao#findPendingLedgerEntriesForEncumbrance(java.util.Map, boolean,
383      *      java.lang.String, int, org.kuali.ole.sys.businessobject.SystemOptions, java.util.List)
384      */
385     @Override
386     public Iterator findPendingLedgerEntriesForEncumbrance(Map fieldValues, boolean isApproved, String currentFiscalPeriodCode, int currentFiscalYear, SystemOptions currentYearOptions, List<String> encumbranceBalanceTypes) {
387         LOG.debug("findPendingLedgerEntriesForEncumbrance started");
388 
389         String docNumber = (String)fieldValues.get(DOC_NUMBER);
390         Criteria subCriteria1 = new Criteria();
391         boolean docNbrCriteriaNeeded = false;
392         if (StringUtils.isNotBlank(docNumber)) {
393             fieldValues.remove(DOC_NUMBER);
394             subCriteria1 = new Criteria();
395             subCriteria1.addEqualTo(OLEPropertyConstants.TRANSACTION_ENCUMBRANCE_UPDT_CD, OLEConstants.ENCUMB_UPDT_REFERENCE_DOCUMENT_CD);
396             Criteria subCriteria1b = new Criteria();
397             subCriteria1b.addEqualTo(OLEPropertyConstants.REFERENCE_FINANCIAL_DOCUMENT_NUMBER, docNumber);
398             subCriteria1.addAndCriteria(subCriteria1b);           
399             Criteria subCriteria2 = new Criteria();
400             subCriteria2.addEqualTo(OLEPropertyConstants.TRANSACTION_ENCUMBRANCE_UPDT_CD, OLEConstants.ENCUMB_UPDT_DOCUMENT_CD);
401             Criteria subCriteria2b = new Criteria();
402             subCriteria2b.addEqualTo(DOC_NUMBER, docNumber);
403             subCriteria2.addAndCriteria(subCriteria2b);           
404             subCriteria1.addOrCriteria(subCriteria2);
405             docNbrCriteriaNeeded = true;
406         }
407 
408         Criteria criteria = buildCriteriaFromMap(fieldValues, this.getEntryClassInstance(), currentFiscalPeriodCode, currentFiscalYear, encumbranceBalanceTypes);
409         criteria.addIn(OLEPropertyConstants.FINANCIAL_BALANCE_TYPE_CODE, Arrays.asList(OLEConstants.ENCUMBRANCE_BALANCE_TYPE));
410 
411         List encumbranceUpdateCodeList = new ArrayList();
412         encumbranceUpdateCodeList.add(OLEConstants.ENCUMB_UPDT_REFERENCE_DOCUMENT_CD);
413         encumbranceUpdateCodeList.add(OLEConstants.ENCUMB_UPDT_DOCUMENT_CD);
414         criteria.addIn(OLEPropertyConstants.TRANSACTION_ENCUMBRANCE_UPDT_CD, encumbranceUpdateCodeList);
415 
416         // add the status codes into the criteria
417         this.addStatusCode(criteria, isApproved);
418 
419         // add criteria to exclude fund balance object type code
420         criteria.addAndCriteria(buildCriteriaToExcludeFundBalance(currentYearOptions));
421 
422         if (docNbrCriteriaNeeded) {
423             criteria.addAndCriteria(subCriteria1);
424         }
425 
426         QueryByCriteria query = QueryFactory.newQuery(this.getEntryClass(), criteria);
427         return getPersistenceBrokerTemplate().getIteratorByQuery(query);
428     }
429 
430     /**
431      * This method creates Criteria that exclude the fund balance object type from the result.
432      *
433      * @return Criteria
434      */
435     protected Criteria buildCriteriaToExcludeFundBalance(SystemOptions currentYearOptions) {
436 
437         String fundBalanceObjectTypeCode = currentYearOptions.getFinObjectTypeFundBalanceCd();
438 
439         Criteria criteria = new Criteria();
440         criteria.addNotEqualTo(OLEPropertyConstants.FINANCIAL_OBJECT_TYPE_CODE, fundBalanceObjectTypeCode);
441         return criteria;
442     }
443 
444 
445     /**
446      * @see org.kuali.ole.sys.dataaccess.GeneralLedgerPendingEntryDao#findPendingLedgerEntriesForAccountBalance(Map, boolean,
447      *      String, int, List)
448      */
449     @Override
450     public Iterator findPendingLedgerEntriesForAccountBalance(Map fieldValues, boolean isApproved, String currentFiscalPeriodCode, int currentFiscalYear, List<String> encumbranceBalanceTypes) {
451         LOG.debug("findPendingLedgerEntriesForAccountBalance started");
452 
453         Criteria criteria = buildCriteriaFromMap(fieldValues, this.getEntryClassInstance(), currentFiscalPeriodCode, currentFiscalYear, encumbranceBalanceTypes);
454 
455         // add the status codes into the criteria
456         this.addStatusCode(criteria, isApproved);
457 
458         QueryByCriteria query = QueryFactory.newQuery(this.getEntryClass(), criteria);
459         return getPersistenceBrokerTemplate().getIteratorByQuery(query);
460     }
461 
462 
463     /**
464      * @see org.kuali.ole.sys.dataaccess.GeneralLedgerPendingEntryDao#findPendingLedgerEntrySummaryForAccountBalance(java.util.Map,
465      *      boolean, java.lang.String, int, java.util.List)
466      */
467     @Override
468     public Iterator findPendingLedgerEntrySummaryForAccountBalance(Map fieldValues, boolean isApproved, String currentFiscalPeriodCode, int currentFiscalYear, List<String> encumbranceBalanceTypes) {
469         LOG.debug("findPendingLedgerEntrySummaryForAccountBalance started");
470 
471         Criteria criteria = buildCriteriaFromMap(fieldValues, this.getEntryClassInstance(), currentFiscalPeriodCode, currentFiscalYear, encumbranceBalanceTypes);
472 
473         // add the status codes into the criteria
474         this.addStatusCode(criteria, isApproved);
475 
476         ReportQueryByCriteria query = QueryFactory.newReportQuery(this.getEntryClass(), criteria);
477 
478         List attributeList = buildAttributeList();
479         List groupByList = buildGroupList();
480 
481         // set the selection attributes
482         String[] attributes = (String[]) attributeList.toArray(new String[attributeList.size()]);
483         query.setAttributes(attributes);
484 
485         // add the group criteria into the selection statement
486         String[] groupBy = (String[]) groupByList.toArray(new String[groupByList.size()]);
487         query.addGroupBy(groupBy);
488 
489         return getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(query);
490     }
491 
492     /**
493      * This method builds the atrribute list used by balance searching
494      *
495      * @return List an attribute list
496      */
497     protected List buildAttributeList() {
498         List attributeList = buildGroupList();
499         attributeList.add("sum(" + OLEPropertyConstants.TRANSACTION_LEDGER_ENTRY_AMOUNT + ")");
500         return attributeList;
501     }
502 
503     /**
504      * This method builds group by attribute list used by balance searching
505      *
506      * @return List an group by attribute list
507      */
508     protected List buildGroupList() {
509         List groupList = new ArrayList();
510 
511         groupList.add(OLEPropertyConstants.FINANCIAL_OBJECT_CODE);
512         groupList.add(OLEPropertyConstants.FINANCIAL_DOCUMENT_APPROVED_CODE);
513         groupList.add(OLEPropertyConstants.TRANSACTION_DEBIT_CREDIT_CODE);
514         groupList.add(OBJECT_TYPE_FIN_OBJECT_TYPE_DEBITCREDIT_CD);
515         return groupList;
516     }
517 
518     /**
519      * add the status code into the given criteria. The status code can be categorized into approved and all.
520      *
521      * @param criteria the given criteria
522      * @param isApproved the flag that indictates if only approved status code can be added into the given searach criteria
523      */
524     protected void addStatusCode(Criteria criteria, boolean isOnlyApproved) {
525         // add criteria for the approved pending entries
526         if (isOnlyApproved) {
527             criteria.addIn("documentHeader.financialDocumentStatusCode", this.buildApprovalCodeList());
528             criteria.addNotEqualTo(OLEPropertyConstants.FINANCIAL_DOCUMENT_APPROVED_CODE, OLEConstants.PENDING_ENTRY_APPROVED_STATUS_CODE.PROCESSED);
529         }
530         else {
531             Criteria subCriteria1 = new Criteria();
532             subCriteria1.addNotEqualTo(OLEPropertyConstants.FINANCIAL_DOCUMENT_APPROVED_CODE, OLEConstants.PENDING_ENTRY_APPROVED_STATUS_CODE.PROCESSED);
533 
534             Criteria subCriteria2 = new Criteria();
535             subCriteria2.addIsNull(OLEPropertyConstants.FINANCIAL_DOCUMENT_APPROVED_CODE);
536 
537             subCriteria1.addOrCriteria(subCriteria2);
538             criteria.addAndCriteria(subCriteria1);
539         }
540     }
541 
542     /**
543      * build a status code list including the legal approval codes
544      *
545      * @return an approval code list
546      */
547     protected List buildApprovalCodeList() {
548         List approvalCodeList = new ArrayList();
549 
550         approvalCodeList.add(OLEConstants.DocumentStatusCodes.APPROVED);
551         return approvalCodeList;
552     }
553 
554     /**
555      * This method builds an OJB query criteria based on the input field map
556      *
557      * @param fieldValues the input field map
558      * @param businessObject the given business object
559      * @return an OJB query criteria
560      */
561     public Criteria buildCriteriaFromMap(Map fieldValues, Object businessObject, String currentFiscalPeriodCode, Integer currentFiscalYear, List<String> encumbranceBalanceTypes) {
562         Criteria criteria = new Criteria();
563 
564         // deal with null fiscal year and fiscal period code as current fiscal year and period code respectively
565         String fiscalPeriodFromForm = null;
566         if (fieldValues.containsKey(OLEPropertyConstants.UNIVERSITY_FISCAL_PERIOD_CODE)) {
567             fiscalPeriodFromForm = (String) fieldValues.get(OLEPropertyConstants.UNIVERSITY_FISCAL_PERIOD_CODE);
568         }
569 
570         String fiscalYearFromForm = null;
571         if (fieldValues.containsKey(OLEPropertyConstants.UNIVERSITY_FISCAL_YEAR)) {
572             fiscalYearFromForm = (String) fieldValues.get(OLEPropertyConstants.UNIVERSITY_FISCAL_YEAR);
573         }
574 
575         boolean includeNullFiscalPeriodCodeInLookup = null != currentFiscalPeriodCode && currentFiscalPeriodCode.equals(fiscalPeriodFromForm);
576         boolean includeNullFiscalYearInLookup = null != currentFiscalYear && currentFiscalYear.toString().equals(fiscalYearFromForm);
577 
578         if (includeNullFiscalPeriodCodeInLookup) {
579             Criteria apValueCriteria = new Criteria();
580             apValueCriteria.addLike(OLEPropertyConstants.UNIVERSITY_FISCAL_PERIOD_CODE, fiscalPeriodFromForm);
581 
582             Criteria apNullCriteria = new Criteria();
583             apNullCriteria.addIsNull(OLEPropertyConstants.UNIVERSITY_FISCAL_PERIOD_CODE);
584 
585             apValueCriteria.addOrCriteria(apNullCriteria);
586             criteria.addAndCriteria(apValueCriteria);
587 
588         }
589 
590         if (includeNullFiscalYearInLookup) {
591             Criteria fyValueCriteria = new Criteria();
592             fyValueCriteria.addEqualTo(OLEPropertyConstants.UNIVERSITY_FISCAL_YEAR, fiscalYearFromForm);
593 
594             Criteria fyNullCriteria = new Criteria();
595             fyNullCriteria.addIsNull(OLEPropertyConstants.UNIVERSITY_FISCAL_YEAR);
596 
597             fyValueCriteria.addOrCriteria(fyNullCriteria);
598             criteria.addAndCriteria(fyValueCriteria);
599         }
600 
601         // handle encumbrance balance type
602         Map<String, Object> localFieldValues = new HashMap();
603         localFieldValues.putAll(fieldValues);
604 
605         // we've already taken care of these fields...
606         if (includeNullFiscalPeriodCodeInLookup) {
607             localFieldValues.remove(OLEPropertyConstants.UNIVERSITY_FISCAL_PERIOD_CODE);
608         }
609         if (includeNullFiscalYearInLookup) {
610             localFieldValues.remove(OLEPropertyConstants.UNIVERSITY_FISCAL_YEAR);
611         }
612 
613         String propertyName = OLEPropertyConstants.FINANCIAL_BALANCE_TYPE_CODE;
614         if (localFieldValues.containsKey(propertyName)) {
615             String propertyValue = (String) fieldValues.get(propertyName);
616             if (OLEConstants.AGGREGATE_ENCUMBRANCE_BALANCE_TYPE_CODE.equals(propertyValue)) {
617                 localFieldValues.remove(OLEPropertyConstants.FINANCIAL_BALANCE_TYPE_CODE);
618                 criteria.addIn(OLEPropertyConstants.FINANCIAL_BALANCE_TYPE_CODE, encumbranceBalanceTypes);
619             }
620         }
621 
622         // remove dummyBusinessObject references - no longer needed
623         List<String> keysToRemove = new ArrayList<String>();
624         for (String key : localFieldValues.keySet()) {
625             if (key.startsWith("dummyBusinessObject.")) {
626                 keysToRemove.add(key);
627             }
628         }
629         for (String key : keysToRemove) {
630             localFieldValues.remove(key);
631         }
632 
633         criteria.addAndCriteria(OJBUtility.buildCriteriaFromMap(localFieldValues, businessObject));
634         return criteria;
635     }
636 
637     /**
638      * @see org.kuali.ole.sys.dataaccess.GeneralLedgerPendingEntryDao#findPendingEntries(java.util.Map, boolean, java.lang.String,
639      *      int, java.util.List)
640      */
641     @Override
642     public Collection findPendingEntries(Map fieldValues, boolean isApproved, String currentFiscalPeriodCode, int currentFiscalYear, List<String> encumbranceBalanceTypes) {
643         LOG.debug("findPendingEntries(Map, boolean) started");
644 
645         Criteria criteria = buildCriteriaFromMap(fieldValues, this.getEntryClassInstance(), currentFiscalPeriodCode, currentFiscalYear, encumbranceBalanceTypes);
646 
647         // add the status codes into the criteria
648         this.addStatusCode(criteria, isApproved);
649 
650         LookupUtils.applySearchResultsLimit(this.getEntryClass(), criteria, getDbPlatform());
651 
652         QueryByCriteria query = QueryFactory.newQuery(this.getEntryClass(), criteria);
653         return getPersistenceBrokerTemplate().getCollectionByQuery(query);
654     }
655 
656     /**
657      * Gets the entryClass attribute.
658      *
659      * @return Returns the entryClass.
660      */
661     public Class getEntryClass() {
662         return GeneralLedgerPendingEntry.class;
663     }
664 
665     protected Object getEntryClassInstance() {
666         Object entryObject = null;
667         try {
668             entryObject = getEntryClass().newInstance();
669         }
670         catch (Exception e) {
671             if (LOG.isDebugEnabled()) {
672                 LOG.debug("Wrong object type" + e);
673             }
674         }
675         return entryObject;
676     }
677 }