1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.service.impl;
16  
17  import com.liferay.portal.DuplicateUserGroupException;
18  import com.liferay.portal.NoSuchUserGroupException;
19  import com.liferay.portal.RequiredUserGroupException;
20  import com.liferay.portal.UserGroupNameException;
21  import com.liferay.portal.kernel.exception.PortalException;
22  import com.liferay.portal.kernel.exception.SystemException;
23  import com.liferay.portal.kernel.lar.PortletDataHandlerKeys;
24  import com.liferay.portal.kernel.lar.UserIdStrategy;
25  import com.liferay.portal.kernel.search.Indexer;
26  import com.liferay.portal.kernel.search.IndexerRegistryUtil;
27  import com.liferay.portal.kernel.util.OrderByComparator;
28  import com.liferay.portal.kernel.util.StringPool;
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.portal.model.Group;
31  import com.liferay.portal.model.ResourceConstants;
32  import com.liferay.portal.model.User;
33  import com.liferay.portal.model.UserGroup;
34  import com.liferay.portal.model.UserGroupConstants;
35  import com.liferay.portal.security.permission.PermissionCacheUtil;
36  import com.liferay.portal.service.base.UserGroupLocalServiceBaseImpl;
37  
38  import java.io.File;
39  
40  import java.util.ArrayList;
41  import java.util.LinkedHashMap;
42  import java.util.List;
43  import java.util.Map;
44  
45  /**
46   * <a href="UserGroupLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
47   *
48   * @author Charles May
49   */
50  public class UserGroupLocalServiceImpl extends UserGroupLocalServiceBaseImpl {
51  
52      public void addGroupUserGroups(long groupId, long[] userGroupIds)
53          throws SystemException {
54  
55          groupPersistence.addUserGroups(groupId, userGroupIds);
56  
57          PermissionCacheUtil.clearCache();
58      }
59  
60      public UserGroup addUserGroup(
61              long userId, long companyId, String name, String description)
62          throws PortalException, SystemException {
63  
64          // User Group
65  
66          validate(0, companyId, name);
67  
68          long userGroupId = counterLocalService.increment();
69  
70          UserGroup userGroup = userGroupPersistence.create(userGroupId);
71  
72          userGroup.setCompanyId(companyId);
73          userGroup.setParentUserGroupId(
74              UserGroupConstants.DEFAULT_PARENT_USER_GROUP_ID);
75          userGroup.setName(name);
76          userGroup.setDescription(description);
77  
78          userGroupPersistence.update(userGroup, false);
79  
80          // Group
81  
82          groupLocalService.addGroup(
83              userId, UserGroup.class.getName(), userGroup.getUserGroupId(),
84              String.valueOf(userGroupId), null, 0, null, true, null);
85  
86          // Resources
87  
88          resourceLocalService.addResources(
89              companyId, 0, userId, UserGroup.class.getName(),
90              userGroup.getUserGroupId(), false, false, false);
91  
92          return userGroup;
93      }
94  
95      public void clearUserUserGroups(long userId) throws SystemException {
96          userPersistence.clearUserGroups(userId);
97  
98          PermissionCacheUtil.clearCache();
99      }
100 
101     public void copyUserGroupLayouts(long userGroupId, long userIds[])
102         throws PortalException, SystemException {
103 
104         Map<String, String[]> parameterMap = getLayoutTemplatesParameters();
105 
106         File[] files = exportLayouts(userGroupId, parameterMap);
107 
108         try {
109             for (long userId : userIds) {
110                 if (!userGroupPersistence.containsUser(userGroupId, userId)) {
111                     importLayouts(userId, parameterMap, files[0], files[1]);
112                 }
113             }
114         }
115         finally {
116             if (files[0] != null) {
117                 files[0].delete();
118             }
119 
120             if (files[1] != null) {
121                 files[1].delete();
122             }
123         }
124     }
125 
126     public void copyUserGroupLayouts(long userGroupIds[], long userId)
127         throws PortalException, SystemException {
128 
129         for (long userGroupId : userGroupIds) {
130             if (!userGroupPersistence.containsUser(userGroupId, userId)) {
131                 copyUserGroupLayouts(userGroupId, userId);
132             }
133         }
134     }
135 
136     public void copyUserGroupLayouts(long userGroupId, long userId)
137         throws PortalException, SystemException {
138 
139         Map<String, String[]> parameterMap = getLayoutTemplatesParameters();
140 
141         File[] files = exportLayouts(userGroupId, parameterMap);
142 
143         try {
144             importLayouts(userId, parameterMap, files[0], files[1]);
145         }
146         finally {
147             if (files[0] != null) {
148                 files[0].delete();
149             }
150 
151             if (files[1] != null) {
152                 files[1].delete();
153             }
154         }
155     }
156 
157     public void deleteUserGroup(long userGroupId)
158         throws PortalException, SystemException {
159 
160         UserGroup userGroup = userGroupPersistence.findByPrimaryKey(
161             userGroupId);
162 
163         if (userLocalService.getUserGroupUsersCount(userGroupId, true) > 0) {
164             throw new RequiredUserGroupException();
165         }
166 
167         // Users
168 
169         clearUserUserGroups(userGroupId);
170 
171         // Group
172 
173         Group group = userGroup.getGroup();
174 
175         groupLocalService.deleteGroup(group.getGroupId());
176 
177         // User group roles
178 
179         userGroupGroupRoleLocalService.deleteUserGroupGroupRolesByUserGroupId(
180             userGroupId);
181 
182         // Resources
183 
184         resourceLocalService.deleteResource(
185             userGroup.getCompanyId(), UserGroup.class.getName(),
186             ResourceConstants.SCOPE_INDIVIDUAL, userGroup.getUserGroupId());
187 
188         // User Group
189 
190         userGroupPersistence.remove(userGroupId);
191 
192         // Permission cache
193 
194         PermissionCacheUtil.clearCache();
195     }
196 
197     public UserGroup getUserGroup(long userGroupId)
198         throws PortalException, SystemException {
199 
200         return userGroupPersistence.findByPrimaryKey(userGroupId);
201     }
202 
203     public UserGroup getUserGroup(long companyId, String name)
204         throws PortalException, SystemException {
205 
206         return userGroupPersistence.findByC_N(companyId, name);
207     }
208 
209     public List<UserGroup> getUserGroups(long companyId)
210         throws SystemException {
211 
212         return userGroupPersistence.findByCompanyId(companyId);
213     }
214 
215     public List<UserGroup> getUserGroups(long[] userGroupIds)
216         throws PortalException, SystemException {
217 
218         List<UserGroup> userGroups = new ArrayList<UserGroup>(
219             userGroupIds.length);
220 
221         for (long userGroupId : userGroupIds) {
222             UserGroup userGroup = getUserGroup(userGroupId);
223 
224             userGroups.add(userGroup);
225         }
226 
227         return userGroups;
228     }
229 
230     public List<UserGroup> getUserUserGroups(long userId)
231         throws SystemException {
232 
233         return userPersistence.getUserGroups(userId);
234     }
235 
236     public boolean hasGroupUserGroup(long groupId, long userGroupId)
237         throws SystemException {
238 
239         return groupPersistence.containsUserGroup(groupId, userGroupId);
240     }
241 
242     public List<UserGroup> search(
243             long companyId, String name, String description,
244             LinkedHashMap<String, Object> params, int start, int end,
245             OrderByComparator obc)
246         throws SystemException {
247 
248         return userGroupFinder.findByC_N_D(
249             companyId, name, description, params, start, end, obc);
250     }
251 
252     public int searchCount(
253             long companyId, String name, String description,
254             LinkedHashMap<String, Object> params)
255         throws SystemException {
256 
257         return userGroupFinder.countByC_N_D(
258             companyId, name, description, params);
259     }
260 
261     public void setUserUserGroups(long userId, long[] userGroupIds)
262         throws PortalException, SystemException {
263 
264         copyUserGroupLayouts(userGroupIds, userId);
265 
266         userPersistence.setUserGroups(userId, userGroupIds);
267 
268         Indexer indexer = IndexerRegistryUtil.getIndexer(User.class);
269 
270         indexer.reindex(userId);
271 
272         PermissionCacheUtil.clearCache();
273     }
274 
275     public void unsetGroupUserGroups(long groupId, long[] userGroupIds)
276         throws SystemException {
277 
278         userGroupGroupRoleLocalService.deleteUserGroupGroupRoles(
279             userGroupIds, groupId);
280 
281         groupPersistence.removeUserGroups(groupId, userGroupIds);
282 
283         PermissionCacheUtil.clearCache();
284     }
285 
286     public UserGroup updateUserGroup(
287             long companyId, long userGroupId, String name,
288             String description)
289         throws PortalException, SystemException {
290 
291         validate(userGroupId, companyId, name);
292 
293         UserGroup userGroup = userGroupPersistence.findByPrimaryKey(
294             userGroupId);
295 
296         userGroup.setName(name);
297         userGroup.setDescription(description);
298 
299         userGroupPersistence.update(userGroup, false);
300 
301         return userGroup;
302     }
303 
304     protected File[] exportLayouts(
305             long userGroupId, Map<String, String[]> parameterMap)
306         throws PortalException, SystemException {
307 
308         File[] files = new File[2];
309 
310         UserGroup userGroup = userGroupLocalService.getUserGroup(userGroupId);
311 
312         long groupId = userGroup.getGroup().getGroupId();
313 
314         if (userGroup.hasPrivateLayouts()) {
315             files[0] = layoutLocalService.exportLayoutsAsFile(
316                 groupId, true, null, parameterMap, null, null);
317         }
318 
319         if (userGroup.hasPublicLayouts()) {
320             files[1] = layoutLocalService.exportLayoutsAsFile(
321                 groupId, false, null, parameterMap, null, null);
322         }
323 
324         return files;
325     }
326 
327     protected Map<String, String[]> getLayoutTemplatesParameters() {
328         Map<String, String[]> parameterMap =
329             new LinkedHashMap<String, String[]>();
330 
331         parameterMap.put(
332             PortletDataHandlerKeys.CATEGORIES,
333             new String[] {Boolean.TRUE.toString()});
334         parameterMap.put(
335             PortletDataHandlerKeys.DATA_STRATEGY,
336             new String[] {PortletDataHandlerKeys.DATA_STRATEGY_MIRROR});
337         parameterMap.put(
338             PortletDataHandlerKeys.DELETE_MISSING_LAYOUTS,
339             new String[] {Boolean.FALSE.toString()});
340         parameterMap.put(
341             PortletDataHandlerKeys.DELETE_PORTLET_DATA,
342             new String[] {Boolean.FALSE.toString()});
343         parameterMap.put(
344             PortletDataHandlerKeys.LAYOUTS_IMPORT_MODE,
345             new String[] {PortletDataHandlerKeys.
346                 LAYOUTS_IMPORT_MODE_MERGE_BY_LAYOUT_NAME});
347         parameterMap.put(
348             PortletDataHandlerKeys.PERMISSIONS,
349             new String[] {Boolean.TRUE.toString()});
350         parameterMap.put(
351             PortletDataHandlerKeys.PORTLET_DATA,
352             new String[] {Boolean.TRUE.toString()});
353         parameterMap.put(
354             PortletDataHandlerKeys.PORTLET_DATA_ALL,
355             new String[] {Boolean.TRUE.toString()});
356         parameterMap.put(
357             PortletDataHandlerKeys.PORTLET_SETUP,
358             new String[] {Boolean.TRUE.toString()});
359         parameterMap.put(
360             PortletDataHandlerKeys.PORTLET_USER_PREFERENCES,
361             new String[] {Boolean.TRUE.toString()});
362         parameterMap.put(
363             PortletDataHandlerKeys.PORTLETS_MERGE_MODE,
364             new String[] {PortletDataHandlerKeys.
365                 PORTLETS_MERGE_MODE_ADD_TO_BOTTOM});
366         parameterMap.put(
367             PortletDataHandlerKeys.THEME,
368             new String[] {Boolean.FALSE.toString()});
369         parameterMap.put(
370             PortletDataHandlerKeys.USER_ID_STRATEGY,
371             new String[] {UserIdStrategy.CURRENT_USER_ID});
372         parameterMap.put(
373             PortletDataHandlerKeys.USER_PERMISSIONS,
374             new String[] {Boolean.FALSE.toString()});
375 
376         return parameterMap;
377     }
378 
379     protected void importLayouts(
380             long userId, Map<String, String[]> parameterMap,
381             File privateLayoutsFile, File publicLayoutsFile)
382         throws PortalException, SystemException {
383 
384         User user = userPersistence.findByPrimaryKey(userId);
385 
386         long groupId = user.getGroup().getGroupId();
387 
388         if (privateLayoutsFile != null) {
389             layoutLocalService.importLayouts(
390                 userId, groupId, true, parameterMap, privateLayoutsFile);
391         }
392 
393         if (publicLayoutsFile != null) {
394             layoutLocalService.importLayouts(
395                 userId, groupId, false, parameterMap, publicLayoutsFile);
396         }
397     }
398 
399     protected void validate(long userGroupId, long companyId, String name)
400         throws PortalException, SystemException {
401 
402         if ((Validator.isNull(name)) || (Validator.isNumber(name)) ||
403             (name.indexOf(StringPool.COMMA) != -1) ||
404             (name.indexOf(StringPool.STAR) != -1)) {
405 
406             throw new UserGroupNameException();
407         }
408 
409         try {
410             UserGroup userGroup = userGroupFinder.findByC_N(companyId, name);
411 
412             if (userGroup.getUserGroupId() != userGroupId) {
413                 throw new DuplicateUserGroupException();
414             }
415         }
416         catch (NoSuchUserGroupException nsuge) {
417         }
418     }
419 
420 }