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.core.api.util.xml.XmlException;
19 import org.kuali.rice.core.framework.persistence.dao.GenericDao;
20 import org.kuali.rice.ken.bo.Notification;
21 import org.kuali.rice.ken.bo.NotificationMessageDelivery;
22 import org.kuali.rice.ken.bo.NotificationRecipient;
23 import org.kuali.rice.ken.bo.NotificationResponse;
24 import org.kuali.rice.ken.dao.NotificationDao;
25 import org.kuali.rice.ken.deliverer.impl.KEWActionListMessageDeliverer;
26 import org.kuali.rice.ken.service.NotificationAuthorizationService;
27 import org.kuali.rice.ken.service.NotificationMessageContentService;
28 import org.kuali.rice.ken.service.NotificationMessageDeliveryService;
29 import org.kuali.rice.ken.service.NotificationRecipientService;
30 import org.kuali.rice.ken.service.NotificationService;
31 import org.kuali.rice.ken.service.NotificationWorkflowDocumentService;
32 import org.kuali.rice.ken.util.NotificationConstants;
33
34 import java.io.IOException;
35 import java.sql.Timestamp;
36 import java.util.Collection;
37 import java.util.HashMap;
38
39
40
41
42
43
44
45 public class NotificationServiceImpl implements NotificationService {
46 private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
47 .getLogger(NotificationServiceImpl.class);
48
49 private GenericDao businessObjectDao;
50 private NotificationDao notDao;
51 private NotificationMessageContentService messageContentService;
52 private NotificationAuthorizationService notificationAuthorizationService;
53 private NotificationRecipientService notificationRecipientService;
54 private NotificationWorkflowDocumentService notificationWorkflowDocumentService;
55 private NotificationMessageDeliveryService notificationMessageDeliveryService;
56
57
58
59
60
61
62
63
64
65
66 public NotificationServiceImpl(GenericDao businessObjectDao, NotificationMessageContentService messageContentService,
67 NotificationAuthorizationService notificationAuthorizationService, NotificationRecipientService notificationRecipientService,
68 NotificationWorkflowDocumentService notificationWorkflowDocumentService,
69 NotificationMessageDeliveryService notificationMessageDeliveryService,
70 NotificationDao notDao) {
71 this.businessObjectDao = businessObjectDao;
72 this.messageContentService = messageContentService;
73 this.notificationAuthorizationService = notificationAuthorizationService;
74 this.notificationRecipientService = notificationRecipientService;
75 this.notificationWorkflowDocumentService = notificationWorkflowDocumentService;
76 this.notificationMessageDeliveryService = notificationMessageDeliveryService;
77 this.notDao = notDao;
78 }
79
80
81
82
83
84 public Notification getNotification(Long id) {
85 HashMap<String, Long> primaryKeys = new HashMap<String, Long>();
86 primaryKeys.put(NotificationConstants.BO_PROPERTY_NAMES.ID, id);
87
88 return (Notification) businessObjectDao.findByPrimaryKey(Notification.class, primaryKeys);
89 }
90
91
92
93
94
95
96
97
98 public NotificationResponse sendNotification(String notificationMessageAsXml) throws IOException, XmlException {
99
100 Notification notification = messageContentService.parseNotificationRequestMessage(notificationMessageAsXml);
101
102
103 return sendNotification(notification);
104 }
105
106
107
108
109 public NotificationResponse sendNotification(Notification notification) {
110 NotificationResponse response = new NotificationResponse();
111
112
113 boolean producerAuthorizedForChannel = notificationAuthorizationService.isProducerAuthorizedToSendNotificationForChannel(notification.getProducer(), notification.getChannel());
114 if(!producerAuthorizedForChannel) {
115 LOG.error("Producer " + notification.getProducer() + " is not authorized to send messages to channel " + notification.getChannel());
116 response.setStatus(NotificationConstants.RESPONSE_STATUSES.FAILURE);
117 response.setMessage(NotificationConstants.RESPONSE_MESSAGES.PRODUCER_NOT_AUTHORIZED_FOR_CHANNEL);
118 return response;
119 }
120
121
122 for(int i = 0; i < notification.getRecipients().size(); i++) {
123 NotificationRecipient recipient = notification.getRecipient(i);
124 boolean validRecipient = notificationRecipientService.isRecipientValid(recipient.getRecipientId(), recipient.getRecipientType());
125 if(!validRecipient) {
126 response.setStatus(NotificationConstants.RESPONSE_STATUSES.FAILURE);
127 response.setMessage(NotificationConstants.RESPONSE_MESSAGES.INVALID_RECIPIENT + " - recipientId=" +
128 recipient.getRecipientId() + ", recipientType=" + recipient.getRecipientType());
129 return response;
130 }
131 }
132
133
134 if (notification.getCreationDateTime() == null) {
135 notification.setCreationDateTime(new Timestamp(System.currentTimeMillis()));
136 }
137
138
139 if(notification.getSendDateTime() == null) {
140 notification.setSendDateTime(new Timestamp(System.currentTimeMillis()));
141 }
142
143
144 if (notification.getAutoRemoveDateTime() != null) {
145 if (notification.getAutoRemoveDateTime().before(notification.getSendDateTime())) {
146 response.setStatus(NotificationConstants.RESPONSE_STATUSES.FAILURE);
147 response.setMessage(NotificationConstants.RESPONSE_MESSAGES.INVALID_REMOVE_DATE);
148 return response;
149 }
150 }
151
152
153 if(!notification.getDeliveryType().equalsIgnoreCase(NotificationConstants.DELIVERY_TYPES.ACK) &&
154 !notification.getDeliveryType().equalsIgnoreCase(NotificationConstants.DELIVERY_TYPES.FYI)) {
155 response.setStatus(NotificationConstants.RESPONSE_STATUSES.FAILURE);
156 response.setMessage(NotificationConstants.RESPONSE_MESSAGES.INVALID_DELIVERY_TYPE + " - deliveryType=" +
157 notification.getDeliveryType());
158 return response;
159 }
160
161
162 try {
163 businessObjectDao.save(notification);
164 } catch(Exception e) {
165 response.setStatus(NotificationConstants.RESPONSE_STATUSES.FAILURE);
166 response.setMessage(NotificationConstants.RESPONSE_MESSAGES.ERROR_SAVING_NOTIFICATION);
167 return response;
168 }
169
170
171 response.setMessage(NotificationConstants.RESPONSE_MESSAGES.SUCCESSFULLY_RECEIVED);
172 response.setNotificationId(notification.getId());
173 return response;
174 }
175
176
177
178
179
180 public Collection getNotificationsForRecipientByType(String contentTypeName, String recipientId) {
181 HashMap<String, String> queryCriteria = new HashMap<String, String>();
182 queryCriteria.put(NotificationConstants.BO_PROPERTY_NAMES.CONTENT_TYPE_NAME, contentTypeName);
183 queryCriteria.put(NotificationConstants.BO_PROPERTY_NAMES.RECIPIENTS_RECIPIENT_ID, recipientId);
184
185 return businessObjectDao.findMatching(Notification.class, queryCriteria);
186 }
187
188
189
190
191 public void dismissNotificationMessageDelivery(Long id, String user, String cause) {
192
193 NotificationMessageDelivery nmd = notificationMessageDeliveryService.getNotificationMessageDelivery(id);
194 dismissNotificationMessageDelivery(nmd, user, cause);
195 }
196
197
198
199
200 public void dismissNotificationMessageDelivery(NotificationMessageDelivery nmd, String user, String cause) {
201
202 Notification notification = nmd.getNotification();
203
204
205 Collection<NotificationMessageDelivery> userDeliveries = notificationMessageDeliveryService.getNotificationMessageDeliveries(notification, nmd.getUserRecipientId());
206
207 final String targetStatus;
208
209
210 if (NotificationConstants.AUTO_REMOVE_CAUSE.equals(cause)) {
211 targetStatus = NotificationConstants.MESSAGE_DELIVERY_STATUS.AUTO_REMOVED;
212 } else {
213 targetStatus = NotificationConstants.MESSAGE_DELIVERY_STATUS.REMOVED;
214 }
215
216 KEWActionListMessageDeliverer deliverer = new KEWActionListMessageDeliverer();
217
218
219 for (NotificationMessageDelivery messageDelivery: userDeliveries) {
220
221
222 if (!NotificationConstants.MESSAGE_DELIVERY_STATUS.DELIVERED.equals(messageDelivery.getMessageDeliveryStatus())) {
223 LOG.info("Skipping dismissal of non-delivered message delivery #" + messageDelivery.getId());
224 } else if (targetStatus.equals(messageDelivery.getMessageDeliveryStatus())) {
225 LOG.info("Skipping dismissal of already removed message delivery #" + messageDelivery.getId());
226 } else {
227 LOG.debug("Dismissing message delivery #" + messageDelivery.getId() + " " + messageDelivery.getVersionNumber());
228
229
230
231 deliverer.dismissMessageDelivery(messageDelivery, user, cause);
232
233
234
235
236 }
237
238
239
240
241
242 messageDelivery.setMessageDeliveryStatus(targetStatus);
243
244
245
246 LOG.debug("Saving message delivery #" + messageDelivery.getId() + " " + messageDelivery.getVersionNumber());
247 businessObjectDao.save(messageDelivery);
248
249 LOG.debug("Message delivery '" + messageDelivery.getId() + "' for notification '" + messageDelivery.getNotification().getId() + "' was successfully dismissed.");
250 }
251 }
252
253
254
255
256
257
258
259
260
261
262 public Collection<Notification> takeNotificationsForResolution() {
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277 Collection<Notification> available_notifications = notDao.findMatchedNotificationsForResolution(new Timestamp(System.currentTimeMillis()), businessObjectDao);
278
279
280
281
282 if (available_notifications != null) {
283 for (Notification notification: available_notifications) {
284 LOG.info("notification: " + notification);
285 notification.setLockedDate(new Timestamp(System.currentTimeMillis()));
286 businessObjectDao.save(notification);
287 }
288 }
289
290
291 return available_notifications;
292 }
293
294
295
296
297
298
299 public void unlockNotification(Notification notification) {
300
301
302
303
304
305
306
307
308
309
310
311 Collection<Notification> notifications = notDao.findMatchedNotificationsForUnlock(notification, businessObjectDao);
312
313 if (notifications == null || notifications.size() == 0) {
314 throw new RuntimeException("Notification #" + notification.getId() + " not found to unlock");
315 }
316
317 Notification n = notifications.iterator().next();
318 n.setLockedDate(null);
319
320 businessObjectDao.save(n);
321 }
322 }