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.DuplicateUserGroupException;
018    import com.liferay.portal.NoSuchUserGroupException;
019    import com.liferay.portal.RequiredUserGroupException;
020    import com.liferay.portal.UserGroupNameException;
021    import com.liferay.portal.kernel.exception.PortalException;
022    import com.liferay.portal.kernel.exception.SystemException;
023    import com.liferay.portal.kernel.lar.PortletDataHandlerKeys;
024    import com.liferay.portal.kernel.lar.UserIdStrategy;
025    import com.liferay.portal.kernel.search.Hits;
026    import com.liferay.portal.kernel.search.Indexer;
027    import com.liferay.portal.kernel.search.IndexerRegistryUtil;
028    import com.liferay.portal.kernel.search.QueryConfig;
029    import com.liferay.portal.kernel.search.SearchContext;
030    import com.liferay.portal.kernel.search.Sort;
031    import com.liferay.portal.kernel.util.CharPool;
032    import com.liferay.portal.kernel.util.OrderByComparator;
033    import com.liferay.portal.kernel.util.Validator;
034    import com.liferay.portal.kernel.workflow.WorkflowConstants;
035    import com.liferay.portal.model.Group;
036    import com.liferay.portal.model.GroupConstants;
037    import com.liferay.portal.model.ResourceConstants;
038    import com.liferay.portal.model.Team;
039    import com.liferay.portal.model.User;
040    import com.liferay.portal.model.UserGroup;
041    import com.liferay.portal.model.UserGroupConstants;
042    import com.liferay.portal.security.auth.MembershipPolicyUtil;
043    import com.liferay.portal.security.ldap.LDAPUserGroupTransactionThreadLocal;
044    import com.liferay.portal.security.permission.PermissionCacheUtil;
045    import com.liferay.portal.service.ServiceContext;
046    import com.liferay.portal.service.base.UserGroupLocalServiceBaseImpl;
047    import com.liferay.portal.util.PropsValues;
048    
049    import java.io.File;
050    import java.io.Serializable;
051    
052    import java.util.ArrayList;
053    import java.util.HashMap;
054    import java.util.LinkedHashMap;
055    import java.util.List;
056    import java.util.Map;
057    import java.util.Set;
058    
059    /**
060     * The implementation of the user group local service.
061     *
062     * @author Charles May
063     */
064    public class UserGroupLocalServiceImpl extends UserGroupLocalServiceBaseImpl {
065    
066            /**
067             * Adds the user groups to the group.
068             *
069             * @param  groupId the primary key of the group
070             * @param  userGroupIds the primary keys of the user groups
071             * @throws SystemException if a system exception occurred
072             */
073            public void addGroupUserGroups(long groupId, long[] userGroupIds)
074                    throws SystemException {
075    
076                    groupPersistence.addUserGroups(groupId, userGroupIds);
077    
078                    PermissionCacheUtil.clearCache();
079            }
080    
081            /**
082             * Adds the user groups to the team.
083             *
084             * @param  teamId the primary key of the team
085             * @param  userGroupIds the primary keys of the user groups
086             * @throws SystemException if a system exception occurred
087             */
088            public void addTeamUserGroups(long teamId, long[] userGroupIds)
089                    throws SystemException {
090    
091                    teamPersistence.addUserGroups(teamId, userGroupIds);
092    
093                    PermissionCacheUtil.clearCache();
094            }
095    
096            /**
097             * Adds a user group.
098             *
099             * <p>
100             * This method handles the creation and bookkeeping of the user group,
101             * including its resources, metadata, and internal data structures. It is
102             * not necessary to make subsequent calls to setup default groups and
103             * resources for the user group.
104             * </p>
105             *
106             * @param      userId the primary key of the user
107             * @param      companyId the primary key of the user group's company
108             * @param      name the user group's name
109             * @param      description the user group's description
110             * @return     the user group
111             * @throws     PortalException if the user group's information was invalid
112             * @throws     SystemException if a system exception occurred
113             * @deprecated {@link #addUserGroup(long, long, String, String,
114             *             ServiceContext)}
115             */
116            public UserGroup addUserGroup(
117                            long userId, long companyId, String name, String description)
118                    throws PortalException, SystemException {
119    
120                    return addUserGroup(userId, companyId, name, description, null);
121            }
122    
123            /**
124             * Adds a user group.
125             *
126             * <p>
127             * This method handles the creation and bookkeeping of the user group,
128             * including its resources, metadata, and internal data structures. It is
129             * not necessary to make subsequent calls to setup default groups and
130             * resources for the user group.
131             * </p>
132             *
133             * @param  userId the primary key of the user
134             * @param  companyId the primary key of the user group's company
135             * @param  name the user group's name
136             * @param  description the user group's description
137             * @param  serviceContext the user group's service context (optionally
138             *         <code>null</code>). Can set expando bridge attributes for the
139             *         user group.
140             * @return the user group
141             * @throws PortalException if the user group's information was invalid
142             * @throws SystemException if a system exception occurred
143             */
144            public UserGroup addUserGroup(
145                            long userId, long companyId, String name, String description,
146                            ServiceContext serviceContext)
147                    throws PortalException, SystemException {
148    
149                    // User group
150    
151                    validate(0, companyId, name);
152    
153                    long userGroupId = counterLocalService.increment();
154    
155                    UserGroup userGroup = userGroupPersistence.create(userGroupId);
156    
157                    userGroup.setCompanyId(companyId);
158                    userGroup.setParentUserGroupId(
159                            UserGroupConstants.DEFAULT_PARENT_USER_GROUP_ID);
160                    userGroup.setName(name);
161                    userGroup.setDescription(description);
162                    userGroup.setAddedByLDAPImport(
163                            LDAPUserGroupTransactionThreadLocal.isOriginatesFromLDAP());
164                    userGroup.setExpandoBridgeAttributes(serviceContext);
165    
166                    userGroupPersistence.update(userGroup);
167    
168                    // Group
169    
170                    groupLocalService.addGroup(
171                            userId, GroupConstants.DEFAULT_PARENT_GROUP_ID,
172                            UserGroup.class.getName(), userGroup.getUserGroupId(),
173                            GroupConstants.DEFAULT_LIVE_GROUP_ID, String.valueOf(userGroupId),
174                            null, 0, null, false, true, null);
175    
176                    // Resources
177    
178                    resourceLocalService.addResources(
179                            companyId, 0, userId, UserGroup.class.getName(),
180                            userGroup.getUserGroupId(), false, false, false);
181    
182                    // Indexer
183    
184                    Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
185                            UserGroup.class);
186    
187                    indexer.reindex(userGroup);
188    
189                    return userGroup;
190            }
191    
192            public void checkMembershipPolicy(User user)
193                    throws PortalException, SystemException {
194    
195                    List<UserGroup> userGroups = getUserUserGroups(user.getUserId());
196    
197                    for (UserGroup userGroup : userGroups) {
198                            if (!MembershipPolicyUtil.isMembershipAllowed(userGroup, user)) {
199                                    userLocalService.unsetUserGroupUsers(
200                                            userGroup.getUserGroupId(), new long[] {user.getUserId()});
201                            }
202                    }
203    
204                    Set<UserGroup> mandatoryUserGroups =
205                            MembershipPolicyUtil.getMandatoryUserGroups(user);
206    
207                    for (UserGroup userGroup : mandatoryUserGroups) {
208                            if (!userLocalService.hasUserGroupUser(
209                                            userGroup.getUserGroupId(), user.getUserId())) {
210    
211                                    userLocalService.addUserGroupUsers(
212                                            userGroup.getUserGroupId(), new long[] {user.getUserId()});
213                            }
214                    }
215            }
216    
217            /**
218             * Clears all associations between the user and its user groups and clears
219             * the permissions cache.
220             *
221             * <p>
222             * This method is called from {@link #deleteUserGroup(UserGroup)}.
223             * </p>
224             *
225             * @param  userId the primary key of the user
226             * @throws SystemException if a system exception occurred
227             */
228            public void clearUserUserGroups(long userId) throws SystemException {
229                    userPersistence.clearUserGroups(userId);
230    
231                    PermissionCacheUtil.clearCache();
232            }
233    
234            /**
235             * Copies the user group's layouts to the users who are not already members
236             * of the user group.
237             *
238             * @param      userGroupId the primary key of the user group
239             * @param      userIds the primary keys of the users
240             * @throws     PortalException if any one of the users could not be found or
241             *             if a portal exception occurred
242             * @throws     SystemException if a system exception occurred
243             * @deprecated
244             */
245            public void copyUserGroupLayouts(long userGroupId, long userIds[])
246                    throws PortalException, SystemException {
247    
248                    Map<String, String[]> parameterMap = getLayoutTemplatesParameters();
249    
250                    File[] files = exportLayouts(userGroupId, parameterMap);
251    
252                    try {
253                            for (long userId : userIds) {
254                                    if (!userGroupPersistence.containsUser(userGroupId, userId)) {
255                                            importLayouts(userId, parameterMap, files[0], files[1]);
256                                    }
257                            }
258                    }
259                    finally {
260                            if (files[0] != null) {
261                                    files[0].delete();
262                            }
263    
264                            if (files[1] != null) {
265                                    files[1].delete();
266                            }
267                    }
268            }
269    
270            /**
271             * Copies the user groups' layouts to the user.
272             *
273             * @param      userGroupIds the primary keys of the user groups
274             * @param      userId the primary key of the user
275             * @throws     PortalException if a user with the primary key could not be
276             *             found or if a portal exception occurred
277             * @throws     SystemException if a system exception occurred
278             * @deprecated
279             */
280            public void copyUserGroupLayouts(long userGroupIds[], long userId)
281                    throws PortalException, SystemException {
282    
283                    for (long userGroupId : userGroupIds) {
284                            if (!userGroupPersistence.containsUser(userGroupId, userId)) {
285                                    copyUserGroupLayouts(userGroupId, userId);
286                            }
287                    }
288            }
289    
290            /**
291             * Copies the user group's layout to the user.
292             *
293             * @param      userGroupId the primary key of the user group
294             * @param      userId the primary key of the user
295             * @throws     PortalException if a user with the primary key could not be
296             *             found or if a portal exception occurred
297             * @throws     SystemException if a system exception occurred
298             * @deprecated
299             */
300            public void copyUserGroupLayouts(long userGroupId, long userId)
301                    throws PortalException, SystemException {
302    
303                    Map<String, String[]> parameterMap = getLayoutTemplatesParameters();
304    
305                    File[] files = exportLayouts(userGroupId, parameterMap);
306    
307                    try {
308                            importLayouts(userId, parameterMap, files[0], files[1]);
309                    }
310                    finally {
311                            if (files[0] != null) {
312                                    files[0].delete();
313                            }
314    
315                            if (files[1] != null) {
316                                    files[1].delete();
317                            }
318                    }
319            }
320    
321            /**
322             * Deletes the user group.
323             *
324             * @param  userGroupId the primary key of the user group
325             * @return the deleted user group
326             * @throws PortalException if a user group with the primary key could not be
327             *         found or if the user group had a workflow in approved status
328             * @throws SystemException if a system exception occurred
329             */
330            @Override
331            public UserGroup deleteUserGroup(long userGroupId)
332                    throws PortalException, SystemException {
333    
334                    UserGroup userGroup = userGroupPersistence.findByPrimaryKey(
335                            userGroupId);
336    
337                    return deleteUserGroup(userGroup);
338            }
339    
340            /**
341             * Deletes the user group.
342             *
343             * @param  userGroup the user group
344             * @return the deleted user group
345             * @throws PortalException if the organization had a workflow in approved
346             *         status
347             * @throws SystemException if a system exception occurred
348             */
349            @Override
350            public UserGroup deleteUserGroup(UserGroup userGroup)
351                    throws PortalException, SystemException {
352    
353                    int count = userLocalService.getUserGroupUsersCount(
354                            userGroup.getUserGroupId(), WorkflowConstants.STATUS_APPROVED);
355    
356                    if (count > 0) {
357                            throw new RequiredUserGroupException();
358                    }
359    
360                    // Expando
361    
362                    expandoValueLocalService.deleteValues(
363                            UserGroup.class.getName(), userGroup.getUserGroupId());
364    
365                    // Users
366    
367                    clearUserUserGroups(userGroup.getUserGroupId());
368    
369                    // Group
370    
371                    Group group = userGroup.getGroup();
372    
373                    groupLocalService.deleteGroup(group);
374    
375                    // User group roles
376    
377                    userGroupGroupRoleLocalService.deleteUserGroupGroupRolesByUserGroupId(
378                            userGroup.getUserGroupId());
379    
380                    // Resources
381    
382                    resourceLocalService.deleteResource(
383                            userGroup.getCompanyId(), UserGroup.class.getName(),
384                            ResourceConstants.SCOPE_INDIVIDUAL, userGroup.getUserGroupId());
385    
386                    // User group
387    
388                    userGroupPersistence.remove(userGroup);
389    
390                    // Permission cache
391    
392                    PermissionCacheUtil.clearCache();
393    
394                    return userGroup;
395            }
396    
397            /**
398             * Returns the user group with the name.
399             *
400             * @param  companyId the primary key of the user group's company
401             * @param  name the user group's name
402             * @return Returns the user group with the name
403             * @throws PortalException if a user group with the name could not be found
404             * @throws SystemException if a system exception occurred
405             */
406            public UserGroup getUserGroup(long companyId, String name)
407                    throws PortalException, SystemException {
408    
409                    return userGroupPersistence.findByC_N(companyId, name);
410            }
411    
412            /**
413             * Returns all the user groups belonging to the company.
414             *
415             * @param  companyId the primary key of the user groups' company
416             * @return the user groups belonging to the company
417             * @throws SystemException if a system exception occurred
418             */
419            public List<UserGroup> getUserGroups(long companyId)
420                    throws SystemException {
421    
422                    return userGroupPersistence.findByCompanyId(companyId);
423            }
424    
425            /**
426             * Returns all the user groups with the primary keys.
427             *
428             * @param  userGroupIds the primary keys of the user groups
429             * @return the user groups with the primary keys
430             * @throws PortalException if any one of the user groups could not be found
431             * @throws SystemException if a system exception occurred
432             */
433            public List<UserGroup> getUserGroups(long[] userGroupIds)
434                    throws PortalException, SystemException {
435    
436                    List<UserGroup> userGroups = new ArrayList<UserGroup>(
437                            userGroupIds.length);
438    
439                    for (long userGroupId : userGroupIds) {
440                            UserGroup userGroup = getUserGroup(userGroupId);
441    
442                            userGroups.add(userGroup);
443                    }
444    
445                    return userGroups;
446            }
447    
448            /**
449             * Returns all the user groups to which the user belongs.
450             *
451             * @param  userId the primary key of the user
452             * @return the user groups to which the user belongs
453             * @throws SystemException if a system exception occurred
454             */
455            public List<UserGroup> getUserUserGroups(long userId)
456                    throws SystemException {
457    
458                    return userPersistence.getUserGroups(userId);
459            }
460    
461            /**
462             * Returns <code>true</code> if the user group is associated with the group.
463             *
464             * @param  groupId the primary key of the group
465             * @param  userGroupId the primary key of the user group
466             * @return <code>true</code> if the user group belongs to the group;
467             *         <code>false</code> otherwise
468             * @throws SystemException if a system exception occurred
469             */
470            public boolean hasGroupUserGroup(long groupId, long userGroupId)
471                    throws SystemException {
472    
473                    return groupPersistence.containsUserGroup(groupId, userGroupId);
474            }
475    
476            /**
477             * Returns <code>true</code> if the user group belongs to the team.
478             *
479             * @param  teamId the primary key of the team
480             * @param  userGroupId the primary key of the user group
481             * @return <code>true</code> if the user group belongs to the team;
482             *         <code>false</code> otherwise
483             * @throws SystemException if a system exception occurred
484             */
485            public boolean hasTeamUserGroup(long teamId, long userGroupId)
486                    throws SystemException {
487    
488                    return teamPersistence.containsUserGroup(teamId, userGroupId);
489            }
490    
491            /**
492             * Returns an ordered range of all the user groups that match the keywords.
493             *
494             * <p>
495             * Useful when paginating results. Returns a maximum of <code>end -
496             * start</code> instances. <code>start</code> and <code>end</code> are not
497             * primary keys, they are indexes in the result set. Thus, <code>0</code>
498             * refers to the first result in the set. Setting both <code>start</code>
499             * and <code>end</code> to {@link
500             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
501             * result set.
502             * </p>
503             *
504             * @param  companyId the primary key of the user group's company
505             * @param  keywords the keywords (space separated), which may occur in the
506             *         user group's name or description (optionally <code>null</code>)
507             * @param  params the finder params (optionally <code>null</code>). For more
508             *         information see {@link
509             *         com.liferay.portal.service.persistence.UserGroupFinder}
510             * @param  start the lower bound of the range of user groups to return
511             * @param  end the upper bound of the range of user groups to return (not
512             *         inclusive)
513             * @param  obc the comparator to order the user groups (optionally
514             *         <code>null</code>)
515             * @return the matching user groups ordered by comparator <code>obc</code>
516             * @throws SystemException if a system exception occurred
517             * @see    com.liferay.portal.service.persistence.UserGroupFinder
518             */
519            public List<UserGroup> search(
520                            long companyId, String keywords,
521                            LinkedHashMap<String, Object> params, int start, int end,
522                            OrderByComparator obc)
523                    throws SystemException {
524    
525                    return userGroupFinder.findByKeywords(
526                            companyId, keywords, params, start, end, obc);
527            }
528    
529            /**
530             * Returns an ordered range of all the user groups that match the keywords,
531             * using the indexer. It is preferable to use this method instead of the
532             * non-indexed version whenever possible for performance reasons.
533             *
534             * <p>
535             * Useful when paginating results. Returns a maximum of <code>end -
536             * start</code> instances. <code>start</code> and <code>end</code> are not
537             * primary keys, they are indexes in the result set. Thus, <code>0</code>
538             * refers to the first result in the set. Setting both <code>start</code>
539             * and <code>end</code> to {@link
540             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
541             * result set.
542             * </p>
543             *
544             * @param  companyId the primary key of the user group's company
545             * @param  keywords the keywords (space separated), which may occur in the
546             *         user group's name or description (optionally <code>null</code>)
547             * @param  params the finder params (optionally <code>null</code>). For more
548             *         information see {@link
549             *         com.liferay.portlet.usergroupsadmin.util.UserGroupIndexer}
550             * @param  start the lower bound of the range of user groups to return
551             * @param  end the upper bound of the range of user groups to return (not
552             *         inclusive)
553             * @param  sort the field and direction by which to sort (optionally
554             *         <code>null</code>)
555             * @return the matching user groups ordered by sort
556             * @throws SystemException if a system exception occurred
557             * @see    com.liferay.portlet.usergroupsadmin.util.UserGroupIndexer
558             */
559            public Hits search(
560                            long companyId, String keywords,
561                            LinkedHashMap<String, Object> params, int start, int end, Sort sort)
562                    throws SystemException {
563    
564                    String name = null;
565                    String description = null;
566                    boolean andOperator = false;
567    
568                    if (Validator.isNotNull(keywords)) {
569                            name = keywords;
570                            description = keywords;
571                    }
572                    else {
573                            andOperator = true;
574                    }
575    
576                    if (params != null) {
577                            params.put("keywords", keywords);
578                    }
579    
580                    return search(
581                            companyId, name, description, params, andOperator, start, end,
582                            sort);
583            }
584    
585            /**
586             * Returns an ordered range of all the user groups that match the name and
587             * description. It is preferable to use this method instead of the
588             * non-indexed version whenever possible for performance reasons.
589             *
590             * <p>
591             * Useful when paginating results. Returns a maximum of <code>end -
592             * start</code> instances. <code>start</code> and <code>end</code> are not
593             * primary keys, they are indexes in the result set. Thus, <code>0</code>
594             * refers to the first result in the set. Setting both <code>start</code>
595             * and <code>end</code> to {@link
596             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
597             * result set.
598             * </p>
599             *
600             * @param  companyId the primary key of the user group's company
601             * @param  name the user group's name (optionally <code>null</code>)
602             * @param  description the user group's description (optionally
603             *         <code>null</code>)
604             * @param  params the finder params (optionally <code>null</code>). For more
605             *         information see {@link
606             *         com.liferay.portlet.usergroupsadmin.util.UserGroupIndexer}
607             * @param  andSearch whether every field must match its keywords or just one
608             *         field
609             * @param  start the lower bound of the range of user groups to return
610             * @param  end the upper bound of the range of user groups to return (not
611             *         inclusive)
612             * @param  sort the field and direction by which to sort (optionally
613             *         <code>null</code>)
614             * @return the matching user groups ordered by sort
615             * @throws SystemException if a system exception occurred
616             * @see    com.liferay.portal.service.persistence.UserGroupFinder
617             */
618            public Hits search(
619                            long companyId, String name, String description,
620                            LinkedHashMap<String, Object> params, boolean andSearch, int start,
621                            int end, Sort sort)
622                    throws SystemException {
623    
624                    try {
625                            SearchContext searchContext = new SearchContext();
626    
627                            searchContext.setAndSearch(andSearch);
628    
629                            Map<String, Serializable> attributes =
630                                    new HashMap<String, Serializable>();
631    
632                            attributes.put("description", description);
633                            attributes.put("name", name);
634    
635                            searchContext.setAttributes(attributes);
636    
637                            searchContext.setCompanyId(companyId);
638                            searchContext.setEnd(end);
639    
640                            if (params != null) {
641                                    String keywords = (String)params.remove("keywords");
642    
643                                    if (Validator.isNotNull(keywords)) {
644                                            searchContext.setKeywords(keywords);
645                                    }
646                            }
647    
648                            QueryConfig queryConfig = new QueryConfig();
649    
650                            queryConfig.setHighlightEnabled(false);
651                            queryConfig.setScoreEnabled(false);
652    
653                            searchContext.setQueryConfig(queryConfig);
654    
655                            if (sort != null) {
656                                    searchContext.setSorts(new Sort[] {sort});
657                            }
658    
659                            searchContext.setStart(start);
660    
661                            Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
662                                    UserGroup.class);
663    
664                            return indexer.search(searchContext);
665                    }
666                    catch (Exception e) {
667                            throw new SystemException(e);
668                    }
669            }
670    
671            /**
672             * Returns the number of user groups that match the keywords
673             *
674             * @param  companyId the primary key of the user group's company
675             * @param  keywords the keywords (space separated), which may occur in the
676             *         user group's name or description (optionally <code>null</code>)
677             * @param  params the finder params (optionally <code>null</code>). For more
678             *         information see {@link
679             *         com.liferay.portal.service.persistence.UserGroupFinder}
680             * @return the number of matching user groups
681             * @throws SystemException if a system exception occurred
682             * @see    com.liferay.portal.service.persistence.UserGroupFinder
683             */
684            public int searchCount(
685                            long companyId, String keywords,
686                            LinkedHashMap<String, Object> params)
687                    throws SystemException {
688    
689                    return userGroupFinder.countByKeywords(companyId, keywords, params);
690            }
691    
692            /**
693             * Sets the user groups associated with the user copying the user group
694             * layouts and removing and adding user group associations for the user as
695             * necessary.
696             *
697             * @param  userId the primary key of the user
698             * @param  userGroupIds the primary keys of the user groups
699             * @throws PortalException if a portal exception occurred
700             * @throws SystemException if a system exception occurred
701             */
702            public void setUserUserGroups(long userId, long[] userGroupIds)
703                    throws PortalException, SystemException {
704    
705                    copyUserGroupLayouts(userGroupIds, userId);
706    
707                    userPersistence.setUserGroups(userId, userGroupIds);
708    
709                    Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);
710    
711                    indexer.reindex(userId);
712    
713                    PermissionCacheUtil.clearCache();
714            }
715    
716            /**
717             * Removes the user groups from the group.
718             *
719             * @param  groupId the primary key of the group
720             * @param  userGroupIds the primary keys of the user groups
721             * @throws SystemException if a system exception occurred
722             */
723            public void unsetGroupUserGroups(long groupId, long[] userGroupIds)
724                    throws SystemException {
725    
726                    List<Team> teams = teamPersistence.findByGroupId(groupId);
727    
728                    for (Team team : teams) {
729                            teamPersistence.removeUserGroups(team.getTeamId(), userGroupIds);
730                    }
731    
732                    userGroupGroupRoleLocalService.deleteUserGroupGroupRoles(
733                            userGroupIds, groupId);
734    
735                    groupPersistence.removeUserGroups(groupId, userGroupIds);
736    
737                    PermissionCacheUtil.clearCache();
738            }
739    
740            /**
741             * Removes the user groups from the team.
742             *
743             * @param  teamId the primary key of the team
744             * @param  userGroupIds the primary keys of the user groups
745             * @throws SystemException if a system exception occurred
746             */
747            public void unsetTeamUserGroups(long teamId, long[] userGroupIds)
748                    throws SystemException {
749    
750                    teamPersistence.removeUserGroups(teamId, userGroupIds);
751    
752                    PermissionCacheUtil.clearCache();
753            }
754    
755            /**
756             * Updates the user group.
757             *
758             * @param      companyId the primary key of the user group's company
759             * @param      userGroupId the primary key of the user group
760             * @param      name the user group's name
761             * @param      description the user group's description
762             * @return     the user group
763             * @throws     PortalException if a user group with the primary key could
764             *             not be found or if the new information was invalid
765             * @throws     SystemException if a system exception occurred
766             * @deprecated {@link #updateUserGroup(long, long, String, String,
767             *             ServiceContext)}
768             */
769            public UserGroup updateUserGroup(
770                            long companyId, long userGroupId, String name, String description)
771                    throws PortalException, SystemException {
772    
773                    return updateUserGroup(companyId, userGroupId, name, description, null);
774            }
775    
776            /**
777             * Updates the user group.
778             *
779             * @param  companyId the primary key of the user group's company
780             * @param  userGroupId the primary key of the user group
781             * @param  name the user group's name
782             * @param  description the user group's description
783             * @param  serviceContext the user group's service context (optionally
784             *         <code>null</code>). Can set expando bridge attributes for the
785             *         user group.
786             * @return the user group
787             * @throws PortalException if a user group with the primary key could not be
788             *         found or if the new information was invalid
789             * @throws SystemException if a system exception occurred
790             */
791            public UserGroup updateUserGroup(
792                            long companyId, long userGroupId, String name, String description,
793                            ServiceContext serviceContext)
794                    throws PortalException, SystemException {
795    
796                    // User group
797    
798                    validate(userGroupId, companyId, name);
799    
800                    UserGroup userGroup = userGroupPersistence.findByPrimaryKey(
801                            userGroupId);
802    
803                    userGroup.setName(name);
804                    userGroup.setDescription(description);
805                    userGroup.setExpandoBridgeAttributes(serviceContext);
806    
807                    userGroupPersistence.update(userGroup);
808    
809                    // Indexer
810    
811                    Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
812                            UserGroup.class);
813    
814                    indexer.reindex(userGroup);
815    
816                    return userGroup;
817            }
818    
819            protected File[] exportLayouts(
820                            long userGroupId, Map<String, String[]> parameterMap)
821                    throws PortalException, SystemException {
822    
823                    File[] files = new File[2];
824    
825                    UserGroup userGroup = userGroupPersistence.findByPrimaryKey(
826                            userGroupId);
827    
828                    Group group = userGroup.getGroup();
829    
830                    if (userGroup.hasPrivateLayouts()) {
831                            files[0] = layoutLocalService.exportLayoutsAsFile(
832                                    group.getGroupId(), true, null, parameterMap, null, null);
833                    }
834    
835                    if (userGroup.hasPublicLayouts()) {
836                            files[1] = layoutLocalService.exportLayoutsAsFile(
837                                    group.getGroupId(), false, null, parameterMap, null, null);
838                    }
839    
840                    return files;
841            }
842    
843            protected Map<String, String[]> getLayoutTemplatesParameters() {
844                    Map<String, String[]> parameterMap =
845                            new LinkedHashMap<String, String[]>();
846    
847                    parameterMap.put(
848                            PortletDataHandlerKeys.CATEGORIES,
849                            new String[] {Boolean.TRUE.toString()});
850                    parameterMap.put(
851                            PortletDataHandlerKeys.DATA_STRATEGY,
852                            new String[] {PortletDataHandlerKeys.DATA_STRATEGY_MIRROR});
853                    parameterMap.put(
854                            PortletDataHandlerKeys.DELETE_MISSING_LAYOUTS,
855                            new String[] {Boolean.FALSE.toString()});
856                    parameterMap.put(
857                            PortletDataHandlerKeys.DELETE_PORTLET_DATA,
858                            new String[] {Boolean.FALSE.toString()});
859                    parameterMap.put(
860                            PortletDataHandlerKeys.LAYOUT_SET_SETTINGS,
861                            new String[] {Boolean.FALSE.toString()});
862                    parameterMap.put(
863                            PortletDataHandlerKeys.LAYOUTS_IMPORT_MODE,
864                            new String[] {PortletDataHandlerKeys.
865                                    LAYOUTS_IMPORT_MODE_CREATED_FROM_PROTOTYPE});
866                    parameterMap.put(
867                            PortletDataHandlerKeys.LOGO,
868                            new String[] {Boolean.FALSE.toString()});
869                    parameterMap.put(
870                            PortletDataHandlerKeys.PERMISSIONS,
871                            new String[] {Boolean.TRUE.toString()});
872                    parameterMap.put(
873                            PortletDataHandlerKeys.PORTLET_DATA,
874                            new String[] {Boolean.TRUE.toString()});
875                    parameterMap.put(
876                            PortletDataHandlerKeys.PORTLET_DATA_ALL,
877                            new String[] {Boolean.TRUE.toString()});
878                    parameterMap.put(
879                            PortletDataHandlerKeys.PORTLET_SETUP,
880                            new String[] {Boolean.TRUE.toString()});
881                    parameterMap.put(
882                            PortletDataHandlerKeys.PORTLET_USER_PREFERENCES,
883                            new String[] {Boolean.TRUE.toString()});
884                    parameterMap.put(
885                            PortletDataHandlerKeys.PORTLETS_MERGE_MODE,
886                            new String[] {PortletDataHandlerKeys.
887                                    PORTLETS_MERGE_MODE_ADD_TO_BOTTOM});
888                    parameterMap.put(
889                            PortletDataHandlerKeys.THEME,
890                            new String[] {Boolean.FALSE.toString()});
891                    parameterMap.put(
892                            PortletDataHandlerKeys.THEME_REFERENCE,
893                            new String[] {Boolean.TRUE.toString()});
894                    parameterMap.put(
895                            PortletDataHandlerKeys.UPDATE_LAST_PUBLISH_DATE,
896                            new String[] {Boolean.FALSE.toString()});
897                    parameterMap.put(
898                            PortletDataHandlerKeys.USER_ID_STRATEGY,
899                            new String[] {UserIdStrategy.CURRENT_USER_ID});
900    
901                    return parameterMap;
902            }
903    
904            protected void importLayouts(
905                            long userId, Map<String, String[]> parameterMap,
906                            File privateLayoutsFile, File publicLayoutsFile)
907                    throws PortalException, SystemException {
908    
909                    User user = userPersistence.findByPrimaryKey(userId);
910    
911                    long groupId = user.getGroup().getGroupId();
912    
913                    if (privateLayoutsFile != null) {
914                            layoutLocalService.importLayouts(
915                                    userId, groupId, true, parameterMap, privateLayoutsFile);
916                    }
917    
918                    if (publicLayoutsFile != null) {
919                            layoutLocalService.importLayouts(
920                                    userId, groupId, false, parameterMap, publicLayoutsFile);
921                    }
922            }
923    
924            protected void validate(long userGroupId, long companyId, String name)
925                    throws PortalException, SystemException {
926    
927                    if (Validator.isNull(name) ||
928                            (name.indexOf(CharPool.COMMA) != -1) ||
929                            (name.indexOf(CharPool.STAR) != -1)) {
930    
931                            throw new UserGroupNameException();
932                    }
933    
934                    if (Validator.isNumber(name) &&
935                            !PropsValues.USER_GROUPS_NAME_ALLOW_NUMERIC) {
936    
937                            throw new UserGroupNameException();
938                    }
939    
940                    try {
941                            UserGroup userGroup = userGroupFinder.findByC_N(companyId, name);
942    
943                            if (userGroup.getUserGroupId() != userGroupId) {
944                                    throw new DuplicateUserGroupException();
945                            }
946                    }
947                    catch (NoSuchUserGroupException nsuge) {
948                    }
949            }
950    
951    }