1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.service;
21  
22  import com.liferay.portal.SystemException;
23  import com.liferay.portal.kernel.json.JSONObject;
24  import com.liferay.portal.kernel.util.LocaleUtil;
25  import com.liferay.portal.kernel.util.StringUtil;
26  import com.liferay.portal.model.PortletPreferencesIds;
27  
28  import java.util.Locale;
29  
30  import javax.portlet.PortletPreferences;
31  
32  /**
33   * <a href="ServiceContextUtil.java.html"><b><i>View Source</i></b></a>
34   *
35   * @author Raymond Augé
36   * @author Brian Wing Shun Chan
37   * @author Jorge Ferrer
38   *
39   */
40  public class ServiceContextUtil {
41  
42      public static Object deserialize(JSONObject jsonObject) {
43          ServiceContext serviceContext = new ServiceContext();
44  
45          // Theme display
46  
47          serviceContext.setCompanyId(jsonObject.getLong("companyId"));
48          serviceContext.setLayoutURL(jsonObject.getString("layoutURL"));
49          serviceContext.setPathMain(jsonObject.getString("pathMain"));
50          serviceContext.setPlid(jsonObject.getLong("plid"));
51          serviceContext.setPortalURL(jsonObject.getString("portalURL"));
52          serviceContext.setScopeGroupId(jsonObject.getLong("scopeGroupId"));
53          serviceContext.setUserDisplayURL(
54              jsonObject.getString("userDisplayURL"));
55  
56          // Permissions
57  
58          String[] communityPermissions = StringUtil.split(
59              jsonObject.getString("communityPermissions"));
60          String[] guestPermissions = StringUtil.split(
61              jsonObject.getString("guestPermissions"));
62  
63          serviceContext.setAddCommunityPermissions(
64              jsonObject.getBoolean("addCommunityPermissions"));
65          serviceContext.setAddGuestPermissions(
66              jsonObject.getBoolean("addGuestPermissions"));
67          serviceContext.setCommunityPermissions(communityPermissions);
68          serviceContext.setGuestPermissions(guestPermissions);
69  
70          // Tags
71  
72          String[] tagsCategories = StringUtil.split(
73              jsonObject.getString("tagsCategories"));
74          String[] tagsEntries = StringUtil.split(
75              jsonObject.getString("tagsEntries"));
76  
77          serviceContext.setTagsCategories(tagsCategories);
78          serviceContext.setTagsEntries(tagsEntries);
79  
80          return serviceContext;
81      }
82  
83      public static Locale getLocale(ServiceContext serviceContext) {
84          return LocaleUtil.fromLanguageId(serviceContext.getLanguageId());
85      }
86  
87      public static PortletPreferences getPortletPreferences(
88              ServiceContext serviceContext)
89          throws SystemException {
90  
91          if (serviceContext == null) {
92              return null;
93          }
94  
95          PortletPreferencesIds portletPreferencesIds =
96              serviceContext.getPortletPreferencesIds();
97  
98          if (portletPreferencesIds == null) {
99              return null;
100         }
101         else {
102             return PortletPreferencesLocalServiceUtil.getPreferences(
103                 portletPreferencesIds.getCompanyId(),
104                 portletPreferencesIds.getOwnerId(),
105                 portletPreferencesIds.getOwnerType(),
106                 portletPreferencesIds.getPlid(),
107                 portletPreferencesIds.getPortletId());
108         }
109     }
110 
111 }