001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.service;
016    
017    import com.liferay.portal.kernel.json.JSONObject;
018    import com.liferay.portal.kernel.util.StringUtil;
019    import com.liferay.portal.model.PortletPreferencesIds;
020    
021    import javax.portlet.PortletPreferences;
022    
023    /**
024     * @author Raymond Aug??
025     * @author Brian Wing Shun Chan
026     * @author Jorge Ferrer
027     */
028    public class ServiceContextUtil {
029    
030            public static Object deserialize(JSONObject jsonObject) {
031                    ServiceContext serviceContext = new ServiceContext();
032    
033                    // Theme display
034    
035                    serviceContext.setCompanyId(jsonObject.getLong("companyId"));
036                    serviceContext.setLayoutFullURL(jsonObject.getString("layoutFullURL"));
037                    serviceContext.setLayoutURL(jsonObject.getString("layoutURL"));
038                    serviceContext.setPathMain(jsonObject.getString("pathMain"));
039                    serviceContext.setPlid(jsonObject.getLong("plid"));
040                    serviceContext.setPortalURL(jsonObject.getString("portalURL"));
041                    serviceContext.setScopeGroupId(jsonObject.getLong("scopeGroupId"));
042                    serviceContext.setUserDisplayURL(
043                            jsonObject.getString("userDisplayURL"));
044                    serviceContext.setUserId(jsonObject.getLong("userId"));
045    
046                    // Permissions
047    
048                    String[] groupPermissions = StringUtil.split(
049                            jsonObject.getString("groupPermissions"));
050                    String[] guestPermissions = StringUtil.split(
051                            jsonObject.getString("guestPermissions"));
052    
053                    serviceContext.setAddGroupPermissions(
054                            jsonObject.getBoolean("addGroupPermissions"));
055                    serviceContext.setAddGuestPermissions(
056                            jsonObject.getBoolean("addGuestPermissions"));
057                    serviceContext.setGroupPermissions(groupPermissions);
058                    serviceContext.setGuestPermissions(guestPermissions);
059    
060                    // Asset
061    
062                    long[] assetCategoryIds = StringUtil.split(
063                            jsonObject.getString("assetCategoryIds"), 0L);
064                    String[] assetTagNames = StringUtil.split(
065                            jsonObject.getString("assetTagNames"));
066    
067                    serviceContext.setAssetCategoryIds(assetCategoryIds);
068                    serviceContext.setAssetTagNames(assetTagNames);
069    
070                    // Workflow
071    
072                    serviceContext.setWorkflowAction(jsonObject.getInt("workflowAction"));
073    
074                    return serviceContext;
075            }
076    
077            public static PortletPreferences getPortletPreferences(
078                    ServiceContext serviceContext) {
079    
080                    if (serviceContext == null) {
081                            return null;
082                    }
083    
084                    PortletPreferencesIds portletPreferencesIds =
085                            serviceContext.getPortletPreferencesIds();
086    
087                    if (portletPreferencesIds == null) {
088                            return null;
089                    }
090                    else {
091                            return PortletPreferencesLocalServiceUtil.getPreferences(
092                                    portletPreferencesIds.getCompanyId(),
093                                    portletPreferencesIds.getOwnerId(),
094                                    portletPreferencesIds.getOwnerType(),
095                                    portletPreferencesIds.getPlid(),
096                                    portletPreferencesIds.getPortletId());
097                    }
098            }
099    
100    }