Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
NotificationSuppression |
|
| 4.0;4 | ||||
NotificationSuppression$1 |
|
| 4.0;4 |
1 | /* | |
2 | * Copyright 2007-2009 The Kuali Foundation | |
3 | * | |
4 | * Licensed under the Educational Community License, Version 2.0 (the "License"); | |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at | |
7 | * | |
8 | * http://www.opensource.org/licenses/ecl2.php | |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
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 | * This utility class encapsulates functions used to provide notification suppression | |
35 | * | |
36 | * @author Kuali Rice Team (rice.collab@kuali.org) | |
37 | * | |
38 | */ | |
39 | 0 | public class NotificationSuppression { |
40 | ||
41 | public static final String SUPPRESS_NOTIFY_KEY_START = "SuppressNotify"; | |
42 | ||
43 | /** | |
44 | * add metadata (a NodeState) to the route node so that if this action request is regenerated | |
45 | * verbatim, the notification email will suppressed (since it is a duplicate!). | |
46 | * @param nodeInstance where additional NodeState will be added | |
47 | * @param actionRequestValue | |
48 | */ | |
49 | public void addNotificationSuppression( | |
50 | RouteNodeInstance nodeInstance, ActionRequestValue actionRequestValue) { | |
51 | ||
52 | // iterative depth first traversal of the action request tree | |
53 | 0 | LinkedList<ActionRequestValue> stack = new LinkedList<ActionRequestValue>(); |
54 | // push | |
55 | 0 | stack.add(actionRequestValue); |
56 | ||
57 | 0 | while (stack.size() > 0) { |
58 | // pop our next action request | |
59 | 0 | ActionRequestValue childActionRequest = stack.removeLast(); |
60 | ||
61 | // process this action request only if it is a leaf | |
62 | 0 | if (childActionRequest.getChildrenRequests() == null || |
63 | childActionRequest.getChildrenRequests().size() == 0) { | |
64 | 0 | List<String> requestKeys = getSuppressNotifyNodeStateKeys(childActionRequest); |
65 | 0 | if (requestKeys != null) for (String requestKey : requestKeys) { |
66 | 0 | if (nodeInstance.getNodeState(requestKey) == null) { // only add once |
67 | 0 | NodeState ns = new NodeState(); |
68 | 0 | ns.setKey(requestKey); |
69 | 0 | ns.setValue("notification suppression"); |
70 | 0 | nodeInstance.addNodeState(ns); |
71 | 0 | } |
72 | } | |
73 | } | |
74 | ||
75 | // put child action requests on the stack | |
76 | 0 | if (childActionRequest.getChildrenRequests() != null) { |
77 | // equivalent to 'push' all | |
78 | 0 | stack.addAll(childActionRequest.getChildrenRequests()); |
79 | } | |
80 | 0 | } |
81 | 0 | } |
82 | ||
83 | /** | |
84 | * This method filters any ActionItems whose related ActionRequestValueS have been flagged for notification | |
85 | * suppression. | |
86 | * | |
87 | * @param actionItems the ActionItemS to filter | |
88 | * @param routeNodeInstance the RouteNodeInstance that the actionItems are associated with | |
89 | */ | |
90 | protected void filterNotificationSuppressedActionItems(List<ActionItem> actionItems, | |
91 | final RouteNodeInstance routeNodeInstance) { | |
92 | ||
93 | // remove all actionItems from the collection whose request has a suppress notification node state element | |
94 | 0 | CollectionUtils.filter(actionItems, new Predicate() { |
95 | public boolean evaluate(Object object) { | |
96 | 0 | boolean result = true; |
97 | 0 | ActionItem actionItem = (ActionItem)object; |
98 | 0 | ActionRequestValue actionRequest = |
99 | KEWServiceLocator.getActionRequestService().findByActionRequestId(actionItem.getActionRequestId()); | |
100 | ||
101 | 0 | List<String> suppressNotificationKeys = getSuppressNotifyNodeStateKeys(actionRequest); |
102 | 0 | if (suppressNotificationKeys != null && suppressNotificationKeys.size() > 0) { |
103 | // if any of the keys are not present, we need to notify | |
104 | 0 | boolean containsAll = true; |
105 | 0 | for (String key : suppressNotificationKeys) { |
106 | 0 | if (routeNodeInstance.getNodeState(key) == null) { |
107 | 0 | containsAll = false; |
108 | 0 | break; |
109 | } | |
110 | } | |
111 | // actionItem will be filtered if this Predicate returns false | |
112 | 0 | result = !containsAll; // only filters if all keys are present |
113 | } | |
114 | 0 | return result; |
115 | } | |
116 | }); | |
117 | 0 | } |
118 | ||
119 | /** | |
120 | * | |
121 | * <p>This method takes care of notification for ActionItemS. It has logic for suppressing notifications | |
122 | * when the RouteNodeInstance has NodeState specifically hinting for notification suppression for a given | |
123 | * ActionItem. | |
124 | * | |
125 | * <p>A side effect is that any notification suppression NodeStateS will be removed | |
126 | * from the RouteNodeInstance after notifications are sent. | |
127 | * | |
128 | * @param actionItems a list of ActionItemS related to the given routeNodeInstance | |
129 | * @param routeNodeInstance the RouteNodeInstance related to the given actionItems | |
130 | */ | |
131 | public void notify(List<ActionItem> actionItems, RouteNodeInstance routeNodeInstance) { | |
132 | ||
133 | 0 | if (actionItems != null && actionItems.size() > 0) { |
134 | 0 | actionItems = new ArrayList<ActionItem>(actionItems); // defensive copy since we will filter |
135 | 0 | filterNotificationSuppressedActionItems(actionItems, routeNodeInstance); |
136 | // notify for any actionItems that were not filtered | |
137 | 0 | if (actionItems.size() > 0) { KEWServiceLocator.getNotificationService().notify(actionItems); } |
138 | 0 | deleteNotificationSuppression(routeNodeInstance); |
139 | } | |
140 | 0 | } |
141 | ||
142 | /** | |
143 | * This method removes all NodeStates related to notification suppression, saving the RouteNodeInstance if there | |
144 | * were any removed. | |
145 | * | |
146 | * @param routeNodeInstance | |
147 | */ | |
148 | @SuppressWarnings("unchecked") | |
149 | private void deleteNotificationSuppression( | |
150 | final RouteNodeInstance routeNodeInstance) { | |
151 | // remove all suppress notification node states | |
152 | 0 | List<NodeState> nodeStates = routeNodeInstance.getState(); |
153 | 0 | if (nodeStates != null && nodeStates.size() > 0) { |
154 | 0 | List<String> nodeStateKeysToRemove = new ArrayList<String>(nodeStates.size()); |
155 | ||
156 | 0 | for (NodeState nodeState : nodeStates) { |
157 | 0 | if (nodeState.getKey().startsWith(NotificationSuppression.SUPPRESS_NOTIFY_KEY_START)) { |
158 | 0 | nodeStateKeysToRemove.add(nodeState.getKey()); |
159 | } | |
160 | } | |
161 | 0 | if (nodeStateKeysToRemove.size() > 0) { |
162 | 0 | for (String nodeStateKeyToRemove : nodeStateKeysToRemove) { |
163 | 0 | routeNodeInstance.removeNodeState(nodeStateKeyToRemove); |
164 | } | |
165 | 0 | KEWServiceLocator.getRouteNodeService().save(routeNodeInstance); |
166 | } | |
167 | } | |
168 | 0 | } |
169 | ||
170 | ||
171 | /** | |
172 | * Builds keys for action requests used for notification suppression. | |
173 | * <p>NOTE: This method needs to stay in sync with {@link #getSuppressNotifyNodeStateKeys(org.kuali.rice.kew.dto.ActionRequestDTO)} | |
174 | * Any changes here must be made there as well! | |
175 | * @param a | |
176 | * @return List | |
177 | */ | |
178 | protected List<String> getSuppressNotifyNodeStateKeys(ActionRequest a) { | |
179 | 0 | List<String> results = Collections.emptyList(); |
180 | 0 | if (a != null) { |
181 | 0 | results = new ArrayList<String>(3); |
182 | 0 | addSuppressNotifyNodeStateKey(results, RecipientType.PRINCIPAL.getCode(), a.getPrincipalId()); |
183 | 0 | addSuppressNotifyNodeStateKey(results, RecipientType.GROUP.getCode(), a.getGroupId()); |
184 | 0 | addSuppressNotifyNodeStateKey(results, RecipientType.ROLE.getCode(), a.getQualifiedRoleName()); |
185 | } | |
186 | 0 | return results; |
187 | } | |
188 | ||
189 | /** | |
190 | * Builds keys for action requests used for notification suppression. | |
191 | * <p>NOTE: This method needs to stay in sync with {@link #getSuppressNotifyNodeStateKeys(org.kuali.rice.kew.actionrequest.ActionRequestValue)} | |
192 | * Any changes here must be made there as well! | |
193 | * @param a | |
194 | * @return List | |
195 | */ | |
196 | protected List<String> getSuppressNotifyNodeStateKeys(ActionRequestValue a) { | |
197 | 0 | List<String> results = Collections.emptyList(); |
198 | 0 | if (a != null) { |
199 | 0 | results = new ArrayList<String>(3); |
200 | 0 | addSuppressNotifyNodeStateKey(results, RecipientType.PRINCIPAL.getCode(), a.getPrincipalId()); |
201 | 0 | addSuppressNotifyNodeStateKey(results, RecipientType.GROUP.getCode(), a.getGroupId()); |
202 | 0 | addSuppressNotifyNodeStateKey(results, RecipientType.ROLE.getCode(), a.getQualifiedRoleName()); |
203 | } | |
204 | 0 | return results; |
205 | } | |
206 | ||
207 | ||
208 | /** | |
209 | * This method adds a suppress notify key to the passed in list | |
210 | * | |
211 | * @param results the list that the key will be added to | |
212 | * @param responsiblePartyType | |
213 | * @param responsiblePartyId | |
214 | */ | |
215 | private void addSuppressNotifyNodeStateKey(List<String> results, String responsiblePartyType, | |
216 | String responsiblePartyId) { | |
217 | 0 | if (responsiblePartyId != null && responsiblePartyType != null) { |
218 | 0 | StringBuilder sb = new StringBuilder(SUPPRESS_NOTIFY_KEY_START); |
219 | 0 | sb.append("("); |
220 | 0 | sb.append(responsiblePartyType); |
221 | 0 | sb.append(","); |
222 | 0 | sb.append(responsiblePartyId); |
223 | 0 | sb.append(")"); |
224 | 0 | results.add(sb.toString()); |
225 | } | |
226 | 0 | } |
227 | ||
228 | } |