MessageUtil

import java.text.MessageFormat;
import java.util.ResourceBundle;

import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

public class MessageUtil extends BaseValidator {
    
    private MessageUtil() {
        
    }
    
    /**
     * Get message from language propriety file
     * @param bundle             Language propriety file key
     * @param key                Message key
     * @param defaultMessage     Default message
     * @param arguments          Message arguments
     */
    public static String getText(String bundle, String key, String defaultMessage, String[] arguments) {
        
        String message = null;
        
        try {
            ServletRequestAttributes servletRequestAttributes = 
                (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
            
            User user = 
                (User) servletRequestAttributes.getAttribute(
                        LogonConstants.SESSION_USER, 
                        RequestAttributes.SCOPE_SESSION);
            
            String local = null;
            if (user != null) {
                local = user.getLocale().getValue();
            } else {
                local = FieldValidationConstants.DEFAULT_LOCAL.getLanguage() + "_" 
                            + FieldValidationConstants.DEFAULT_LOCAL.getCountry().toLowerCase();
            }
            
            String fullPath = "language." + bundle + "." + bundle + "_" + local;
            ResourceBundle oBundle = ResourceBundle.getBundle(fullPath);
            message = oBundle.getString(key);
        } catch (Exception e) {
            message = defaultMessage;
            e.printStackTrace();
        }
        
        if (arguments != null) {
            message = MessageFormat.format(message, arguments);
        }
        
        return message;  
    }
    
    public static void addPageMessage(boolean success, String successMessage, String errorMessage) {
        if (success) {
            if (!Util.isEmpty(successMessage)) {
                MessageUtil.addPageSuccessMessage(successMessage);
            }
        } else {
            if (!Util.isEmpty(errorMessage)) {
                MessageUtil.addPageErrorMessage(errorMessage);
            }
        }
    }
    
    /**
     * Add page error message to request attribute 
     * @param errorMessage       Page error message
     */
    public static void addPageErrorMessage(String errorMessage) {
        
        if (!Util.isEmpty(errorMessage)) {
            ServletRequestAttributes servletRequestAttributes = 
                (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
            
            Messages messages = BaseValidator.getMessages(servletRequestAttributes);
            
            messages.addErrorMessage(errorMessage);
            
            servletRequestAttributes.setAttribute(FieldValidationConstants.MESSAGES, 
                    messages, RequestAttributes.SCOPE_REQUEST);
        }
    }
    
    /**
     * Add page error message to request attribute 
     * @param bundle             Language property file id
     * @param key                Message key
     * @param defaultMessage     Default message
     * @param arguments          The arguments of message
     */
    public static void addPageErrorMessage(String bundle, String key, String defaultMessage, String[] arguments) {
        
        String errorMessage = MessageUtil.getText(bundle, key, defaultMessage, arguments);
        
        MessageUtil.addPageErrorMessage(errorMessage);
    }
    
    
    /**
     * Add page success message to request attribute 
     * @param successMessage     Page success message
     */
    public static void addPageSuccessMessage(String successMessage) {
        
        if (!Util.isEmpty(successMessage)) {
            ServletRequestAttributes servletRequestAttributes = 
                (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
            
            Messages messages = BaseValidator.getMessages(servletRequestAttributes);
            
            messages.addSuccessMessage(successMessage);
            
            servletRequestAttributes.setAttribute(FieldValidationConstants.MESSAGES, 
                    messages, RequestAttributes.SCOPE_REQUEST);
        }
    }
    
    /**
     * Add page success message to request attribute 
     * @param bundle             Language property file id
     * @param key                Message key
     * @param defaultMessage     Default message
     * @param arguments          The arguments of message
     */
    public static void addPageSuccessMessage(String bundle, String key, String defaultMessage, String[] arguments) {
        
        String successMessage = MessageUtil.getText(bundle, key, defaultMessage, arguments);
        
        MessageUtil.addPageSuccessMessage(successMessage);
    }
    
}

 

你可能感兴趣的:(MessageUtil)