1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.service;
16  
17  import com.liferay.portal.kernel.exception.SystemException;
18  import com.liferay.portal.kernel.json.JSONObject;
19  import com.liferay.portal.kernel.util.LocaleUtil;
20  import com.liferay.portal.kernel.util.StringUtil;
21  import com.liferay.portal.model.PortletPreferencesIds;
22  
23  import java.util.Locale;
24  
25  import javax.portlet.PortletPreferences;
26  
27  /**
28   * <a href="ServiceContextUtil.java.html"><b><i>View Source</i></b></a>
29   *
30   * @author Raymond Augé
31   * @author Brian Wing Shun Chan
32   * @author Jorge Ferrer
33   */
34  public class ServiceContextUtil {
35  
36      public static Object deserialize(JSONObject jsonObject) {
37          ServiceContext serviceContext = new ServiceContext();
38  
39          // Theme display
40  
41          serviceContext.setCompanyId(jsonObject.getLong("companyId"));
42          serviceContext.setLayoutFullURL(jsonObject.getString("layoutFullURL"));
43          serviceContext.setLayoutURL(jsonObject.getString("layoutURL"));
44          serviceContext.setPathMain(jsonObject.getString("pathMain"));
45          serviceContext.setPlid(jsonObject.getLong("plid"));
46          serviceContext.setPortalURL(jsonObject.getString("portalURL"));
47          serviceContext.setScopeGroupId(jsonObject.getLong("scopeGroupId"));
48          serviceContext.setUserDisplayURL(
49              jsonObject.getString("userDisplayURL"));
50  
51          // Permissions
52  
53          String[] communityPermissions = StringUtil.split(
54              jsonObject.getString("communityPermissions"));
55          String[] guestPermissions = StringUtil.split(
56              jsonObject.getString("guestPermissions"));
57  
58          serviceContext.setAddCommunityPermissions(
59              jsonObject.getBoolean("addCommunityPermissions"));
60          serviceContext.setAddGuestPermissions(
61              jsonObject.getBoolean("addGuestPermissions"));
62          serviceContext.setCommunityPermissions(communityPermissions);
63          serviceContext.setGuestPermissions(guestPermissions);
64  
65          // Asset
66  
67          long[] assetCategoryIds = StringUtil.split(
68              jsonObject.getString("assetCategoryIds"), 0L);
69          String[] assetTagNames = StringUtil.split(
70              jsonObject.getString("assetTagNames"));
71  
72          serviceContext.setAssetCategoryIds(assetCategoryIds);
73          serviceContext.setAssetTagNames(assetTagNames);
74  
75          // Workflow
76  
77          serviceContext.setStatus(jsonObject.getInt("status"));
78  
79          return serviceContext;
80      }
81  
82      public static Locale getLocale(ServiceContext serviceContext) {
83          return LocaleUtil.fromLanguageId(serviceContext.getLanguageId());
84      }
85  
86      public static PortletPreferences getPortletPreferences(
87              ServiceContext serviceContext)
88          throws SystemException {
89  
90          if (serviceContext == null) {
91              return null;
92          }
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 }