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.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 userInfo = (Map)portletRequest.getAttribute(
59              RenderRequest.USER_INFO);
60  
61          if (userInfo != null) {
62              companyId = GetterUtil.getLong(
63                  (String)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 = portletArtifactHack.getClass();
108 
109                 Method method = portletArtifactHackClass.getMethod(
110                     "getPortletRequest", null);
111 
112                 if (method != null) {
113                     Object value = method.invoke(portletArtifactHack, null);
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 = LogFactory.getLog(JSFPortletUtil.class);
161 
162 }