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.RiceConstants;
19 import org.kuali.rice.core.framework.persistence.dao.GenericDao;
20 import org.kuali.rice.ken.bo.NotificationBo;
21 import org.kuali.rice.ken.bo.NotificationMessageDelivery;
22 import org.kuali.rice.ken.dao.NotificationMessegeDeliveryDao;
23 import org.kuali.rice.ken.service.NotificationMessageDeliveryService;
24 import org.kuali.rice.ken.util.NotificationConstants;
25
26 import java.sql.Timestamp;
27 import java.util.Collection;
28 import java.util.HashMap;
29 import java.util.Map;
30
31
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 GenericDao businessObjectDao;
43 private NotificationMessegeDeliveryDao ntdDao;
44
45
46
47
48
49 public NotificationMessageDeliveryServiceImpl(GenericDao businessObjectDao, NotificationMessegeDeliveryDao ntdDao) {
50 this.businessObjectDao = businessObjectDao;
51 this.ntdDao = ntdDao;
52 }
53
54
55
56
57
58
59 public NotificationMessageDelivery getNotificationMessageDelivery(Long id) {
60 HashMap<String, Long> primaryKeys = new HashMap<String, Long>();
61 primaryKeys.put(NotificationConstants.BO_PROPERTY_NAMES.ID, id);
62
63 return (NotificationMessageDelivery) businessObjectDao.findByPrimaryKey(NotificationMessageDelivery.class, primaryKeys);
64 }
65
66
67
68
69
70 public NotificationMessageDelivery getNotificationMessageDeliveryByDelivererId(String id) {
71 Map<String, Object> c = new HashMap<String, Object>();
72 c.put(NotificationConstants.BO_PROPERTY_NAMES.DELIVERY_SYSTEM_ID, id);
73 Collection<NotificationMessageDelivery> results = businessObjectDao.findMatching(NotificationMessageDelivery.class, c);
74
75 if (results == null || results.size() == 0) return null;
76 if (results.size() > 1) {
77 throw new RuntimeException("More than one message delivery found with the following delivery system id: " + id);
78 }
79 return results.iterator().next();
80 }
81
82
83
84
85 public Collection<NotificationMessageDelivery> getNotificationMessageDeliveries() {
86 return businessObjectDao.findAll(NotificationMessageDelivery.class);
87 }
88
89
90
91
92
93 public Collection<NotificationMessageDelivery> getNotificationMessageDeliveries(NotificationBo notification, String userRecipientId) {
94
95 Map<String, Object> c = new HashMap<String, Object>();
96 c.put(NotificationConstants.BO_PROPERTY_NAMES.NOTIFICATION, notification.getId());
97 c.put(NotificationConstants.BO_PROPERTY_NAMES.USER_RECIPIENT_ID, userRecipientId);
98
99 return businessObjectDao.findMatching(NotificationMessageDelivery.class, c);
100 }
101
102
103
104
105
106
107
108
109
110
111
112 public Collection<NotificationMessageDelivery> takeMessageDeliveriesForDispatch() {
113
114
115
116
117 Collection<NotificationMessageDelivery> messageDeliveries = ntdDao.getUndeliveredMessageDelivers(businessObjectDao);
118
119
120 LOG.debug("Retrieved " + messageDeliveries.size() + " available message deliveries: " + System.currentTimeMillis());
121
122
123 for (NotificationMessageDelivery delivery: messageDeliveries) {
124 delivery.setLockedDateValue(new Timestamp(System.currentTimeMillis()));
125 businessObjectDao.save(delivery);
126 }
127 return messageDeliveries;
128 }
129
130
131
132
133
134
135
136
137
138 public Collection<NotificationMessageDelivery> takeMessageDeliveriesForAutoRemoval() {
139
140 Collection<NotificationMessageDelivery> messageDeliveries = ntdDao.getMessageDeliveriesForAutoRemoval(new Timestamp(System.currentTimeMillis()), businessObjectDao);
141
142 for (NotificationMessageDelivery d: messageDeliveries) {
143 d.setLockedDateValue(new Timestamp(System.currentTimeMillis()));
144 businessObjectDao.save(d);
145 }
146
147 return messageDeliveries;
148
149 }
150
151
152
153
154
155 public void unlockMessageDelivery(NotificationMessageDelivery messageDelivery) {
156 Map<String, Long> c = new HashMap<String, Long>();
157 c.put(NotificationConstants.BO_PROPERTY_NAMES.DELIVERY_SYSTEM_ID, messageDelivery.getId());
158
159 Collection<NotificationMessageDelivery> deliveries = businessObjectDao.findMatching(NotificationMessageDelivery.class, c, true, RiceConstants.NO_WAIT);
160 if (deliveries == null || deliveries.size() == 0) {
161 throw new RuntimeException("NotificationMessageDelivery #" + messageDelivery.getId() + " not found to unlock");
162 }
163
164 NotificationMessageDelivery d = deliveries.iterator().next();
165 d.setLockedDateValue(null);
166
167 businessObjectDao.save(d);
168 }
169 }