001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.service.impl;
016    
017    import com.liferay.portal.DuplicateUserGroupException;
018    import com.liferay.portal.RequiredUserGroupException;
019    import com.liferay.portal.UserGroupNameException;
020    import com.liferay.portal.kernel.dao.orm.QueryUtil;
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             * @param  userId the primary key of the user
224             * @throws SystemException if a system exception occurred
225             */
226            @Override
227            public void clearUserUserGroups(long userId) throws SystemException {
228                    userPersistence.clearUserGroups(userId);
229    
230                    PermissionCacheUtil.clearCache(userId);
231            }
232    
233            /**
234             * Copies the user group's layout to the user.
235             *
236             * @param      userGroupId the primary key of the user group
237             * @param      userId the primary key of the user
238             * @throws     PortalException if a user with the primary key could not be
239             *             found or if a portal exception occurred
240             * @throws     SystemException if a system exception occurred
241             * @deprecated As of 6.2.0
242             */
243            @Override
244            public void copyUserGroupLayouts(long userGroupId, long userId)
245                    throws PortalException, SystemException {
246    
247                    Map<String, String[]> parameterMap = getLayoutTemplatesParameters();
248    
249                    File[] files = exportLayouts(userGroupId, parameterMap);
250    
251                    try {
252                            importLayouts(userId, parameterMap, files[0], files[1]);
253                    }
254                    finally {
255                            if (files[0] != null) {
256                                    files[0].delete();
257                            }
258    
259                            if (files[1] != null) {
260                                    files[1].delete();
261                            }
262                    }
263            }
264    
265            /**
266             * Copies the user group's layouts to the users who are not already members
267             * of the user group.
268             *
269             * @param      userGroupId the primary key of the user group
270             * @param      userIds the primary keys of the users
271             * @throws     PortalException if any one of the users could not be found or
272             *             if a portal exception occurred
273             * @throws     SystemException if a system exception occurred
274             * @deprecated As of 6.1.0
275             */
276            @Override
277            public void copyUserGroupLayouts(long userGroupId, long[] userIds)
278                    throws PortalException, SystemException {
279    
280                    Map<String, String[]> parameterMap = getLayoutTemplatesParameters();
281    
282                    File[] files = exportLayouts(userGroupId, parameterMap);
283    
284                    try {
285                            for (long userId : userIds) {
286                                    if (!userGroupPersistence.containsUser(userGroupId, userId)) {
287                                            importLayouts(userId, parameterMap, files[0], files[1]);
288                                    }
289                            }
290                    }
291                    finally {
292                            if (files[0] != null) {
293                                    files[0].delete();
294                            }
295    
296                            if (files[1] != null) {
297                                    files[1].delete();
298                            }
299                    }
300            }
301    
302            /**
303             * Copies the user groups' layouts to the user.
304             *
305             * @param      userGroupIds the primary keys of the user groups
306             * @param      userId the primary key of the user
307             * @throws     PortalException if a user with the primary key could not be
308             *             found or if a portal exception occurred
309             * @throws     SystemException if a system exception occurred
310             * @deprecated As of 6.1.0
311             */
312            @Override
313            public void copyUserGroupLayouts(long[] userGroupIds, long userId)
314                    throws PortalException, SystemException {
315    
316                    for (long userGroupId : userGroupIds) {
317                            if (!userGroupPersistence.containsUser(userGroupId, userId)) {
318                                    copyUserGroupLayouts(userGroupId, userId);
319                            }
320                    }
321            }
322    
323            /**
324             * Deletes the user group.
325             *
326             * @param  userGroupId the primary key of the user group
327             * @return the deleted user group
328             * @throws PortalException if a user group with the primary key could not be
329             *         found or if the user group had a workflow in approved status
330             * @throws SystemException if a system exception occurred
331             */
332            @Override
333            public UserGroup deleteUserGroup(long userGroupId)
334                    throws PortalException, SystemException {
335    
336                    UserGroup userGroup = userGroupPersistence.findByPrimaryKey(
337                            userGroupId);
338    
339                    return userGroupLocalService.deleteUserGroup(userGroup);
340            }
341    
342            /**
343             * Deletes the user group.
344             *
345             * @param  userGroup the user group
346             * @return the deleted user group
347             * @throws PortalException if the organization had a workflow in approved
348             *         status
349             * @throws SystemException if a system exception occurred
350             */
351            @Override
352            @SystemEvent(
353                    action = SystemEventConstants.ACTION_SKIP,
354                    type = SystemEventConstants.TYPE_DELETE)
355            public UserGroup deleteUserGroup(UserGroup userGroup)
356                    throws PortalException, SystemException {
357    
358                    int count = userLocalService.getUserGroupUsersCount(
359                            userGroup.getUserGroupId(), WorkflowConstants.STATUS_APPROVED);
360    
361                    if (count > 0) {
362                            throw new RequiredUserGroupException();
363                    }
364    
365                    // Expando
366    
367                    expandoRowLocalService.deleteRows(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            @Override
398            public void deleteUserGroups(long companyId)
399                    throws PortalException, SystemException {
400    
401                    List<UserGroup> userGroups = userGroupPersistence.findByCompanyId(
402                            companyId);
403    
404                    for (UserGroup userGroup : userGroups) {
405                            userGroupLocalService.deleteUserGroup(userGroup);
406                    }
407            }
408    
409            @Override
410            public UserGroup fetchUserGroup(long companyId, String name)
411                    throws SystemException {
412    
413                    return userGroupPersistence.fetchByC_N(companyId, name);
414            }
415    
416            /**
417             * Returns the user group with the name.
418             *
419             * @param  companyId the primary key of the user group's company
420             * @param  name the user group's name
421             * @return Returns the user group with the name
422             * @throws PortalException if a user group with the name could not be found
423             * @throws SystemException if a system exception occurred
424             */
425            @Override
426            public UserGroup getUserGroup(long companyId, String name)
427                    throws PortalException, SystemException {
428    
429                    return userGroupPersistence.findByC_N(companyId, name);
430            }
431    
432            /**
433             * Returns all the user groups belonging to the company.
434             *
435             * @param  companyId the primary key of the user groups' company
436             * @return the user groups belonging to the company
437             * @throws SystemException if a system exception occurred
438             */
439            @Override
440            public List<UserGroup> getUserGroups(long companyId)
441                    throws SystemException {
442    
443                    return userGroupPersistence.findByCompanyId(companyId);
444            }
445    
446            /**
447             * Returns all the user groups with the primary keys.
448             *
449             * @param  userGroupIds the primary keys of the user groups
450             * @return the user groups with the primary keys
451             * @throws PortalException if any one of the user groups could not be found
452             * @throws SystemException if a system exception occurred
453             */
454            @Override
455            public List<UserGroup> getUserGroups(long[] userGroupIds)
456                    throws PortalException, SystemException {
457    
458                    List<UserGroup> userGroups = new ArrayList<UserGroup>(
459                            userGroupIds.length);
460    
461                    for (long userGroupId : userGroupIds) {
462                            UserGroup userGroup = getUserGroup(userGroupId);
463    
464                            userGroups.add(userGroup);
465                    }
466    
467                    return userGroups;
468            }
469    
470            /**
471             * Returns an ordered range of all the user groups that match the keywords.
472             *
473             * <p>
474             * Useful when paginating results. Returns a maximum of <code>end -
475             * start</code> instances. <code>start</code> and <code>end</code> are not
476             * primary keys, they are indexes in the result set. Thus, <code>0</code>
477             * refers to the first result in the set. Setting both <code>start</code>
478             * and <code>end</code> to {@link
479             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
480             * result set.
481             * </p>
482             *
483             * @param  companyId the primary key of the user group's company
484             * @param  keywords the keywords (space separated), which may occur in the
485             *         user group's name or description (optionally <code>null</code>)
486             * @param  params the finder params (optionally <code>null</code>). For more
487             *         information see {@link
488             *         com.liferay.portal.service.persistence.UserGroupFinder}
489             * @param  start the lower bound of the range of user groups to return
490             * @param  end the upper bound of the range of user groups to return (not
491             *         inclusive)
492             * @param  obc the comparator to order the user groups (optionally
493             *         <code>null</code>)
494             * @return the matching user groups ordered by comparator <code>obc</code>
495             * @throws SystemException if a system exception occurred
496             * @see    com.liferay.portal.service.persistence.UserGroupFinder
497             */
498            @Override
499            public List<UserGroup> search(
500                            long companyId, String keywords,
501                            LinkedHashMap<String, Object> params, int start, int end,
502                            OrderByComparator obc)
503                    throws SystemException {
504    
505                    return userGroupFinder.findByKeywords(
506                            companyId, keywords, params, start, end, obc);
507            }
508    
509            /**
510             * Returns an ordered range of all the user groups that match the keywords,
511             * using the indexer. It is preferable to use this method instead of the
512             * non-indexed version whenever possible for performance reasons.
513             *
514             * <p>
515             * Useful when paginating results. Returns a maximum of <code>end -
516             * start</code> instances. <code>start</code> and <code>end</code> are not
517             * primary keys, they are indexes in the result set. Thus, <code>0</code>
518             * refers to the first result in the set. Setting both <code>start</code>
519             * and <code>end</code> to {@link
520             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
521             * result set.
522             * </p>
523             *
524             * @param  companyId the primary key of the user group's company
525             * @param  keywords the keywords (space separated), which may occur in the
526             *         user group's name or description (optionally <code>null</code>)
527             * @param  params the finder params (optionally <code>null</code>). For more
528             *         information see {@link
529             *         com.liferay.portlet.usergroupsadmin.util.UserGroupIndexer}
530             * @param  start the lower bound of the range of user groups to return
531             * @param  end the upper bound of the range of user groups to return (not
532             *         inclusive)
533             * @param  sort the field and direction by which to sort (optionally
534             *         <code>null</code>)
535             * @return the matching user groups ordered by sort
536             * @throws SystemException if a system exception occurred
537             * @see    com.liferay.portlet.usergroupsadmin.util.UserGroupIndexer
538             */
539            @Override
540            public Hits search(
541                            long companyId, String keywords,
542                            LinkedHashMap<String, Object> params, int start, int end, Sort sort)
543                    throws SystemException {
544    
545                    String name = null;
546                    String description = null;
547                    boolean andOperator = false;
548    
549                    if (Validator.isNotNull(keywords)) {
550                            name = keywords;
551                            description = keywords;
552                    }
553                    else {
554                            andOperator = true;
555                    }
556    
557                    if (params != null) {
558                            params.put("keywords", keywords);
559                    }
560    
561                    return search(
562                            companyId, name, description, params, andOperator, start, end,
563                            sort);
564            }
565    
566            /**
567             * Returns an ordered range of all the user groups that match the name and
568             * description. It is preferable to use this method instead of the
569             * non-indexed version whenever possible for performance reasons.
570             *
571             * <p>
572             * Useful when paginating results. Returns a maximum of <code>end -
573             * start</code> instances. <code>start</code> and <code>end</code> are not
574             * primary keys, they are indexes in the result set. Thus, <code>0</code>
575             * refers to the first result in the set. Setting both <code>start</code>
576             * and <code>end</code> to {@link
577             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
578             * result set.
579             * </p>
580             *
581             * @param  companyId the primary key of the user group's company
582             * @param  name the user group's name (optionally <code>null</code>)
583             * @param  description the user group's description (optionally
584             *         <code>null</code>)
585             * @param  params the finder params (optionally <code>null</code>). For more
586             *         information see {@link
587             *         com.liferay.portlet.usergroupsadmin.util.UserGroupIndexer}
588             * @param  andSearch whether every field must match its keywords or just one
589             *         field
590             * @param  start the lower bound of the range of user groups to return
591             * @param  end the upper bound of the range of user groups to return (not
592             *         inclusive)
593             * @param  sort the field and direction by which to sort (optionally
594             *         <code>null</code>)
595             * @return the matching user groups ordered by sort
596             * @throws SystemException if a system exception occurred
597             * @see    com.liferay.portal.service.persistence.UserGroupFinder
598             */
599            @Override
600            public Hits search(
601                            long companyId, String name, String description,
602                            LinkedHashMap<String, Object> params, boolean andSearch, int start,
603                            int end, Sort sort)
604                    throws SystemException {
605    
606                    try {
607                            SearchContext searchContext = buildSearchContext(
608                                    companyId, name, description, params, andSearch, start, end,
609                                    sort);
610    
611                            Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
612                                    UserGroup.class);
613    
614                            return indexer.search(searchContext);
615                    }
616                    catch (Exception e) {
617                            throw new SystemException(e);
618                    }
619            }
620    
621            /**
622             * Returns the number of user groups that match the keywords
623             *
624             * @param  companyId the primary key of the user group's company
625             * @param  keywords the keywords (space separated), which may occur in the
626             *         user group's name or description (optionally <code>null</code>)
627             * @param  params the finder params (optionally <code>null</code>). For more
628             *         information see {@link
629             *         com.liferay.portal.service.persistence.UserGroupFinder}
630             * @return the number of matching user groups
631             * @throws SystemException if a system exception occurred
632             * @see    com.liferay.portal.service.persistence.UserGroupFinder
633             */
634            @Override
635            public int searchCount(
636                            long companyId, String keywords,
637                            LinkedHashMap<String, Object> params)
638                    throws SystemException {
639    
640                    if (!PropsValues.USER_GROUPS_INDEXER_ENABLED ||
641                            !PropsValues.USER_GROUPS_SEARCH_WITH_INDEX ||
642                            isUseCustomSQL(params)) {
643    
644                            return userGroupFinder.countByKeywords(companyId, keywords, params);
645                    }
646    
647                    String name = null;
648                    String description = null;
649                    boolean andOperator = false;
650    
651                    if (Validator.isNotNull(keywords)) {
652                            name = keywords;
653                            description = keywords;
654                    }
655                    else {
656                            andOperator = true;
657                    }
658    
659                    if (params != null) {
660                            params.put("keywords", keywords);
661                    }
662    
663                    try {
664                            Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
665                                    UserGroup.class);
666    
667                            SearchContext searchContext = buildSearchContext(
668                                    companyId, name, description, params, andOperator,
669                                    QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
670    
671                            Hits hits = indexer.search(searchContext);
672    
673                            return hits.getLength();
674                    }
675                    catch (Exception e) {
676                            throw new SystemException(e);
677                    }
678            }
679    
680            /**
681             * Sets the user groups associated with the user copying the user group
682             * layouts and removing and adding user group associations for the user as
683             * necessary.
684             *
685             * @param  userId the primary key of the user
686             * @param  userGroupIds the primary keys of the user groups
687             * @throws PortalException if a portal exception occurred
688             * @throws SystemException if a system exception occurred
689             */
690            @Override
691            public void setUserUserGroups(long userId, long[] userGroupIds)
692                    throws PortalException, SystemException {
693    
694                    if (PropsValues.USER_GROUPS_COPY_LAYOUTS_TO_USER_PERSONAL_SITE) {
695                            copyUserGroupLayouts(userGroupIds, userId);
696                    }
697    
698                    userPersistence.setUserGroups(userId, userGroupIds);
699    
700                    Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);
701    
702                    indexer.reindex(userId);
703    
704                    PermissionCacheUtil.clearCache(userId);
705            }
706    
707            /**
708             * Removes the user groups from the group.
709             *
710             * @param  groupId the primary key of the group
711             * @param  userGroupIds the primary keys of the user groups
712             * @throws SystemException if a system exception occurred
713             */
714            @Override
715            public void unsetGroupUserGroups(long groupId, long[] userGroupIds)
716                    throws SystemException {
717    
718                    List<Team> teams = teamPersistence.findByGroupId(groupId);
719    
720                    for (Team team : teams) {
721                            teamPersistence.removeUserGroups(team.getTeamId(), userGroupIds);
722                    }
723    
724                    userGroupGroupRoleLocalService.deleteUserGroupGroupRoles(
725                            userGroupIds, groupId);
726    
727                    groupPersistence.removeUserGroups(groupId, userGroupIds);
728    
729                    PermissionCacheUtil.clearCache();
730            }
731    
732            /**
733             * Removes the user groups from the team.
734             *
735             * @param  teamId the primary key of the team
736             * @param  userGroupIds the primary keys of the user groups
737             * @throws SystemException if a system exception occurred
738             */
739            @Override
740            public void unsetTeamUserGroups(long teamId, long[] userGroupIds)
741                    throws SystemException {
742    
743                    teamPersistence.removeUserGroups(teamId, userGroupIds);
744    
745                    PermissionCacheUtil.clearCache();
746            }
747    
748            /**
749             * Updates the user group.
750             *
751             * @param      companyId the primary key of the user group's company
752             * @param      userGroupId the primary key of the user group
753             * @param      name the user group's name
754             * @param      description the user group's description
755             * @return     the user group
756             * @throws     PortalException if a user group with the primary key could
757             *             not be found or if the new information was invalid
758             * @throws     SystemException if a system exception occurred
759             * @deprecated As of 6.2.0, replaced by {@link #updateUserGroup(long, long,
760             *             String, String, ServiceContext)}
761             */
762            @Override
763            public UserGroup updateUserGroup(
764                            long companyId, long userGroupId, String name, String description)
765                    throws PortalException, SystemException {
766    
767                    return updateUserGroup(companyId, userGroupId, name, description, null);
768            }
769    
770            /**
771             * Updates the user group.
772             *
773             * @param  companyId the primary key of the user group's company
774             * @param  userGroupId the primary key of the user group
775             * @param  name the user group's name
776             * @param  description the user group's description
777             * @param  serviceContext the service context to be applied (optionally
778             *         <code>null</code>). Can set expando bridge attributes for the
779             *         user group.
780             * @return the user group
781             * @throws PortalException if a user group with the primary key could not be
782             *         found or if the new information was invalid
783             * @throws SystemException if a system exception occurred
784             */
785            @Override
786            public UserGroup updateUserGroup(
787                            long companyId, long userGroupId, String name, String description,
788                            ServiceContext serviceContext)
789                    throws PortalException, SystemException {
790    
791                    // User group
792    
793                    validate(userGroupId, companyId, name);
794    
795                    UserGroup userGroup = userGroupPersistence.findByPrimaryKey(
796                            userGroupId);
797    
798                    userGroup.setModifiedDate(new Date());
799                    userGroup.setName(name);
800                    userGroup.setDescription(description);
801                    userGroup.setExpandoBridgeAttributes(serviceContext);
802    
803                    userGroupPersistence.update(userGroup);
804    
805                    // Indexer
806    
807                    Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
808                            UserGroup.class);
809    
810                    indexer.reindex(userGroup);
811    
812                    return userGroup;
813            }
814    
815            protected SearchContext buildSearchContext(
816                    long companyId, String name, String description,
817                    LinkedHashMap<String, Object> params, boolean andSearch, int start,
818                    int end, Sort sort) {
819    
820                    SearchContext searchContext = new SearchContext();
821    
822                    searchContext.setAndSearch(andSearch);
823    
824                    Map<String, Serializable> attributes =
825                            new HashMap<String, Serializable>();
826    
827                    attributes.put("description", description);
828                    attributes.put("name", name);
829    
830                    searchContext.setAttributes(attributes);
831    
832                    searchContext.setCompanyId(companyId);
833                    searchContext.setEnd(end);
834    
835                    if (params != null) {
836                            String keywords = (String)params.remove("keywords");
837    
838                            if (Validator.isNotNull(keywords)) {
839                                    searchContext.setKeywords(keywords);
840                            }
841                    }
842    
843                    QueryConfig queryConfig = new QueryConfig();
844    
845                    queryConfig.setHighlightEnabled(false);
846                    queryConfig.setScoreEnabled(false);
847    
848                    searchContext.setQueryConfig(queryConfig);
849    
850                    if (sort != null) {
851                            searchContext.setSorts(sort);
852                    }
853    
854                    searchContext.setStart(start);
855    
856                    return searchContext;
857            }
858    
859            protected File[] exportLayouts(
860                            long userGroupId, Map<String, String[]> parameterMap)
861                    throws PortalException, SystemException {
862    
863                    File[] files = new File[2];
864    
865                    UserGroup userGroup = userGroupPersistence.findByPrimaryKey(
866                            userGroupId);
867    
868                    Group group = userGroup.getGroup();
869    
870                    if (userGroup.hasPrivateLayouts()) {
871                            files[0] = layoutLocalService.exportLayoutsAsFile(
872                                    group.getGroupId(), true, null, parameterMap, null, null);
873                    }
874    
875                    if (userGroup.hasPublicLayouts()) {
876                            files[1] = layoutLocalService.exportLayoutsAsFile(
877                                    group.getGroupId(), false, null, parameterMap, null, null);
878                    }
879    
880                    return files;
881            }
882    
883            protected Map<String, String[]> getLayoutTemplatesParameters() {
884                    Map<String, String[]> parameterMap =
885                            new LinkedHashMap<String, String[]>();
886    
887                    parameterMap.put(
888                            PortletDataHandlerKeys.CATEGORIES,
889                            new String[] {Boolean.TRUE.toString()});
890                    parameterMap.put(
891                            PortletDataHandlerKeys.DATA_STRATEGY,
892                            new String[] {PortletDataHandlerKeys.DATA_STRATEGY_MIRROR});
893                    parameterMap.put(
894                            PortletDataHandlerKeys.DELETE_MISSING_LAYOUTS,
895                            new String[] {Boolean.FALSE.toString()});
896                    parameterMap.put(
897                            PortletDataHandlerKeys.DELETE_PORTLET_DATA,
898                            new String[] {Boolean.FALSE.toString()});
899                    parameterMap.put(
900                            PortletDataHandlerKeys.LAYOUT_SET_SETTINGS,
901                            new String[] {Boolean.FALSE.toString()});
902                    parameterMap.put(
903                            PortletDataHandlerKeys.LAYOUTS_IMPORT_MODE,
904                            new String[] {PortletDataHandlerKeys.
905                                    LAYOUTS_IMPORT_MODE_CREATED_FROM_PROTOTYPE});
906                    parameterMap.put(
907                            PortletDataHandlerKeys.LOGO,
908                            new String[] {Boolean.FALSE.toString()});
909                    parameterMap.put(
910                            PortletDataHandlerKeys.PERMISSIONS,
911                            new String[] {Boolean.TRUE.toString()});
912                    parameterMap.put(
913                            PortletDataHandlerKeys.PORTLET_CONFIGURATION,
914                            new String[] {Boolean.TRUE.toString()});
915                    parameterMap.put(
916                            PortletDataHandlerKeys.PORTLET_CONFIGURATION_ALL,
917                            new String[] {Boolean.TRUE.toString()});
918                    parameterMap.put(
919                            PortletDataHandlerKeys.PORTLET_DATA,
920                            new String[] {Boolean.TRUE.toString()});
921                    parameterMap.put(
922                            PortletDataHandlerKeys.PORTLET_DATA_ALL,
923                            new String[] {Boolean.TRUE.toString()});
924                    parameterMap.put(
925                            PortletDataHandlerKeys.PORTLET_SETUP_ALL,
926                            new String[] {Boolean.TRUE.toString()});
927                    parameterMap.put(
928                            PortletDataHandlerKeys.PORTLETS_MERGE_MODE,
929                            new String[] {PortletDataHandlerKeys.
930                                    PORTLETS_MERGE_MODE_ADD_TO_BOTTOM});
931                    parameterMap.put(
932                            PortletDataHandlerKeys.THEME_REFERENCE,
933                            new String[] {Boolean.TRUE.toString()});
934                    parameterMap.put(
935                            PortletDataHandlerKeys.UPDATE_LAST_PUBLISH_DATE,
936                            new String[] {Boolean.FALSE.toString()});
937                    parameterMap.put(
938                            PortletDataHandlerKeys.USER_ID_STRATEGY,
939                            new String[] {UserIdStrategy.CURRENT_USER_ID});
940    
941                    return parameterMap;
942            }
943    
944            protected void importLayouts(
945                            long userId, Map<String, String[]> parameterMap,
946                            File privateLayoutsFile, File publicLayoutsFile)
947                    throws PortalException, SystemException {
948    
949                    User user = userPersistence.findByPrimaryKey(userId);
950    
951                    long groupId = user.getGroupId();
952    
953                    if (privateLayoutsFile != null) {
954                            layoutLocalService.importLayouts(
955                                    userId, groupId, true, parameterMap, privateLayoutsFile);
956                    }
957    
958                    if (publicLayoutsFile != null) {
959                            layoutLocalService.importLayouts(
960                                    userId, groupId, false, parameterMap, publicLayoutsFile);
961                    }
962            }
963    
964            protected boolean isUseCustomSQL(LinkedHashMap<String, Object> params) {
965                    if ((params == null) || params.isEmpty()) {
966                            return false;
967                    }
968    
969                    return true;
970            }
971    
972            protected void validate(long userGroupId, long companyId, String name)
973                    throws PortalException, SystemException {
974    
975                    if (Validator.isNull(name) ||
976                            (name.indexOf(CharPool.COMMA) != -1) ||
977                            (name.indexOf(CharPool.STAR) != -1)) {
978    
979                            throw new UserGroupNameException();
980                    }
981    
982                    if (Validator.isNumber(name) &&
983                            !PropsValues.USER_GROUPS_NAME_ALLOW_NUMERIC) {
984    
985                            throw new UserGroupNameException();
986                    }
987    
988                    UserGroup userGroup = fetchUserGroup(companyId, name);
989    
990                    if ((userGroup != null) &&
991                            (userGroup.getUserGroupId() != userGroupId)) {
992    
993                            throw new DuplicateUserGroupException("{name=" + name + "}");
994                    }
995            }
996    
997    }