1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.student.core.messages.service.impl; |
17 | |
|
18 | |
import java.util.ArrayList; |
19 | |
import java.util.HashMap; |
20 | |
import java.util.HashSet; |
21 | |
import java.util.Iterator; |
22 | |
import java.util.List; |
23 | |
import java.util.Map; |
24 | |
import java.util.Set; |
25 | |
|
26 | |
import org.apache.log4j.Logger; |
27 | |
import org.kuali.student.common.messages.dto.LocaleKeyList; |
28 | |
import org.kuali.student.common.messages.dto.Message; |
29 | |
import org.kuali.student.common.messages.dto.MessageGroupKeyList; |
30 | |
import org.kuali.student.common.messages.dto.MessageList; |
31 | |
import org.kuali.student.common.messages.service.MessageService; |
32 | |
import org.kuali.student.core.messages.entity.MessageEntity; |
33 | |
import org.springframework.beans.factory.xml.XmlBeanFactory; |
34 | |
import org.springframework.core.io.ClassPathResource; |
35 | |
import org.springframework.core.io.FileSystemResource; |
36 | |
import org.springframework.core.io.Resource; |
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | 5 | public class MessageServiceMock implements MessageService { |
46 | |
|
47 | 5 | final Logger logger = Logger.getLogger(MessageServiceMock.class); |
48 | |
|
49 | |
List<String> messageFiles; |
50 | |
|
51 | 5 | Map<String, LocaleMessages> messages = new HashMap<String, LocaleMessages>(); |
52 | |
|
53 | 20 | private static class LocaleMessages { |
54 | 10 | Map<String, Map<String, String>> localeMessages = new HashMap<String, Map<String, String>>(); |
55 | |
|
56 | |
public Map<String, String> getMessages(String groupKey) { |
57 | 9 | return localeMessages.get(groupKey); |
58 | |
} |
59 | |
|
60 | |
public Set<String> getGroups() { |
61 | 2 | return localeMessages.keySet(); |
62 | |
} |
63 | |
|
64 | |
public void putMessage(String groupKey, String messageKey, |
65 | |
String message) { |
66 | |
Map<String, String> groupMessages; |
67 | |
|
68 | 30 | groupMessages = localeMessages.get(groupKey); |
69 | 30 | if (groupMessages == null) { |
70 | 20 | groupMessages = new HashMap<String, String>(); |
71 | |
} |
72 | 30 | groupMessages.put(messageKey, message); |
73 | 30 | this.localeMessages.put(groupKey, groupMessages); |
74 | 30 | } |
75 | |
} |
76 | |
|
77 | |
private void putMessage(String locale, String group, String key, |
78 | |
String value) { |
79 | |
|
80 | 30 | LocaleMessages localeMessages = messages.get(locale); |
81 | 30 | if (localeMessages == null) { |
82 | 10 | localeMessages = new LocaleMessages(); |
83 | |
} |
84 | 30 | localeMessages.putMessage(group, key, value); |
85 | 30 | this.messages.put(locale, localeMessages); |
86 | 30 | } |
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
@Override |
92 | |
public Message addMessage(Message messageInfo) { |
93 | |
|
94 | 0 | LocaleMessages localeMessages = this.messages.get(messageInfo |
95 | |
.getLocale()); |
96 | 0 | if (localeMessages == null) { |
97 | 0 | localeMessages = new LocaleMessages(); |
98 | |
} |
99 | 0 | localeMessages.putMessage(messageInfo.getGroupName(), messageInfo |
100 | |
.getId(), messageInfo.getValue()); |
101 | 0 | this.messages.put(messageInfo.getLocale(), localeMessages); |
102 | 0 | return messageInfo; |
103 | |
} |
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
@Override |
109 | |
public LocaleKeyList getLocales() { |
110 | |
|
111 | 1 | LocaleKeyList locales = new LocaleKeyList(); |
112 | |
|
113 | 1 | if (!this.messages.isEmpty()) { |
114 | 1 | locales.setLocales(new ArrayList<String>(this.messages.keySet())); |
115 | |
} |
116 | |
|
117 | 1 | return locales; |
118 | |
} |
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
@Override |
125 | |
public Message getMessage(String localeKey, String messageGroupKey, |
126 | |
String messageKey) { |
127 | |
|
128 | 2 | Message m = null; |
129 | 2 | m = new Message(); |
130 | 2 | m.setGroupName(messageGroupKey); |
131 | 2 | m.setLocale(localeKey); |
132 | 2 | m.setId(messageKey); |
133 | 2 | m.setValue(this.messages.get(localeKey).getMessages(messageGroupKey) |
134 | |
.get(messageKey)); |
135 | |
|
136 | 2 | return m; |
137 | |
} |
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
@Override |
143 | |
public MessageGroupKeyList getMessageGroups() { |
144 | |
|
145 | 1 | MessageGroupKeyList groupKeys = new MessageGroupKeyList(); |
146 | 1 | Iterator<String> i = messages.keySet().iterator(); |
147 | 1 | Set<String> groupKeySet = new HashSet<String>(); |
148 | 3 | while (i.hasNext()) { |
149 | 2 | String locale = i.next(); |
150 | 2 | groupKeySet.addAll(messages.get(locale).getGroups()); |
151 | 2 | } |
152 | 1 | groupKeys.setMessageGroupKeys(new ArrayList<String>(groupKeySet)); |
153 | |
|
154 | 1 | return groupKeys; |
155 | |
} |
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
@Override |
162 | |
public MessageList getMessages(String localeKey, String messageGroupKey) { |
163 | |
|
164 | 7 | MessageList messageList = new MessageList(); |
165 | |
|
166 | 7 | Map<String, String> groupMessages = ((LocaleMessages) messages |
167 | |
.get(localeKey)).getMessages(messageGroupKey); |
168 | 7 | List<Message> messageArrayList = new ArrayList<Message>(); |
169 | |
|
170 | 7 | Iterator<String> i = groupMessages.keySet().iterator(); |
171 | 18 | while (i.hasNext()) { |
172 | 11 | String id = i.next(); |
173 | 11 | Message m = new Message(); |
174 | 11 | m.setGroupName(messageGroupKey); |
175 | 11 | m.setId(id); |
176 | 11 | m.setValue(groupMessages.get(id)); |
177 | 11 | m.setLocale(localeKey); |
178 | 11 | messageArrayList.add(m); |
179 | 11 | } |
180 | 7 | messageList.setMessages(messageArrayList); |
181 | |
|
182 | 7 | return messageList; |
183 | |
} |
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
@Override |
190 | |
public MessageList getMessagesByGroups(String localeKey, |
191 | |
MessageGroupKeyList messageGroupKeyList) { |
192 | |
|
193 | 2 | MessageList groupMessageList = new MessageList(); |
194 | 2 | List<Message> messageArrayList = new ArrayList<Message>(); |
195 | 2 | for (String groupKey : messageGroupKeyList.getMessageGroupKeys()) { |
196 | 4 | MessageList messageList = getMessages(localeKey, groupKey); |
197 | 4 | messageArrayList.addAll(messageList.getMessages()); |
198 | 4 | } |
199 | 2 | groupMessageList.setMessages(messageArrayList); |
200 | |
|
201 | 2 | return groupMessageList; |
202 | |
} |
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
@Override |
210 | |
public Message updateMessage(String localeKey, String messageGroupKey, |
211 | |
String messageKey, Message messageInfo) { |
212 | 0 | if (getMessage(localeKey, messageGroupKey, messageKey) != null) { |
213 | 0 | putMessage(localeKey, messageGroupKey, messageKey, messageInfo |
214 | |
.getValue()); |
215 | |
} |
216 | 0 | return messageInfo; |
217 | |
} |
218 | |
|
219 | |
public List<String> getMessageFiles() { |
220 | 0 | return messageFiles; |
221 | |
} |
222 | |
|
223 | |
public void setMessageFiles(List<String> messageFiles) { |
224 | 5 | this.messageFiles = messageFiles; |
225 | |
|
226 | |
|
227 | 5 | logger.debug("Bootstrap messages started."); |
228 | 5 | if (this.messages.isEmpty()) { |
229 | 5 | for (String messageFileName : messageFiles) { |
230 | |
Resource res; |
231 | |
try { |
232 | 5 | if (messageFileName.startsWith("classpath:")) { |
233 | 5 | res = new ClassPathResource(messageFileName |
234 | |
.substring("classpath:".length())); |
235 | |
} else { |
236 | 0 | res = new FileSystemResource(messageFileName); |
237 | |
} |
238 | 0 | } catch (Exception e) { |
239 | 0 | throw new RuntimeException(e); |
240 | 5 | } |
241 | |
|
242 | 5 | XmlBeanFactory factory = new XmlBeanFactory(res); |
243 | |
|
244 | |
try { |
245 | 5 | Object[] beanArray = factory.getBeansOfType( |
246 | |
MessageEntity.class).values().toArray(); |
247 | 35 | for (Object o : beanArray) { |
248 | 30 | MessageEntity m = (MessageEntity) o; |
249 | 30 | putMessage(m.getLocale(), m.getGroupName(), m.getMessageId(), |
250 | |
m.getValue()); |
251 | |
} |
252 | 0 | } catch (Exception e) { |
253 | 0 | logger.debug(e); |
254 | 5 | } |
255 | 5 | } |
256 | |
} |
257 | |
|
258 | 5 | logger.debug("Bootstrap messages finished."); |
259 | 5 | } |
260 | |
|
261 | |
} |