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  /*
17   * Created on Aug 12, 2004
18   */
19  package org.kuali.ole.pdp.service.impl;
20  
21  import java.sql.Timestamp;
22  import java.util.Date;
23  import java.util.HashMap;
24  import java.util.List;
25  import java.util.Map;
26  
27  import org.kuali.ole.pdp.PdpConstants;
28  import org.kuali.ole.pdp.PdpKeyConstants;
29  import org.kuali.ole.pdp.PdpPropertyConstants;
30  import org.kuali.ole.pdp.businessobject.PaymentChangeCode;
31  import org.kuali.ole.pdp.businessobject.PaymentGroup;
32  import org.kuali.ole.pdp.businessobject.PaymentGroupHistory;
33  import org.kuali.ole.pdp.businessobject.PaymentStatus;
34  import org.kuali.ole.pdp.dataaccess.BatchMaintenanceDao;
35  import org.kuali.ole.pdp.service.BatchMaintenanceService;
36  import org.kuali.ole.pdp.service.PaymentGroupService;
37  import org.kuali.rice.kim.api.identity.Person;
38  import org.kuali.rice.krad.service.BusinessObjectService;
39  import org.kuali.rice.krad.util.GlobalVariables;
40  import org.kuali.rice.krad.util.KRADConstants;
41  import org.springframework.transaction.annotation.Transactional;
42  
43  /**
44   * This class provides methods for Batch maintenance.
45   */
46  @Transactional
47  public class BatchMaintenanceServiceImpl implements BatchMaintenanceService {
48      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(BatchMaintenanceServiceImpl.class);
49  
50      private BatchMaintenanceDao batchMaintenanceDao;
51      private BusinessObjectService businessObjectService;
52      private PaymentGroupService paymentGroupService;
53  
54      /**
55       * This method changes the status for PaymentGroup and PaymentGroupHistory.
56       * 
57       * @param paymentGroup the PaymentGroup to change the status
58       * @param newPaymentStatus the new payment status
59       * @param changeStatus the payment change status code
60       * @param note a note from the user
61       */
62      public void changeStatus(PaymentGroup paymentGroup, String newPaymentStatus, String changeStatus, String note, Person user) {
63          if (LOG.isDebugEnabled()) {
64              LOG.debug("changeStatus() enter method with new status of " + newPaymentStatus);
65          }
66  
67          PaymentGroupHistory paymentGroupHistory = new PaymentGroupHistory();
68          PaymentChangeCode paymentChange = (PaymentChangeCode) businessObjectService.findBySinglePrimaryKey(PaymentChangeCode.class, changeStatus);
69  
70          paymentGroupHistory.setPaymentChange(paymentChange);
71          paymentGroupHistory.setOrigPaymentStatus(paymentGroup.getPaymentStatus());
72          paymentGroupHistory.setChangeUser(user);
73          paymentGroupHistory.setChangeNoteText(note);
74          paymentGroupHistory.setPaymentGroup(paymentGroup);
75          paymentGroupHistory.setChangeTime(new Timestamp(new Date().getTime()));
76  
77          this.businessObjectService.save(paymentGroupHistory);
78  
79          PaymentStatus paymentStatus = (PaymentStatus) businessObjectService.findBySinglePrimaryKey(PaymentStatus.class, newPaymentStatus);
80  
81          paymentGroup.setPaymentStatus(paymentStatus);
82  
83          this.businessObjectService.save(paymentGroup);
84  
85          LOG.debug("changeStatus() Status has been changed; exit method.");
86      }
87  
88      /**
89       * cancelPendingBatch() This method cancels a pending batch by canceling each payment in the batch if the following rules apply.
90       * - All payments must have a status of 'OPEN'.
91       * 
92       * @param paymentBatchId (Integer) Primary key of the Pending Batch to be canceled.
93       * @param note (String) Change note text entered by user.
94       * @param user (User) Actor making change.
95       */
96      public boolean cancelPendingBatch(Integer paymentBatchId, String note, Person user) {
97          if (LOG.isDebugEnabled()) {
98              LOG.debug("cancelPendingBatch() Enter method to cancel batch with id = " + paymentBatchId);
99          }
100 
101         if (doBatchPaymentsHaveOpenOrHeldStatus(paymentBatchId)) {
102             List<PaymentGroup> paymentGroupList = this.paymentGroupService.getByBatchId(paymentBatchId);
103 
104             if (paymentGroupList == null || paymentGroupList.isEmpty()) {
105                 LOG.debug("cancelPendingBatch() Pending payment groups not found for batchId; throw exception.");
106 
107                 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, PdpKeyConstants.BatchConstants.ErrorMessages.ERROR_PENDING_PAYMNET_GROUP_NOT_FOUND);
108 
109                 return false;
110             }
111 
112             // cancel each payment
113             // All actions must be performed on entire group not individual detail record
114             for (PaymentGroup paymentGroup : paymentGroupList) {
115                 changeStatus(paymentGroup, PdpConstants.PaymentStatusCodes.CANCEL_PAYMENT, PdpConstants.PaymentChangeCodes.CANCEL_BATCH_CHNG_CD, note, user);
116             }
117 
118             LOG.debug("cancelPendingBatch() All payment groups in batch have been canceled; exit method.");
119         }
120         else {
121             LOG.debug("cancelPendingBatch() Not all payment groups are open; cannot cancel batch.");
122 
123             GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, PdpKeyConstants.BatchConstants.ErrorMessages.ERROR_NOT_ALL_PAYMENT_GROUPS_OPEN_CANNOT_CANCEL);
124 
125             return false;
126         }
127         return true;
128     }
129 
130     /**
131      * holdPendingBatch() This method holds a pending batch by holding each payment in the batch if the following rules apply. - All
132      * payments must have a status of 'OPEN'.
133      * 
134      * @param paymentBatchId (Integer) Primary key of the Pending Batch to be held.
135      * @param note (String) Change note text entered by user.
136      * @param user (User) Actor making change.
137      */
138     public boolean holdPendingBatch(Integer paymentBatchId, String note, Person user) {
139         if (LOG.isDebugEnabled()) {
140             LOG.debug("holdPendingBatch() Enter method to hold batch with id = " + paymentBatchId);
141         }
142 
143         if (doBatchPaymentsHaveOpenStatus(paymentBatchId)) {
144             List<PaymentGroup> paymentGroupList = this.paymentGroupService.getByBatchId(paymentBatchId);
145 
146             if (paymentGroupList == null || paymentGroupList.isEmpty()) {
147                 LOG.debug("holdPendingBatch() Pending payment groups not found for batchId; throw exception.");
148 
149                 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, PdpKeyConstants.BatchConstants.ErrorMessages.ERROR_PENDING_PAYMNET_GROUP_NOT_FOUND);
150 
151                 return false;
152             }
153 
154             // hold each payment
155             // All actions must be performed on entire group not individual detail record
156             for (PaymentGroup paymentGroup : paymentGroupList) {
157                 changeStatus(paymentGroup, PdpConstants.PaymentStatusCodes.HELD_CD, PdpConstants.PaymentChangeCodes.HOLD_BATCH_CHNG_CD, note, user);
158             }
159 
160             LOG.debug("holdPendingBatch() All payment groups in batch have been held; exit method.");
161         }
162         else {
163             LOG.debug("holdPendingBatch() Not all payment groups are open; cannot hold batch.");
164 
165             GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, PdpKeyConstants.BatchConstants.ErrorMessages.ERROR_NOT_ALL_PAYMENT_GROUPS_OPEN_CANNOT_HOLD);
166 
167             return false;
168         }
169         return true;
170     }
171 
172     /**
173      * removeBatchHold() This method removes holds on batches of payments if the following rules apply. - All Payments' statuses in
174      * batch must be: "held".
175      * 
176      * @param paymentBatchId (Integer) Primary key of the Pending Batch to be released from hold.
177      * @param note (String) Change note text entered by user.
178      * @param user (User) Actor making change.
179      */
180     public boolean removeBatchHold(Integer paymentBatchId, String note, Person user) {
181         if (LOG.isDebugEnabled()) {
182             LOG.debug("removeBatchHold() Enter method to remove hold batch with id = " + paymentBatchId);
183         }
184 
185         if (doBatchPaymentsHaveHeldStatus(paymentBatchId)) {
186             List<PaymentGroup> paymentGroupList = this.paymentGroupService.getByBatchId(paymentBatchId);
187 
188             if (paymentGroupList == null || paymentGroupList.isEmpty()) {
189                 LOG.debug("removeBatchHold() Pending payment groups not found for batchId; throw exception.");
190 
191                 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, PdpKeyConstants.BatchConstants.ErrorMessages.ERROR_PENDING_PAYMNET_GROUP_NOT_FOUND);
192 
193                 return false;
194             }
195 
196             // hold each payment
197             // All actions must be performed on entire group not individual detail record
198             for (PaymentGroup paymentGroup : paymentGroupList) {
199                 changeStatus(paymentGroup, PdpConstants.PaymentStatusCodes.OPEN, PdpConstants.PaymentChangeCodes.REMOVE_HOLD_BATCH_CHNG_CD, note, user);
200             }
201 
202             LOG.debug("removeBatchHold() All payment groups in batch have been held; exit method.");
203         }
204         else {
205             LOG.debug("removeBatchHold() Not all payment groups are open; cannot remove hold on batch.");
206 
207             GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, PdpKeyConstants.BatchConstants.ErrorMessages.ERROR_NOT_ALL_PAYMENT_GROUPS_OPEN_CANNOT_REMOVE_HOLD);
208 
209             return false;
210         }
211         return true;
212 
213     }
214 
215     /**
216      * @see org.kuali.ole.pdp.document.service.BatchMaintenanceService#doBatchPaymentsHaveOpenStatus(java.lang.Integer)
217      */
218     public boolean doBatchPaymentsHaveOpenStatus(Integer batchId) {
219         // check if batch has any payments
220         Map<String, String> fieldValues = new HashMap<String, String>();
221         fieldValues.put(PdpPropertyConstants.PaymentGroup.PAYMENT_GROUP_BATCH_ID, String.valueOf(batchId));
222         List batchPayments = (List) businessObjectService.findMatching(PaymentGroup.class, fieldValues);
223 
224         List statusList = (List) this.businessObjectService.findAll(PaymentStatus.class);
225 
226         return batchMaintenanceDao.doBatchPaymentsHaveOpenStatus(batchId, batchPayments, statusList);
227     }
228 
229     /**
230      * @see org.kuali.ole.pdp.document.service.BatchMaintenanceService#doBatchPaymentsHaveHeldStatus(java.lang.Integer)
231      */
232     public boolean doBatchPaymentsHaveHeldStatus(Integer batchId) {
233         // check if batch has any payments
234         Map<String, String> fieldValues = new HashMap<String, String>();
235         fieldValues.put(PdpPropertyConstants.PaymentGroup.PAYMENT_GROUP_BATCH_ID, String.valueOf(batchId));
236         List batchPayments = (List) businessObjectService.findMatching(PaymentGroup.class, fieldValues);
237         List statusList = (List) this.businessObjectService.findAll(PaymentStatus.class);
238 
239         return batchMaintenanceDao.doBatchPaymentsHaveHeldStatus(batchId, batchPayments, statusList);
240     }
241 
242     /**
243      * @see org.kuali.ole.pdp.document.service.BatchMaintenanceService#doBatchPaymentsHaveOpenOrHeldStatus(java.lang.Integer)
244      */
245     public boolean doBatchPaymentsHaveOpenOrHeldStatus(Integer batchId) {
246         LOG.debug("doBatchPaymentsHaveOpenOrHeldStatus() enter method.");
247 
248         if ((doBatchPaymentsHaveOpenStatus(batchId)) || (doBatchPaymentsHaveHeldStatus(batchId))) {
249             LOG.debug("doBatchPaymentsHaveOpenOrHeldStatus() All payment groups have status 'HELD' or all payments have status 'OPEN'.");
250 
251             return true;
252         }
253         else {
254             LOG.debug("doBatchPaymentsHaveOpenOrHeldStatus() Not all payment groups have status 'HELD' or not all payments have status 'OPEN'.");
255 
256             return false;
257         }
258     }
259 
260     /**
261      * This method sets the batchMaintenanceDao
262      * 
263      * @param batchMaintenanceDao BatchMaintenanceDao
264      */
265     public void setBatchMaintenanceDao(BatchMaintenanceDao batchMaintenanceDao) {
266         this.batchMaintenanceDao = batchMaintenanceDao;
267     }
268 
269     /**
270      * Sets the business object service
271      * 
272      * @param businessObjectService
273      */
274     public void setBusinessObjectService(BusinessObjectService businessObjectService) {
275         this.businessObjectService = businessObjectService;
276     }
277 
278     public void setPaymentGroupService(PaymentGroupService paymentGroupService) {
279         this.paymentGroupService = paymentGroupService;
280     }
281 
282 }