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 java.util.ArrayList;
19  
20  import javax.servlet.http.HttpServletRequest;
21  import javax.servlet.http.HttpServletResponse;
22  
23  import org.apache.commons.lang.StringUtils;
24  import org.apache.struts.action.ActionForm;
25  import org.apache.struts.action.ActionForward;
26  import org.apache.struts.action.ActionMapping;
27  import org.kuali.ole.fp.businessobject.ProcurementCardTargetAccountingLine;
28  import org.kuali.ole.fp.businessobject.ProcurementCardTransactionDetail;
29  import org.kuali.ole.fp.document.ProcurementCardDocument;
30  import org.kuali.ole.sys.OLEConstants;
31  import org.kuali.ole.sys.OLEKeyConstants;
32  import org.kuali.ole.sys.businessobject.AccountingLine;
33  import org.kuali.ole.sys.businessobject.TargetAccountingLine;
34  import org.kuali.ole.sys.context.SpringContext;
35  import org.kuali.ole.sys.document.AccountingDocument;
36  import org.kuali.ole.sys.document.validation.event.AddAccountingLineEvent;
37  import org.kuali.ole.sys.document.validation.event.DeleteAccountingLineEvent;
38  import org.kuali.ole.sys.web.struts.KualiAccountingDocumentFormBase;
39  import org.kuali.rice.core.api.util.type.KualiDecimal;
40  import org.kuali.rice.kew.api.exception.WorkflowException;
41  import org.kuali.rice.kns.web.struts.form.KualiDocumentFormBase;
42  import org.kuali.rice.krad.service.KualiRuleService;
43  import org.kuali.rice.krad.service.PersistenceService;
44  import org.kuali.rice.krad.util.GlobalVariables;
45  import org.kuali.rice.krad.util.KRADConstants;
46  
47  /**
48   * This class handles specific Actions requests for the ProcurementCard.
49   */
50  public class ProcurementCardAction extends CapitalAccountingLinesActionBase {
51      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ProcurementCardAction.class);
52  
53      @Override
54      protected void loadDocument(KualiDocumentFormBase kualiDocumentFormBase) throws WorkflowException {
55          super.loadDocument(kualiDocumentFormBase);
56          ProcurementCardDocument procureCardDocument = (ProcurementCardDocument)kualiDocumentFormBase.getDocument();
57          int transactionsCount = procureCardDocument.getTransactionEntries().size();
58          ProcurementCardForm procurementCardForm = (ProcurementCardForm)kualiDocumentFormBase;
59          procurementCardForm.buildNewTargetAccountingLines(transactionsCount);
60      }
61      
62      /**
63       * Override to accomodate multiple target lines.
64       * 
65       * @param transForm
66       */
67      @Override
68      protected void processAccountingLineOverrides(KualiAccountingDocumentFormBase transForm) {
69          ProcurementCardForm procurementCardForm = (ProcurementCardForm) transForm;
70  
71          processAccountingLineOverrides(procurementCardForm.getNewSourceLine());
72          processAccountingLineOverrides(procurementCardForm.getNewTargetLines());
73          if (procurementCardForm.hasDocumentId()) {
74              AccountingDocument financialDocument = (AccountingDocument) procurementCardForm.getDocument();
75  
76              processAccountingLineOverrides(financialDocument.getSourceAccountingLines());
77              processAccountingLineOverrides(financialDocument.getTargetAccountingLines());
78          }
79      }
80  
81      /**
82       * Override to add the new accounting line to the correct transaction
83       * 
84       * @see org.kuali.module.financial.web.struts.action.KualiFinancialDocumentActionBase#insertTargetLine(org.apache.struts.action.ActionMapping,
85       *      org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
86       */
87      @Override
88      public ActionForward insertTargetLine(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
89          ProcurementCardForm procurementCardForm = (ProcurementCardForm) form;
90          ProcurementCardDocument procurementCardDocument = (ProcurementCardDocument) procurementCardForm.getDocument();
91  
92          int targetContainerIndex = this.getSelectedContainer(request);
93          ProcurementCardTargetAccountingLine line = (ProcurementCardTargetAccountingLine)procurementCardForm.getNewTargetLines().get(targetContainerIndex);       
94  
95          ProcurementCardTransactionDetail transactionDetail = (ProcurementCardTransactionDetail) procurementCardDocument.getTransactionEntries().get(targetContainerIndex);
96          line.setFinancialDocumentTransactionLineNumber(transactionDetail.getFinancialDocumentTransactionLineNumber());
97  
98          // check any business rules
99          boolean rulePassed = SpringContext.getBean(KualiRuleService.class).applyRules(new AddAccountingLineEvent(OLEConstants.NEW_TARGET_ACCT_LINES_PROPERTY_NAME + "[" + Integer.toString(targetContainerIndex) + "]", procurementCardForm.getDocument(), (AccountingLine) line));
100 
101         if (rulePassed) {
102             // add accountingLine
103             SpringContext.getBean(PersistenceService.class).retrieveNonKeyFields(line);
104             insertAccountingLine(false, procurementCardForm, line);
105 
106             ProcurementCardTargetAccountingLine newLine = new ProcurementCardTargetAccountingLine();
107             newLine.setTransactionContainerIndex(targetContainerIndex);
108             
109             procurementCardForm.getNewTargetLines().set(targetContainerIndex, newLine);
110         }
111 
112         return mapping.findForward(OLEConstants.MAPPING_BASIC);
113     }
114 
115     /**
116      * @see org.kuali.ole.sys.web.struts.KualiAccountingDocumentActionBase#performBalanceInquiryForTargetLine(org.apache.struts.action.ActionMapping,
117      *      org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
118      */
119     @Override
120     public ActionForward performBalanceInquiryForTargetLine(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
121         ProcurementCardForm procurementCardForm = (ProcurementCardForm) form;
122         ProcurementCardDocument procurementCardDocument = (ProcurementCardDocument) procurementCardForm.getDocument();
123 
124         int targetContainerIndex = this.getSelectedContainer(request);
125         ProcurementCardTransactionDetail ProcurementCardTransactionDetail = (ProcurementCardTransactionDetail) procurementCardDocument.getTransactionEntries().get(targetContainerIndex);
126 
127         int targetIndex = super.getSelectedLine(request);
128         TargetAccountingLine targetLine = (ProcurementCardTargetAccountingLine) ProcurementCardTransactionDetail.getTargetAccountingLines().get(targetIndex);
129 
130         return performBalanceInquiryForAccountingLine(mapping, form, request, targetLine);
131     }
132 
133     /**
134      * Override to resync base accounting lines. New lines on the PCDO document can be inserted anywhere in the list, not necessary
135      * at the end.
136      * 
137      * @see org.kuali.module.financial.web.struts.action.KualiFinancialDocumentActionBase#insertAccountingLine(boolean,
138      *      org.kuali.module.financial.web.struts.form.KualiFinancialDocumentFormBase, org.kuali.rice.krad.bo.AccountingLine)
139      */
140     @Override
141     protected void insertAccountingLine(boolean isSource, KualiAccountingDocumentFormBase financialDocumentForm, AccountingLine line) {
142         AccountingDocument tdoc = financialDocumentForm.getFinancialDocument();
143 
144         // add it to the document
145         tdoc.addTargetAccountingLine((TargetAccountingLine) line);
146     }
147 
148     /**
149      * Override to get the correct container of the transaction and then delete the correct accounting line
150      * 
151      * @see org.kuali.module.financial.web.struts.action.KualiFinancialDocumentActionBase#deleteTargetLine(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response
152      */
153     @Override
154     public ActionForward deleteTargetLine(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
155         int targetContainerIndex = this.getSelectedContainer(request);
156         int targetIndex = this.getSelectedLine(request);
157         
158         KualiAccountingDocumentFormBase financialDocumentForm = (KualiAccountingDocumentFormBase) form;
159 
160         String errorPath = OLEConstants.DOCUMENT_PROPERTY_NAME + "." + OLEConstants.EXISTING_TARGET_ACCT_LINE_PROPERTY_NAME + "[" + targetIndex + "]";
161         boolean rulePassed = SpringContext.getBean(KualiRuleService.class).applyRules(new DeleteAccountingLineEvent(errorPath, financialDocumentForm.getDocument(), ((AccountingDocument) financialDocumentForm.getDocument()).getTargetAccountingLine(targetIndex), false));
162 
163         // if the rule evaluation passed, let's delete it
164         if (rulePassed) {
165             deleteAccountingLineFromTransactionContainer(financialDocumentForm, targetContainerIndex, targetIndex);
166         }
167         else {
168             String[] errorParams = new String[] { "target", Integer.toString(targetIndex + 1) };
169             GlobalVariables.getMessageMap().putError(errorPath, OLEKeyConstants.ERROR_ACCOUNTINGLINE_DELETERULE_INVALIDACCOUNT, errorParams);
170         }
171 
172         return mapping.findForward(OLEConstants.MAPPING_BASIC);
173     }
174     
175     /**
176      * Override to remove the accounting line from the correct transaction
177      * 
178      * @see org.kuali.module.financial.web.struts.action.KualiFinancialDocumentActionBase#deleteAccountingLine(boolean,
179      *      org.kuali.module.financial.web.struts.form.KualiFinancialDocumentFormBase, int)
180      */
181     @Override
182     protected void deleteAccountingLine(boolean isSource, KualiAccountingDocumentFormBase financialDocumentForm, int deleteIndex) {
183         ProcurementCardDocument procurementCardDocument = (ProcurementCardDocument) financialDocumentForm.getDocument();
184         procurementCardDocument.removeTargetAccountingLine(deleteIndex);
185     }
186 
187     /**
188      * Ensures that ProcurementCardForm.newTargetLines is cleared. Otherwise works like super.reload.
189      * 
190      * @see org.kuali.rice.kns.web.struts.action.KualiDocumentActionBase#reload(org.apache.struts.action.ActionMapping,
191      *      org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
192      */
193     @Override
194     public ActionForward reload(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
195         ProcurementCardForm procurementCardForm = (ProcurementCardForm) form;
196         procurementCardForm.setNewTargetLines(new ArrayList<ProcurementCardTargetAccountingLine>());
197 
198         return super.reload(mapping, procurementCardForm, request, response);
199     }
200 
201     // get the index of selected transaction entry
202     protected int getSelectedContainer(HttpServletRequest request) {
203         int selectedContainer = -1;
204         String parameterName = (String) request.getAttribute(KRADConstants.METHOD_TO_CALL_ATTRIBUTE);
205         if (StringUtils.isNotBlank(parameterName)) {
206             String lineNumber = StringUtils.substringBetween(parameterName, ".transactionEntries[", "].");
207             selectedContainer = Integer.parseInt(lineNumber);
208         }
209 
210         return selectedContainer;
211     }
212     
213     /**
214      * Removes the target accounting line at the given index from the transaction container transaction entries.
215      * 
216      * @param financialDocumentForm, targetContainerIndex, targetIndex
217      */
218     protected void deleteAccountingLineFromTransactionContainer(KualiAccountingDocumentFormBase financialDocumentForm, int targetContainerIndex, int targetIndex) {
219         ProcurementCardDocument procurementCardDocument = (ProcurementCardDocument) financialDocumentForm.getDocument(); 
220         ProcurementCardTransactionDetail procurementCardTransactionDetail = (ProcurementCardTransactionDetail) procurementCardDocument.getTransactionEntries().get(targetContainerIndex);
221         procurementCardTransactionDetail.getTargetAccountingLines().remove(targetIndex);
222     }
223 }