001    /**
002     * Copyright (c) 2000-2013 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.exception.SystemException;
018    import com.liferay.portal.kernel.json.JSONObject;
019    import com.liferay.portal.kernel.util.StringUtil;
020    import com.liferay.portal.model.PortletPreferencesIds;
021    
022    import javax.portlet.PortletPreferences;
023    
024    /**
025     * @author Raymond Aug??
026     * @author Brian Wing Shun Chan
027     * @author Jorge Ferrer
028     */
029    public class ServiceContextUtil {
030    
031            public static Object deserialize(JSONObject jsonObject) {
032                    ServiceContext serviceContext = new ServiceContext();
033    
034                    // Theme display
035    
036                    serviceContext.setCompanyId(jsonObject.getLong("companyId"));
037                    serviceContext.setLayoutFullURL(jsonObject.getString("layoutFullURL"));
038                    serviceContext.setLayoutURL(jsonObject.getString("layoutURL"));
039                    serviceContext.setPathMain(jsonObject.getString("pathMain"));
040                    serviceContext.setPlid(jsonObject.getLong("plid"));
041                    serviceContext.setPortalURL(jsonObject.getString("portalURL"));
042                    serviceContext.setScopeGroupId(jsonObject.getLong("scopeGroupId"));
043                    serviceContext.setUserDisplayURL(
044                            jsonObject.getString("userDisplayURL"));
045                    serviceContext.setUserId(jsonObject.getLong("userId"));
046    
047                    // Permissions
048    
049                    String[] groupPermissions = StringUtil.split(
050                            jsonObject.getString("groupPermissions"));
051                    String[] guestPermissions = StringUtil.split(
052                            jsonObject.getString("guestPermissions"));
053    
054                    serviceContext.setAddGroupPermissions(
055                            jsonObject.getBoolean("addGroupPermissions"));
056                    serviceContext.setAddGuestPermissions(
057                            jsonObject.getBoolean("addGuestPermissions"));
058                    serviceContext.setGroupPermissions(groupPermissions);
059                    serviceContext.setGuestPermissions(guestPermissions);
060    
061                    // Asset
062    
063                    long[] assetCategoryIds = StringUtil.split(
064                            jsonObject.getString("assetCategoryIds"), 0L);
065                    String[] assetTagNames = StringUtil.split(
066                            jsonObject.getString("assetTagNames"));
067    
068                    serviceContext.setAssetCategoryIds(assetCategoryIds);
069                    serviceContext.setAssetTagNames(assetTagNames);
070    
071                    // Workflow
072    
073                    serviceContext.setWorkflowAction(jsonObject.getInt("workflowAction"));
074    
075                    return serviceContext;
076            }
077    
078            public static PortletPreferences getPortletPreferences(
079                            ServiceContext serviceContext)
080                    throws SystemException {
081    
082                    if (serviceContext == null) {
083                            return null;
084                    }
085    
086                    PortletPreferencesIds portletPreferencesIds =
087                            serviceContext.getPortletPreferencesIds();
088    
089                    if (portletPreferencesIds == null) {
090                            return null;
091                    }
092                    else {
093                            return PortletPreferencesLocalServiceUtil.getPreferences(
094                                    portletPreferencesIds.getCompanyId(),
095                                    portletPreferencesIds.getOwnerId(),
096                                    portletPreferencesIds.getOwnerType(),
097                                    portletPreferencesIds.getPlid(),
098                                    portletPreferencesIds.getPortletId());
099                    }
100            }
101    
102    }