001
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
064 public class UserGroupLocalServiceImpl extends UserGroupLocalServiceBaseImpl {
065
066
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
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
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
146 public UserGroup addUserGroup(
147 long userId, long companyId, String name, String description,
148 ServiceContext serviceContext)
149 throws PortalException, SystemException {
150
151
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
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
199
200 resourceLocalService.addResources(
201 companyId, 0, userId, UserGroup.class.getName(),
202 userGroup.getUserGroupId(), false, false, false);
203
204
205
206 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
207 UserGroup.class);
208
209 indexer.reindex(userGroup);
210
211 return userGroup;
212 }
213
214
225 @Override
226 public void clearUserUserGroups(long userId) throws SystemException {
227 userPersistence.clearUserGroups(userId);
228
229 PermissionCacheUtil.clearCache();
230 }
231
232
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
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
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
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
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
359
360 expandoValueLocalService.deleteValues(
361 UserGroup.class.getName(), userGroup.getUserGroupId());
362
363
364
365 clearUserUserGroups(userGroup.getUserGroupId());
366
367
368
369 Group group = userGroup.getGroup();
370
371 groupLocalService.deleteGroup(group);
372
373
374
375 userGroupGroupRoleLocalService.deleteUserGroupGroupRolesByUserGroupId(
376 userGroup.getUserGroupId());
377
378
379
380 resourceLocalService.deleteResource(
381 userGroup.getCompanyId(), UserGroup.class.getName(),
382 ResourceConstants.SCOPE_INDIVIDUAL, userGroup.getUserGroupId());
383
384
385
386 userGroupPersistence.remove(userGroup);
387
388
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
417 public UserGroup getUserGroup(long companyId, String name)
418 throws PortalException, SystemException {
419
420 return userGroupPersistence.findByC_N(companyId, name);
421 }
422
423
430 public List<UserGroup> getUserGroups(long companyId)
431 throws SystemException {
432
433 return userGroupPersistence.findByCompanyId(companyId);
434 }
435
436
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
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
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
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
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
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
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
716 public void unsetTeamUserGroups(long teamId, long[] userGroupIds)
717 throws SystemException {
718
719 teamPersistence.removeUserGroups(teamId, userGroupIds);
720
721 PermissionCacheUtil.clearCache();
722 }
723
724
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
760 public UserGroup updateUserGroup(
761 long companyId, long userGroupId, String name, String description,
762 ServiceContext serviceContext)
763 throws PortalException, SystemException {
764
765
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
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 }