001    /**
002     * Copyright (c) 2000-2010 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.Indexer;
026    import com.liferay.portal.kernel.search.IndexerRegistryUtil;
027    import com.liferay.portal.kernel.util.OrderByComparator;
028    import com.liferay.portal.kernel.util.StringPool;
029    import com.liferay.portal.kernel.util.Validator;
030    import com.liferay.portal.model.Group;
031    import com.liferay.portal.model.ResourceConstants;
032    import com.liferay.portal.model.User;
033    import com.liferay.portal.model.UserGroup;
034    import com.liferay.portal.model.UserGroupConstants;
035    import com.liferay.portal.security.permission.PermissionCacheUtil;
036    import com.liferay.portal.service.base.UserGroupLocalServiceBaseImpl;
037    
038    import java.io.File;
039    
040    import java.util.ArrayList;
041    import java.util.LinkedHashMap;
042    import java.util.List;
043    import java.util.Map;
044    
045    /**
046     * @author Charles May
047     */
048    public class UserGroupLocalServiceImpl extends UserGroupLocalServiceBaseImpl {
049    
050            public void addGroupUserGroups(long groupId, long[] userGroupIds)
051                    throws SystemException {
052    
053                    groupPersistence.addUserGroups(groupId, userGroupIds);
054    
055                    PermissionCacheUtil.clearCache();
056            }
057    
058            public UserGroup addUserGroup(
059                            long userId, long companyId, String name, String description)
060                    throws PortalException, SystemException {
061    
062                    // User Group
063    
064                    validate(0, companyId, name);
065    
066                    long userGroupId = counterLocalService.increment();
067    
068                    UserGroup userGroup = userGroupPersistence.create(userGroupId);
069    
070                    userGroup.setCompanyId(companyId);
071                    userGroup.setParentUserGroupId(
072                            UserGroupConstants.DEFAULT_PARENT_USER_GROUP_ID);
073                    userGroup.setName(name);
074                    userGroup.setDescription(description);
075    
076                    userGroupPersistence.update(userGroup, false);
077    
078                    // Group
079    
080                    groupLocalService.addGroup(
081                            userId, UserGroup.class.getName(), userGroup.getUserGroupId(),
082                            String.valueOf(userGroupId), null, 0, null, true, null);
083    
084                    // Resources
085    
086                    resourceLocalService.addResources(
087                            companyId, 0, userId, UserGroup.class.getName(),
088                            userGroup.getUserGroupId(), false, false, false);
089    
090                    return userGroup;
091            }
092    
093            public void clearUserUserGroups(long userId) throws SystemException {
094                    userPersistence.clearUserGroups(userId);
095    
096                    PermissionCacheUtil.clearCache();
097            }
098    
099            public void copyUserGroupLayouts(long userGroupId, long userIds[])
100                    throws PortalException, SystemException {
101    
102                    Map<String, String[]> parameterMap = getLayoutTemplatesParameters();
103    
104                    File[] files = exportLayouts(userGroupId, parameterMap);
105    
106                    try {
107                            for (long userId : userIds) {
108                                    if (!userGroupPersistence.containsUser(userGroupId, userId)) {
109                                            importLayouts(userId, parameterMap, files[0], files[1]);
110                                    }
111                            }
112                    }
113                    finally {
114                            if (files[0] != null) {
115                                    files[0].delete();
116                            }
117    
118                            if (files[1] != null) {
119                                    files[1].delete();
120                            }
121                    }
122            }
123    
124            public void copyUserGroupLayouts(long userGroupIds[], long userId)
125                    throws PortalException, SystemException {
126    
127                    for (long userGroupId : userGroupIds) {
128                            if (!userGroupPersistence.containsUser(userGroupId, userId)) {
129                                    copyUserGroupLayouts(userGroupId, userId);
130                            }
131                    }
132            }
133    
134            public void copyUserGroupLayouts(long userGroupId, long userId)
135                    throws PortalException, SystemException {
136    
137                    Map<String, String[]> parameterMap = getLayoutTemplatesParameters();
138    
139                    File[] files = exportLayouts(userGroupId, parameterMap);
140    
141                    try {
142                            importLayouts(userId, parameterMap, files[0], files[1]);
143                    }
144                    finally {
145                            if (files[0] != null) {
146                                    files[0].delete();
147                            }
148    
149                            if (files[1] != null) {
150                                    files[1].delete();
151                            }
152                    }
153            }
154    
155            public void deleteUserGroup(long userGroupId)
156                    throws PortalException, SystemException {
157    
158                    UserGroup userGroup = userGroupPersistence.findByPrimaryKey(
159                            userGroupId);
160    
161                    if (userLocalService.getUserGroupUsersCount(userGroupId, true) > 0) {
162                            throw new RequiredUserGroupException();
163                    }
164    
165                    // Users
166    
167                    clearUserUserGroups(userGroupId);
168    
169                    // Group
170    
171                    Group group = userGroup.getGroup();
172    
173                    groupLocalService.deleteGroup(group.getGroupId());
174    
175                    // User group roles
176    
177                    userGroupGroupRoleLocalService.deleteUserGroupGroupRolesByUserGroupId(
178                            userGroupId);
179    
180                    // Resources
181    
182                    resourceLocalService.deleteResource(
183                            userGroup.getCompanyId(), UserGroup.class.getName(),
184                            ResourceConstants.SCOPE_INDIVIDUAL, userGroup.getUserGroupId());
185    
186                    // User Group
187    
188                    userGroupPersistence.remove(userGroupId);
189    
190                    // Permission cache
191    
192                    PermissionCacheUtil.clearCache();
193            }
194    
195            public UserGroup getUserGroup(long userGroupId)
196                    throws PortalException, SystemException {
197    
198                    return userGroupPersistence.findByPrimaryKey(userGroupId);
199            }
200    
201            public UserGroup getUserGroup(long companyId, String name)
202                    throws PortalException, SystemException {
203    
204                    return userGroupPersistence.findByC_N(companyId, name);
205            }
206    
207            public List<UserGroup> getUserGroups(long companyId)
208                    throws SystemException {
209    
210                    return userGroupPersistence.findByCompanyId(companyId);
211            }
212    
213            public List<UserGroup> getUserGroups(long[] userGroupIds)
214                    throws PortalException, SystemException {
215    
216                    List<UserGroup> userGroups = new ArrayList<UserGroup>(
217                            userGroupIds.length);
218    
219                    for (long userGroupId : userGroupIds) {
220                            UserGroup userGroup = getUserGroup(userGroupId);
221    
222                            userGroups.add(userGroup);
223                    }
224    
225                    return userGroups;
226            }
227    
228            public List<UserGroup> getUserUserGroups(long userId)
229                    throws SystemException {
230    
231                    return userPersistence.getUserGroups(userId);
232            }
233    
234            public boolean hasGroupUserGroup(long groupId, long userGroupId)
235                    throws SystemException {
236    
237                    return groupPersistence.containsUserGroup(groupId, userGroupId);
238            }
239    
240            public List<UserGroup> search(
241                            long companyId, String name, String description,
242                            LinkedHashMap<String, Object> params, int start, int end,
243                            OrderByComparator obc)
244                    throws SystemException {
245    
246                    return userGroupFinder.findByC_N_D(
247                            companyId, name, description, params, start, end, obc);
248            }
249    
250            public int searchCount(
251                            long companyId, String name, String description,
252                            LinkedHashMap<String, Object> params)
253                    throws SystemException {
254    
255                    return userGroupFinder.countByC_N_D(
256                            companyId, name, description, params);
257            }
258    
259            public void setUserUserGroups(long userId, long[] userGroupIds)
260                    throws PortalException, SystemException {
261    
262                    copyUserGroupLayouts(userGroupIds, userId);
263    
264                    userPersistence.setUserGroups(userId, userGroupIds);
265    
266                    Indexer indexer = IndexerRegistryUtil.getIndexer(User.class);
267    
268                    indexer.reindex(userId);
269    
270                    PermissionCacheUtil.clearCache();
271            }
272    
273            public void unsetGroupUserGroups(long groupId, long[] userGroupIds)
274                    throws SystemException {
275    
276                    userGroupGroupRoleLocalService.deleteUserGroupGroupRoles(
277                            userGroupIds, groupId);
278    
279                    groupPersistence.removeUserGroups(groupId, userGroupIds);
280    
281                    PermissionCacheUtil.clearCache();
282            }
283    
284            public UserGroup updateUserGroup(
285                            long companyId, long userGroupId, String name,
286                            String description)
287                    throws PortalException, SystemException {
288    
289                    validate(userGroupId, companyId, name);
290    
291                    UserGroup userGroup = userGroupPersistence.findByPrimaryKey(
292                            userGroupId);
293    
294                    userGroup.setName(name);
295                    userGroup.setDescription(description);
296    
297                    userGroupPersistence.update(userGroup, false);
298    
299                    return userGroup;
300            }
301    
302            protected File[] exportLayouts(
303                            long userGroupId, Map<String, String[]> parameterMap)
304                    throws PortalException, SystemException {
305    
306                    File[] files = new File[2];
307    
308                    UserGroup userGroup = userGroupLocalService.getUserGroup(userGroupId);
309    
310                    long groupId = userGroup.getGroup().getGroupId();
311    
312                    if (userGroup.hasPrivateLayouts()) {
313                            files[0] = layoutLocalService.exportLayoutsAsFile(
314                                    groupId, true, null, parameterMap, null, null);
315                    }
316    
317                    if (userGroup.hasPublicLayouts()) {
318                            files[1] = layoutLocalService.exportLayoutsAsFile(
319                                    groupId, false, null, parameterMap, null, null);
320                    }
321    
322                    return files;
323            }
324    
325            protected Map<String, String[]> getLayoutTemplatesParameters() {
326                    Map<String, String[]> parameterMap =
327                            new LinkedHashMap<String, String[]>();
328    
329                    parameterMap.put(
330                            PortletDataHandlerKeys.CATEGORIES,
331                            new String[] {Boolean.TRUE.toString()});
332                    parameterMap.put(
333                            PortletDataHandlerKeys.DATA_STRATEGY,
334                            new String[] {PortletDataHandlerKeys.DATA_STRATEGY_MIRROR});
335                    parameterMap.put(
336                            PortletDataHandlerKeys.DELETE_MISSING_LAYOUTS,
337                            new String[] {Boolean.FALSE.toString()});
338                    parameterMap.put(
339                            PortletDataHandlerKeys.DELETE_PORTLET_DATA,
340                            new String[] {Boolean.FALSE.toString()});
341                    parameterMap.put(
342                            PortletDataHandlerKeys.LAYOUTS_IMPORT_MODE,
343                            new String[] {PortletDataHandlerKeys.
344                                    LAYOUTS_IMPORT_MODE_MERGE_BY_LAYOUT_NAME});
345                    parameterMap.put(
346                            PortletDataHandlerKeys.PERMISSIONS,
347                            new String[] {Boolean.TRUE.toString()});
348                    parameterMap.put(
349                            PortletDataHandlerKeys.PORTLET_DATA,
350                            new String[] {Boolean.TRUE.toString()});
351                    parameterMap.put(
352                            PortletDataHandlerKeys.PORTLET_DATA_ALL,
353                            new String[] {Boolean.TRUE.toString()});
354                    parameterMap.put(
355                            PortletDataHandlerKeys.PORTLET_SETUP,
356                            new String[] {Boolean.TRUE.toString()});
357                    parameterMap.put(
358                            PortletDataHandlerKeys.PORTLET_USER_PREFERENCES,
359                            new String[] {Boolean.TRUE.toString()});
360                    parameterMap.put(
361                            PortletDataHandlerKeys.PORTLETS_MERGE_MODE,
362                            new String[] {PortletDataHandlerKeys.
363                                    PORTLETS_MERGE_MODE_ADD_TO_BOTTOM});
364                    parameterMap.put(
365                            PortletDataHandlerKeys.THEME,
366                            new String[] {Boolean.FALSE.toString()});
367                    parameterMap.put(
368                            PortletDataHandlerKeys.USER_ID_STRATEGY,
369                            new String[] {UserIdStrategy.CURRENT_USER_ID});
370                    parameterMap.put(
371                            PortletDataHandlerKeys.USER_PERMISSIONS,
372                            new String[] {Boolean.FALSE.toString()});
373    
374                    return parameterMap;
375            }
376    
377            protected void importLayouts(
378                            long userId, Map<String, String[]> parameterMap,
379                            File privateLayoutsFile, File publicLayoutsFile)
380                    throws PortalException, SystemException {
381    
382                    User user = userPersistence.findByPrimaryKey(userId);
383    
384                    long groupId = user.getGroup().getGroupId();
385    
386                    if (privateLayoutsFile != null) {
387                            layoutLocalService.importLayouts(
388                                    userId, groupId, true, parameterMap, privateLayoutsFile);
389                    }
390    
391                    if (publicLayoutsFile != null) {
392                            layoutLocalService.importLayouts(
393                                    userId, groupId, false, parameterMap, publicLayoutsFile);
394                    }
395            }
396    
397            protected void validate(long userGroupId, long companyId, String name)
398                    throws PortalException, SystemException {
399    
400                    if ((Validator.isNull(name)) || (Validator.isNumber(name)) ||
401                            (name.indexOf(StringPool.COMMA) != -1) ||
402                            (name.indexOf(StringPool.STAR) != -1)) {
403    
404                            throw new UserGroupNameException();
405                    }
406    
407                    try {
408                            UserGroup userGroup = userGroupFinder.findByC_N(companyId, name);
409    
410                            if (userGroup.getUserGroupId() != userGroupId) {
411                                    throw new DuplicateUserGroupException();
412                            }
413                    }
414                    catch (NoSuchUserGroupException nsuge) {
415                    }
416            }
417    
418    }