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.criteria.QueryByCriteria;
19 import org.kuali.rice.ken.bo.NotificationBo;
20 import org.kuali.rice.ken.bo.NotificationMessageDelivery;
21 import org.kuali.rice.ken.dao.NotificationMessegeDeliveryDao;
22 import org.kuali.rice.ken.service.NotificationMessageDeliveryService;
23 import org.kuali.rice.ken.util.NotificationConstants;
24 import org.kuali.rice.krad.data.DataObjectService;
25
26 import java.sql.Timestamp;
27 import java.util.ArrayList;
28 import java.util.Collection;
29 import java.util.List;
30
31 import static org.kuali.rice.core.api.criteria.PredicateFactory.*;
32
33
34
35
36
37
38 public class NotificationMessageDeliveryServiceImpl implements NotificationMessageDeliveryService {
39 private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
40 .getLogger(NotificationMessageDeliveryServiceImpl.class);
41
42 private DataObjectService dataObjectService;
43 private NotificationMessegeDeliveryDao ntdDao;
44
45
46
47
48
49
50 public NotificationMessageDeliveryServiceImpl(DataObjectService dataObjectService, NotificationMessegeDeliveryDao ntdDao) {
51 this.dataObjectService = dataObjectService;
52 this.ntdDao = ntdDao;
53 }
54
55
56
57
58
59
60 public NotificationMessageDelivery getNotificationMessageDelivery(Long id) {
61
62 return dataObjectService.find(NotificationMessageDelivery.class, id);
63 }
64
65
66
67
68
69 @Override
70 public NotificationMessageDelivery getNotificationMessageDeliveryByDelivererId(String id) {
71 QueryByCriteria.Builder criteria = QueryByCriteria.Builder.create();
72 criteria.setPredicates(equal(NotificationConstants.BO_PROPERTY_NAMES.DELIVERY_SYSTEM_ID, id));
73 Collection<NotificationMessageDelivery> results = dataObjectService.findMatching(NotificationMessageDelivery.class, criteria.build()).getResults();
74
75 if (results.isEmpty()) {
76 return null;
77 }
78 if (results.size() > 1) {
79 throw new RuntimeException("More than one message delivery found with the following delivery system id: " + id);
80 }
81
82 return results.iterator().next();
83 }
84
85
86
87
88 public Collection<NotificationMessageDelivery> getNotificationMessageDeliveries() {
89 return dataObjectService.findMatching(NotificationMessageDelivery.class, QueryByCriteria.Builder.create().build()).getResults();
90 }
91
92
93
94
95
96 @Override
97 public Collection<NotificationMessageDelivery> getNotificationMessageDeliveries(NotificationBo notification, String userRecipientId) {
98
99 QueryByCriteria.Builder criteria = QueryByCriteria.Builder.create();
100 criteria.setPredicates(equal(NotificationConstants.BO_PROPERTY_NAMES.NOTIFICATION + ".id", notification.getId()),
101 equal(NotificationConstants.BO_PROPERTY_NAMES.USER_RECIPIENT_ID, userRecipientId));
102
103 return dataObjectService.findMatching(NotificationMessageDelivery.class, criteria.build()).getResults();
104 }
105
106
107
108
109
110
111
112
113
114
115
116 @Override
117 public Collection<NotificationMessageDelivery> takeMessageDeliveriesForDispatch() {
118
119
120
121
122 Collection<NotificationMessageDelivery> messageDeliveries = ntdDao.getUndeliveredMessageDelivers(dataObjectService);
123 List<NotificationMessageDelivery> savedMsgDel = new ArrayList<NotificationMessageDelivery>();
124
125 LOG.debug("Retrieved " + messageDeliveries.size() + " available message deliveries: " + System.currentTimeMillis());
126
127
128 for (NotificationMessageDelivery delivery: messageDeliveries) {
129 delivery.setLockedDateValue(new Timestamp(System.currentTimeMillis()));
130 savedMsgDel.add(dataObjectService.save(delivery));
131 }
132 return savedMsgDel;
133 }
134
135
136
137
138
139
140
141
142
143 @Override
144 public Collection<NotificationMessageDelivery> takeMessageDeliveriesForAutoRemoval() {
145
146 Collection<NotificationMessageDelivery> messageDeliveries = ntdDao.getMessageDeliveriesForAutoRemoval(new Timestamp(System.currentTimeMillis()), dataObjectService);
147 List<NotificationMessageDelivery> savedMsgDel = new ArrayList<NotificationMessageDelivery>();
148 for (NotificationMessageDelivery d: messageDeliveries) {
149 d.setLockedDateValue(new Timestamp(System.currentTimeMillis()));
150 savedMsgDel.add(dataObjectService.save(d));
151 }
152
153 return savedMsgDel;
154
155 }
156
157
158
159
160
161 @Override
162 public void unlockMessageDelivery(NotificationMessageDelivery messageDelivery) {
163
164 NotificationMessageDelivery d = dataObjectService.find(NotificationMessageDelivery.class, messageDelivery.getId());
165
166 if (d == null) {
167 throw new RuntimeException("NotificationMessageDelivery #" + messageDelivery.getId() + " not found to unlock");
168 }
169
170 d.setLockedDateValue(null);
171 dataObjectService.save(d);
172 }
173 }