com.liferay.portal.kernel.service.ServiceContextUtil.java Source code

Java tutorial

Introduction

Here is the source code for com.liferay.portal.kernel.service.ServiceContextUtil.java

Source

/**
 * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.liferay.portal.kernel.service;

import com.liferay.portal.kernel.json.JSONObject;
import com.liferay.portal.kernel.model.PortletPreferencesIds;
import com.liferay.portal.kernel.service.permission.ModelPermissionsFactory;
import com.liferay.portal.kernel.util.StringUtil;

import javax.portlet.PortletPreferences;

/**
 * @author Raymond Aug
 * @author Brian Wing Shun Chan
 * @author Jorge Ferrer
 */
public class ServiceContextUtil {

    public static Object deserialize(JSONObject jsonObject) {
        ServiceContext serviceContext = new ServiceContext();

        // Theme display

        serviceContext.setCompanyId(jsonObject.getLong("companyId"));
        serviceContext.setLayoutFullURL(jsonObject.getString("layoutFullURL"));
        serviceContext.setLayoutURL(jsonObject.getString("layoutURL"));
        serviceContext.setPathMain(jsonObject.getString("pathMain"));
        serviceContext.setPlid(jsonObject.getLong("plid"));
        serviceContext.setPortalURL(jsonObject.getString("portalURL"));
        serviceContext.setScopeGroupId(jsonObject.getLong("scopeGroupId"));
        serviceContext.setUserDisplayURL(jsonObject.getString("userDisplayURL"));
        serviceContext.setUserId(jsonObject.getLong("userId"));

        // Permissions

        serviceContext.setAddGroupPermissions(jsonObject.getBoolean("addGroupPermissions"));
        serviceContext.setAddGuestPermissions(jsonObject.getBoolean("addGuestPermissions"));

        String groupPermissions = jsonObject.getString("groupPermissions");
        String guestPermissions = jsonObject.getString("guestPermissions");

        if ((groupPermissions != null) || (guestPermissions != null)) {
            serviceContext.setModelPermissions(ModelPermissionsFactory.create(StringUtil.split(groupPermissions),
                    StringUtil.split(guestPermissions)));
        }

        // Asset

        long[] assetCategoryIds = StringUtil.split(jsonObject.getString("assetCategoryIds"), 0L);
        String[] assetTagNames = StringUtil.split(jsonObject.getString("assetTagNames"));

        serviceContext.setAssetCategoryIds(assetCategoryIds);
        serviceContext.setAssetTagNames(assetTagNames);

        // Workflow

        serviceContext.setWorkflowAction(jsonObject.getInt("workflowAction"));

        return serviceContext;
    }

    public static PortletPreferences getPortletPreferences(ServiceContext serviceContext) {

        if (serviceContext == null) {
            return null;
        }

        PortletPreferencesIds portletPreferencesIds = serviceContext.getPortletPreferencesIds();

        if (portletPreferencesIds == null) {
            return null;
        }

        return PortletPreferencesLocalServiceUtil.getPreferences(portletPreferencesIds.getCompanyId(),
                portletPreferencesIds.getOwnerId(), portletPreferencesIds.getOwnerType(),
                portletPreferencesIds.getPlid(), portletPreferencesIds.getPortletId());
    }

}