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.purap.businessobject;
017    
018    import java.util.LinkedHashMap;
019    import java.util.List;
020    
021    import org.kuali.kfs.module.purap.PurapParameterConstants;
022    import org.kuali.kfs.module.purap.document.ContractManagerAssignmentDocument;
023    import org.kuali.kfs.module.purap.document.RequisitionDocument;
024    import org.kuali.kfs.sys.KFSPropertyConstants;
025    import org.kuali.kfs.sys.context.SpringContext;
026    import org.kuali.kfs.vnd.businessobject.CommodityContractManager;
027    import org.kuali.kfs.vnd.businessobject.ContractManager;
028    import org.kuali.rice.kew.exception.WorkflowException;
029    import org.kuali.rice.kns.bo.PersistableBusinessObjectBase;
030    import org.kuali.rice.kns.service.ParameterService;
031    import org.kuali.rice.kns.web.format.DateViewTimestampObjectFormatter;
032    import org.kuali.rice.kns.web.format.Formatter;
033    
034    /**
035     * Assign Contract Manager Detail Business Object. Defines attributes in Assign Contract Manager tab.
036     */
037    public class ContractManagerAssignmentDetail extends PersistableBusinessObjectBase {
038    
039        private String documentNumber;
040        private Integer requisitionIdentifier;
041        private Integer contractManagerCode;
042        private String deliveryCampusCode;
043        private String vendorName;
044        
045        private RequisitionDocument requisition;
046        private ContractManager contractManager;
047        private ContractManagerAssignmentDocument contractManagerAssignmentDocument;
048        
049        private String createDate;
050        
051        /**
052         * Default constructor.
053         */
054        public ContractManagerAssignmentDetail() {
055    
056        }
057    
058        /**
059         * Constructs a ContractManagerAssignmentDetail object from an existing ContractManagerAssignmentDocument object.
060         * 
061         * @param acmDocument the ContractManagerAssignmentDocument to copy from.
062         * @param requisitionDocument reference to the related requisition document.
063         */
064        public ContractManagerAssignmentDetail(ContractManagerAssignmentDocument acmDocument, RequisitionDocument requisitionDocument) {
065            this.documentNumber = acmDocument.getDocumentNumber();
066            this.contractManagerAssignmentDocument = acmDocument;
067            this.requisition = requisitionDocument;
068            this.requisitionIdentifier = requisitionDocument.getPurapDocumentIdentifier();
069            this.deliveryCampusCode = requisitionDocument.getDeliveryCampusCode();
070            this.vendorName = requisitionDocument.getVendorName();
071        }
072    
073        public String getDocumentNumber() {
074            return documentNumber;
075        }
076    
077        public void setDocumentNumber(String documentNumber) {
078            this.documentNumber = documentNumber;
079        }
080    
081        public Integer getRequisitionIdentifier() {
082            return requisitionIdentifier;
083        }
084    
085        public void setRequisitionIdentifier(Integer requisitionIdentifier) {
086            this.requisitionIdentifier = requisitionIdentifier;
087        }
088    
089        /**
090         * Returns the default contract manager code if the first line item of the
091         * requisition contains commodity codes with one contract manager whose campus
092         * code matches the delivery campus code on the requisition. If there are more
093         * than one contract managers of the same campus code as the delivery code, we'll
094         * return null. If the first line item of the requisition does not contain commodity
095         * code, or contain commodity code that does not have contract manager, we'll
096         * also return null
097         * 
098         * @return Integer the default contract manager code if applicable, or null.
099         */
100        public Integer getContractManagerCode() {
101            String paramName = PurapParameterConstants.ENABLE_DEFAULT_CONTRACT_MANAGER_IND;
102            String paramValue = SpringContext.getBean(ParameterService.class).getParameterValue(ContractManagerAssignmentDocument.class, paramName);
103            if ( paramValue.equals("Y") && (contractManagerCode == null) && getFirstLineItem().getCommodityCode() != null) {
104                List<CommodityContractManager> commodityContractManagers = getFirstLineItem().getCommodityCode().getCommodityContractManagers();
105                if (commodityContractManagers != null && commodityContractManagers.size() > 0) {
106                    int count = 0;
107                    Integer matchingContractManagerCode = null;
108                    for (CommodityContractManager commodityContractManager : commodityContractManagers) {
109                        if (this.getRequisition().getDeliveryCampusCode().equals(commodityContractManager.getCampusCode())) {
110                            count = count + 1;
111                            matchingContractManagerCode = commodityContractManager.getContractManagerCode();
112                        }
113                    }
114                    if (count == 1) {
115                        setContractManagerCode(matchingContractManagerCode);
116                        return contractManagerCode;
117                    }
118                }
119            }
120            return contractManagerCode;
121        }
122    
123        public void setContractManagerCode(Integer contractManagerCode) {
124            this.contractManagerCode = contractManagerCode;
125        }
126    
127        public ContractManager getContractManager() {
128            return contractManager;
129        }
130    
131        /**
132         * @deprecated
133         */
134        public void setContractManager(ContractManager contractManager) {
135            this.contractManager = contractManager;
136        }
137    
138        public RequisitionDocument getRequisition() {
139            return requisition;
140        }
141    
142        /**
143         * @deprecated
144         */
145        public void setRequisition(RequisitionDocument requisition) {
146            this.requisition = requisition;
147        }
148    
149        public ContractManagerAssignmentDocument getContractManagerAssignmentDocument() {
150            return contractManagerAssignmentDocument;
151        }
152    
153        public void setContractManagerAssignmentDocument(ContractManagerAssignmentDocument contractManagerAssignmentDocument) {
154            this.contractManagerAssignmentDocument = contractManagerAssignmentDocument;
155        }
156        
157        /**
158         * Returns the formatted string of the create date. If the createDate is currently null, we'll
159         * get the createDate from the workflowDocument.
160         * 
161         * @return
162         * @throws WorkflowException
163         */
164        public String getCreateDate() throws WorkflowException{
165            if (createDate == null) {
166                Formatter formatter = new DateViewTimestampObjectFormatter();
167                createDate = (String)formatter.format(getRequisition().getDocumentHeader().getWorkflowDocument().getCreateDate());
168            }
169            return createDate;
170        }
171    
172        public void setCreateDate(String createDate) {
173            this.createDate = createDate;
174        }
175    
176        private PurchasingItemBase getFirstLineItem() {
177            return (PurchasingItemBase)this.getRequisition().getItem(0);
178        }
179    
180        public String getDeliveryCampusCode() {
181            return deliveryCampusCode;
182        }
183    
184        public void setDeliveryCampusCode(String deliveryCampusCode) {
185            this.deliveryCampusCode = deliveryCampusCode;
186        }
187    
188        public String getVendorName() {
189            return vendorName;
190        }
191    
192        public void setVendorName(String vendorName) {
193            this.vendorName = vendorName;
194        }
195    
196        /**
197         * @see org.kuali.rice.kns.bo.BusinessObjectBase#toStringMapper()
198         */
199        protected LinkedHashMap toStringMapper() {
200            LinkedHashMap m = new LinkedHashMap();
201            m.put(KFSPropertyConstants.DOCUMENT_NUMBER, this.documentNumber);
202            if (this.requisitionIdentifier != null) {
203                m.put("requisitionIdentifier", this.requisitionIdentifier.toString());
204            }
205            return m;
206        }
207        
208    }