1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class JSFPortletUtil {
48  
49      public static long getCompanyId(FacesContext facesContext) {
50          return getCompanyId(getPortletRequest(facesContext));
51      }
52  
53      public static long getCompanyId(PortletRequest portletRequest) {
54          long companyId = 0;
55  
56          Map<String, String> userInfo =
57              (Map<String, String>)portletRequest.getAttribute(
58                  RenderRequest.USER_INFO);
59  
60          if (userInfo != null) {
61              companyId = GetterUtil.getLong(userInfo.get("liferay.company.id"));
62          }
63  
64          return companyId;
65      }
66  
67      public static Locale getLocale(FacesContext facesContext) {
68          Locale locale = facesContext.getViewRoot().getLocale();
69  
70          if (locale == null) {
71              locale = facesContext.getApplication().getDefaultLocale();
72          }
73  
74          return (locale);
75      }
76  
77      public static PortletPreferences getPortletPreferences(
78          FacesContext facesContext) {
79  
80          return JSFPortletUtil.getPortletRequest(facesContext).getPreferences();
81      }
82  
83      public static PortletRequest getPortletRequest(FacesContext facesContext) {
84          Object request = facesContext.getExternalContext().getRequest();
85  
86          if (request == null) {
87              return null;
88          }
89  
90          if (request instanceof PortletRequest) {
91              return (PortletRequest)request;
92          }
93          else if (request instanceof HttpServletRequest) {
94              HttpServletRequest httpServletRequest =
95                  (HttpServletRequest)request;
96  
97              Object portletArtifactHack = httpServletRequest.getAttribute(
98                  "com.icesoft.faces.portletHack");
99  
100             if (portletArtifactHack == null) {
101                 return null;
102             }
103 
104             try {
105                 Class<?> portletArtifactHackClass =
106                     portletArtifactHack.getClass();
107 
108                 Method method = portletArtifactHackClass.getMethod(
109                     "getPortletRequest");
110 
111                 if (method != null) {
112                     Object value = method.invoke(portletArtifactHack);
113 
114                     if ((value != null) && (value instanceof PortletRequest)) {
115                         return (PortletRequest)value;
116                     }
117                 }
118             }
119             catch (Exception e) {
120                 _log.error(e, e);
121             }
122         }
123 
124         return null;
125     }
126 
127     public static String getPreferenceValue(
128         FacesContext facesContext, String preferenceName) {
129 
130         return getPreferenceValue(facesContext, preferenceName, null);
131     }
132 
133     public static String getPreferenceValue(
134         PortletPreferences portletPreferences, String preferenceName) {
135 
136         return getPreferenceValue(portletPreferences, preferenceName, null);
137     }
138 
139     public static String getPreferenceValue(
140         FacesContext facesContext, String preferenceName, String defaultValue) {
141 
142         return getPreferenceValue(
143             getPortletPreferences(facesContext), preferenceName, defaultValue);
144     }
145 
146     public static String getPreferenceValue(
147         PortletPreferences portletPreferences, String preferenceName,
148         String defaultValue) {
149 
150         String value = defaultValue;
151 
152         if (portletPreferences != null) {
153             value = portletPreferences.getValue(preferenceName, defaultValue);
154         }
155 
156         return value;
157     }
158 
159     private static Log _log = LogFactoryUtil.getLog(JSFPortletUtil.class);
160 
161 }