1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.service;
16  
17  import com.liferay.portal.kernel.exception.PortalException;
18  import com.liferay.portal.kernel.exception.SystemException;
19  import com.liferay.portal.kernel.util.Constants;
20  import com.liferay.portal.kernel.util.ParamUtil;
21  import com.liferay.portal.kernel.util.StringUtil;
22  import com.liferay.portal.kernel.util.WebKeys;
23  import com.liferay.portal.model.PortletPreferencesIds;
24  import com.liferay.portal.theme.ThemeDisplay;
25  import com.liferay.portal.util.PortalUtil;
26  import com.liferay.portlet.PortletPreferencesFactoryUtil;
27  import com.liferay.portlet.expando.util.ExpandoBridgeFactoryUtil;
28  
29  import java.io.Serializable;
30  
31  import java.util.Enumeration;
32  import java.util.HashMap;
33  import java.util.Map;
34  
35  import javax.portlet.PortletRequest;
36  
37  import javax.servlet.http.HttpServletRequest;
38  
39  /**
40   * <a href="ServiceContextFactory.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Brian Wing Shun Chan
43   * @author Raymond Augé
44   */
45  public class ServiceContextFactory {
46  
47      public static ServiceContext getInstance(
48              String className, PortletRequest portletRequest)
49          throws PortalException, SystemException {
50  
51          ServiceContext serviceContext = new ServiceContext();
52  
53          // Theme display
54  
55          ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
56              WebKeys.THEME_DISPLAY);
57  
58          serviceContext.setCompanyId(themeDisplay.getCompanyId());
59          serviceContext.setLanguageId(themeDisplay.getLanguageId());
60          serviceContext.setLayoutFullURL(
61              PortalUtil.getLayoutFullURL(themeDisplay));
62          serviceContext.setLayoutURL(PortalUtil.getLayoutURL(themeDisplay));
63          serviceContext.setPathMain(PortalUtil.getPathMain());
64          serviceContext.setPlid(themeDisplay.getPlid());
65          serviceContext.setPortalURL(PortalUtil.getPortalURL(portletRequest));
66          serviceContext.setScopeGroupId(themeDisplay.getScopeGroupId());
67          serviceContext.setUserDisplayURL(
68              themeDisplay.getUser().getDisplayURL(themeDisplay));
69          serviceContext.setUserId(themeDisplay.getUserId());
70  
71          // Attributes
72  
73          Map<String, Serializable> attributes =
74              new HashMap<String, Serializable>();
75  
76          Enumeration<String> enu = portletRequest.getParameterNames();
77  
78          while (enu.hasMoreElements()) {
79              String param = enu.nextElement();
80  
81              String[] values = portletRequest.getParameterValues(param);
82  
83              if ((values != null) && (values.length > 0)) {
84                  if (values.length == 1) {
85                      attributes.put(param, values[0]);
86                  }
87                  else {
88                      attributes.put(param, values);
89                  }
90              }
91          }
92  
93          serviceContext.setAttributes(attributes);
94  
95          // Command
96  
97          String cmd = ParamUtil.getString(portletRequest, Constants.CMD);
98  
99          serviceContext.setCommand(cmd);
100 
101         // Expando
102 
103         Map<String, Serializable> expandoBridgeAttributes =
104             PortalUtil.getExpandoBridgeAttributes(
105                 ExpandoBridgeFactoryUtil.getExpandoBridge(
106                     themeDisplay.getCompanyId(), className),
107                 portletRequest);
108 
109         serviceContext.setExpandoBridgeAttributes(expandoBridgeAttributes);
110 
111         // Permissions
112 
113         boolean addCommunityPermissions = ParamUtil.getBoolean(
114             portletRequest, "addCommunityPermissions");
115         boolean addGuestPermissions = ParamUtil.getBoolean(
116             portletRequest, "addGuestPermissions");
117         String[] communityPermissions = PortalUtil.getCommunityPermissions(
118             portletRequest);
119         String[] guestPermissions = PortalUtil.getGuestPermissions(
120             portletRequest);
121 
122         serviceContext.setAddCommunityPermissions(addCommunityPermissions);
123         serviceContext.setAddGuestPermissions(addGuestPermissions);
124         serviceContext.setCommunityPermissions(communityPermissions);
125         serviceContext.setGuestPermissions(guestPermissions);
126 
127         // Portlet preferences ids
128 
129         HttpServletRequest request = PortalUtil.getHttpServletRequest(
130             portletRequest);
131 
132         String portletId = PortalUtil.getPortletId(portletRequest);
133 
134         PortletPreferencesIds portletPreferencesIds =
135             PortletPreferencesFactoryUtil.getPortletPreferencesIds(
136                 request, portletId);
137 
138         serviceContext.setPortletPreferencesIds(portletPreferencesIds);
139 
140         // Asset
141 
142         long[] assetCategoryIds = StringUtil.split(
143             ParamUtil.getString(portletRequest, "assetCategoryIds"), 0L);
144         String[] assetTagNames = StringUtil.split(
145             ParamUtil.getString(portletRequest, "assetTagNames"));
146 
147         serviceContext.setAssetCategoryIds(assetCategoryIds);
148         serviceContext.setAssetTagNames(assetTagNames);
149 
150         // Workflow
151 
152         int status = ParamUtil.getInteger(portletRequest, "status");
153 
154         serviceContext.setStatus(status);
155 
156         return serviceContext;
157     }
158 
159 }