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