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();
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.getUserGroupId());
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                    copyUserGroupLayouts(userGroupIds, userId);
698    
699                    userPersistence.setUserGroups(userId, userGroupIds);
700    
701                    Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);
702    
703                    indexer.reindex(userId);
704    
705                    PermissionCacheUtil.clearCache();
706            }
707    
708            /**
709             * Removes the user groups from the group.
710             *
711             * @param  groupId the primary key of the group
712             * @param  userGroupIds the primary keys of the user groups
713             * @throws SystemException if a system exception occurred
714             */
715            @Override
716            public void unsetGroupUserGroups(long groupId, long[] userGroupIds)
717                    throws SystemException {
718    
719                    List<Team> teams = teamPersistence.findByGroupId(groupId);
720    
721                    for (Team team : teams) {
722                            teamPersistence.removeUserGroups(team.getTeamId(), userGroupIds);
723                    }
724    
725                    userGroupGroupRoleLocalService.deleteUserGroupGroupRoles(
726                            userGroupIds, groupId);
727    
728                    groupPersistence.removeUserGroups(groupId, userGroupIds);
729    
730                    PermissionCacheUtil.clearCache();
731            }
732    
733            /**
734             * Removes the user groups from the team.
735             *
736             * @param  teamId the primary key of the team
737             * @param  userGroupIds the primary keys of the user groups
738             * @throws SystemException if a system exception occurred
739             */
740            @Override
741            public void unsetTeamUserGroups(long teamId, long[] userGroupIds)
742                    throws SystemException {
743    
744                    teamPersistence.removeUserGroups(teamId, userGroupIds);
745    
746                    PermissionCacheUtil.clearCache();
747            }
748    
749            /**
750             * Updates the user group.
751             *
752             * @param      companyId the primary key of the user group's company
753             * @param      userGroupId the primary key of the user group
754             * @param      name the user group's name
755             * @param      description the user group's description
756             * @return     the user group
757             * @throws     PortalException if a user group with the primary key could
758             *             not be found or if the new information was invalid
759             * @throws     SystemException if a system exception occurred
760             * @deprecated As of 6.2.0, replaced by {@link #updateUserGroup(long, long,
761             *             String, String, ServiceContext)}
762             */
763            @Override
764            public UserGroup updateUserGroup(
765                            long companyId, long userGroupId, String name, String description)
766                    throws PortalException, SystemException {
767    
768                    return updateUserGroup(companyId, userGroupId, name, description, null);
769            }
770    
771            /**
772             * Updates the user group.
773             *
774             * @param  companyId the primary key of the user group's company
775             * @param  userGroupId the primary key of the user group
776             * @param  name the user group's name
777             * @param  description the user group's description
778             * @param  serviceContext the service context to be applied (optionally
779             *         <code>null</code>). Can set expando bridge attributes for the
780             *         user group.
781             * @return the user group
782             * @throws PortalException if a user group with the primary key could not be
783             *         found or if the new information was invalid
784             * @throws SystemException if a system exception occurred
785             */
786            @Override
787            public UserGroup updateUserGroup(
788                            long companyId, long userGroupId, String name, String description,
789                            ServiceContext serviceContext)
790                    throws PortalException, SystemException {
791    
792                    // User group
793    
794                    validate(userGroupId, companyId, name);
795    
796                    UserGroup userGroup = userGroupPersistence.findByPrimaryKey(
797                            userGroupId);
798    
799                    userGroup.setModifiedDate(new Date());
800                    userGroup.setName(name);
801                    userGroup.setDescription(description);
802                    userGroup.setExpandoBridgeAttributes(serviceContext);
803    
804                    userGroupPersistence.update(userGroup);
805    
806                    // Indexer
807    
808                    Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
809                            UserGroup.class);
810    
811                    indexer.reindex(userGroup);
812    
813                    return userGroup;
814            }
815    
816            protected File[] exportLayouts(
817                            long userGroupId, Map<String, String[]> parameterMap)
818                    throws PortalException, SystemException {
819    
820                    File[] files = new File[2];
821    
822                    UserGroup userGroup = userGroupPersistence.findByPrimaryKey(
823                            userGroupId);
824    
825                    Group group = userGroup.getGroup();
826    
827                    if (userGroup.hasPrivateLayouts()) {
828                            files[0] = layoutLocalService.exportLayoutsAsFile(
829                                    group.getGroupId(), true, null, parameterMap, null, null);
830                    }
831    
832                    if (userGroup.hasPublicLayouts()) {
833                            files[1] = layoutLocalService.exportLayoutsAsFile(
834                                    group.getGroupId(), false, null, parameterMap, null, null);
835                    }
836    
837                    return files;
838            }
839    
840            protected Map<String, String[]> getLayoutTemplatesParameters() {
841                    Map<String, String[]> parameterMap =
842                            new LinkedHashMap<String, String[]>();
843    
844                    parameterMap.put(
845                            PortletDataHandlerKeys.CATEGORIES,
846                            new String[] {Boolean.TRUE.toString()});
847                    parameterMap.put(
848                            PortletDataHandlerKeys.DATA_STRATEGY,
849                            new String[] {PortletDataHandlerKeys.DATA_STRATEGY_MIRROR});
850                    parameterMap.put(
851                            PortletDataHandlerKeys.DELETE_MISSING_LAYOUTS,
852                            new String[] {Boolean.FALSE.toString()});
853                    parameterMap.put(
854                            PortletDataHandlerKeys.DELETE_PORTLET_DATA,
855                            new String[] {Boolean.FALSE.toString()});
856                    parameterMap.put(
857                            PortletDataHandlerKeys.LAYOUT_SET_SETTINGS,
858                            new String[] {Boolean.FALSE.toString()});
859                    parameterMap.put(
860                            PortletDataHandlerKeys.LAYOUTS_IMPORT_MODE,
861                            new String[] {PortletDataHandlerKeys.
862                                    LAYOUTS_IMPORT_MODE_CREATED_FROM_PROTOTYPE});
863                    parameterMap.put(
864                            PortletDataHandlerKeys.LOGO,
865                            new String[] {Boolean.FALSE.toString()});
866                    parameterMap.put(
867                            PortletDataHandlerKeys.PERMISSIONS,
868                            new String[] {Boolean.TRUE.toString()});
869                    parameterMap.put(
870                            PortletDataHandlerKeys.PORTLET_CONFIGURATION,
871                            new String[] {Boolean.TRUE.toString()});
872                    parameterMap.put(
873                            PortletDataHandlerKeys.PORTLET_CONFIGURATION_ALL,
874                            new String[] {Boolean.TRUE.toString()});
875                    parameterMap.put(
876                            PortletDataHandlerKeys.PORTLET_DATA,
877                            new String[] {Boolean.TRUE.toString()});
878                    parameterMap.put(
879                            PortletDataHandlerKeys.PORTLET_DATA_ALL,
880                            new String[] {Boolean.TRUE.toString()});
881                    parameterMap.put(
882                            PortletDataHandlerKeys.PORTLET_SETUP_ALL,
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    }