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.kuali.rice.kew.actiontaken.ActionTakenValue;
019import org.kuali.rice.kew.api.WorkflowRuntimeException;
020import org.kuali.rice.kew.api.exception.InvalidActionTakenException;
021import org.kuali.rice.kew.api.exception.WorkflowException;
022import org.kuali.rice.kew.doctype.bo.DocumentType;
023import org.kuali.rice.kew.engine.BlanketApproveEngine;
024import org.kuali.rice.kew.engine.OrchestrationConfig;
025import org.kuali.rice.kew.engine.OrchestrationConfig.EngineCapability;
026import org.kuali.rice.kew.exception.*;
027import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
028import org.kuali.rice.kew.service.KEWServiceLocator;
029import org.kuali.rice.kew.api.KewApiConstants;
030import org.kuali.rice.kim.api.identity.principal.PrincipalContract;
031
032
033import java.util.ArrayList;
034import java.util.Collections;
035import java.util.List;
036
037
038/**
039 * Does a node level super user approve action.  All approve/complete requests outstanding for
040 * this node are satisfied by this action.
041 *
042 * @author Kuali Rice Team (rice.collab@kuali.org)
043 *
044 */
045public class SuperUserNodeApproveEvent extends SuperUserActionTakenEvent {
046
047    private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(SuperUserNodeApproveEvent.class);
048    private String nodeName;
049
050    public SuperUserNodeApproveEvent(DocumentRouteHeaderValue routeHeader, PrincipalContract principal) {
051        super(KewApiConstants.ACTION_TAKEN_SU_ROUTE_LEVEL_APPROVED_CD, KewApiConstants.SUPER_USER_ROUTE_LEVEL_APPROVE, routeHeader, principal);
052    }
053
054    public SuperUserNodeApproveEvent(DocumentRouteHeaderValue routeHeader, PrincipalContract principal, String annotation, boolean runPostProcessor, String nodeName) {
055        super(KewApiConstants.ACTION_TAKEN_SU_ROUTE_LEVEL_APPROVED_CD, KewApiConstants.SUPER_USER_ROUTE_LEVEL_APPROVE, routeHeader, principal, annotation, runPostProcessor);
056        this.nodeName = nodeName;
057    }
058
059    public void recordAction() throws InvalidActionTakenException {
060
061        if (org.apache.commons.lang.StringUtils.isEmpty(nodeName)) {
062            throw new InvalidActionTakenException("No approval node name set");
063        }
064
065        DocumentType docType = getRouteHeader().getDocumentType();
066
067        String errorMessage = super.validateActionRules();
068        if (!org.apache.commons.lang.StringUtils.isEmpty(errorMessage)) {
069            LOG.info("User not authorized");
070            List<WorkflowServiceErrorImpl> errors = new ArrayList<WorkflowServiceErrorImpl>();
071            errors.add(new WorkflowServiceErrorImpl(errorMessage, SuperUserActionTakenEvent.AUTHORIZATION));
072            throw new WorkflowServiceErrorException(errorMessage, errors);
073        }
074
075        ActionTakenValue actionTaken = saveActionTaken();
076
077        notifyActionTaken(actionTaken);
078
079            if (getRouteHeader().isInException()) {
080                LOG.debug("Moving document back to Enroute from Exception");
081                String oldStatus = getRouteHeader().getDocRouteStatus();
082                getRouteHeader().markDocumentEnroute();
083                String newStatus = getRouteHeader().getDocRouteStatus();
084                notifyStatusChange(newStatus, oldStatus);
085                DocumentRouteHeaderValue routeHeaderValue = KEWServiceLocator.getRouteHeaderService().
086                        saveRouteHeader(getRouteHeader());
087                setRouteHeader(routeHeaderValue);
088            }
089
090            OrchestrationConfig config = new OrchestrationConfig(EngineCapability.BLANKET_APPROVAL, Collections.singleton(nodeName), actionTaken, docType.getSuperUserApproveNotificationPolicy().getPolicyValue(), isRunPostProcessorLogic());
091            try {
092                BlanketApproveEngine blanketApproveEngine = KEWServiceLocator.getWorkflowEngineFactory().newEngine(config);
093                        blanketApproveEngine.process(getRouteHeader().getDocumentId(), null);
094            } catch (Exception e) {
095                if (e instanceof RuntimeException) {
096                        throw (RuntimeException)e;
097                } else {
098                        throw new WorkflowRuntimeException(e.toString(), e);
099                }
100            }
101
102        //queueDocument();
103    }
104
105    protected void markDocument() throws WorkflowException {
106        // do nothing since we are overriding the entire behavior
107    }
108
109
110
111
112
113}