1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.messages;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.kuali.rice.coreservice.framework.CoreFrameworkServiceLocator;
20 import org.kuali.rice.krad.util.KRADConstants;
21
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.List;
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 public class MessageServiceImpl implements MessageService {
46 private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(MessageServiceImpl.class);
47
48 private List<MessageProvider> messageProviders;
49
50
51
52
53 public Message getMessage(String namespace, String component, String key) {
54 return getMessage(namespace, component, key, getDefaultLocaleCode());
55 }
56
57
58
59
60 public Message getMessage(String namespace, String component, String key, String locale) {
61 Message message = null;
62
63
64 if (StringUtils.isBlank(namespace)) {
65 namespace = DEFAULT_NAMESPACE_CODE;
66 }
67
68 if (StringUtils.isBlank(component)) {
69 component = DEFAULT_COMPONENT_CODE;
70 }
71
72 if (StringUtils.isBlank(locale)) {
73 locale = getDefaultLocaleCode();
74 }
75
76 for (MessageProvider provider : messageProviders) {
77 message = provider.getMessage(namespace, component, key, locale);
78
79 if (message != null) {
80
81 break;
82 }
83 }
84
85 return message;
86 }
87
88
89
90
91 public String getMessageText(String namespace, String component, String key) {
92 return getMessageText(namespace, component, key, getDefaultLocaleCode());
93 }
94
95
96
97
98 public String getMessageText(String namespace, String component, String key, String locale) {
99 Message message = getMessage(namespace, component, key, locale);
100 if (message != null) {
101 return message.getText();
102 }
103
104 return null;
105 }
106
107
108
109
110 public String getMessageText(String key) {
111 return getMessageText(key, getDefaultLocaleCode());
112 }
113
114
115
116
117 public String getMessageText(String key, String locale) {
118 Message message = getMessage(null, null, key, locale);
119 if (message != null) {
120 return message.getText();
121 }
122
123 return null;
124 }
125
126
127
128
129 public Collection<Message> getAllMessagesForComponent(String namespace, String component) {
130 return getAllMessagesForComponent(namespace, component, getDefaultLocaleCode());
131 }
132
133
134
135
136 public Collection<Message> getAllMessagesForComponent(String namespace, String component, String locale) {
137 Collection<Message> messages = new ArrayList<Message>();
138
139 if (StringUtils.isBlank(locale)) {
140 locale = getDefaultLocaleCode();
141 }
142
143 for (MessageProvider provider : messageProviders) {
144 Collection<Message> providerMessages = provider.getAllMessagesForComponent(namespace, component, locale);
145 mergeMessages(messages, providerMessages);
146 }
147
148 return messages;
149 }
150
151
152
153
154
155
156
157
158
159
160
161
162 protected void mergeMessages(Collection<Message> messages, Collection<Message> messagesToMerge) {
163 for (Message message : messagesToMerge) {
164 if (!messages.contains(message)) {
165 messages.add(message);
166 }
167 }
168 }
169
170
171
172
173
174
175 protected String getDefaultLocaleCode() {
176 return CoreFrameworkServiceLocator.getParameterService().getParameterValueAsString(KRADConstants.KNS_NAMESPACE,
177 KRADConstants.DetailTypes.ALL_DETAIL_TYPE, KRADConstants.ParameterNames.DEFAULT_LOCALE_CODE);
178 }
179
180
181
182
183
184
185 protected List<MessageProvider> getMessageProviders() {
186 return messageProviders;
187 }
188
189
190
191
192
193
194
195 public void setMessageProviders(List<MessageProvider> messageProviders) {
196 this.messageProviders = messageProviders;
197 }
198 }