001 /*
002 * Copyright 2011 The Kuali Foundation.
003 *
004 * Licensed under the Educational Community License, Version 2.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/ecl2.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 /*
017 * Created on Aug 11, 2004
018 *
019 */
020 package org.kuali.kfs.pdp.dataaccess.impl;
021
022 import java.util.ArrayList;
023 import java.util.HashMap;
024 import java.util.Iterator;
025 import java.util.List;
026 import java.util.Map;
027
028 import org.apache.ojb.broker.query.Criteria;
029 import org.apache.ojb.broker.query.QueryFactory;
030 import org.apache.ojb.broker.query.ReportQueryByCriteria;
031 import org.kuali.kfs.pdp.PdpPropertyConstants;
032 import org.kuali.kfs.pdp.PdpConstants.PaymentStatusCodes;
033 import org.kuali.kfs.pdp.businessobject.PaymentGroup;
034 import org.kuali.kfs.pdp.businessobject.PaymentStatus;
035 import org.kuali.kfs.pdp.dataaccess.BatchMaintenanceDao;
036 import org.kuali.rice.kns.dao.impl.PlatformAwareDaoBaseOjb;
037 import org.kuali.rice.kns.service.BusinessObjectService;
038 import org.kuali.rice.kns.util.ObjectUtils;
039 import org.kuali.rice.kns.util.TransactionalServiceUtils;
040
041
042 /**
043 *
044 */
045 public class BatchMaintenanceDaoOjb extends PlatformAwareDaoBaseOjb implements BatchMaintenanceDao {
046 private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(BatchMaintenanceDaoOjb.class);
047 private BusinessObjectService businessObjectService;
048
049 public BatchMaintenanceDaoOjb() {
050 super();
051 }
052
053 public void setBusinessObjectService(BusinessObjectService businessObjectService) {
054 this.businessObjectService = businessObjectService;
055 }
056
057 /**
058 * doBatchPaymentsHaveOpenStatus() Return true if all payments in batch have an 'OPEN' status. Return false if all payments in
059 * batch do not have an 'OPEN' status. The query in this method searches the payment detail table for payments of the given
060 * batchId where the status equals any status other than 'OPEN'. If any rows exist with a status other than 'OPEN', return
061 * false.
062 *
063 * @param batchId Integer value of batch id of payments to search.
064 * @return boolean true = all payments are 'OPEN'; false = all payments are not 'OPEN'
065 */
066 public boolean doBatchPaymentsHaveOpenStatus(Integer batchId) {
067 LOG.debug("doBatchPaymentsHaveOpenStatus() enter method.");
068
069 // check if batch has any payments
070 Map<String, String> fieldValues = new HashMap<String, String>();
071 fieldValues.put(PdpPropertyConstants.PaymentGroup.PAYMENT_GROUP_BATCH_ID, String.valueOf(batchId));
072 List batchPayments = (List)businessObjectService.findMatching(PaymentGroup.class, fieldValues);
073
074 if(ObjectUtils.isNull(batchPayments) || batchPayments.isEmpty())
075 {
076 return false;
077 }
078
079 List codeList = new ArrayList();
080 List statusList = (List) this.businessObjectService.findAll(PaymentStatus.class);
081 for (Iterator i = statusList.iterator(); i.hasNext();) {
082 PaymentStatus element = (PaymentStatus) i.next();
083 if (!(element.getCode().equals(PaymentStatusCodes.OPEN))) {
084 codeList.add(element.getCode());
085 }
086 }
087
088 Criteria crit = new Criteria();
089 crit.addEqualTo(PdpPropertyConstants.PaymentGroup.PAYMENT_GROUP_BATCH_ID, batchId);
090 crit.addIn(PdpPropertyConstants.PaymentGroup.PAYMENT_GROUP_PAYMENT_STATUS_CODE, codeList);
091
092 ReportQueryByCriteria q = QueryFactory.newReportQuery(PaymentGroup.class, crit);
093 q.setAttributes(new String[] { PdpPropertyConstants.PaymentGroup.PAYMENT_GROUP_PAYMENT_STATUS_CODE });
094 q.addGroupBy(PdpPropertyConstants.PaymentGroup.PAYMENT_GROUP_PAYMENT_STATUS_CODE);
095
096 Iterator i = getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(q);
097 if (i.hasNext()) {
098 LOG.debug("doBatchPaymentsHaveOpenStatus() Not all payment groups have status 'OPEN'.");
099 TransactionalServiceUtils.exhaustIterator(i);
100 return false;
101 }
102 else {
103 LOG.debug("doBatchPaymentsHaveOpenStatus() All payment groups have status 'OPEN'.");
104 return true;
105 }
106 }
107
108 /**
109 * doBatchPaymentsHaveHeldStatus() Return true if all payments in batch have an 'HELD' status. Return false if all payments in
110 * batch do not have an 'HELD' status. The query in this method searches the payment detail table for payments of the given
111 * batchId where the status equals any status other than 'HELD'. If any rows exist with a status other than 'HELD', return
112 * false.
113 *
114 * @param batchId Integer value of batch id of payments to search.
115 * @return boolean true = all payments are 'HELD'; false = all payments are not 'HELD'
116 */
117 public boolean doBatchPaymentsHaveHeldStatus(Integer batchId) {
118 LOG.debug("doBatchPaymentsHaveHeldStatus() enter method.");
119
120 // check if batch has any payments
121 Map<String, String> fieldValues = new HashMap<String, String>();
122 fieldValues.put(PdpPropertyConstants.PaymentGroup.PAYMENT_GROUP_BATCH_ID, String.valueOf(batchId));
123 List batchPayments = (List)businessObjectService.findMatching(PaymentGroup.class, fieldValues);
124
125 if(ObjectUtils.isNull(batchPayments) || batchPayments.isEmpty())
126 {
127 return false;
128 }
129
130 List codeList = new ArrayList();
131 List statusList = (List) this.businessObjectService.findAll(PaymentStatus.class);
132 for (Iterator i = statusList.iterator(); i.hasNext();) {
133 PaymentStatus element = (PaymentStatus) i.next();
134 if (!(element.getCode().equals(PaymentStatusCodes.HELD_CD))) {
135 codeList.add(element.getCode());
136 }
137 }
138
139 Criteria crit = new Criteria();
140 crit.addEqualTo(PdpPropertyConstants.PaymentGroup.PAYMENT_GROUP_BATCH_ID, batchId);
141 crit.addIn(PdpPropertyConstants.PaymentGroup.PAYMENT_GROUP_PAYMENT_STATUS_CODE, codeList);
142
143 ReportQueryByCriteria q = QueryFactory.newReportQuery(PaymentGroup.class, crit);
144 q.setAttributes(new String[] { PdpPropertyConstants.PaymentGroup.PAYMENT_GROUP_PAYMENT_STATUS_CODE });
145 q.addGroupBy(PdpPropertyConstants.PaymentGroup.PAYMENT_GROUP_PAYMENT_STATUS_CODE);
146
147 Iterator i = getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(q);
148 if (i.hasNext()) {
149 LOG.debug("doBatchPaymentsHaveHeldStatus() Not all payment groups have status 'HELD'.");
150 TransactionalServiceUtils.exhaustIterator(i);
151 return false;
152 }
153 else {
154 LOG.debug("doBatchPaymentsHaveHeldStatus() All payment groups have status 'HELD'.");
155 return true;
156 }
157 }
158 }