View Javadoc
1   /**
2    * Copyright 2005-2016 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kew.actions;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.apache.log4j.Logger;
20  import org.kuali.rice.kew.actionrequest.ActionRequestFactory;
21  import org.kuali.rice.kew.actionrequest.ActionRequestValue;
22  import org.kuali.rice.kew.actiontaken.ActionTakenValue;
23  import org.kuali.rice.kew.api.doctype.DocumentTypePolicy;
24  import org.kuali.rice.kew.api.exception.InvalidActionTakenException;
25  import org.kuali.rice.kew.api.exception.WorkflowException;
26  import org.kuali.rice.kew.doctype.bo.DocumentType;
27  import org.kuali.rice.kew.engine.BlanketApproveEngine;
28  import org.kuali.rice.kew.engine.OrchestrationConfig;
29  import org.kuali.rice.kew.engine.RouteContext;
30  import org.kuali.rice.kew.engine.OrchestrationConfig.EngineCapability;
31  import org.kuali.rice.kew.engine.node.RequestsNode;
32  import org.kuali.rice.kew.exception.WorkflowServiceErrorException;
33  import org.kuali.rice.kew.exception.WorkflowServiceErrorImpl;
34  import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
35  import org.kuali.rice.kew.service.KEWServiceLocator;
36  import org.kuali.rice.kew.api.KewApiConstants;
37  import org.kuali.rice.kim.api.identity.principal.PrincipalContract;
38  
39  
40  import java.util.ArrayList;
41  import java.util.HashSet;
42  import java.util.List;
43  
44  
45  /**
46   * Does a super user approve action.
47   *
48   * @author Kuali Rice Team (rice.collab@kuali.org)
49   *
50   */
51  public class SuperUserApproveEvent extends SuperUserActionTakenEvent {
52  
53  	private static final Logger LOG = Logger.getLogger(SuperUserApproveEvent.class);
54      private final boolean allowFinalApproval;
55  
56      public SuperUserApproveEvent(DocumentRouteHeaderValue routeHeader, PrincipalContract principal) {
57          this(routeHeader, principal, DEFAULT_ANNOTATION, DEFAULT_RUN_POSTPROCESSOR_LOGIC);
58      }
59  
60      public SuperUserApproveEvent(DocumentRouteHeaderValue routeHeader, PrincipalContract principal, String annotation, boolean runPostProcessor) {
61          super(KewApiConstants.ACTION_TAKEN_SU_APPROVED_CD, KewApiConstants.SUPER_USER_APPROVE, routeHeader, principal, annotation, runPostProcessor);
62          this.allowFinalApproval = isPolicySet(routeHeader.getDocumentType(), DocumentTypePolicy.ALLOW_SU_FINAL_APPROVAL, true);
63      }
64  
65      @Override
66      public String validateActionRules() {
67          String error = super.validateActionRules();
68          if (StringUtils.isBlank(error)) {
69              if (!allowFinalApproval && KEWServiceLocator.getRouteNodeService().findFutureNodeNames(getRouteHeader().getDocumentId()).isEmpty()) {
70                  error = "Super User Approval disallowed on final node by " + DocumentTypePolicy.ALLOW_SU_FINAL_APPROVAL.getCode() + " policy";
71              }
72          }
73          return error;
74      }
75  
76  	public void recordAction() throws InvalidActionTakenException {
77  		// TODO: this is used because calling this code from SuperUserAction without
78          // it causes an optimistic lock
79          //setRouteHeader(KEWServiceLocator.getRouteHeaderService().getRouteHeader(getDocumentId(), true));
80  
81  		DocumentType docType = getRouteHeader().getDocumentType();
82  
83          String errorMessage = validateActionRules();
84          if (!org.apache.commons.lang.StringUtils.isEmpty(errorMessage)) {
85              LOG.info("User not authorized");
86              List<WorkflowServiceErrorImpl> errors = new ArrayList<WorkflowServiceErrorImpl>();
87              errors.add(new WorkflowServiceErrorImpl(errorMessage, AUTHORIZATION));
88              throw new WorkflowServiceErrorException(errorMessage, errors);
89          }
90  
91          ActionTakenValue actionTaken = saveActionTaken();
92  
93  	        notifyActionTaken(actionTaken);
94  
95  		if (getRouteHeader().isInException() || getRouteHeader().isStateInitiated()) {
96  			LOG.debug("Moving document back to Enroute");
97  			String oldStatus = getRouteHeader().getDocRouteStatus();
98  			getRouteHeader().markDocumentEnroute();
99  			String newStatus = getRouteHeader().getDocRouteStatus();
100 			notifyStatusChange(newStatus, oldStatus);
101             DocumentRouteHeaderValue routeHeaderValue = KEWServiceLocator.getRouteHeaderService().
102                     saveRouteHeader(getRouteHeader());
103             setRouteHeader(routeHeaderValue);
104 		}
105 
106 		OrchestrationConfig config = new OrchestrationConfig(EngineCapability.BLANKET_APPROVAL, new HashSet<String>(), actionTaken, docType.getSuperUserApproveNotificationPolicy().getPolicyValue(), isRunPostProcessorLogic());
107 		RequestsNode.setSuppressPolicyErrors(RouteContext.getCurrentRouteContext());
108 		try {
109 			completeAnyOutstandingCompleteApproveRequests(actionTaken, docType.getSuperUserApproveNotificationPolicy().getPolicyValue());
110 			BlanketApproveEngine blanketApproveEngine = KEWServiceLocator.getWorkflowEngineFactory().newEngine(config);
111 			blanketApproveEngine.process(getRouteHeader().getDocumentId(), null);
112 		} catch (Exception e) {
113 			LOG.error("Failed to orchestrate the document to SuperUserApproved.", e);
114 			throw new InvalidActionTakenException("Failed to orchestrate the document to SuperUserApproved.", e);
115 		}
116 
117 	}
118 
119 	@SuppressWarnings("unchecked")
120 	protected void completeAnyOutstandingCompleteApproveRequests(ActionTakenValue actionTaken, boolean sendNotifications) throws Exception {
121         List<ActionRequestValue> actionRequests = new ArrayList<ActionRequestValue>();
122 		actionRequests.addAll(KEWServiceLocator.getActionRequestService().findPendingByActionRequestedAndDocId(KewApiConstants.ACTION_REQUEST_APPROVE_REQ, getDocumentId()));
123 		actionRequests.addAll(KEWServiceLocator.getActionRequestService().findPendingByActionRequestedAndDocId(KewApiConstants.ACTION_REQUEST_COMPLETE_REQ, getDocumentId()));
124         List<ActionRequestValue> deactivatedRequests = new ArrayList<ActionRequestValue>();
125 		for (ActionRequestValue actionRequest : actionRequests) {
126             deactivatedRequests.add(KEWServiceLocator.getActionRequestService().deactivateRequest(actionTaken, actionRequest));
127 		}
128 		if (sendNotifications) {
129 			new ActionRequestFactory(this.getRouteHeader()).generateNotifications(deactivatedRequests, getPrincipal(), this.findDelegatorForActionRequests(deactivatedRequests), KewApiConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ, KewApiConstants.ACTION_TAKEN_SU_APPROVED_CD);
130 		}
131 	}
132 
133 	protected void markDocument() throws WorkflowException {
134 		// do nothing since we are overriding the entire behavior
135 	}
136 }