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 | 0 | public class NotificationDaoImpl implements NotificationDao { |
32 | |
|
33 | |
@PersistenceContext |
34 | |
private EntityManager entityManager; |
35 | |
|
36 | |
@Override |
37 | |
public Notification findNotificationById(Long id) { |
38 | 0 | Query query = entityManager.createQuery("select n from Notification n where n.notificationId = :id"); |
39 | 0 | query.setParameter("id", id); |
40 | |
try { |
41 | 0 | return (Notification) query.getSingleResult(); |
42 | 0 | } catch (Exception e) { |
43 | 0 | return null; |
44 | |
} |
45 | |
} |
46 | |
|
47 | |
@SuppressWarnings("unchecked") |
48 | |
@Override |
49 | |
public List<Notification> findAllNotifications() { |
50 | 0 | Query query = entityManager.createQuery("select n from Notification n"); |
51 | |
try { |
52 | 0 | return (List<Notification>) query.getResultList(); |
53 | 0 | } catch (Exception e) { |
54 | 0 | return null; |
55 | |
} |
56 | |
} |
57 | |
|
58 | |
@SuppressWarnings("unchecked") |
59 | |
@Override |
60 | |
public List<Notification> findAllValidNotifications(Date date) { |
61 | 0 | 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 | 0 | query.setParameter("date", date); |
63 | |
try { |
64 | 0 | return (List<Notification>) query.getResultList(); |
65 | 0 | } catch (Exception e) { |
66 | 0 | return null; |
67 | |
} |
68 | |
} |
69 | |
|
70 | |
@Override |
71 | |
public Long saveNotification(Notification notification) { |
72 | 0 | if (notification == null) { |
73 | 0 | return null; |
74 | |
} |
75 | |
try { |
76 | 0 | if (notification.getNotificationId() == null) { |
77 | 0 | entityManager.persist(notification); |
78 | |
} else { |
79 | 0 | entityManager.merge(notification); |
80 | |
} |
81 | 0 | } catch (OptimisticLockException oe) { |
82 | 0 | return null; |
83 | 0 | } |
84 | 0 | return notification.getNotificationId(); |
85 | |
} |
86 | |
|
87 | |
@Override |
88 | |
public void deleteNotificationById(Long id) { |
89 | 0 | Query query = entityManager.createQuery("delete from Notification n where n.notificationId = :id"); |
90 | 0 | query.setParameter("id", id); |
91 | 0 | query.executeUpdate(); |
92 | 0 | } |
93 | |
|
94 | |
@Override |
95 | |
public UserNotification findUserNotificationById(Long id) { |
96 | 0 | Query query = entityManager.createQuery("select un from UserNotification un where un.userNotificationId = :id"); |
97 | 0 | query.setParameter("id", id); |
98 | |
try { |
99 | 0 | return (UserNotification) query.getSingleResult(); |
100 | 0 | } catch (Exception e) { |
101 | 0 | return null; |
102 | |
} |
103 | |
} |
104 | |
|
105 | |
@Override |
106 | |
public UserNotification findUserNotificationByNotificationId(Long id) { |
107 | 0 | Query query = entityManager.createQuery("select un from UserNotification un where un.notificationId = :id"); |
108 | 0 | query.setParameter("id", id); |
109 | |
try { |
110 | 0 | return (UserNotification) query.getSingleResult(); |
111 | 0 | } catch (Exception e) { |
112 | 0 | return null; |
113 | |
} |
114 | |
} |
115 | |
|
116 | |
|
117 | |
@Override |
118 | |
public Long saveUserNotification(UserNotification userNotification) { |
119 | 0 | if (userNotification == null) { |
120 | 0 | return null; |
121 | |
} |
122 | |
try { |
123 | 0 | if (userNotification.getUserNotificationId() == null) { |
124 | 0 | entityManager.persist(userNotification); |
125 | |
} else { |
126 | 0 | entityManager.merge(userNotification); |
127 | |
} |
128 | 0 | } catch (OptimisticLockException oe) { |
129 | 0 | return null; |
130 | 0 | } |
131 | 0 | return userNotification.getUserNotificationId(); |
132 | |
} |
133 | |
|
134 | |
@Override |
135 | |
public void deleteUserNotificationById(Long id) { |
136 | 0 | Query query = entityManager.createQuery("delete from UserNotification un where un.userNotificationId = :id"); |
137 | 0 | query.setParameter("id", id); |
138 | 0 | query.executeUpdate(); |
139 | 0 | } |
140 | |
|
141 | |
@SuppressWarnings("unchecked") |
142 | |
@Override |
143 | |
public List<UserNotification> findAllUserNotificationsByDeviceId(String id) { |
144 | 0 | Query query = entityManager.createQuery("select un from UserNotification un where deviceId = :id"); |
145 | 0 | query.setParameter("id", id); |
146 | |
try { |
147 | 0 | return (List<UserNotification>) query.getResultList(); |
148 | 0 | } catch (Exception e) { |
149 | 0 | return null; |
150 | |
} |
151 | |
} |
152 | |
|
153 | |
@SuppressWarnings("unchecked") |
154 | |
@Override |
155 | |
public List<UserNotification> findAllUserNotificationsByPersonId(Long id) { |
156 | 0 | Query query = entityManager.createQuery("select un from UserNotification un where personId = :id"); |
157 | 0 | query.setParameter("id", id); |
158 | |
try { |
159 | 0 | return (List<UserNotification>) query.getResultList(); |
160 | 0 | } catch (Exception e) { |
161 | 0 | return null; |
162 | |
} |
163 | |
} |
164 | |
|
165 | |
} |