001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.util.bridges.jsf.common;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    
021    import java.lang.reflect.Method;
022    
023    import java.util.Locale;
024    import java.util.Map;
025    
026    import javax.faces.context.FacesContext;
027    
028    import javax.portlet.PortletPreferences;
029    import javax.portlet.PortletRequest;
030    import javax.portlet.RenderRequest;
031    
032    import javax.servlet.http.HttpServletRequest;
033    
034    /**
035     * @author Neil Griffin
036     */
037    public class JSFPortletUtil {
038    
039            public static long getCompanyId(FacesContext facesContext) {
040                    return getCompanyId(getPortletRequest(facesContext));
041            }
042    
043            public static long getCompanyId(PortletRequest portletRequest) {
044                    long companyId = 0;
045    
046                    Map<String, String> userInfo =
047                            (Map<String, String>)portletRequest.getAttribute(
048                                    RenderRequest.USER_INFO);
049    
050                    if (userInfo != null) {
051                            companyId = GetterUtil.getLong(userInfo.get("liferay.company.id"));
052                    }
053    
054                    return companyId;
055            }
056    
057            public static Locale getLocale(FacesContext facesContext) {
058                    Locale locale = facesContext.getViewRoot().getLocale();
059    
060                    if (locale == null) {
061                            locale = facesContext.getApplication().getDefaultLocale();
062                    }
063    
064                    return (locale);
065            }
066    
067            public static PortletPreferences getPortletPreferences(
068                    FacesContext facesContext) {
069    
070                    return JSFPortletUtil.getPortletRequest(facesContext).getPreferences();
071            }
072    
073            public static PortletRequest getPortletRequest(FacesContext facesContext) {
074                    Object request = facesContext.getExternalContext().getRequest();
075    
076                    if (request == null) {
077                            return null;
078                    }
079    
080                    if (request instanceof PortletRequest) {
081                            return (PortletRequest)request;
082                    }
083                    else if (request instanceof HttpServletRequest) {
084                            HttpServletRequest httpServletRequest =
085                                    (HttpServletRequest)request;
086    
087                            Object portletArtifactHack = httpServletRequest.getAttribute(
088                                    "com.icesoft.faces.portletHack");
089    
090                            if (portletArtifactHack == null) {
091                                    return null;
092                            }
093    
094                            try {
095                                    Class<?> portletArtifactHackClass =
096                                            portletArtifactHack.getClass();
097    
098                                    Method method = portletArtifactHackClass.getMethod(
099                                            "getPortletRequest");
100    
101                                    if (method != null) {
102                                            Object value = method.invoke(portletArtifactHack);
103    
104                                            if ((value != null) && (value instanceof PortletRequest)) {
105                                                    return (PortletRequest)value;
106                                            }
107                                    }
108                            }
109                            catch (Exception e) {
110                                    _log.error(e, e);
111                            }
112                    }
113    
114                    return null;
115            }
116    
117            public static String getPreferenceValue(
118                    FacesContext facesContext, String preferenceName) {
119    
120                    return getPreferenceValue(facesContext, preferenceName, null);
121            }
122    
123            public static String getPreferenceValue(
124                    PortletPreferences portletPreferences, String preferenceName) {
125    
126                    return getPreferenceValue(portletPreferences, preferenceName, null);
127            }
128    
129            public static String getPreferenceValue(
130                    FacesContext facesContext, String preferenceName, String defaultValue) {
131    
132                    return getPreferenceValue(
133                            getPortletPreferences(facesContext), preferenceName, defaultValue);
134            }
135    
136            public static String getPreferenceValue(
137                    PortletPreferences portletPreferences, String preferenceName,
138                    String defaultValue) {
139    
140                    String value = defaultValue;
141    
142                    if (portletPreferences != null) {
143                            value = portletPreferences.getValue(preferenceName, defaultValue);
144                    }
145    
146                    return value;
147            }
148    
149            private static Log _log = LogFactoryUtil.getLog(JSFPortletUtil.class);
150    
151    }