001    /**
002     * Copyright (c) 2000-2011 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.portal.service;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.util.Constants;
020    import com.liferay.portal.kernel.util.ParamUtil;
021    import com.liferay.portal.kernel.util.StringUtil;
022    import com.liferay.portal.kernel.util.WebKeys;
023    import com.liferay.portal.kernel.workflow.WorkflowConstants;
024    import com.liferay.portal.model.PortletPreferencesIds;
025    import com.liferay.portal.theme.ThemeDisplay;
026    import com.liferay.portal.util.PortalUtil;
027    import com.liferay.portlet.PortletPreferencesFactoryUtil;
028    import com.liferay.portlet.expando.util.ExpandoBridgeFactoryUtil;
029    
030    import java.io.Serializable;
031    
032    import java.util.Enumeration;
033    import java.util.HashMap;
034    import java.util.Map;
035    
036    import javax.portlet.PortletRequest;
037    
038    import javax.servlet.http.HttpServletRequest;
039    
040    /**
041     * @author Brian Wing Shun Chan
042     * @author Raymond Augé
043     */
044    public class ServiceContextFactory {
045    
046            public static ServiceContext getInstance(HttpServletRequest request)
047                    throws PortalException, SystemException {
048    
049                    ServiceContext serviceContext = new ServiceContext();
050    
051                    // Theme display
052    
053                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
054                            WebKeys.THEME_DISPLAY);
055    
056                    serviceContext.setCompanyId(themeDisplay.getCompanyId());
057                    serviceContext.setLanguageId(themeDisplay.getLanguageId());
058                    serviceContext.setLayoutFullURL(
059                            PortalUtil.getLayoutFullURL(themeDisplay));
060                    serviceContext.setLayoutURL(PortalUtil.getLayoutURL(themeDisplay));
061                    serviceContext.setPathMain(PortalUtil.getPathMain());
062                    serviceContext.setPlid(themeDisplay.getPlid());
063                    serviceContext.setPortalURL(PortalUtil.getPortalURL(request));
064                    serviceContext.setScopeGroupId(themeDisplay.getScopeGroupId());
065                    serviceContext.setUserDisplayURL(
066                            themeDisplay.getUser().getDisplayURL(themeDisplay));
067                    serviceContext.setUserId(themeDisplay.getUserId());
068    
069                    // Attributes
070    
071                    Map<String, Serializable> attributes =
072                            new HashMap<String, Serializable>();
073    
074                    Enumeration<String> enu = request.getParameterNames();
075    
076                    while (enu.hasMoreElements()) {
077                            String param = enu.nextElement();
078    
079                            String[] values = request.getParameterValues(param);
080    
081                            if ((values != null) && (values.length > 0)) {
082                                    if (values.length == 1) {
083                                            attributes.put(param, values[0]);
084                                    }
085                                    else {
086                                            attributes.put(param, values);
087                                    }
088                            }
089                    }
090    
091                    serviceContext.setAttributes(attributes);
092    
093                    return serviceContext;
094            }
095    
096            public static ServiceContext getInstance(PortletRequest portletRequest)
097                    throws PortalException, SystemException {
098    
099                    // Theme display
100    
101                    ServiceContext serviceContext =
102                            ServiceContextThreadLocal.getServiceContext();
103    
104                    ThemeDisplay themeDisplay =
105                            (ThemeDisplay)portletRequest.getAttribute(
106                                    WebKeys.THEME_DISPLAY);
107    
108                    if (serviceContext != null) {
109                            serviceContext = (ServiceContext)serviceContext.clone();
110                    }
111                    else {
112                            serviceContext = new ServiceContext();
113    
114                            serviceContext.setCompanyId(themeDisplay.getCompanyId());
115                            serviceContext.setLanguageId(themeDisplay.getLanguageId());
116                            serviceContext.setLayoutFullURL(
117                                    PortalUtil.getLayoutFullURL(themeDisplay));
118                            serviceContext.setLayoutURL(PortalUtil.getLayoutURL(themeDisplay));
119                            serviceContext.setPathMain(PortalUtil.getPathMain());
120                            serviceContext.setPlid(themeDisplay.getPlid());
121                            serviceContext.setPortalURL(
122                                    PortalUtil.getPortalURL(portletRequest));
123                            serviceContext.setUserDisplayURL(
124                                    themeDisplay.getUser().getDisplayURL(themeDisplay));
125                            serviceContext.setUserId(themeDisplay.getUserId());
126                    }
127    
128                    serviceContext.setScopeGroupId(themeDisplay.getScopeGroupId());
129    
130                    // Attributes
131    
132                    Map<String, Serializable> attributes =
133                            new HashMap<String, Serializable>();
134    
135                    Enumeration<String> enu = portletRequest.getParameterNames();
136    
137                    while (enu.hasMoreElements()) {
138                            String param = enu.nextElement();
139    
140                            String[] values = portletRequest.getParameterValues(param);
141    
142                            if ((values != null) && (values.length > 0)) {
143                                    if (values.length == 1) {
144                                            attributes.put(param, values[0]);
145                                    }
146                                    else {
147                                            attributes.put(param, values);
148                                    }
149                            }
150                    }
151    
152                    serviceContext.setAttributes(attributes);
153    
154                    // Command
155    
156                    String cmd = ParamUtil.getString(portletRequest, Constants.CMD);
157    
158                    serviceContext.setCommand(cmd);
159    
160                    // Permissions
161    
162                    boolean addCommunityPermissions = ParamUtil.getBoolean(
163                            portletRequest, "addCommunityPermissions");
164                    boolean addGuestPermissions = ParamUtil.getBoolean(
165                            portletRequest, "addGuestPermissions");
166                    String[] communityPermissions = PortalUtil.getCommunityPermissions(
167                            portletRequest);
168                    String[] guestPermissions = PortalUtil.getGuestPermissions(
169                            portletRequest);
170    
171                    serviceContext.setAddCommunityPermissions(addCommunityPermissions);
172                    serviceContext.setAddGuestPermissions(addGuestPermissions);
173                    serviceContext.setCommunityPermissions(communityPermissions);
174                    serviceContext.setGuestPermissions(guestPermissions);
175    
176                    // Portlet preferences ids
177    
178                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
179                            portletRequest);
180    
181                    String portletId = PortalUtil.getPortletId(portletRequest);
182    
183                    PortletPreferencesIds portletPreferencesIds =
184                            PortletPreferencesFactoryUtil.getPortletPreferencesIds(
185                                    request, portletId);
186    
187                    serviceContext.setPortletPreferencesIds(portletPreferencesIds);
188    
189                    // Asset
190    
191                    long[] assetCategoryIds = StringUtil.split(
192                            ParamUtil.getString(portletRequest, "assetCategoryIds"), 0L);
193                    String[] assetTagNames = StringUtil.split(
194                            ParamUtil.getString(portletRequest, "assetTagNames"));
195    
196                    serviceContext.setAssetCategoryIds(assetCategoryIds);
197                    serviceContext.setAssetTagNames(assetTagNames);
198    
199                    // Workflow
200    
201                    int workflowAction = ParamUtil.getInteger(
202                            portletRequest, "workflowAction", WorkflowConstants.ACTION_PUBLISH);
203    
204                    serviceContext.setWorkflowAction(workflowAction);
205    
206                    return serviceContext;
207            }
208    
209            public static ServiceContext getInstance(
210                            String className, PortletRequest portletRequest)
211                    throws PortalException, SystemException {
212    
213                    ServiceContext serviceContext = getInstance(portletRequest);
214    
215                    // Expando
216    
217                    Map<String, Serializable> expandoBridgeAttributes =
218                            PortalUtil.getExpandoBridgeAttributes(
219                                    ExpandoBridgeFactoryUtil.getExpandoBridge(
220                                            serviceContext.getCompanyId(), className),
221                                    portletRequest);
222    
223                    serviceContext.setExpandoBridgeAttributes(expandoBridgeAttributes);
224    
225                    return serviceContext;
226            }
227    
228    }