001 /* 002 * Copyright 2011 The Kuali Foundation. 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.kuali.kfs.sys; 017 018 import java.text.MessageFormat; 019 import java.util.List; 020 021 import org.apache.commons.lang.StringUtils; 022 import org.kuali.kfs.sys.context.SpringContext; 023 import org.kuali.rice.kns.bo.BusinessObject; 024 import org.kuali.rice.kns.datadictionary.DataDictionary; 025 import org.kuali.rice.kns.service.DataDictionaryService; 026 import org.kuali.rice.kns.service.KualiConfigurationService; 027 028 /** 029 * This class provides a set of utilities that can be used to build error message 030 */ 031 public class MessageBuilder { 032 private static KualiConfigurationService kualiConfigurationService = SpringContext.getBean(KualiConfigurationService.class); 033 private static DataDictionaryService dataDictionaryService = SpringContext.getBean(DataDictionaryService.class); 034 private static DataDictionary dataDictionary = dataDictionaryService.getDataDictionary(); 035 036 /** 037 * add the given message into the given message list 038 * 039 * @param messageList the given message list 040 * @param message the given message 041 */ 042 public static void addMessageIntoList(List<Message> messageList, Message message) { 043 if (message != null) { 044 messageList.add(message); 045 } 046 } 047 048 /** 049 * Build the error message with the message body and error type 050 */ 051 public static Message buildMessage(String errorMessageKey, int errorType) { 052 return MessageBuilder.buildMessage(errorMessageKey, null, errorType); 053 } 054 055 /** 056 * Build the message for a fatal error with the message body and invalid value 057 */ 058 public static Message buildMessage(String errorMessageKey, String invalidValue) { 059 return buildMessage(errorMessageKey, invalidValue, Message.TYPE_FATAL); 060 } 061 062 /** 063 * Build the error message with the message body, invalid value and error type 064 */ 065 public static Message buildMessage(String errorMessageKey, String invalidValue, int errorType) { 066 String errorMessageBody = getPropertyString(errorMessageKey); 067 String errorMessage = formatMessageBody(errorMessageBody, invalidValue); 068 return new Message(errorMessage, errorType); 069 } 070 071 /** 072 * Format the error message body based on the given error message and invalid value 073 */ 074 public static String formatMessageBody(String errorMessageBody, String invalidValue) { 075 String value = StringUtils.isBlank(invalidValue) ? "" : "[" + invalidValue + "]"; 076 return errorMessageBody + value; 077 } 078 079 /** 080 * Build the error message with the message body, invalid value and error type. The message body contains place holders. 081 */ 082 public static Message buildMessageWithPlaceHolder(String errorMessageKey, int errorType, Object... invalidValues) { 083 String errorMessageBody = getPropertyString(errorMessageKey); 084 String errorMessage = MessageFormat.format(errorMessageBody, invalidValues); 085 return new Message(errorMessage, errorType); 086 } 087 088 /** 089 * Build the message for a fatal error with the message body and invalid value. The message body contains place holders. 090 */ 091 public static Message buildMessageWithPlaceHolder(String errorMessageKey, Object... invalidValues) { 092 return buildMessageWithPlaceHolder(errorMessageKey, Message.TYPE_FATAL, invalidValues); 093 } 094 095 /** 096 * get the message from application resource properties with the given key 097 * 098 * @param messageKey the given message key 099 * @return the message from application resource properties with the given key 100 */ 101 public static String getPropertyString(String messageKey) { 102 return kualiConfigurationService.getPropertyString(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 }