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.businessobject;
017    
018    import java.util.ArrayList;
019    import java.util.HashMap;
020    import java.util.LinkedHashMap;
021    import java.util.List;
022    import java.util.Map;
023    
024    import org.apache.commons.lang.StringUtils;
025    import org.kuali.kfs.module.cab.CabConstants;
026    import org.kuali.kfs.module.cab.CabPropertyConstants;
027    import org.kuali.kfs.module.purap.PurapPropertyConstants;
028    import org.kuali.kfs.module.purap.businessobject.CreditMemoStatus;
029    import org.kuali.kfs.module.purap.businessobject.PaymentRequestStatus;
030    import org.kuali.kfs.module.purap.document.PaymentRequestDocument;
031    import org.kuali.kfs.module.purap.document.VendorCreditMemoDocument;
032    import org.kuali.kfs.sys.businessobject.FinancialSystemDocumentHeader;
033    import org.kuali.kfs.sys.context.SpringContext;
034    import org.kuali.rice.kew.doctype.bo.DocumentTypeEBO;
035    import org.kuali.rice.kew.service.impl.KEWModuleService;
036    import org.kuali.rice.kns.bo.PersistableBusinessObjectBase;
037    import org.kuali.rice.kns.service.BusinessObjectService;
038    import org.kuali.rice.kns.util.ObjectUtils;
039    import org.kuali.rice.kns.util.TypedArrayList;
040    
041    /**
042     * @author Kuali Nervous System Team (kualidev@oncourse.iu.edu)
043     */
044    public class PurchasingAccountsPayableDocument extends PersistableBusinessObjectBase {
045    
046        private String documentNumber;
047        private Integer purapDocumentIdentifier;
048        private Integer purchaseOrderIdentifier;
049        private String documentTypeCode;
050        private String activityStatusCode;
051    
052        // References
053        private DocumentTypeEBO financialSystemDocumentTypeCode;
054        private FinancialSystemDocumentHeader documentHeader;
055        private List<PurchasingAccountsPayableItemAsset> purchasingAccountsPayableItemAssets;
056    
057        // non-persistent
058        private boolean active;
059        private String purApContactEmailAddress;
060        private String purApContactPhoneNumber;
061        private String statusDescription;
062        private String capitalAssetSystemTypeCodeFromPurAp;
063    
064        public PurchasingAccountsPayableDocument() {
065            this.purchasingAccountsPayableItemAssets = new TypedArrayList(PurchasingAccountsPayableItemAsset.class);
066        }
067    
068    
069        /**
070         * Gets the capitalAssetSystemTypeCodeFromPurAp attribute. 
071         * @return Returns the capitalAssetSystemTypeCodeFromPurAp.
072         */
073        public String getCapitalAssetSystemTypeCodeFromPurAp() {
074            return capitalAssetSystemTypeCodeFromPurAp;
075        }
076    
077    
078        /**
079         * Sets the capitalAssetSystemTypeCodeFromPurAp attribute value.
080         * @param capitalAssetSystemTypeCodeFromPurAp The capitalAssetSystemTypeCodeFromPurAp to set.
081         */
082        public void setCapitalAssetSystemTypeCodeFromPurAp(String capitalAssetSystemTypeCodeFromPurAp) {
083            this.capitalAssetSystemTypeCodeFromPurAp = capitalAssetSystemTypeCodeFromPurAp;
084        }
085    
086    
087        /**
088         * Gets the documentNumber attribute.
089         * 
090         * @return Returns the documentNumber.
091         */
092        public String getDocumentNumber() {
093            return documentNumber;
094        }
095    
096    
097        /**
098         * Sets the documentNumber attribute value.
099         * 
100         * @param documentNumber The documentNumber to set.
101         */
102        public void setDocumentNumber(String documentNumber) {
103            this.documentNumber = documentNumber;
104        }
105    
106    
107        /**
108         * Gets the purapDocumentIdentifier attribute.
109         * 
110         * @return Returns the purapDocumentIdentifier.
111         */
112        public Integer getPurapDocumentIdentifier() {
113            return purapDocumentIdentifier;
114        }
115    
116    
117        /**
118         * Sets the purapDocumentIdentifier attribute value.
119         * 
120         * @param purapDocumentIdentifier The purapDocumentIdentifier to set.
121         */
122        public void setPurapDocumentIdentifier(Integer purapDocumentIdentifier) {
123            this.purapDocumentIdentifier = purapDocumentIdentifier;
124        }
125    
126    
127        /**
128         * Gets the purchaseOrderIdentifier attribute.
129         * 
130         * @return Returns the purchaseOrderIdentifier.
131         */
132        public Integer getPurchaseOrderIdentifier() {
133            return purchaseOrderIdentifier;
134        }
135    
136    
137        /**
138         * Sets the purchaseOrderIdentifier attribute value.
139         * 
140         * @param purchaseOrderIdentifier The purchaseOrderIdentifier to set.
141         */
142        public void setPurchaseOrderIdentifier(Integer purchaseOrderIdentifier) {
143            this.purchaseOrderIdentifier = purchaseOrderIdentifier;
144        }
145    
146    
147        /**
148         * Gets the documentTypeCode attribute.
149         * 
150         * @return Returns the documentTypeCode.
151         */
152        public String getDocumentTypeCode() {
153            return documentTypeCode;
154        }
155    
156    
157        /**
158         * Sets the documentTypeCode attribute value.
159         * 
160         * @param documentTypeCode The documentTypeCode to set.
161         */
162        public void setDocumentTypeCode(String documentTypeCode) {
163            this.documentTypeCode = documentTypeCode;
164        }
165    
166    
167        /**
168         * Gets the active attribute.
169         * 
170         * @return Returns the active.
171         */
172        public boolean isActive() {
173            return CabConstants.ActivityStatusCode.NEW.equalsIgnoreCase(this.getActivityStatusCode()) || CabConstants.ActivityStatusCode.MODIFIED.equalsIgnoreCase(this.getActivityStatusCode());
174        }
175    
176    
177        /**
178         * Gets the activityStatusCode attribute.
179         * 
180         * @return Returns the activityStatusCode.
181         */
182        public String getActivityStatusCode() {
183            return activityStatusCode;
184        }
185    
186    
187        /**
188         * Sets the activityStatusCode attribute value.
189         * 
190         * @param activityStatusCode The activityStatusCode to set.
191         */
192        public void setActivityStatusCode(String activityStatusCode) {
193            this.activityStatusCode = activityStatusCode;
194        }
195    
196    
197        /**
198         * Gets the financialSystemDocumentTypeCode attribute.
199         * 
200         * @return Returns the financialSystemDocumentTypeCode.
201         */
202        public DocumentTypeEBO getFinancialSystemDocumentTypeCode() {
203            return financialSystemDocumentTypeCode = SpringContext.getBean(KEWModuleService.class).retrieveExternalizableBusinessObjectIfNecessary(this, financialSystemDocumentTypeCode, "financialSystemDocumentTypeCode");
204        }
205    
206        /**
207         * Gets the documentHeader attribute.
208         * 
209         * @return Returns the documentHeader.
210         */
211        public FinancialSystemDocumentHeader getDocumentHeader() {
212            return documentHeader;
213        }
214    
215    
216        /**
217         * Sets the documentHeader attribute value.
218         * 
219         * @param documentHeader The documentHeader to set.
220         */
221        public void setDocumentHeader(FinancialSystemDocumentHeader documentHeader) {
222            this.documentHeader = documentHeader;
223        }
224    
225    
226        /**
227         * Gets the purchasingAccountsPayableItemAssets attribute.
228         * 
229         * @return Returns the purchasingAccountsPayableItemAssets.
230         */
231        public List<PurchasingAccountsPayableItemAsset> getPurchasingAccountsPayableItemAssets() {
232            return purchasingAccountsPayableItemAssets;
233        }
234    
235    
236        /**
237         * Sets the purchasingAccountsPayableItemAssets attribute value.
238         * 
239         * @param purchasingAccountsPayableItemAssets The purchasingAccountsPayableItemAssets to set.
240         */
241        public void setPurchasingAccountsPayableItemAssets(List<PurchasingAccountsPayableItemAsset> purchasingAccountsPayableItemAssets) {
242            this.purchasingAccountsPayableItemAssets = purchasingAccountsPayableItemAssets;
243        }
244    
245    
246        /**
247         * Gets the purApContactEmailAddress attribute.
248         * 
249         * @return Returns the purApContactEmailAddress.
250         */
251        public String getPurApContactEmailAddress() {
252            return purApContactEmailAddress;
253        }
254    
255    
256        /**
257         * Sets the purApContactEmailAddress attribute value.
258         * 
259         * @param purApContactEmailAddress The purApContactEmailAddress to set.
260         */
261        public void setPurApContactEmailAddress(String purApContactEmailAddress) {
262            this.purApContactEmailAddress = purApContactEmailAddress;
263        }
264    
265    
266        /**
267         * Gets the purApContactPhoneNumber attribute.
268         * 
269         * @return Returns the purApContactPhoneNumber.
270         */
271        public String getPurApContactPhoneNumber() {
272            return purApContactPhoneNumber;
273        }
274    
275    
276        /**
277         * Sets the purApContactPhoneNumber attribute value.
278         * 
279         * @param purApContactPhoneNumber The purApContactPhoneNumber to set.
280         */
281        public void setPurApContactPhoneNumber(String purApContactPhoneNumber) {
282            this.purApContactPhoneNumber = purApContactPhoneNumber;
283        }
284    
285    
286        /**
287         * Gets the statusDescription attribute.
288         * 
289         * @return Returns the statusDescription.
290         */
291        public String getStatusDescription() {
292            String statusCode;
293    
294            if (StringUtils.isNotBlank(this.statusDescription)) {
295                return this.statusDescription;
296            }
297            else {
298                Map objectKeys = new HashMap();
299                objectKeys.put(CabPropertyConstants.PurchasingAccountsPayableDocument.PURAP_DOCUMENT_IDENTIFIER, this.getPurapDocumentIdentifier());
300    
301                if (CabConstants.PREQ.equals(this.documentTypeCode)) {
302    
303                    PaymentRequestDocument paymentRequestDocument = (PaymentRequestDocument) SpringContext.getBean(BusinessObjectService.class).findByPrimaryKey(PaymentRequestDocument.class, objectKeys);
304                    if (ObjectUtils.isNotNull(paymentRequestDocument)) {
305                        statusCode = paymentRequestDocument.getStatusCode();
306    
307                        objectKeys = new HashMap();
308                        objectKeys.put(PurapPropertyConstants.STATUS_CODE, statusCode);
309                        PaymentRequestStatus paymentRequestStatus = (PaymentRequestStatus) SpringContext.getBean(BusinessObjectService.class).findByPrimaryKey(PaymentRequestStatus.class, objectKeys);
310                        if (ObjectUtils.isNotNull(paymentRequestStatus)) {
311                            statusDescription = paymentRequestStatus.getStatusDescription();
312                        }
313                    }
314                }
315                else {
316                    VendorCreditMemoDocument vendorCreditMemoDocument = (VendorCreditMemoDocument) SpringContext.getBean(BusinessObjectService.class).findByPrimaryKey(VendorCreditMemoDocument.class, objectKeys);
317                    if (ObjectUtils.isNotNull(vendorCreditMemoDocument)) {
318                        statusCode = vendorCreditMemoDocument.getStatusCode();
319    
320                        objectKeys = new HashMap();
321                        objectKeys.put(PurapPropertyConstants.STATUS_CODE, statusCode);
322                        CreditMemoStatus creditMemoStatus = (CreditMemoStatus) SpringContext.getBean(BusinessObjectService.class).findByPrimaryKey(CreditMemoStatus.class, objectKeys);
323                        if (ObjectUtils.isNotNull(creditMemoStatus)) {
324                            statusDescription = creditMemoStatus.getStatusDescription();
325                        }
326                    }
327                }
328            }
329    
330            return statusDescription;
331        }
332    
333    
334        /**
335         * Sets the statusDescription attribute value.
336         * 
337         * @param statusDescription The statusDescription to set.
338         */
339        public void setStatusDescription(String statusDescription) {
340            this.statusDescription = statusDescription;
341        }
342    
343    
344        /**
345         * Need to override this method, so we can save item assets, the framework can delete the allocated item assets.
346         * 
347         * @see org.kuali.rice.kns.bo.PersistableBusinessObjectBase#buildListOfDeletionAwareLists()
348         */
349        @Override
350        public List buildListOfDeletionAwareLists() {
351            List<List> managedLists = new ArrayList<List>();
352    
353            managedLists.add(getPurchasingAccountsPayableItemAssets());
354            return managedLists;
355        }
356    
357        /**
358         * @see org.kuali.rice.kns.bo.BusinessObjectBase#toStringMapper()
359         */
360        protected LinkedHashMap toStringMapper() {
361            LinkedHashMap m = new LinkedHashMap();
362            m.put("documentNumber", this.documentNumber);
363            return m;
364        }
365    
366        public PurchasingAccountsPayableItemAsset getPurchasingAccountsPayableItemAsset(int index) {
367            int size = getPurchasingAccountsPayableItemAssets().size();
368            while (size <= index || getPurchasingAccountsPayableItemAssets().get(index) == null) {
369                getPurchasingAccountsPayableItemAssets().add(size++, new PurchasingAccountsPayableItemAsset());
370            }
371            return (PurchasingAccountsPayableItemAsset) getPurchasingAccountsPayableItemAssets().get(index);
372    
373        }
374    
375    }