1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.ken.service.impl;
17
18 import org.kuali.rice.ken.bo.NotificationBo;
19 import org.kuali.rice.ken.bo.NotificationMessageDelivery;
20 import org.kuali.rice.ken.bo.NotificationRecipientBo;
21 import org.kuali.rice.ken.bo.NotificationRecipientListBo;
22 import org.kuali.rice.ken.bo.UserChannelSubscriptionBo;
23 import org.kuali.rice.ken.deliverer.impl.KEWActionListMessageDeliverer;
24 import org.kuali.rice.ken.exception.NotificationMessageDeliveryException;
25 import org.kuali.rice.ken.service.NotificationMessageDeliveryResolverService;
26 import org.kuali.rice.ken.service.NotificationRecipientService;
27 import org.kuali.rice.ken.service.NotificationService;
28 import org.kuali.rice.ken.service.ProcessingResult;
29 import org.kuali.rice.ken.util.NotificationConstants;
30 import org.kuali.rice.kim.api.KimConstants.KimGroupMemberTypes;
31 import org.kuali.rice.kim.api.identity.principal.Principal;
32 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
33 import org.kuali.rice.krad.data.DataObjectService;
34 import org.springframework.transaction.PlatformTransactionManager;
35
36 import java.sql.Timestamp;
37 import java.util.ArrayList;
38 import java.util.Collection;
39 import java.util.HashSet;
40 import java.util.Iterator;
41 import java.util.List;
42 import java.util.concurrent.ExecutorService;
43
44
45
46
47
48
49
50
51 public class NotificationMessageDeliveryResolverServiceImpl extends ConcurrentJob<NotificationBo> implements NotificationMessageDeliveryResolverService {
52 private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
53 .getLogger(NotificationMessageDeliveryResolverServiceImpl.class);
54
55 private NotificationRecipientService notificationRecipientService;
56 private DataObjectService dataObjectService;
57 private NotificationService notificationService;
58
59
60
61
62
63
64
65
66 public NotificationMessageDeliveryResolverServiceImpl(NotificationService notificationService, NotificationRecipientService notificationRecipientService,
67 DataObjectService dataObjectService, PlatformTransactionManager txManager, ExecutorService executor) {
68 super(txManager, executor);
69 this.notificationService = notificationService;
70 this.notificationRecipientService = notificationRecipientService;
71 this.dataObjectService = dataObjectService;
72 }
73
74
75
76
77
78 @Override
79 protected Collection<NotificationBo> takeAvailableWorkItems() {
80 Collection<NotificationBo> nots = notificationService.takeNotificationsForResolution();
81
82
83
84
85
86 return nots;
87 }
88
89
90
91
92
93
94
95
96 private HashSet<String> buildCompleteRecipientList(NotificationBo notification) {
97 HashSet<String> completeRecipientList = new HashSet<String>(notification.getRecipients().size());
98
99
100 for (int i = 0; i < notification.getRecipients().size(); i++) {
101 NotificationRecipientBo recipient = notification.getRecipient(i);
102 if (KimGroupMemberTypes.GROUP_MEMBER_TYPE.getCode().equals(recipient.getRecipientType())) {
103
104 String[] groupMembers = notificationRecipientService.getGroupMembers(recipient.getRecipientId());
105 for(int j = 0; j < groupMembers.length; j++) {
106 completeRecipientList.add(groupMembers[j]);
107 }
108 } else {
109 Principal principal = KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName(recipient.getRecipientId());
110 completeRecipientList.add(principal.getPrincipalId());
111 }
112 }
113
114
115 Iterator<NotificationRecipientListBo> i = notification.getChannel().getRecipientLists().iterator();
116 while (i.hasNext()) {
117 NotificationRecipientListBo listRecipient = i.next();
118 if (KimGroupMemberTypes.GROUP_MEMBER_TYPE.getCode().equals(listRecipient.getRecipientType())) {
119
120 String[] groupMembers = notificationRecipientService.getGroupMembers(listRecipient.getRecipientId());
121 for (int j = 0; j < groupMembers.length; j++) {
122 completeRecipientList.add(groupMembers[j]);
123 }
124 } else {
125 Principal principal = KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName(listRecipient.getRecipientId());
126 completeRecipientList.add(principal.getPrincipalId());
127 }
128 }
129
130
131 List<UserChannelSubscriptionBo> subscriptions = notification.getChannel().getSubscriptions();
132 for (UserChannelSubscriptionBo subscription: subscriptions) {
133
134
135
136 completeRecipientList.add(subscription.getUserId());
137 }
138
139 return completeRecipientList;
140 }
141
142
143
144
145
146
147
148
149
150 @Override
151 protected Collection<Object> processWorkItems(Collection<NotificationBo> notifications) {
152 List<Object> successes = new ArrayList<Object>();
153
154
155
156 for (NotificationBo notification: notifications) {
157
158 HashSet<String> uniqueRecipients = buildCompleteRecipientList(notification);
159
160
161 Iterator<String> j = uniqueRecipients.iterator();
162 while(j.hasNext()) {
163 String userRecipientId = j.next();
164
165 NotificationMessageDelivery defaultMessageDelivery = new NotificationMessageDelivery();
166 defaultMessageDelivery.setMessageDeliveryStatus(NotificationConstants.MESSAGE_DELIVERY_STATUS.UNDELIVERED);
167 defaultMessageDelivery.setNotification(notification);
168 defaultMessageDelivery.setUserRecipientId(userRecipientId);
169
170
171 defaultMessageDelivery = dataObjectService.save(defaultMessageDelivery);
172
173 try {
174 new KEWActionListMessageDeliverer().deliverMessage(defaultMessageDelivery);
175 } catch (NotificationMessageDeliveryException e) {
176 throw new RuntimeException(e);
177 }
178
179
180
181 defaultMessageDelivery.setMessageDeliveryStatus(NotificationConstants.MESSAGE_DELIVERY_STATUS.DELIVERED);
182 defaultMessageDelivery = dataObjectService.save(defaultMessageDelivery);
183
184 successes.add(defaultMessageDelivery);
185
186
187 notification.setProcessingFlag(NotificationConstants.PROCESSING_FLAGS.RESOLVED);
188
189 notification.setLockedDateValue(null);
190 dataObjectService.save(notification);
191 }
192
193 }
194
195 return successes;
196 }
197
198
199
200
201 @Override
202 protected void unlockWorkItem(NotificationBo notification) {
203 LOG.debug("Unlocking notification: " + notification.getId() + " " + notification.getTitle());
204 notificationService.unlockNotification(notification);
205 }
206
207
208
209
210
211
212 public ProcessingResult resolveNotificationMessageDeliveries() {
213 LOG.debug("[" + new Timestamp(System.currentTimeMillis()).toString() + "] STARTING RESOLUTION OF NOTIFICATION MESSAGE DELIVERIES");
214
215 ProcessingResult result = run();
216
217 LOG.debug("[" + new Timestamp(System.currentTimeMillis()).toString() + "] FINISHED RESOLUTION OF NOTIFICATION MESSAGE DELIVERIES - " +
218 "Message Delivery End Points Resolved = " + result.getSuccesses().size());
219
220 return result;
221 }
222 }