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