1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.util.ExpandoBridgeFactoryUtil;
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  public class ServiceContextFactory {
50  
51      public static ServiceContext getInstance(
52              String className, PortletRequest portletRequest)
53          throws PortalException, SystemException {
54  
55          ServiceContext serviceContext = new ServiceContext();
56  
57          // Theme display
58  
59          ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
60              WebKeys.THEME_DISPLAY);
61  
62          serviceContext.setCompanyId(themeDisplay.getCompanyId());
63          serviceContext.setLanguageId(themeDisplay.getLanguageId());
64          serviceContext.setLayoutFullURL(
65              PortalUtil.getLayoutFullURL(themeDisplay));
66          serviceContext.setLayoutURL(PortalUtil.getLayoutURL(themeDisplay));
67          serviceContext.setPathMain(PortalUtil.getPathMain());
68          serviceContext.setPlid(themeDisplay.getPlid());
69          serviceContext.setPortalURL(PortalUtil.getPortalURL(portletRequest));
70          serviceContext.setScopeGroupId(themeDisplay.getScopeGroupId());
71          serviceContext.setUserDisplayURL(
72              themeDisplay.getUser().getDisplayURL(themeDisplay));
73          serviceContext.setUserId(themeDisplay.getUserId());
74  
75          // Expando
76  
77          Map<String, Serializable> attributes =
78              PortalUtil.getExpandoBridgeAttributes(
79                  ExpandoBridgeFactoryUtil.getExpandoBridge(className),
80                  portletRequest);
81  
82          serviceContext.setExpandoBridgeAttributes(attributes);
83  
84          // Permissions
85  
86          boolean addCommunityPermissions = ParamUtil.getBoolean(
87              portletRequest, "addCommunityPermissions");
88          boolean addGuestPermissions = ParamUtil.getBoolean(
89              portletRequest, "addGuestPermissions");
90          String[] communityPermissions = PortalUtil.getCommunityPermissions(
91              portletRequest);
92          String[] guestPermissions = PortalUtil.getGuestPermissions(
93              portletRequest);
94  
95          serviceContext.setAddCommunityPermissions(addCommunityPermissions);
96          serviceContext.setAddGuestPermissions(addGuestPermissions);
97          serviceContext.setCommunityPermissions(communityPermissions);
98          serviceContext.setGuestPermissions(guestPermissions);
99  
100         // Portlet preferences ids
101 
102         HttpServletRequest request = PortalUtil.getHttpServletRequest(
103             portletRequest);
104 
105         String portletId = PortalUtil.getPortletId(portletRequest);
106 
107         PortletPreferencesIds portletPreferencesIds =
108             PortletPreferencesFactoryUtil.getPortletPreferencesIds(
109                 request, portletId);
110 
111         serviceContext.setPortletPreferencesIds(portletPreferencesIds);
112 
113         // Tags
114 
115         String[] tagsCategories = PortalUtil.getTagsCategories(portletRequest);
116         String[] tagsEntries = PortalUtil.getTagsEntries(portletRequest);
117 
118         serviceContext.setTagsCategories(tagsCategories);
119 
120         serviceContext.setTagsEntries(tagsEntries);
121 
122         return serviceContext;
123     }
124 
125 }