1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.mobility.notification.dao;
17
18 import java.util.Date;
19 import java.util.List;
20
21 import javax.persistence.EntityManager;
22 import javax.persistence.OptimisticLockException;
23 import javax.persistence.PersistenceContext;
24 import javax.persistence.Query;
25
26 import org.kuali.mobility.notification.entity.Notification;
27 import org.kuali.mobility.notification.entity.UserNotification;
28 import org.springframework.stereotype.Repository;
29
30 @Repository
31 public class NotificationDaoImpl implements NotificationDao {
32
33 @PersistenceContext
34 private EntityManager entityManager;
35
36 @Override
37 public Notification findNotificationById(Long id) {
38 Query query = entityManager.createQuery("select n from Notification n where n.notificationId = :id");
39 query.setParameter("id", id);
40 try {
41 return (Notification) query.getSingleResult();
42 } catch (Exception e) {
43 return null;
44 }
45 }
46
47 @SuppressWarnings("unchecked")
48 @Override
49 public List<Notification> findAllNotifications() {
50 Query query = entityManager.createQuery("select n from Notification n");
51 try {
52 return (List<Notification>) query.getResultList();
53 } catch (Exception e) {
54 return null;
55 }
56 }
57
58 @SuppressWarnings("unchecked")
59 @Override
60 public List<Notification> findAllValidNotifications(Date date) {
61 Query query = entityManager.createQuery("select n from Notification n where (n.startDate is null and n.endDate is null) or (n.startDate is null and :date < n.endDate) or (:date > n.startDate and n.endDate is null) or (:date between n.startDate and n.endDate)");
62 query.setParameter("date", date);
63 try {
64 return (List<Notification>) query.getResultList();
65 } catch (Exception e) {
66 return null;
67 }
68 }
69
70 @Override
71 public Long saveNotification(Notification notification) {
72 if (notification == null) {
73 return null;
74 }
75 try {
76 if (notification.getNotificationId() == null) {
77 entityManager.persist(notification);
78 } else {
79 entityManager.merge(notification);
80 }
81 } catch (OptimisticLockException oe) {
82 return null;
83 }
84 return notification.getNotificationId();
85 }
86
87 @Override
88 public void deleteNotificationById(Long id) {
89 Query query = entityManager.createQuery("delete from Notification n where n.notificationId = :id");
90 query.setParameter("id", id);
91 query.executeUpdate();
92 }
93
94 @Override
95 public UserNotification findUserNotificationById(Long id) {
96 Query query = entityManager.createQuery("select un from UserNotification un where un.userNotificationId = :id");
97 query.setParameter("id", id);
98 try {
99 return (UserNotification) query.getSingleResult();
100 } catch (Exception e) {
101 return null;
102 }
103 }
104
105 @Override
106 public UserNotification findUserNotificationByNotificationId(Long id) {
107 Query query = entityManager.createQuery("select un from UserNotification un where un.notificationId = :id");
108 query.setParameter("id", id);
109 try {
110 return (UserNotification) query.getSingleResult();
111 } catch (Exception e) {
112 return null;
113 }
114 }
115
116
117 @Override
118 public Long saveUserNotification(UserNotification userNotification) {
119 if (userNotification == null) {
120 return null;
121 }
122 try {
123 if (userNotification.getUserNotificationId() == null) {
124 entityManager.persist(userNotification);
125 } else {
126 entityManager.merge(userNotification);
127 }
128 } catch (OptimisticLockException oe) {
129 return null;
130 }
131 return userNotification.getUserNotificationId();
132 }
133
134 @Override
135 public void deleteUserNotificationById(Long id) {
136 Query query = entityManager.createQuery("delete from UserNotification un where un.userNotificationId = :id");
137 query.setParameter("id", id);
138 query.executeUpdate();
139 }
140
141 @SuppressWarnings("unchecked")
142 @Override
143 public List<UserNotification> findAllUserNotificationsByDeviceId(String id) {
144 Query query = entityManager.createQuery("select un from UserNotification un where deviceId = :id");
145 query.setParameter("id", id);
146 try {
147 return (List<UserNotification>) query.getResultList();
148 } catch (Exception e) {
149 return null;
150 }
151 }
152
153 @SuppressWarnings("unchecked")
154 @Override
155 public List<UserNotification> findAllUserNotificationsByPersonId(Long id) {
156 Query query = entityManager.createQuery("select un from UserNotification un where personId = :id");
157 query.setParameter("id", id);
158 try {
159 return (List<UserNotification>) query.getResultList();
160 } catch (Exception e) {
161 return null;
162 }
163 }
164
165 }