001    /**
002     * Copyright (c) 2000-2013 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 = (HttpServletRequest)request;
085    
086                            Object portletArtifactHack = httpServletRequest.getAttribute(
087                                    "com.icesoft.faces.portletHack");
088    
089                            if (portletArtifactHack == null) {
090                                    return null;
091                            }
092    
093                            try {
094                                    Class<?> portletArtifactHackClass =
095                                            portletArtifactHack.getClass();
096    
097                                    Method method = portletArtifactHackClass.getMethod(
098                                            "getPortletRequest");
099    
100                                    if (method != null) {
101                                            Object value = method.invoke(portletArtifactHack);
102    
103                                            if ((value != null) && (value instanceof PortletRequest)) {
104                                                    return (PortletRequest)value;
105                                            }
106                                    }
107                            }
108                            catch (Exception e) {
109                                    _log.error(e, e);
110                            }
111                    }
112    
113                    return null;
114            }
115    
116            public static String getPreferenceValue(
117                    FacesContext facesContext, String preferenceName) {
118    
119                    return getPreferenceValue(facesContext, preferenceName, null);
120            }
121    
122            public static String getPreferenceValue(
123                    FacesContext facesContext, String preferenceName, String defaultValue) {
124    
125                    return getPreferenceValue(
126                            getPortletPreferences(facesContext), preferenceName, defaultValue);
127            }
128    
129            public static String getPreferenceValue(
130                    PortletPreferences portletPreferences, String preferenceName) {
131    
132                    return getPreferenceValue(portletPreferences, preferenceName, null);
133            }
134    
135            public static String getPreferenceValue(
136                    PortletPreferences portletPreferences, String preferenceName,
137                    String defaultValue) {
138    
139                    String value = defaultValue;
140    
141                    if (portletPreferences != null) {
142                            value = portletPreferences.getValue(preferenceName, defaultValue);
143                    }
144    
145                    return value;
146            }
147    
148            private static Log _log = LogFactoryUtil.getLog(JSFPortletUtil.class);
149    
150    }