1
2
3
4
5
6
7
8
9
10
11
12
13
14
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.engine.node.RouteNodeInstance;
33 import org.kuali.rice.kew.exception.WorkflowServiceErrorException;
34 import org.kuali.rice.kew.exception.WorkflowServiceErrorImpl;
35 import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
36 import org.kuali.rice.kew.service.KEWServiceLocator;
37 import org.kuali.rice.kew.api.KewApiConstants;
38 import org.kuali.rice.kim.api.identity.principal.PrincipalContract;
39
40
41 import java.util.ArrayList;
42 import java.util.HashSet;
43 import java.util.List;
44
45
46
47
48
49
50
51
52 public class SuperUserApproveEvent extends SuperUserActionTakenEvent {
53
54 private static final Logger LOG = Logger.getLogger(SuperUserApproveEvent.class);
55 private final boolean allowFinalApproval;
56
57 public SuperUserApproveEvent(DocumentRouteHeaderValue routeHeader, PrincipalContract principal) {
58 this(routeHeader, principal, DEFAULT_ANNOTATION, DEFAULT_RUN_POSTPROCESSOR_LOGIC);
59 }
60
61 public SuperUserApproveEvent(DocumentRouteHeaderValue routeHeader, PrincipalContract principal, String annotation, boolean runPostProcessor) {
62 super(KewApiConstants.ACTION_TAKEN_SU_APPROVED_CD, KewApiConstants.SUPER_USER_APPROVE, routeHeader, principal, annotation, runPostProcessor);
63 this.allowFinalApproval = isPolicySet(routeHeader.getDocumentType(), DocumentTypePolicy.ALLOW_SU_FINAL_APPROVAL, true);
64 }
65
66 @Override
67 public String validateActionRules() {
68 String error = super.validateActionRules();
69 if (StringUtils.isBlank(error)) {
70 if (!allowFinalApproval && KEWServiceLocator.getRouteNodeService().findFutureNodeNames(getRouteHeader().getDocumentId()).isEmpty()) {
71 error = "Super User Approval disallowed on final node by " + DocumentTypePolicy.ALLOW_SU_FINAL_APPROVAL.getCode() + " policy";
72 }
73 }
74 return error;
75 }
76
77 public void recordAction() throws InvalidActionTakenException {
78
79
80
81
82 DocumentType docType = getRouteHeader().getDocumentType();
83
84 String errorMessage = validateActionRules();
85 if (!org.apache.commons.lang.StringUtils.isEmpty(errorMessage)) {
86 LOG.info("User not authorized");
87 List<WorkflowServiceErrorImpl> errors = new ArrayList<WorkflowServiceErrorImpl>();
88 errors.add(new WorkflowServiceErrorImpl(errorMessage, AUTHORIZATION));
89 throw new WorkflowServiceErrorException(errorMessage, errors);
90 }
91
92 ActionTakenValue actionTaken = saveActionTaken();
93
94 notifyActionTaken(actionTaken);
95
96 if (getRouteHeader().isInException() || getRouteHeader().isStateInitiated()) {
97 LOG.debug("Moving document back to Enroute");
98 String oldStatus = getRouteHeader().getDocRouteStatus();
99 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
132 }
133 }