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