001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.util.bridges.jsf.common;
016    
017    import com.liferay.portal.kernel.language.LanguageUtil;
018    
019    import java.util.Locale;
020    
021    import javax.faces.application.FacesMessage.Severity;
022    import javax.faces.application.FacesMessage;
023    import javax.faces.context.FacesContext;
024    
025    /**
026     * <p>
027     * This class provides static convenience methods for creating FacesMessage
028     * objects from locale-specific values in the Liferay portal.properties file,
029     * and adding them to the FacesContext either globally, or to individual
030     * components.
031     * </p>
032     *
033     * @author Neil Griffin
034     */
035    public class FacesMessageUtil {
036    
037            public static void error(FacesContext facesContext, String key) {
038                    error(null, facesContext, key);
039            }
040    
041            public static void error(
042                    FacesContext facesContext, String key, Object argument) {
043    
044                    error(null, facesContext, key, argument);
045            }
046    
047            public static void error(
048                    FacesContext facesContext, String key, Object[] arguments) {
049    
050                    error(null, facesContext, key, arguments);
051            }
052    
053            public static void error(
054                    String clientId, FacesContext facesContext, String key) {
055    
056                    _addMessage(clientId, facesContext, FacesMessage.SEVERITY_ERROR, key);
057            }
058    
059            public static void error(
060                    String clientId, FacesContext facesContext, String key,
061                    Object argument) {
062    
063                    _addMessage(
064                            clientId, facesContext, FacesMessage.SEVERITY_ERROR, key, argument);
065            }
066    
067            public static void error(
068                    String clientId, FacesContext facesContext, String key,
069                    Object[] arguments) {
070    
071                    _addMessage(
072                            clientId, facesContext, FacesMessage.SEVERITY_ERROR, key,
073                            arguments);
074            }
075    
076            public static void info(FacesContext facesContext, String key) {
077                    info(null, facesContext, key);
078            }
079    
080            public static void info(
081                    FacesContext facesContext, String key, Object argument) {
082    
083                    info(null, facesContext, key, argument);
084            }
085    
086            public static void info(
087                    FacesContext facesContext, String key, Object[] arguments) {
088    
089                    info(null, facesContext, key, arguments);
090            }
091    
092            public static void info(
093                    String clientId, FacesContext facesContext, String key) {
094    
095                    _addMessage(clientId, facesContext, FacesMessage.SEVERITY_INFO, key);
096            }
097    
098            public static void info(
099                    String clientId, FacesContext facesContext, String key,
100                    Object argument) {
101    
102                    _addMessage(
103                            clientId, facesContext, FacesMessage.SEVERITY_INFO, key, argument);
104            }
105    
106            public static void info(
107                    String clientId, FacesContext facesContext, String key,
108                    Object[] arguments) {
109    
110                    _addMessage(
111                            clientId, facesContext, FacesMessage.SEVERITY_INFO, key, arguments);
112            }
113    
114            private static void _addMessage(
115                    String clientId, FacesContext facesContext, Severity severity,
116                    String key) {
117    
118                    Locale locale = JSFPortletUtil.getLocale(facesContext);
119    
120                    String message = LanguageUtil.get(locale, key);
121    
122                    FacesMessage facesMessage = new FacesMessage(severity, message, null);
123    
124                    facesContext.addMessage(clientId, facesMessage);
125            }
126    
127            private static void _addMessage(
128                    String clientId, FacesContext facesContext, Severity severity,
129                    String key, Object argument) {
130    
131                    Locale locale = JSFPortletUtil.getLocale(facesContext);
132    
133                    String message = LanguageUtil.format(locale, key, argument);
134    
135                    FacesMessage facesMessage = new FacesMessage(severity, message, null);
136    
137                    facesContext.addMessage(clientId, facesMessage);
138            }
139    
140            private static void _addMessage(
141                    String clientId, FacesContext facesContext, Severity severity,
142                    String key, Object[] arguments) {
143    
144                    Locale locale = JSFPortletUtil.getLocale(facesContext);
145    
146                    String message = LanguageUtil.format(locale, key, arguments);
147    
148                    FacesMessage facesMessage = new FacesMessage(severity, message, null);
149    
150                    facesContext.addMessage(clientId, facesMessage);
151            }
152    
153    }