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.ActionRequestValue;
20 import org.kuali.rice.kew.actiontaken.ActionTakenValue;
21 import org.kuali.rice.kew.api.exception.InvalidActionTakenException;
22 import org.kuali.rice.kew.api.exception.WorkflowException;
23 import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
24 import org.kuali.rice.kew.service.KEWServiceLocator;
25 import org.kuali.rice.kew.api.KewApiConstants;
26 import org.kuali.rice.kim.api.identity.principal.PrincipalContract;
27
28
29 import java.util.Iterator;
30 import java.util.List;
31
32
33
34
35
36
37
38 public class CancelAction extends ActionTakenEvent {
39
40 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(CancelAction.class);
41
42 public CancelAction(DocumentRouteHeaderValue rh, PrincipalContract principal) {
43 super(KewApiConstants.ACTION_TAKEN_CANCELED_CD, rh, principal);
44 }
45
46 public CancelAction(DocumentRouteHeaderValue rh, PrincipalContract principal, String annotation) {
47 super(KewApiConstants.ACTION_TAKEN_CANCELED_CD, rh, principal, annotation);
48 }
49
50
51
52
53 @Override
54 public String validateActionRules() {
55 return validateActionRules(getActionRequestService().findAllPendingRequests(routeHeader.getDocumentId()));
56 }
57
58 public String validateActionRules(List<ActionRequestValue> actionRequests) {
59
60 if (!getRouteHeader().isValidActionToTake(getActionPerformedCode())) {
61 return "Document is not in a state to be cancelled";
62 }
63 List<ActionRequestValue> filteredActionRequests = filterActionRequestsByCode(actionRequests, KewApiConstants.ACTION_REQUEST_COMPLETE_REQ);
64 if (!isActionCompatibleRequest(filteredActionRequests)) {
65 return "No request for the user is compatible with the Cancel Action";
66 }
67
68 if (! KEWServiceLocator.getDocumentTypePermissionService().canCancel(getPrincipal().getPrincipalId(), getDocumentId(), getRouteHeader().getDocumentType(), getRouteHeader().getCurrentNodeNames(), getRouteHeader().getDocRouteStatus(), getRouteHeader().getInitiatorWorkflowId())) {
69 return "User is not authorized to Cancel document";
70 }
71 return "";
72 }
73
74
75
76
77 @Override
78 public boolean isActionCompatibleRequest(List<ActionRequestValue> requests) {
79
80
81 if (routeHeader.isStateInitiated() || routeHeader.isStateSaved()) {
82 return true;
83 }
84
85 boolean actionCompatible = false;
86 Iterator ars = requests.iterator();
87 ActionRequestValue actionRequest = null;
88
89 while (ars.hasNext()) {
90 actionRequest = (ActionRequestValue) ars.next();
91 String request = actionRequest.getActionRequested();
92
93
94 if ( (KewApiConstants.ACTION_REQUEST_APPROVE_REQ.equals(request)) ||
95 (KewApiConstants.ACTION_REQUEST_COMPLETE_REQ.equals(request)) ) {
96 actionCompatible = true;
97 break;
98 }
99 }
100
101 return actionCompatible;
102 }
103
104 public void recordAction() throws InvalidActionTakenException {
105 MDC.put("docId", getRouteHeader().getDocumentId());
106 updateSearchableAttributesIfPossible();
107
108 LOG.debug("Canceling document : " + annotation);
109
110 List actionRequests = getActionRequestService().findAllValidRequests(getPrincipal().getPrincipalId(), getDocumentId(), KewApiConstants.ACTION_REQUEST_COMPLETE_REQ);
111 LOG.debug("Checking to see if the action is legal");
112 String errorMessage = validateActionRules(actionRequests);
113 if (!org.apache.commons.lang.StringUtils.isEmpty(errorMessage)) {
114 throw new InvalidActionTakenException(errorMessage);
115 }
116
117 LOG.debug("Record the cancel action");
118 ActionTakenValue actionTaken = saveActionTaken(findDelegatorForActionRequests(actionRequests));
119
120 LOG.debug("Deactivate all pending action requests");
121 actionRequests = getActionRequestService().findPendingByDoc(getDocumentId());
122
123 getActionRequestService().deactivateRequests(actionTaken, actionRequests);
124 notifyActionTaken(actionTaken);
125
126 LOG.debug("Canceling document");
127
128 try {
129 String oldStatus = getRouteHeader().getDocRouteStatus();
130 getRouteHeader().markDocumentCanceled();
131 String newStatus = getRouteHeader().getDocRouteStatus();
132 KEWServiceLocator.getRouteHeaderService().saveRouteHeader(getRouteHeader());
133 notifyStatusChange(newStatus, oldStatus);
134 } catch (WorkflowException ex) {
135 LOG.warn(ex, ex);
136 throw new InvalidActionTakenException(ex.getMessage());
137 }
138 }
139 }