1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.kuali.mobility.push.dao;
16
17 import java.util.List;
18
19 import javax.persistence.EntityManager;
20 import javax.persistence.PersistenceContext;
21 import javax.persistence.Query;
22
23 import org.apache.log4j.Logger;
24 import org.kuali.mobility.push.entity.PushMessage;
25 import org.springframework.stereotype.Repository;
26 import org.springframework.transaction.annotation.Transactional;
27
28
29
30
31
32
33
34 @Repository
35 public class PushMessageDaoImpl implements PushMessageDao {
36
37
38
39 private static final Logger LOG = Logger.getLogger(PushMessageDaoImpl.class);
40
41
42 @PersistenceContext
43 private EntityManager entityManager;
44
45
46
47
48 public PushMessageDaoImpl(){}
49
50
51
52
53 @SuppressWarnings("unchecked")
54 public List<PushMessage> findAllPushMessagesByLanguage(String language) {
55 Query query = entityManager.createNamedQuery("PushMessage.findByLanguage");
56 query.setParameter("language", language);
57 return query.getResultList();
58 }
59
60
61
62
63 @Override
64 public PushMessage findPushMessageById(Long id) {
65 Query query = entityManager.createNamedQuery("PushMessage.findById");
66 query.setParameter("id", id);
67 PushMessage result;
68 try{
69 result = (PushMessage)query.getSingleResult();
70 }catch(Exception e){
71 result = null;
72 }
73 return result;
74 }
75
76
77
78
79 @Transactional
80 public void savePushMessage(PushMessage pushMessage) {
81 if(pushMessage == null){
82 return;
83 }
84 if(pushMessage.getMessageId() == null){
85 entityManager.persist(pushMessage);
86 LOG.info("Persist PushMessage");
87 }else{
88 entityManager.merge(pushMessage);
89 LOG.info("Merge PushMessage");
90 }
91 LOG.info(pushMessage);
92 }
93
94
95
96
97 @Transactional
98 public boolean removePushMessage(PushMessage pushMessage) {
99 boolean result = true;
100 if(pushMessage == null){
101 result = false;
102 } else if(pushMessage.getMessageId() == null) {
103 result = false;
104 } else {
105 try{
106 getEntityManager().remove(getEntityManager().contains(pushMessage)?pushMessage:getEntityManager().merge(pushMessage));
107 }catch(Exception e){
108 LOG.info("Exception while trying to remove push notification.", e);
109 result = false;
110 }
111 }
112 return result;
113 }
114
115
116
117
118 public EntityManager getEntityManager() {
119 return entityManager;
120 }
121
122
123
124
125 public void setEntityManager(EntityManager entityManager) {
126 this.entityManager = entityManager;
127 }
128
129 }