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