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: /*
017: * Created on Aug 12, 2004
018: */
019: package org.kuali.module.pdp.service.impl;
020:
021: import java.util.Iterator;
022: import java.util.List;
023:
024: import org.kuali.module.pdp.bo.Code;
025: import org.kuali.module.pdp.bo.PaymentChange;
026: import org.kuali.module.pdp.bo.PaymentGroup;
027: import org.kuali.module.pdp.bo.PaymentGroupHistory;
028: import org.kuali.module.pdp.bo.PaymentStatus;
029: import org.kuali.module.pdp.bo.PdpUser;
030: import org.kuali.module.pdp.dao.BatchMaintenanceDao;
031: import org.kuali.module.pdp.dao.PaymentGroupDao;
032: import org.kuali.module.pdp.dao.PaymentGroupHistoryDao;
033: import org.kuali.module.pdp.exception.CancelPaymentException;
034: import org.kuali.module.pdp.exception.PdpException;
035: import org.kuali.module.pdp.service.BatchMaintenanceService;
036: import org.kuali.module.pdp.service.ReferenceService;
037: import org.springframework.transaction.annotation.Transactional;
038:
039: /**
040: * @author HSTAPLET
041: */
042: @Transactional
043: public class BatchMaintenanceServiceImpl implements
044: BatchMaintenanceService {
045: private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
046: .getLogger(BatchMaintenanceServiceImpl.class);
047:
048: // Payment Status Codes
049: private static String HELD_CD = "HELD";
050: private static String OPEN_CD = "OPEN";
051: private static String CANCEL_PAYMENT_CD = "CPAY";
052:
053: // Payment Change Codes
054: private static String CANCEL_BATCH_CHNG_CD = "CB";
055: private static String HOLD_BATCH_CHNG_CD = "HB";
056: private static String REMOVE_HOLD_BATCH_CHNG_CD = "RHB";
057:
058: private BatchMaintenanceDao batchMaintenanceDao;
059: private PaymentGroupDao paymentGroupDao;
060: private PaymentGroupHistoryDao paymentGroupHistoryDao;
061: private ReferenceService referenceService;
062:
063: public void changeStatus(PaymentGroup paymentGroup,
064: String newPaymentStatus, String changeStatus, String note,
065: PdpUser user) {
066: LOG.debug("changeStatus() enter method with new status of "
067: + newPaymentStatus);
068: PaymentGroupHistory paymentGroupHistory = new PaymentGroupHistory();
069: Code cd = referenceService.getCode("PaymentChange",
070: changeStatus);
071: paymentGroupHistory.setPaymentChange((PaymentChange) cd);
072: paymentGroupHistory.setOrigPaymentStatus(paymentGroup
073: .getPaymentStatus());
074: paymentGroupHistory.setChangeUser(user);
075: paymentGroupHistory.setChangeNoteText(note);
076: paymentGroupHistory.setPaymentGroup(paymentGroup);
077: paymentGroupHistoryDao.save(paymentGroupHistory);
078:
079: Code code = referenceService.getCode("PaymentStatus",
080: newPaymentStatus);
081: paymentGroup.setPaymentStatus((PaymentStatus) code);
082: paymentGroupDao.save(paymentGroup);
083: LOG
084: .debug("changeStatus() Status has been changed; exit method.");
085: }
086:
087: /**
088: * cancelPendingBatch() This method cancels a pending batch by canceling each payment in the batch if the following rules apply. -
089: * All payments must have a status of 'OPEN'.
090: *
091: * @param paymentBatchId (Integer) Primary key of the Pending Batch to be canceled.
092: * @param note (String) Change note text entered by user.
093: * @param user (User) Actor making change.
094: */
095: public void cancelPendingBatch(Integer paymentBatchId, String note,
096: PdpUser user) throws PdpException {
097: LOG
098: .debug("cancelPendingBatch() Enter method to cancel batch with id = "
099: + paymentBatchId);
100: if (doBatchPaymentsHaveOpenOrHeldStatus(paymentBatchId)) {
101: List paymentGroupList = paymentGroupDao
102: .getByBatchId(paymentBatchId);
103: if (paymentGroupList == null || paymentGroupList.isEmpty()) {
104: LOG
105: .debug("cancelPendingBatch() Pending payment groups not found for batchId; throw exception.");
106: throw new CancelPaymentException(
107: "Pending payment groups not found for batchId.");
108: }
109:
110: // cancel each payment
111: // All actions must be performed on entire group not individual detail record
112: for (Iterator iter = paymentGroupList.iterator(); iter
113: .hasNext();) {
114: PaymentGroup element = (PaymentGroup) iter.next();
115: changeStatus(element, CANCEL_PAYMENT_CD,
116: CANCEL_BATCH_CHNG_CD, note, user);
117: }
118: LOG
119: .debug("cancelPendingBatch() All payment groups in batch have been canceled; exit method.");
120: } else {
121: LOG
122: .debug("cancelPendingBatch() Not all payment groups are open; cannot cancel batch.");
123: throw new CancelPaymentException(
124: "Not all payment groups are open; cannot cancel batch.");
125: }
126: }// end cancelPendingBatch()
127:
128: /**
129: * holdPendingBatch() This method holds a pending batch by holding each payment in the batch if the following rules apply. - All
130: * payments must have a status of 'OPEN'.
131: *
132: * @param paymentBatchId (Integer) Primary key of the Pending Batch to be held.
133: * @param note (String) Change note text entered by user.
134: * @param user (User) Actor making change.
135: */
136: public void holdPendingBatch(Integer paymentBatchId, String note,
137: PdpUser user) throws PdpException {
138: LOG
139: .debug("holdPendingBatch() Enter method to hold batch with id = "
140: + paymentBatchId);
141: if (doBatchPaymentsHaveOpenStatus(paymentBatchId)) {
142: List paymentGroupList = paymentGroupDao
143: .getByBatchId(paymentBatchId);
144: if (paymentGroupList == null || paymentGroupList.isEmpty()) {
145: LOG
146: .debug("holdPendingBatch() Pending payment groups not found for batchId; throw exception.");
147: throw new CancelPaymentException(
148: "Pending payment groups not found for batchId.");
149: }
150:
151: // hold each payment
152: // All actions must be performed on entire group not individual detail record
153: for (Iterator iter = paymentGroupList.iterator(); iter
154: .hasNext();) {
155: PaymentGroup element = (PaymentGroup) iter.next();
156: changeStatus(element, HELD_CD, HOLD_BATCH_CHNG_CD,
157: note, user);
158: }
159: LOG
160: .debug("holdPendingBatch() All payment groups in batch have been held; exit method.");
161: } else {
162: LOG
163: .debug("holdPendingBatch() Not all payment groups are open; cannot hold batch.");
164: throw new CancelPaymentException(
165: "Not all payment groups are open; cannot hold batch.");
166: }
167: }// end holdPendingBatch()
168:
169: /**
170: * removeBatchHold() This method removes holds on batches of payments if the following rules apply. - All Payments' statuses in
171: * batch must be: "held".
172: *
173: * @param paymentBatchId (Integer) Primary key of the Pending Batch to be released from hold.
174: * @param note (String) Change note text entered by user.
175: * @param user (User) Actor making change.
176: */
177: public void removeBatchHold(Integer paymentBatchId, String note,
178: PdpUser user) throws PdpException {
179: LOG
180: .debug("removeBatchHold() Enter method to hold batch with id = "
181: + paymentBatchId);
182: if (doBatchPaymentsHaveHeldStatus(paymentBatchId)) {
183: List paymentGroupList = paymentGroupDao
184: .getByBatchId(paymentBatchId);
185: if (paymentGroupList == null || paymentGroupList.isEmpty()) {
186: LOG
187: .debug("removeBatchHold() Pending payment groups not found for batchId; throw exception.");
188: throw new CancelPaymentException(
189: "Pending payment groups not found for batchId.");
190: }
191:
192: // hold each payment
193: // All actions must be performed on entire group not individual detail record
194: for (Iterator iter = paymentGroupList.iterator(); iter
195: .hasNext();) {
196: PaymentGroup element = (PaymentGroup) iter.next();
197: changeStatus(element, OPEN_CD,
198: REMOVE_HOLD_BATCH_CHNG_CD, note, user);
199: }
200: LOG
201: .debug("holdPendingBatch() All payment groups in batch have been held; exit method.");
202: } else {
203: LOG
204: .debug("holdPendingBatch() Not all payment groups are open; cannot hold batch.");
205: throw new CancelPaymentException(
206: "Not all payment groups are open; cannot hold batch.");
207: }
208:
209: }// end removeBatchHold()
210:
211: public boolean doBatchPaymentsHaveOpenStatus(Integer batchId) {
212: return batchMaintenanceDao
213: .doBatchPaymentsHaveOpenStatus(batchId);
214: }
215:
216: public boolean doBatchPaymentsHaveHeldStatus(Integer batchId) {
217: return batchMaintenanceDao
218: .doBatchPaymentsHaveHeldStatus(batchId);
219: }
220:
221: public boolean doBatchPaymentsHaveOpenOrHeldStatus(Integer batchId) {
222: LOG
223: .debug("doBatchPaymentsHaveOpenOrHeldStatus() enter method.");
224:
225: if ((doBatchPaymentsHaveOpenStatus(batchId))
226: || (doBatchPaymentsHaveHeldStatus(batchId))) {
227: LOG
228: .debug("doBatchPaymentsHaveOpenOrHeldStatus() All payment groups have status 'HELD' or all payments have status 'OPEN'.");
229: return true;
230: } else {
231: LOG
232: .debug("doBatchPaymentsHaveOpenOrHeldStatus() Not all payment groups have status 'HELD' or not all payments have status 'OPEN'.");
233: return false;
234: }
235: }
236:
237: /**
238: * inject
239: *
240: * @param dao
241: */
242: public void setPaymentGroupDao(PaymentGroupDao dao) {
243: paymentGroupDao = dao;
244: }
245:
246: /**
247: * inject
248: *
249: * @param dao
250: */
251: public void setPaymentGroupHistoryDao(PaymentGroupHistoryDao dao) {
252: paymentGroupHistoryDao = dao;
253: }
254:
255: /**
256: * inject
257: *
258: * @param service
259: */
260: public void setReferenceService(ReferenceService service) {
261: referenceService = service;
262: }
263:
264: /**
265: * @param batchMaintenanceDao The batchMaintenanceDao to set.
266: */
267: public void setBatchMaintenanceDao(
268: BatchMaintenanceDao batchMaintenanceDao) {
269: this.batchMaintenanceDao = batchMaintenanceDao;
270: }
271: }
|