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.log4j.MDC;
19 import org.kuali.rice.kew.actionrequest.ActionRequestFactory;
20 import org.kuali.rice.kew.actionrequest.ActionRequestValue;
21 import org.kuali.rice.kew.actionrequest.KimPrincipalRecipient;
22 import org.kuali.rice.kew.actiontaken.ActionTakenValue;
23 import org.kuali.rice.kew.api.KewApiConstants;
24 import org.kuali.rice.kew.api.action.ActionType;
25 import org.kuali.rice.kew.api.exception.InvalidActionTakenException;
26 import org.kuali.rice.kew.api.exception.WorkflowException;
27 import org.kuali.rice.kew.doctype.bo.DocumentType;
28 import org.kuali.rice.kew.exception.WorkflowServiceErrorException;
29 import org.kuali.rice.kew.exception.WorkflowServiceErrorImpl;
30 import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
31 import org.kuali.rice.kew.service.KEWServiceLocator;
32 import org.kuali.rice.kim.api.identity.principal.PrincipalContract;
33
34 import java.util.ArrayList;
35 import java.util.List;
36
37
38
39
40
41
42
43 public class SuperUserActionRequestApproveEvent extends SuperUserActionTakenEvent {
44
45
46
47
48
49
50
51
52
53
54 private static final String UNDEFINED_ACTION_TAKEN_CODE = null;
55
56 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(SuperUserActionRequestApproveEvent.class);
57 private String actionRequestId;
58
59 public SuperUserActionRequestApproveEvent(DocumentRouteHeaderValue routeHeader, PrincipalContract principal) {
60 super(UNDEFINED_ACTION_TAKEN_CODE, KewApiConstants.SUPER_USER_ACTION_REQUEST_APPROVE, routeHeader, principal);
61 }
62
63 public SuperUserActionRequestApproveEvent(DocumentRouteHeaderValue routeHeader, PrincipalContract principal, String actionRequestId, String annotation, boolean runPostProcessor) {
64 super(UNDEFINED_ACTION_TAKEN_CODE, KewApiConstants.SUPER_USER_ACTION_REQUEST_APPROVE, routeHeader, principal, annotation, runPostProcessor);
65 this.actionRequestId = actionRequestId;
66 }
67
68 public void setActionTaken() {
69 String actionRequestCode = "";
70
71 ActionRequestValue actionRequest = getActionRequestService().findByActionRequestId(actionRequestId);
72
73 setActionRequest(actionRequest);
74
75 actionRequestCode = actionRequest.getActionRequested();
76
77 ActionType suActionType = ActionType.toSuperUserActionType(ActionType.fromCode(actionRequestCode, true));
78 if (suActionType == null) {
79
80 LOG.error("Invalid SU delegation action request code: " + actionRequestCode);
81 throw new RuntimeException("Invalid SU delegation action request code: " + actionRequestCode);
82 } else {
83 this.setActionTakenCode(suActionType.getCode());
84 }
85 }
86
87 protected ActionTakenValue processActionRequests() throws InvalidActionTakenException {
88
89
90 DocumentType docType = getRouteHeader().getDocumentType();
91
92
93 String errorMessage = super.validateActionRules();
94 if (!org.apache.commons.lang.StringUtils.isEmpty(errorMessage)) {
95 LOG.info("User not authorized");
96 List<WorkflowServiceErrorImpl> errors = new ArrayList<WorkflowServiceErrorImpl>();
97 errors.add(new WorkflowServiceErrorImpl(errorMessage, SuperUserActionTakenEvent.AUTHORIZATION));
98 throw new WorkflowServiceErrorException(errorMessage, errors);
99 }
100
101
102
103
104
105
106 this.setActionTaken();
107
108 MDC.put("docId", getRouteHeader().getDocumentId());
109
110 LOG.debug("Super User Delegation Action on action request: " + annotation);
111 KimPrincipalRecipient superUserRecipient = null;
112 if (getActionRequest().getPrincipal() != null) {
113 superUserRecipient = new KimPrincipalRecipient(getActionRequest().getPrincipal());
114 }
115
116 ActionTakenValue actionTaken = this.saveActionTaken(superUserRecipient);
117
118 LOG.debug("Deactivate this action request");
119
120 ActionRequestValue request = getActionRequest();
121 request = getActionRequestService().deactivateRequest(actionTaken, request);
122 if (docType.getSuperUserApproveNotificationPolicy().getPolicyValue() && request.isApproveOrCompleteRequest()) {
123 KEWServiceLocator.getActionRequestService().activateRequest(
124 new ActionRequestFactory(this.getRouteHeader()).createNotificationRequest(KewApiConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ, request.getPrincipal(), this.getActionTakenCode(), getPrincipal(), null));
125 }
126 notifyActionTaken(actionTaken);
127
128 if (!(KewApiConstants.ACTION_TAKEN_SU_ACTION_REQUEST_FYI_CD.equals(this.getActionTakenCode()) && KewApiConstants.ACTION_TAKEN_SU_ACTION_REQUEST_ACKNOWLEDGED_CD.equals(this.getActionTakenCode()))) {
129 if (getRouteHeader().isInException()) {
130 LOG.debug("Moving document back to Enroute from Exception");
131
132 String oldStatus = getRouteHeader().getDocRouteStatus();
133 this.getRouteHeader().markDocumentEnroute();
134
135 String newStatus = getRouteHeader().getDocRouteStatus();
136 this.notifyStatusChange(newStatus, oldStatus);
137 DocumentRouteHeaderValue routeHeaderValue = KEWServiceLocator.getRouteHeaderService().
138 saveRouteHeader(getRouteHeader());
139 setRouteHeader(routeHeaderValue);
140 }
141 else if (getRouteHeader().isStateSaved()) {
142 if (KewApiConstants.SAVED_REQUEST_RESPONSIBILITY_ID.equals(request.getResponsibilityId())) {
143 LOG.debug("Moving document to Enroute from Saved because action request was request generated by save action");
144
145 String oldStatus = getRouteHeader().getDocRouteStatus();
146 this.getRouteHeader().markDocumentEnroute();
147 String newStatus = getRouteHeader().getDocRouteStatus();
148 this.notifyStatusChange(newStatus, oldStatus);
149 DocumentRouteHeaderValue routeHeaderValue = KEWServiceLocator.getRouteHeaderService().
150 saveRouteHeader(getRouteHeader());
151 setRouteHeader(routeHeaderValue);
152 }
153 }
154 }
155
156 return actionTaken;
157 }
158
159 public void recordAction() throws InvalidActionTakenException {
160 this.processActionRequests();
161 this.queueDocumentProcessing();
162 }
163
164 protected void markDocument() throws WorkflowException {
165 }
166 }