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 java.util.Iterator;
19
20 import javax.servlet.http.HttpServletRequest;
21 import javax.servlet.http.HttpServletResponse;
22
23 import org.apache.struts.action.ActionForm;
24 import org.apache.struts.action.ActionForward;
25 import org.apache.struts.action.ActionMapping;
26 import org.kuali.ole.fp.businessobject.AdvanceDepositDetail;
27 import org.kuali.ole.fp.document.AdvanceDepositDocument;
28 import org.kuali.ole.fp.document.validation.impl.AdvanceDepositDocumentRuleUtil;
29 import org.kuali.ole.sys.OLEConstants;
30 import org.kuali.ole.sys.OLEPropertyConstants;
31 import org.kuali.rice.core.api.util.type.KualiDecimal;
32 import org.kuali.rice.krad.util.GlobalVariables;
33
34 /**
35 * This is the action class for the Advance Deposit document.
36 */
37 public class AdvanceDepositAction extends CapitalAccountingLinesActionBase {
38
39 /**
40 * Adds handling for advance deposit detail 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 public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
46 AdvanceDepositForm adForm = (AdvanceDepositForm) form;
47
48 if ( adForm != null && adForm.hasDocumentId()) {
49 AdvanceDepositDocument adDoc = adForm.getAdvanceDepositDocument();
50
51 adDoc.setTotalAdvanceDepositAmount(calculateAdvanceDepositTotal(adDoc)); // recalc b/c changes to the amounts could
52 // have happened
53 }
54
55 // proceed as usual
56 return super.execute(mapping, form, request, response);
57 }
58
59 /* NOTE
60 * The following method was originally added to fix the NPE caused by the reference to AdvanceDepositDocument inside AdvanceDepositDetail.
61 * Now it's commented out as we are fixing the NPE in a better way, by removing the nested reference to AdvanceDepositDocument,
62 * which is not used anymore.
63 */
64 /**
65 * Overridden to ensure that the nested AdvanceDepositDocuments in AdvanceDepositDetails are populated.
66 * @see org.kuali.rice.kns.web.struts.action.KualiTransactionalDocumentActionBase#copy(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
67 *
68 @Override
69 public ActionForward copy(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
70 AdvanceDepositForm adForm = (AdvanceDepositForm) form;
71 AdvanceDepositDocument adDoc = adForm.getAdvanceDepositDocument();
72 List<AdvanceDepositDetail> advanceDeposits = adDoc.getAdvanceDeposits();
73
74 for (AdvanceDepositDetail advanceDeposit: advanceDeposits) {
75 /* somehow ObjectUtils.isNull(nestAdDoc) returns false here
76 AdvanceDepositDocument nestAdDoc = advanceDeposit.getAdvanceDepositDocument();
77 if (ObjectUtils.isNull(nestAdDoc)) {
78 nestAdDoc = null;
79 }
80 *
81 // need to populate the referred document, otherwise there will be NPE when copying
82 advanceDeposit.setAdvanceDepositDocument(adDoc);
83 }
84 return super.copy(mapping, form, request, response);
85 }
86 */
87
88 /**
89 * Adds a AdvanceDepositDetail instance created from the current "new advanceDeposit" line to the document
90 *
91 * @param mapping
92 * @param form
93 * @param request
94 * @param response
95 * @return ActionForward
96 * @throws Exception
97 */
98 public ActionForward addAdvanceDeposit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
99 AdvanceDepositForm adForm = (AdvanceDepositForm) form;
100 AdvanceDepositDocument adDoc = adForm.getAdvanceDepositDocument();
101
102 AdvanceDepositDetail newAdvanceDeposit = adForm.getNewAdvanceDeposit();
103 adDoc.prepareNewAdvanceDeposit(newAdvanceDeposit);
104
105 // advanceDeposit business rules
106 boolean rulePassed = validateNewAdvanceDeposit(newAdvanceDeposit);
107 if (rulePassed) {
108 // add advanceDeposit
109 adDoc.addAdvanceDeposit(newAdvanceDeposit);
110
111 // clear the used advanceDeposit
112 AdvanceDepositDetail advanceDepositDetail = new AdvanceDepositDetail();
113 advanceDepositDetail.setDefaultBankCode();
114 adForm.setNewAdvanceDeposit(advanceDepositDetail);
115 }
116
117 return mapping.findForward(OLEConstants.MAPPING_BASIC);
118 }
119
120 /**
121 * Deletes the selected advanceDeposit (line) from the document
122 *
123 * @param mapping
124 * @param form
125 * @param request
126 * @param response
127 * @return ActionForward
128 * @throws Exception
129 */
130 public ActionForward deleteAdvanceDeposit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
131 AdvanceDepositForm adForm = (AdvanceDepositForm) form;
132 AdvanceDepositDocument adDoc = adForm.getAdvanceDepositDocument();
133
134 int deleteIndex = getLineToDelete(request);
135 // delete advanceDeposit
136 adDoc.removeAdvanceDeposit(deleteIndex);
137
138 return mapping.findForward(OLEConstants.MAPPING_BASIC);
139 }
140
141 /**
142 * This method validates a new advance deposit detail record.
143 *
144 * @param advanceDeposit
145 * @return boolean
146 */
147 protected boolean validateNewAdvanceDeposit(AdvanceDepositDetail advanceDeposit) {
148 GlobalVariables.getMessageMap().addToErrorPath(OLEPropertyConstants.NEW_ADVANCE_DEPOSIT);
149 boolean isValid = AdvanceDepositDocumentRuleUtil.validateAdvanceDeposit(advanceDeposit);
150 GlobalVariables.getMessageMap().removeFromErrorPath(OLEPropertyConstants.NEW_ADVANCE_DEPOSIT);
151 return isValid;
152 }
153
154 /**
155 * Recalculates the advance deposit total since user could have changed it during their update.
156 *
157 * @param advanceDepositDocument
158 */
159 protected KualiDecimal calculateAdvanceDepositTotal(AdvanceDepositDocument advanceDepositDocument) {
160 KualiDecimal total = KualiDecimal.ZERO;
161 Iterator<AdvanceDepositDetail> deposits = advanceDepositDocument.getAdvanceDeposits().iterator();
162 while (deposits.hasNext()) {
163 AdvanceDepositDetail deposit = deposits.next();
164 total = total.add(deposit.getFinancialDocumentAdvanceDepositAmount());
165 }
166 return total;
167 }
168 }