1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.service;
21  
22  import com.liferay.portal.PortalException;
23  import com.liferay.portal.SystemException;
24  import com.liferay.portal.kernel.util.ParamUtil;
25  import com.liferay.portal.kernel.util.WebKeys;
26  import com.liferay.portal.model.PortletPreferencesIds;
27  import com.liferay.portal.theme.ThemeDisplay;
28  import com.liferay.portal.util.PortalUtil;
29  import com.liferay.portlet.PortletPreferencesFactoryUtil;
30  import com.liferay.portlet.expando.model.impl.ExpandoBridgeImpl;
31  
32  import java.io.Serializable;
33  
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   */
47  public class ServiceContextFactory {
48  
49      public static ServiceContext getInstance(
50              String className, PortletRequest portletRequest)
51          throws PortalException, SystemException {
52  
53          ServiceContext serviceContext = new ServiceContext();
54  
55          // Theme display
56  
57          ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
58              WebKeys.THEME_DISPLAY);
59  
60          serviceContext.setCompanyId(themeDisplay.getCompanyId());
61          serviceContext.setLanguageId(themeDisplay.getLanguageId());
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          // Expando
72  
73          Map<String, Serializable> attributes =
74              PortalUtil.getExpandoBridgeAttributes(
75                  new ExpandoBridgeImpl(className, 0), portletRequest);
76  
77          serviceContext.setExpandoBridgeAttributes(attributes);
78  
79          // Permissions
80  
81          boolean addCommunityPermissions = ParamUtil.getBoolean(
82              portletRequest, "addCommunityPermissions");
83          boolean addGuestPermissions = ParamUtil.getBoolean(
84              portletRequest, "addGuestPermissions");
85          String[] communityPermissions = PortalUtil.getCommunityPermissions(
86              portletRequest);
87          String[] guestPermissions = PortalUtil.getGuestPermissions(
88              portletRequest);
89  
90          serviceContext.setAddCommunityPermissions(addCommunityPermissions);
91          serviceContext.setAddGuestPermissions(addGuestPermissions);
92          serviceContext.setCommunityPermissions(communityPermissions);
93          serviceContext.setGuestPermissions(guestPermissions);
94  
95          // Portlet preferences ids
96  
97          HttpServletRequest request = PortalUtil.getHttpServletRequest(
98              portletRequest);
99  
100         String portletId = PortalUtil.getPortletId(portletRequest);
101 
102         PortletPreferencesIds portletPreferencesIds =
103             PortletPreferencesFactoryUtil.getPortletPreferencesIds(
104                 request, portletId);
105 
106         serviceContext.setPortletPreferencesIds(portletPreferencesIds);
107 
108         // Tags
109 
110         String[] tagsCategories = PortalUtil.getTagsCategories(portletRequest);
111         String[] tagsEntries = PortalUtil.getTagsEntries(portletRequest);
112 
113         serviceContext.setTagsCategories(tagsCategories);
114 
115         serviceContext.setTagsEntries(tagsEntries);
116 
117         return serviceContext;
118     }
119 
120 }