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.module.purap.dao.ojb;
017:
018: import java.sql.Date;
019: import java.util.ArrayList;
020: import java.util.Arrays;
021: import java.util.Iterator;
022: import java.util.List;
023:
024: import org.apache.log4j.Logger;
025: import org.apache.ojb.broker.query.Criteria;
026: import org.apache.ojb.broker.query.Query;
027: import org.apache.ojb.broker.query.QueryByCriteria;
028: import org.apache.ojb.broker.query.QueryFactory;
029: import org.apache.ojb.broker.query.ReportQueryByCriteria;
030: import org.kuali.core.dao.ojb.PlatformAwareDaoBaseOjb;
031: import org.kuali.core.exceptions.InfrastructureException;
032: import org.kuali.core.service.DateTimeService;
033: import org.kuali.core.service.DocumentService;
034: import org.kuali.core.service.KualiConfigurationService;
035: import org.kuali.core.util.KualiDecimal;
036: import org.kuali.core.util.TransactionalServiceUtils;
037: import org.kuali.kfs.KFSPropertyConstants;
038: import org.kuali.kfs.context.SpringContext;
039: import org.kuali.module.purap.PurapConstants;
040: import org.kuali.module.purap.PurapPropertyConstants;
041: import org.kuali.module.purap.PurapConstants.PaymentRequestStatuses;
042: import org.kuali.module.purap.bo.PaymentRequestSummaryAccount;
043: import org.kuali.module.purap.dao.NegativePaymentRequestApprovalLimitDao;
044: import org.kuali.module.purap.dao.PaymentRequestDao;
045: import org.kuali.module.purap.document.PaymentRequestDocument;
046: import org.kuali.module.purap.service.PurapAccountingService;
047: import org.kuali.module.purap.util.VendorGroupingHelper;
048:
049: import edu.iu.uis.eden.exception.WorkflowException;
050:
051: /**
052: * OJB Implementation of PaymentRequestDao.
053: */
054: public class PaymentRequestDaoOjb extends PlatformAwareDaoBaseOjb
055: implements PaymentRequestDao {
056: private static Logger LOG = Logger
057: .getLogger(PaymentRequestDaoOjb.class);
058:
059: private NegativePaymentRequestApprovalLimitDao negativePaymentRequestApprovalLimitDao;
060: private DateTimeService dateTimeService;
061: private PurapAccountingService purapAccountingService;
062: private KualiConfigurationService kualiConfigurationService;
063:
064: /**
065: * The special payments query should be this: select * from pur.ap_pmt_rqst_t where pmt_rqst_stat_cd in ('AUTO', 'DPTA') and
066: * prcs_cmp_cd = ? and pmt_extrt_ts is NULL and pmt_hld_ind = 'N' and ( ( ( pmt_spcl_handlg_instrc_ln1_txt is not NULL or
067: * pmt_spcl_handlg_instrc_ln2_txt is not NULL or pmt_spcl_handlg_instrc_ln3_txt is not NULL or pmt_att_ind = 'Y') and trunc
068: * (pmt_rqst_pay_dt) <= trunc (sysdate)) or IMD_PMT_IND = 'Y')})
069: *
070: * @see org.kuali.module.purap.dao.PaymentRequestDao#getPaymentRequestsToExtract(boolean, java.lang.String)
071: */
072: public Iterator<PaymentRequestDocument> getPaymentRequestsToExtract(
073: boolean onlySpecialPayments, String chartCode) {
074: LOG.debug("getPaymentRequestsToExtract() started");
075:
076: Criteria criteria = new Criteria();
077: if (chartCode != null) {
078: criteria.addEqualTo("processingCampusCode", chartCode);
079: }
080: criteria
081: .addIn(
082: "statusCode",
083: Arrays
084: .asList(PaymentRequestStatuses.STATUSES_ALLOWED_FOR_EXTRACTION));
085: criteria.addIsNull("extractedDate");
086: criteria.addEqualTo("holdIndicator", Boolean.FALSE);
087:
088: if (onlySpecialPayments) {
089: Criteria a = new Criteria();
090:
091: Criteria c1 = new Criteria();
092: c1.addNotNull("specialHandlingInstructionLine1Text");
093: Criteria c2 = new Criteria();
094: c2.addNotNull("specialHandlingInstructionLine2Text");
095: Criteria c3 = new Criteria();
096: c3.addNotNull("specialHandlingInstructionLine3Text");
097: Criteria c4 = new Criteria();
098: c4.addEqualTo("paymentAttachmentIndicator", Boolean.TRUE);
099:
100: c1.addOrCriteria(c2);
101: c1.addOrCriteria(c3);
102: c1.addOrCriteria(c4);
103:
104: a.addAndCriteria(c1);
105: a.addLessOrEqualThan("paymentRequestPayDate",
106: dateTimeService.getCurrentSqlDateMidnight());
107:
108: Criteria c5 = new Criteria();
109: c5.addEqualTo("immediatePaymentIndicator", Boolean.TRUE);
110: c5.addOrCriteria(a);
111:
112: criteria.addAndCriteria(a);
113: } else {
114: Criteria c1 = new Criteria();
115: c1.addLessOrEqualThan("paymentRequestPayDate",
116: dateTimeService.getCurrentSqlDateMidnight());
117:
118: Criteria c2 = new Criteria();
119: c2.addEqualTo("immediatePaymentIndicator", Boolean.TRUE);
120:
121: c1.addOrCriteria(c2);
122: criteria.addAndCriteria(c1);
123: }
124:
125: return getPersistenceBrokerTemplate().getIteratorByQuery(
126: new QueryByCriteria(PaymentRequestDocument.class,
127: criteria));
128: }
129:
130: /**
131: * @see org.kuali.module.purap.dao.PaymentRequestDao#getImmediatePaymentRequestsToExtract(java.lang.String)
132: */
133: public Iterator<PaymentRequestDocument> getImmediatePaymentRequestsToExtract(
134: String chartCode) {
135: LOG.debug("getImmediatePaymentRequestsToExtract() started");
136:
137: Criteria criteria = new Criteria();
138: if (chartCode != null) {
139: criteria.addEqualTo("processingCampusCode", chartCode);
140: }
141:
142: criteria
143: .addIn(
144: "statusCode",
145: Arrays
146: .asList(PaymentRequestStatuses.STATUSES_ALLOWED_FOR_EXTRACTION));
147: criteria.addIsNull("extractedDate");
148: criteria.addEqualTo("immediatePaymentIndicator", Boolean.TRUE);
149:
150: return getPersistenceBrokerTemplate().getIteratorByQuery(
151: new QueryByCriteria(PaymentRequestDocument.class,
152: criteria));
153: }
154:
155: /**
156: * @see org.kuali.module.purap.dao.PaymentRequestDao#getPaymentRequestsToExtract(java.lang.String, java.lang.Integer,
157: * java.lang.Integer, java.lang.Integer, java.lang.Integer)
158: */
159: public Iterator<PaymentRequestDocument> getPaymentRequestsToExtract(
160: String campusCode, Integer paymentRequestIdentifier,
161: Integer purchaseOrderIdentifier,
162: Integer vendorHeaderGeneratedIdentifier,
163: Integer vendorDetailAssignedIdentifier) {
164: LOG.debug("getPaymentRequestsToExtract() started");
165:
166: List statuses = new ArrayList();
167: statuses
168: .add(PurapConstants.PaymentRequestStatuses.AUTO_APPROVED);
169: statuses
170: .add(PurapConstants.PaymentRequestStatuses.DEPARTMENT_APPROVED);
171:
172: Criteria criteria = new Criteria();
173: criteria.addEqualTo("processingCampusCode", campusCode);
174: criteria.addIn("statusCode", statuses);
175: criteria.addIsNull("extractedDate");
176: criteria.addEqualTo("holdIndicator", Boolean.FALSE);
177:
178: Criteria c1 = new Criteria();
179: c1.addLessOrEqualThan("paymentRequestPayDate", dateTimeService
180: .getCurrentSqlDateMidnight());
181:
182: Criteria c2 = new Criteria();
183: c2.addEqualTo("immediatePaymentIndicator", Boolean.TRUE);
184:
185: c1.addOrCriteria(c2);
186: criteria.addAndCriteria(c1);
187:
188: if (paymentRequestIdentifier != null) {
189: criteria.addEqualTo("purapDocumentIdentifier",
190: paymentRequestIdentifier);
191: }
192: if (purchaseOrderIdentifier != null) {
193: criteria.addEqualTo("purchaseOrderIdentifier",
194: purchaseOrderIdentifier);
195: }
196: criteria.addEqualTo("vendorHeaderGeneratedIdentifier",
197: vendorHeaderGeneratedIdentifier);
198: criteria.addEqualTo("vendorDetailAssignedIdentifier",
199: vendorDetailAssignedIdentifier);
200:
201: return getPersistenceBrokerTemplate().getIteratorByQuery(
202: new QueryByCriteria(PaymentRequestDocument.class,
203: criteria));
204: }
205:
206: /**
207: *
208: * @see org.kuali.module.purap.dao.PaymentRequestDao#getPaymentRequestsToExtractForVendor(java.lang.String, org.kuali.module.purap.util.VendorGroupingHelper)
209: */
210: public Iterator<PaymentRequestDocument> getPaymentRequestsToExtractForVendor(
211: String campusCode, VendorGroupingHelper vendor) {
212: LOG.debug("getPaymentRequestsToExtract() started");
213:
214: List statuses = new ArrayList();
215: statuses
216: .add(PurapConstants.PaymentRequestStatuses.AUTO_APPROVED);
217: statuses
218: .add(PurapConstants.PaymentRequestStatuses.DEPARTMENT_APPROVED);
219:
220: Criteria criteria = new Criteria();
221: criteria.addEqualTo("processingCampusCode", campusCode);
222: criteria.addIn("statusCode", statuses);
223: criteria.addIsNull("extractedDate");
224: criteria.addEqualTo("holdIndicator", Boolean.FALSE);
225:
226: Criteria c1 = new Criteria();
227: c1.addLessOrEqualThan("paymentRequestPayDate", dateTimeService
228: .getCurrentSqlDateMidnight());
229:
230: Criteria c2 = new Criteria();
231: c2.addEqualTo("immediatePaymentIndicator", Boolean.TRUE);
232:
233: c1.addOrCriteria(c2);
234: criteria.addAndCriteria(c1);
235:
236: criteria.addEqualTo("vendorHeaderGeneratedIdentifier", vendor
237: .getVendorHeaderGeneratedIdentifier());
238: criteria.addEqualTo("vendorDetailAssignedIdentifier", vendor
239: .getVendorDetailAssignedIdentifier());
240: criteria.addEqualTo("vendorCountryCode", vendor
241: .getVendorCountry());
242: criteria.addEqualTo("vendorPostalCode", vendor
243: .getVendorPostalCode());
244:
245: return getPersistenceBrokerTemplate().getIteratorByQuery(
246: new QueryByCriteria(PaymentRequestDocument.class,
247: criteria));
248: }
249:
250: /**
251: * @see org.kuali.module.purap.dao.PaymentRequestDao#getEligibleForAutoApproval()
252: */
253: public List<PaymentRequestDocument> getEligibleForAutoApproval() {
254: Date todayAtMidnight = dateTimeService
255: .getCurrentSqlDateMidnight();
256: Criteria criteria = new Criteria();
257: criteria.addLessOrEqualThan(
258: PurapPropertyConstants.PAYMENT_REQUEST_PAY_DATE,
259: todayAtMidnight);
260: criteria.addNotEqualTo("holdIndicator", "Y");
261: criteria.addNotEqualTo("paymentRequestedCancelIndicator", "Y");
262: criteria
263: .addIn(
264: "status",
265: Arrays
266: .asList(PurapConstants.PaymentRequestStatuses.PREQ_STATUSES_FOR_AUTO_APPROVE));
267:
268: Query query = new QueryByCriteria(PaymentRequestDocument.class,
269: criteria);
270: Iterator<PaymentRequestDocument> documents = (Iterator<PaymentRequestDocument>) getPersistenceBrokerTemplate()
271: .getIteratorByQuery(query);
272: ArrayList<String> documentHeaderIds = new ArrayList<String>();
273: while (documents.hasNext()) {
274: PaymentRequestDocument document = (PaymentRequestDocument) documents
275: .next();
276: documentHeaderIds.add(document.getDocumentNumber());
277: }
278:
279: if (documentHeaderIds.size() > 0) {
280: try {
281: return SpringContext.getBean(DocumentService.class)
282: .getDocumentsByListOfDocumentHeaderIds(
283: PaymentRequestDocument.class,
284: documentHeaderIds);
285: } catch (WorkflowException e) {
286: throw new InfrastructureException(
287: "unable to retrieve paymentRequestDocuments", e);
288: }
289: } else {
290: return null;
291: }
292:
293: }
294:
295: /**
296: * @see org.kuali.module.purap.dao.PaymentRequestDao#getDocumentNumberByPaymentRequestId(java.lang.Integer)
297: */
298: public String getDocumentNumberByPaymentRequestId(Integer id) {
299: Criteria criteria = new Criteria();
300: criteria.addEqualTo(PurapPropertyConstants.PURAP_DOC_ID, id);
301: return getDocumentNumberOfPaymentRequestByCriteria(criteria);
302: }
303:
304: /**
305: * @see org.kuali.module.purap.dao.PaymentRequestDao#getDocumentNumbersByPurchaseOrderId(java.lang.Integer)
306: */
307: public List<String> getDocumentNumbersByPurchaseOrderId(
308: Integer poPurApId) {
309: List<String> returnList = new ArrayList<String>();
310: Criteria criteria = new Criteria();
311: criteria.addEqualTo(
312: PurapPropertyConstants.PURCHASE_ORDER_IDENTIFIER,
313: poPurApId);
314: Iterator<Object[]> iter = getDocumentNumbersOfPaymentRequestByCriteria(
315: criteria, false);
316: while (iter.hasNext()) {
317: Object[] cols = (Object[]) iter.next();
318: returnList.add((String) cols[0]);
319: }
320: return returnList;
321: }
322:
323: /**
324: * Retrieves a document number for a payment request by user defined criteria.
325: *
326: * @param criteria - list of criteria to use in the retrieve
327: * @return - document number
328: */
329: private String getDocumentNumberOfPaymentRequestByCriteria(
330: Criteria criteria) {
331: LOG
332: .debug("getDocumentNumberOfPaymentRequestByCriteria() started");
333: Iterator<Object[]> iter = getDocumentNumbersOfPaymentRequestByCriteria(
334: criteria, false);
335: if (iter.hasNext()) {
336: Object[] cols = (Object[]) iter.next();
337: if (iter.hasNext()) {
338: // the iterator should have held only a single doc id of data but it holds 2 or more
339: String errorMsg = "Expected single document number for given criteria but multiple (at least 2) were returned";
340: LOG.error(errorMsg);
341: TransactionalServiceUtils.exhaustIterator(iter);
342: throw new RuntimeException();
343: }
344: // at this part of the code, we know there's no more elements in iterator
345: return (String) cols[0];
346: }
347: return null;
348: }
349:
350: /**
351: * Retrieves a document number for a payment request by user defined criteria and sorts the values ascending if orderByAscending
352: * parameter is true, descending otherwise.
353: *
354: * @param criteria - list of criteria to use in the retrieve
355: * @param orderByAscending - boolean to sort results ascending if true, descending otherwise
356: * @return - Iterator of document numbers
357: */
358: private Iterator<Object[]> getDocumentNumbersOfPaymentRequestByCriteria(
359: Criteria criteria, boolean orderByAscending) {
360: LOG
361: .debug("getDocumentNumberOfPaymentRequestByCriteria() started");
362: ReportQueryByCriteria rqbc = new ReportQueryByCriteria(
363: PaymentRequestDocument.class, criteria);
364: rqbc
365: .setAttributes(new String[] { KFSPropertyConstants.DOCUMENT_NUMBER });
366: if (orderByAscending) {
367: rqbc
368: .addOrderByAscending(KFSPropertyConstants.DOCUMENT_NUMBER);
369: } else {
370: rqbc
371: .addOrderByDescending(KFSPropertyConstants.DOCUMENT_NUMBER);
372: }
373: return getPersistenceBrokerTemplate()
374: .getReportQueryIteratorByQuery(rqbc);
375: }
376:
377: /**
378: * Retrieves a list of payment requests by user defined criteria.
379: *
380: * @param qbc - query with criteria
381: * @return - list of payment requests
382: */
383: private List getPaymentRequestsByQueryByCriteria(QueryByCriteria qbc) {
384: LOG.debug("getPaymentRequestsByQueryByCriteria() started");
385: List l = (List) getPersistenceBrokerTemplate()
386: .getCollectionByQuery(qbc);
387: return l;
388: }
389:
390: /**
391: * Retrieves a list of payment requests with the given vendor id and invoice number.
392: *
393: * @param vendorHeaderGeneratedId - header id of the vendor id
394: * @param vendorDetailAssignedId - detail id of the vendor id
395: * @param invoiceNumber - invoice number as entered by AP
396: * @return - List of payment requests.
397: */
398: public List getActivePaymentRequestsByVendorNumberInvoiceNumber(
399: Integer vendorHeaderGeneratedId,
400: Integer vendorDetailAssignedId, String invoiceNumber) {
401: LOG
402: .debug("getActivePaymentRequestsByVendorNumberInvoiceNumber() started");
403: Criteria criteria = new Criteria();
404: criteria.addEqualTo("vendorHeaderGeneratedIdentifier",
405: vendorHeaderGeneratedId);
406: criteria.addEqualTo("vendorDetailAssignedIdentifier",
407: vendorDetailAssignedId);
408: criteria.addEqualTo("invoiceNumber", invoiceNumber);
409: QueryByCriteria qbc = new QueryByCriteria(
410: PaymentRequestDocument.class, criteria);
411: return this .getPaymentRequestsByQueryByCriteria(qbc);
412: }
413:
414: /**
415: * @see org.kuali.module.purap.dao.PaymentRequestDao#getActivePaymentRequestsByPOIdInvoiceAmountInvoiceDate(java.lang.Integer,
416: * org.kuali.core.util.KualiDecimal, java.sql.Date)
417: */
418: public List getActivePaymentRequestsByPOIdInvoiceAmountInvoiceDate(
419: Integer poId, KualiDecimal vendorInvoiceAmount,
420: Date invoiceDate) {
421: LOG
422: .debug("getActivePaymentRequestsByVendorNumberInvoiceNumber() started");
423: Criteria criteria = new Criteria();
424: criteria.addEqualTo("purchaseOrderIdentifier", poId);
425: criteria.addEqualTo("vendorInvoiceAmount", vendorInvoiceAmount);
426: criteria.addEqualTo("invoiceDate", invoiceDate);
427: QueryByCriteria qbc = new QueryByCriteria(
428: PaymentRequestDocument.class, criteria);
429: return this .getPaymentRequestsByQueryByCriteria(qbc);
430: }
431:
432: /**
433: * @see org.kuali.module.purap.dao.PaymentRequestDao#deleteSummaryAccounts(java.lang.Integer)
434: */
435: public void deleteSummaryAccounts(Integer purapDocumentIdentifier) {
436: LOG.debug("deleteSummaryAccounts() started");
437:
438: if (purapDocumentIdentifier != null) {
439: Criteria criteria = new Criteria();
440: criteria.addEqualTo(PurapPropertyConstants.PURAP_DOC_ID,
441: purapDocumentIdentifier);
442:
443: getPersistenceBrokerTemplate().deleteByQuery(
444: QueryFactory.newQuery(
445: PaymentRequestSummaryAccount.class,
446: criteria));
447: getPersistenceBrokerTemplate().clearCache();
448: }
449: }
450:
451: public void setNegativePaymentRequestApprovalLimitDao(
452: NegativePaymentRequestApprovalLimitDao negativePaymentRequestApprovalLimitDao) {
453: this .negativePaymentRequestApprovalLimitDao = negativePaymentRequestApprovalLimitDao;
454: }
455:
456: public void setDateTimeService(DateTimeService dateTimeService) {
457: this .dateTimeService = dateTimeService;
458: }
459:
460: public void setPurapAccountingService(
461: PurapAccountingService purapAccountingService) {
462: this .purapAccountingService = purapAccountingService;
463: }
464:
465: public void setKualiConfigurationService(
466: KualiConfigurationService kualiConfigurationService) {
467: this.kualiConfigurationService = kualiConfigurationService;
468: }
469:
470: }
|