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             * <ul>
405             * <li>
406             * Class name &quot;User&quot; includes the user's layout set
407             * group.
408             * </li>
409             * <li>
410             * Class name &quot;Organization&quot; includes the user's
411             * immediate organization groups and inherited organization groups.
412             * </li>
413             * <li>
414             * Class name &quot;Group&quot; includes the user's immediate
415             * organization groups and site groups.
416             * </li>
417             * <li>
418             * A <code>classNames</code>
419             * value of <code>null</code> includes the user's layout set group,
420             * organization groups, inherited organization groups, and site groups.
421             * </li>
422             * </ul>
423             *
424             * @param  userId the primary key of the user
425             * @param  classNames the group entity class names (optionally
426             *         <code>null</code>). For more information see {@link
427             *         #getUserPlaces(long, String[], int)}
428             * @param  max the maximum number of groups to return
429             * @return the user's group &quot;places&quot;
430             * @throws PortalException if a portal exception occurred
431             * @throws SystemException if a system exception occurred
432             */
433            public List<Group> getUserPlaces(long userId, String[] classNames, int max)
434                    throws PortalException, SystemException {
435    
436                    return getUserPlaces(userId, classNames, false, max);
437            }
438    
439            public List<Group> getUserPlaces(
440                            long userId, String[] classNames, String name, boolean active,
441                            boolean includeControlPanel, int start, int end)
442                    throws PortalException, SystemException {
443    
444                    User user = userPersistence.fetchByPrimaryKey(userId);
445    
446                    if (user.isDefaultUser()) {
447                            return Collections.emptyList();
448                    }
449    
450                    List<Group> userPlaces = new UniqueList<Group>();
451    
452                    if ((classNames == null) ||
453                            ArrayUtil.contains(classNames, Group.class.getName())) {
454    
455                            LinkedHashMap<String, Object> groupParams =
456                                    new LinkedHashMap<String, Object>();
457    
458                            groupParams.put("active", active);
459                            groupParams.put("usersGroups", new Long(userId));
460    
461                            userPlaces.addAll(
462                                    groupLocalService.search(
463                                            user.getCompanyId(), name, groupParams, start, end));
464                    }
465    
466                    if ((classNames == null) ||
467                            ArrayUtil.contains(classNames, Organization.class.getName())) {
468    
469                            List<Organization> userOrgs =
470                                    organizationLocalService.getOrganizations(
471                                            userId, start, end, null);
472    
473                            for (Organization organization : userOrgs) {
474                                    if (!organization.hasPrivateLayouts() &&
475                                            !organization.hasPublicLayouts()) {
476    
477                                            userPlaces.remove(organization.getGroup());
478                                    }
479                                    else {
480                                            userPlaces.add(0, organization.getGroup());
481                                    }
482    
483                                    if (!PropsValues.ORGANIZATIONS_MEMBERSHIP_STRICT) {
484                                            for (Organization ancestorOrganization :
485                                                            organization.getAncestors()) {
486    
487                                                    if (!organization.hasPrivateLayouts() &&
488                                                            !organization.hasPublicLayouts()) {
489    
490                                                            continue;
491                                                    }
492    
493                                                    userPlaces.add(0, ancestorOrganization.getGroup());
494                                            }
495                                    }
496                            }
497                    }
498    
499                    if ((classNames == null) ||
500                            ArrayUtil.contains(classNames, User.class.getName())) {
501    
502                            if (PropsValues.LAYOUT_USER_PRIVATE_LAYOUTS_ENABLED ||
503                                    PropsValues.LAYOUT_USER_PUBLIC_LAYOUTS_ENABLED) {
504    
505                                    Group userGroup = user.getGroup();
506    
507                                    userPlaces.add(0, userGroup);
508                            }
509                    }
510    
511                    PermissionChecker permissionChecker = getPermissionChecker();
512    
513                    if (permissionChecker.getUserId() != userId) {
514                            try {
515                                    permissionChecker = PermissionCheckerFactoryUtil.create(user);
516                            }
517                            catch (Exception e) {
518                                    throw new PrincipalException(e);
519                            }
520                    }
521    
522                    if (includeControlPanel &&
523                            PortalPermissionUtil.contains(
524                                    permissionChecker, ActionKeys.VIEW_CONTROL_PANEL)) {
525    
526                            Group controlPanelGroup = groupLocalService.getGroup(
527                                    user.getCompanyId(), GroupConstants.CONTROL_PANEL);
528    
529                            userPlaces.add(0, controlPanelGroup);
530                    }
531    
532                    if ((end != QueryUtil.ALL_POS) && (userPlaces.size() > end)) {
533                            if (start < 0) {
534                                    start = 0;
535                            }
536    
537                            userPlaces = ListUtil.subList(userPlaces, start, end);
538                    }
539    
540                    return Collections.unmodifiableList(userPlaces);
541            }
542    
543            /**
544             * Returns the guest or current user's group &quot;places&quot; associated
545             * with the group entity class names, including the Control Panel group if
546             * the user is permitted to view the Control Panel.
547             *
548             * <ul>
549             * <li>
550             * Class name &quot;User&quot; includes the user's layout set
551             * group.
552             * </li>
553             * <li>
554             * Class name &quot;Organization&quot; includes the user's
555             * immediate organization groups and inherited organization groups.
556             * </li>
557             * <li>
558             * Class name &quot;Group&quot; includes the user's immediate
559             * organization groups and site groups.
560             * </li>
561             * <li>
562             * A <code>classNames</code>
563             * value of <code>null</code> includes the user's layout set group,
564             * organization groups, inherited organization groups, and site groups.
565             * </li>
566             * </ul>
567             *
568             * @param  classNames the group entity class names (optionally
569             *         <code>null</code>). For more information see {@link
570             *         #getUserPlaces(String[], int)}
571             * @param  max the maximum number of groups to return
572             * @return the user's group &quot;places&quot;
573             * @throws PortalException if a portal exception occurred
574             * @throws SystemException if a system exception occurred
575             */
576            public List<Group> getUserPlaces(String[] classNames, int max)
577                    throws PortalException, SystemException {
578    
579                    return getUserPlaces(getGuestOrUserId(), classNames, false, max);
580            }
581    
582            /**
583             * Returns the number of the guest or current user's group
584             * &quot;places&quot; associated with the group entity class names,
585             * including the Control Panel group if the user is permitted to view the
586             * Control Panel.
587             *
588             * @return the number of user's group &quot;places&quot;
589             * @throws PortalException if a portal exception occurred
590             * @throws SystemException if a system exception occurred
591             */
592            public int getUserPlacesCount() throws PortalException, SystemException {
593                    List<Group> userPlaces = getUserPlaces(
594                            getGuestOrUserId(), null, true, QueryUtil.ALL_POS);
595    
596                    return userPlaces.size();
597            }
598    
599            /**
600             * Returns the guest or current user's layout set group, organization
601             * groups, inherited organization groups, and site groups.
602             *
603             * @return the user's layout set group, organization groups, and inherited
604             *         organization groups, and site groups
605             * @throws PortalException if a portal exception occurred
606             * @throws SystemException if a system exception occurred
607             */
608            public List<Group> getUserSites() throws PortalException, SystemException {
609                    return getUserPlaces(null, QueryUtil.ALL_POS);
610            }
611    
612            /**
613             * Returns <code>true</code> if the user is associated with the group,
614             * including the user's inherited organizations and user groups. System and
615             * staged groups are not included.
616             *
617             * @param  userId the primary key of the user
618             * @param  groupId the primary key of the group
619             * @return <code>true</code> if the user is associated with the group;
620             *         <code>false</code> otherwise
621             * @throws PortalException if the current user did not have permission to
622             *         view the user or group members
623             * @throws SystemException if a system exception occurred
624             */
625            public boolean hasUserGroup(long userId, long groupId)
626                    throws PortalException, SystemException {
627    
628                    try {
629                            UserPermissionUtil.check(
630                                    getPermissionChecker(), userId, ActionKeys.VIEW);
631                    }
632                    catch (PrincipalException e) {
633                            GroupPermissionUtil.check(
634                                    getPermissionChecker(), groupId, ActionKeys.VIEW_MEMBERS);
635                    }
636    
637                    return groupLocalService.hasUserGroup(userId, groupId);
638            }
639    
640            /**
641             * Returns an ordered range of all the site groups and organization groups
642             * that match the name and description, optionally including the user's
643             * inherited organization groups and user groups. System and staged groups
644             * are not included.
645             *
646             * <p>
647             * Useful when paginating results. Returns a maximum of <code>end -
648             * start</code> instances. <code>start</code> and <code>end</code> are not
649             * primary keys, they are indexes in the result set. Thus, <code>0</code>
650             * refers to the first result in the set. Setting both <code>start</code>
651             * and <code>end</code> to {@link
652             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
653             * result set.
654             * </p>
655             *
656             * @param  companyId the primary key of the company
657             * @param  name the group's name (optionally <code>null</code>)
658             * @param  description the group's description (optionally
659             *         <code>null</code>)
660             * @param  params the finder params (optionally <code>null</code>). To
661             *         include the user's inherited organizations and user groups in the
662             *         search, add entries having &quot;usersGroups&quot; and
663             *         &quot;inherit&quot; as keys mapped to the the user's ID. For more
664             *         information see {@link
665             *         com.liferay.portal.service.persistence.GroupFinder}
666             * @param  start the lower bound of the range of groups to return
667             * @param  end the upper bound of the range of groups to return (not
668             *         inclusive)
669             * @return the matching groups ordered by name
670             * @throws PortalException if a portal exception occurred
671             * @throws SystemException if a system exception occurred
672             */
673            public List<Group> search(
674                            long companyId, String name, String description, String[] params,
675                            int start, int end)
676                    throws PortalException, SystemException {
677    
678                    if (params == null) {
679                            params = new String[0];
680                    }
681    
682                    LinkedHashMap<String, Object> paramsObj = MapUtil.toLinkedHashMap(
683                            params);
684    
685                    List<Group> groups = groupLocalService.search(
686                            companyId, name, description, paramsObj, true, start, end);
687    
688                    return filterGroups(groups);
689            }
690    
691            /**
692             * Returns the number of groups and organization groups that match the name
693             * and description, optionally including the user's inherited organizations
694             * and user groups. System and staged groups are not included.
695             *
696             * @param  companyId the primary key of the company
697             * @param  name the group's name (optionally <code>null</code>)
698             * @param  description the group's description (optionally
699             *         <code>null</code>)
700             * @param  params the finder params (optionally <code>null</code>). To
701             *         include the user's inherited organizations and user groups in the
702             *         search, add entries having &quot;usersGroups&quot; and
703             *         &quot;inherit&quot; as keys mapped to the the user's ID. For more
704             *         information see {@link
705             *         com.liferay.portal.service.persistence.GroupFinder}
706             * @return the number of matching groups
707             * @throws SystemException if a system exception occurred
708             */
709            public int searchCount(
710                            long companyId, String name, String description, String[] params)
711                    throws SystemException {
712    
713                    if (params == null) {
714                            params = new String[0];
715                    }
716    
717                    LinkedHashMap<String, Object> paramsObj = MapUtil.toLinkedHashMap(
718                            params);
719    
720                    return groupLocalService.searchCount(
721                            companyId, name, description, paramsObj, true);
722            }
723    
724            /**
725             * Sets the groups associated with the role, removing and adding
726             * associations as necessary.
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
731             *         update the role
732             * @throws SystemException if a system exception occurred
733             */
734            public void setRoleGroups(long roleId, long[] groupIds)
735                    throws PortalException, SystemException {
736    
737                    RolePermissionUtil.check(
738                            getPermissionChecker(), roleId, ActionKeys.ASSIGN_MEMBERS);
739    
740                    groupLocalService.setRoleGroups(roleId, groupIds);
741            }
742    
743            /**
744             * Removes the groups from the role.
745             *
746             * @param  roleId the primary key of the role
747             * @param  groupIds the primary keys of the groups
748             * @throws PortalException if the user did not have permission to update the
749             *         role
750             * @throws SystemException if a system exception occurred
751             */
752            public void unsetRoleGroups(long roleId, long[] groupIds)
753                    throws PortalException, SystemException {
754    
755                    RolePermissionUtil.check(
756                            getPermissionChecker(), roleId, ActionKeys.ASSIGN_MEMBERS);
757    
758                    groupLocalService.unsetRoleGroups(roleId, groupIds);
759            }
760    
761            /**
762             * Updates the group's friendly URL.
763             *
764             * @param  groupId the primary key of the group
765             * @param  friendlyURL the group's new friendlyURL (optionally
766             *         <code>null</code>)
767             * @return the group
768             * @throws PortalException if the user did not have permission to update the
769             *         group, if a group with the primary key could not be found, or if
770             *         a valid friendly URL could not be created for the group
771             * @throws SystemException if a system exception occurred
772             */
773            public Group updateFriendlyURL(long groupId, String friendlyURL)
774                    throws PortalException, SystemException {
775    
776                    GroupPermissionUtil.check(
777                            getPermissionChecker(), groupId, ActionKeys.UPDATE);
778    
779                    return groupLocalService.updateFriendlyURL(groupId, friendlyURL);
780            }
781    
782            /**
783             * Updates the group.
784             *
785             * @param  groupId the primary key of the group
786             * @param  parentGroupId the primary key of the parent group
787             * @param  name the group's new name
788             * @param  description the group's new description (optionally
789             *         <code>null</code>)
790             * @param  type the group's new type. For more information see {@link
791             *         com.liferay.portal.model.GroupConstants}
792             * @param  friendlyURL the group's new friendlyURL (optionally
793             *         <code>null</code>)
794             * @param  active whether the group is active
795             * @param  serviceContext the service context to be applied (optionally
796             *         <code>null</code>). Can set the asset category IDs and asset tag
797             *         names for the group.
798             * @return the group
799             * @throws PortalException if the user did not have permission to update the
800             *         group, if a group with the primary key could not be found, if the
801             *         friendly URL was invalid or could one not be created
802             * @throws SystemException if a system exception occurred
803             */
804            public Group updateGroup(
805                            long groupId, long parentGroupId, String name, String description,
806                            int type, String friendlyURL, boolean active,
807                            ServiceContext serviceContext)
808                    throws PortalException, SystemException {
809    
810                    GroupPermissionUtil.check(
811                            getPermissionChecker(), groupId, ActionKeys.UPDATE);
812    
813                    return groupLocalService.updateGroup(
814                            groupId, parentGroupId, name, description, type, friendlyURL,
815                            active, serviceContext);
816            }
817    
818            /**
819             * Updates the group's type settings.
820             *
821             * @param  groupId the primary key of the group
822             * @param  typeSettings the group's new type settings (optionally
823             *         <code>null</code>)
824             * @return the group
825             * @throws PortalException if the user did not have permission to update the
826             *         group or if a group with the primary key could not be found
827             * @throws SystemException if a system exception occurred
828             */
829            public Group updateGroup(long groupId, String typeSettings)
830                    throws PortalException, SystemException {
831    
832                    GroupPermissionUtil.check(
833                            getPermissionChecker(), groupId, ActionKeys.UPDATE);
834    
835                    return groupLocalService.updateGroup(groupId, typeSettings);
836            }
837    
838            protected List<Group> filterGroups(List<Group> groups)
839                    throws PortalException, SystemException {
840    
841                    List<Group> filteredGroups = new ArrayList<Group>();
842    
843                    for (Group group : groups) {
844                            if (GroupPermissionUtil.contains(
845                                            getPermissionChecker(), group.getGroupId(),
846                                            ActionKeys.VIEW)) {
847    
848                                    filteredGroups.add(group);
849                            }
850                    }
851    
852                    return filteredGroups;
853            }
854    
855    }