View Javadoc
1   /*
2    * Copyright 2007 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.module.purap.document.web.struts;
17  
18  import org.kuali.ole.module.purap.businessobject.PurApAccountingLine;
19  import org.kuali.ole.module.purap.businessobject.PurApItem;
20  import org.kuali.ole.module.purap.document.AccountsPayableDocument;
21  import org.kuali.ole.module.purap.document.service.PurchaseOrderService;
22  import org.kuali.ole.module.purap.util.PurApItemUtils;
23  import org.kuali.ole.sys.OLEPropertyConstants;
24  import org.kuali.ole.sys.context.SpringContext;
25  
26  import javax.servlet.http.HttpServletRequest;
27  import java.util.List;
28  import java.util.Map;
29  
30  /**
31   * Struts Action Form for Accounts Payable documents.
32   */
33  public class AccountsPayableFormBase extends PurchasingAccountsPayableFormBase {
34  
35      protected PurApItem newPurchasingItemLine;
36      protected boolean calculated;
37      protected int countOfAboveTheLine = 0;
38      protected int countOfBelowTheLine = 0;
39  
40      /**
41       * Constructs an AccountsPayableForm instance and sets up the appropriately casted document.
42       */
43      public AccountsPayableFormBase() {
44          super();
45          calculated = false;
46      }
47  
48      public PurApItem getNewPurchasingItemLine() {
49          return newPurchasingItemLine;
50      }
51  
52      public void setNewPurchasingItemLine(PurApItem newPurchasingItemLine) {
53          this.newPurchasingItemLine = newPurchasingItemLine;
54      }
55  
56      public PurApItem getAndResetNewPurchasingItemLine() {
57          PurApItem aPurchasingItemLine = getNewPurchasingItemLine();
58          setNewPurchasingItemLine(setupNewPurchasingItemLine());
59          return aPurchasingItemLine;
60      }
61  
62      /**
63       * This method should be overriden (or see accountingLines for an alternate way of doing this with newInstance)
64       *
65       * @return - null, enforces overriding
66       */
67      public PurApItem setupNewPurchasingItemLine() {
68          return null;
69      }
70  
71      public boolean isCalculated() {
72          return calculated;
73      }
74  
75      public void setCalculated(boolean calculated) {
76          this.calculated = calculated;
77      }
78  
79      public int getCountOfAboveTheLine() {
80          return countOfAboveTheLine;
81      }
82  
83      public void setCountOfAboveTheLine(int countOfAboveTheLine) {
84          this.countOfAboveTheLine = countOfAboveTheLine;
85      }
86  
87      public int getCountOfBelowTheLine() {
88          return countOfBelowTheLine;
89      }
90  
91      public void setCountOfBelowTheLine(int countOfBelowTheLine) {
92          this.countOfBelowTheLine = countOfBelowTheLine;
93      }
94  
95      /**
96       * @see org.kuali.ole.sys.web.struts.KualiAccountingDocumentFormBase#populate(javax.servlet.http.HttpServletRequest)
97       */
98      @Override
99      public void populate(HttpServletRequest request) {
100         super.populate(request);
101         AccountsPayableDocument apDoc = (AccountsPayableDocument) this.getDocument();
102 
103         // update po doc
104         if (apDoc.getPurchaseOrderIdentifier() != null) {
105             apDoc.setPurchaseOrderDocument(SpringContext.getBean(PurchaseOrderService.class).getCurrentPurchaseOrder(apDoc.getPurchaseOrderIdentifier()));
106         }
107 
108         // update counts after populate
109         updateItemCounts();
110     }
111 
112     /**
113      * overridden to make sure accounting lines on items are repopulated
114      *
115      * @see org.kuali.ole.sys.web.struts.KualiAccountingDocumentFormBase#populateAccountingLinesForResponse(java.lang.String, java.util.Map)
116      */
117     @Override
118     protected void populateAccountingLinesForResponse(String methodToCall, Map parameterMap) {
119         super.populateAccountingLinesForResponse(methodToCall, parameterMap);
120 
121         populateItemAccountingLines(parameterMap);
122     }
123 
124     /**
125      * Populates accounting lines for each item on the AP document
126      *
127      * @param parameterMap the map of parameters
128      */
129     @Override
130     protected void populateItemAccountingLines(Map parameterMap) {
131         int itemCount = 0;
132         for (PurApItem item : ((AccountsPayableDocument) getDocument()).getItems()) {
133             populateAccountingLine(item.getNewSourceLine(), OLEPropertyConstants.DOCUMENT + "." + OLEPropertyConstants.ITEM + "[" + itemCount + "]." + OLEPropertyConstants.NEW_SOURCE_LINE, parameterMap);
134 
135             int sourceLineCount = 0;
136             for (PurApAccountingLine purApLine : item.getSourceAccountingLines()) {
137                 populateAccountingLine(purApLine, OLEPropertyConstants.DOCUMENT + "." + OLEPropertyConstants.ITEM + "[" + itemCount + "]." + OLEPropertyConstants.SOURCE_ACCOUNTING_LINE + "[" + sourceLineCount + "]", parameterMap);
138                 sourceLineCount += 1;
139             }
140         }
141     }
142 
143     /**
144      * Updates item counts for display
145      */
146     public void updateItemCounts() {
147         List<PurApItem> items = ((AccountsPayableDocument) this.getDocument()).getItems();
148         countOfBelowTheLine = PurApItemUtils.countBelowTheLineItems(items);
149         countOfAboveTheLine = items.size() - countOfBelowTheLine;
150     }
151 
152 }