001 /* 002 * Copyright 2011 The Kuali Foundation. 003 * 004 * Licensed under the Educational Community License, Version 2.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/ecl2.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 package org.kuali.kfs.module.endow.document.web.struts; 017 018 import static org.kuali.kfs.module.endow.EndowConstants.EXISTING_SOURCE_TRAN_LINE_PROPERTY_NAME; 019 import static org.kuali.kfs.module.endow.EndowConstants.EXISTING_TARGET_TRAN_LINE_PROPERTY_NAME; 020 021 import javax.servlet.http.HttpServletRequest; 022 import javax.servlet.http.HttpServletResponse; 023 024 import org.apache.commons.lang.StringUtils; 025 import org.apache.struts.action.ActionForm; 026 import org.apache.struts.action.ActionForward; 027 import org.apache.struts.action.ActionMapping; 028 import org.kuali.kfs.module.endow.EndowConstants; 029 import org.kuali.kfs.module.endow.businessobject.EndowmentTransactionLine; 030 import org.kuali.kfs.module.endow.document.AssetDecreaseDocument; 031 import org.kuali.kfs.module.endow.document.EndowmentTaxLotLinesDocument; 032 import org.kuali.kfs.module.endow.document.EndowmentTransactionLinesDocument; 033 import org.kuali.kfs.module.endow.document.EndowmentTransactionLinesDocumentBase; 034 import org.kuali.kfs.module.endow.document.SecurityTransferDocument; 035 import org.kuali.kfs.module.endow.document.validation.event.DeleteTaxLotLineEvent; 036 import org.kuali.kfs.module.endow.document.validation.event.RefreshTransactionLineEvent; 037 import org.kuali.kfs.sys.KFSConstants; 038 import org.kuali.kfs.sys.businessobject.FinancialSystemDocumentHeader; 039 import org.kuali.kfs.sys.context.SpringContext; 040 import org.kuali.kfs.sys.document.AmountTotaling; 041 import org.kuali.rice.kns.service.KualiRuleService; 042 import org.kuali.rice.kns.util.KNSConstants; 043 044 public abstract class EndowmentTaxLotLinesDocumentActionBase extends EndowmentTransactionLinesDocumentActionBase { 045 046 /** 047 * Updates the tax lots for the given transaction line. 048 * 049 * @param isSource 050 * @param etlDocument 051 * @param transLine 052 */ 053 protected abstract void updateTransactionLineTaxLots(boolean isUpdate, boolean isSource, EndowmentTransactionLinesDocument etlDocument, EndowmentTransactionLine transLine); 054 055 /** 056 * Updates the tax lots for the given document. 057 * 058 * @param isSource 059 * @param etlDocument 060 */ 061 protected void updateTaxLots(EndowmentTransactionLinesDocument etlDocument) { 062 063 064 if (etlDocument.getSourceTransactionLines() != null) { 065 for (int i = 0; i < etlDocument.getSourceTransactionLines().size(); i++) { 066 EndowmentTransactionLine endowmentTransactionLine = (EndowmentTransactionLine) etlDocument.getSourceTransactionLines().get(i); 067 boolean rulePassed = true; 068 // check any business rules 069 rulePassed &= SpringContext.getBean(KualiRuleService.class).applyRules(new RefreshTransactionLineEvent(EXISTING_SOURCE_TRAN_LINE_PROPERTY_NAME, etlDocument, endowmentTransactionLine, i)); 070 071 if (rulePassed) { 072 updateTransactionLineTaxLots(true, true, etlDocument, endowmentTransactionLine); 073 } 074 075 } 076 } 077 078 if (etlDocument.getTargetTransactionLines() != null) { 079 for (int i = 0; i < etlDocument.getTargetTransactionLines().size(); i++) { 080 EndowmentTransactionLine endowmentTransactionLine = (EndowmentTransactionLine) etlDocument.getTargetTransactionLines().get(i); 081 boolean rulePassed = true; 082 // check any business rules 083 rulePassed &= SpringContext.getBean(KualiRuleService.class).applyRules(new RefreshTransactionLineEvent(EXISTING_TARGET_TRAN_LINE_PROPERTY_NAME, etlDocument, endowmentTransactionLine, i)); 084 085 if (rulePassed) { 086 updateTransactionLineTaxLots(true, false, etlDocument, endowmentTransactionLine); 087 } 088 } 089 } 090 } 091 092 /** 093 * Refreshes the tax lots for the selected target transaction line. 094 * 095 * @param mapping 096 * @param form 097 * @param request 098 * @param response 099 * @return 100 * @throws Exception 101 */ 102 public ActionForward refreshTargetTaxLots(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { 103 EndowmentTransactionLinesDocumentFormBase documentForm = (EndowmentTransactionLinesDocumentFormBase) form; 104 EndowmentTransactionLinesDocument endowmentDocument = (EndowmentTransactionLinesDocument) documentForm.getDocument(); 105 int selectedLine = this.getSelectedLine(request); 106 EndowmentTransactionLine transLine = endowmentDocument.getTargetTransactionLines().get(selectedLine); 107 108 boolean rulePassed = true; 109 110 // check any business rules 111 rulePassed &= SpringContext.getBean(KualiRuleService.class).applyRules(new RefreshTransactionLineEvent(EXISTING_TARGET_TRAN_LINE_PROPERTY_NAME, endowmentDocument, transLine, selectedLine)); 112 113 if (rulePassed) { 114 updateTransactionLineTaxLots(false, false, endowmentDocument, transLine); 115 } 116 117 if (endowmentDocument instanceof AmountTotaling) 118 ((FinancialSystemDocumentHeader) documentForm.getDocument().getDocumentHeader()).setFinancialDocumentTotalAmount(((AmountTotaling) endowmentDocument).getTotalDollarAmount()); 119 120 return mapping.findForward(KFSConstants.MAPPING_BASIC); 121 } 122 123 /** 124 * Refreshes the tax lots for the selected source transaction line. 125 * 126 * @param mapping 127 * @param form 128 * @param request 129 * @param response 130 * @return 131 * @throws Exception 132 */ 133 public ActionForward refreshSourceTaxLots(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { 134 EndowmentTransactionLinesDocumentFormBase documentForm = (EndowmentTransactionLinesDocumentFormBase) form; 135 EndowmentTransactionLinesDocument endowmentDocument = (EndowmentTransactionLinesDocument) documentForm.getDocument(); 136 int selectedLine = this.getSelectedLine(request); 137 EndowmentTransactionLine transLine = endowmentDocument.getSourceTransactionLines().get(selectedLine); 138 139 boolean rulePassed = true; 140 // check any business rules 141 rulePassed &= SpringContext.getBean(KualiRuleService.class).applyRules(new RefreshTransactionLineEvent(EXISTING_SOURCE_TRAN_LINE_PROPERTY_NAME, endowmentDocument, transLine, selectedLine)); 142 143 if (rulePassed) { 144 updateTransactionLineTaxLots(false, true, endowmentDocument, transLine); 145 } 146 147 if (endowmentDocument instanceof AmountTotaling) 148 ((FinancialSystemDocumentHeader) documentForm.getDocument().getDocumentHeader()).setFinancialDocumentTotalAmount(((AmountTotaling) endowmentDocument).getTotalDollarAmount()); 149 150 151 return mapping.findForward(KFSConstants.MAPPING_BASIC); 152 } 153 154 /** 155 * @see org.kuali.kfs.module.endow.document.web.struts.EndowmentTransactionLinesDocumentActionBase#insertTransactionLine(boolean, 156 * org.kuali.kfs.module.endow.document.web.struts.EndowmentTransactionLinesDocumentFormBase, 157 * org.kuali.kfs.module.endow.businessobject.EndowmentTransactionLine) 158 */ 159 @Override 160 protected void insertTransactionLine(boolean isSource, EndowmentTransactionLinesDocumentFormBase etlDocumentForm, EndowmentTransactionLine line) { 161 EndowmentTransactionLinesDocumentBase etlDoc = etlDocumentForm.getEndowmentTransactionLinesDocumentBase(); 162 163 super.insertTransactionLine(isSource, etlDocumentForm, line); 164 165 updateTransactionLineTaxLots(false, isSource, etlDoc, line); 166 } 167 168 /** 169 * Deletes a source tax lot line. 170 * 171 * @param mapping 172 * @param form 173 * @param request 174 * @param response 175 * @return 176 * @throws Exception 177 */ 178 public ActionForward deleteSourceTaxLotLine(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { 179 EndowmentTransactionLinesDocumentFormBase etlForm = (EndowmentTransactionLinesDocumentFormBase) form; 180 EndowmentTaxLotLinesDocument etlDoc = (EndowmentTaxLotLinesDocument) etlForm.getEndowmentTransactionLinesDocumentBase(); 181 182 int transLineindex = getLineToDelete(request); 183 int taxLotIndex = getTaxLotToDelete(request); 184 String errorPath = KFSConstants.DOCUMENT_PROPERTY_NAME + "." + EndowConstants.EXISTING_SOURCE_TRAN_LINE_PROPERTY_NAME + "[" + transLineindex + "]"; 185 EndowmentTransactionLine transLine = (EndowmentTransactionLine) etlDoc.getSourceTransactionLines().get(transLineindex); 186 boolean rulePassed = SpringContext.getBean(KualiRuleService.class).applyRules(new DeleteTaxLotLineEvent(errorPath, etlDoc, transLine.getTaxLotLines().get(taxLotIndex), transLine, transLineindex, taxLotIndex)); 187 188 // if the rule evaluation passed, let's delete it 189 if (rulePassed) { 190 deleteTaxLot(true, etlForm, transLineindex, taxLotIndex); 191 updateTransactionLineTaxLots(true, true, etlDoc, transLine); 192 } 193 194 return mapping.findForward(KFSConstants.MAPPING_BASIC); 195 } 196 197 /** 198 * Deletes a target tax lot line 199 * 200 * @param mapping 201 * @param form 202 * @param request 203 * @param response 204 * @return 205 * @throws Exception 206 */ 207 public ActionForward deleteTargetTaxLotLine(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { 208 EndowmentTransactionLinesDocumentFormBase etlForm = (EndowmentTransactionLinesDocumentFormBase) form; 209 EndowmentTaxLotLinesDocument etlDoc = (EndowmentTaxLotLinesDocument) etlForm.getEndowmentTransactionLinesDocumentBase(); 210 211 int transLineindex = getLineToDelete(request); 212 int taxLotIndex = getTaxLotToDelete(request); 213 String errorPath = KFSConstants.DOCUMENT_PROPERTY_NAME + "." + EndowConstants.EXISTING_TARGET_TRAN_LINE_PROPERTY_NAME + "[" + transLineindex + "]"; 214 EndowmentTransactionLine transLine = (EndowmentTransactionLine) etlDoc.getTargetTransactionLines().get(transLineindex); 215 boolean rulePassed = SpringContext.getBean(KualiRuleService.class).applyRules(new DeleteTaxLotLineEvent(errorPath, etlDoc, transLine.getTaxLotLines().get(taxLotIndex), transLine, transLineindex, taxLotIndex)); 216 217 // if the rule evaluation passed, let's delete it 218 if (rulePassed) { 219 deleteTaxLot(false, etlForm, transLineindex, taxLotIndex); 220 updateTransactionLineTaxLots(true, false, etlDoc, transLine); 221 } 222 223 return mapping.findForward(KFSConstants.MAPPING_BASIC); 224 } 225 226 /** 227 * Deletes a tax lot. 228 * 229 * @param isSource 230 * @param etlDocumentForm 231 * @param transLineindex 232 * @param taxLotIndex 233 */ 234 private void deleteTaxLot(boolean isSource, EndowmentTransactionLinesDocumentFormBase etlDocumentForm, int transLineindex, int taxLotIndex) { 235 if (isSource) { 236 etlDocumentForm.getEndowmentTransactionLinesDocumentBase().getSourceTransactionLines().get(transLineindex).getTaxLotLines().remove(taxLotIndex); 237 } 238 else { 239 etlDocumentForm.getEndowmentTransactionLinesDocumentBase().getTargetTransactionLines().get(transLineindex).getTaxLotLines().remove(taxLotIndex); 240 } 241 } 242 243 /** 244 * Gets the index of the tax lot line to be deleted. 245 * 246 * @param request 247 * @return the index of the tax lot line to be deleted 248 */ 249 protected int getTaxLotToDelete(HttpServletRequest request) { 250 int selectedTaxLot = -1; 251 String parameterName = (String) request.getAttribute(KNSConstants.METHOD_TO_CALL_ATTRIBUTE); 252 if (StringUtils.isNotBlank(parameterName)) { 253 String lotNumber = StringUtils.substringBetween(parameterName, ".taxLot", "."); 254 selectedTaxLot = Integer.parseInt(lotNumber); 255 } 256 257 return selectedTaxLot; 258 } 259 260 /** 261 * @see org.kuali.rice.kns.web.struts.action.KualiDocumentActionBase#save(org.apache.struts.action.ActionMapping, 262 * org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) 263 */ 264 @Override 265 public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { 266 267 EndowmentTransactionLinesDocumentFormBase documentForm = (EndowmentTransactionLinesDocumentFormBase) form; 268 EndowmentTransactionLinesDocument endowmentDocument = (EndowmentTransactionLinesDocument) documentForm.getDocument(); 269 270 // on AssetDecreaseDocument and SecurityTransferDocument we can delete tax lots and that would be overridden by this 271 // updateTaxLots call 272 if (getRefreshTaxLotsOnSaveOrSubmit()) { 273 updateTaxLots(endowmentDocument); 274 } 275 276 return super.save(mapping, form, request, response); 277 } 278 279 /** 280 * @see org.kuali.rice.kns.web.struts.action.KualiDocumentActionBase#route(org.apache.struts.action.ActionMapping, 281 * org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) 282 */ 283 @Override 284 public ActionForward route(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { 285 286 EndowmentTransactionLinesDocumentFormBase documentForm = (EndowmentTransactionLinesDocumentFormBase) form; 287 EndowmentTransactionLinesDocument endowmentDocument = (EndowmentTransactionLinesDocument) documentForm.getDocument(); 288 289 // on AssetDecreaseDocument and SecurityTransferDocument we can delete tax lots and that would be overridden by this 290 // updateTaxLots call 291 if (getRefreshTaxLotsOnSaveOrSubmit()) { 292 updateTaxLots(endowmentDocument); 293 } 294 295 return super.route(mapping, form, request, response); 296 } 297 298 /** 299 * Tells whether the tax lot lines related to the transaction lines should be refreshed on save or submit. For documents that 300 * support tax lots deletion this method should return false. For documents that do not support tax lots deletion this method 301 * can return true so that the tax lots are updated on save or submit. 302 * 303 * @return true or false depending on the document 304 */ 305 abstract protected boolean getRefreshTaxLotsOnSaveOrSubmit(); 306 }