1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class FacesMessageUtil {
46  
47      public static void error(FacesContext facesContext, String key) {
48          error(null, facesContext, key);
49      }
50  
51      public static void error(
52          FacesContext facesContext, String key, Object argument) {
53  
54          error(null, facesContext, key, argument);
55      }
56  
57      public static void error(
58          FacesContext facesContext, String key, Object[] arguments) {
59  
60          error(null, facesContext, key, arguments);
61      }
62  
63      public static void error(
64          String clientId, FacesContext facesContext, String key) {
65  
66          _addMessage(clientId, facesContext, FacesMessage.SEVERITY_ERROR, key);
67      }
68  
69      public static void error(
70          String clientId, FacesContext facesContext, String key,
71          Object argument) {
72  
73          _addMessage(
74              clientId, facesContext, FacesMessage.SEVERITY_ERROR, key, argument);
75      }
76  
77      public static void error(
78          String clientId, FacesContext facesContext, String key,
79          Object[] arguments) {
80  
81          _addMessage(
82              clientId, facesContext, FacesMessage.SEVERITY_ERROR, key,
83              arguments);
84      }
85  
86      public static void info(FacesContext facesContext, String key) {
87          info(null, facesContext, key);
88      }
89  
90      public static void info(
91          FacesContext facesContext, String key, Object argument) {
92  
93          info(null, facesContext, key, argument);
94      }
95  
96      public static void info(
97          FacesContext facesContext, String key, Object[] arguments) {
98  
99          info(null, facesContext, key, arguments);
100     }
101 
102     public static void info(
103         String clientId, FacesContext facesContext, String key) {
104 
105         _addMessage(clientId, facesContext, FacesMessage.SEVERITY_INFO, key);
106     }
107 
108     public static void info(
109         String clientId, FacesContext facesContext, String key,
110         Object argument) {
111 
112         _addMessage(
113             clientId, facesContext, FacesMessage.SEVERITY_INFO, key, argument);
114     }
115 
116     public static void info(
117         String clientId, FacesContext facesContext, String key,
118         Object[] arguments) {
119 
120         _addMessage(
121             clientId, facesContext, FacesMessage.SEVERITY_INFO, key, arguments);
122     }
123 
124     private static void _addMessage(
125         String clientId, FacesContext facesContext, Severity severity,
126         String key) {
127 
128         Locale locale = JSFPortletUtil.getLocale(facesContext);
129 
130         String message = LanguageUtil.get(locale, key);
131 
132         FacesMessage facesMessage = new FacesMessage(severity, message, null);
133 
134         facesContext.addMessage(clientId, facesMessage);
135     }
136 
137     private static void _addMessage(
138         String clientId, FacesContext facesContext, Severity severity,
139         String key, Object argument) {
140 
141         Locale locale = JSFPortletUtil.getLocale(facesContext);
142 
143         String message = LanguageUtil.format(locale, key, argument);
144 
145         FacesMessage facesMessage = new FacesMessage(severity, message, null);
146 
147         facesContext.addMessage(clientId, facesMessage);
148     }
149 
150     private static void _addMessage(
151         String clientId, FacesContext facesContext, Severity severity,
152         String key, Object[] arguments) {
153 
154         Locale locale = JSFPortletUtil.getLocale(facesContext);
155 
156         String message = LanguageUtil.format(locale, key, arguments);
157 
158         FacesMessage facesMessage = new FacesMessage(severity, message, null);
159 
160         facesContext.addMessage(clientId, facesMessage);
161     }
162 
163 }