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.document.web.struts;
017    
018    import java.util.ArrayList;
019    import java.util.HashMap;
020    import java.util.List;
021    import java.util.Map;
022    
023    import org.kuali.kfs.module.purap.PurapConstants;
024    import org.kuali.kfs.module.purap.businessobject.ContractManagerAssignmentDetail;
025    import org.kuali.kfs.module.purap.document.ContractManagerAssignmentDocument;
026    import org.kuali.kfs.module.purap.document.RequisitionDocument;
027    import org.kuali.kfs.sys.context.SpringContext;
028    import org.kuali.kfs.sys.document.web.struts.FinancialSystemTransactionalDocumentActionBase;
029    import org.kuali.rice.kew.exception.WorkflowException;
030    import org.kuali.rice.kew.util.KEWConstants;
031    import org.kuali.rice.kns.document.Document;
032    import org.kuali.rice.kns.document.authorization.DocumentAuthorizer;
033    import org.kuali.rice.kns.service.DocumentService;
034    import org.kuali.rice.kns.util.GlobalVariables;
035    import org.kuali.rice.kns.web.struts.form.KualiDocumentFormBase;
036    
037    /**
038     * Struts Action for Contract Manager Assignment document.
039     */
040    public class ContractManagerAssignmentAction extends FinancialSystemTransactionalDocumentActionBase {
041        private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ContractManagerAssignmentAction.class);
042    
043        /**
044         * Do initialization for a new <code>ContractManagerAssignmentDocument</code>.
045         * 
046         * @see org.kuali.rice.kns.web.struts.action.KualiDocumentActionBase#createDocument(org.kuali.rice.kns.web.struts.form.KualiDocumentFormBase)
047         */
048        @Override
049        protected void createDocument(KualiDocumentFormBase kualiDocumentFormBase) throws WorkflowException {
050            super.createDocument(kualiDocumentFormBase);
051            ContractManagerAssignmentDocument acmDocument = (ContractManagerAssignmentDocument) kualiDocumentFormBase.getDocument();
052            acmDocument.getDocumentHeader().setDocumentDescription(PurapConstants.ASSIGN_CONTRACT_MANAGER_DEFAULT_DESC);
053            acmDocument.populateDocumentWithRequisitions();
054        }
055        
056        /**
057         * Overrides the method in KualiDocumentActionBase to fetch a List of requisition documents for the
058         * ContractManagerAssignmentDocument from documentService, because we need the workflowDocument to get the
059         * createDate. If we don't fetch the requisition documents from the documentService, the workflowDocument
060         * in the requisition's documentHeader would be null and would cause the transient flexDoc is null error.
061         * That's the reason we need this override.
062         * 
063         * @see org.kuali.rice.kns.web.struts.action.KualiDocumentActionBase#loadDocument(org.kuali.rice.kns.web.struts.form.KualiDocumentFormBase)
064         */
065        @Override
066        protected void loadDocument (KualiDocumentFormBase kualiDocumentFormBase) throws WorkflowException {
067            super.loadDocument(kualiDocumentFormBase);
068            ContractManagerAssignmentDocument document = (ContractManagerAssignmentDocument)kualiDocumentFormBase.getDocument();
069            List<String>documentHeaderIds = new ArrayList();
070            Map<String, ContractManagerAssignmentDetail>documentHeaderIdsAndDetails = new HashMap();
071            
072            //Compose a Map in which the keys are the document header ids of each requisition in this acm document and the values are the 
073            //corresponding ContractManagerAssignmentDetail object.
074            for (ContractManagerAssignmentDetail detail : (List<ContractManagerAssignmentDetail>)document.getContractManagerAssignmentDetails()) {
075                documentHeaderIdsAndDetails.put(detail.getRequisition().getDocumentNumber(), detail);
076            }
077            //Add all of the document header ids (which are the keys of the documentHeaderIdsAndDetails  map) to the 
078            //documentHeaderIds List.
079            documentHeaderIds.addAll(documentHeaderIdsAndDetails.keySet());
080            
081            //Get a List of requisition documents from documentService so that we can have the workflowDocument as well
082            List<RequisitionDocument> requisitionDocumentsFromDocService = new ArrayList();
083            try {
084                if ( documentHeaderIds.size() > 0 )
085                    requisitionDocumentsFromDocService = SpringContext.getBean(DocumentService.class).getDocumentsByListOfDocumentHeaderIds(RequisitionDocument.class, documentHeaderIds);
086            }
087            catch (WorkflowException we) {
088                String errorMsg = "Workflow Exception caught: " + we.getLocalizedMessage();
089                LOG.error(errorMsg, we);
090                throw new RuntimeException(errorMsg, we);
091            }
092            
093            //Set the documentHeader of the requisition of each of the ContractManagerAssignmentDetail to the documentHeader of
094            //the requisitions resulted from the documentService, so that we'll have workflowDocument in the documentHeader.
095            for (RequisitionDocument req : requisitionDocumentsFromDocService) {
096                ContractManagerAssignmentDetail detail = (ContractManagerAssignmentDetail)documentHeaderIdsAndDetails.get(req.getDocumentNumber());
097                detail.getRequisition().setDocumentHeader(req.getDocumentHeader());
098            }
099        }
100        
101        @Override
102        protected void populateAdHocActionRequestCodes(KualiDocumentFormBase formBase){
103            Document document = formBase.getDocument();
104            DocumentAuthorizer documentAuthorizer = getDocumentHelperService().getDocumentAuthorizer(document);
105            Map<String,String> adHocActionRequestCodes = new HashMap<String,String>();
106    
107            if (documentAuthorizer.canSendAdHocRequests(document, KEWConstants.ACTION_REQUEST_FYI_REQ, GlobalVariables.getUserSession().getPerson())) {
108                    adHocActionRequestCodes.put(KEWConstants.ACTION_REQUEST_FYI_REQ, KEWConstants.ACTION_REQUEST_FYI_REQ_LABEL);
109            }
110            if ( (document.getDocumentHeader().getWorkflowDocument().stateIsInitiated()
111                  || document.getDocumentHeader().getWorkflowDocument().stateIsSaved()
112                  || document.getDocumentHeader().getWorkflowDocument().stateIsEnroute()
113                  )&& documentAuthorizer.canSendAdHocRequests(document, KEWConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ, GlobalVariables.getUserSession().getPerson())) {
114                    adHocActionRequestCodes.put(KEWConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ, KEWConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ_LABEL);
115            } 
116            formBase.setAdHocActionRequestCodes(adHocActionRequestCodes);
117    
118        }
119    
120    }