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    package org.kuali.kfs.pdp.dataaccess.impl;
017    
018    import java.sql.Timestamp;
019    import java.util.ArrayList;
020    import java.util.Calendar;
021    import java.util.Date;
022    import java.util.Iterator;
023    import java.util.List;
024    
025    import org.apache.ojb.broker.query.Criteria;
026    import org.apache.ojb.broker.query.QueryByCriteria;
027    import org.kuali.kfs.pdp.PdpConstants;
028    import org.kuali.kfs.pdp.PdpPropertyConstants;
029    import org.kuali.kfs.pdp.businessobject.CustomerProfile;
030    import org.kuali.kfs.pdp.businessobject.PaymentGroup;
031    import org.kuali.kfs.pdp.businessobject.PaymentProcess;
032    import org.kuali.kfs.pdp.businessobject.PaymentStatus;
033    import org.kuali.kfs.pdp.dataaccess.FormatPaymentDao;
034    import org.kuali.rice.kns.dao.impl.PlatformAwareDaoBaseOjb;
035    import org.kuali.rice.kns.service.BusinessObjectService;
036    
037    public class FormatPaymentDaoOjb extends PlatformAwareDaoBaseOjb implements FormatPaymentDao {
038        private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(FormatPaymentDaoOjb.class);
039    
040        private BusinessObjectService businessObjectService;
041    
042        /**
043         * @see org.kuali.kfs.pdp.dataaccess.FormatPaymentDao#markPaymentsForFormat(org.kuali.kfs.pdp.businessobject.PaymentProcess,
044         *      java.util.List, java.util.Date, java.lang.String)
045         */
046        public void markPaymentsForFormat(PaymentProcess proc, List customers, Date paydate, String paymentTypes) {
047            LOG.debug("markPaymentsForFormat() started");
048    
049            Timestamp now = new Timestamp((new Date()).getTime());
050            java.sql.Date sqlDate = new java.sql.Date(paydate.getTime());
051            Calendar c = Calendar.getInstance();
052            c.setTime(sqlDate);
053            c.set(Calendar.HOUR, 11);
054            c.set(Calendar.MINUTE, 59);
055            c.set(Calendar.SECOND, 59);
056            c.set(Calendar.MILLISECOND, 59);
057            c.set(Calendar.AM_PM, Calendar.PM);
058            Timestamp paydateTs = new Timestamp(c.getTime().getTime());
059    
060            LOG.debug("markPaymentsForFormat() last update = " + now);
061            LOG.debug("markPaymentsForFormat() entered paydate = " + paydate);
062            LOG.debug("markPaymentsForFormat() actual paydate = " + paydateTs);
063    
064            PaymentStatus format = (PaymentStatus) this.businessObjectService.findBySinglePrimaryKey(PaymentStatus.class, PdpConstants.PaymentStatusCodes.FORMAT);
065    
066            List customerIds = new ArrayList();
067            for (Iterator iter = customers.iterator(); iter.hasNext();) {
068                CustomerProfile element = (CustomerProfile) iter.next();
069                customerIds.add(element.getId());
070            }
071    
072            Criteria criteria = new Criteria();
073    
074            if (customerIds.size() > 0) {
075                criteria.addIn(PdpPropertyConstants.PaymentGroup.PAYMENT_GROUP_BATCH + "." + PdpPropertyConstants.BatchConstants.CUSTOMER_ID, customerIds);
076            }
077            else {
078                // no payments to mark as no customer was selected
079                return;
080            }
081    
082            criteria.addEqualTo(PdpPropertyConstants.PaymentGroup.PAYMENT_GROUP_PAYMENT_STATUS_CODE, PdpConstants.PaymentStatusCodes.OPEN);
083    
084            if (PdpConstants.PaymentTypes.DISBURSEMENTS_WITH_SPECIAL_HANDLING.equals(paymentTypes)) {
085                // special handling only
086                criteria.addEqualTo(PdpPropertyConstants.PaymentGroup.PAYMENT_SPECIAL_HANDLING, Boolean.TRUE);
087            }
088            else if (PdpConstants.PaymentTypes.DISBURSEMENTS_NO_SPECIAL_HANDLING.equals(paymentTypes)) {
089                // no special handling only
090                criteria.addEqualTo(PdpPropertyConstants.PaymentGroup.PAYMENT_SPECIAL_HANDLING, Boolean.FALSE);
091            }
092            else if (PdpConstants.PaymentTypes.DISBURSEMENTS_WITH_ATTACHMENTS.equals(paymentTypes)) {
093                // attachments only
094                criteria.addEqualTo(PdpPropertyConstants.PaymentGroup.PAYMENT_ATTACHMENT, Boolean.TRUE);
095            }
096            else if (PdpConstants.PaymentTypes.DISBURSEMENTS_NO_ATTACHMENTS.equals(paymentTypes)) {
097                // no attachments only
098                criteria.addEqualTo(PdpPropertyConstants.PaymentGroup.PAYMENT_ATTACHMENT, Boolean.FALSE);
099            }
100    
101            if (PdpConstants.PaymentTypes.PROCESS_IMMEDIATE.equals(paymentTypes)) {
102                criteria.addEqualTo(PdpPropertyConstants.PaymentGroup.PROCESS_IMMEDIATE, Boolean.TRUE);
103            }
104            else {
105                // (Payment date <= usePaydate OR immediate = TRUE)
106                Criteria criteria1 = new Criteria();
107                criteria1.addEqualTo(PdpPropertyConstants.PaymentGroup.PROCESS_IMMEDIATE, Boolean.TRUE);
108    
109                Criteria criteria2 = new Criteria();
110                criteria2.addLessOrEqualThan(PdpPropertyConstants.PaymentGroup.PAYMENT_DATE, paydateTs);
111                criteria1.addOrCriteria(criteria2);
112    
113                criteria.addAndCriteria(criteria1);
114            }
115    
116            Iterator groupIterator = getPersistenceBrokerTemplate().getIteratorByQuery(new QueryByCriteria(PaymentGroup.class, criteria));
117            while (groupIterator.hasNext()) {
118                PaymentGroup paymentGroup = (PaymentGroup) groupIterator.next();
119                paymentGroup.setLastUpdate(paydateTs);// delete this one
120                paymentGroup.setPaymentStatus(format);
121                paymentGroup.setProcess(proc);
122                getPersistenceBrokerTemplate().store(paymentGroup);
123            }
124        }
125    
126        /**
127         * @see org.kuali.kfs.pdp.dataaccess.FormatPaymentDao#unmarkPaymentsForFormat(org.kuali.kfs.pdp.businessobject.PaymentProcess)
128         */
129        public void unmarkPaymentsForFormat(PaymentProcess proc) {
130            LOG.debug("unmarkPaymentsForFormat() started");
131    
132            Timestamp now = new Timestamp((new Date()).getTime());
133    
134            PaymentStatus openStatus = (PaymentStatus) businessObjectService.findBySinglePrimaryKey(PaymentStatus.class, PdpConstants.PaymentStatusCodes.OPEN);
135    
136            Criteria criteria = new Criteria();
137            criteria.addEqualTo(PdpPropertyConstants.PaymentGroup.PAYMENT_GROUP_PROCESS_ID, proc.getId());
138            criteria.addEqualTo(PdpPropertyConstants.PaymentGroup.PAYMENT_GROUP_PAYMENT_STATUS_CODE, PdpConstants.PaymentStatusCodes.FORMAT);
139    
140            Iterator groupIterator = getPersistenceBrokerTemplate().getIteratorByQuery(new QueryByCriteria(PaymentGroup.class, criteria));
141            while (groupIterator.hasNext()) {
142                PaymentGroup paymentGroup = (PaymentGroup) groupIterator.next();
143                paymentGroup.setLastUpdate(now);
144                paymentGroup.setPaymentStatus(openStatus);
145                getPersistenceBrokerTemplate().store(paymentGroup);
146            }
147        }
148    
149        /**
150         * Gets the businessObjectService attribute.
151         * 
152         * @return Returns the businessObjectService.
153         */
154        public BusinessObjectService getBusinessObjectService() {
155            return businessObjectService;
156        }
157    
158        /**
159         * Sets the businessObjectService attribute value.
160         * 
161         * @param businessObjectService The businessObjectService to set.
162         */
163        public void setBusinessObjectService(BusinessObjectService businessObjectService) {
164            this.businessObjectService = businessObjectService;
165        }
166    
167    }