1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.util.bridges.jsf.common;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.util.GetterUtil;
20  
21  import java.lang.reflect.Method;
22  
23  import java.util.Locale;
24  import java.util.Map;
25  
26  import javax.faces.context.FacesContext;
27  
28  import javax.portlet.PortletPreferences;
29  import javax.portlet.PortletRequest;
30  import javax.portlet.RenderRequest;
31  
32  import javax.servlet.http.HttpServletRequest;
33  
34  /**
35   * <a href="JSFPortletUtil.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Neil Griffin
38   */
39  public class JSFPortletUtil {
40  
41      public static long getCompanyId(FacesContext facesContext) {
42          return getCompanyId(getPortletRequest(facesContext));
43      }
44  
45      public static long getCompanyId(PortletRequest portletRequest) {
46          long companyId = 0;
47  
48          Map<String, String> userInfo =
49              (Map<String, String>)portletRequest.getAttribute(
50                  RenderRequest.USER_INFO);
51  
52          if (userInfo != null) {
53              companyId = GetterUtil.getLong(userInfo.get("liferay.company.id"));
54          }
55  
56          return companyId;
57      }
58  
59      public static Locale getLocale(FacesContext facesContext) {
60          Locale locale = facesContext.getViewRoot().getLocale();
61  
62          if (locale == null) {
63              locale = facesContext.getApplication().getDefaultLocale();
64          }
65  
66          return (locale);
67      }
68  
69      public static PortletPreferences getPortletPreferences(
70          FacesContext facesContext) {
71  
72          return JSFPortletUtil.getPortletRequest(facesContext).getPreferences();
73      }
74  
75      public static PortletRequest getPortletRequest(FacesContext facesContext) {
76          Object request = facesContext.getExternalContext().getRequest();
77  
78          if (request == null) {
79              return null;
80          }
81  
82          if (request instanceof PortletRequest) {
83              return (PortletRequest)request;
84          }
85          else if (request instanceof HttpServletRequest) {
86              HttpServletRequest httpServletRequest =
87                  (HttpServletRequest)request;
88  
89              Object portletArtifactHack = httpServletRequest.getAttribute(
90                  "com.icesoft.faces.portletHack");
91  
92              if (portletArtifactHack == null) {
93                  return null;
94              }
95  
96              try {
97                  Class<?> portletArtifactHackClass =
98                      portletArtifactHack.getClass();
99  
100                 Method method = portletArtifactHackClass.getMethod(
101                     "getPortletRequest");
102 
103                 if (method != null) {
104                     Object value = method.invoke(portletArtifactHack);
105 
106                     if ((value != null) && (value instanceof PortletRequest)) {
107                         return (PortletRequest)value;
108                     }
109                 }
110             }
111             catch (Exception e) {
112                 _log.error(e, e);
113             }
114         }
115 
116         return null;
117     }
118 
119     public static String getPreferenceValue(
120         FacesContext facesContext, String preferenceName) {
121 
122         return getPreferenceValue(facesContext, preferenceName, null);
123     }
124 
125     public static String getPreferenceValue(
126         PortletPreferences portletPreferences, String preferenceName) {
127 
128         return getPreferenceValue(portletPreferences, preferenceName, null);
129     }
130 
131     public static String getPreferenceValue(
132         FacesContext facesContext, String preferenceName, String defaultValue) {
133 
134         return getPreferenceValue(
135             getPortletPreferences(facesContext), preferenceName, defaultValue);
136     }
137 
138     public static String getPreferenceValue(
139         PortletPreferences portletPreferences, String preferenceName,
140         String defaultValue) {
141 
142         String value = defaultValue;
143 
144         if (portletPreferences != null) {
145             value = portletPreferences.getValue(preferenceName, defaultValue);
146         }
147 
148         return value;
149     }
150 
151     private static Log _log = LogFactoryUtil.getLog(JSFPortletUtil.class);
152 
153 }