1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.mobility.push.dao;
17
18 import java.sql.Timestamp;
19 import java.util.Iterator;
20 import java.util.List;
21
22 import javax.persistence.EntityManager;
23 import javax.persistence.PersistenceContext;
24 import javax.persistence.Query;
25
26 import org.apache.log4j.Logger;
27 import org.kuali.mobility.push.entity.Device;
28 import org.kuali.mobility.push.entity.Push;
29 import org.kuali.mobility.push.entity.PushDeviceTuple;
30 import org.kuali.mobility.push.service.PushDeviceTupleService;
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.stereotype.Repository;
33 import org.springframework.transaction.annotation.Transactional;
34
35
36
37
38
39
40
41 @Repository
42 public class PushDaoImpl implements PushDao {
43
44
45 private static final Logger LOG = Logger.getLogger(PushDaoImpl.class);
46
47
48 @PersistenceContext
49 private EntityManager entityManager;
50
51 @Autowired
52 private PushDeviceTupleService pdtService;
53
54
55
56
57 public PushDaoImpl(){}
58
59
60
61
62
63 @SuppressWarnings("unchecked")
64 public Push findPushById(Long id){
65 Query query = entityManager.createNamedQuery("Push.find");
66 query.setParameter("pushId", id);
67 Push result;
68 try{
69 result = (Push) query.getSingleResult();
70 }catch(Exception e){
71 LOG.info("Exception: " + e.getMessage());
72 result = null;
73 }
74 return result;
75 }
76
77
78
79
80
81 @SuppressWarnings("unchecked")
82 public List<Push> findAllPush(){
83 Query query = entityManager.createNamedQuery("Push.findAll");
84 return query.getResultList();
85 }
86
87
88
89
90
91 @SuppressWarnings("unchecked")
92 public int countPushes(){
93 Query query = entityManager.createNamedQuery("Push.countAll");
94 return ((Long)query.getSingleResult()).intValue();
95 }
96
97
98
99
100
101 @SuppressWarnings("unchecked")
102 public List<PushDeviceTuple> findUnsentPushTuples(){
103 Query query = entityManager.createNamedQuery("PushDeviceTuple.findUnsent");
104 return query.getResultList();
105 }
106
107
108
109
110
111 @Transactional
112 public void savePush(Push push){
113 if(push == null){
114 return;
115 }
116 if(push.getPushId() == null){
117 entityManager.persist(push);
118 }else{
119 entityManager.merge(push);
120 }
121 }
122
123
124
125
126
127 @Transactional
128 public boolean removePush(Push push){
129 boolean result = true;
130 if(push == null){
131 result = false;
132 } else if(push.getPushId() == null) {
133 result = false;
134 } else {
135
136 int removedTuples = pdtService.removeTuplesForPush(push);
137 if(removedTuples > 0){
138 LOG.info("Removed " + removedTuples + " tuples associated with Push notifications.");
139 }else{
140 LOG.info("No tuples associated with Push notifications removed.");
141 }
142
143 try{
144 getEntityManager().remove(getEntityManager().contains(push)?push:getEntityManager().merge(push));
145 }catch(Exception e){
146 LOG.info("Exception while trying to remove push notification.", e);
147 result = false;
148 }
149 }
150 return result;
151 }
152
153
154
155
156
157 @Transactional
158 public void savePush(Push push, List<Device> devices){
159 if(push == null){
160 return;
161 }
162 if(push.getPushId() == null){
163 entityManager.persist(push);
164 }else{
165 entityManager.merge(push);
166 }
167 Iterator<Device> i = devices.iterator();
168 while(i.hasNext()){
169 Device d = i.next();
170 PushDeviceTuple pdt = new PushDeviceTuple();
171 pdt.setPushId(push.getPushId());
172 pdt.setDeviceId(d.getId());
173 pdt.setPostedTimestamp(new Timestamp(System.currentTimeMillis()));
174 pdt.setStatus(PushDeviceTuple.STATUS_PENDING);
175 if(pdt.getId() == null){
176 entityManager.persist(pdt);
177 }else{
178 entityManager.merge(pdt);
179 }
180 }
181 }
182
183
184
185
186
187 @Transactional
188 public List<Device> findDevicesForPush(Push push){
189 Query query = entityManager.createNamedQuery("PushDeviceTuple.findPushDevices");
190 query.setParameter("pushId", push.getPushId());
191 return query.getResultList();
192 }
193
194 @Transactional
195 public void savePushDeviceTuples(List<PushDeviceTuple> tuples){
196 if(tuples == null){
197 return;
198 }
199 Iterator<PushDeviceTuple> i = tuples.iterator();
200 while(i.hasNext()){
201 PushDeviceTuple pdt = i.next();
202 if(pdt.getId() == null){
203 entityManager.persist(pdt);
204 }else{
205 entityManager.merge(pdt);
206 }
207 }
208 }
209
210 public EntityManager getEntityManager() {
211 return entityManager;
212 }
213
214 public void setEntityManager(EntityManager entityManager) {
215 this.entityManager = entityManager;
216 }
217
218
219
220
221 public PushDeviceTupleService getPdtService() {
222 return pdtService;
223 }
224
225
226
227
228 public void setPdtService(PushDeviceTupleService pdtService) {
229 this.pdtService = pdtService;
230 }
231
232
233 }