001    /*
002     * Copyright 2011 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     */
016    package org.kuali.kfs.module.cam.document.web.struts;
017    
018    import java.util.Map;
019    
020    import javax.servlet.http.HttpServletRequest;
021    
022    import org.apache.commons.logging.Log;
023    import org.apache.commons.logging.LogFactory;
024    import org.kuali.kfs.module.cam.CamsPropertyConstants;
025    import org.kuali.kfs.module.cam.businessobject.AssetPaymentAssetDetail;
026    import org.kuali.kfs.module.cam.businessobject.AssetPaymentDetail;
027    import org.kuali.kfs.module.cam.document.AssetPaymentDocument;
028    import org.kuali.kfs.sys.KFSConstants;
029    import org.kuali.kfs.sys.KFSPropertyConstants;
030    import org.kuali.kfs.sys.businessobject.OriginationCode;
031    import org.kuali.kfs.sys.businessobject.SourceAccountingLine;
032    import org.kuali.kfs.sys.context.SpringContext;
033    import org.kuali.kfs.sys.web.struts.KualiAccountingDocumentFormBase;
034    import org.kuali.rice.kew.doctype.bo.DocumentTypeEBO;
035    import org.kuali.rice.kns.service.DataDictionaryService;
036    import org.kuali.rice.kns.util.GlobalVariables;
037    import org.kuali.rice.kns.util.KNSConstants;
038    
039    public class AssetPaymentForm extends KualiAccountingDocumentFormBase {
040        protected static Log LOG = LogFactory.getLog(AssetPaymentForm.class);
041    
042        // Indicates which result set we are using when refreshing/returning from a multi-value lookup.
043        protected String lookupResultsSequenceNumber;
044    
045        // Type of result returned by the multi-value lookup. ?to be persisted in the lookup results service instead?
046        protected String lookupResultsBOClassName;
047    
048        // The name of the collection looked up (by a multiple value lookup)
049        protected String lookedUpCollectionName;
050    
051        String capitalAssetNumber = "";
052    
053        /**
054         * Constructs a AssetPaymentForm.java.
055         */
056        public AssetPaymentForm() {
057            super();
058        }
059    
060        @Override
061        protected String getDefaultDocumentTypeName() {
062            return "MPAY";
063        }
064        
065        /**
066         * This method gets the asset payment document
067         * 
068         * @return AssetPaymentDocument
069         */
070        public AssetPaymentDocument getAssetPaymentDocument() {
071            return (AssetPaymentDocument) getDocument();
072        }
073    
074        /**
075         * @see org.kuali.kfs.sys.web.struts.KualiAccountingDocumentFormBase#getForcedLookupOptionalFields()
076         */
077        @Override
078        public Map<String, String> getForcedLookupOptionalFields() {
079            Map<String, String> forcedLookupOptionalFields = super.getForcedLookupOptionalFields();
080            forcedLookupOptionalFields.put(CamsPropertyConstants.AssetPaymentDetail.DOCUMENT_TYPE_CODE, KFSPropertyConstants.FINANCIAL_DOCUMENT_TYPE_CODE + ";" + DocumentTypeEBO.class.getName());
081            forcedLookupOptionalFields.put(CamsPropertyConstants.AssetPaymentDetail.ORIGINATION_CODE, KFSPropertyConstants.FINANCIAL_SYSTEM_ORIGINATION_CODE + ";" + OriginationCode.class.getName());
082    
083            return forcedLookupOptionalFields;
084        }
085    
086        /**
087         * This method sets the asset# selected
088         * 
089         * @param capitalAssetNumber
090         */
091        public void setCapitalAssetNumber(String capitalAssetNumber) {
092            this.capitalAssetNumber = capitalAssetNumber;
093        }
094    
095        /**
096         * gets the asset# that was previously selected
097         * 
098         * @return
099         */
100        public String getCapitalAssetNumber() {
101            return this.capitalAssetNumber;
102        }
103    
104    
105        /**
106         * @see org.kuali.kfs.sys.web.struts.KualiAccountingDocumentFormBase#getNewSourceLine()
107         */
108        @Override
109        public SourceAccountingLine getNewSourceLine() {
110            // Getting the workflow document number created for the asset payment document.
111            String worflowDocumentNumber = "";
112            try {
113                if (GlobalVariables.getUserSession().getWorkflowDocument(this.getAssetPaymentDocument().getDocumentNumber()) != null)
114                    worflowDocumentNumber = GlobalVariables.getUserSession().getWorkflowDocument(this.getAssetPaymentDocument().getDocumentNumber()).getRouteHeaderId().toString();
115            }
116            catch (Exception e) {
117                throw new RuntimeException("Error converting workflow document number to string:" + e.getMessage());
118            }
119    
120            AssetPaymentDetail newSourceLine = (AssetPaymentDetail) super.getNewSourceLine();
121    
122            // Setting default document type.
123            if (newSourceLine.getExpenditureFinancialDocumentTypeCode() == null || newSourceLine.getExpenditureFinancialDocumentTypeCode().trim().equals("")) {
124                newSourceLine.setExpenditureFinancialDocumentTypeCode(KFSConstants.FinancialDocumentTypeCodes.ASSET_PAYMENT);
125            }
126    
127            // Setting the default asset payment row document number.
128            if (newSourceLine.getExpenditureFinancialDocumentNumber() == null || newSourceLine.getExpenditureFinancialDocumentNumber().trim().equals("")) {
129                newSourceLine.setExpenditureFinancialDocumentNumber(worflowDocumentNumber);
130            }
131    
132            // Setting the default asset payment row origination code.
133            if (newSourceLine.getExpenditureFinancialSystemOriginationCode() == null || newSourceLine.getExpenditureFinancialSystemOriginationCode().trim().equals("")) {
134                newSourceLine.setExpenditureFinancialSystemOriginationCode(KFSConstants.ORIGIN_CODE_KUALI);
135            }
136    
137            return (SourceAccountingLine) newSourceLine;
138        }
139    
140        /**
141         * @see org.kuali.rice.kns.web.struts.form.KualiDocumentFormBase#populate(javax.servlet.http.HttpServletRequest)
142         */
143        @Override
144        public void populate(HttpServletRequest request) {
145            super.populate(request);
146    
147            // Refreshing reference to the asset table.
148            for (AssetPaymentAssetDetail assetPaymentAssetDetail : this.getAssetPaymentDocument().getAssetPaymentAssetDetail()) {
149                assetPaymentAssetDetail.refreshReferenceObject(CamsPropertyConstants.AssetPaymentDocument.ASSET);
150            }
151        }
152    
153        public String getLookupResultsSequenceNumber() {
154            return lookupResultsSequenceNumber;
155        }
156    
157        public void setLookupResultsSequenceNumber(String lookupResultsSequenceNumber) {
158            this.lookupResultsSequenceNumber = lookupResultsSequenceNumber;
159        }
160    
161        public String getLookupResultsBOClassName() {
162            return lookupResultsBOClassName;
163        }
164    
165        public void setLookupResultsBOClassName(String lookupResultsBOClassName) {
166            this.lookupResultsBOClassName = lookupResultsBOClassName;
167        }
168    
169        public String getLookedUpCollectionName() {
170            return lookedUpCollectionName;
171        }
172    
173        public void setLookedUpCollectionName(String lookedUpCollectionName) {
174            this.lookedUpCollectionName = lookedUpCollectionName;
175        }
176    
177        /**
178         * @see org.kuali.rice.kns.web.struts.form.AccountingDocumentFormBase#getRefreshCaller()
179         */
180        @Override
181        public String getRefreshCaller() {
182            return KFSConstants.MULTIPLE_VALUE;
183        }
184    
185        /**
186         * @see org.kuali.rice.kns.web.struts.form.KualiDocumentFormBase#addRequiredNonEditableProperties()
187         */
188        @Override
189        public void addRequiredNonEditableProperties() {
190            super.addRequiredNonEditableProperties();
191            registerRequiredNonEditableProperty(KNSConstants.LOOKUP_RESULTS_SEQUENCE_NUMBER);
192        }
193    }