View Javadoc
1   /*
2    * Copyright 2006 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  package org.kuali.ole.fp.document.web.struts;
17  
18  import javax.servlet.http.HttpServletRequest;
19  import javax.servlet.http.HttpServletResponse;
20  
21  import org.apache.struts.action.ActionForm;
22  import org.apache.struts.action.ActionForward;
23  import org.apache.struts.action.ActionMapping;
24  import org.kuali.ole.fp.businessobject.CreditCardDetail;
25  import org.kuali.ole.fp.document.CreditCardReceiptDocument;
26  import org.kuali.ole.fp.document.validation.impl.CreditCardReceiptDocumentRuleUtil;
27  import org.kuali.ole.sys.OLEConstants;
28  import org.kuali.ole.sys.OLEPropertyConstants;
29  import org.kuali.ole.sys.context.SpringContext;
30  import org.kuali.ole.sys.service.BankService;
31  import org.kuali.rice.kew.api.exception.WorkflowException;
32  import org.kuali.rice.kns.web.struts.form.KualiDocumentFormBase;
33  import org.kuali.rice.krad.util.GlobalVariables;
34  
35  /**
36   * This is the action class for the CreditCardReceiptDocument.
37   */
38  public class CreditCardReceiptAction extends CapitalAccountingLinesActionBase {
39      /**
40       * Adds handling for credit card receipt amount updates.
41       * 
42       * @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm,
43       *      javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
44       */
45      @Override
46      public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
47          CreditCardReceiptForm ccrForm = (CreditCardReceiptForm) form;
48  
49          if (ccrForm.hasDocumentId()) {
50              CreditCardReceiptDocument ccrDoc = ccrForm.getCreditCardReceiptDocument();
51  
52              ccrDoc.setTotalCreditCardAmount(ccrDoc.calculateCreditCardReceiptTotal()); // recalc b/c changes to the amounts could
53              // have happened
54  
55              //set bank
56              ccrDoc.setBank(SpringContext.getBean(BankService.class).getByPrimaryId(ccrDoc.getCreditCardReceiptBankCode()));
57          }
58  
59          // proceed as usual
60          return super.execute(mapping, form, request, response);
61      }
62  
63      /**
64       * Adds a CreditCardDetail instance created from the current "new creditCardReceipt" line to the document
65       * 
66       * @param mapping
67       * @param form
68       * @param request
69       * @param response
70       * @return ActionForward
71       * @throws Exception
72       */
73      public ActionForward addCreditCardReceipt(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
74          CreditCardReceiptForm ccrForm = (CreditCardReceiptForm) form;
75          CreditCardReceiptDocument ccrDoc = ccrForm.getCreditCardReceiptDocument();
76  
77          CreditCardDetail newCreditCardReceipt = ccrForm.getNewCreditCardReceipt();
78          ccrDoc.prepareNewCreditCardReceipt(newCreditCardReceipt);
79  
80          // creditCardReceipt business rules
81          boolean rulePassed = validateNewCreditCardReceipt(newCreditCardReceipt);
82          if (rulePassed) {
83              // add creditCardReceipt
84              ccrDoc.addCreditCardReceipt(newCreditCardReceipt);
85  
86              // clear the used creditCardReceipt
87              ccrForm.setNewCreditCardReceipt(new CreditCardDetail());
88          }
89  
90          return mapping.findForward(OLEConstants.MAPPING_BASIC);
91      }
92  
93      /**
94       * Deletes the selected creditCardReceipt (line) from the document
95       * 
96       * @param mapping
97       * @param form
98       * @param request
99       * @param response
100      * @return ActionForward
101      * @throws Exception
102      */
103     public ActionForward deleteCreditCardReceipt(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
104         CreditCardReceiptForm ccrForm = (CreditCardReceiptForm) form;
105         CreditCardReceiptDocument ccrDoc = ccrForm.getCreditCardReceiptDocument();
106 
107         int deleteIndex = getLineToDelete(request);
108         // delete creditCardReceipt
109         ccrDoc.removeCreditCardReceipt(deleteIndex);
110 
111         return mapping.findForward(OLEConstants.MAPPING_BASIC);
112     }
113 
114     /**
115      * This method validates a new credit card receipt detail record.
116      * 
117      * @param creditCardReceipt
118      * @return boolean
119      */
120     protected boolean validateNewCreditCardReceipt(CreditCardDetail creditCardReceipt) {
121         GlobalVariables.getMessageMap().addToErrorPath(OLEPropertyConstants.NEW_CREDIT_CARD_RECEIPT);
122         boolean isValid = CreditCardReceiptDocumentRuleUtil.validateCreditCardReceipt(creditCardReceipt);
123         GlobalVariables.getMessageMap().removeFromErrorPath(OLEPropertyConstants.NEW_CREDIT_CARD_RECEIPT);
124         return isValid;
125     }
126 
127     /**
128      * Do initialization for a new credit card receipt
129      *
130      * @see org.kuali.rice.kns.web.struts.action.KualiDocumentActionBase#createDocument(org.kuali.rice.kns.web.struts.form.KualiDocumentFormBase)
131      */
132     @Override
133     protected void createDocument(KualiDocumentFormBase kualiDocumentFormBase) throws WorkflowException {
134         super.createDocument(kualiDocumentFormBase);
135         ((CreditCardReceiptDocument) kualiDocumentFormBase.getDocument()).initiateDocument();
136     }
137 }