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