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