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.kernel.workflow.StatusConstants;
24  import com.liferay.portal.model.PortletPreferencesIds;
25  import com.liferay.portal.theme.ThemeDisplay;
26  import com.liferay.portal.util.PortalUtil;
27  import com.liferay.portlet.PortletPreferencesFactoryUtil;
28  import com.liferay.portlet.expando.util.ExpandoBridgeFactoryUtil;
29  
30  import java.io.Serializable;
31  
32  import java.util.Enumeration;
33  import java.util.HashMap;
34  import java.util.Map;
35  
36  import javax.portlet.PortletRequest;
37  
38  import javax.servlet.http.HttpServletRequest;
39  
40  /**
41   * <a href="ServiceContextFactory.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Brian Wing Shun Chan
44   * @author Raymond Augé
45   */
46  public class ServiceContextFactory {
47  
48      public static ServiceContext getInstance(
49              String className, PortletRequest portletRequest)
50          throws PortalException, SystemException {
51  
52          ServiceContext serviceContext = new ServiceContext();
53  
54          // Theme display
55  
56          ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
57              WebKeys.THEME_DISPLAY);
58  
59          serviceContext.setCompanyId(themeDisplay.getCompanyId());
60          serviceContext.setLanguageId(themeDisplay.getLanguageId());
61          serviceContext.setLayoutFullURL(
62              PortalUtil.getLayoutFullURL(themeDisplay));
63          serviceContext.setLayoutURL(PortalUtil.getLayoutURL(themeDisplay));
64          serviceContext.setPathMain(PortalUtil.getPathMain());
65          serviceContext.setPlid(themeDisplay.getPlid());
66          serviceContext.setPortalURL(PortalUtil.getPortalURL(portletRequest));
67          serviceContext.setScopeGroupId(themeDisplay.getScopeGroupId());
68          serviceContext.setUserDisplayURL(
69              themeDisplay.getUser().getDisplayURL(themeDisplay));
70          serviceContext.setUserId(themeDisplay.getUserId());
71  
72          // Attributes
73  
74          Map<String, Serializable> attributes =
75              new HashMap<String, Serializable>();
76  
77          Enumeration<String> enu = portletRequest.getParameterNames();
78  
79          while (enu.hasMoreElements()) {
80              String param = enu.nextElement();
81  
82              String[] values = portletRequest.getParameterValues(param);
83  
84              if ((values != null) && (values.length > 0)) {
85                  if (values.length == 1) {
86                      attributes.put(param, values[0]);
87                  }
88                  else {
89                      attributes.put(param, values);
90                  }
91              }
92          }
93  
94          serviceContext.setAttributes(attributes);
95  
96          // Command
97  
98          String cmd = ParamUtil.getString(portletRequest, Constants.CMD);
99  
100         serviceContext.setCommand(cmd);
101 
102         // Expando
103 
104         Map<String, Serializable> expandoBridgeAttributes =
105             PortalUtil.getExpandoBridgeAttributes(
106                 ExpandoBridgeFactoryUtil.getExpandoBridge(
107                     themeDisplay.getCompanyId(), className),
108                 portletRequest);
109 
110         serviceContext.setExpandoBridgeAttributes(expandoBridgeAttributes);
111 
112         // Permissions
113 
114         boolean addCommunityPermissions = ParamUtil.getBoolean(
115             portletRequest, "addCommunityPermissions");
116         boolean addGuestPermissions = ParamUtil.getBoolean(
117             portletRequest, "addGuestPermissions");
118         String[] communityPermissions = PortalUtil.getCommunityPermissions(
119             portletRequest);
120         String[] guestPermissions = PortalUtil.getGuestPermissions(
121             portletRequest);
122 
123         serviceContext.setAddCommunityPermissions(addCommunityPermissions);
124         serviceContext.setAddGuestPermissions(addGuestPermissions);
125         serviceContext.setCommunityPermissions(communityPermissions);
126         serviceContext.setGuestPermissions(guestPermissions);
127 
128         // Portlet preferences ids
129 
130         HttpServletRequest request = PortalUtil.getHttpServletRequest(
131             portletRequest);
132 
133         String portletId = PortalUtil.getPortletId(portletRequest);
134 
135         PortletPreferencesIds portletPreferencesIds =
136             PortletPreferencesFactoryUtil.getPortletPreferencesIds(
137                 request, portletId);
138 
139         serviceContext.setPortletPreferencesIds(portletPreferencesIds);
140 
141         // Asset
142 
143         long[] assetCategoryIds = StringUtil.split(
144             ParamUtil.getString(portletRequest, "assetCategoryIds"), 0L);
145         String[] assetTagNames = StringUtil.split(
146             ParamUtil.getString(portletRequest, "assetTagNames"));
147 
148         serviceContext.setAssetCategoryIds(assetCategoryIds);
149         serviceContext.setAssetTagNames(assetTagNames);
150 
151         // Workflow
152 
153         serviceContext.setStartWorkflow(true);
154 
155         int status = ParamUtil.getInteger(
156             portletRequest, "status", StatusConstants.APPROVED);
157 
158         serviceContext.setStatus(status);
159 
160         return serviceContext;
161     }
162 
163 }