001/*
002 * Copyright 2006-2008 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 */
016package org.kuali.ole.module.purap.document.web.struts;
017
018import org.kuali.ole.module.purap.PurapConstants;
019import org.kuali.ole.module.purap.businessobject.ContractManagerAssignmentDetail;
020import org.kuali.ole.module.purap.document.ContractManagerAssignmentDocument;
021import org.kuali.ole.module.purap.document.RequisitionDocument;
022import org.kuali.ole.sys.context.SpringContext;
023import org.kuali.ole.sys.document.web.struts.FinancialSystemTransactionalDocumentActionBase;
024import org.kuali.rice.kew.api.KewApiConstants;
025import org.kuali.rice.kew.api.exception.WorkflowException;
026import org.kuali.rice.kns.document.authorization.DocumentAuthorizer;
027import org.kuali.rice.kns.web.struts.form.KualiDocumentFormBase;
028import org.kuali.rice.krad.document.Document;
029import org.kuali.rice.krad.service.DocumentService;
030import org.kuali.rice.krad.util.GlobalVariables;
031
032import java.util.ArrayList;
033import java.util.HashMap;
034import java.util.List;
035import java.util.Map;
036
037/**
038 * Struts Action for Contract Manager Assignment document.
039 */
040public 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<Document> requisitionDocumentsFromDocService = new ArrayList();
083        try {
084            if (documentHeaderIds.size() > 0)
085                requisitionDocumentsFromDocService = SpringContext.getBean(DocumentService.class).getDocumentsByListOfDocumentHeaderIds(RequisitionDocument.class, documentHeaderIds);
086        } catch (WorkflowException we) {
087            String errorMsg = "Workflow Exception caught: " + we.getLocalizedMessage();
088            LOG.error(errorMsg, we);
089            throw new RuntimeException(errorMsg, we);
090        }
091
092        //Set the documentHeader of the requisition of each of the ContractManagerAssignmentDetail to the documentHeader of
093        //the requisitions resulted from the documentService, so that we'll have workflowDocument in the documentHeader.
094        for (Document req : requisitionDocumentsFromDocService) {
095            ContractManagerAssignmentDetail detail = (ContractManagerAssignmentDetail) documentHeaderIdsAndDetails.get(req.getDocumentNumber());
096            detail.getRequisition().setDocumentHeader(req.getDocumentHeader());
097        }
098    }
099
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}