1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.service;
16  
17  import com.liferay.portal.PortalException;
18  import com.liferay.portal.SystemException;
19  import com.liferay.portal.kernel.util.Constants;
20  import com.liferay.portal.kernel.util.ParamUtil;
21  import com.liferay.portal.kernel.util.WebKeys;
22  import com.liferay.portal.model.PortletPreferencesIds;
23  import com.liferay.portal.theme.ThemeDisplay;
24  import com.liferay.portal.util.PortalUtil;
25  import com.liferay.portlet.PortletPreferencesFactoryUtil;
26  import com.liferay.portlet.expando.util.ExpandoBridgeFactoryUtil;
27  
28  import java.io.Serializable;
29  
30  import java.util.Enumeration;
31  import java.util.HashMap;
32  import java.util.Map;
33  
34  import javax.portlet.PortletRequest;
35  
36  import javax.servlet.http.HttpServletRequest;
37  
38  /**
39   * <a href="ServiceContextFactory.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Brian Wing Shun Chan
42   * @author Raymond Augé
43   */
44  public class ServiceContextFactory {
45  
46      public static ServiceContext getInstance(
47              String className, PortletRequest portletRequest)
48          throws PortalException, SystemException {
49  
50          ServiceContext serviceContext = new ServiceContext();
51  
52          // Theme display
53  
54          ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
55              WebKeys.THEME_DISPLAY);
56  
57          serviceContext.setCompanyId(themeDisplay.getCompanyId());
58          serviceContext.setLanguageId(themeDisplay.getLanguageId());
59          serviceContext.setLayoutFullURL(
60              PortalUtil.getLayoutFullURL(themeDisplay));
61          serviceContext.setLayoutURL(PortalUtil.getLayoutURL(themeDisplay));
62          serviceContext.setPathMain(PortalUtil.getPathMain());
63          serviceContext.setPlid(themeDisplay.getPlid());
64          serviceContext.setPortalURL(PortalUtil.getPortalURL(portletRequest));
65          serviceContext.setScopeGroupId(themeDisplay.getScopeGroupId());
66          serviceContext.setUserDisplayURL(
67              themeDisplay.getUser().getDisplayURL(themeDisplay));
68          serviceContext.setUserId(themeDisplay.getUserId());
69  
70          // Attributes
71  
72          Map<String, Serializable> attributes =
73              new HashMap<String, Serializable>();
74  
75          Enumeration<String> enu = portletRequest.getParameterNames();
76  
77          while (enu.hasMoreElements()) {
78              String param = enu.nextElement();
79  
80              String[] values = portletRequest.getParameterValues(param);
81  
82              if ((values != null) && (values.length > 0)) {
83                  if (values.length == 1) {
84                      attributes.put(param, values[0]);
85                  }
86                  else {
87                      attributes.put(param, values);
88                  }
89              }
90          }
91  
92          serviceContext.setAttributes(attributes);
93  
94          // Command
95  
96          String cmd = ParamUtil.getString(portletRequest, Constants.CMD);
97  
98          serviceContext.setCommand(cmd);
99  
100         // Expando
101 
102         Map<String, Serializable> expandoBridgeAttributes =
103             PortalUtil.getExpandoBridgeAttributes(
104                 ExpandoBridgeFactoryUtil.getExpandoBridge(className),
105                 portletRequest);
106 
107         serviceContext.setExpandoBridgeAttributes(expandoBridgeAttributes);
108 
109         // Permissions
110 
111         boolean addCommunityPermissions = ParamUtil.getBoolean(
112             portletRequest, "addCommunityPermissions");
113         boolean addGuestPermissions = ParamUtil.getBoolean(
114             portletRequest, "addGuestPermissions");
115         String[] communityPermissions = PortalUtil.getCommunityPermissions(
116             portletRequest);
117         String[] guestPermissions = PortalUtil.getGuestPermissions(
118             portletRequest);
119 
120         serviceContext.setAddCommunityPermissions(addCommunityPermissions);
121         serviceContext.setAddGuestPermissions(addGuestPermissions);
122         serviceContext.setCommunityPermissions(communityPermissions);
123         serviceContext.setGuestPermissions(guestPermissions);
124 
125         // Portlet preferences ids
126 
127         HttpServletRequest request = PortalUtil.getHttpServletRequest(
128             portletRequest);
129 
130         String portletId = PortalUtil.getPortletId(portletRequest);
131 
132         PortletPreferencesIds portletPreferencesIds =
133             PortletPreferencesFactoryUtil.getPortletPreferencesIds(
134                 request, portletId);
135 
136         serviceContext.setPortletPreferencesIds(portletPreferencesIds);
137 
138         // Tags
139 
140         String[] tagsCategories = PortalUtil.getTagsCategories(portletRequest);
141         String[] tagsEntries = PortalUtil.getTagsEntries(portletRequest);
142 
143         serviceContext.setTagsCategories(tagsCategories);
144 
145         serviceContext.setTagsEntries(tagsEntries);
146 
147         return serviceContext;
148     }
149 
150 }