1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.service;
24  
25  import com.liferay.portal.PortalException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.util.ParamUtil;
28  import com.liferay.portal.kernel.util.WebKeys;
29  import com.liferay.portal.model.PortletPreferencesIds;
30  import com.liferay.portal.theme.ThemeDisplay;
31  import com.liferay.portal.util.PortalUtil;
32  import com.liferay.portlet.PortletPreferencesFactoryUtil;
33  import com.liferay.portlet.expando.model.impl.ExpandoBridgeImpl;
34  
35  import java.io.Serializable;
36  
37  import java.util.Map;
38  
39  import javax.portlet.PortletRequest;
40  
41  import javax.servlet.http.HttpServletRequest;
42  
43  /**
44   * <a href="ServiceContextFactory.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Brian Wing Shun Chan
47   * @author Raymond Augé
48   *
49   */
50  public class ServiceContextFactory {
51  
52      public static ServiceContext getInstance(
53              String className, PortletRequest portletRequest)
54          throws PortalException, SystemException {
55  
56          ServiceContext serviceContext = new ServiceContext();
57  
58          // Theme display
59  
60          ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
61              WebKeys.THEME_DISPLAY);
62  
63          serviceContext.setCompanyId(themeDisplay.getCompanyId());
64          serviceContext.setLanguageId(themeDisplay.getLanguageId());
65          serviceContext.setLayoutURL(PortalUtil.getLayoutURL(themeDisplay));
66          serviceContext.setPathMain(PortalUtil.getPathMain());
67          serviceContext.setPlid(themeDisplay.getPlid());
68          serviceContext.setPortalURL(PortalUtil.getPortalURL(portletRequest));
69          serviceContext.setScopeGroupId(themeDisplay.getScopeGroupId());
70          serviceContext.setUserDisplayURL(
71              themeDisplay.getUser().getDisplayURL(themeDisplay));
72          serviceContext.setUserId(themeDisplay.getUserId());
73  
74          // Expando
75  
76          Map<String, Serializable> attributes =
77              PortalUtil.getExpandoBridgeAttributes(
78                  new ExpandoBridgeImpl(className, 0), portletRequest);
79  
80          serviceContext.setExpandoBridgeAttributes(attributes);
81  
82          // Permissions
83  
84          boolean addCommunityPermissions = ParamUtil.getBoolean(
85              portletRequest, "addCommunityPermissions");
86          boolean addGuestPermissions = ParamUtil.getBoolean(
87              portletRequest, "addGuestPermissions");
88          String[] communityPermissions = portletRequest.getParameterValues(
89              "communityPermissions");
90          String[] guestPermissions = portletRequest.getParameterValues(
91              "guestPermissions");
92  
93          serviceContext.setAddCommunityPermissions(addCommunityPermissions);
94          serviceContext.setAddGuestPermissions(addGuestPermissions);
95          serviceContext.setCommunityPermissions(communityPermissions);
96          serviceContext.setGuestPermissions(guestPermissions);
97  
98          // Portlet preferences ids
99  
100         HttpServletRequest request = PortalUtil.getHttpServletRequest(
101             portletRequest);
102 
103         String portletId = PortalUtil.getPortletId(portletRequest);
104 
105         PortletPreferencesIds portletPreferencesIds =
106             PortletPreferencesFactoryUtil.getPortletPreferencesIds(
107                 request, portletId);
108 
109         serviceContext.setPortletPreferencesIds(portletPreferencesIds);
110 
111         // Tags
112 
113         String[] tagsCategories = PortalUtil.getTagsCategories(portletRequest);
114         String[] tagsEntries = PortalUtil.getTagsEntries(portletRequest);
115 
116         serviceContext.setTagsCategories(tagsCategories);
117 
118         serviceContext.setTagsEntries(tagsEntries);
119 
120         return serviceContext;
121     }
122 
123 }