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.exception.WorkflowException;
24 import org.kuali.rice.kew.doctype.bo.DocumentType;
25 import org.kuali.rice.kew.api.exception.InvalidActionTakenException;
26 import org.kuali.rice.kew.exception.WorkflowServiceErrorException;
27 import org.kuali.rice.kew.exception.WorkflowServiceErrorImpl;
28 import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
29 import org.kuali.rice.kew.service.KEWServiceLocator;
30 import org.kuali.rice.kew.api.KewApiConstants;
31 import org.kuali.rice.kim.api.identity.principal.PrincipalContract;
32
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 if (KewApiConstants.ACTION_REQUEST_APPROVE_REQ.equals(actionRequestCode)) {
78 this.setActionTakenCode(KewApiConstants.ACTION_TAKEN_SU_ACTION_REQUEST_APPROVED_CD);
79 } else if (KewApiConstants.ACTION_REQUEST_COMPLETE_REQ.equals(actionRequestCode)) {
80 this.setActionTakenCode(KewApiConstants.ACTION_TAKEN_SU_ACTION_REQUEST_COMPLETED_CD);
81 } else if (KewApiConstants.ACTION_REQUEST_FYI_REQ.equals(actionRequestCode)) {
82 this.setActionTakenCode(KewApiConstants.ACTION_TAKEN_SU_ACTION_REQUEST_FYI_CD);
83 } else if (KewApiConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ.equals(actionRequestCode)) {
84 this.setActionTakenCode(KewApiConstants.ACTION_TAKEN_SU_ACTION_REQUEST_ACKNOWLEDGED_CD);
85 } else {
86
87 LOG.error("Invalid SU delegation action request code: " + actionRequestCode);
88 throw new RuntimeException("Invalid SU delegation action request code: " + actionRequestCode);
89 }
90 }
91
92 protected ActionTakenValue processActionRequests() throws InvalidActionTakenException {
93
94
95 DocumentType docType = getRouteHeader().getDocumentType();
96
97
98 String errorMessage = super.validateActionRules();
99 if (!org.apache.commons.lang.StringUtils.isEmpty(errorMessage)) {
100 LOG.info("User not authorized");
101 List<WorkflowServiceErrorImpl> errors = new ArrayList<WorkflowServiceErrorImpl>();
102 errors.add(new WorkflowServiceErrorImpl(errorMessage, SuperUserActionTakenEvent.AUTHORIZATION));
103 throw new WorkflowServiceErrorException(errorMessage, errors);
104 }
105
106
107
108
109
110
111 this.setActionTaken();
112
113 MDC.put("docId", getRouteHeader().getDocumentId());
114
115 LOG.debug("Super User Delegation Action on action request: " + annotation);
116 KimPrincipalRecipient superUserRecipient = null;
117 if (getActionRequest().getPrincipal() != null) {
118 superUserRecipient = new KimPrincipalRecipient(getActionRequest().getPrincipal());
119 }
120
121 ActionTakenValue actionTaken = this.saveActionTaken(superUserRecipient);
122
123 LOG.debug("Deactivate this action request");
124
125 ActionRequestValue request = getActionRequest();
126 getActionRequestService().deactivateRequest(actionTaken, request);
127 if (docType.getSuperUserApproveNotificationPolicy().getPolicyValue() && request.isApproveOrCompleteRequest()) {
128 KEWServiceLocator.getActionRequestService().activateRequest(
129 new ActionRequestFactory(this.getRouteHeader()).createNotificationRequest(KewApiConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ, request.getPrincipal(), this.getActionTakenCode(), getPrincipal(), null));
130 }
131 notifyActionTaken(actionTaken);
132
133 if (!(KewApiConstants.ACTION_TAKEN_SU_ACTION_REQUEST_FYI_CD.equals(this.getActionTakenCode()) && KewApiConstants.ACTION_TAKEN_SU_ACTION_REQUEST_ACKNOWLEDGED_CD.equals(this.getActionTakenCode()))) {
134 if (getRouteHeader().isInException()) {
135 LOG.debug("Moving document back to Enroute from Exception");
136
137 String oldStatus = getRouteHeader().getDocRouteStatus();
138 this.getRouteHeader().markDocumentEnroute();
139
140 String newStatus = getRouteHeader().getDocRouteStatus();
141 this.notifyStatusChange(newStatus, oldStatus);
142 KEWServiceLocator.getRouteHeaderService().saveRouteHeader(getRouteHeader());
143 }
144 else if (getRouteHeader().isStateSaved()) {
145 if (KewApiConstants.SAVED_REQUEST_RESPONSIBILITY_ID.equals(request.getResponsibilityId())) {
146 LOG.debug("Moving document to Enroute from Saved because action request was request generated by save action");
147
148 String oldStatus = getRouteHeader().getDocRouteStatus();
149 this.getRouteHeader().markDocumentEnroute();
150 String newStatus = getRouteHeader().getDocRouteStatus();
151 this.notifyStatusChange(newStatus, oldStatus);
152 KEWServiceLocator.getRouteHeaderService().saveRouteHeader(getRouteHeader());
153 }
154 }
155 }
156
157 return actionTaken;
158 }
159
160 public void recordAction() throws InvalidActionTakenException {
161 this.processActionRequests();
162 this.queueDocumentProcessing();
163 }
164
165 protected void markDocument() throws WorkflowException {
166 }
167 }