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.NotificationMessageDelivery;
19 import org.kuali.rice.ken.deliverer.NotificationMessageDeliverer;
20 import org.kuali.rice.ken.deliverer.impl.KEWActionListMessageDeliverer;
21 import org.kuali.rice.ken.exception.NotificationAutoRemoveException;
22 import org.kuali.rice.ken.service.NotificationMessageDeliveryAutoRemovalService;
23 import org.kuali.rice.ken.service.NotificationMessageDeliveryService;
24 import org.kuali.rice.ken.service.ProcessingResult;
25 import org.kuali.rice.ken.util.NotificationConstants;
26 import org.kuali.rice.krad.data.DataObjectService;
27 import org.springframework.transaction.PlatformTransactionManager;
28
29 import java.sql.Timestamp;
30 import java.util.ArrayList;
31 import java.util.Collection;
32 import java.util.List;
33 import java.util.concurrent.ExecutorService;
34
35
36
37
38
39 public class NotificationMessageDeliveryAutoRemovalServiceImpl extends ConcurrentJob<NotificationMessageDelivery> implements NotificationMessageDeliveryAutoRemovalService {
40 private DataObjectService dataObjectService;
41 private NotificationMessageDeliveryService messageDeliveryService;
42
43
44
45
46
47
48
49
50 public NotificationMessageDeliveryAutoRemovalServiceImpl(DataObjectService dataObjectService, PlatformTransactionManager txManager,
51 ExecutorService executor, NotificationMessageDeliveryService messageDeliveryService) {
52 super(txManager, executor);
53 this.messageDeliveryService = messageDeliveryService;
54 this.dataObjectService = dataObjectService;
55 }
56
57
58
59
60 @Override
61 protected Collection<NotificationMessageDelivery> takeAvailableWorkItems() {
62 return messageDeliveryService.takeMessageDeliveriesForAutoRemoval();
63 }
64
65
66
67
68 @Override
69 protected Collection<String> processWorkItems(Collection<NotificationMessageDelivery> messageDeliveries) {
70 NotificationMessageDelivery firstMessageDelivery = messageDeliveries.iterator().next();
71
72 KEWActionListMessageDeliverer deliverer = new KEWActionListMessageDeliverer();
73 Collection<String> successes = new ArrayList<String>();
74 for (NotificationMessageDelivery delivery: messageDeliveries) {
75 successes.addAll(autoRemove(deliverer, delivery));
76 }
77 return successes;
78 }
79
80
81
82
83
84
85
86 protected Collection<String> autoRemove(NotificationMessageDeliverer messageDeliverer, NotificationMessageDelivery messageDelivery) {
87 List<String> successes = new ArrayList<String>(1);
88
89
90 try {
91 messageDeliverer.autoRemoveMessageDelivery(messageDelivery);
92 LOG.debug("Auto-removal of message delivery '" + messageDelivery.getId() + "' for notification '" + messageDelivery.getNotification().getId() + "' was successful.");
93 successes.add("Auto-removal of message delivery '" + messageDelivery.getId() + "' for notification '" + messageDelivery.getNotification().getId() + "' was successful.");
94 } catch (NotificationAutoRemoveException nmde) {
95 LOG.error("Error auto-removing message " + messageDelivery);
96 throw new RuntimeException(nmde);
97 }
98
99
100
101 markAutoRemoved(messageDelivery);
102
103 return successes;
104 }
105
106
107
108
109
110 protected void markAutoRemoved(NotificationMessageDelivery messageDelivery) {
111 messageDelivery.setMessageDeliveryStatus(NotificationConstants.MESSAGE_DELIVERY_STATUS.AUTO_REMOVED);
112
113 messageDelivery.setLockedDateValue(null);
114 dataObjectService.save(messageDelivery);
115 }
116
117
118
119
120 @Override
121 protected void unlockWorkItem(NotificationMessageDelivery delivery) {
122 messageDeliveryService.unlockMessageDelivery(delivery);
123 }
124
125
126
127
128
129
130
131 public ProcessingResult processAutoRemovalOfDeliveredNotificationMessageDeliveries() {
132 LOG.debug("[" + new Timestamp(System.currentTimeMillis()).toString() + "] STARTING NOTIFICATION AUTO-REMOVAL PROCESSING");
133
134 ProcessingResult result = run();
135
136 LOG.debug("[" + new Timestamp(System.currentTimeMillis()).toString() + "] FINISHED NOTIFICATION AUTO-REMOVAL PROCESSING - Successes = " + result.getSuccesses().size() + ", Failures = " + result.getFailures().size());
137
138 return result;
139 }
140 }