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.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.util.GetterUtil;
28  
29  import java.lang.reflect.Method;
30  
31  import java.util.Locale;
32  import java.util.Map;
33  
34  import javax.faces.context.FacesContext;
35  
36  import javax.portlet.PortletPreferences;
37  import javax.portlet.PortletRequest;
38  import javax.portlet.RenderRequest;
39  
40  import javax.servlet.http.HttpServletRequest;
41  
42  /**
43   * <a href="JSFPortletUtil.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Neil Griffin
46   *
47   */
48  public class JSFPortletUtil {
49  
50      public static long getCompanyId(FacesContext facesContext) {
51          return getCompanyId(getPortletRequest(facesContext));
52      }
53  
54      public static long getCompanyId(PortletRequest portletRequest) {
55          long companyId = 0;
56  
57          Map<String, String> userInfo =
58              (Map<String, String>)portletRequest.getAttribute(
59                  RenderRequest.USER_INFO);
60  
61          if (userInfo != null) {
62              companyId = GetterUtil.getLong(userInfo.get("liferay.company.id"));
63          }
64  
65          return companyId;
66      }
67  
68      public static Locale getLocale(FacesContext facesContext) {
69          Locale locale = facesContext.getViewRoot().getLocale();
70  
71          if (locale == null) {
72              locale = facesContext.getApplication().getDefaultLocale();
73          }
74  
75          return (locale);
76      }
77  
78      public static PortletPreferences getPortletPreferences(
79          FacesContext facesContext) {
80  
81          return JSFPortletUtil.getPortletRequest(facesContext).getPreferences();
82      }
83  
84      public static PortletRequest getPortletRequest(FacesContext facesContext) {
85          Object request = facesContext.getExternalContext().getRequest();
86  
87          if (request == null) {
88              return null;
89          }
90  
91          if (request instanceof PortletRequest) {
92              return (PortletRequest)request;
93          }
94          else if (request instanceof HttpServletRequest) {
95              HttpServletRequest httpServletRequest =
96                  (HttpServletRequest)request;
97  
98              Object portletArtifactHack = httpServletRequest.getAttribute(
99                  "com.icesoft.faces.portletHack");
100 
101             if (portletArtifactHack == null) {
102                 return null;
103             }
104 
105             try {
106                 Class<?> portletArtifactHackClass =
107                     portletArtifactHack.getClass();
108 
109                 Method method = portletArtifactHackClass.getMethod(
110                     "getPortletRequest");
111 
112                 if (method != null) {
113                     Object value = method.invoke(portletArtifactHack);
114 
115                     if ((value != null) && (value instanceof PortletRequest)) {
116                         return (PortletRequest)value;
117                     }
118                 }
119             }
120             catch (Exception e) {
121                 _log.error(e, e);
122             }
123         }
124 
125         return null;
126     }
127 
128     public static String getPreferenceValue(
129         FacesContext facesContext, String preferenceName) {
130 
131         return getPreferenceValue(facesContext, preferenceName, null);
132     }
133 
134     public static String getPreferenceValue(
135         PortletPreferences portletPreferences, String preferenceName) {
136 
137         return getPreferenceValue(portletPreferences, preferenceName, null);
138     }
139 
140     public static String getPreferenceValue(
141         FacesContext facesContext, String preferenceName, String defaultValue) {
142 
143         return getPreferenceValue(
144             getPortletPreferences(facesContext), preferenceName, defaultValue);
145     }
146 
147     public static String getPreferenceValue(
148         PortletPreferences portletPreferences, String preferenceName,
149         String defaultValue) {
150 
151         String value = defaultValue;
152 
153         if (portletPreferences != null) {
154             value = portletPreferences.getValue(preferenceName, defaultValue);
155         }
156 
157         return value;
158     }
159 
160     private static Log _log = LogFactoryUtil.getLog(JSFPortletUtil.class);
161 
162 }