001    /**
002     * Copyright (c) 2000-2012 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.dao.orm.QueryUtil;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.kernel.util.ArrayUtil;
021    import com.liferay.portal.kernel.util.ListUtil;
022    import com.liferay.portal.kernel.util.MapUtil;
023    import com.liferay.portal.kernel.util.UniqueList;
024    import com.liferay.portal.model.Group;
025    import com.liferay.portal.model.GroupConstants;
026    import com.liferay.portal.model.Organization;
027    import com.liferay.portal.model.Portlet;
028    import com.liferay.portal.model.User;
029    import com.liferay.portal.model.UserGroup;
030    import com.liferay.portal.security.auth.PrincipalException;
031    import com.liferay.portal.security.permission.ActionKeys;
032    import com.liferay.portal.security.permission.PermissionChecker;
033    import com.liferay.portal.security.permission.PermissionCheckerFactoryUtil;
034    import com.liferay.portal.service.ServiceContext;
035    import com.liferay.portal.service.base.GroupServiceBaseImpl;
036    import com.liferay.portal.service.permission.GroupPermissionUtil;
037    import com.liferay.portal.service.permission.PortalPermissionUtil;
038    import com.liferay.portal.service.permission.PortletPermissionUtil;
039    import com.liferay.portal.service.permission.RolePermissionUtil;
040    import com.liferay.portal.service.permission.UserPermissionUtil;
041    import com.liferay.portal.util.PropsValues;
042    
043    import java.util.ArrayList;
044    import java.util.Collection;
045    import java.util.Collections;
046    import java.util.Iterator;
047    import java.util.LinkedHashMap;
048    import java.util.List;
049    
050    /**
051     * The group remote service is responsible for accessing, creating, modifying
052     * and deleting groups. For more information on group services and Group, see
053     * {@link com.liferay.portal.service.impl.GroupLocalServiceImpl}.
054     *
055     * @author Brian Wing Shun Chan
056     */
057    public class GroupServiceImpl extends GroupServiceBaseImpl {
058    
059            /**
060             * Adds a group.
061             *
062             * @param  parentGroupId the primary key of the parent group
063             * @param  liveGroupId the primary key of the live group
064             * @param  name the entity's name
065             * @param  description the group's description (optionally
066             *         <code>null</code>)
067             * @param  type the group's type. For more information see {@link
068             *         com.liferay.portal.model.GroupConstants}
069             * @param  friendlyURL the group's friendlyURL (optionally
070             *         <code>null</code>)
071             * @param  site whether the group is to be associated with a main site
072             * @param  active whether the group is active
073             * @param  serviceContext the service context to be applied (optionally
074             *         <code>null</code>). Can set the asset category IDs and asset tag
075             *         names for the group, and can set whether the group is for staging
076             * @return the group
077             * @throws PortalException if the user did not have permission to add the
078             *         group, if a creator could not be found, if the group's
079             *         information was invalid, if a layout could not be found, or if a
080             *         valid friendly URL could not be created for the group
081             * @throws SystemException if a system exception occurred
082             */
083            public Group addGroup(
084                            long parentGroupId, long liveGroupId, String name,
085                            String description, int type, String friendlyURL, boolean site,
086                            boolean active, ServiceContext serviceContext)
087                    throws PortalException, SystemException {
088    
089                    if (!GroupPermissionUtil.contains(
090                                    getPermissionChecker(), parentGroupId,
091                                    ActionKeys.MANAGE_SUBGROUPS) &&
092                            !PortalPermissionUtil.contains(
093                                    getPermissionChecker(), ActionKeys.ADD_COMMUNITY)) {
094    
095                            throw new PrincipalException(
096                                    "User " + getUserId() + " does not have permissions to add " +
097                                            "a site with parent " + parentGroupId);
098                    }
099    
100                    return groupLocalService.addGroup(
101                            getUserId(), parentGroupId, null, 0, liveGroupId, name, description,
102                            type, friendlyURL, site, active, serviceContext);
103            }
104    
105            /**
106             * Adds the group using the group default live group ID.
107             *
108             * @param      parentGroupId the primary key of the parent group
109             * @param      name the entity's name
110             * @param      description the group's description (optionally
111             *             <code>null</code>)
112             * @param      type the group's type. For more information see {@link
113             *             com.liferay.portal.model.GroupConstants}
114             * @param      friendlyURL the group's friendlyURL
115             * @param      site whether the group is to be associated with a main site
116             * @param      active whether the group is active
117             * @param      serviceContext the service context to be applied (optionally
118             *             <code>null</code>). Can set asset category IDs and asset tag
119             *             names for the group, and can set whether the group is for
120             *             staging
121             * @return     the group
122             * @throws     PortalException if the user did not have permission to add
123             *             the group, if a creator could not be found, if the group's
124             *             information was invalid, if a layout could not be found, or
125             *             if a valid friendly URL could not be created for the group
126             * @throws     SystemException if a system exception occurred
127             * @deprecated {@link #addGroup(long, long, String, String, int, String,
128             *             boolean, boolean, ServiceContext)}
129             */
130            public Group addGroup(
131                            long parentGroupId, String name, String description, int type,
132                            String friendlyURL, boolean site, boolean active,
133                            ServiceContext serviceContext)
134                    throws PortalException, SystemException {
135    
136                    return addGroup(
137                            parentGroupId, GroupConstants.DEFAULT_LIVE_GROUP_ID, name,
138                            description, type, friendlyURL, site, active, serviceContext);
139            }
140    
141            /**
142             * @deprecated {@link #addGroup(long, String, String, int, String, boolean,
143             *             boolean, ServiceContext)}
144             */
145            public Group addGroup(
146                            String name, String description, int type, String friendlyURL,
147                            boolean site, boolean active, ServiceContext serviceContext)
148                    throws PortalException, SystemException {
149    
150                    return addGroup(
151                            GroupConstants.DEFAULT_PARENT_GROUP_ID, name, description, type,
152                            friendlyURL, site, active, serviceContext);
153            }
154    
155            /**
156             * Adds the groups to the role.
157             *
158             * @param  roleId the primary key of the role
159             * @param  groupIds the primary keys of the groups
160             * @throws PortalException if the user did not have permission to update the
161             *         role
162             * @throws SystemException if a system exception occurred
163             */
164            public void addRoleGroups(long roleId, long[] groupIds)
165                    throws PortalException, SystemException {
166    
167                    RolePermissionUtil.check(
168                            getPermissionChecker(), roleId, ActionKeys.UPDATE);
169    
170                    groupLocalService.addRoleGroups(roleId, groupIds);
171            }
172    
173            /**
174             * Deletes the group.
175             *
176             * <p>
177             * The group is unstaged and its assets and resources including layouts,
178             * membership requests, subscriptions, teams, blogs, bookmarks, calendar
179             * events, image gallery, journals, message boards, polls, shopping related
180             * entities, software catalog, and wikis are also deleted.
181             * </p>
182             *
183             * @param  groupId the primary key of the group
184             * @throws PortalException if the user did not have permission to delete the
185             *         group or its assets or resources, if a group with the primary key
186             *         could not be found, or if the group was a system group
187             * @throws SystemException if a system exception occurred
188             */
189            public void deleteGroup(long groupId)
190                    throws PortalException, SystemException {
191    
192                    GroupPermissionUtil.check(
193                            getPermissionChecker(), groupId, ActionKeys.DELETE);
194    
195                    groupLocalService.deleteGroup(groupId);
196            }
197    
198            /**
199             * Returns the group with the primary key.
200             *
201             * @param  groupId the primary key of the group
202             * @return the group with the primary key
203             * @throws PortalException if a group with the primary key could not be
204             *         found or if the current user did not have permission to view the
205             *         group
206             * @throws SystemException if a system exception occurred
207             */
208            public Group getGroup(long groupId)
209                    throws PortalException, SystemException {
210    
211                    GroupPermissionUtil.check(
212                            getPermissionChecker(), groupId, ActionKeys.VIEW);
213    
214                    return groupLocalService.getGroup(groupId);
215            }
216    
217            /**
218             * Returns the group with the name.
219             *
220             * @param  companyId the primary key of the company
221             * @param  name the group's name
222             * @return the group with the name
223             * @throws PortalException if a matching group could not be found or if the
224             *         current user did not have permission to view the group
225             * @throws SystemException if a system exception occurred
226             */
227            public Group getGroup(long companyId, String name)
228                    throws PortalException, SystemException {
229    
230                    Group group = groupLocalService.getGroup(companyId, name);
231    
232                    GroupPermissionUtil.check(
233                            getPermissionChecker(), group.getGroupId(), ActionKeys.VIEW);
234    
235                    return group;
236            }
237    
238            /**
239             * Returns a range of all the site groups for which the user has control
240             * panel access.
241             *
242             * @param  portlets the portlets to manage
243             * @param  max the upper bound of the range of groups to consider (not
244             *         inclusive)
245             * @return the range of site groups for which the user has Control Panel
246             *         access
247             * @throws PortalException if a portal exception occurred
248             * @throws SystemException if a system exception occurred
249             */
250            public List<Group> getManageableSites(Collection<Portlet> portlets, int max)
251                    throws PortalException, SystemException {
252    
253                    PermissionChecker permissionChecker = getPermissionChecker();
254    
255                    if (permissionChecker.isCompanyAdmin()) {
256                            LinkedHashMap<String, Object> params =
257                                    new LinkedHashMap<String, Object>();
258    
259                            params.put("site", Boolean.TRUE);
260    
261                            return groupLocalService.search(
262                                    permissionChecker.getCompanyId(), null, null, null, params,
263                                    true, 0, max);
264                    }
265    
266                    List<Group> groups = new UniqueList<Group>();
267    
268                    groups.addAll(
269                            userPersistence.getGroups(permissionChecker.getUserId(), 0, max));
270                    groups.addAll(
271                            getUserOrganizationsGroups(permissionChecker.getUserId(), 0, max));
272    
273                    List<UserGroup> userGroups = userPersistence.getUserGroups(
274                            permissionChecker.getUserId(), 0, max);
275    
276                    for (UserGroup userGroup : userGroups) {
277                            groups.addAll(
278                                    userGroupPersistence.getGroups(
279                                            userGroup.getUserGroupId(), 0, max));
280                    }
281    
282                    Iterator<Group> itr = groups.iterator();
283    
284                    while (itr.hasNext()) {
285                            Group group = itr.next();
286    
287                            if (!group.isSite() ||
288                                    !PortletPermissionUtil.contains(
289                                            permissionChecker, group.getGroupId(), 0L, portlets,
290                                            ActionKeys.ACCESS_IN_CONTROL_PANEL)) {
291    
292                                    itr.remove();
293                            }
294                    }
295    
296                    return groups;
297            }
298    
299            /**
300             * Returns the groups associated with the organizations.
301             *
302             * @param  organizations the organizations
303             * @return the groups associated with the organizations
304             * @throws PortalException if a portal exception occurred
305             * @throws SystemException if a system exception occurred
306             */
307            public List<Group> getOrganizationsGroups(List<Organization> organizations)
308                    throws PortalException, SystemException {
309    
310                    List<Group> groups = groupLocalService.getOrganizationsGroups(
311                            organizations);
312    
313                    return filterGroups(groups);
314            }
315    
316            /**
317             * Returns the group associated with the user.
318             *
319             * @param  companyId the primary key of the company
320             * @param  userId the primary key of the user
321             * @return the group associated with the user
322             * @throws PortalException if a matching group could not be found or if the
323             *         current user did not have permission to view the group
324             * @throws SystemException if a system exception occurred
325             */
326            public Group getUserGroup(long companyId, long userId)
327                    throws PortalException, SystemException {
328    
329                    Group group = groupLocalService.getUserGroup(companyId, userId);
330    
331                    GroupPermissionUtil.check(
332                            getPermissionChecker(), group.getGroupId(), ActionKeys.VIEW);
333    
334                    return group;
335            }
336    
337            /**
338             * Returns the groups associated with the user groups.
339             *
340             * @param  userGroups the user groups
341             * @return the groups associated with the user groups
342             * @throws PortalException if any one of the user group's group could not be
343             *         found
344             * @throws SystemException if a system exception occurred
345             */
346            public List<Group> getUserGroupsGroups(List<UserGroup> userGroups)
347                    throws PortalException, SystemException {
348    
349                    List<Group> groups = groupLocalService.getUserGroupsGroups(userGroups);
350    
351                    return filterGroups(groups);
352            }
353    
354            /**
355             * Returns the range of all groups associated with the user's organization
356             * groups, including the ancestors of the organization groups, unless portal
357             * property <code>organizations.membership.strict</code> is set to
358             * <code>true</code>.
359             *
360             * <p>
361             * Useful when paginating results. Returns a maximum of <code>end -
362             * start</code> instances. <code>start</code> and <code>end</code> are not
363             * primary keys, they are indexes in the result set. Thus, <code>0</code>
364             * refers to the first result in the set. Setting both <code>start</code>
365             * and <code>end</code> to {@link
366             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
367             * result set.
368             * </p>
369             *
370             * @param  userId the primary key of the user
371             * @param  start the lower bound of the range of groups to consider
372             * @param  end the upper bound of the range of groups to consider (not
373             *         inclusive)
374             * @return the range of groups associated with the user's organizations
375             * @throws PortalException if a user with the primary key could not be found
376             *         or if another portal exception occurred
377             * @throws SystemException if a system exception occurred
378             */
379            public List<Group> getUserOrganizationsGroups(
380                            long userId, int start, int end)
381                    throws PortalException, SystemException {
382    
383                    List<Group> groups = groupLocalService.getUserOrganizationsGroups(
384                            userId, start, end);
385    
386                    return filterGroups(groups);
387            }
388    
389            public List<Group> getUserPlaces(
390                            long userId, String[] classNames, boolean includeControlPanel,
391                            int max)
392                    throws PortalException, SystemException {
393    
394                    return getUserPlaces(
395                            userId, classNames, null, true, includeControlPanel,
396                            QueryUtil.ALL_POS, max);
397            }
398    
399            /**
400             * Returns the user's group &quot;places&quot; associated with the group
401             * entity class names, including the Control Panel group if the user is
402             * permitted to view the Control Panel.
403             *
404             * <p>
405             * <ul> <li> Class name &quot;User&quot; includes the user's layout set
406             * group. </li> <li> Class name &quot;Organization&quot; includes the user's
407             * immediate organization groups and inherited organization groups. </li>
408             * <li> Class name &quot;Group&quot; includes the user's immediate
409             * organization groups and site groups. </li> <li> A <code>classNames</code>
410             * value of <code>null</code> includes the user's layout set group,
411             * organization groups, inherited organization groups, and site groups.
412             * </li> </ul>
413             * </p>
414             *
415             * @param  userId the primary key of the user
416             * @param  classNames the group entity class names (optionally
417             *         <code>null</code>). For more information see {@link
418             *         #getUserPlaces(long, String[], int)}
419             * @param  max the maximum number of groups to return
420             * @return the user's group &quot;places&quot;
421             * @throws PortalException if a portal exception occurred
422             * @throws SystemException if a system exception occurred
423             */
424            public List<Group> getUserPlaces(long userId, String[] classNames, int max)
425                    throws PortalException, SystemException {
426    
427                    return getUserPlaces(userId, classNames, false, max);
428            }
429    
430            public List<Group> getUserPlaces(
431                            long userId, String[] classNames, String name, boolean active,
432                            boolean includeControlPanel, int start, int end)
433                    throws PortalException, SystemException {
434    
435                    User user = userPersistence.fetchByPrimaryKey(userId);
436    
437                    if (user.isDefaultUser()) {
438                            return Collections.emptyList();
439                    }
440    
441                    List<Group> userPlaces = new UniqueList<Group>();
442    
443                    if ((classNames == null) ||
444                            ArrayUtil.contains(classNames, Group.class.getName())) {
445    
446                            LinkedHashMap<String, Object> groupParams =
447                                    new LinkedHashMap<String, Object>();
448    
449                            groupParams.put("active", active);
450                            groupParams.put("usersGroups", new Long(userId));
451    
452                            userPlaces.addAll(
453                                    groupLocalService.search(
454                                            user.getCompanyId(), name, groupParams, start, end));
455                    }
456    
457                    if ((classNames == null) ||
458                            ArrayUtil.contains(classNames, Organization.class.getName())) {
459    
460                            List<Organization> userOrgs =
461                                    organizationLocalService.getOrganizations(
462                                            userId, start, end, null);
463    
464                            for (Organization organization : userOrgs) {
465                                    if (!organization.hasPrivateLayouts() &&
466                                            !organization.hasPublicLayouts()) {
467    
468                                            userPlaces.remove(organization.getGroup());
469                                    }
470                                    else {
471                                            userPlaces.add(0, organization.getGroup());
472                                    }
473    
474                                    if (!PropsValues.ORGANIZATIONS_MEMBERSHIP_STRICT) {
475                                            for (Organization ancestorOrganization :
476                                                            organization.getAncestors()) {
477    
478                                                    if (!organization.hasPrivateLayouts() &&
479                                                            !organization.hasPublicLayouts()) {
480    
481                                                            continue;
482                                                    }
483    
484                                                    userPlaces.add(0, ancestorOrganization.getGroup());
485                                            }
486                                    }
487                            }
488                    }
489    
490                    if ((classNames == null) ||
491                            ArrayUtil.contains(classNames, User.class.getName())) {
492    
493                            if (PropsValues.LAYOUT_USER_PRIVATE_LAYOUTS_ENABLED ||
494                                    PropsValues.LAYOUT_USER_PUBLIC_LAYOUTS_ENABLED) {
495    
496                                    Group userGroup = user.getGroup();
497    
498                                    userPlaces.add(0, userGroup);
499                            }
500                    }
501    
502                    PermissionChecker permissionChecker = getPermissionChecker();
503    
504                    if (permissionChecker.getUserId() != userId) {
505                            try {
506                                    permissionChecker = PermissionCheckerFactoryUtil.create(user);
507                            }
508                            catch (Exception e) {
509                                    throw new PrincipalException(e);
510                            }
511                    }
512    
513                    if (includeControlPanel &&
514                            PortalPermissionUtil.contains(
515                                    permissionChecker, ActionKeys.VIEW_CONTROL_PANEL)) {
516    
517                            Group controlPanelGroup = groupLocalService.getGroup(
518                                    user.getCompanyId(), GroupConstants.CONTROL_PANEL);
519    
520                            userPlaces.add(0, controlPanelGroup);
521                    }
522    
523                    if ((end != QueryUtil.ALL_POS) && (userPlaces.size() > end)) {
524                            if (start < 0) {
525                                    start = 0;
526                            }
527    
528                            userPlaces = ListUtil.subList(userPlaces, start, end);
529                    }
530    
531                    return Collections.unmodifiableList(userPlaces);
532            }
533    
534            /**
535             * Returns the guest or current user's group &quot;places&quot; associated
536             * with the group entity class names, including the Control Panel group if
537             * the user is permitted to view the Control Panel.
538             *
539             * <p>
540             * <ul> <li> Class name &quot;User&quot; includes the user's layout set
541             * group. </li> <li> Class name &quot;Organization&quot; includes the user's
542             * immediate organization groups and inherited organization groups. </li>
543             * <li> Class name &quot;Group&quot; includes the user's immediate
544             * organization groups and site groups. </li> <li> A <code>classNames</code>
545             * value of <code>null</code> includes the user's layout set group,
546             * organization groups, inherited organization groups, and site groups.
547             * </li> </ul>
548             * </p>
549             *
550             * @param  classNames the group entity class names (optionally
551             *         <code>null</code>). For more information see {@link
552             *         #getUserPlaces(String[], int)}
553             * @param  max the maximum number of groups to return
554             * @return the user's group &quot;places&quot;
555             * @throws PortalException if a portal exception occurred
556             * @throws SystemException if a system exception occurred
557             */
558            public List<Group> getUserPlaces(String[] classNames, int max)
559                    throws PortalException, SystemException {
560    
561                    return getUserPlaces(getGuestOrUserId(), classNames, false, max);
562            }
563    
564            /**
565             * Returns the number of the guest or current user's group
566             * &quot;places&quot; associated with the group entity class names,
567             * including the Control Panel group if the user is permitted to view the
568             * Control Panel.
569             *
570             * @return the number of user's group &quot;places&quot;
571             * @throws PortalException if a portal exception occurred
572             * @throws SystemException if a system exception occurred
573             */
574            public int getUserPlacesCount() throws PortalException, SystemException {
575                    List<Group> userPlaces = getUserPlaces(
576                            getGuestOrUserId(), null, true, QueryUtil.ALL_POS);
577    
578                    return userPlaces.size();
579            }
580    
581            /**
582             * Returns the guest or current user's layout set group, organization
583             * groups, inherited organization groups, and site groups.
584             *
585             * @return the user's layout set group, organization groups, and inherited
586             *         organization groups, and site groups
587             * @throws PortalException if a portal exception occurred
588             * @throws SystemException if a system exception occurred
589             */
590            public List<Group> getUserSites() throws PortalException, SystemException {
591                    return getUserPlaces(null, QueryUtil.ALL_POS);
592            }
593    
594            /**
595             * Returns <code>true</code> if the user is associated with the group,
596             * including the user's inherited organizations and user groups. System and
597             * staged groups are not included.
598             *
599             * @param  userId the primary key of the user
600             * @param  groupId the primary key of the group
601             * @return <code>true</code> if the user is associated with the group;
602             *         <code>false</code> otherwise
603             * @throws PortalException if the current user did not have permission to
604             *         view the user or group members
605             * @throws SystemException if a system exception occurred
606             */
607            public boolean hasUserGroup(long userId, long groupId)
608                    throws PortalException, SystemException {
609    
610                    try {
611                            UserPermissionUtil.check(
612                                    getPermissionChecker(), userId, ActionKeys.VIEW);
613                    }
614                    catch (PrincipalException e) {
615                            GroupPermissionUtil.check(
616                                    getPermissionChecker(), groupId, ActionKeys.VIEW_MEMBERS);
617                    }
618    
619                    return groupLocalService.hasUserGroup(userId, groupId);
620            }
621    
622            /**
623             * Returns an ordered range of all the site groups and organization groups
624             * that match the name and description, optionally including the user's
625             * inherited organization groups and user groups. System and staged groups
626             * are not included.
627             *
628             * <p>
629             * Useful when paginating results. Returns a maximum of <code>end -
630             * start</code> instances. <code>start</code> and <code>end</code> are not
631             * primary keys, they are indexes in the result set. Thus, <code>0</code>
632             * refers to the first result in the set. Setting both <code>start</code>
633             * and <code>end</code> to {@link
634             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
635             * result set.
636             * </p>
637             *
638             * @param  companyId the primary key of the company
639             * @param  name the group's name (optionally <code>null</code>)
640             * @param  description the group's description (optionally
641             *         <code>null</code>)
642             * @param  params the finder params (optionally <code>null</code>). To
643             *         include the user's inherited organizations and user groups in the
644             *         search, add entries having &quot;usersGroups&quot; and
645             *         &quot;inherit&quot; as keys mapped to the the user's ID. For more
646             *         information see {@link
647             *         com.liferay.portal.service.persistence.GroupFinder}
648             * @param  start the lower bound of the range of groups to return
649             * @param  end the upper bound of the range of groups to return (not
650             *         inclusive)
651             * @return the matching groups ordered by name
652             * @throws PortalException if a portal exception occurred
653             * @throws SystemException if a system exception occurred
654             */
655            public List<Group> search(
656                            long companyId, String name, String description, String[] params,
657                            int start, int end)
658                    throws PortalException, SystemException {
659    
660                    if (params == null) {
661                            params = new String[0];
662                    }
663    
664                    LinkedHashMap<String, Object> paramsObj = MapUtil.toLinkedHashMap(
665                            params);
666    
667                    List<Group> groups = groupLocalService.search(
668                            companyId, name, description, paramsObj, true, start, end);
669    
670                    return filterGroups(groups);
671            }
672    
673            /**
674             * Returns the number of groups and organization groups that match the name
675             * and description, optionally including the user's inherited organizations
676             * and user groups. System and staged groups are not included.
677             *
678             * @param  companyId the primary key of the company
679             * @param  name the group's name (optionally <code>null</code>)
680             * @param  description the group's description (optionally
681             *         <code>null</code>)
682             * @param  params the finder params (optionally <code>null</code>). To
683             *         include the user's inherited organizations and user groups in the
684             *         search, add entries having &quot;usersGroups&quot; and
685             *         &quot;inherit&quot; as keys mapped to the the user's ID. For more
686             *         information see {@link
687             *         com.liferay.portal.service.persistence.GroupFinder}
688             * @return the number of matching groups
689             * @throws SystemException if a system exception occurred
690             */
691            public int searchCount(
692                            long companyId, String name, String description, String[] params)
693                    throws SystemException {
694    
695                    if (params == null) {
696                            params = new String[0];
697                    }
698    
699                    LinkedHashMap<String, Object> paramsObj = MapUtil.toLinkedHashMap(
700                            params);
701    
702                    return groupLocalService.searchCount(
703                            companyId, name, description, paramsObj, true);
704            }
705    
706            /**
707             * Sets the groups associated with the role, removing and adding
708             * associations as necessary.
709             *
710             * @param  roleId the primary key of the role
711             * @param  groupIds the primary keys of the groups
712             * @throws PortalException if the user did not have permission to update
713             *         update the role
714             * @throws SystemException if a system exception occurred
715             */
716            public void setRoleGroups(long roleId, long[] groupIds)
717                    throws PortalException, SystemException {
718    
719                    RolePermissionUtil.check(
720                            getPermissionChecker(), roleId, ActionKeys.ASSIGN_MEMBERS);
721    
722                    groupLocalService.setRoleGroups(roleId, groupIds);
723            }
724    
725            /**
726             * Removes the groups from the role.
727             *
728             * @param  roleId the primary key of the role
729             * @param  groupIds the primary keys of the groups
730             * @throws PortalException if the user did not have permission to update the
731             *         role
732             * @throws SystemException if a system exception occurred
733             */
734            public void unsetRoleGroups(long roleId, long[] groupIds)
735                    throws PortalException, SystemException {
736    
737                    RolePermissionUtil.check(
738                            getPermissionChecker(), roleId, ActionKeys.ASSIGN_MEMBERS);
739    
740                    groupLocalService.unsetRoleGroups(roleId, groupIds);
741            }
742    
743            /**
744             * Updates the group's friendly URL.
745             *
746             * @param  groupId the primary key of the group
747             * @param  friendlyURL the group's new friendlyURL (optionally
748             *         <code>null</code>)
749             * @return the group
750             * @throws PortalException if the user did not have permission to update the
751             *         group, if a group with the primary key could not be found, or if
752             *         a valid friendly URL could not be created for the group
753             * @throws SystemException if a system exception occurred
754             */
755            public Group updateFriendlyURL(long groupId, String friendlyURL)
756                    throws PortalException, SystemException {
757    
758                    GroupPermissionUtil.check(
759                            getPermissionChecker(), groupId, ActionKeys.UPDATE);
760    
761                    return groupLocalService.updateFriendlyURL(groupId, friendlyURL);
762            }
763    
764            /**
765             * Updates the group.
766             *
767             * @param  groupId the primary key of the group
768             * @param  parentGroupId the primary key of the parent group
769             * @param  name the group's new name
770             * @param  description the group's new description (optionally
771             *         <code>null</code>)
772             * @param  type the group's new type. For more information see {@link
773             *         com.liferay.portal.model.GroupConstants}
774             * @param  friendlyURL the group's new friendlyURL (optionally
775             *         <code>null</code>)
776             * @param  active whether the group is active
777             * @param  serviceContext the service context to be applied (optionally
778             *         <code>null</code>). Can set the asset category IDs and asset tag
779             *         names for the group.
780             * @return the group
781             * @throws PortalException if the user did not have permission to update the
782             *         group, if a group with the primary key could not be found, if the
783             *         friendly URL was invalid or could one not be created
784             * @throws SystemException if a system exception occurred
785             */
786            public Group updateGroup(
787                            long groupId, long parentGroupId, String name, String description,
788                            int type, String friendlyURL, boolean active,
789                            ServiceContext serviceContext)
790                    throws PortalException, SystemException {
791    
792                    GroupPermissionUtil.check(
793                            getPermissionChecker(), groupId, ActionKeys.UPDATE);
794    
795                    return groupLocalService.updateGroup(
796                            groupId, parentGroupId, name, description, type, friendlyURL,
797                            active, serviceContext);
798            }
799    
800            /**
801             * Updates the group's type settings.
802             *
803             * @param  groupId the primary key of the group
804             * @param  typeSettings the group's new type settings (optionally
805             *         <code>null</code>)
806             * @return the group
807             * @throws PortalException if the user did not have permission to update the
808             *         group or if a group with the primary key could not be found
809             * @throws SystemException if a system exception occurred
810             */
811            public Group updateGroup(long groupId, String typeSettings)
812                    throws PortalException, SystemException {
813    
814                    GroupPermissionUtil.check(
815                            getPermissionChecker(), groupId, ActionKeys.UPDATE);
816    
817                    return groupLocalService.updateGroup(groupId, typeSettings);
818            }
819    
820            protected List<Group> filterGroups(List<Group> groups)
821                    throws PortalException, SystemException {
822    
823                    List<Group> filteredGroups = new ArrayList<Group>();
824    
825                    for (Group group : groups) {
826                            if (GroupPermissionUtil.contains(
827                                            getPermissionChecker(), group.getGroupId(),
828                                            ActionKeys.VIEW)) {
829    
830                                    filteredGroups.add(group);
831                            }
832                    }
833    
834                    return filteredGroups;
835            }
836    
837    }