1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.util.bridges.jsf.common;
21  
22  import com.liferay.portal.kernel.log.Log;
23  import com.liferay.portal.kernel.log.LogFactoryUtil;
24  import com.liferay.portal.kernel.util.GetterUtil;
25  
26  import java.lang.reflect.Method;
27  
28  import java.util.Locale;
29  import java.util.Map;
30  
31  import javax.faces.context.FacesContext;
32  
33  import javax.portlet.PortletPreferences;
34  import javax.portlet.PortletRequest;
35  import javax.portlet.RenderRequest;
36  
37  import javax.servlet.http.HttpServletRequest;
38  
39  /**
40   * <a href="JSFPortletUtil.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Neil Griffin
43   *
44   */
45  public class JSFPortletUtil {
46  
47      public static long getCompanyId(FacesContext facesContext) {
48          return getCompanyId(getPortletRequest(facesContext));
49      }
50  
51      public static long getCompanyId(PortletRequest portletRequest) {
52          long companyId = 0;
53  
54          Map<String, String> userInfo =
55              (Map<String, String>)portletRequest.getAttribute(
56                  RenderRequest.USER_INFO);
57  
58          if (userInfo != null) {
59              companyId = GetterUtil.getLong(userInfo.get("liferay.company.id"));
60          }
61  
62          return companyId;
63      }
64  
65      public static Locale getLocale(FacesContext facesContext) {
66          Locale locale = facesContext.getViewRoot().getLocale();
67  
68          if (locale == null) {
69              locale = facesContext.getApplication().getDefaultLocale();
70          }
71  
72          return (locale);
73      }
74  
75      public static PortletPreferences getPortletPreferences(
76          FacesContext facesContext) {
77  
78          return JSFPortletUtil.getPortletRequest(facesContext).getPreferences();
79      }
80  
81      public static PortletRequest getPortletRequest(FacesContext facesContext) {
82          Object request = facesContext.getExternalContext().getRequest();
83  
84          if (request == null) {
85              return null;
86          }
87  
88          if (request instanceof PortletRequest) {
89              return (PortletRequest)request;
90          }
91          else if (request instanceof HttpServletRequest) {
92              HttpServletRequest httpServletRequest =
93                  (HttpServletRequest)request;
94  
95              Object portletArtifactHack = httpServletRequest.getAttribute(
96                  "com.icesoft.faces.portletHack");
97  
98              if (portletArtifactHack == null) {
99                  return null;
100             }
101 
102             try {
103                 Class<?> portletArtifactHackClass =
104                     portletArtifactHack.getClass();
105 
106                 Method method = portletArtifactHackClass.getMethod(
107                     "getPortletRequest");
108 
109                 if (method != null) {
110                     Object value = method.invoke(portletArtifactHack);
111 
112                     if ((value != null) && (value instanceof PortletRequest)) {
113                         return (PortletRequest)value;
114                     }
115                 }
116             }
117             catch (Exception e) {
118                 _log.error(e, e);
119             }
120         }
121 
122         return null;
123     }
124 
125     public static String getPreferenceValue(
126         FacesContext facesContext, String preferenceName) {
127 
128         return getPreferenceValue(facesContext, preferenceName, null);
129     }
130 
131     public static String getPreferenceValue(
132         PortletPreferences portletPreferences, String preferenceName) {
133 
134         return getPreferenceValue(portletPreferences, preferenceName, null);
135     }
136 
137     public static String getPreferenceValue(
138         FacesContext facesContext, String preferenceName, String defaultValue) {
139 
140         return getPreferenceValue(
141             getPortletPreferences(facesContext), preferenceName, defaultValue);
142     }
143 
144     public static String getPreferenceValue(
145         PortletPreferences portletPreferences, String preferenceName,
146         String defaultValue) {
147 
148         String value = defaultValue;
149 
150         if (portletPreferences != null) {
151             value = portletPreferences.getValue(preferenceName, defaultValue);
152         }
153 
154         return value;
155     }
156 
157     private static Log _log = LogFactoryUtil.getLog(JSFPortletUtil.class);
158 
159 }