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