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.portlet.social.service.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.json.JSONArray;
020    import com.liferay.portal.kernel.json.JSONFactoryUtil;
021    import com.liferay.portal.kernel.json.JSONObject;
022    import com.liferay.portal.kernel.util.LocaleUtil;
023    import com.liferay.portal.security.auth.PrincipalException;
024    import com.liferay.portal.security.permission.PermissionChecker;
025    import com.liferay.portlet.social.model.SocialActivityCounterDefinition;
026    import com.liferay.portlet.social.model.SocialActivityDefinition;
027    import com.liferay.portlet.social.service.base.SocialActivitySettingServiceBaseImpl;
028    import com.liferay.portlet.social.util.comparator.SocialActivityDefinitionNameComparator;
029    
030    import java.util.Collections;
031    import java.util.List;
032    
033    /**
034     * @author Zsolt Berentey
035     */
036    public class SocialActivitySettingServiceImpl
037            extends SocialActivitySettingServiceBaseImpl {
038    
039            @Override
040            public SocialActivityDefinition getActivityDefinition(
041                            long groupId, String className, int activityType)
042                    throws PortalException, SystemException {
043    
044                    checkPermission(groupId);
045    
046                    return socialActivitySettingLocalService.getActivityDefinition(
047                            groupId, className, activityType);
048            }
049    
050            @Override
051            public List<SocialActivityDefinition> getActivityDefinitions(
052                            long groupId, String className)
053                    throws PortalException, SystemException {
054    
055                    checkPermission(groupId);
056    
057                    return socialActivitySettingLocalService.getActivityDefinitions(
058                            groupId, className);
059            }
060    
061            @Override
062            public JSONArray getJSONActivityDefinitions(long groupId, String className)
063                    throws PortalException, SystemException {
064    
065                    checkPermission(groupId);
066    
067                    JSONArray jsonArray = JSONFactoryUtil.createJSONArray();
068    
069                    List<SocialActivityDefinition> activityDefinitions =
070                            socialActivitySettingLocalService.getActivityDefinitions(
071                                    groupId, className);
072    
073                    Collections.sort(
074                            activityDefinitions,
075                            new SocialActivityDefinitionNameComparator(
076                                    LocaleUtil.getMostRelevantLocale()));
077    
078                    for (SocialActivityDefinition activityDefinition :
079                                    activityDefinitions) {
080    
081                            JSONObject activityDefinitionJSONObject =
082                                    JSONFactoryUtil.createJSONObject(
083                                            JSONFactoryUtil.looseSerialize(activityDefinition));
084    
085                            JSONArray activityCounterDefinitionsJSONArray =
086                                    JSONFactoryUtil.createJSONArray();
087    
088                            for (SocialActivityCounterDefinition activityCounterDefinition :
089                                            activityDefinition.getActivityCounterDefinitions()) {
090    
091                                    JSONObject activityCounterDefinitionJSONObject =
092                                            JSONFactoryUtil.createJSONObject(
093                                                    JSONFactoryUtil.looseSerialize(
094                                                            activityCounterDefinition));
095    
096                                    activityCounterDefinitionsJSONArray.put(
097                                            activityCounterDefinitionJSONObject);
098                            }
099    
100                            activityDefinitionJSONObject.put(
101                                    "counters", activityCounterDefinitionsJSONArray);
102    
103                            jsonArray.put(activityDefinitionJSONObject);
104                    }
105    
106                    return jsonArray;
107            }
108    
109            @Override
110            public void updateActivitySetting(
111                            long groupId, String className, boolean enabled)
112                    throws PortalException, SystemException {
113    
114                    checkPermission(groupId);
115    
116                    socialActivitySettingLocalService.updateActivitySetting(
117                            groupId, className, enabled);
118            }
119    
120            @Override
121            public void updateActivitySetting(
122                            long groupId, String className, int activityType,
123                            SocialActivityCounterDefinition activityCounterDefinition)
124                    throws PortalException, SystemException {
125    
126                    checkPermission(groupId);
127    
128                    socialActivitySettingLocalService.updateActivitySetting(
129                            groupId, className, activityType, activityCounterDefinition);
130            }
131    
132            @Override
133            public void updateActivitySettings(
134                            long groupId, String className, int activityType,
135                            List<SocialActivityCounterDefinition> activityCounterDefinitions)
136                    throws PortalException, SystemException {
137    
138                    checkPermission(groupId);
139    
140                    socialActivitySettingLocalService.updateActivitySettings(
141                            groupId, className, activityType, activityCounterDefinitions);
142            }
143    
144            protected void checkPermission(long groupId) throws PortalException {
145                    PermissionChecker permissionChecker = getPermissionChecker();
146    
147                    if (!permissionChecker.isGroupAdmin(groupId) &&
148                            !permissionChecker.isGroupOwner(groupId)) {
149    
150                            throw new PrincipalException();
151                    }
152            }
153    
154    }