001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.util.bridges.jsf.common;
022:
023: import com.liferay.portal.kernel.util.GetterUtil;
024:
025: import java.lang.reflect.Method;
026:
027: import java.util.Locale;
028: import java.util.Map;
029:
030: import javax.faces.context.FacesContext;
031:
032: import javax.portlet.PortletPreferences;
033: import javax.portlet.PortletRequest;
034: import javax.portlet.RenderRequest;
035:
036: import javax.servlet.http.HttpServletRequest;
037:
038: import org.apache.commons.logging.Log;
039: import org.apache.commons.logging.LogFactory;
040:
041: /**
042: * <a href="JSFPortletUtil.java.html"><b><i>View Source</i></b></a>
043: *
044: * @author Neil Griffin
045: *
046: */
047: public class JSFPortletUtil {
048:
049: public static long getCompanyId(FacesContext facesContext) {
050: return getCompanyId(getPortletRequest(facesContext));
051: }
052:
053: public static long getCompanyId(PortletRequest portletRequest) {
054: long companyId = 0;
055:
056: Map userInfo = (Map) portletRequest
057: .getAttribute(RenderRequest.USER_INFO);
058:
059: if (userInfo != null) {
060: companyId = GetterUtil.getLong((String) userInfo
061: .get("liferay.company.id"));
062: }
063:
064: return companyId;
065: }
066:
067: public static Locale getLocale(FacesContext facesContext) {
068: Locale locale = facesContext.getViewRoot().getLocale();
069:
070: if (locale == null) {
071: locale = facesContext.getApplication().getDefaultLocale();
072: }
073:
074: return (locale);
075: }
076:
077: public static PortletPreferences getPortletPreferences(
078: FacesContext facesContext) {
079:
080: return JSFPortletUtil.getPortletRequest(facesContext)
081: .getPreferences();
082: }
083:
084: public static PortletRequest getPortletRequest(
085: FacesContext facesContext) {
086: Object request = facesContext.getExternalContext().getRequest();
087:
088: if (request == null) {
089: return null;
090: }
091:
092: if (request instanceof PortletRequest) {
093: return (PortletRequest) request;
094: } else if (request instanceof HttpServletRequest) {
095: HttpServletRequest httpServletRequest = (HttpServletRequest) request;
096:
097: Object portletArtifactHack = httpServletRequest
098: .getAttribute("com.icesoft.faces.portletHack");
099:
100: if (portletArtifactHack == null) {
101: return null;
102: }
103:
104: try {
105: Class portletArtifactHackClass = portletArtifactHack
106: .getClass();
107:
108: Method method = portletArtifactHackClass.getMethod(
109: "getPortletRequest", null);
110:
111: if (method != null) {
112: Object value = method.invoke(portletArtifactHack,
113: null);
114:
115: if ((value != null)
116: && (value instanceof PortletRequest)) {
117: return (PortletRequest) value;
118: }
119: }
120: } catch (Exception e) {
121: _log.error(e, e);
122: }
123: }
124:
125: return null;
126: }
127:
128: public static String getPreferenceValue(FacesContext facesContext,
129: String preferenceName) {
130:
131: return getPreferenceValue(facesContext, preferenceName, null);
132: }
133:
134: public static String getPreferenceValue(
135: PortletPreferences portletPreferences, String preferenceName) {
136:
137: return getPreferenceValue(portletPreferences, preferenceName,
138: null);
139: }
140:
141: public static String getPreferenceValue(FacesContext facesContext,
142: String preferenceName, String defaultValue) {
143:
144: return getPreferenceValue(getPortletPreferences(facesContext),
145: preferenceName, defaultValue);
146: }
147:
148: public static String getPreferenceValue(
149: PortletPreferences portletPreferences,
150: String preferenceName, String defaultValue) {
151:
152: String value = defaultValue;
153:
154: if (portletPreferences != null) {
155: value = portletPreferences.getValue(preferenceName,
156: defaultValue);
157: }
158:
159: return value;
160: }
161:
162: private static Log _log = LogFactory.getLog(JSFPortletUtil.class);
163:
164: }
|