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  package org.kuali.ole.module.purap.document.web.struts;
17  
18  import org.kuali.ole.module.purap.PurapConstants;
19  import org.kuali.ole.module.purap.businessobject.ContractManagerAssignmentDetail;
20  import org.kuali.ole.module.purap.document.ContractManagerAssignmentDocument;
21  import org.kuali.ole.module.purap.document.RequisitionDocument;
22  import org.kuali.ole.sys.context.SpringContext;
23  import org.kuali.ole.sys.document.web.struts.FinancialSystemTransactionalDocumentActionBase;
24  import org.kuali.rice.kew.api.KewApiConstants;
25  import org.kuali.rice.kew.api.exception.WorkflowException;
26  import org.kuali.rice.kns.document.authorization.DocumentAuthorizer;
27  import org.kuali.rice.kns.web.struts.form.KualiDocumentFormBase;
28  import org.kuali.rice.krad.document.Document;
29  import org.kuali.rice.krad.service.DocumentService;
30  import org.kuali.rice.krad.util.GlobalVariables;
31  
32  import java.util.ArrayList;
33  import java.util.HashMap;
34  import java.util.List;
35  import java.util.Map;
36  
37  /**
38   * Struts Action for Contract Manager Assignment document.
39   */
40  public class ContractManagerAssignmentAction extends FinancialSystemTransactionalDocumentActionBase {
41      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ContractManagerAssignmentAction.class);
42  
43      /**
44       * Do initialization for a new <code>ContractManagerAssignmentDocument</code>.
45       *
46       * @see org.kuali.rice.kns.web.struts.action.KualiDocumentActionBase#createDocument(org.kuali.rice.kns.web.struts.form.KualiDocumentFormBase)
47       */
48      @Override
49      protected void createDocument(KualiDocumentFormBase kualiDocumentFormBase) throws WorkflowException {
50          super.createDocument(kualiDocumentFormBase);
51          ContractManagerAssignmentDocument acmDocument = (ContractManagerAssignmentDocument) kualiDocumentFormBase.getDocument();
52          acmDocument.getDocumentHeader().setDocumentDescription(PurapConstants.ASSIGN_CONTRACT_MANAGER_DEFAULT_DESC);
53          acmDocument.populateDocumentWithRequisitions();
54      }
55  
56      /**
57       * Overrides the method in KualiDocumentActionBase to fetch a List of requisition documents for the
58       * ContractManagerAssignmentDocument from documentService, because we need the workflowDocument to get the
59       * createDate. If we don't fetch the requisition documents from the documentService, the workflowDocument
60       * in the requisition's documentHeader would be null and would cause the transient flexDoc is null error.
61       * That's the reason we need this override.
62       *
63       * @see org.kuali.rice.kns.web.struts.action.KualiDocumentActionBase#loadDocument(org.kuali.rice.kns.web.struts.form.KualiDocumentFormBase)
64       */
65      @Override
66      protected void loadDocument(KualiDocumentFormBase kualiDocumentFormBase) throws WorkflowException {
67          super.loadDocument(kualiDocumentFormBase);
68          ContractManagerAssignmentDocument document = (ContractManagerAssignmentDocument) kualiDocumentFormBase.getDocument();
69          List<String> documentHeaderIds = new ArrayList();
70          Map<String, ContractManagerAssignmentDetail> documentHeaderIdsAndDetails = new HashMap();
71  
72          //Compose a Map in which the keys are the document header ids of each requisition in this acm document and the values are the 
73          //corresponding ContractManagerAssignmentDetail object.
74          for (ContractManagerAssignmentDetail detail : (List<ContractManagerAssignmentDetail>) document.getContractManagerAssignmentDetails()) {
75              documentHeaderIdsAndDetails.put(detail.getRequisition().getDocumentNumber(), detail);
76          }
77          //Add all of the document header ids (which are the keys of the documentHeaderIdsAndDetails  map) to the 
78          //documentHeaderIds List.
79          documentHeaderIds.addAll(documentHeaderIdsAndDetails.keySet());
80  
81          //Get a List of requisition documents from documentService so that we can have the workflowDocument as well
82          List<Document> requisitionDocumentsFromDocService = new ArrayList();
83          try {
84              if (documentHeaderIds.size() > 0)
85                  requisitionDocumentsFromDocService = SpringContext.getBean(DocumentService.class).getDocumentsByListOfDocumentHeaderIds(RequisitionDocument.class, documentHeaderIds);
86          } catch (WorkflowException we) {
87              String errorMsg = "Workflow Exception caught: " + we.getLocalizedMessage();
88              LOG.error(errorMsg, we);
89              throw new RuntimeException(errorMsg, we);
90          }
91  
92          //Set the documentHeader of the requisition of each of the ContractManagerAssignmentDetail to the documentHeader of
93          //the requisitions resulted from the documentService, so that we'll have workflowDocument in the documentHeader.
94          for (Document req : requisitionDocumentsFromDocService) {
95              ContractManagerAssignmentDetail detail = (ContractManagerAssignmentDetail) documentHeaderIdsAndDetails.get(req.getDocumentNumber());
96              detail.getRequisition().setDocumentHeader(req.getDocumentHeader());
97          }
98      }
99  
100     @Override
101     protected void populateAdHocActionRequestCodes(KualiDocumentFormBase formBase) {
102         Document document = formBase.getDocument();
103         DocumentAuthorizer documentAuthorizer = getDocumentHelperService().getDocumentAuthorizer(document);
104         Map<String, String> adHocActionRequestCodes = new HashMap<String, String>();
105 
106         if (documentAuthorizer.canSendAdHocRequests(document, KewApiConstants.ACTION_REQUEST_FYI_REQ, GlobalVariables.getUserSession().getPerson())) {
107             adHocActionRequestCodes.put(KewApiConstants.ACTION_REQUEST_FYI_REQ, KewApiConstants.ACTION_REQUEST_FYI_REQ_LABEL);
108         }
109         if ((document.getDocumentHeader().getWorkflowDocument().isInitiated()
110                 || document.getDocumentHeader().getWorkflowDocument().isSaved()
111                 || document.getDocumentHeader().getWorkflowDocument().isEnroute()
112         ) && documentAuthorizer.canSendAdHocRequests(document, KewApiConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ, GlobalVariables.getUserSession().getPerson())) {
113             adHocActionRequestCodes.put(KewApiConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ, KewApiConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ_LABEL);
114         }
115         formBase.setAdHocActionRequestCodes(adHocActionRequestCodes);
116 
117     }
118 
119 }