001    /**
002     * Copyright (c) 2000-2010 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    
049                    // Permissions
050    
051                    String[] communityPermissions = StringUtil.split(
052                            jsonObject.getString("communityPermissions"));
053                    String[] guestPermissions = StringUtil.split(
054                            jsonObject.getString("guestPermissions"));
055    
056                    serviceContext.setAddCommunityPermissions(
057                            jsonObject.getBoolean("addCommunityPermissions"));
058                    serviceContext.setAddGuestPermissions(
059                            jsonObject.getBoolean("addGuestPermissions"));
060                    serviceContext.setCommunityPermissions(communityPermissions);
061                    serviceContext.setGuestPermissions(guestPermissions);
062    
063                    // Asset
064    
065                    long[] assetCategoryIds = StringUtil.split(
066                            jsonObject.getString("assetCategoryIds"), 0L);
067                    String[] assetTagNames = StringUtil.split(
068                            jsonObject.getString("assetTagNames"));
069    
070                    serviceContext.setAssetCategoryIds(assetCategoryIds);
071                    serviceContext.setAssetTagNames(assetTagNames);
072    
073                    // Workflow
074    
075                    serviceContext.setWorkflowAction(jsonObject.getInt("workflowAction"));
076    
077                    return serviceContext;
078            }
079    
080            public static Locale getLocale(ServiceContext serviceContext) {
081                    return LocaleUtil.fromLanguageId(serviceContext.getLanguageId());
082            }
083    
084            public static PortletPreferences getPortletPreferences(
085                            ServiceContext serviceContext)
086                    throws SystemException {
087    
088                    if (serviceContext == null) {
089                            return null;
090                    }
091    
092                    PortletPreferencesIds portletPreferencesIds =
093                            serviceContext.getPortletPreferencesIds();
094    
095                    if (portletPreferencesIds == null) {
096                            return null;
097                    }
098                    else {
099                            return PortletPreferencesLocalServiceUtil.getPreferences(
100                                    portletPreferencesIds.getCompanyId(),
101                                    portletPreferencesIds.getOwnerId(),
102                                    portletPreferencesIds.getOwnerType(),
103                                    portletPreferencesIds.getPlid(),
104                                    portletPreferencesIds.getPortletId());
105                    }
106            }
107    
108    }