View Javadoc
1   /*
2    * Copyright 2006-2008 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.kuali.ole.module.purap.document;
18  
19  import org.kuali.ole.module.purap.PurapParameterConstants;
20  import org.kuali.ole.module.purap.PurapPropertyConstants;
21  import org.kuali.ole.module.purap.PurapWorkflowConstants;
22  import org.kuali.ole.module.purap.businessobject.ContractManagerAssignmentDetail;
23  import org.kuali.ole.module.purap.document.service.RequisitionService;
24  import org.kuali.ole.select.document.service.OlePurchaseOrderService;
25  import org.kuali.ole.sys.DynamicCollectionComparator;
26  import org.kuali.ole.sys.context.SpringContext;
27  import org.kuali.ole.sys.document.FinancialSystemTransactionalDocumentBase;
28  import org.kuali.rice.coreservice.framework.parameter.ParameterService;
29  import org.kuali.rice.kew.api.WorkflowDocument;
30  import org.kuali.rice.kew.api.action.ActionRequestType;
31  import org.kuali.rice.kew.api.document.DocumentStatus;
32  import org.kuali.rice.kew.api.exception.WorkflowException;
33  import org.kuali.rice.kew.framework.postprocessor.DocumentRouteStatusChange;
34  import org.kuali.rice.krad.document.Document;
35  import org.kuali.rice.krad.service.DocumentService;
36  
37  import java.util.ArrayList;
38  import java.util.List;
39  import java.util.Set;
40  
41  public class ContractManagerAssignmentDocument extends FinancialSystemTransactionalDocumentBase {
42      protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ContractManagerAssignmentDocument.class);
43  
44      protected List<ContractManagerAssignmentDetail> contractManagerAssignmentDetails = new ArrayList<ContractManagerAssignmentDetail>();
45  
46      // Not persisted (only for labels in tag)
47      protected String requisitionNumber;
48      protected String deliveryCampusCode;
49      protected String vendorName;
50      protected String generalDescription;
51      protected String requisitionTotalAmount;
52      protected String requisitionCreateDate;
53      protected String firstItemDescription;
54      protected String firstItemCommodityCode;
55      protected String firstObjectCode;
56      protected String universityFiscalYear;
57  
58  
59      /**
60       * Default constructor.
61       */
62      public ContractManagerAssignmentDocument() {
63          super();
64      }
65  
66      public ContractManagerAssignmentDetail getContractManagerAssignmentDetail(int index) {
67          while (contractManagerAssignmentDetails.size() <= index) {
68              contractManagerAssignmentDetails.add(new ContractManagerAssignmentDetail());
69          }
70          return (ContractManagerAssignmentDetail) contractManagerAssignmentDetails.get(index);
71      }
72  
73      /**
74       * Perform logic needed to populate the Assign Contract Manager Document with requisitions in status of Awaiting Contract
75       * Manager Assignment.
76       */
77      public void populateDocumentWithRequisitions() {
78          LOG.debug("populateDocumentWithRequisitions() Entering method.");
79  
80          List<RequisitionDocument> unassignedRequisitions = new ArrayList(SpringContext.getBean(RequisitionService.class).getRequisitionsAwaitingContractManagerAssignment());
81          List<String> documentHeaderIds = new ArrayList();
82          for (RequisitionDocument req : unassignedRequisitions) {
83              documentHeaderIds.add(req.getDocumentNumber());
84          }
85  
86          List<Document> requisitionDocumentsFromDocService = new ArrayList();
87          try {
88              if (documentHeaderIds.size() > 0)
89                  requisitionDocumentsFromDocService = SpringContext.getBean(DocumentService.class).getDocumentsByListOfDocumentHeaderIds(RequisitionDocument.class, documentHeaderIds);
90          } catch (WorkflowException we) {
91              String errorMsg = "Workflow Exception caught: " + we.getLocalizedMessage();
92              LOG.error(errorMsg, we);
93              throw new RuntimeException(errorMsg, we);
94          }
95  
96          for (Document req : requisitionDocumentsFromDocService) {
97              contractManagerAssignmentDetails.add(new ContractManagerAssignmentDetail(this, (RequisitionDocument) req));
98          }
99  
100         String[] fieldNames = {PurapPropertyConstants.DELIVERY_CAMPUS_CODE, PurapPropertyConstants.VENDOR_NAME, PurapPropertyConstants.REQUISITION_IDENTIFIER};
101         DynamicCollectionComparator.sort(contractManagerAssignmentDetails, fieldNames);
102         LOG.debug("populateDocumentWithRequisitions() Leaving method.");
103     }
104 
105     @Override
106     public void doRouteStatusChange(DocumentRouteStatusChange statusChangeEvent) {
107         LOG.debug("doRouteStatusChange() Entering method.");
108 
109         super.doRouteStatusChange(statusChangeEvent);
110 
111         if (this.getDocumentHeader().getWorkflowDocument().isProcessed()) {
112             boolean isSuccess = true;
113             StringBuffer failedReqs = new StringBuffer();
114             SpringContext.getBean(OlePurchaseOrderService.class).processACMReq(this);
115 
116             if (!isSuccess) {
117                 failedReqs.deleteCharAt(failedReqs.lastIndexOf(","));
118                 WorkflowDocument workflowDoc = this.getDocumentHeader().getWorkflowDocument();
119                 String currentNodeName = null;
120                 try {
121                     currentNodeName = PurapWorkflowConstants.DOC_ADHOC_NODE_NAME;
122                     if (!(DocumentStatus.INITIATED.equals(workflowDoc.getStatus()))) {
123                         if (this.getCurrentRouteNodeName(workflowDoc) != null) {
124                             currentNodeName = this.getCurrentRouteNodeName(workflowDoc);
125                         }
126                     }
127                     workflowDoc.adHocToPrincipal(ActionRequestType.FYI, currentNodeName, PurapWorkflowConstants.ContractManagerAssignmentDocument.ASSIGN_CONTRACT_DOC_ERROR_COMPLETING_POST_PROCESSING + failedReqs, workflowDoc.getInitiatorPrincipalId(), "Initiator", true);
128                 } catch (WorkflowException e) {
129                     // do nothing; document should have processed successfully and problem is with sending FYI
130                 }
131             }
132         }
133         LOG.debug("doRouteStatusChange() Leaving method.");
134     }
135 
136     /**
137      * @param wd
138      * @return
139      * @throws WorkflowException
140      */
141     protected String getCurrentRouteNodeName(WorkflowDocument wd) throws WorkflowException {
142         Set<String> nodeNames = wd.getCurrentNodeNames();
143         if (nodeNames == null || nodeNames.isEmpty()) {
144             return null;
145         } else {
146             return nodeNames.iterator().next();
147         }
148     }
149 
150     /**
151      * @see org.kuali.rice.krad.document.Document#getDocumentTitle()
152      */
153     @Override
154     public String getDocumentTitle() {
155         String title = "";
156         if (SpringContext.getBean(ParameterService.class).getParameterValueAsBoolean(ContractManagerAssignmentDocument.class, PurapParameterConstants.PURAP_OVERRIDE_ASSIGN_CONTRACT_MGR_DOC_TITLE)) {
157             title = PurapWorkflowConstants.ContractManagerAssignmentDocument.WORKFLOW_DOCUMENT_TITLE;
158         } else {
159             title = super.getDocumentTitle();
160         }
161         return title;
162     }
163 
164     public List getContractManagerAssignmentDetails() {
165         return contractManagerAssignmentDetails;
166     }
167 
168     public void setContractManagerAssignmentDetailss(List contractManagerAssignmentDetails) {
169         this.contractManagerAssignmentDetails = contractManagerAssignmentDetails;
170     }
171 
172     /**
173      * Gets the firstObjectCode attribute.
174      *
175      * @return Returns the firstObjectCode.
176      */
177     public String getFirstObjectCode() {
178         return firstObjectCode;
179     }
180 
181     /**
182      * Gets the deliveryCampusCode attribute.
183      *
184      * @return Returns the deliveryCampusCode.
185      */
186     public String getDeliveryCampusCode() {
187         return deliveryCampusCode;
188     }
189 
190     /**
191      * Gets the firstItemDescription attribute.
192      *
193      * @return Returns the firstItemDescription.
194      */
195     public String getFirstItemDescription() {
196         return firstItemDescription;
197     }
198 
199     /**
200      * Gets the firstItemCommodityCode attribute.
201      *
202      * @return Returns the firstItemCommodityCode.
203      */
204     public String getFirstItemCommodityCode() {
205         return firstItemCommodityCode;
206     }
207 
208     /**
209      * Gets the generalDescription attribute.
210      *
211      * @return Returns the generalDescription.
212      */
213     public String getGeneralDescription() {
214         return generalDescription;
215     }
216 
217     /**
218      * Gets the requisitionCreateDate attribute.
219      *
220      * @return Returns the requisitionCreateDate.
221      */
222     public String getRequisitionCreateDate() {
223         return requisitionCreateDate;
224     }
225 
226     /**
227      * Gets the requisitionNumber attribute.
228      *
229      * @return Returns the requisitionNumber.
230      */
231     public String getRequisitionNumber() {
232         return requisitionNumber;
233     }
234 
235     /**
236      * Gets the requisitionTotalAmount attribute.
237      *
238      * @return Returns the requisitionTotalAmount.
239      */
240     public String getRequisitionTotalAmount() {
241         return requisitionTotalAmount;
242     }
243 
244     /**
245      * Gets the vendorName attribute.
246      *
247      * @return Returns the vendorName.
248      */
249     public String getVendorName() {
250         return vendorName;
251     }
252 
253     public String getUniversityFiscalYear() {
254         return universityFiscalYear;
255     }
256 
257 }