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