View Javadoc
1   /*
2    * Copyright 2007-2008 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * This class provides a set of utilities that can be used to build error message
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       * add the given message into the given message list
38       * 
39       * @param messageList the given message list
40       * @param message the given message
41       */
42      public static void addMessageIntoList(List<Message> messageList, Message message) {
43          if (message != null) {
44              messageList.add(message);
45          }
46      }
47  
48      /**
49       * Build the error message with the message body and error type
50       */
51      public static Message buildMessage(String errorMessageKey, int errorType) {
52          return MessageBuilder.buildMessage(errorMessageKey, null, errorType);
53      }
54  
55      /**
56       * Build the message for a fatal error with the message body and invalid value
57       */
58      public static Message buildMessage(String errorMessageKey, String invalidValue) {
59          return buildMessage(errorMessageKey, invalidValue, Message.TYPE_FATAL);
60      }
61  
62      /**
63       * Build the error message with the message body, invalid value and error type
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       * Format the error message body based on the given error message and invalid value
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       * Build the error message with the message body, invalid value and error type. The message body contains place holders.
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       * Build the message for a fatal error with the message body and invalid value. The message body contains place holders.
90       */
91      public static Message buildMessageWithPlaceHolder(String errorMessageKey, Object... invalidValues) {
92          return buildMessageWithPlaceHolder(errorMessageKey, Message.TYPE_FATAL, invalidValues);
93      }
94  
95      /**
96       * get the message from application resource properties with the given key
97       * 
98       * @param messageKey the given message key
99       * @return the message from application resource properties with the given key
100      */
101     public static String getPropertyValueAsString(String messageKey) {
102         return kualiConfigurationService.getPropertyValueAsString(messageKey);
103     }
104 
105     /**
106      * build the error message with the given label and current value
107      * 
108      * @param label the given label
109      * @param currentValue the given current value
110      * @return the error message built from the given label and current value
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      * get the label of the specified attribute of the given business object
120      * 
121      * @param businessObjectClass the given business object
122      * @param attributeName the specified attribute name
123      * @return the label of the specified attribute of the given business object
124      */
125     public static String getShortLabel(Class<? extends BusinessObject> businessObjectClass, String attributeName) {
126         return dataDictionary.getBusinessObjectEntry(businessObjectClass.getName()).getAttributeDefinition(attributeName).getShortLabel();
127     }
128 }