001/**
002 * Copyright 2005-2016 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.rice.kew.actions;
017
018import org.apache.commons.lang.StringUtils;
019import org.apache.log4j.Logger;
020import org.kuali.rice.kew.actionrequest.ActionRequestFactory;
021import org.kuali.rice.kew.actionrequest.ActionRequestValue;
022import org.kuali.rice.kew.actiontaken.ActionTakenValue;
023import org.kuali.rice.kew.api.doctype.DocumentTypePolicy;
024import org.kuali.rice.kew.api.exception.InvalidActionTakenException;
025import org.kuali.rice.kew.api.exception.WorkflowException;
026import org.kuali.rice.kew.doctype.bo.DocumentType;
027import org.kuali.rice.kew.engine.BlanketApproveEngine;
028import org.kuali.rice.kew.engine.OrchestrationConfig;
029import org.kuali.rice.kew.engine.RouteContext;
030import org.kuali.rice.kew.engine.OrchestrationConfig.EngineCapability;
031import org.kuali.rice.kew.engine.node.RequestsNode;
032import org.kuali.rice.kew.engine.node.RouteNodeInstance;
033import org.kuali.rice.kew.exception.WorkflowServiceErrorException;
034import org.kuali.rice.kew.exception.WorkflowServiceErrorImpl;
035import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
036import org.kuali.rice.kew.service.KEWServiceLocator;
037import org.kuali.rice.kew.api.KewApiConstants;
038import org.kuali.rice.kim.api.identity.principal.PrincipalContract;
039
040
041import java.util.ArrayList;
042import java.util.HashSet;
043import java.util.List;
044
045
046/**
047 * Does a super user approve action.
048 *
049 * @author Kuali Rice Team (rice.collab@kuali.org)
050 *
051 */
052public class SuperUserApproveEvent extends SuperUserActionTakenEvent {
053
054        private static final Logger LOG = Logger.getLogger(SuperUserApproveEvent.class);
055    private final boolean allowFinalApproval;
056
057    public SuperUserApproveEvent(DocumentRouteHeaderValue routeHeader, PrincipalContract principal) {
058        this(routeHeader, principal, DEFAULT_ANNOTATION, DEFAULT_RUN_POSTPROCESSOR_LOGIC);
059    }
060
061    public SuperUserApproveEvent(DocumentRouteHeaderValue routeHeader, PrincipalContract principal, String annotation, boolean runPostProcessor) {
062        super(KewApiConstants.ACTION_TAKEN_SU_APPROVED_CD, KewApiConstants.SUPER_USER_APPROVE, routeHeader, principal, annotation, runPostProcessor);
063        this.allowFinalApproval = isPolicySet(routeHeader.getDocumentType(), DocumentTypePolicy.ALLOW_SU_FINAL_APPROVAL, true);
064    }
065
066    @Override
067    public String validateActionRules() {
068        String error = super.validateActionRules();
069        if (StringUtils.isBlank(error)) {
070            if (!allowFinalApproval && KEWServiceLocator.getRouteNodeService().findFutureNodeNames(getRouteHeader().getDocumentId()).isEmpty()) {
071                error = "Super User Approval disallowed on final node by " + DocumentTypePolicy.ALLOW_SU_FINAL_APPROVAL.getCode() + " policy";
072            }
073        }
074        return error;
075    }
076
077        public void recordAction() throws InvalidActionTakenException {
078                // TODO: this is used because calling this code from SuperUserAction without
079        // it causes an optimistic lock
080        //setRouteHeader(KEWServiceLocator.getRouteHeaderService().getRouteHeader(getDocumentId(), true));
081
082                DocumentType docType = getRouteHeader().getDocumentType();
083
084        String errorMessage = validateActionRules();
085        if (!org.apache.commons.lang.StringUtils.isEmpty(errorMessage)) {
086            LOG.info("User not authorized");
087            List<WorkflowServiceErrorImpl> errors = new ArrayList<WorkflowServiceErrorImpl>();
088            errors.add(new WorkflowServiceErrorImpl(errorMessage, AUTHORIZATION));
089            throw new WorkflowServiceErrorException(errorMessage, errors);
090        }
091
092        ActionTakenValue actionTaken = saveActionTaken();
093
094                notifyActionTaken(actionTaken);
095
096                if (getRouteHeader().isInException() || getRouteHeader().isStateInitiated()) {
097                        LOG.debug("Moving document back to Enroute");
098                        String oldStatus = getRouteHeader().getDocRouteStatus();
099                        getRouteHeader().markDocumentEnroute();
100                        String newStatus = getRouteHeader().getDocRouteStatus();
101                        notifyStatusChange(newStatus, oldStatus);
102                        KEWServiceLocator.getRouteHeaderService().saveRouteHeader(getRouteHeader());
103                }
104
105                OrchestrationConfig config = new OrchestrationConfig(EngineCapability.BLANKET_APPROVAL, new HashSet<String>(), actionTaken, docType.getSuperUserApproveNotificationPolicy().getPolicyValue(), isRunPostProcessorLogic());
106                RequestsNode.setSupressPolicyErrors(RouteContext.getCurrentRouteContext());
107                try {
108                        completeAnyOutstandingCompleteApproveRequests(actionTaken, docType.getSuperUserApproveNotificationPolicy().getPolicyValue());
109                        BlanketApproveEngine blanketApproveEngine = KEWServiceLocator.getWorkflowEngineFactory().newEngine(config);
110                        blanketApproveEngine.process(getRouteHeader().getDocumentId(), null);
111                } catch (Exception e) {
112                        LOG.error("Failed to orchestrate the document to SuperUserApproved.", e);
113                        throw new InvalidActionTakenException("Failed to orchestrate the document to SuperUserApproved.", e);
114                }
115
116        }
117
118        @SuppressWarnings("unchecked")
119        protected void completeAnyOutstandingCompleteApproveRequests(ActionTakenValue actionTaken, boolean sendNotifications) throws Exception {
120                List<ActionRequestValue> actionRequests = KEWServiceLocator.getActionRequestService().findPendingByActionRequestedAndDocId(KewApiConstants.ACTION_REQUEST_APPROVE_REQ, getDocumentId());
121                actionRequests.addAll(KEWServiceLocator.getActionRequestService().findPendingByActionRequestedAndDocId(KewApiConstants.ACTION_REQUEST_COMPLETE_REQ, getDocumentId()));
122                for (ActionRequestValue actionRequest : actionRequests) {
123                        KEWServiceLocator.getActionRequestService().deactivateRequest(actionTaken, actionRequest);
124                }
125                if (sendNotifications) {
126                        new ActionRequestFactory(this.getRouteHeader()).generateNotifications(actionRequests, getPrincipal(), this.findDelegatorForActionRequests(actionRequests), KewApiConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ, KewApiConstants.ACTION_TAKEN_SU_APPROVED_CD);
127                }
128        }
129
130        protected void markDocument() throws WorkflowException {
131                // do nothing since we are overriding the entire behavior
132        }
133}