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