1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kew.actionrequest.service.impl;
17
18 import java.util.ArrayList;
19 import java.util.Collections;
20 import java.util.LinkedList;
21 import java.util.List;
22
23 import org.apache.commons.collections.CollectionUtils;
24 import org.apache.commons.collections.Predicate;
25 import org.kuali.rice.kew.actionitem.ActionItem;
26 import org.kuali.rice.kew.actionrequest.ActionRequestValue;
27 import org.kuali.rice.kew.api.action.ActionRequest;
28 import org.kuali.rice.kew.api.action.RecipientType;
29 import org.kuali.rice.kew.engine.node.NodeState;
30 import org.kuali.rice.kew.engine.node.RouteNodeInstance;
31 import org.kuali.rice.kew.service.KEWServiceLocator;
32
33
34
35
36
37
38
39 public class NotificationSuppression {
40
41 public static final String SUPPRESS_NOTIFY_KEY_START = "SuppressNotify";
42
43
44
45
46
47
48
49 public void addNotificationSuppression(
50 RouteNodeInstance nodeInstance, ActionRequestValue actionRequestValue) {
51
52
53 LinkedList<ActionRequestValue> stack = new LinkedList<ActionRequestValue>();
54
55 stack.add(actionRequestValue);
56
57 while (stack.size() > 0) {
58
59 ActionRequestValue childActionRequest = stack.removeLast();
60
61
62 if (childActionRequest.getChildrenRequests() == null ||
63 childActionRequest.getChildrenRequests().size() == 0) {
64 List<String> requestKeys = getSuppressNotifyNodeStateKeys(childActionRequest);
65 if (requestKeys != null) for (String requestKey : requestKeys) {
66 if (nodeInstance.getNodeState(requestKey) == null) {
67 NodeState ns = new NodeState();
68 ns.setKey(requestKey);
69 ns.setValue("notification suppression");
70 nodeInstance.addNodeState(ns);
71 }
72 }
73 }
74
75
76 if (childActionRequest.getChildrenRequests() != null) {
77
78 stack.addAll(childActionRequest.getChildrenRequests());
79 }
80 }
81 }
82
83
84
85
86
87
88
89
90 protected void filterNotificationSuppressedActionItems(List<ActionItem> actionItems,
91 final RouteNodeInstance routeNodeInstance) {
92
93
94 CollectionUtils.filter(actionItems, new Predicate() {
95 public boolean evaluate(Object object) {
96 boolean result = true;
97 ActionItem actionItem = (ActionItem)object;
98 ActionRequestValue actionRequest =
99 KEWServiceLocator.getActionRequestService().findByActionRequestId(actionItem.getActionRequestId());
100
101 List<String> suppressNotificationKeys = getSuppressNotifyNodeStateKeys(actionRequest);
102 if (suppressNotificationKeys != null && suppressNotificationKeys.size() > 0) {
103
104 boolean containsAll = true;
105 for (String key : suppressNotificationKeys) {
106 if (routeNodeInstance.getNodeState(key) == null) {
107 containsAll = false;
108 break;
109 }
110 }
111
112 result = !containsAll;
113 }
114 return result;
115 }
116 });
117 }
118
119
120
121
122
123
124
125
126
127
128
129
130
131 public void notify(List<ActionItem> actionItems, RouteNodeInstance routeNodeInstance) {
132
133 if (actionItems != null && actionItems.size() > 0) {
134 actionItems = new ArrayList<ActionItem>(actionItems);
135 filterNotificationSuppressedActionItems(actionItems, routeNodeInstance);
136
137 if (actionItems.size() > 0) {
138 KEWServiceLocator.getNotificationService().notify(ActionItem.to(actionItems));
139 }
140 deleteNotificationSuppression(routeNodeInstance);
141 }
142 }
143
144
145
146
147
148
149
150 @SuppressWarnings("unchecked")
151 private void deleteNotificationSuppression(
152 final RouteNodeInstance routeNodeInstance) {
153
154 List<NodeState> nodeStates = routeNodeInstance.getState();
155 if (nodeStates != null && nodeStates.size() > 0) {
156 List<String> nodeStateKeysToRemove = new ArrayList<String>(nodeStates.size());
157
158 for (NodeState nodeState : nodeStates) {
159 if (nodeState.getKey().startsWith(NotificationSuppression.SUPPRESS_NOTIFY_KEY_START)) {
160 nodeStateKeysToRemove.add(nodeState.getKey());
161 }
162 }
163 if (nodeStateKeysToRemove.size() > 0) {
164 for (String nodeStateKeyToRemove : nodeStateKeysToRemove) {
165 routeNodeInstance.removeNodeState(nodeStateKeyToRemove);
166 }
167 KEWServiceLocator.getRouteNodeService().save(routeNodeInstance);
168 }
169 }
170 }
171
172
173
174
175
176
177
178
179
180 protected List<String> getSuppressNotifyNodeStateKeys(ActionRequest a) {
181 List<String> results = Collections.emptyList();
182 if (a != null) {
183 results = new ArrayList<String>(3);
184 addSuppressNotifyNodeStateKey(results, RecipientType.PRINCIPAL.getCode(), a.getPrincipalId());
185 addSuppressNotifyNodeStateKey(results, RecipientType.GROUP.getCode(), a.getGroupId());
186 addSuppressNotifyNodeStateKey(results, RecipientType.ROLE.getCode(), a.getQualifiedRoleName());
187 }
188 return results;
189 }
190
191
192
193
194
195
196
197
198 protected List<String> getSuppressNotifyNodeStateKeys(ActionRequestValue a) {
199 List<String> results = Collections.emptyList();
200 if (a != null) {
201 results = new ArrayList<String>(3);
202 addSuppressNotifyNodeStateKey(results, RecipientType.PRINCIPAL.getCode(), a.getPrincipalId());
203 addSuppressNotifyNodeStateKey(results, RecipientType.GROUP.getCode(), a.getGroupId());
204 addSuppressNotifyNodeStateKey(results, RecipientType.ROLE.getCode(), a.getQualifiedRoleName());
205 }
206 return results;
207 }
208
209
210
211
212
213
214
215
216
217 private void addSuppressNotifyNodeStateKey(List<String> results, String responsiblePartyType,
218 String responsiblePartyId) {
219 if (responsiblePartyId != null && responsiblePartyType != null) {
220 StringBuilder sb = new StringBuilder(SUPPRESS_NOTIFY_KEY_START);
221 sb.append("(");
222 sb.append(responsiblePartyType);
223 sb.append(",");
224 sb.append(responsiblePartyId);
225 sb.append(")");
226 results.add(sb.toString());
227 }
228 }
229
230 }