001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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(
047                            String className, PortletRequest portletRequest)
048                    throws PortalException, SystemException {
049    
050                    ServiceContext serviceContext = new ServiceContext();
051    
052                    // Theme display
053    
054                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
055                            WebKeys.THEME_DISPLAY);
056    
057                    serviceContext.setCompanyId(themeDisplay.getCompanyId());
058                    serviceContext.setLanguageId(themeDisplay.getLanguageId());
059                    serviceContext.setLayoutFullURL(
060                            PortalUtil.getLayoutFullURL(themeDisplay));
061                    serviceContext.setLayoutURL(PortalUtil.getLayoutURL(themeDisplay));
062                    serviceContext.setPathMain(PortalUtil.getPathMain());
063                    serviceContext.setPlid(themeDisplay.getPlid());
064                    serviceContext.setPortalURL(PortalUtil.getPortalURL(portletRequest));
065                    serviceContext.setScopeGroupId(themeDisplay.getScopeGroupId());
066                    serviceContext.setUserDisplayURL(
067                            themeDisplay.getUser().getDisplayURL(themeDisplay));
068                    serviceContext.setUserId(themeDisplay.getUserId());
069    
070                    // Attributes
071    
072                    Map<String, Serializable> attributes =
073                            new HashMap<String, Serializable>();
074    
075                    Enumeration<String> enu = portletRequest.getParameterNames();
076    
077                    while (enu.hasMoreElements()) {
078                            String param = enu.nextElement();
079    
080                            String[] values = portletRequest.getParameterValues(param);
081    
082                            if ((values != null) && (values.length > 0)) {
083                                    if (values.length == 1) {
084                                            attributes.put(param, values[0]);
085                                    }
086                                    else {
087                                            attributes.put(param, values);
088                                    }
089                            }
090                    }
091    
092                    serviceContext.setAttributes(attributes);
093    
094                    // Command
095    
096                    String cmd = ParamUtil.getString(portletRequest, Constants.CMD);
097    
098                    serviceContext.setCommand(cmd);
099    
100                    // Expando
101    
102                    Map<String, Serializable> expandoBridgeAttributes =
103                            PortalUtil.getExpandoBridgeAttributes(
104                                    ExpandoBridgeFactoryUtil.getExpandoBridge(
105                                            themeDisplay.getCompanyId(), className),
106                                    portletRequest);
107    
108                    serviceContext.setExpandoBridgeAttributes(expandoBridgeAttributes);
109    
110                    // Permissions
111    
112                    boolean addCommunityPermissions = ParamUtil.getBoolean(
113                            portletRequest, "addCommunityPermissions");
114                    boolean addGuestPermissions = ParamUtil.getBoolean(
115                            portletRequest, "addGuestPermissions");
116                    String[] communityPermissions = PortalUtil.getCommunityPermissions(
117                            portletRequest);
118                    String[] guestPermissions = PortalUtil.getGuestPermissions(
119                            portletRequest);
120    
121                    serviceContext.setAddCommunityPermissions(addCommunityPermissions);
122                    serviceContext.setAddGuestPermissions(addGuestPermissions);
123                    serviceContext.setCommunityPermissions(communityPermissions);
124                    serviceContext.setGuestPermissions(guestPermissions);
125    
126                    // Portlet preferences ids
127    
128                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
129                            portletRequest);
130    
131                    String portletId = PortalUtil.getPortletId(portletRequest);
132    
133                    PortletPreferencesIds portletPreferencesIds =
134                            PortletPreferencesFactoryUtil.getPortletPreferencesIds(
135                                    request, portletId);
136    
137                    serviceContext.setPortletPreferencesIds(portletPreferencesIds);
138    
139                    // Asset
140    
141                    long[] assetCategoryIds = StringUtil.split(
142                            ParamUtil.getString(portletRequest, "assetCategoryIds"), 0L);
143                    String[] assetTagNames = StringUtil.split(
144                            ParamUtil.getString(portletRequest, "assetTagNames"));
145    
146                    serviceContext.setAssetCategoryIds(assetCategoryIds);
147                    serviceContext.setAssetTagNames(assetTagNames);
148    
149                    // Workflow
150    
151                    int workflowAction = ParamUtil.getInteger(
152                            portletRequest, "workflowAction", WorkflowConstants.ACTION_PUBLISH);
153    
154                    serviceContext.setWorkflowAction(workflowAction);
155    
156                    return serviceContext;
157            }
158    
159    }