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.SystemException;
26  import com.liferay.portal.kernel.json.JSONObject;
27  import com.liferay.portal.kernel.util.LocaleUtil;
28  import com.liferay.portal.kernel.util.StringUtil;
29  import com.liferay.portal.model.PortletPreferencesIds;
30  
31  import java.util.Locale;
32  
33  import javax.portlet.PortletPreferences;
34  
35  /**
36   * <a href="ServiceContextUtil.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Raymond Augé
39   * @author Brian Wing Shun Chan
40   * @author Jorge Ferrer
41   *
42   */
43  public class ServiceContextUtil {
44  
45      public static Object deserialize(JSONObject jsonObject) {
46          ServiceContext serviceContext = new ServiceContext();
47  
48          // Theme display
49  
50          serviceContext.setCompanyId(jsonObject.getLong("companyId"));
51          serviceContext.setLayoutURL(jsonObject.getString("layoutURL"));
52          serviceContext.setPathMain(jsonObject.getString("pathMain"));
53          serviceContext.setPlid(jsonObject.getLong("plid"));
54          serviceContext.setPortalURL(jsonObject.getString("portalURL"));
55          serviceContext.setScopeGroupId(jsonObject.getLong("scopeGroupId"));
56          serviceContext.setUserDisplayURL(
57              jsonObject.getString("userDisplayURL"));
58  
59          // Permissions
60  
61          String[] communityPermissions = StringUtil.split(
62              jsonObject.getString("communityPermissions"));
63          String[] guestPermissions = StringUtil.split(
64              jsonObject.getString("guestPermissions"));
65  
66          serviceContext.setAddCommunityPermissions(
67              jsonObject.getBoolean("addCommunityPermissions"));
68          serviceContext.setAddGuestPermissions(
69              jsonObject.getBoolean("addGuestPermissions"));
70          serviceContext.setCommunityPermissions(communityPermissions);
71          serviceContext.setGuestPermissions(guestPermissions);
72  
73          // Tags
74  
75          String[] tagsCategories = StringUtil.split(
76              jsonObject.getString("tagsCategories"));
77          String[] tagsEntries = StringUtil.split(
78              jsonObject.getString("tagsEntries"));
79  
80          serviceContext.setTagsCategories(tagsCategories);
81          serviceContext.setTagsEntries(tagsEntries);
82  
83          return serviceContext;
84      }
85  
86      public static Locale getLocale(ServiceContext serviceContext) {
87          return LocaleUtil.fromLanguageId(serviceContext.getLanguageId());
88      }
89  
90      public static PortletPreferences getPortletPreferences(
91              ServiceContext serviceContext)
92          throws SystemException {
93  
94          PortletPreferencesIds portletPreferencesIds =
95              serviceContext.getPortletPreferencesIds();
96  
97          if (portletPreferencesIds == null) {
98              return null;
99          }
100         else {
101             return PortletPreferencesLocalServiceUtil.getPreferences(
102                 portletPreferencesIds.getCompanyId(),
103                 portletPreferencesIds.getOwnerId(),
104                 portletPreferencesIds.getOwnerType(),
105                 portletPreferencesIds.getPlid(),
106                 portletPreferencesIds.getPortletId());
107         }
108     }
109 
110 }