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