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.PurchasingAccountsPayableDocument;
21  import org.kuali.ole.module.purap.service.PurapAccountingService;
22  import org.kuali.ole.module.purap.util.SummaryAccount;
23  import org.kuali.ole.pdp.PdpPropertyConstants;
24  import org.kuali.ole.pdp.businessobject.PurchasingPaymentDetail;
25  import org.kuali.ole.sys.OLEConstants;
26  import org.kuali.ole.sys.OLEParameterKeyConstants;
27  import org.kuali.ole.sys.OLEPropertyConstants;
28  import org.kuali.ole.sys.businessobject.AccountingLine;
29  import org.kuali.ole.sys.context.SpringContext;
30  import org.kuali.ole.sys.service.impl.OleParameterConstants;
31  import org.kuali.ole.sys.web.struts.KualiAccountingDocumentFormBase;
32  import org.kuali.rice.core.api.config.property.ConfigurationService;
33  import org.kuali.rice.coreservice.framework.parameter.ParameterService;
34  import org.kuali.rice.kns.web.ui.ExtraButton;
35  import org.kuali.rice.krad.util.KRADConstants;
36  import org.kuali.rice.krad.util.ObjectUtils;
37  import org.kuali.rice.krad.util.UrlFactory;
38  
39  import javax.servlet.http.HttpServletRequest;
40  import java.util.ArrayList;
41  import java.util.List;
42  import java.util.Map;
43  import java.util.Properties;
44  
45  /**
46   * Struts Action Form for Purchasing and Accounts Payable documents.
47   */
48  public class PurchasingAccountsPayableFormBase extends KualiAccountingDocumentFormBase {
49  
50      protected transient List<SummaryAccount> summaryAccounts;
51      protected boolean readOnlyAccountDistributionMethod;
52  
53      /**
54       * Constructs a PurchasingAccountsPayableFormBase instance and initializes summary accounts.
55       */
56      public PurchasingAccountsPayableFormBase() {
57          super();
58          clearSummaryAccounts();
59          setupAccountDistributionMethod();
60      }
61  
62      /**
63       * retrieves the system parameter value for account distribution method and determines
64       * if the drop-down box on the form should be read only or not.
65       */
66      protected void setupAccountDistributionMethod() {
67          // OLE-3405 : disabling the distribution method choice
68          //String defaultDistributionMethod = SpringContext.getBean(ParameterService.class).getParameterValueAsString(PurapConstants.PURAP_NAMESPACE, "Document", PurapParameterConstants.DISTRIBUTION_METHOD_FOR_ACCOUNTING_LINES);
69  
70          //if (PurapConstants.AccountDistributionMethodCodes.PROPORTIONAL_CODE.equalsIgnoreCase(defaultDistributionMethod) || PurapConstants.AccountDistributionMethodCodes.SEQUENTIAL_CODE.equalsIgnoreCase(defaultDistributionMethod)) {
71          this.setReadOnlyAccountDistributionMethod(true);
72          //}
73          //else {
74          //    this.setReadOnlyAccountDistributionMethod(false);
75          //}
76      }
77  
78      /**
79       * Updates the summaryAccounts that are contained in the form. Currently we are only calling this on load and when
80       * refreshAccountSummary is called.
81       */
82      public void refreshAccountSummmary() {
83          clearSummaryAccounts();
84          PurchasingAccountsPayableDocument purapDocument = (PurchasingAccountsPayableDocument) getDocument();
85          summaryAccounts.addAll(SpringContext.getBean(PurapAccountingService.class).generateSummaryAccounts(purapDocument));
86      }
87  
88      /**
89       * Initializes summary accounts.
90       */
91      public void clearSummaryAccounts() {
92          summaryAccounts = new ArrayList<SummaryAccount>();
93      }
94  
95      /**
96       * @see org.kuali.ole.sys.web.struts.KualiAccountingDocumentFormBase#getBaselineSourceAccountingLines()
97       */
98      public List getBaselineSourceAccountingLines() {
99          List<AccountingLine> accounts = new ArrayList<AccountingLine>();
100         if (ObjectUtils.isNull(accounts) || accounts.isEmpty()) {
101             accounts = new ArrayList<AccountingLine>();
102             for (PurApItem item : ((PurchasingAccountsPayableDocument) getDocument()).getItems()) {
103                 List<PurApAccountingLine> lines = item.getBaselineSourceAccountingLines();
104                 for (PurApAccountingLine line : lines) {
105                     accounts.add(line);
106                 }
107 
108             }
109         }
110         return accounts;
111     }
112 
113     @Override
114     public void populate(HttpServletRequest request) {
115         super.populate(request);
116         PurchasingAccountsPayableDocument purapDoc = (PurchasingAccountsPayableDocument) this.getDocument();
117 
118         //fix document item/account references if necessary
119         purapDoc.fixItemReferences();
120     }
121 
122     public List<SummaryAccount> getSummaryAccounts() {
123         if (summaryAccounts == null) {
124             refreshAccountSummmary();
125         }
126         return summaryAccounts;
127     }
128 
129     public void setSummaryAccounts(List<SummaryAccount> summaryAccounts) {
130         this.summaryAccounts = summaryAccounts;
131     }
132 
133     /**
134      * KRAD Conversion: Performs customization of an extra button.
135      * <p/>
136      * No data dictionary is involved.
137      */
138     protected void addExtraButton(String property, String source, String altText) {
139 
140         ExtraButton newButton = new ExtraButton();
141 
142         newButton.setExtraButtonProperty(property);
143         newButton.setExtraButtonSource(source);
144         newButton.setExtraButtonAltText(altText);
145 
146         extraButtons.add(newButton);
147     }
148 
149     /**
150      * This method builds the url for the disbursement info on the purap documents.
151      *
152      * @return the disbursement info url
153      */
154     public String getDisbursementInfoUrl() {
155         String basePath = SpringContext.getBean(ConfigurationService.class).getPropertyValueAsString(OLEConstants.APPLICATION_URL_KEY);
156         ParameterService parameterService = SpringContext.getBean(ParameterService.class);
157 
158         String orgCode = parameterService.getParameterValueAsString(OleParameterConstants.PURCHASING_BATCH.class, OLEParameterKeyConstants.PurapPdpParameterConstants.PURAP_PDP_ORG_CODE);
159         String subUnitCode = parameterService.getParameterValueAsString(OleParameterConstants.PURCHASING_BATCH.class, OLEParameterKeyConstants.PurapPdpParameterConstants.PURAP_PDP_SUB_UNIT_CODE);
160 
161         Properties parameters = new Properties();
162         parameters.put(OLEConstants.DISPATCH_REQUEST_PARAMETER, OLEConstants.SEARCH_METHOD);
163         parameters.put(OLEConstants.BACK_LOCATION, basePath + "/" + OLEConstants.MAPPING_PORTAL + ".do");
164         parameters.put(KRADConstants.DOC_FORM_KEY, "88888888");
165         parameters.put(OLEConstants.BUSINESS_OBJECT_CLASS_ATTRIBUTE, PurchasingPaymentDetail.class.getName());
166         parameters.put(OLEConstants.HIDE_LOOKUP_RETURN_LINK, "true");
167         parameters.put(OLEConstants.SUPPRESS_ACTIONS, "false");
168         parameters.put(PdpPropertyConstants.PaymentDetail.PAYMENT_UNIT_CODE, orgCode);
169         parameters.put(PdpPropertyConstants.PaymentDetail.PAYMENT_SUBUNIT_CODE, subUnitCode);
170 
171         String lookupUrl = UrlFactory.parameterizeUrl(basePath + "/" + OLEConstants.LOOKUP_ACTION, parameters);
172 
173         return lookupUrl;
174     }
175 
176     /**
177      * overridden to make sure accounting lines on items are repopulated
178      *
179      * @see org.kuali.ole.sys.web.struts.KualiAccountingDocumentFormBase#populateAccountingLinesForResponse(java.lang.String, java.util.Map)
180      */
181     @Override
182     protected void populateAccountingLinesForResponse(String methodToCall, Map parameterMap) {
183         super.populateAccountingLinesForResponse(methodToCall, parameterMap);
184 
185         populateItemAccountingLines(parameterMap);
186     }
187 
188     /**
189      * Populates accounting lines for each item on the Purchasing AP document
190      *
191      * @param parameterMap the map of parameters
192      */
193     protected void populateItemAccountingLines(Map parameterMap) {
194         int itemCount = 0;
195         for (PurApItem item : ((PurchasingAccountsPayableDocument) getDocument()).getItems()) {
196             populateAccountingLine(item.getNewSourceLine(), OLEPropertyConstants.DOCUMENT + "." + OLEPropertyConstants.ITEM + "[" + itemCount + "]." + OLEPropertyConstants.NEW_SOURCE_LINE, parameterMap);
197 
198             int sourceLineCount = 0;
199             for (PurApAccountingLine purApLine : item.getSourceAccountingLines()) {
200                 populateAccountingLine(purApLine, OLEPropertyConstants.DOCUMENT + "." + OLEPropertyConstants.ITEM + "[" + itemCount + "]." + OLEPropertyConstants.SOURCE_ACCOUNTING_LINE + "[" + sourceLineCount + "]", parameterMap);
201                 sourceLineCount += 1;
202             }
203         }
204     }
205 
206     /**
207      * Gets the readOnlyAccountDistributionMethod attribute.
208      *
209      * @return Returns the readOnlyAccountDistributionMethod
210      */
211 
212     public boolean isReadOnlyAccountDistributionMethod() {
213         return readOnlyAccountDistributionMethod;
214     }
215 
216     /**
217      * Sets the readOnlyAccountDistributionMethod attribute.
218      *
219      * @param readOnlyAccountDistributionMethod
220      *         The readOnlyAccountDistributionMethod to set.
221      */
222     public void setReadOnlyAccountDistributionMethod(boolean readOnlyAccountDistributionMethod) {
223         this.readOnlyAccountDistributionMethod = readOnlyAccountDistributionMethod;
224     }
225 }