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