1 | |
package org.kuali.student.admin.messages.service.impl; |
2 | |
|
3 | |
import com.google.common.cache.Cache; |
4 | |
import com.google.common.cache.CacheBuilder; |
5 | |
import org.kuali.rice.krad.service.BusinessObjectService; |
6 | |
import org.kuali.rice.krad.service.KRADServiceLocator; |
7 | |
import org.kuali.student.common.messages.dto.LocaleKeyList; |
8 | |
import org.kuali.student.common.messages.dto.Message; |
9 | |
import org.kuali.student.common.messages.dto.MessageGroupKeyList; |
10 | |
import org.kuali.student.common.messages.dto.MessageList; |
11 | |
import org.kuali.student.common.messages.service.MessageService; |
12 | |
import org.kuali.student.core.enumerationmanagement.bo.EnumeratedValue; |
13 | |
import org.kuali.student.core.messages.bo.MessageEntity; |
14 | |
import org.springframework.beans.factory.InitializingBean; |
15 | |
|
16 | |
import javax.jws.WebService; |
17 | |
import java.util.ArrayList; |
18 | |
import java.util.Collection; |
19 | |
import java.util.HashMap; |
20 | |
import java.util.Iterator; |
21 | |
import java.util.List; |
22 | |
import java.util.Map; |
23 | |
import java.util.concurrent.Callable; |
24 | |
import java.util.concurrent.ExecutionException; |
25 | |
import java.util.concurrent.TimeUnit; |
26 | |
|
27 | |
|
28 | |
@WebService(endpointInterface = "org.kuali.student.common.messages.service.MessageService", serviceName = "MessageService", portName = "MessageService", targetNamespace = "http://student.kuali.org/wsdl/messages") |
29 | |
|
30 | |
|
31 | 0 | public class MessageServiceImpl implements MessageService, InitializingBean { |
32 | |
|
33 | 0 | protected boolean cachingEnabled = false; |
34 | 0 | protected int msgsCacheMaxSize = 20; |
35 | 0 | protected int msgsCacheMaxAgeSeconds = 90; |
36 | |
protected Cache<String, MessageList> msgsCache; |
37 | |
|
38 | |
private BusinessObjectService businessObjectService; |
39 | |
|
40 | |
public Message addMessage(Message messageInfo) { |
41 | 0 | MessageEntity entity = toMessageEntity(messageInfo); |
42 | 0 | getBusinessObjectService().save(entity); |
43 | |
|
44 | 0 | return toMessage(entity); |
45 | |
} |
46 | |
|
47 | |
@SuppressWarnings("unchecked") |
48 | |
public LocaleKeyList getLocales() { |
49 | 0 | List<String> keyList = new ArrayList<String>(); |
50 | 0 | Map<String, Object> criteria = new HashMap<String,Object>(); |
51 | |
|
52 | 0 | criteria.put(EnumeratedValue.ENUMERATION_KEY, MessageEntity.LOCALE_ENUMERATION); |
53 | 0 | BusinessObjectService boService = KRADServiceLocator.getBusinessObjectService(); |
54 | 0 | Collection<EnumeratedValue> values = boService.findMatching(EnumeratedValue.class, criteria); |
55 | |
|
56 | 0 | Iterator<EnumeratedValue> iterator = values.iterator(); |
57 | 0 | while(iterator.hasNext()) { |
58 | 0 | EnumeratedValue value = iterator.next(); |
59 | 0 | keyList.add(value.getCode()); |
60 | 0 | } |
61 | |
|
62 | 0 | LocaleKeyList localeKeyList = new LocaleKeyList(); |
63 | 0 | localeKeyList.setLocales(keyList); |
64 | |
|
65 | 0 | return localeKeyList; |
66 | |
} |
67 | |
|
68 | |
public Message getMessage(String localeKey, String messageGroupKey, String messageKey) { |
69 | 0 | Map<String, Object> queryMap = new HashMap<String, Object>(); |
70 | 0 | queryMap.put("locale", localeKey); |
71 | 0 | queryMap.put("groupName", messageGroupKey); |
72 | 0 | queryMap.put("messageId", messageKey); |
73 | |
|
74 | 0 | MessageEntity entity = (MessageEntity) getBusinessObjectService().findByPrimaryKey(MessageEntity.class, queryMap); |
75 | |
|
76 | 0 | return toMessage(entity); |
77 | |
} |
78 | |
|
79 | |
@SuppressWarnings("unchecked") |
80 | |
public MessageGroupKeyList getMessageGroups() { |
81 | 0 | List<String> keyList = new ArrayList<String>(); |
82 | 0 | Map<String, Object> criteria = new HashMap<String,Object>(); |
83 | |
|
84 | 0 | criteria.put(EnumeratedValue.ENUMERATION_KEY, MessageEntity.GROUP_NAME_ENUMERATION); |
85 | 0 | BusinessObjectService boService = KRADServiceLocator.getBusinessObjectService(); |
86 | 0 | Collection<EnumeratedValue> values = boService.findMatching(EnumeratedValue.class, criteria); |
87 | |
|
88 | 0 | Iterator<EnumeratedValue> iterator = values.iterator(); |
89 | 0 | while(iterator.hasNext()) { |
90 | 0 | EnumeratedValue value = iterator.next(); |
91 | 0 | keyList.add(value.getCode()); |
92 | 0 | } |
93 | |
|
94 | 0 | MessageGroupKeyList messageGroupKeyList = new MessageGroupKeyList(); |
95 | 0 | messageGroupKeyList.setMessageGroupKeys(keyList); |
96 | |
|
97 | 0 | return messageGroupKeyList; |
98 | |
} |
99 | |
|
100 | |
@SuppressWarnings("unchecked") |
101 | |
public MessageList getMessages(final String localeKey, final String messageGroupKey) { |
102 | |
|
103 | 0 | if(cachingEnabled){ |
104 | |
try { |
105 | 0 | return msgsCache.get("localeKey="+localeKey+", messageGroupKey="+messageGroupKey, new Callable<MessageList>() { |
106 | |
@Override |
107 | |
public MessageList call() throws Exception { |
108 | 0 | return getMessagesHelper(localeKey, messageGroupKey); |
109 | |
} |
110 | |
}); |
111 | 0 | } catch (ExecutionException e) { |
112 | 0 | throw new RuntimeException(e); |
113 | |
} |
114 | |
} else { |
115 | 0 | return getMessagesHelper(localeKey, messageGroupKey); |
116 | |
} |
117 | |
} |
118 | |
|
119 | |
private MessageList getMessagesHelper(String localeKey, String messageGroupKey) { |
120 | 0 | Map<String, String> fieldValues = new HashMap<String, String>(); |
121 | 0 | fieldValues.put("locale", localeKey); |
122 | 0 | fieldValues.put("groupName", messageGroupKey); |
123 | |
|
124 | 0 | List<MessageEntity> messageEntityList = (List<MessageEntity>) getBusinessObjectService().findMatching(MessageEntity.class, fieldValues); |
125 | |
|
126 | 0 | List<Message> messages = new ArrayList<Message>(); |
127 | |
|
128 | 0 | for (MessageEntity messageEntity : messageEntityList) { |
129 | 0 | messages.add(toMessage(messageEntity)); |
130 | |
} |
131 | |
|
132 | 0 | MessageList messageList = new MessageList(); |
133 | 0 | messageList.setMessages(messages); |
134 | |
|
135 | 0 | return messageList; |
136 | |
} |
137 | |
|
138 | |
public MessageList getMessagesByGroups(String localeKey, MessageGroupKeyList messageGroupKeyList) { |
139 | |
|
140 | 0 | List<Message> messages = new ArrayList<Message>(); |
141 | |
|
142 | 0 | for(String messageGroupKey: messageGroupKeyList.getMessageGroupKeys()){ |
143 | 0 | messages.addAll(getMessages(localeKey, messageGroupKey).getMessages()); |
144 | |
} |
145 | |
|
146 | 0 | MessageList messageList = new MessageList(); |
147 | 0 | messageList.setMessages(messages); |
148 | |
|
149 | 0 | return messageList; |
150 | |
|
151 | |
} |
152 | |
|
153 | |
public Message updateMessage(String localeKey, String messageGroupKey, String messageKey, Message messageInfo) { |
154 | |
|
155 | 0 | Map<String,String> primaryKeys = new HashMap<String,String>(); |
156 | 0 | primaryKeys.put("locale", localeKey); |
157 | 0 | primaryKeys.put("groupName", messageGroupKey); |
158 | 0 | primaryKeys.put("messageId", messageKey); |
159 | |
|
160 | 0 | MessageEntity entity = (MessageEntity) getBusinessObjectService().findByPrimaryKey(MessageEntity.class, primaryKeys); |
161 | |
|
162 | 0 | entity.setMessageId(messageInfo.getId()); |
163 | 0 | entity.setLocale(messageInfo.getLocale()); |
164 | 0 | entity.setGroupName(messageInfo.getGroupName()); |
165 | 0 | entity.setValue(messageInfo.getValue()); |
166 | |
|
167 | 0 | getBusinessObjectService().save(entity); |
168 | |
|
169 | 0 | return toMessage(entity); |
170 | |
|
171 | |
} |
172 | |
|
173 | |
protected MessageEntity toMessageEntity(Message message) { |
174 | 0 | MessageEntity result = new MessageEntity(); |
175 | |
|
176 | 0 | result.setMessageId(message.getId()); |
177 | 0 | result.setLocale(message.getLocale()); |
178 | 0 | result.setGroupName(message.getGroupName()); |
179 | 0 | result.setValue(message.getValue()); |
180 | |
|
181 | 0 | return result; |
182 | |
} |
183 | |
|
184 | |
protected Message toMessage(MessageEntity entity) { |
185 | 0 | Message result = new Message(); |
186 | |
|
187 | 0 | result.setGroupName(entity.getGroupName()); |
188 | 0 | result.setId(entity.getMessageId()); |
189 | 0 | result.setLocale(entity.getLocale()); |
190 | 0 | result.setValue(entity.getValue()); |
191 | |
|
192 | 0 | return result; |
193 | |
} |
194 | |
|
195 | |
protected BusinessObjectService getBusinessObjectService() { |
196 | 0 | if (businessObjectService == null) { |
197 | 0 | businessObjectService = KRADServiceLocator.getBusinessObjectService(); |
198 | |
} |
199 | 0 | return businessObjectService; |
200 | |
} |
201 | |
|
202 | |
@Override |
203 | |
public void afterPropertiesSet() throws Exception { |
204 | 0 | if(cachingEnabled){ |
205 | 0 | msgsCache = CacheBuilder.newBuilder() |
206 | |
.expireAfterAccess(msgsCacheMaxAgeSeconds, TimeUnit.SECONDS) |
207 | |
.maximumSize(msgsCacheMaxSize) |
208 | |
.softValues() |
209 | |
.build(); |
210 | |
} |
211 | 0 | } |
212 | |
|
213 | |
public void setCachingEnabled(boolean cachingEnabled) { |
214 | 0 | this.cachingEnabled = cachingEnabled; |
215 | 0 | } |
216 | |
|
217 | |
} |