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