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