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    
017    package org.kuali.kfs.module.purap.document;
018    
019    import java.util.ArrayList;
020    import java.util.HashMap;
021    import java.util.List;
022    import java.util.Map;
023    
024    import org.kuali.kfs.module.purap.PurapConstants;
025    import org.kuali.kfs.module.purap.PurapParameterConstants;
026    import org.kuali.kfs.module.purap.PurapPropertyConstants;
027    import org.kuali.kfs.module.purap.PurapWorkflowConstants;
028    import org.kuali.kfs.module.purap.businessobject.ContractManagerAssignmentDetail;
029    import org.kuali.kfs.module.purap.document.service.PurchaseOrderService;
030    import org.kuali.kfs.module.purap.document.service.RequisitionService;
031    import org.kuali.kfs.sys.DynamicCollectionComparator;
032    import org.kuali.kfs.sys.context.SpringContext;
033    import org.kuali.kfs.sys.document.FinancialSystemTransactionalDocumentBase;
034    import org.kuali.rice.kew.dto.DocumentRouteStatusChangeDTO;
035    import org.kuali.rice.kew.dto.NetworkIdDTO;
036    import org.kuali.rice.kew.exception.WorkflowException;
037    import org.kuali.rice.kew.util.KEWConstants;
038    import org.kuali.rice.kns.service.BusinessObjectService;
039    import org.kuali.rice.kns.service.DocumentService;
040    import org.kuali.rice.kns.service.ParameterService;
041    import org.kuali.rice.kns.workflow.service.KualiWorkflowDocument;
042    
043    public class ContractManagerAssignmentDocument extends FinancialSystemTransactionalDocumentBase {
044        protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ContractManagerAssignmentDocument.class);
045    
046        protected List<ContractManagerAssignmentDetail> contractManagerAssignmentDetails = new ArrayList<ContractManagerAssignmentDetail>();
047    
048        // Not persisted (only for labels in tag)
049        protected String requisitionNumber;
050        protected String deliveryCampusCode;
051        protected String vendorName;
052        protected String generalDescription;
053        protected String requisitionTotalAmount;
054        protected String requisitionCreateDate;
055        protected String firstItemDescription;
056        protected String firstItemCommodityCode;
057        protected String firstObjectCode;
058        protected String universityFiscalYear;
059    
060    
061        /**
062         * Default constructor.
063         */
064        public ContractManagerAssignmentDocument() {
065            super();
066        }
067    
068        public ContractManagerAssignmentDetail getContractManagerAssignmentDetail(int index) {
069            while (contractManagerAssignmentDetails.size() <= index) {
070                contractManagerAssignmentDetails.add(new ContractManagerAssignmentDetail());
071            }
072            return (ContractManagerAssignmentDetail) contractManagerAssignmentDetails.get(index);
073        }
074    
075        /**
076         * Perform logic needed to populate the Assign Contract Manager Document with requisitions in status of Awaiting Contract
077         * Manager Assignment.
078         */
079        public void populateDocumentWithRequisitions() {
080            LOG.debug("populateDocumentWithRequisitions() Entering method.");
081            
082            List<RequisitionDocument> unassignedRequisitions = new ArrayList(SpringContext.getBean(RequisitionService.class).getRequisitionsAwaitingContractManagerAssignment());
083            List<String>documentHeaderIds = new ArrayList();
084            for (RequisitionDocument req : unassignedRequisitions) {
085                documentHeaderIds.add(req.getDocumentNumber());
086            }
087            
088            List<RequisitionDocument> requisitionDocumentsFromDocService = new ArrayList();
089            try {
090                if ( documentHeaderIds.size() > 0 )
091                    requisitionDocumentsFromDocService = SpringContext.getBean(DocumentService.class).getDocumentsByListOfDocumentHeaderIds(RequisitionDocument.class, documentHeaderIds);
092            }
093            catch (WorkflowException we) {
094                String errorMsg = "Workflow Exception caught: " + we.getLocalizedMessage();
095                LOG.error(errorMsg, we);
096                throw new RuntimeException(errorMsg, we);
097            }
098      
099            for (RequisitionDocument req : requisitionDocumentsFromDocService) {
100                contractManagerAssignmentDetails.add(new ContractManagerAssignmentDetail(this, req));
101            }
102    
103            String[] fieldNames = {PurapPropertyConstants.DELIVERY_CAMPUS_CODE, PurapPropertyConstants.VENDOR_NAME, PurapPropertyConstants.REQUISITION_IDENTIFIER};
104            DynamicCollectionComparator.sort(contractManagerAssignmentDetails, fieldNames);
105            LOG.debug("populateDocumentWithRequisitions() Leaving method.");   
106        }
107    
108        @Override
109        public void doRouteStatusChange(DocumentRouteStatusChangeDTO statusChangeEvent) {
110            LOG.debug("doRouteStatusChange() Entering method.");
111    
112            super.doRouteStatusChange(statusChangeEvent);
113    
114            if (this.getDocumentHeader().getWorkflowDocument().stateIsProcessed()) {
115                boolean isSuccess = true;
116                StringBuffer failedReqs = new StringBuffer();
117                SpringContext.getBean(PurchaseOrderService.class).processACMReq(this);
118    
119                if (!isSuccess) {
120                    failedReqs.deleteCharAt(failedReqs.lastIndexOf(","));
121                    KualiWorkflowDocument workflowDoc = this.getDocumentHeader().getWorkflowDocument();
122                    String currentNodeName = null;
123                    try {
124                        currentNodeName = PurapWorkflowConstants.DOC_ADHOC_NODE_NAME;
125                        if (!(KEWConstants.ROUTE_HEADER_INITIATED_CD.equals(workflowDoc.getRouteHeader().getDocRouteStatus()))) {
126                            if (this.getCurrentRouteNodeName(workflowDoc) != null) {
127                                currentNodeName = this.getCurrentRouteNodeName(workflowDoc);
128                            }
129                        }
130                        workflowDoc.adHocRouteDocumentToPrincipal(KEWConstants.ACTION_REQUEST_FYI_REQ, currentNodeName, PurapWorkflowConstants.ContractManagerAssignmentDocument.ASSIGN_CONTRACT_DOC_ERROR_COMPLETING_POST_PROCESSING + failedReqs, workflowDoc.getRouteHeader().getInitiatorPrincipalId(), "Initiator", true);
131                    }
132                    catch (WorkflowException e) {
133                        // do nothing; document should have processed successfully and problem is with sending FYI
134                    }
135                }
136            }
137            LOG.debug("doRouteStatusChange() Leaving method.");
138        }
139    
140        protected String getCurrentRouteNodeName(KualiWorkflowDocument wd) throws WorkflowException {
141            String[] nodeNames = wd.getNodeNames();
142            if ((nodeNames == null) || (nodeNames.length == 0)) {
143                return null;
144            }
145            else {
146                return nodeNames[0];
147            }
148        }
149    
150        /**
151         * @see org.kuali.rice.kns.document.Document#getDocumentTitle()
152         */
153        @Override
154        public String getDocumentTitle() {
155            String title = "";
156            if (SpringContext.getBean(ParameterService.class).getIndicatorParameter(ContractManagerAssignmentDocument.class, PurapParameterConstants.PURAP_OVERRIDE_ASSIGN_CONTRACT_MGR_DOC_TITLE)) {
157                title = PurapWorkflowConstants.ContractManagerAssignmentDocument.WORKFLOW_DOCUMENT_TITLE;
158            }
159            else {
160                title = super.getDocumentTitle();
161            }
162            return title;
163        }
164    
165        public List getContractManagerAssignmentDetails() {
166            return contractManagerAssignmentDetails;
167        }
168    
169        public void setContractManagerAssignmentDetailss(List contractManagerAssignmentDetails) {
170            this.contractManagerAssignmentDetails = contractManagerAssignmentDetails;
171        }
172    
173        /**
174         * Gets the firstObjectCode attribute.
175         * 
176         * @return Returns the firstObjectCode.
177         */
178        public String getFirstObjectCode() {
179            return firstObjectCode;
180        }
181    
182        /**
183         * Gets the deliveryCampusCode attribute.
184         * 
185         * @return Returns the deliveryCampusCode.
186         */
187        public String getDeliveryCampusCode() {
188            return deliveryCampusCode;
189        }
190    
191        /**
192         * Gets the firstItemDescription attribute.
193         * 
194         * @return Returns the firstItemDescription.
195         */
196        public String getFirstItemDescription() {
197            return firstItemDescription;
198        }
199    
200       /**
201        * Gets the firstItemCommodityCode attribute.
202        * 
203        * @return Returns the firstItemCommodityCode.
204        */
205        public String getFirstItemCommodityCode() {
206            return firstItemCommodityCode;
207        }
208    
209        /**
210         * Gets the generalDescription attribute.
211         * 
212         * @return Returns the generalDescription.
213         */
214        public String getGeneralDescription() {
215            return generalDescription;
216        }
217    
218        /**
219         * Gets the requisitionCreateDate attribute.
220         * 
221         * @return Returns the requisitionCreateDate.
222         */
223        public String getRequisitionCreateDate() {
224            return requisitionCreateDate;
225        }
226    
227        /**
228         * Gets the requisitionNumber attribute.
229         * 
230         * @return Returns the requisitionNumber.
231         */
232        public String getRequisitionNumber() {
233            return requisitionNumber;
234        }
235    
236        /**
237         * Gets the requisitionTotalAmount attribute.
238         * 
239         * @return Returns the requisitionTotalAmount.
240         */
241        public String getRequisitionTotalAmount() {
242            return requisitionTotalAmount;
243        }
244    
245        /**
246         * Gets the vendorName attribute.
247         * 
248         * @return Returns the vendorName.
249         */
250        public String getVendorName() {
251            return vendorName;
252        }
253    
254        public String getUniversityFiscalYear() {
255            return universityFiscalYear;
256        }
257        
258    }