001/*
002 * Copyright 2006 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 */
016package org.kuali.ole.fp.document.web.struts;
017
018import static org.kuali.ole.fp.document.validation.impl.ProcurementCardDocumentRuleConstants.DISPUTE_URL_PARM_NM;
019
020import java.util.ArrayList;
021import java.util.Iterator;
022import java.util.List;
023import java.util.Map;
024
025import javax.servlet.http.HttpServletRequest;
026
027import org.apache.commons.lang.StringUtils;
028import org.kuali.ole.fp.businessobject.CapitalAssetInformation;
029import org.kuali.ole.fp.businessobject.ProcurementCardTargetAccountingLine;
030import org.kuali.ole.fp.businessobject.ProcurementCardTransactionDetail;
031import org.kuali.ole.fp.document.CapitalAssetEditable;
032import org.kuali.ole.fp.document.ProcurementCardDocument;
033import org.kuali.ole.sys.OLEConstants;
034import org.kuali.ole.sys.OLEPropertyConstants;
035import org.kuali.ole.sys.businessobject.TargetAccountingLine;
036import org.kuali.ole.sys.context.SpringContext;
037import org.kuali.ole.sys.document.authorization.AccountingLineAuthorizer;
038import org.kuali.ole.sys.document.datadictionary.AccountingLineGroupDefinition;
039import org.kuali.ole.sys.document.datadictionary.FinancialSystemTransactionalDocumentEntry;
040import org.kuali.rice.coreservice.framework.parameter.ParameterService;
041import org.kuali.rice.kim.api.identity.Person;
042import org.kuali.rice.kns.service.DataDictionaryService;
043import org.kuali.rice.krad.datadictionary.DataDictionary;
044import org.kuali.rice.krad.util.GlobalVariables;
045import org.kuali.rice.krad.util.KRADConstants;
046
047/**
048 * This class is the form class for the ProcurementCard document. This method extends the parent KualiTransactionalDocumentFormBase
049 * class which contains all of the common form methods and form attributes needed by the Procurment Card document.
050 */
051public class ProcurementCardForm extends CapitalAccountingLinesFormBase implements CapitalAssetEditable{
052    protected static final long serialVersionUID = 1L;
053    protected List<ProcurementCardTargetAccountingLine> newTargetLines;
054    protected List<Boolean> transactionCreditCardNumbersViewStatus;
055    protected final static String TARGET_ACCOUNTING_LINE_GROUP_NAME = "target";
056    
057    protected List<CapitalAssetInformation> capitalAssetInformation;
058
059    /**
060     * Override to accomodate multiple target lines.
061     * 
062     * @see org.kuali.rice.kns.web.struts.pojo.PojoForm#populate(javax.servlet.http.HttpServletRequest)
063     */
064    @Override
065    public void populate(HttpServletRequest request) {
066        super.populate(request);
067
068        //
069        // now run through all of the accounting lines and make sure they've been uppercased and populated appropriately
070
071        // handle new accountingLine, if one is being added
072        final String methodToCall = this.getMethodToCall();
073        final Map parameterMap = request.getParameterMap();
074        if (StringUtils.isNotBlank(methodToCall)) {
075            if (methodToCall.equals(OLEConstants.INSERT_SOURCE_LINE_METHOD)) {
076                populateSourceAccountingLine(getNewSourceLine(), OLEPropertyConstants.NEW_SOURCE_LINE, parameterMap);
077            }
078
079            if (methodToCall.equals(OLEConstants.INSERT_TARGET_LINE_METHOD)) {
080                // This is the addition for the override: Handle multiple accounting lines ...
081                for (Iterator newTargetLinesIter = getNewTargetLines().iterator(); newTargetLinesIter.hasNext();) {
082                    TargetAccountingLine targetAccountingLine = (TargetAccountingLine) newTargetLinesIter.next();
083                    populateTargetAccountingLine(targetAccountingLine, OLEPropertyConstants.NEW_TARGET_LINE, parameterMap);
084                }
085            }
086        }
087
088        // don't call populateAccountingLines if you are copying or errorCorrecting a document,
089        // since you want the accountingLines in the copy to be "identical" to those in the original
090        if (!StringUtils.equals(methodToCall, OLEConstants.COPY_METHOD) && !StringUtils.equals(methodToCall, OLEConstants.ERRORCORRECT_METHOD)) {
091            populateAccountingLines(parameterMap);
092        }
093
094        setDocTypeName(discoverDocumentTypeName());
095    }
096
097    /**
098     * Constructs a ProcurmentCardForm instance and sets up the appropriately casted document. Also, the newSourceLine needs to be
099     * 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}