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.util.List;
19
20 import javax.persistence.EntityManager;
21 import javax.persistence.PersistenceContext;
22 import javax.persistence.Query;
23
24 import org.kuali.mobility.push.entity.Preference;
25 import org.kuali.mobility.push.entity.Sender;
26 import org.springframework.stereotype.Repository;
27
28
29
30
31
32
33
34 @Repository
35 public class PreferenceDaoImpl implements PreferenceDao{
36
37
38 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(PreferenceDaoImpl.class);
39
40
41 @PersistenceContext
42 private EntityManager entityManager;
43
44
45
46
47 public PreferenceDaoImpl(){}
48
49
50
51
52
53 public List<Preference> findPreferencesByUsername(String username){
54 Query query = entityManager.createQuery("select p from Preference p where p.username = :username");
55 query.setParameter("username", username);
56
57 return query.getResultList();
58 }
59
60
61
62
63
64 public Preference findPreference(String username, String shortName){
65
66
67
68
69
70
71
72
73
74
75
76
77 Query query = getEntityManager().createNamedQuery("Preference.findPreferenceWithUsernameAndShortname");
78
79 query.setParameter("username", username);
80 query.setParameter("shortName", shortName);
81
82 Preference result;
83 try{
84 result = (Preference) query.getSingleResult();
85 }catch(Exception e){
86 LOG.info("No Preference Found: " + username + ", " + shortName);
87 result = null;
88 }
89 return result;
90 }
91
92
93
94
95
96 public Preference findPreference(String username, Sender sender){
97
98 Query query = entityManager.createQuery("select p from Preference p where p.username = :username and p.pushSenderID = :senderId");
99 query.setParameter("username", username);
100 query.setParameter("senderId", sender.getId());
101 LOG.info(username + " & " + sender.getId());
102 Preference result;
103 try{
104 result = (Preference) query.getSingleResult();
105 }catch(Exception e){
106 LOG.info("No Preference Found: " + username + ", " + sender.getShortName());
107 result = null;
108 }
109 return result;
110 }
111
112
113
114
115
116 public Preference findPreference(long id){
117 Query query = entityManager.createQuery("select p from Preference p where p.id = :id");
118 query.setParameter("id", id);
119
120 Preference result;
121 try{
122 result = (Preference) query.getSingleResult();
123 }catch(Exception e){
124 LOG.info("Exception: " + e.getMessage());
125 result = null;
126 }
127 return result;
128
129 }
130
131
132
133
134
135 public void setPreference(Preference preference, boolean enabled ){
136 if(preference == null){
137 return;
138 }
139 if(preference.getId() == null){
140 LOG.info("persist");
141 entityManager.persist(preference);
142 }else{
143 LOG.info("merge");
144 preference.setEnabled(enabled);
145 entityManager.merge(preference);
146 }
147 }
148
149
150
151
152
153 public void savePreference(Preference preference){
154 if(preference == null){
155 return;
156 }
157 if(preference.getId() == null){
158 LOG.info("persist");
159 entityManager.persist(preference);
160 }else{
161 LOG.info("merge");
162 entityManager.merge(preference);
163 }
164 }
165
166
167
168
169
170 @SuppressWarnings("unchecked")
171 public boolean removePreference(Preference preference){
172 LOG.info("PreferenceDaoImpl.removePreference");
173 boolean result = true;
174 if(preference == null){
175 LOG.info("PreferenceDaoImpl.removePreference null");
176 return false;
177 }
178 if(preference.getId() != null){
179 LOG.info("PreferenceDaoImpl.removePreference ! null");
180 try{
181 Preference p = entityManager.find(Preference.class, preference.getId());
182 entityManager.remove(p);
183 LOG.info("PreferenceDaoImpl.removePreference : remove(p)");
184 }catch(Exception e){
185 LOG.info("Exception Caught: " + e.getMessage());
186 result = false;
187 }
188 }else{
189 result = false;
190 }
191 LOG.info("PreferenceDaoImpl.removePreference " + (result ? "true":"false") );
192 return result;
193 }
194
195 /**
196 * Returns the reference to the <code>EntityManager</code>
197 * @return The reference to the <code>EntityManager</code>
198 */
199 public EntityManager getEntityManager() {
200 return entityManager;
201 }
202
203
204
205
206
207 public void setEntityManager(EntityManager entityManager) {
208 this.entityManager = entityManager;
209 }
210 }