001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.LocaleThreadLocal;
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 edu.emory.mathcs.backport.java.util.Collections;
031    
032    import java.util.List;
033    
034    /**
035     * @author Zsolt Berentey
036     */
037    public class SocialActivitySettingServiceImpl
038            extends SocialActivitySettingServiceBaseImpl {
039    
040            public SocialActivityDefinition getActivityDefinition(
041                            long groupId, String className, int activityType)
042                    throws PortalException, SystemException {
043    
044                    PermissionChecker permissionChecker = getPermissionChecker();
045    
046                    if (!permissionChecker.isGroupAdmin(groupId)) {
047                            throw new PrincipalException();
048                    }
049    
050                    return socialActivitySettingLocalService.getActivityDefinition(
051                            groupId, className, activityType);
052            }
053    
054            public List<SocialActivityDefinition> getActivityDefinitions(
055                            long groupId, String className)
056                    throws PortalException, SystemException {
057    
058                    PermissionChecker permissionChecker = getPermissionChecker();
059    
060                    if (!permissionChecker.isGroupAdmin(groupId)) {
061                            throw new PrincipalException();
062                    }
063    
064                    return socialActivitySettingLocalService.getActivityDefinitions(
065                            groupId, className);
066            }
067    
068            public JSONArray getJSONActivityDefinitions(
069                            long groupId, String className)
070                    throws PortalException, SystemException {
071    
072                    JSONArray jsonArray = JSONFactoryUtil.createJSONArray();
073    
074                    List<SocialActivityDefinition> activityDefinitions =
075                            socialActivitySettingLocalService.getActivityDefinitions(
076                                    groupId, className);
077    
078                    Collections.sort(
079                            activityDefinitions,
080                            new SocialActivityDefinitionNameComparator(
081                                    LocaleThreadLocal.getThemeDisplayLocale()));
082    
083                    for (SocialActivityDefinition activityDefinition :
084                                    activityDefinitions) {
085    
086                            JSONObject activityDefinitionJSONObject =
087                                    JSONFactoryUtil.createJSONObject(
088                                            JSONFactoryUtil.looseSerialize(activityDefinition));
089    
090                            JSONArray activityCounterDefinitionsJSONArray =
091                                    JSONFactoryUtil.createJSONArray();
092    
093                            for (SocialActivityCounterDefinition activityCounterDefinition :
094                                            activityDefinition.getActivityCounterDefinitions()) {
095    
096                                    JSONObject activityCounterDefinitionJSONObject =
097                                            JSONFactoryUtil.createJSONObject(
098                                                    JSONFactoryUtil.looseSerialize(
099                                                            activityCounterDefinition));
100    
101                                    activityCounterDefinitionsJSONArray.put(
102                                            activityCounterDefinitionJSONObject);
103                            }
104    
105                            activityDefinitionJSONObject.put(
106                                    "counters", activityCounterDefinitionsJSONArray);
107    
108                            jsonArray.put(activityDefinitionJSONObject);
109                    }
110    
111                    return jsonArray;
112            }
113    
114            public void updateActivitySetting(
115                            long groupId, String className, boolean enabled)
116                    throws PortalException, SystemException {
117    
118                    PermissionChecker permissionChecker = getPermissionChecker();
119    
120                    if (!permissionChecker.isGroupAdmin(groupId)) {
121                            throw new PrincipalException();
122                    }
123    
124                    socialActivitySettingLocalService.updateActivitySetting(
125                            groupId, className, enabled);
126            }
127    
128            public void updateActivitySetting(
129                            long groupId, String className, int activityType,
130                            SocialActivityCounterDefinition activityCounterDefinition)
131                    throws PortalException, SystemException {
132    
133                    PermissionChecker permissionChecker = getPermissionChecker();
134    
135                    if (!permissionChecker.isGroupAdmin(groupId)) {
136                            throw new PrincipalException();
137                    }
138    
139                    socialActivitySettingLocalService.updateActivitySetting(
140                            groupId, className, activityType, activityCounterDefinition);
141            }
142    
143            public void updateActivitySettings(
144                            long groupId, String className, int activityType,
145                            List<SocialActivityCounterDefinition> activityCounterDefinitions)
146                    throws PortalException, SystemException {
147    
148                    PermissionChecker permissionChecker = getPermissionChecker();
149    
150                    if (!permissionChecker.isGroupAdmin(groupId)) {
151                            throw new PrincipalException();
152                    }
153    
154                    socialActivitySettingLocalService.updateActivitySettings(
155                            groupId, className, activityType, activityCounterDefinitions);
156            }
157    
158    }