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  import com.liferay.portal.kernel.log.Log;
27  import com.liferay.portal.kernel.log.LogFactoryUtil;
28  
29  import java.util.Collection;
30  import java.util.Locale;
31  import java.util.Map;
32  import java.util.Set;
33  
34  import javax.faces.context.FacesContext;
35  
36  /**
37   * <a href="LanguageManagedBean.java.html"><b><i>View Source</i></b></a>
38   *
39   * <p>
40   * This class serves as a bridge between the JSF Expression Language (EL)
41   * and Liferay's Language.properties resource bundle.
42   * </p>
43   *
44   * @author Neil Griffin
45   *
46   */
47  public class LanguageManagedBean implements Map<String, String> {
48  
49      public LanguageManagedBean() {
50  
51          // LEP-3275
52  
53          FacesContext facesContext = FacesContext.getCurrentInstance();
54  
55          _companyId = JSFPortletUtil.getCompanyId(facesContext);
56  
57          if (_companyId == 0) {
58              _log.error("Unable to determine company id from facesContext");
59          }
60      }
61  
62      public void clear() {
63          throw new UnsupportedOperationException();
64      }
65  
66      public boolean containsKey(Object key) {
67          throw new UnsupportedOperationException();
68      }
69  
70      public boolean containsValue(Object value) {
71          throw new UnsupportedOperationException();
72      }
73  
74      public boolean isEmpty() {
75          throw new UnsupportedOperationException();
76      }
77  
78      public Set<Entry<String, String>> entrySet() {
79          throw new UnsupportedOperationException();
80      }
81  
82      public String get(Object key) {
83          String value = null;
84  
85          if (key != null) {
86              FacesContext facesContext = FacesContext.getCurrentInstance();
87  
88              Locale locale = facesContext.getViewRoot().getLocale();
89  
90              if (locale == null) {
91                  locale = facesContext.getApplication().getDefaultLocale();
92              }
93  
94              value = LanguageUtil.get(_companyId, locale, key.toString());
95  
96              if (_log.isDebugEnabled()) {
97                  _log.debug(
98                      "{companyId=" + _companyId + ", locale=" + locale +
99                          ", key=" + key + ", value=" + value);
100             }
101         }
102 
103         return value;
104     }
105 
106     public Set<String> keySet() {
107         throw new UnsupportedOperationException();
108     }
109 
110     public String put(String key, String value) {
111         throw new UnsupportedOperationException();
112     }
113 
114     public void putAll(Map<? extends String, ? extends String> map) {
115         throw new UnsupportedOperationException();
116     }
117 
118     public String remove(Object key) {
119         throw new UnsupportedOperationException();
120     }
121 
122     public int size() {
123         throw new UnsupportedOperationException();
124     }
125 
126     public Collection<String> values() {
127         throw new UnsupportedOperationException();
128     }
129 
130     private static Log _log = LogFactoryUtil.getLog(LanguageManagedBean.class);
131 
132     private long _companyId = 0;
133 
134 }