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