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.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  public class ServiceContextUtil {
43  
44      public static Object deserialize(JSONObject jsonObject) {
45          ServiceContext serviceContext = new ServiceContext();
46  
47          // Theme display
48  
49          serviceContext.setCompanyId(jsonObject.getLong("companyId"));
50          serviceContext.setLayoutFullURL(jsonObject.getString("layoutFullURL"));
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          if (serviceContext == null) {
95              return null;
96          }
97  
98          PortletPreferencesIds portletPreferencesIds =
99              serviceContext.getPortletPreferencesIds();
100 
101         if (portletPreferencesIds == null) {
102             return null;
103         }
104         else {
105             return PortletPreferencesLocalServiceUtil.getPreferences(
106                 portletPreferencesIds.getCompanyId(),
107                 portletPreferencesIds.getOwnerId(),
108                 portletPreferencesIds.getOwnerType(),
109                 portletPreferencesIds.getPlid(),
110                 portletPreferencesIds.getPortletId());
111         }
112     }
113 
114 }