1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kew.notification.service.impl;
17
18 import java.util.Collections;
19 import java.util.Comparator;
20 import java.util.HashSet;
21 import java.util.Iterator;
22 import java.util.List;
23 import java.util.Set;
24
25 import org.apache.commons.collections.ComparatorUtils;
26 import org.kuali.rice.core.api.delegation.DelegationType;
27 import org.kuali.rice.kew.actionitem.ActionItemComparator;
28 import org.kuali.rice.kew.api.KewApiServiceLocator;
29 import org.kuali.rice.kew.api.WorkflowRuntimeException;
30 import org.kuali.rice.kew.api.action.ActionItem;
31 import org.kuali.rice.kew.api.mail.ImmediateEmailReminderQueue;
32 import org.kuali.rice.kew.doctype.bo.DocumentType;
33 import org.kuali.rice.kew.engine.RouteContext;
34 import org.kuali.rice.kew.notification.service.NotificationService;
35 import org.kuali.rice.kew.api.preferences.Preferences;
36 import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
37 import org.kuali.rice.kew.service.KEWServiceLocator;
38 import org.kuali.rice.kew.api.KewApiConstants;
39
40
41
42
43
44
45
46 public class DefaultNotificationService implements NotificationService {
47
48 protected final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(getClass());
49
50 private static final Comparator notificationPriorityComparator = ComparatorUtils.reversedComparator(new ActionItemComparator());
51
52
53
54
55
56 public void notify(List<ActionItem> actionItems) {
57
58 Collections.sort(actionItems, notificationPriorityComparator);
59 Set sentNotifications = new HashSet();
60 for (Iterator iterator = actionItems.iterator(); iterator.hasNext();) {
61 ActionItem actionItem = (ActionItem) iterator.next();
62 if (!sentNotifications.contains(actionItem.getPrincipalId()) && shouldNotify(actionItem)) {
63 sentNotifications.add(actionItem.getPrincipalId());
64 sendNotification(actionItem);
65 }
66 }
67 }
68
69
70
71
72
73 protected void sendNotification(ActionItem actionItem) {
74 ImmediateEmailReminderQueue immediateEmailQueue = KewApiServiceLocator.getImmediateEmailReminderQueue();
75 immediateEmailQueue.sendReminder(actionItem, RouteContext.getCurrentRouteContext().isDoNotSendApproveNotificationEmails());
76
77 }
78
79 protected boolean shouldNotify(ActionItem actionItem) {
80 try {
81 boolean sendEmail = true;
82
83
84
85
86 if (sendEmail && isItemOriginatingFromSave(actionItem) && !shouldNotifyOnSave(actionItem)) {
87 sendEmail = false;
88 }
89 return sendEmail;
90 } catch (Exception e) {
91 throw new WorkflowRuntimeException("Error loading user with workflow id " + actionItem.getPrincipalId() + " for notification.", e);
92 }
93 }
94
95
96
97
98
99 protected boolean isItemOriginatingFromSave(ActionItem actionItem) {
100 return actionItem.getResponsibilityId() != null && actionItem.getResponsibilityId().equals(KewApiConstants.SAVED_REQUEST_RESPONSIBILITY_ID);
101 }
102
103 protected boolean shouldNotifyOnSave(ActionItem actionItem) {
104 DocumentRouteHeaderValue document = KEWServiceLocator.getRouteHeaderService().getRouteHeader(actionItem.getDocumentId());
105 DocumentType documentType = KEWServiceLocator.getDocumentTypeService().findById(document.getDocumentTypeId());
106 return documentType.getNotifyOnSavePolicy().getPolicyValue().booleanValue();
107 }
108
109 public void removeNotification(List<ActionItem> actionItems) {
110
111 }
112 }