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