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 static org.kuali.ole.fp.document.validation.impl.ProcurementCardDocumentRuleConstants.DISPUTE_URL_PARM_NM;
19  
20  import java.util.ArrayList;
21  import java.util.Iterator;
22  import java.util.List;
23  import java.util.Map;
24  
25  import javax.servlet.http.HttpServletRequest;
26  
27  import org.apache.commons.lang.StringUtils;
28  import org.kuali.ole.fp.businessobject.CapitalAssetInformation;
29  import org.kuali.ole.fp.businessobject.ProcurementCardTargetAccountingLine;
30  import org.kuali.ole.fp.businessobject.ProcurementCardTransactionDetail;
31  import org.kuali.ole.fp.document.CapitalAssetEditable;
32  import org.kuali.ole.fp.document.ProcurementCardDocument;
33  import org.kuali.ole.sys.OLEConstants;
34  import org.kuali.ole.sys.OLEPropertyConstants;
35  import org.kuali.ole.sys.businessobject.TargetAccountingLine;
36  import org.kuali.ole.sys.context.SpringContext;
37  import org.kuali.ole.sys.document.authorization.AccountingLineAuthorizer;
38  import org.kuali.ole.sys.document.datadictionary.AccountingLineGroupDefinition;
39  import org.kuali.ole.sys.document.datadictionary.FinancialSystemTransactionalDocumentEntry;
40  import org.kuali.rice.coreservice.framework.parameter.ParameterService;
41  import org.kuali.rice.kim.api.identity.Person;
42  import org.kuali.rice.kns.service.DataDictionaryService;
43  import org.kuali.rice.krad.datadictionary.DataDictionary;
44  import org.kuali.rice.krad.util.GlobalVariables;
45  import org.kuali.rice.krad.util.KRADConstants;
46  
47  /**
48   * This class is the form class for the ProcurementCard document. This method extends the parent KualiTransactionalDocumentFormBase
49   * class which contains all of the common form methods and form attributes needed by the Procurment Card document.
50   */
51  public class ProcurementCardForm extends CapitalAccountingLinesFormBase implements CapitalAssetEditable{
52      protected static final long serialVersionUID = 1L;
53      protected List<ProcurementCardTargetAccountingLine> newTargetLines;
54      protected List<Boolean> transactionCreditCardNumbersViewStatus;
55      protected final static String TARGET_ACCOUNTING_LINE_GROUP_NAME = "target";
56      
57      protected List<CapitalAssetInformation> capitalAssetInformation;
58  
59      /**
60       * Override to accomodate multiple target lines.
61       * 
62       * @see org.kuali.rice.kns.web.struts.pojo.PojoForm#populate(javax.servlet.http.HttpServletRequest)
63       */
64      @Override
65      public void populate(HttpServletRequest request) {
66          super.populate(request);
67  
68          //
69          // now run through all of the accounting lines and make sure they've been uppercased and populated appropriately
70  
71          // handle new accountingLine, if one is being added
72          final String methodToCall = this.getMethodToCall();
73          final Map parameterMap = request.getParameterMap();
74          if (StringUtils.isNotBlank(methodToCall)) {
75              if (methodToCall.equals(OLEConstants.INSERT_SOURCE_LINE_METHOD)) {
76                  populateSourceAccountingLine(getNewSourceLine(), OLEPropertyConstants.NEW_SOURCE_LINE, parameterMap);
77              }
78  
79              if (methodToCall.equals(OLEConstants.INSERT_TARGET_LINE_METHOD)) {
80                  // This is the addition for the override: Handle multiple accounting lines ...
81                  for (Iterator newTargetLinesIter = getNewTargetLines().iterator(); newTargetLinesIter.hasNext();) {
82                      TargetAccountingLine targetAccountingLine = (TargetAccountingLine) newTargetLinesIter.next();
83                      populateTargetAccountingLine(targetAccountingLine, OLEPropertyConstants.NEW_TARGET_LINE, parameterMap);
84                  }
85              }
86          }
87  
88          // don't call populateAccountingLines if you are copying or errorCorrecting a document,
89          // since you want the accountingLines in the copy to be "identical" to those in the original
90          if (!StringUtils.equals(methodToCall, OLEConstants.COPY_METHOD) && !StringUtils.equals(methodToCall, OLEConstants.ERRORCORRECT_METHOD)) {
91              populateAccountingLines(parameterMap);
92          }
93  
94          setDocTypeName(discoverDocumentTypeName());
95      }
96  
97      /**
98       * Constructs a ProcurmentCardForm instance and sets up the appropriately casted document. Also, the newSourceLine needs to be
99       * the extended ProcurementCardSourceAccountingLine, for the additional trans line nbr.
100      */
101     public ProcurementCardForm() {
102         super();
103         
104         this.newTargetLines = new ArrayList<ProcurementCardTargetAccountingLine>();
105        // buildNewTargetAccountingLines();
106         capitalAssetInformation = new ArrayList<CapitalAssetInformation>();
107     }
108 
109     public void buildNewTargetAccountingLines(int transactionsCount) {
110         for (int i=0; i < transactionsCount; i++) {
111             ProcurementCardTargetAccountingLine newLine = new ProcurementCardTargetAccountingLine();
112             newLine.setTransactionContainerIndex(i);
113             this.newTargetLines.add(i, newLine);
114         }
115     }
116     
117     @Override
118     protected String getDefaultDocumentTypeName() {
119         return "OLE_PCDO";
120     }
121     
122     /**
123      * @return The retreived APC string used for the dispute url.
124      */
125     public String getDisputeURL() {
126         return SpringContext.getBean(ParameterService.class).getParameterValueAsString(ProcurementCardDocument.class, DISPUTE_URL_PARM_NM);
127     }
128 
129 
130     /**
131      * @return Returns the newTargetLines.
132      */
133     public List getNewTargetLines() {
134         return newTargetLines;
135     }
136 
137     /**
138      * @param newTargetLines The newTargetLines to set.
139      */
140     public void setNewTargetLines(List newTargetLines) {
141         this.newTargetLines = newTargetLines;
142     }
143     
144     /**
145      * @see org.kuali.ole.fp.document.CapitalAssetEditable#getCapitalAssetInformation()
146      */
147     public List<CapitalAssetInformation> getCapitalAssetInformation() {
148         return this.capitalAssetInformation;
149     }
150 
151     /**
152      * @see org.kuali.ole.fp.document.CapitalAssetEditable#setCapitalAssetInformation(org.kuali.ole.fp.businessobject.CapitalAssetInformation)
153      */
154     public void setCapitalAssetInformation(List<CapitalAssetInformation> capitalAssetInformation) {
155         this.capitalAssetInformation = capitalAssetInformation;        
156     }
157     
158     /**
159      * @return an array, parallel to the ProcurementCardDocument#getTransactionEntries, which holds whether the
160      * current user can see the credit card number or not
161      */
162     public List<Boolean> getTransactionCreditCardNumbersViewStatus() {
163         if (this.transactionCreditCardNumbersViewStatus == null) {
164             populateTransactionCreditCardNumbersViewStatuses();
165         }
166         return transactionCreditCardNumbersViewStatus;
167     }
168     
169     /**
170      * populates an array, parallel to the ProcurementCardDocument#getTransactionEntries, which holds whether the
171      * current user can see the credit card number or not - based on if any of the accounting lines are editable to
172      * the user or not...
173      */
174     protected void populateTransactionCreditCardNumbersViewStatuses() {
175         final AccountingLineAuthorizer accountingLineAuthorizer = getAccountingLineAuthorizerForDocument();
176         final Person currentUser = GlobalVariables.getUserSession().getPerson();
177         transactionCreditCardNumbersViewStatus = new ArrayList<Boolean>();
178         
179         for (Object transactionEntryAsObject : ((ProcurementCardDocument)getDocument()).getTransactionEntries()) {
180             final ProcurementCardTransactionDetail transactionDetail = (ProcurementCardTransactionDetail)transactionEntryAsObject;
181             Boolean canEditAnyAccountingLine = Boolean.FALSE;
182                 
183             int count = 0;
184             while (!canEditAnyAccountingLine.booleanValue() && count < transactionDetail.getTargetAccountingLines().size()) {
185                 final TargetAccountingLine accountingLine = (TargetAccountingLine)transactionDetail.getTargetAccountingLines().get(count);
186                 if (accountingLineAuthorizer.hasEditPermissionOnAccountingLine(((ProcurementCardDocument)getDocument()), accountingLine, getAccountingLineCollectionName(), currentUser, getDocumentActions().containsKey(KRADConstants.KUALI_ACTION_CAN_EDIT))) {
187                     canEditAnyAccountingLine = Boolean.TRUE;
188                 }
189                 count += 1;
190             }
191             transactionCreditCardNumbersViewStatus.add(canEditAnyAccountingLine);
192         }
193     }
194     
195     /**
196      * @return the accounting line authorizer for the target lines of this document
197      */
198     protected AccountingLineAuthorizer getAccountingLineAuthorizerForDocument() {
199         final DataDictionaryService dataDictionaryService = SpringContext.getBean(DataDictionaryService.class);
200         final DataDictionary dataDictionary = dataDictionaryService.getDataDictionary();
201         final String documentTypeCode = dataDictionaryService.getDocumentTypeNameByClass(this.getDocument().getClass());
202         final FinancialSystemTransactionalDocumentEntry documentEntry = (FinancialSystemTransactionalDocumentEntry)dataDictionary.getDocumentEntry(documentTypeCode);
203         final AccountingLineGroupDefinition targetAccountingLineGroupDefinition = documentEntry.getAccountingLineGroups().get(ProcurementCardForm.TARGET_ACCOUNTING_LINE_GROUP_NAME);
204         return targetAccountingLineGroupDefinition.getAccountingLineAuthorizer();
205     }
206     
207     /**
208      * @return the name of the accounting line collection for the permission check
209      */
210     protected String getAccountingLineCollectionName() {
211         // we'll just cheat...
212         return "targetAccountingLines";
213     }
214 }