001: /*
002: * Copyright 2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.opensource.org/licenses/ecl1.php
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.kuali.kfs.dao.ojb;
017:
018: import java.math.BigDecimal;
019: import java.util.ArrayList;
020: import java.util.Arrays;
021: import java.util.Collection;
022: import java.util.HashMap;
023: import java.util.Iterator;
024: import java.util.List;
025: import java.util.Map;
026:
027: import org.apache.commons.lang.StringUtils;
028: import org.apache.ojb.broker.query.Criteria;
029: import org.apache.ojb.broker.query.QueryByCriteria;
030: import org.apache.ojb.broker.query.QueryFactory;
031: import org.apache.ojb.broker.query.ReportQueryByCriteria;
032: import org.kuali.core.dao.ojb.PlatformAwareDaoBaseOjb;
033: import org.kuali.core.lookup.LookupUtils;
034: import org.kuali.core.util.KualiDecimal;
035: import org.kuali.core.util.TransactionalServiceUtils;
036: import org.kuali.kfs.KFSConstants;
037: import org.kuali.kfs.KFSPropertyConstants;
038: import org.kuali.kfs.bo.GeneralLedgerPendingEntry;
039: import org.kuali.kfs.context.SpringContext;
040: import org.kuali.kfs.dao.GeneralLedgerPendingEntryDao;
041: import org.kuali.kfs.service.ParameterService;
042: import org.kuali.module.chart.bo.Account;
043: import org.kuali.module.chart.service.BalanceTypService;
044: import org.kuali.module.financial.service.UniversityDateService;
045: import org.kuali.module.gl.bo.Balance;
046: import org.kuali.module.gl.bo.Encumbrance;
047: import org.kuali.module.gl.bo.UniversityDate;
048: import org.kuali.module.gl.util.OJBUtility;
049:
050: /**
051: *
052: *
053: */
054: public class GeneralLedgerPendingEntryDaoOjb extends
055: PlatformAwareDaoBaseOjb implements GeneralLedgerPendingEntryDao {
056: private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
057: .getLogger(GeneralLedgerPendingEntryDaoOjb.class);
058:
059: private final static String TRANSACTION_LEDGER_ENTRY_SEQUENCE_NUMBER = "transactionLedgerEntrySequenceNumber";
060: private final static String FINANCIAL_DOCUMENT_APPROVED_CODE = "financialDocumentApprovedCode";
061: private final static String ACCOUNT_NUMBER = "accountNumber";
062: private final static String CHART_OF_ACCOUNTS_CODE = "chartOfAccountsCode";
063: private final static String CHART_FINANCIAL_CASH_OBJECT_CODE = "chart.financialCashObjectCode";
064: private final static String OBJECT_TYPE_FIN_OBJECT_TYPE_DEBITCREDIT_CD = "objectType.finObjectTypeDebitcreditCd";
065:
066: private ParameterService parameterService;
067: private BalanceTypService balanceTypService;
068:
069: /**
070: * @see org.kuali.module.gl.dao.GeneralLedgerPendingEntryDao#getTransactionSummary(java.lang.Integer, java.lang.String,
071: * java.lang.String, java.util.Collection, java.util.Collection, java.lang.String, boolean)
072: */
073: public KualiDecimal getTransactionSummary(
074: Integer universityFiscalYear, String chartOfAccountsCode,
075: String accountNumber, Collection objectTypeCodes,
076: Collection balanceTypeCodes,
077: String acctSufficientFundsFinObjCd, boolean isYearEnd) {
078: LOG.debug("getTransactionSummary() started");
079:
080: Criteria criteria = new Criteria();
081: criteria.addEqualTo(
082: KFSConstants.UNIVERSITY_FISCAL_YEAR_PROPERTY_NAME,
083: universityFiscalYear);
084: criteria.addEqualTo(
085: KFSConstants.CHART_OF_ACCOUNTS_CODE_PROPERTY_NAME,
086: chartOfAccountsCode);
087: criteria.addEqualTo(KFSConstants.ACCOUNT_NUMBER_PROPERTY_NAME,
088: accountNumber);
089: criteria.addEqualTo("acctSufficientFundsFinObjCd",
090: acctSufficientFundsFinObjCd);
091: criteria.addIn(
092: KFSConstants.FINANCIAL_BALANCE_TYPE_CODE_PROPERTY_NAME,
093: balanceTypeCodes);
094: criteria.addIn("financialObjectTypeCode", objectTypeCodes);
095:
096: if (isYearEnd) {
097: criteria.addLike("financialDocumentTypeCode", "YE%");
098: } else {
099: criteria.addNotLike("financialDocumentTypeCode", "YE%");
100: }
101:
102: Collection status = new ArrayList();
103: status.add(KFSConstants.DocumentStatusCodes.CANCELLED);
104: status.add(KFSConstants.DocumentStatusCodes.DISAPPROVED);
105:
106: criteria
107: .addNotIn(
108: KFSConstants.DOCUMENT_HEADER_PROPERTY_NAME
109: + "."
110: + KFSConstants.DOCUMENT_HEADER_DOCUMENT_STATUS_CODE_PROPERTY_NAME,
111: status);
112:
113: ReportQueryByCriteria reportQuery = QueryFactory
114: .newReportQuery(this .getEntryClass(), criteria);
115: reportQuery.setAttributes(new String[] { "sum("
116: + KFSConstants.TRANSACTION_LEDGER_ENTRY_AMOUNT + ")" });
117:
118: KualiDecimal rv = null;
119: Iterator iterator = getPersistenceBrokerTemplate()
120: .getReportQueryIteratorByQuery(reportQuery);
121: if (iterator.hasNext()) {
122: rv = (KualiDecimal) ((Object[]) TransactionalServiceUtils
123: .retrieveFirstAndExhaustIterator(iterator))[0];
124: }
125: return (rv == null) ? KualiDecimal.ZERO : rv;
126: }
127:
128: /**
129: * @see org.kuali.module.gl.dao.GeneralLedgerPendingEntryDao#getTransactionSummary(java.lang.Integer, java.lang.String,
130: * java.lang.String, java.util.Collection, java.util.Collection, boolean, boolean)
131: */
132: public KualiDecimal getTransactionSummary(
133: Integer universityFiscalYear, String chartOfAccountsCode,
134: String accountNumber, Collection objectTypeCodes,
135: Collection balanceTypeCodes,
136: String acctSufficientFundsFinObjCd, boolean isDebit,
137: boolean isYearEnd) {
138: LOG.debug("getTransactionSummary() started");
139:
140: Criteria criteria = new Criteria();
141: criteria.addEqualTo(
142: KFSConstants.UNIVERSITY_FISCAL_YEAR_PROPERTY_NAME,
143: universityFiscalYear);
144: criteria.addEqualTo(
145: KFSConstants.CHART_OF_ACCOUNTS_CODE_PROPERTY_NAME,
146: chartOfAccountsCode);
147: criteria.addEqualTo(KFSConstants.ACCOUNT_NUMBER_PROPERTY_NAME,
148: accountNumber);
149: criteria.addEqualTo("acctSufficientFundsFinObjCd",
150: acctSufficientFundsFinObjCd);
151: criteria.addIn(
152: KFSConstants.FINANCIAL_BALANCE_TYPE_CODE_PROPERTY_NAME,
153: balanceTypeCodes);
154: criteria.addIn("financialObjectTypeCode", objectTypeCodes);
155:
156: if (isYearEnd) {
157: criteria.addLike("financialDocumentTypeCode", "YE%");
158: } else {
159: criteria.addNotLike("financialDocumentTypeCode", "YE%");
160: }
161:
162: if (isDebit) {
163: criteria.addEqualTo(
164: KFSConstants.TRANSACTION_DEBIT_CREDIT_CODE,
165: KFSConstants.GL_DEBIT_CODE);
166: } else {
167: criteria.addNotEqualTo(
168: KFSConstants.TRANSACTION_DEBIT_CREDIT_CODE,
169: KFSConstants.GL_DEBIT_CODE);
170: }
171:
172: Collection status = new ArrayList();
173: status.add(KFSConstants.DocumentStatusCodes.CANCELLED);
174: status.add(KFSConstants.DocumentStatusCodes.DISAPPROVED);
175:
176: criteria
177: .addNotIn(
178: KFSConstants.DOCUMENT_HEADER_PROPERTY_NAME
179: + "."
180: + KFSConstants.DOCUMENT_HEADER_DOCUMENT_STATUS_CODE_PROPERTY_NAME,
181: status);
182:
183: ReportQueryByCriteria reportQuery = QueryFactory
184: .newReportQuery(this .getEntryClass(), criteria);
185: reportQuery.setAttributes(new String[] { "sum("
186: + KFSConstants.TRANSACTION_LEDGER_ENTRY_AMOUNT + ")" });
187:
188: KualiDecimal rv = null;
189: Iterator iterator = getPersistenceBrokerTemplate()
190: .getReportQueryIteratorByQuery(reportQuery);
191: if (iterator.hasNext()) {
192: rv = (KualiDecimal) ((Object[]) iterator.next())[0];
193: }
194: return (rv == null) ? KualiDecimal.ZERO : rv;
195: }
196:
197: /**
198: * @see org.kuali.module.gl.dao.GeneralLedgerPendingEntryDao#getTransactionSummary(java.util.Collection, java.lang.String,
199: * java.lang.String, java.util.Collection, java.util.Collection, boolean)
200: */
201: public KualiDecimal getTransactionSummary(
202: Collection universityFiscalYears,
203: String chartOfAccountsCode, String accountNumber,
204: Collection objectCodes, Collection balanceTypeCodes,
205: boolean isDebit) {
206: LOG.debug("getTransactionSummary() started");
207:
208: Criteria criteria = new Criteria();
209: criteria.addIn(
210: KFSConstants.UNIVERSITY_FISCAL_YEAR_PROPERTY_NAME,
211: universityFiscalYears);
212: criteria.addEqualTo(
213: KFSConstants.CHART_OF_ACCOUNTS_CODE_PROPERTY_NAME,
214: chartOfAccountsCode);
215: criteria.addEqualTo(KFSConstants.ACCOUNT_NUMBER_PROPERTY_NAME,
216: accountNumber);
217: criteria.addIn(
218: KFSConstants.FINANCIAL_OBJECT_CODE_PROPERTY_NAME,
219: objectCodes);
220: criteria.addIn(
221: KFSConstants.FINANCIAL_BALANCE_TYPE_CODE_PROPERTY_NAME,
222: balanceTypeCodes);
223:
224: if (isDebit) {
225: criteria.addEqualTo(
226: KFSConstants.TRANSACTION_DEBIT_CREDIT_CODE,
227: KFSConstants.GL_DEBIT_CODE);
228: } else {
229: criteria.addNotEqualTo(
230: KFSConstants.TRANSACTION_DEBIT_CREDIT_CODE,
231: KFSConstants.GL_DEBIT_CODE);
232: }
233:
234: Collection status = new ArrayList();
235: status.add(KFSConstants.DocumentStatusCodes.CANCELLED);
236: status.add(KFSConstants.DocumentStatusCodes.DISAPPROVED);
237:
238: criteria
239: .addNotIn(
240: KFSConstants.DOCUMENT_HEADER_PROPERTY_NAME
241: + "."
242: + KFSConstants.DOCUMENT_HEADER_DOCUMENT_STATUS_CODE_PROPERTY_NAME,
243: status);
244:
245: ReportQueryByCriteria reportQuery = QueryFactory
246: .newReportQuery(this .getEntryClass(), criteria);
247: reportQuery.setAttributes(new String[] { "sum("
248: + KFSConstants.TRANSACTION_LEDGER_ENTRY_AMOUNT + ")" });
249:
250: KualiDecimal rv = null;
251: Iterator iterator = getPersistenceBrokerTemplate()
252: .getReportQueryIteratorByQuery(reportQuery);
253: if (iterator.hasNext()) {
254: rv = (KualiDecimal) ((Object[]) TransactionalServiceUtils
255: .retrieveFirstAndExhaustIterator(iterator))[0];
256: }
257: return (rv == null) ? KualiDecimal.ZERO : rv;
258: }
259:
260: /*
261: * (non-Javadoc)
262: *
263: * @see org.kuali.dao.GeneralLedgerPendingEntryDao#getByPrimaryId(java.lang.Long, java.lang.Long)
264: */
265: public GeneralLedgerPendingEntry getByPrimaryId(
266: String documentHeaderId,
267: Integer transactionLedgerEntrySequenceNumber) {
268: LOG.debug("getByPrimaryId() started");
269:
270: Criteria criteria = new Criteria();
271: criteria.addEqualTo(KFSPropertyConstants.DOCUMENT_NUMBER,
272: documentHeaderId);
273: criteria.addEqualTo(TRANSACTION_LEDGER_ENTRY_SEQUENCE_NUMBER,
274: transactionLedgerEntrySequenceNumber);
275:
276: return (GeneralLedgerPendingEntry) getPersistenceBrokerTemplate()
277: .getObjectByQuery(
278: QueryFactory.newQuery(this .getEntryClass(),
279: criteria));
280: }
281:
282: /*
283: * (non-Javadoc)
284: *
285: * @see org.kuali.dao.GeneralLedgerPendingEntryDao#save(org.kuali.bo.GeneralLedgerPendingEntry)
286: */
287: public void save(GeneralLedgerPendingEntry generalLedgerPendingEntry) {
288: LOG.debug("save() started");
289:
290: getPersistenceBrokerTemplate().store(generalLedgerPendingEntry);
291: }
292:
293: /*
294: * (non-Javadoc)
295: *
296: * @see org.kuali.dao.GeneralLedgerPendingEntryDao#delete(Long)
297: */
298: public void delete(String documentHeaderId) {
299: LOG.debug("delete() started");
300:
301: if (documentHeaderId != null) {
302: Criteria criteria = new Criteria();
303: criteria.addEqualTo(KFSPropertyConstants.DOCUMENT_NUMBER,
304: documentHeaderId);
305:
306: getPersistenceBrokerTemplate().deleteByQuery(
307: QueryFactory.newQuery(this .getEntryClass(),
308: criteria));
309: getPersistenceBrokerTemplate().clearCache();
310: }
311: }
312:
313: public void deleteByFinancialDocumentApprovedCode(
314: String financialDocumentApprovedCode) {
315: LOG.debug("deleteByFinancialDocumentApprovedCode() started");
316:
317: Criteria criteria = new Criteria();
318: criteria.addEqualTo(FINANCIAL_DOCUMENT_APPROVED_CODE,
319: financialDocumentApprovedCode);
320:
321: QueryByCriteria qbc = QueryFactory.newQuery(this
322: .getEntryClass(), criteria);
323: getPersistenceBrokerTemplate().deleteByQuery(qbc);
324: getPersistenceBrokerTemplate().clearCache();
325: }
326:
327: /**
328: * @see org.kuali.module.gl.dao.GeneralLedgerPendingEntryDao#findApprovedPendingLedgerEntries()
329: */
330: public Iterator findApprovedPendingLedgerEntries() {
331: LOG.debug("findApprovedPendingLedgerEntries() started");
332:
333: // only process the document for which document status code is A (approved)
334: Criteria criteria = new Criteria();
335: criteria
336: .addEqualTo(
337: "financialDocumentApprovedCode",
338: KFSConstants.PENDING_ENTRY_APPROVED_STATUS_CODE.APPROVED);
339:
340: QueryByCriteria query = QueryFactory.newQuery(this
341: .getEntryClass(), criteria);
342: return getPersistenceBrokerTemplate().getIteratorByQuery(query);
343: }
344:
345: /**
346: * @see org.kuali.module.gl.dao.GeneralLedgerPendingEntryDao#countPendingLedgerEntries(org.kuali.module.chart.bo.Account)
347: */
348: public int countPendingLedgerEntries(Account account) {
349: LOG.debug("findPendingLedgerEntries(Account) started");
350:
351: Criteria criteria = new Criteria();
352: criteria.addEqualTo(ACCOUNT_NUMBER, account.getAccountNumber());
353: criteria.addEqualTo(CHART_OF_ACCOUNTS_CODE, account
354: .getChartOfAccountsCode());
355:
356: ReportQueryByCriteria query = QueryFactory.newReportQuery(this
357: .getEntryClass(), criteria);
358: query.setAttributes(new String[] { "count(*)" });
359: Iterator i = getPersistenceBrokerTemplate()
360: .getReportQueryIteratorByQuery(query);
361: if (i.hasNext()) {
362: Object[] values = (Object[]) TransactionalServiceUtils
363: .retrieveFirstAndExhaustIterator(i);
364: if (values[0] instanceof BigDecimal) {
365: return ((BigDecimal) values[0]).intValue();
366: } else {
367: return ((Long) values[0]).intValue();
368: }
369: } else {
370: return 0;
371: }
372: }
373:
374: /**
375: * @see org.kuali.module.gl.dao.GeneralLedgerPendingEntryDao#findPendingLedgerEntries(org.kuali.module.gl.bo.Encumbrance,
376: * boolean)
377: */
378: public Iterator findPendingLedgerEntries(Encumbrance encumbrance,
379: boolean isApproved) {
380: LOG
381: .debug("findPendingLedgerEntries(Encumbrance, boolean) started");
382:
383: // find pending ledger entry by the primary key fields of encumbrance
384: Criteria criteria = new Criteria();
385: criteria.addEqualTo(
386: KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR,
387: encumbrance.getUniversityFiscalYear());
388: criteria.addEqualTo(
389: KFSPropertyConstants.CHART_OF_ACCOUNTS_CODE,
390: encumbrance.getChartOfAccountsCode());
391: criteria.addEqualTo(KFSPropertyConstants.ACCOUNT_NUMBER,
392: encumbrance.getAccountNumber());
393: criteria.addEqualTo(KFSPropertyConstants.SUB_ACCOUNT_NUMBER,
394: encumbrance.getSubAccountNumber());
395: criteria.addEqualTo(KFSPropertyConstants.FINANCIAL_OBJECT_CODE,
396: encumbrance.getObjectCode());
397: criteria.addEqualTo(
398: KFSPropertyConstants.FINANCIAL_SUB_OBJECT_CODE,
399: encumbrance.getSubObjectCode());
400: criteria.addEqualTo(
401: KFSPropertyConstants.FINANCIAL_BALANCE_TYPE_CODE,
402: encumbrance.getBalanceTypeCode());
403: criteria.addEqualTo(
404: KFSPropertyConstants.FINANCIAL_DOCUMENT_TYPE_CODE,
405: encumbrance.getDocumentTypeCode());
406: criteria.addEqualTo(
407: KFSPropertyConstants.ORGANIZATION_DOCUMENT_NUMBER,
408: encumbrance.getDocumentNumber());
409:
410: // add the status codes into the criteria
411: this .addStatusCode(criteria, isApproved);
412:
413: // Criteria: (originCode=originationCode OR originCode=originationReferenceCode)
414: Criteria criteria1 = new Criteria();
415: Criteria criteria2 = new Criteria();
416: criteria1.addEqualTo(
417: KFSPropertyConstants.FINANCIAL_SYSTEM_ORIGINATION_CODE,
418: encumbrance.getOriginCode());
419: criteria2
420: .addEqualTo(
421: KFSPropertyConstants.REFERENCE_FINANCIAL_SYSTEM_ORIGINATION_CODE,
422: encumbrance.getOriginCode());
423: criteria1.addOrCriteria(criteria2);
424:
425: // combine all criteria together
426: criteria.addAndCriteria(criteria1);
427:
428: QueryByCriteria query = QueryFactory.newQuery(this
429: .getEntryClass(), criteria);
430: return getPersistenceBrokerTemplate().getIteratorByQuery(query);
431: }
432:
433: /**
434: * @see org.kuali.module.gl.dao.GeneralLedgerPendingEntryDao#findPendingLedgerEntries(org.kuali.module.gl.bo.Balance, boolean,
435: * boolean)
436: */
437: public Iterator findPendingLedgerEntries(Balance balance,
438: boolean isApproved, boolean isConsolidated) {
439: LOG
440: .debug("findPendingLedgerEntries(Balance, boolean, boolean) started");
441:
442: // find pending ledger entry by the primary key fields of balance
443: Criteria criteria = new Criteria();
444: criteria.addEqualTo(
445: KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR, balance
446: .getUniversityFiscalYear());
447: criteria.addEqualTo(
448: KFSPropertyConstants.CHART_OF_ACCOUNTS_CODE, balance
449: .getChartOfAccountsCode());
450: criteria.addEqualTo(KFSPropertyConstants.ACCOUNT_NUMBER,
451: balance.getAccountNumber());
452: criteria.addEqualTo(KFSPropertyConstants.FINANCIAL_OBJECT_CODE,
453: balance.getObjectCode());
454: criteria.addEqualTo(
455: KFSPropertyConstants.FINANCIAL_BALANCE_TYPE_CODE,
456: balance.getBalanceTypeCode());
457:
458: if (!isConsolidated) {
459: criteria.addEqualTo(
460: KFSPropertyConstants.SUB_ACCOUNT_NUMBER, balance
461: .getSubAccountNumber());
462: }
463:
464: // add the status codes into the criteria
465: this .addStatusCode(criteria, isApproved);
466:
467: QueryByCriteria query = QueryFactory.newQuery(this
468: .getEntryClass(), criteria);
469: return getPersistenceBrokerTemplate().getIteratorByQuery(query);
470: }
471:
472: /**
473: * @see org.kuali.module.gl.dao.GeneralLedgerPendingEntryDao#findPendingLedgerEntriesForEntry(java.util.Map, boolean)
474: */
475: public Iterator findPendingLedgerEntriesForEntry(Map fieldValues,
476: boolean isApproved) {
477: LOG.debug("findPendingLedgerEntriesForEntry started");
478:
479: Criteria criteria = buildCriteriaFromMap(fieldValues,
480: new GeneralLedgerPendingEntry());
481:
482: // add the status codes into the criteria
483: this .addStatusCode(criteria, isApproved);
484:
485: LookupUtils.applySearchResultsLimit(criteria, getDbPlatform());
486:
487: QueryByCriteria query = QueryFactory.newQuery(this
488: .getEntryClass(), criteria);
489: return getPersistenceBrokerTemplate().getIteratorByQuery(query);
490: }
491:
492: /**
493: * @see org.kuali.module.gl.dao.GeneralLedgerPendingEntryDao#findPendingLedgerEntriesForBalance(java.util.Map, boolean)
494: */
495: public Iterator findPendingLedgerEntriesForBalance(Map fieldValues,
496: boolean isApproved) {
497: LOG.debug("findPendingLedgerEntriesForBalance started");
498:
499: Criteria criteria = buildCriteriaFromMap(fieldValues, this
500: .getEntryClassInstance());
501:
502: // add the status codes into the criteria
503: this .addStatusCode(criteria, isApproved);
504:
505: QueryByCriteria query = QueryFactory.newQuery(this
506: .getEntryClass(), criteria);
507: return getPersistenceBrokerTemplate().getIteratorByQuery(query);
508: }
509:
510: /**
511: * @see org.kuali.module.gl.dao.GeneralLedgerPendingEntryDao#findPendingLedgerEntriesForCashBalance(java.util.Map, boolean)
512: */
513: public Iterator findPendingLedgerEntriesForCashBalance(
514: Map fieldValues, boolean isApproved) {
515: LOG.debug("findPendingLedgerEntriesForCashBalance started");
516:
517: Criteria criteria = buildCriteriaFromMap(fieldValues, this
518: .getEntryClassInstance());
519: criteria.addEqualTo(
520: KFSPropertyConstants.FINANCIAL_BALANCE_TYPE_CODE, "AC");
521: criteria.addEqualToField(
522: KFSPropertyConstants.FINANCIAL_OBJECT_CODE,
523: CHART_FINANCIAL_CASH_OBJECT_CODE);
524:
525: // add the status codes into the criteria
526: this .addStatusCode(criteria, isApproved);
527:
528: QueryByCriteria query = QueryFactory.newQuery(this
529: .getEntryClass(), criteria);
530: return getPersistenceBrokerTemplate().getIteratorByQuery(query);
531: }
532:
533: /**
534: * @see org.kuali.module.gl.dao.GeneralLedgerPendingEntryDao#findPendingLedgerEntriesForEncumbrance(Map, boolean)
535: */
536: public Iterator findPendingLedgerEntriesForEncumbrance(
537: Map fieldValues, boolean isApproved) {
538: LOG.debug("findPendingLedgerEntriesForEncumbrance started");
539:
540: Criteria criteria = buildCriteriaFromMap(fieldValues, this
541: .getEntryClassInstance());
542: criteria.addIn(
543: KFSPropertyConstants.FINANCIAL_BALANCE_TYPE_CODE,
544: Arrays.asList(KFSConstants.ENCUMBRANCE_BALANCE_TYPE));
545:
546: List encumbranceUpdateCodeList = new ArrayList();
547: encumbranceUpdateCodeList
548: .add(KFSConstants.ENCUMB_UPDT_REFERENCE_DOCUMENT_CD);
549: encumbranceUpdateCodeList
550: .add(KFSConstants.ENCUMB_UPDT_DOCUMENT_CD);
551: criteria.addIn(
552: KFSPropertyConstants.TRANSACTION_ENCUMBRANCE_UPDT_CD,
553: encumbranceUpdateCodeList);
554:
555: // add the status codes into the criteria
556: this .addStatusCode(criteria, isApproved);
557:
558: QueryByCriteria query = QueryFactory.newQuery(this
559: .getEntryClass(), criteria);
560: return getPersistenceBrokerTemplate().getIteratorByQuery(query);
561: }
562:
563: /**
564: * @see org.kuali.module.gl.dao.GeneralLedgerPendingEntryDao#findPendingLedgerEntriesForAccountBalance(java.util.Map, boolean,
565: * boolean)
566: */
567: public Iterator findPendingLedgerEntriesForAccountBalance(
568: Map fieldValues, boolean isApproved) {
569: LOG.debug("findPendingLedgerEntriesForAccountBalance started");
570:
571: Criteria criteria = buildCriteriaFromMap(fieldValues, this
572: .getEntryClassInstance());
573:
574: // add the status codes into the criteria
575: this .addStatusCode(criteria, isApproved);
576:
577: QueryByCriteria query = QueryFactory.newQuery(this
578: .getEntryClass(), criteria);
579: return getPersistenceBrokerTemplate().getIteratorByQuery(query);
580: }
581:
582: /**
583: * @see org.kuali.module.gl.dao.GeneralLedgerPendingEntryDao#findPendingLedgerEntrySummaryForAccountBalance(java.util.Map,
584: * boolean, boolean)
585: */
586: public Iterator findPendingLedgerEntrySummaryForAccountBalance(
587: Map fieldValues, boolean isApproved) {
588: LOG
589: .debug("findPendingLedgerEntrySummaryForAccountBalance started");
590:
591: Criteria criteria = buildCriteriaFromMap(fieldValues, this
592: .getEntryClassInstance());
593:
594: // add the status codes into the criteria
595: this .addStatusCode(criteria, isApproved);
596:
597: ReportQueryByCriteria query = QueryFactory.newReportQuery(this
598: .getEntryClass(), criteria);
599:
600: List attributeList = buildAttributeList();
601: List groupByList = buildGroupList();
602:
603: // set the selection attributes
604: String[] attributes = (String[]) attributeList
605: .toArray(new String[attributeList.size()]);
606: query.setAttributes(attributes);
607:
608: // add the group criteria into the selection statement
609: String[] groupBy = (String[]) groupByList
610: .toArray(new String[groupByList.size()]);
611: query.addGroupBy(groupBy);
612:
613: return getPersistenceBrokerTemplate()
614: .getReportQueryIteratorByQuery(query);
615: }
616:
617: /**
618: * This method builds the atrribute list used by balance searching
619: *
620: * @return List an attribute list
621: */
622: private List buildAttributeList() {
623: List attributeList = buildGroupList();
624: attributeList.add("sum("
625: + KFSPropertyConstants.TRANSACTION_LEDGER_ENTRY_AMOUNT
626: + ")");
627: return attributeList;
628: }
629:
630: /**
631: * This method builds group by attribute list used by balance searching
632: *
633: * @return List an group by attribute list
634: */
635: private List buildGroupList() {
636: List groupList = new ArrayList();
637:
638: groupList.add(KFSPropertyConstants.FINANCIAL_OBJECT_CODE);
639: groupList
640: .add(KFSPropertyConstants.FINANCIAL_DOCUMENT_APPROVED_CODE);
641: groupList
642: .add(KFSPropertyConstants.TRANSACTION_DEBIT_CREDIT_CODE);
643: groupList.add(OBJECT_TYPE_FIN_OBJECT_TYPE_DEBITCREDIT_CD);
644: return groupList;
645: }
646:
647: /**
648: * add the status code into the given criteria. The status code can be categorized into approved and all.
649: *
650: * @param criteria the given criteria
651: * @param isApproved the flag that indictates if only approved status code can be added into the given searach criteria
652: */
653: private void addStatusCode(Criteria criteria, boolean isOnlyApproved) {
654: // add criteria for the approved pending entries
655: if (isOnlyApproved) {
656: criteria.addIn(
657: "documentHeader.financialDocumentStatusCode", this
658: .buildApprovalCodeList());
659: criteria
660: .addNotEqualTo(
661: KFSPropertyConstants.FINANCIAL_DOCUMENT_APPROVED_CODE,
662: KFSConstants.PENDING_ENTRY_APPROVED_STATUS_CODE.PROCESSED);
663: } else {
664: Criteria subCriteria1 = new Criteria();
665: subCriteria1
666: .addNotEqualTo(
667: KFSPropertyConstants.FINANCIAL_DOCUMENT_APPROVED_CODE,
668: KFSConstants.PENDING_ENTRY_APPROVED_STATUS_CODE.PROCESSED);
669:
670: Criteria subCriteria2 = new Criteria();
671: subCriteria2
672: .addIsNull(KFSPropertyConstants.FINANCIAL_DOCUMENT_APPROVED_CODE);
673:
674: subCriteria1.addOrCriteria(subCriteria2);
675: criteria.addAndCriteria(subCriteria1);
676: }
677: }
678:
679: /**
680: * build a status code list including the legal approval codes
681: *
682: * @return an approval code list
683: */
684: private List buildApprovalCodeList() {
685: List approvalCodeList = new ArrayList();
686:
687: approvalCodeList.add(KFSConstants.DocumentStatusCodes.APPROVED);
688: return approvalCodeList;
689: }
690:
691: /**
692: * This method builds an OJB query criteria based on the input field map
693: *
694: * @param fieldValues the input field map
695: * @param businessObject the given business object
696: * @return an OJB query criteria
697: */
698: public Criteria buildCriteriaFromMap(Map fieldValues,
699: Object businessObject) {
700: Criteria criteria = new Criteria();
701:
702: UniversityDate currentUniversityDate = SpringContext.getBean(
703: UniversityDateService.class).getCurrentUniversityDate();
704: String currentFiscalPeriodCode = currentUniversityDate
705: .getUniversityFiscalAccountingPeriod();
706: Integer currentFiscalYear = currentUniversityDate
707: .getUniversityFiscalYear();
708:
709: // deal with null fiscal year and fiscal period code as current fiscal year and period code respectively
710: String fiscalPeriodFromForm = null;
711: if (fieldValues
712: .containsKey(KFSPropertyConstants.UNIVERSITY_FISCAL_PERIOD_CODE)) {
713: fiscalPeriodFromForm = (String) fieldValues
714: .get(KFSPropertyConstants.UNIVERSITY_FISCAL_PERIOD_CODE);
715: }
716:
717: String fiscalYearFromForm = null;
718: if (fieldValues
719: .containsKey(KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR)) {
720: fiscalYearFromForm = (String) fieldValues
721: .get(KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR);
722: }
723:
724: boolean includeNullFiscalPeriodCodeInLookup = null != currentFiscalPeriodCode
725: && currentFiscalPeriodCode.equals(fiscalPeriodFromForm);
726: boolean includeNullFiscalYearInLookup = null != currentFiscalYear
727: && currentFiscalYear.toString().equals(
728: fiscalYearFromForm);
729:
730: if (includeNullFiscalPeriodCodeInLookup) {
731: Criteria apValueCriteria = new Criteria();
732: apValueCriteria.addLike(
733: KFSPropertyConstants.UNIVERSITY_FISCAL_PERIOD_CODE,
734: fiscalPeriodFromForm);
735:
736: Criteria apNullCriteria = new Criteria();
737: apNullCriteria
738: .addIsNull(KFSPropertyConstants.UNIVERSITY_FISCAL_PERIOD_CODE);
739:
740: apValueCriteria.addOrCriteria(apNullCriteria);
741: criteria.addAndCriteria(apValueCriteria);
742:
743: fieldValues
744: .remove(KFSPropertyConstants.UNIVERSITY_FISCAL_PERIOD_CODE);
745: }
746:
747: if (includeNullFiscalYearInLookup) {
748: Criteria fyValueCriteria = new Criteria();
749: fyValueCriteria.addEqualTo(
750: KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR,
751: fiscalYearFromForm);
752:
753: Criteria fyNullCriteria = new Criteria();
754: fyNullCriteria
755: .addIsNull(KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR);
756:
757: fyValueCriteria.addOrCriteria(fyNullCriteria);
758: criteria.addAndCriteria(fyValueCriteria);
759:
760: fieldValues
761: .remove(KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR);
762: }
763:
764: // handle encumbrance balance type
765: Map<String, Object> localFieldValues = new HashMap();
766: localFieldValues.putAll(fieldValues);
767:
768: String propertyName = KFSPropertyConstants.FINANCIAL_BALANCE_TYPE_CODE;
769: if (localFieldValues.containsKey(propertyName)) {
770: String propertyValue = (String) fieldValues
771: .get(propertyName);
772: if (KFSConstants.AGGREGATE_ENCUMBRANCE_BALANCE_TYPE_CODE
773: .equals(propertyValue)) {
774: localFieldValues
775: .remove(KFSPropertyConstants.FINANCIAL_BALANCE_TYPE_CODE);
776:
777: // parse the fiscal year (it's not a required field on the lookup screens
778: String universityFiscalYearStr = (String) localFieldValues
779: .get(KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR);
780: if (StringUtils.isNotBlank(universityFiscalYearStr)) {
781: Integer universityFiscalYear = new Integer(
782: universityFiscalYearStr);
783: criteria
784: .addIn(
785: KFSPropertyConstants.FINANCIAL_BALANCE_TYPE_CODE,
786: balanceTypService
787: .getEncumbranceBalanceTypes(universityFiscalYear));
788: } else {
789: criteria
790: .addIn(
791: KFSPropertyConstants.FINANCIAL_BALANCE_TYPE_CODE,
792: balanceTypService
793: .getEncumbranceBalanceTypes());
794: }
795: }
796: }
797:
798: // remove dummyBusinessObject references - no longer needed
799: List<String> keysToRemove = new ArrayList<String>();
800: for (String key : localFieldValues.keySet()) {
801: if (key.startsWith("dummyBusinessObject.")) {
802: keysToRemove.add(key);
803: }
804: }
805: for (String key : keysToRemove) {
806: localFieldValues.remove(key);
807: }
808:
809: criteria.addAndCriteria(OJBUtility.buildCriteriaFromMap(
810: localFieldValues, businessObject));
811: return criteria;
812: }
813:
814: public Collection findPendingEntries(Map fieldValues,
815: boolean isApproved) {
816: LOG.debug("findPendingEntries(Map, boolean) started");
817:
818: Criteria criteria = buildCriteriaFromMap(fieldValues, this
819: .getEntryClassInstance());
820:
821: // add the status codes into the criteria
822: this .addStatusCode(criteria, isApproved);
823:
824: LookupUtils.applySearchResultsLimit(criteria, getDbPlatform());
825:
826: QueryByCriteria query = QueryFactory.newQuery(this
827: .getEntryClass(), criteria);
828: return getPersistenceBrokerTemplate().getCollectionByQuery(
829: query);
830: }
831:
832: public void setParameterService(ParameterService parameterService) {
833: this .parameterService = parameterService;
834: }
835:
836: /**
837: * Gets the entryClass attribute.
838: *
839: * @return Returns the entryClass.
840: */
841: public Class getEntryClass() {
842: return GeneralLedgerPendingEntry.class;
843: }
844:
845: private Object getEntryClassInstance() {
846: Object entryObject = null;
847: try {
848: entryObject = getEntryClass().newInstance();
849: } catch (Exception e) {
850: LOG.debug("Wrong object type" + e);
851: }
852: return entryObject;
853: }
854:
855: public void setBalanceTypService(BalanceTypService balanceTypService) {
856: this.balanceTypService = balanceTypService;
857: }
858: }
|