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.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.util.ListUtil;
020    import com.liferay.portal.kernel.util.MapUtil;
021    import com.liferay.portal.model.Group;
022    import com.liferay.portal.model.Organization;
023    import com.liferay.portal.model.UserGroup;
024    import com.liferay.portal.security.permission.ActionKeys;
025    import com.liferay.portal.security.permission.PermissionChecker;
026    import com.liferay.portal.service.ServiceContext;
027    import com.liferay.portal.service.base.GroupServiceBaseImpl;
028    import com.liferay.portal.service.permission.GroupPermissionUtil;
029    import com.liferay.portal.service.permission.PortalPermissionUtil;
030    import com.liferay.portal.service.permission.RolePermissionUtil;
031    
032    import java.util.Iterator;
033    import java.util.LinkedHashMap;
034    import java.util.List;
035    
036    /**
037     * @author Brian Wing Shun Chan
038     */
039    public class GroupServiceImpl extends GroupServiceBaseImpl {
040    
041            public Group addGroup(
042                            String name, String description, int type, String friendlyURL,
043                            boolean active, ServiceContext serviceContext)
044                    throws PortalException, SystemException {
045    
046                    PortalPermissionUtil.check(
047                            getPermissionChecker(), ActionKeys.ADD_COMMUNITY);
048    
049                    return groupLocalService.addGroup(
050                            getUserId(), null, 0, name, description, type, friendlyURL, active,
051                            serviceContext);
052            }
053    
054            public Group addGroup(
055                            long liveGroupId, String name, String description, int type,
056                            String friendlyURL, boolean active, ServiceContext serviceContext)
057                    throws PortalException, SystemException {
058    
059                    GroupPermissionUtil.check(
060                            getPermissionChecker(), liveGroupId, ActionKeys.UPDATE);
061    
062                    return groupLocalService.addGroup(
063                            getUserId(), null, 0, liveGroupId, name, description, type,
064                            friendlyURL, active, serviceContext);
065            }
066    
067            public void addRoleGroups(long roleId, long[] groupIds)
068                    throws PortalException, SystemException {
069    
070                    RolePermissionUtil.check(
071                            getPermissionChecker(), roleId, ActionKeys.UPDATE);
072    
073                    groupLocalService.addRoleGroups(roleId, groupIds);
074            }
075    
076            public void deleteGroup(long groupId)
077                    throws PortalException, SystemException {
078    
079                    GroupPermissionUtil.check(
080                            getPermissionChecker(), groupId, ActionKeys.DELETE);
081    
082                    groupLocalService.deleteGroup(groupId);
083            }
084    
085            public Group getGroup(long groupId)
086                    throws PortalException, SystemException {
087    
088                    return groupLocalService.getGroup(groupId);
089            }
090    
091            public Group getGroup(long companyId, String name)
092                    throws PortalException, SystemException {
093    
094                    return groupLocalService.getGroup(companyId, name);
095            }
096    
097            public List<Group> getManageableGroups(String actionId, int max)
098                    throws PortalException, SystemException {
099    
100                    PermissionChecker permissionChecker = getPermissionChecker();
101    
102                    if (permissionChecker.isCompanyAdmin()) {
103                            return groupLocalService.search(
104                                    permissionChecker.getCompanyId(), null, null, null, 0, max);
105                    }
106    
107                    List<Group> groups = userPersistence.getGroups(
108                            permissionChecker.getUserId(), 0, max);
109    
110                    groups = ListUtil.copy(groups);
111    
112                    Iterator<Group> itr = groups.iterator();
113    
114                    while (itr.hasNext()) {
115                            Group group = itr.next();
116    
117                            if (!GroupPermissionUtil.contains(
118                                            permissionChecker, group.getGroupId(), actionId)) {
119    
120                                    itr.remove();
121                            }
122                    }
123    
124                    return groups;
125            }
126    
127            public List<Group> getOrganizationsGroups(
128                    List<Organization> organizations) {
129    
130                    return groupLocalService.getOrganizationsGroups(organizations);
131            }
132    
133            public Group getUserGroup(long companyId, long userId)
134                    throws PortalException, SystemException {
135    
136                    return groupLocalService.getUserGroup(companyId, userId);
137            }
138    
139            public List<Group> getUserGroupsGroups(List<UserGroup> userGroups)
140                    throws PortalException, SystemException {
141    
142                    return groupLocalService.getUserGroupsGroups(userGroups);
143            }
144    
145            public List<Group> getUserOrganizationsGroups(
146                            long userId, int start, int end)
147                    throws PortalException, SystemException {
148    
149                    return groupLocalService.getUserOrganizationsGroups(userId, start, end);
150            }
151    
152            public boolean hasUserGroup(long userId, long groupId)
153                    throws SystemException {
154    
155                    return groupLocalService.hasUserGroup(userId, groupId);
156            }
157    
158            public List<Group> search(
159                            long companyId, String name, String description, String[] params,
160                            int start, int end)
161                    throws SystemException {
162    
163                    LinkedHashMap<String, Object> paramsObj = MapUtil.toLinkedHashMap(
164                            params);
165    
166                    return groupLocalService.search(
167                            companyId, name, description, paramsObj, start, end);
168            }
169    
170            public int searchCount(
171                            long companyId, String name, String description, String[] params)
172                    throws SystemException {
173    
174                    LinkedHashMap<String, Object> paramsObj = MapUtil.toLinkedHashMap(
175                            params);
176    
177                    return groupLocalService.searchCount(
178                            companyId, name, description, paramsObj);
179            }
180    
181            public void setRoleGroups(long roleId, long[] groupIds)
182                    throws PortalException, SystemException {
183    
184                    RolePermissionUtil.check(
185                            getPermissionChecker(), roleId, ActionKeys.UPDATE);
186    
187                    groupLocalService.setRoleGroups(roleId, groupIds);
188            }
189    
190            public void unsetRoleGroups(long roleId, long[] groupIds)
191                    throws PortalException, SystemException {
192    
193                    RolePermissionUtil.check(
194                            getPermissionChecker(), roleId, ActionKeys.UPDATE);
195    
196                    groupLocalService.unsetRoleGroups(roleId, groupIds);
197            }
198    
199            public Group updateFriendlyURL(long groupId, String friendlyURL)
200                    throws PortalException, SystemException {
201    
202                    GroupPermissionUtil.check(
203                            getPermissionChecker(), groupId, ActionKeys.UPDATE);
204    
205                    return groupLocalService.updateFriendlyURL(groupId, friendlyURL);
206            }
207    
208            public Group updateGroup(
209                            long groupId, String name, String description, int type,
210                            String friendlyURL, boolean active, ServiceContext serviceContext)
211                    throws PortalException, SystemException {
212    
213                    GroupPermissionUtil.check(
214                            getPermissionChecker(), groupId, ActionKeys.UPDATE);
215    
216                    return groupLocalService.updateGroup(
217                            groupId, name, description, type, friendlyURL, active,
218                            serviceContext);
219            }
220    
221            public Group updateGroup(long groupId, String typeSettings)
222                    throws PortalException, SystemException {
223    
224                    GroupPermissionUtil.check(
225                            getPermissionChecker(), groupId, ActionKeys.UPDATE);
226    
227                    return groupLocalService.updateGroup(groupId, typeSettings);
228            }
229    
230            public Group updateWorkflow(
231                            long groupId, boolean workflowEnabled, int workflowStages,
232                            String workflowRoleNames)
233                    throws PortalException, SystemException {
234    
235                    GroupPermissionUtil.check(
236                            getPermissionChecker(), groupId, ActionKeys.MANAGE_STAGING);
237    
238                    return groupLocalService.updateWorkflow(
239                            groupId, workflowEnabled, workflowStages, workflowRoleNames);
240            }
241    
242    }