1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.util.bridges.jsf.common;
24  
25  import com.liferay.portal.kernel.language.LanguageUtil;
26  
27  import java.util.Locale;
28  
29  import javax.faces.application.FacesMessage.Severity;
30  import javax.faces.application.FacesMessage;
31  import javax.faces.context.FacesContext;
32  
33  /**
34   * <a href="FacesMessageUtil.java.html"><b><i>View Source</i></b></a>
35   *
36   * <p>
37   * This class provides static convenience methods for creating FacesMessage
38   * objects from locale-specific values in the Liferay portal.properties file,
39   * and adding them to the FacesContext either globally, or to individual
40   * components.
41   * </p>
42   *
43   * @author Neil Griffin
44   *
45   */
46  public class FacesMessageUtil {
47  
48      public static void error(FacesContext facesContext, String key) {
49          error(null, facesContext, key);
50      }
51  
52      public static void error(
53          FacesContext facesContext, String key, Object argument) {
54  
55          error(null, facesContext, key, argument);
56      }
57  
58      public static void error(
59          FacesContext facesContext, String key, Object[] arguments) {
60  
61          error(null, facesContext, key, arguments);
62      }
63  
64      public static void error(
65          String clientId, FacesContext facesContext, String key) {
66  
67          _addMessage(clientId, facesContext, FacesMessage.SEVERITY_ERROR, key);
68      }
69  
70      public static void error(
71          String clientId, FacesContext facesContext, String key,
72          Object argument) {
73  
74          _addMessage(
75              clientId, facesContext, FacesMessage.SEVERITY_ERROR, key, argument);
76      }
77  
78      public static void error(
79          String clientId, FacesContext facesContext, String key,
80          Object[] arguments) {
81  
82          _addMessage(
83              clientId, facesContext, FacesMessage.SEVERITY_ERROR, key,
84              arguments);
85      }
86  
87      public static void info(FacesContext facesContext, String key) {
88          info(null, facesContext, key);
89      }
90  
91      public static void info(
92          FacesContext facesContext, String key, Object argument) {
93  
94          info(null, facesContext, key, argument);
95      }
96  
97      public static void info(
98          FacesContext facesContext, String key, Object[] arguments) {
99  
100         info(null, facesContext, key, arguments);
101     }
102 
103     public static void info(
104         String clientId, FacesContext facesContext, String key) {
105 
106         _addMessage(clientId, facesContext, FacesMessage.SEVERITY_INFO, key);
107     }
108 
109     public static void info(
110         String clientId, FacesContext facesContext, String key,
111         Object argument) {
112 
113         _addMessage(
114             clientId, facesContext, FacesMessage.SEVERITY_INFO, key, argument);
115     }
116 
117     public static void info(
118         String clientId, FacesContext facesContext, String key,
119         Object[] arguments) {
120 
121         _addMessage(
122             clientId, facesContext, FacesMessage.SEVERITY_INFO, key, arguments);
123     }
124 
125     private static void _addMessage(
126         String clientId, FacesContext facesContext, Severity severity,
127         String key) {
128 
129         Locale locale = JSFPortletUtil.getLocale(facesContext);
130 
131         String message = LanguageUtil.get(locale, key);
132 
133         FacesMessage facesMessage = new FacesMessage(severity, message, null);
134 
135         facesContext.addMessage(clientId, facesMessage);
136     }
137 
138     private static void _addMessage(
139         String clientId, FacesContext facesContext, Severity severity,
140         String key, Object argument) {
141 
142         Locale locale = JSFPortletUtil.getLocale(facesContext);
143 
144         String message = LanguageUtil.format(locale, key, argument);
145 
146         FacesMessage facesMessage = new FacesMessage(severity, message, null);
147 
148         facesContext.addMessage(clientId, facesMessage);
149     }
150 
151     private static void _addMessage(
152         String clientId, FacesContext facesContext, Severity severity,
153         String key, Object[] arguments) {
154 
155         Locale locale = JSFPortletUtil.getLocale(facesContext);
156 
157         String message = LanguageUtil.format(locale, key, arguments);
158 
159         FacesMessage facesMessage = new FacesMessage(severity, message, null);
160 
161         facesContext.addMessage(clientId, facesMessage);
162     }
163 
164 }