1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.student.core.messages.dao.impl; |
17 | |
|
18 | |
import java.util.ArrayList; |
19 | |
import java.util.List; |
20 | |
|
21 | |
import javax.persistence.EntityManager; |
22 | |
import javax.persistence.PersistenceContext; |
23 | |
import javax.persistence.Query; |
24 | |
|
25 | |
import org.kuali.student.common.messages.MessageException; |
26 | |
import org.kuali.student.core.messages.dao.MessageManagementDAO; |
27 | |
import org.kuali.student.core.messages.entity.MessageEntity; |
28 | |
import org.slf4j.Logger; |
29 | |
import org.slf4j.LoggerFactory; |
30 | |
|
31 | |
|
32 | 0 | public class MessageManagementDAOImpl implements MessageManagementDAO { |
33 | |
private EntityManager entityManager; |
34 | |
|
35 | 0 | final static Logger logger = LoggerFactory.getLogger(MessageManagementDAOImpl.class); |
36 | |
|
37 | |
@PersistenceContext(unitName = "MessageManagement") |
38 | |
public void setEntityManager(EntityManager entityManager) { |
39 | 0 | this.entityManager = entityManager; |
40 | 0 | } |
41 | |
|
42 | |
public EntityManager getEntityManager(){ |
43 | 0 | return this.entityManager; |
44 | |
} |
45 | |
|
46 | |
public MessageEntity addMessage(MessageEntity me){ |
47 | 0 | MessageEntity result = null; |
48 | |
try{ |
49 | 0 | entityManager.persist(me); |
50 | 0 | result = this.getMessage(me.getLocale(), me.getGroupName(), me.getId()); |
51 | |
} |
52 | 0 | catch(Exception e){ |
53 | 0 | throw new MessageException("adding message data query failed.", e); |
54 | 0 | } |
55 | 0 | return result; |
56 | |
} |
57 | |
|
58 | |
public int getTotalMessages(){ |
59 | 0 | int count = 0; |
60 | |
try{ |
61 | 0 | Query query = entityManager.createQuery("SELECT COUNT(m) FROM MessageEntity m"); |
62 | 0 | count = ((Long)query.getSingleResult()).intValue(); |
63 | |
} |
64 | 0 | catch(Exception e){ |
65 | 0 | logger.error("count query failed.", e); |
66 | 0 | throw new MessageException("count query failed.", e); |
67 | 0 | } |
68 | 0 | return count; |
69 | |
} |
70 | |
|
71 | |
@SuppressWarnings("unchecked") |
72 | |
public List<String> getLocales() { |
73 | |
List<String> locales; |
74 | |
try{ |
75 | 0 | Query query = entityManager.createQuery("SELECT distinct m.locale FROM MessageEntity m"); |
76 | 0 | locales = (List<String>) query.getResultList(); |
77 | |
} |
78 | 0 | catch(Exception e){ |
79 | 0 | logger.error("getLocales query failed.", e); |
80 | 0 | throw new MessageException("getLocales query failed.", e); |
81 | 0 | } |
82 | 0 | return locales; |
83 | |
} |
84 | |
|
85 | |
@SuppressWarnings("unchecked") |
86 | |
public List<String> getMessageGroups() { |
87 | |
List<String> groups; |
88 | |
try{ |
89 | 0 | Query query = entityManager.createQuery("SELECT distinct m.groupName FROM MessageEntity m"); |
90 | 0 | groups = (List<String>) query.getResultList(); |
91 | |
} |
92 | 0 | catch(Exception e){ |
93 | 0 | logger.error("getMessageGroups query failed.", e); |
94 | 0 | throw new MessageException("getMessageGroups query failed.", e); |
95 | 0 | } |
96 | 0 | return groups; |
97 | |
} |
98 | |
|
99 | |
public MessageEntity getMessage(String locale, String groupKey, String messageKey) { |
100 | |
|
101 | 0 | MessageEntity entity = null; |
102 | |
try{ |
103 | 0 | Query query = entityManager.createQuery( |
104 | |
"select m from MessageEntity m " + |
105 | |
"where m.locale = :locale and " + |
106 | |
"m.groupName = :groupKey and " + |
107 | |
"m.messageId = :messageKey"); |
108 | 0 | query.setParameter("locale", locale); |
109 | 0 | query.setParameter("groupKey", groupKey); |
110 | 0 | query.setParameter("messageKey", messageKey); |
111 | |
|
112 | 0 | if(!query.getResultList().isEmpty()){ |
113 | 0 | entity = (MessageEntity)query.getResultList().get(0); |
114 | |
}else{ |
115 | 0 | return null; |
116 | |
} |
117 | |
} |
118 | 0 | catch(Exception e){ |
119 | 0 | logger.error("getMessage query failed.", e); |
120 | 0 | throw new MessageException("getMessage query failed.", e); |
121 | 0 | } |
122 | 0 | return entity; |
123 | |
} |
124 | |
|
125 | |
|
126 | |
@SuppressWarnings("unchecked") |
127 | |
public List<MessageEntity> getMessages(String locale, String groupKey) { |
128 | |
List<MessageEntity> list; |
129 | |
try{ |
130 | 0 | Query query = entityManager.createQuery( |
131 | |
"select m from MessageEntity m " + |
132 | |
"where m.locale = :locale and " + |
133 | |
"m.groupName = :groupKey "); |
134 | 0 | query.setParameter("locale", locale); |
135 | 0 | query.setParameter("groupKey", groupKey); |
136 | 0 | list = (List<MessageEntity>)query.getResultList(); |
137 | |
} |
138 | 0 | catch(Exception e){ |
139 | 0 | logger.error("getMessages query failed.", e); |
140 | 0 | throw new MessageException("getMessages query failed.", e); |
141 | 0 | } |
142 | 0 | return list; |
143 | |
} |
144 | |
|
145 | |
@SuppressWarnings("unchecked") |
146 | |
public List<MessageEntity> getMessagesByGroups(String locale, List<String> groupKeys) { |
147 | 0 | List<MessageEntity> list = new ArrayList<MessageEntity>(); |
148 | |
try{ |
149 | 0 | for(String currentKey: groupKeys){ |
150 | 0 | Query query = entityManager.createQuery( |
151 | |
"select m from MessageEntity m " + |
152 | |
"where m.locale = :locale and " + |
153 | |
"m.groupName = :groupKey "); |
154 | 0 | query.setParameter("locale", locale); |
155 | 0 | query.setParameter("groupKey", currentKey); |
156 | 0 | list.addAll((List<MessageEntity>)query.getResultList()); |
157 | 0 | } |
158 | |
} |
159 | 0 | catch(Exception e){ |
160 | 0 | logger.error("getMessagesByGroups query failed.", e); |
161 | 0 | throw new MessageException("getMessagesByGroups query failed.", e); |
162 | 0 | } |
163 | 0 | return list; |
164 | |
} |
165 | |
|
166 | |
|
167 | |
public MessageEntity updateMessage(String locale, String groupKey, String messageKey, MessageEntity updatedMessage) { |
168 | 0 | MessageEntity returnValue = null; |
169 | |
try{ |
170 | 0 | MessageEntity theEntity = this.getMessage(locale, groupKey, messageKey); |
171 | 0 | if(theEntity != null){ |
172 | 0 | theEntity.setValue(updatedMessage.getValue()); |
173 | 0 | theEntity.setMessageId(updatedMessage.getMessageId()); |
174 | 0 | theEntity.setLocale(updatedMessage.getLocale()); |
175 | 0 | theEntity.setGroupName(updatedMessage.getGroupName()); |
176 | 0 | entityManager.merge(theEntity); |
177 | 0 | returnValue = theEntity; |
178 | |
} |
179 | |
} |
180 | 0 | catch(Exception e){ |
181 | 0 | throw new MessageException("updateMessage query failed.", e); |
182 | 0 | } |
183 | 0 | return returnValue; |
184 | |
} |
185 | |
|
186 | |
|
187 | |
} |