1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.sys;
17
18 import java.text.MessageFormat;
19 import java.util.List;
20
21 import org.apache.commons.lang.StringUtils;
22 import org.kuali.ole.sys.context.SpringContext;
23 import org.kuali.rice.core.api.config.property.ConfigurationService;
24 import org.kuali.rice.kns.service.DataDictionaryService;
25 import org.kuali.rice.krad.bo.BusinessObject;
26 import org.kuali.rice.krad.datadictionary.DataDictionary;
27
28
29
30
31 public class MessageBuilder {
32 private static ConfigurationService kualiConfigurationService = SpringContext.getBean(ConfigurationService.class);
33 private static DataDictionaryService dataDictionaryService = SpringContext.getBean(DataDictionaryService.class);
34 private static DataDictionary dataDictionary = dataDictionaryService.getDataDictionary();
35
36
37
38
39
40
41
42 public static void addMessageIntoList(List<Message> messageList, Message message) {
43 if (message != null) {
44 messageList.add(message);
45 }
46 }
47
48
49
50
51 public static Message buildMessage(String errorMessageKey, int errorType) {
52 return MessageBuilder.buildMessage(errorMessageKey, null, errorType);
53 }
54
55
56
57
58 public static Message buildMessage(String errorMessageKey, String invalidValue) {
59 return buildMessage(errorMessageKey, invalidValue, Message.TYPE_FATAL);
60 }
61
62
63
64
65 public static Message buildMessage(String errorMessageKey, String invalidValue, int errorType) {
66 String errorMessageBody = getPropertyValueAsString(errorMessageKey);
67 String errorMessage = formatMessageBody(errorMessageBody, invalidValue);
68 return new Message(errorMessage, errorType);
69 }
70
71
72
73
74 public static String formatMessageBody(String errorMessageBody, String invalidValue) {
75 String value = StringUtils.isBlank(invalidValue) ? "" : "[" + invalidValue + "]";
76 return errorMessageBody + value;
77 }
78
79
80
81
82 public static Message buildMessageWithPlaceHolder(String errorMessageKey, int errorType, Object... invalidValues) {
83 String errorMessageBody = getPropertyValueAsString(errorMessageKey);
84 String errorMessage = MessageFormat.format(errorMessageBody, invalidValues);
85 return new Message(errorMessage, errorType);
86 }
87
88
89
90
91 public static Message buildMessageWithPlaceHolder(String errorMessageKey, Object... invalidValues) {
92 return buildMessageWithPlaceHolder(errorMessageKey, Message.TYPE_FATAL, invalidValues);
93 }
94
95
96
97
98
99
100
101 public static String getPropertyValueAsString(String messageKey) {
102 return kualiConfigurationService.getPropertyValueAsString(messageKey);
103 }
104
105
106
107
108
109
110
111
112 public static String buildErrorMessageWithDataDictionary(Class<? extends BusinessObject> businessObjectClass, String attributeName, String currentValue) {
113 String label = getShortLabel(businessObjectClass, attributeName);
114
115 return label + ":" + currentValue;
116 }
117
118
119
120
121
122
123
124
125 public static String getShortLabel(Class<? extends BusinessObject> businessObjectClass, String attributeName) {
126 return dataDictionary.getBusinessObjectEntry(businessObjectClass.getName()).getAttributeDefinition(attributeName).getShortLabel();
127 }
128 }