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