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.cab.document.web.struts;
017    
018    import java.util.List;
019    
020    import javax.servlet.http.HttpServletRequest;
021    
022    import org.apache.commons.lang.StringUtils;
023    import org.apache.log4j.Logger;
024    import org.kuali.kfs.module.cab.CabConstants;
025    import org.kuali.kfs.module.cab.CabPropertyConstants;
026    import org.kuali.kfs.module.cab.businessobject.PurchasingAccountsPayableDocument;
027    import org.kuali.kfs.module.cab.document.service.GlAndPurApHelperService;
028    import org.kuali.kfs.module.cab.document.service.PurApInfoService;
029    import org.kuali.kfs.module.purap.document.PurchaseOrderDocument;
030    import org.kuali.kfs.sys.context.SpringContext;
031    import org.kuali.rice.kew.exception.WorkflowException;
032    import org.kuali.rice.kns.service.DocumentService;
033    import org.kuali.rice.kns.util.KNSConstants;
034    import org.kuali.rice.kns.util.KualiDecimal;
035    import org.kuali.rice.kns.util.ObjectUtils;
036    import org.kuali.rice.kns.util.TypedArrayList;
037    import org.kuali.rice.kns.web.struts.form.KualiForm;
038    
039    public class PurApLineForm extends KualiForm {
040        private static final Logger LOG = Logger.getLogger(PurApLineAction.class);
041        private Integer purchaseOrderIdentifier;
042        private String purApContactEmailAddress;
043        private String purApContactPhoneNumber;
044    
045        private List<PurchasingAccountsPayableDocument> purApDocs;
046        private int actionPurApDocIndex;
047        private int actionItemAssetIndex;
048    
049        private KualiDecimal mergeQty;
050        private String mergeDesc;
051    
052        private Integer requisitionIdentifier;
053    
054        private String purchaseOrderInquiryUrl;
055    
056        private boolean selectAll;
057    
058        private String documentNumber;
059    
060    
061        public PurApLineForm() {
062            this.purApDocs = new TypedArrayList(PurchasingAccountsPayableDocument.class);
063        }
064    
065        @Override
066        public boolean shouldMethodToCallParameterBeUsed(String methodToCallParameterName, String methodToCallParameterValue, HttpServletRequest request) {
067            if (StringUtils.equals(methodToCallParameterName, KNSConstants.DISPATCH_REQUEST_PARAMETER) && StringUtils.equals(methodToCallParameterValue, CabConstants.Actions.START)) {
068                return true;
069            }
070            return super.shouldMethodToCallParameterBeUsed(methodToCallParameterName, methodToCallParameterValue, request);
071        }
072    
073        @Override
074        public void addRequiredNonEditableProperties() {
075            super.addRequiredNonEditableProperties();
076            registerRequiredNonEditableProperty(CabPropertyConstants.PurchasingAccountsPayableDocument.PURCHASE_ORDER_IDENTIFIER);
077        }
078    
079    
080        /**
081         * Gets the documentNumber attribute.
082         * 
083         * @return Returns the documentNumber.
084         */
085        public String getDocumentNumber() {
086            return documentNumber;
087        }
088    
089    
090        /**
091         * Sets the documentNumber attribute value.
092         * 
093         * @param documentNumber The documentNumber to set.
094         */
095        public void setDocumentNumber(String documentNumber) {
096            this.documentNumber = documentNumber;
097        }
098    
099    
100        /**
101         * Gets the requisitionIdentifier attribute.
102         * 
103         * @return Returns the requisitionIdentifier.
104         */
105        public Integer getRequisitionIdentifier() {
106            return requisitionIdentifier;
107        }
108    
109    
110        /**
111         * Sets the requisitionIdentifier attribute value.
112         * 
113         * @param requisitionIdentifier The requisitionIdentifier to set.
114         */
115        public void setRequisitionIdentifier(Integer requisitionIdentifier) {
116            this.requisitionIdentifier = requisitionIdentifier;
117        }
118    
119        /**
120         * Gets the mergeQty attribute.
121         * 
122         * @return Returns the mergeQty.
123         */
124        public KualiDecimal getMergeQty() {
125            return mergeQty;
126        }
127    
128    
129        /**
130         * Sets the mergeQty attribute value.
131         * 
132         * @param mergeQty The mergeQty to set.
133         */
134        public void setMergeQty(KualiDecimal mergeQty) {
135            this.mergeQty = mergeQty;
136        }
137    
138    
139        /**
140         * Gets the mergeDesc attribute.
141         * 
142         * @return Returns the mergeDesc.
143         */
144        public String getMergeDesc() {
145            return mergeDesc;
146        }
147    
148    
149        /**
150         * Sets the mergeDesc attribute value.
151         * 
152         * @param mergeDesc The mergeDesc to set.
153         */
154        public void setMergeDesc(String mergeDesc) {
155            this.mergeDesc = mergeDesc;
156        }
157    
158    
159        /**
160         * Gets the purApContactEmailAddress attribute.
161         * 
162         * @return Returns the purApContactEmailAddress.
163         */
164        public String getPurApContactEmailAddress() {
165            return purApContactEmailAddress;
166        }
167    
168    
169        /**
170         * Sets the purApContactEmailAddress attribute value.
171         * 
172         * @param purApContactEmailAddress The purApContactEmailAddress to set.
173         */
174        public void setPurApContactEmailAddress(String purApContactEmailAddress) {
175            this.purApContactEmailAddress = purApContactEmailAddress;
176        }
177    
178    
179        /**
180         * Gets the purApContactPhoneNumber attribute.
181         * 
182         * @return Returns the purApContactPhoneNumber.
183         */
184        public String getPurApContactPhoneNumber() {
185            return purApContactPhoneNumber;
186        }
187    
188    
189        /**
190         * Sets the purApContactPhoneNumber attribute value.
191         * 
192         * @param purApContactPhoneNumber The purApContactPhoneNumber to set.
193         */
194        public void setPurApContactPhoneNumber(String purApContactPhoneNumber) {
195            this.purApContactPhoneNumber = purApContactPhoneNumber;
196        }
197    
198    
199        /**
200         * Gets the actionPurApDocIndex attribute.
201         * 
202         * @return Returns the actionPurApDocIndex.
203         */
204        public int getActionPurApDocIndex() {
205            return actionPurApDocIndex;
206        }
207    
208    
209        /**
210         * Sets the actionPurApDocIndex attribute value.
211         * 
212         * @param actionPurApDocIndex The actionPurApDocIndex to set.
213         */
214        public void setActionPurApDocIndex(int actionPurApDocIndex) {
215            this.actionPurApDocIndex = actionPurApDocIndex;
216        }
217    
218    
219        /**
220         * Gets the actionItemAssetIndex attribute.
221         * 
222         * @return Returns the actionItemAssetIndex.
223         */
224        public int getActionItemAssetIndex() {
225            return actionItemAssetIndex;
226        }
227    
228    
229        /**
230         * Sets the actionItemAssetIndex attribute value.
231         * 
232         * @param actionItemAssetIndex The actionItemAssetIndex to set.
233         */
234        public void setActionItemAssetIndex(int actionItemAssetIndex) {
235            this.actionItemAssetIndex = actionItemAssetIndex;
236        }
237    
238    
239        /**
240         * Gets the purchaseOrderIdentifier attribute.
241         * 
242         * @return Returns the purchaseOrderIdentifier.
243         */
244        public Integer getPurchaseOrderIdentifier() {
245            return purchaseOrderIdentifier;
246        }
247    
248    
249        /**
250         * Sets the purchaseOrderIdentifier attribute value.
251         * 
252         * @param purchaseOrderIdentifier The purchaseOrderIdentifier to set.
253         */
254        public void setPurchaseOrderIdentifier(Integer purchaseOrderIdentifier) {
255            this.purchaseOrderIdentifier = purchaseOrderIdentifier;
256        }
257    
258    
259        /**
260         * Gets the purApDocs attribute.
261         * 
262         * @return Returns the purApDocs.
263         */
264        public List<PurchasingAccountsPayableDocument> getPurApDocs() {
265            return purApDocs;
266        }
267    
268    
269        /**
270         * Sets the purApDocs attribute value.
271         * 
272         * @param purApDocs The purApDocs to set.
273         */
274        public void setPurApDocs(List<PurchasingAccountsPayableDocument> purApDocs) {
275            this.purApDocs = purApDocs;
276        }
277    
278    
279        /**
280         * Gets the purchaseOrderInquiryUrl attribute.
281         * 
282         * @return Returns the purchaseOrderInquiryUrl.
283         */
284        public String getPurchaseOrderInquiryUrl() {
285            return purchaseOrderInquiryUrl;
286        }
287    
288    
289        @Override
290        public void populate(HttpServletRequest request) {
291            super.populate(request);
292    
293            String parameterName = (String) request.getAttribute(KNSConstants.METHOD_TO_CALL_ATTRIBUTE);
294            if (StringUtils.isNotBlank(parameterName)) {
295                // populate collection index
296                String purApDocIndex = StringUtils.substringBetween(parameterName, CabConstants.DOT_DOC, ".");
297                if (StringUtils.isNotBlank(purApDocIndex)) {
298                    this.setActionPurApDocIndex(Integer.parseInt(purApDocIndex));
299                }
300                String itemAssetIndex = StringUtils.substringBetween(parameterName, CabConstants.DOT_LINE, ".");
301                if (StringUtils.isNotBlank(itemAssetIndex)) {
302                    this.setActionItemAssetIndex(Integer.parseInt(itemAssetIndex));
303                }
304            }
305    
306            if (this.purchaseOrderIdentifier != null) {
307                PurchaseOrderDocument poDoc = this.getPurApInfoService().getCurrentDocumentForPurchaseOrderIdentifier(this.purchaseOrderIdentifier);
308                if (ObjectUtils.isNotNull(poDoc) && StringUtils.isNotBlank(poDoc.getDocumentNumber())) {
309                    this.purchaseOrderInquiryUrl = "purapPurchaseOrder.do?methodToCall=docHandler&docId=" + poDoc.getDocumentNumber() + "&command=displayDocSearchView";
310                }
311            }
312    
313            // clear up the documentNumber saved when submit CAMS doc
314            this.setDocumentNumber(null);
315        }
316    
317        private PurApInfoService getPurApInfoService() {
318            return SpringContext.getBean(PurApInfoService.class);
319        }
320    
321    
322        public boolean isSelectAll() {
323            return selectAll;
324        }
325    
326    
327        public void setSelectAll(boolean selectAll) {
328            this.selectAll = selectAll;
329        }
330    
331        /**
332         * Return Asset Global forwarding URL.
333         * 
334         * @param request
335         * @param documentNumber
336         * @return
337         */
338        public String getDocHandlerForwardLink() {
339            String docHandlerLink = "";
340            if (StringUtils.isNotBlank(this.documentNumber)) {
341                try {
342                    String docTypeName = SpringContext.getBean(DocumentService.class).getByDocumentHeaderId(this.documentNumber).getDocumentHeader().getWorkflowDocument().getDocumentType();
343                    docHandlerLink = SpringContext.getBean(GlAndPurApHelperService.class).getDocHandlerUrl(this.documentNumber, docTypeName);
344                }
345                catch (WorkflowException e) {
346                    throw new RuntimeException("Caught WorkflowException trying to get document type name from Workflow", e);
347                }
348            }
349            return docHandlerLink;
350        }
351    
352        public PurchasingAccountsPayableDocument getPurApDoc(int index) {
353            int size = getPurApDocs().size();
354            while (size <= index || getPurApDocs().get(index) == null) {
355                getPurApDocs().add(size++, new PurchasingAccountsPayableDocument());
356            }
357            return (PurchasingAccountsPayableDocument) getPurApDocs().get(index);
358    
359        }
360    }