1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
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  
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  
56  
57  
58  
59  
60  
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  
90  
91  
92  
93  
94  
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             
113             
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 
132 
133 
134 
135 
136 
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             
155             
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 
174 
175 
176 
177 
178 
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             
197             
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 
217 
218     public boolean doBatchPaymentsHaveOpenStatus(Integer batchId) {
219         
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 
231 
232     public boolean doBatchPaymentsHaveHeldStatus(Integer batchId) {
233         
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 
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 
262 
263 
264 
265     public void setBatchMaintenanceDao(BatchMaintenanceDao batchMaintenanceDao) {
266         this.batchMaintenanceDao = batchMaintenanceDao;
267     }
268 
269     
270 
271 
272 
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 }