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.systemevent.SystemEvent;
032 import com.liferay.portal.kernel.util.CharPool;
033 import com.liferay.portal.kernel.util.OrderByComparator;
034 import com.liferay.portal.kernel.util.Validator;
035 import com.liferay.portal.kernel.workflow.WorkflowConstants;
036 import com.liferay.portal.model.Group;
037 import com.liferay.portal.model.GroupConstants;
038 import com.liferay.portal.model.ResourceConstants;
039 import com.liferay.portal.model.SystemEventConstants;
040 import com.liferay.portal.model.Team;
041 import com.liferay.portal.model.User;
042 import com.liferay.portal.model.UserGroup;
043 import com.liferay.portal.model.UserGroupConstants;
044 import com.liferay.portal.security.ldap.LDAPUserGroupTransactionThreadLocal;
045 import com.liferay.portal.security.permission.PermissionCacheUtil;
046 import com.liferay.portal.service.ServiceContext;
047 import com.liferay.portal.service.base.UserGroupLocalServiceBaseImpl;
048 import com.liferay.portal.util.PropsValues;
049
050 import java.io.File;
051 import java.io.Serializable;
052
053 import java.util.ArrayList;
054 import java.util.Date;
055 import java.util.HashMap;
056 import java.util.LinkedHashMap;
057 import java.util.List;
058 import java.util.Map;
059
060
066 public class UserGroupLocalServiceImpl extends UserGroupLocalServiceBaseImpl {
067
068
075 @Override
076 public void addGroupUserGroups(long groupId, long[] userGroupIds)
077 throws SystemException {
078
079 groupPersistence.addUserGroups(groupId, userGroupIds);
080
081 PermissionCacheUtil.clearCache();
082 }
083
084
091 @Override
092 public void addTeamUserGroups(long teamId, long[] userGroupIds)
093 throws SystemException {
094
095 teamPersistence.addUserGroups(teamId, userGroupIds);
096
097 PermissionCacheUtil.clearCache();
098 }
099
100
120 @Override
121 public UserGroup addUserGroup(
122 long userId, long companyId, String name, String description)
123 throws PortalException, SystemException {
124
125 return addUserGroup(userId, companyId, name, description, null);
126 }
127
128
149 @Override
150 public UserGroup addUserGroup(
151 long userId, long companyId, String name, String description,
152 ServiceContext serviceContext)
153 throws PortalException, SystemException {
154
155
156
157 Date now = new Date();
158
159 validate(0, companyId, name);
160
161 User user = userPersistence.findByPrimaryKey(userId);
162
163 long userGroupId = counterLocalService.increment();
164
165 UserGroup userGroup = userGroupPersistence.create(userGroupId);
166
167 if (serviceContext != null) {
168 userGroup.setUuid(serviceContext.getUuid());
169 }
170
171 userGroup.setCompanyId(companyId);
172 userGroup.setUserId(user.getUserId());
173 userGroup.setUserName(user.getFullName());
174
175 if (serviceContext != null) {
176 userGroup.setCreateDate(serviceContext.getCreateDate(now));
177 userGroup.setModifiedDate(serviceContext.getModifiedDate(now));
178 }
179 else {
180 userGroup.setCreateDate(now);
181 userGroup.setModifiedDate(now);
182 }
183
184 userGroup.setParentUserGroupId(
185 UserGroupConstants.DEFAULT_PARENT_USER_GROUP_ID);
186 userGroup.setName(name);
187 userGroup.setDescription(description);
188 userGroup.setAddedByLDAPImport(
189 LDAPUserGroupTransactionThreadLocal.isOriginatesFromLDAP());
190 userGroup.setExpandoBridgeAttributes(serviceContext);
191
192 userGroupPersistence.update(userGroup);
193
194
195
196 groupLocalService.addGroup(
197 userId, GroupConstants.DEFAULT_PARENT_GROUP_ID,
198 UserGroup.class.getName(), userGroup.getUserGroupId(),
199 GroupConstants.DEFAULT_LIVE_GROUP_ID, String.valueOf(userGroupId),
200 null, 0, true, GroupConstants.DEFAULT_MEMBERSHIP_RESTRICTION, null,
201 false, true, null);
202
203
204
205 resourceLocalService.addResources(
206 companyId, 0, userId, UserGroup.class.getName(),
207 userGroup.getUserGroupId(), false, false, false);
208
209
210
211 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
212 UserGroup.class);
213
214 indexer.reindex(userGroup);
215
216 return userGroup;
217 }
218
219
230 @Override
231 public void clearUserUserGroups(long userId) throws SystemException {
232 userPersistence.clearUserGroups(userId);
233
234 PermissionCacheUtil.clearCache(userId);
235 }
236
237
247 @Override
248 public void copyUserGroupLayouts(long userGroupId, long userId)
249 throws PortalException, SystemException {
250
251 Map<String, String[]> parameterMap = getLayoutTemplatesParameters();
252
253 File[] files = exportLayouts(userGroupId, parameterMap);
254
255 try {
256 importLayouts(userId, parameterMap, files[0], files[1]);
257 }
258 finally {
259 if (files[0] != null) {
260 files[0].delete();
261 }
262
263 if (files[1] != null) {
264 files[1].delete();
265 }
266 }
267 }
268
269
280 @Override
281 public void copyUserGroupLayouts(long userGroupId, long[] userIds)
282 throws PortalException, SystemException {
283
284 Map<String, String[]> parameterMap = getLayoutTemplatesParameters();
285
286 File[] files = exportLayouts(userGroupId, parameterMap);
287
288 try {
289 for (long userId : userIds) {
290 if (!userGroupPersistence.containsUser(userGroupId, userId)) {
291 importLayouts(userId, parameterMap, files[0], files[1]);
292 }
293 }
294 }
295 finally {
296 if (files[0] != null) {
297 files[0].delete();
298 }
299
300 if (files[1] != null) {
301 files[1].delete();
302 }
303 }
304 }
305
306
316 @Override
317 public void copyUserGroupLayouts(long[] userGroupIds, long userId)
318 throws PortalException, SystemException {
319
320 for (long userGroupId : userGroupIds) {
321 if (!userGroupPersistence.containsUser(userGroupId, userId)) {
322 copyUserGroupLayouts(userGroupId, userId);
323 }
324 }
325 }
326
327
336 @Override
337 public UserGroup deleteUserGroup(long userGroupId)
338 throws PortalException, SystemException {
339
340 UserGroup userGroup = userGroupPersistence.findByPrimaryKey(
341 userGroupId);
342
343 return userGroupLocalService.deleteUserGroup(userGroup);
344 }
345
346
355 @Override
356 @SystemEvent(
357 action = SystemEventConstants.ACTION_SKIP,
358 type = SystemEventConstants.TYPE_DELETE)
359 public UserGroup deleteUserGroup(UserGroup userGroup)
360 throws PortalException, SystemException {
361
362 int count = userLocalService.getUserGroupUsersCount(
363 userGroup.getUserGroupId(), WorkflowConstants.STATUS_APPROVED);
364
365 if (count > 0) {
366 throw new RequiredUserGroupException();
367 }
368
369
370
371 expandoRowLocalService.deleteRows(userGroup.getUserGroupId());
372
373
374
375 clearUserUserGroups(userGroup.getUserId());
376
377
378
379 Group group = userGroup.getGroup();
380
381 groupLocalService.deleteGroup(group);
382
383
384
385 userGroupGroupRoleLocalService.deleteUserGroupGroupRolesByUserGroupId(
386 userGroup.getUserGroupId());
387
388
389
390 resourceLocalService.deleteResource(
391 userGroup.getCompanyId(), UserGroup.class.getName(),
392 ResourceConstants.SCOPE_INDIVIDUAL, userGroup.getUserGroupId());
393
394
395
396 userGroupPersistence.remove(userGroup);
397
398
399
400 PermissionCacheUtil.clearCache();
401
402 return userGroup;
403 }
404
405 @Override
406 public void deleteUserGroups(long companyId)
407 throws PortalException, SystemException {
408
409 List<UserGroup> userGroups = userGroupPersistence.findByCompanyId(
410 companyId);
411
412 for (UserGroup userGroup : userGroups) {
413 userGroupLocalService.deleteUserGroup(userGroup);
414 }
415 }
416
417 @Override
418 public UserGroup fetchUserGroup(long companyId, String name)
419 throws SystemException {
420
421 return userGroupPersistence.fetchByC_N(companyId, name);
422 }
423
424
433 @Override
434 public UserGroup getUserGroup(long companyId, String name)
435 throws PortalException, SystemException {
436
437 return userGroupPersistence.findByC_N(companyId, name);
438 }
439
440
447 @Override
448 public List<UserGroup> getUserGroups(long companyId)
449 throws SystemException {
450
451 return userGroupPersistence.findByCompanyId(companyId);
452 }
453
454
462 @Override
463 public List<UserGroup> getUserGroups(long[] userGroupIds)
464 throws PortalException, SystemException {
465
466 List<UserGroup> userGroups = new ArrayList<UserGroup>(
467 userGroupIds.length);
468
469 for (long userGroupId : userGroupIds) {
470 UserGroup userGroup = getUserGroup(userGroupId);
471
472 userGroups.add(userGroup);
473 }
474
475 return userGroups;
476 }
477
478
506 @Override
507 public List<UserGroup> search(
508 long companyId, String keywords,
509 LinkedHashMap<String, Object> params, int start, int end,
510 OrderByComparator obc)
511 throws SystemException {
512
513 return userGroupFinder.findByKeywords(
514 companyId, keywords, params, start, end, obc);
515 }
516
517
547 @Override
548 public Hits search(
549 long companyId, String keywords,
550 LinkedHashMap<String, Object> params, int start, int end, Sort sort)
551 throws SystemException {
552
553 String name = null;
554 String description = null;
555 boolean andOperator = false;
556
557 if (Validator.isNotNull(keywords)) {
558 name = keywords;
559 description = keywords;
560 }
561 else {
562 andOperator = true;
563 }
564
565 if (params != null) {
566 params.put("keywords", keywords);
567 }
568
569 return search(
570 companyId, name, description, params, andOperator, start, end,
571 sort);
572 }
573
574
607 @Override
608 public Hits search(
609 long companyId, String name, String description,
610 LinkedHashMap<String, Object> params, boolean andSearch, int start,
611 int end, Sort sort)
612 throws SystemException {
613
614 try {
615 SearchContext searchContext = new SearchContext();
616
617 searchContext.setAndSearch(andSearch);
618
619 Map<String, Serializable> attributes =
620 new HashMap<String, Serializable>();
621
622 attributes.put("description", description);
623 attributes.put("name", name);
624
625 searchContext.setAttributes(attributes);
626
627 searchContext.setCompanyId(companyId);
628 searchContext.setEnd(end);
629
630 if (params != null) {
631 String keywords = (String)params.remove("keywords");
632
633 if (Validator.isNotNull(keywords)) {
634 searchContext.setKeywords(keywords);
635 }
636 }
637
638 QueryConfig queryConfig = new QueryConfig();
639
640 queryConfig.setHighlightEnabled(false);
641 queryConfig.setScoreEnabled(false);
642
643 searchContext.setQueryConfig(queryConfig);
644
645 if (sort != null) {
646 searchContext.setSorts(sort);
647 }
648
649 searchContext.setStart(start);
650
651 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
652 UserGroup.class);
653
654 return indexer.search(searchContext);
655 }
656 catch (Exception e) {
657 throw new SystemException(e);
658 }
659 }
660
661
674 @Override
675 public int searchCount(
676 long companyId, String keywords,
677 LinkedHashMap<String, Object> params)
678 throws SystemException {
679
680 return userGroupFinder.countByKeywords(companyId, keywords, params);
681 }
682
683
693 @Override
694 public void setUserUserGroups(long userId, long[] userGroupIds)
695 throws PortalException, SystemException {
696
697 if (PropsValues.USER_GROUPS_COPY_LAYOUTS_TO_USER_PERSONAL_SITE) {
698 copyUserGroupLayouts(userGroupIds, userId);
699 }
700
701 userPersistence.setUserGroups(userId, userGroupIds);
702
703 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);
704
705 indexer.reindex(userId);
706
707 PermissionCacheUtil.clearCache(userId);
708 }
709
710
717 @Override
718 public void unsetGroupUserGroups(long groupId, long[] userGroupIds)
719 throws SystemException {
720
721 List<Team> teams = teamPersistence.findByGroupId(groupId);
722
723 for (Team team : teams) {
724 teamPersistence.removeUserGroups(team.getTeamId(), userGroupIds);
725 }
726
727 userGroupGroupRoleLocalService.deleteUserGroupGroupRoles(
728 userGroupIds, groupId);
729
730 groupPersistence.removeUserGroups(groupId, userGroupIds);
731
732 PermissionCacheUtil.clearCache();
733 }
734
735
742 @Override
743 public void unsetTeamUserGroups(long teamId, long[] userGroupIds)
744 throws SystemException {
745
746 teamPersistence.removeUserGroups(teamId, userGroupIds);
747
748 PermissionCacheUtil.clearCache();
749 }
750
751
765 @Override
766 public UserGroup updateUserGroup(
767 long companyId, long userGroupId, String name, String description)
768 throws PortalException, SystemException {
769
770 return updateUserGroup(companyId, userGroupId, name, description, null);
771 }
772
773
788 @Override
789 public UserGroup updateUserGroup(
790 long companyId, long userGroupId, String name, String description,
791 ServiceContext serviceContext)
792 throws PortalException, SystemException {
793
794
795
796 validate(userGroupId, companyId, name);
797
798 UserGroup userGroup = userGroupPersistence.findByPrimaryKey(
799 userGroupId);
800
801 userGroup.setModifiedDate(new Date());
802 userGroup.setName(name);
803 userGroup.setDescription(description);
804 userGroup.setExpandoBridgeAttributes(serviceContext);
805
806 userGroupPersistence.update(userGroup);
807
808
809
810 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
811 UserGroup.class);
812
813 indexer.reindex(userGroup);
814
815 return userGroup;
816 }
817
818 protected File[] exportLayouts(
819 long userGroupId, Map<String, String[]> parameterMap)
820 throws PortalException, SystemException {
821
822 File[] files = new File[2];
823
824 UserGroup userGroup = userGroupPersistence.findByPrimaryKey(
825 userGroupId);
826
827 Group group = userGroup.getGroup();
828
829 if (userGroup.hasPrivateLayouts()) {
830 files[0] = layoutLocalService.exportLayoutsAsFile(
831 group.getGroupId(), true, null, parameterMap, null, null);
832 }
833
834 if (userGroup.hasPublicLayouts()) {
835 files[1] = layoutLocalService.exportLayoutsAsFile(
836 group.getGroupId(), false, null, parameterMap, null, null);
837 }
838
839 return files;
840 }
841
842 protected Map<String, String[]> getLayoutTemplatesParameters() {
843 Map<String, String[]> parameterMap =
844 new LinkedHashMap<String, String[]>();
845
846 parameterMap.put(
847 PortletDataHandlerKeys.CATEGORIES,
848 new String[] {Boolean.TRUE.toString()});
849 parameterMap.put(
850 PortletDataHandlerKeys.DATA_STRATEGY,
851 new String[] {PortletDataHandlerKeys.DATA_STRATEGY_MIRROR});
852 parameterMap.put(
853 PortletDataHandlerKeys.DELETE_MISSING_LAYOUTS,
854 new String[] {Boolean.FALSE.toString()});
855 parameterMap.put(
856 PortletDataHandlerKeys.DELETE_PORTLET_DATA,
857 new String[] {Boolean.FALSE.toString()});
858 parameterMap.put(
859 PortletDataHandlerKeys.LAYOUT_SET_SETTINGS,
860 new String[] {Boolean.FALSE.toString()});
861 parameterMap.put(
862 PortletDataHandlerKeys.LAYOUTS_IMPORT_MODE,
863 new String[] {PortletDataHandlerKeys.
864 LAYOUTS_IMPORT_MODE_CREATED_FROM_PROTOTYPE});
865 parameterMap.put(
866 PortletDataHandlerKeys.LOGO,
867 new String[] {Boolean.FALSE.toString()});
868 parameterMap.put(
869 PortletDataHandlerKeys.PERMISSIONS,
870 new String[] {Boolean.TRUE.toString()});
871 parameterMap.put(
872 PortletDataHandlerKeys.PORTLET_CONFIGURATION,
873 new String[] {Boolean.TRUE.toString()});
874 parameterMap.put(
875 PortletDataHandlerKeys.PORTLET_CONFIGURATION_ALL,
876 new String[] {Boolean.TRUE.toString()});
877 parameterMap.put(
878 PortletDataHandlerKeys.PORTLET_DATA,
879 new String[] {Boolean.TRUE.toString()});
880 parameterMap.put(
881 PortletDataHandlerKeys.PORTLET_DATA_ALL,
882 new String[] {Boolean.TRUE.toString()});
883 parameterMap.put(
884 PortletDataHandlerKeys.PORTLET_SETUP_ALL,
885 new String[] {Boolean.TRUE.toString()});
886 parameterMap.put(
887 PortletDataHandlerKeys.PORTLETS_MERGE_MODE,
888 new String[] {PortletDataHandlerKeys.
889 PORTLETS_MERGE_MODE_ADD_TO_BOTTOM});
890 parameterMap.put(
891 PortletDataHandlerKeys.THEME_REFERENCE,
892 new String[] {Boolean.TRUE.toString()});
893 parameterMap.put(
894 PortletDataHandlerKeys.UPDATE_LAST_PUBLISH_DATE,
895 new String[] {Boolean.FALSE.toString()});
896 parameterMap.put(
897 PortletDataHandlerKeys.USER_ID_STRATEGY,
898 new String[] {UserIdStrategy.CURRENT_USER_ID});
899
900 return parameterMap;
901 }
902
903 protected void importLayouts(
904 long userId, Map<String, String[]> parameterMap,
905 File privateLayoutsFile, File publicLayoutsFile)
906 throws PortalException, SystemException {
907
908 User user = userPersistence.findByPrimaryKey(userId);
909
910 long groupId = user.getGroupId();
911
912 if (privateLayoutsFile != null) {
913 layoutLocalService.importLayouts(
914 userId, groupId, true, parameterMap, privateLayoutsFile);
915 }
916
917 if (publicLayoutsFile != null) {
918 layoutLocalService.importLayouts(
919 userId, groupId, false, parameterMap, publicLayoutsFile);
920 }
921 }
922
923 protected void validate(long userGroupId, long companyId, String name)
924 throws PortalException, SystemException {
925
926 if (Validator.isNull(name) ||
927 (name.indexOf(CharPool.COMMA) != -1) ||
928 (name.indexOf(CharPool.STAR) != -1)) {
929
930 throw new UserGroupNameException();
931 }
932
933 if (Validator.isNumber(name) &&
934 !PropsValues.USER_GROUPS_NAME_ALLOW_NUMERIC) {
935
936 throw new UserGroupNameException();
937 }
938
939 try {
940 UserGroup userGroup = userGroupFinder.findByC_N(companyId, name);
941
942 if (userGroup.getUserGroupId() != userGroupId) {
943 throw new DuplicateUserGroupException(
944 "{userGroupId=" + userGroupId + "}");
945 }
946 }
947 catch (NoSuchUserGroupException nsuge) {
948 }
949 }
950
951 }