1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.kuali.rice.kew.actions; |
18 | |
|
19 | |
import java.util.ArrayList; |
20 | |
import java.util.List; |
21 | |
|
22 | |
import org.apache.commons.collections.CollectionUtils; |
23 | |
import org.apache.log4j.MDC; |
24 | |
import org.kuali.rice.kew.actionrequest.ActionRequestValue; |
25 | |
import org.kuali.rice.kew.actionrequest.Recipient; |
26 | |
import org.kuali.rice.kew.actiontaken.ActionTakenValue; |
27 | |
import org.kuali.rice.kew.api.action.AdHocRevoke; |
28 | |
import org.kuali.rice.kew.exception.InvalidActionTakenException; |
29 | |
import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue; |
30 | |
import org.kuali.rice.kew.util.KEWConstants; |
31 | |
import org.kuali.rice.kim.api.identity.principal.PrincipalContract; |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
public class RevokeAdHocAction extends ActionTakenEvent { |
40 | |
|
41 | 0 | private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(RevokeAdHocAction.class); |
42 | |
|
43 | |
private String actionRequestId; |
44 | |
private AdHocRevoke revoke; |
45 | |
|
46 | |
public RevokeAdHocAction(DocumentRouteHeaderValue routeHeader, PrincipalContract principal) { |
47 | 0 | super(KEWConstants.ACTION_TAKEN_ADHOC_REVOKED_CD, routeHeader, principal); |
48 | 0 | } |
49 | |
|
50 | |
public RevokeAdHocAction(DocumentRouteHeaderValue routeHeader, PrincipalContract principal, String actionRequestId, String annotation) { |
51 | 0 | super(KEWConstants.ACTION_TAKEN_ADHOC_REVOKED_CD, routeHeader, principal, annotation); |
52 | 0 | this.actionRequestId = actionRequestId; |
53 | 0 | } |
54 | |
|
55 | |
public RevokeAdHocAction(DocumentRouteHeaderValue routeHeader, PrincipalContract principal, AdHocRevoke revoke, String annotation) { |
56 | 0 | super(KEWConstants.ACTION_TAKEN_ADHOC_REVOKED_CD, routeHeader, principal, annotation); |
57 | 0 | this.revoke = revoke; |
58 | 0 | } |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
@Override |
64 | |
public String validateActionRules() { |
65 | 0 | if (!getRouteHeader().isValidActionToTake(getActionPerformedCode())) { |
66 | 0 | return "Revoke adhoc request is not valid on this document"; |
67 | |
} |
68 | 0 | return ""; |
69 | |
} |
70 | |
|
71 | |
@Override |
72 | |
public String validateActionRules(List<ActionRequestValue> actionRequests) { |
73 | 0 | return validateActionRules(); |
74 | |
} |
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
public void recordAction() throws InvalidActionTakenException { |
86 | 0 | MDC.put("docId", getRouteHeader().getDocumentId()); |
87 | 0 | updateSearchableAttributesIfPossible(); |
88 | |
|
89 | 0 | String errorMessage = validateActionRules(); |
90 | 0 | if (!org.apache.commons.lang.StringUtils.isEmpty(errorMessage)) { |
91 | 0 | throw new InvalidActionTakenException(errorMessage); |
92 | |
} |
93 | |
|
94 | 0 | LOG.debug("Revoking adhoc request : " + annotation); |
95 | |
|
96 | 0 | List<ActionRequestValue> requestsToRevoke = new ArrayList<ActionRequestValue>(); |
97 | 0 | List<ActionRequestValue> actionRequests = getActionRequestService().findPendingRootRequestsByDocId(getDocumentId()); |
98 | 0 | for (ActionRequestValue actionRequest : actionRequests) |
99 | |
{ |
100 | 0 | if (matchesActionRequest(revoke, actionRequest)) |
101 | |
{ |
102 | 0 | requestsToRevoke.add(actionRequest); |
103 | |
} |
104 | |
} |
105 | 0 | if (requestsToRevoke.isEmpty() && actionRequestId != null) { |
106 | 0 | throw new InvalidActionTakenException("Failed to revoke action request with id " + actionRequestId + |
107 | |
". ID does not represent a valid ad hoc request!"); |
108 | |
} |
109 | |
|
110 | 0 | Recipient delegator = findDelegatorForActionRequests(actionRequests); |
111 | 0 | LOG.debug("Record the revoke action"); |
112 | 0 | ActionTakenValue actionTaken = saveActionTaken(delegator); |
113 | |
|
114 | 0 | LOG.debug("Revoke all matching action requests, number of matching requests: " + requestsToRevoke.size()); |
115 | 0 | getActionRequestService().deactivateRequests(actionTaken, requestsToRevoke); |
116 | 0 | notifyActionTaken(actionTaken); |
117 | |
|
118 | 0 | } |
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
protected boolean matchesActionRequest(AdHocRevoke adHocRevokeCommand, ActionRequestValue actionRequest) { |
124 | 0 | if (!actionRequest.isAdHocRequest()) { |
125 | 0 | return false; |
126 | |
} |
127 | 0 | if (actionRequestId != null) { |
128 | 0 | return actionRequestId.equals(actionRequest.getActionRequestId().toString()); |
129 | 0 | } else if (adHocRevokeCommand != null) { |
130 | 0 | boolean principalOrGroupId = !CollectionUtils.isEmpty(adHocRevokeCommand.getPrincipalIds()) || !CollectionUtils.isEmpty(adHocRevokeCommand.getGroupIds()); |
131 | 0 | if (!CollectionUtils.isEmpty(adHocRevokeCommand.getNodeNames()) && !adHocRevokeCommand.getNodeNames().contains(actionRequest.getNodeInstance().getName())) { |
132 | 0 | return false; |
133 | |
} |
134 | 0 | if (actionRequest.isUserRequest() && !CollectionUtils.isEmpty(adHocRevokeCommand.getPrincipalIds())) { |
135 | 0 | return adHocRevokeCommand.getPrincipalIds().contains(actionRequest.getPrincipalId()); |
136 | |
} |
137 | 0 | if (actionRequest.isGroupRequest() && !CollectionUtils.isEmpty(adHocRevokeCommand.getGroupIds())) { |
138 | 0 | return adHocRevokeCommand.getGroupIds().contains(actionRequest.getGroupId()); |
139 | |
} |
140 | 0 | return !principalOrGroupId; |
141 | |
} |
142 | 0 | return true; |
143 | |
} |
144 | |
|
145 | |
} |