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