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