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();
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.getUserGroupId());
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 copyUserGroupLayouts(userGroupIds, userId);
698
699 userPersistence.setUserGroups(userId, userGroupIds);
700
701 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);
702
703 indexer.reindex(userId);
704
705 PermissionCacheUtil.clearCache();
706 }
707
708
715 @Override
716 public void unsetGroupUserGroups(long groupId, long[] userGroupIds)
717 throws SystemException {
718
719 List<Team> teams = teamPersistence.findByGroupId(groupId);
720
721 for (Team team : teams) {
722 teamPersistence.removeUserGroups(team.getTeamId(), userGroupIds);
723 }
724
725 userGroupGroupRoleLocalService.deleteUserGroupGroupRoles(
726 userGroupIds, groupId);
727
728 groupPersistence.removeUserGroups(groupId, userGroupIds);
729
730 PermissionCacheUtil.clearCache();
731 }
732
733
740 @Override
741 public void unsetTeamUserGroups(long teamId, long[] userGroupIds)
742 throws SystemException {
743
744 teamPersistence.removeUserGroups(teamId, userGroupIds);
745
746 PermissionCacheUtil.clearCache();
747 }
748
749
763 @Override
764 public UserGroup updateUserGroup(
765 long companyId, long userGroupId, String name, String description)
766 throws PortalException, SystemException {
767
768 return updateUserGroup(companyId, userGroupId, name, description, null);
769 }
770
771
786 @Override
787 public UserGroup updateUserGroup(
788 long companyId, long userGroupId, String name, String description,
789 ServiceContext serviceContext)
790 throws PortalException, SystemException {
791
792
793
794 validate(userGroupId, companyId, name);
795
796 UserGroup userGroup = userGroupPersistence.findByPrimaryKey(
797 userGroupId);
798
799 userGroup.setModifiedDate(new Date());
800 userGroup.setName(name);
801 userGroup.setDescription(description);
802 userGroup.setExpandoBridgeAttributes(serviceContext);
803
804 userGroupPersistence.update(userGroup);
805
806
807
808 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
809 UserGroup.class);
810
811 indexer.reindex(userGroup);
812
813 return userGroup;
814 }
815
816 protected File[] exportLayouts(
817 long userGroupId, Map<String, String[]> parameterMap)
818 throws PortalException, SystemException {
819
820 File[] files = new File[2];
821
822 UserGroup userGroup = userGroupPersistence.findByPrimaryKey(
823 userGroupId);
824
825 Group group = userGroup.getGroup();
826
827 if (userGroup.hasPrivateLayouts()) {
828 files[0] = layoutLocalService.exportLayoutsAsFile(
829 group.getGroupId(), true, null, parameterMap, null, null);
830 }
831
832 if (userGroup.hasPublicLayouts()) {
833 files[1] = layoutLocalService.exportLayoutsAsFile(
834 group.getGroupId(), false, null, parameterMap, null, null);
835 }
836
837 return files;
838 }
839
840 protected Map<String, String[]> getLayoutTemplatesParameters() {
841 Map<String, String[]> parameterMap =
842 new LinkedHashMap<String, String[]>();
843
844 parameterMap.put(
845 PortletDataHandlerKeys.CATEGORIES,
846 new String[] {Boolean.TRUE.toString()});
847 parameterMap.put(
848 PortletDataHandlerKeys.DATA_STRATEGY,
849 new String[] {PortletDataHandlerKeys.DATA_STRATEGY_MIRROR});
850 parameterMap.put(
851 PortletDataHandlerKeys.DELETE_MISSING_LAYOUTS,
852 new String[] {Boolean.FALSE.toString()});
853 parameterMap.put(
854 PortletDataHandlerKeys.DELETE_PORTLET_DATA,
855 new String[] {Boolean.FALSE.toString()});
856 parameterMap.put(
857 PortletDataHandlerKeys.LAYOUT_SET_SETTINGS,
858 new String[] {Boolean.FALSE.toString()});
859 parameterMap.put(
860 PortletDataHandlerKeys.LAYOUTS_IMPORT_MODE,
861 new String[] {PortletDataHandlerKeys.
862 LAYOUTS_IMPORT_MODE_CREATED_FROM_PROTOTYPE});
863 parameterMap.put(
864 PortletDataHandlerKeys.LOGO,
865 new String[] {Boolean.FALSE.toString()});
866 parameterMap.put(
867 PortletDataHandlerKeys.PERMISSIONS,
868 new String[] {Boolean.TRUE.toString()});
869 parameterMap.put(
870 PortletDataHandlerKeys.PORTLET_CONFIGURATION,
871 new String[] {Boolean.TRUE.toString()});
872 parameterMap.put(
873 PortletDataHandlerKeys.PORTLET_CONFIGURATION_ALL,
874 new String[] {Boolean.TRUE.toString()});
875 parameterMap.put(
876 PortletDataHandlerKeys.PORTLET_DATA,
877 new String[] {Boolean.TRUE.toString()});
878 parameterMap.put(
879 PortletDataHandlerKeys.PORTLET_DATA_ALL,
880 new String[] {Boolean.TRUE.toString()});
881 parameterMap.put(
882 PortletDataHandlerKeys.PORTLET_SETUP_ALL,
883 new String[] {Boolean.TRUE.toString()});
884 parameterMap.put(
885 PortletDataHandlerKeys.PORTLETS_MERGE_MODE,
886 new String[] {PortletDataHandlerKeys.
887 PORTLETS_MERGE_MODE_ADD_TO_BOTTOM});
888 parameterMap.put(
889 PortletDataHandlerKeys.THEME_REFERENCE,
890 new String[] {Boolean.TRUE.toString()});
891 parameterMap.put(
892 PortletDataHandlerKeys.UPDATE_LAST_PUBLISH_DATE,
893 new String[] {Boolean.FALSE.toString()});
894 parameterMap.put(
895 PortletDataHandlerKeys.USER_ID_STRATEGY,
896 new String[] {UserIdStrategy.CURRENT_USER_ID});
897
898 return parameterMap;
899 }
900
901 protected void importLayouts(
902 long userId, Map<String, String[]> parameterMap,
903 File privateLayoutsFile, File publicLayoutsFile)
904 throws PortalException, SystemException {
905
906 User user = userPersistence.findByPrimaryKey(userId);
907
908 long groupId = user.getGroupId();
909
910 if (privateLayoutsFile != null) {
911 layoutLocalService.importLayouts(
912 userId, groupId, true, parameterMap, privateLayoutsFile);
913 }
914
915 if (publicLayoutsFile != null) {
916 layoutLocalService.importLayouts(
917 userId, groupId, false, parameterMap, publicLayoutsFile);
918 }
919 }
920
921 protected void validate(long userGroupId, long companyId, String name)
922 throws PortalException, SystemException {
923
924 if (Validator.isNull(name) ||
925 (name.indexOf(CharPool.COMMA) != -1) ||
926 (name.indexOf(CharPool.STAR) != -1)) {
927
928 throw new UserGroupNameException();
929 }
930
931 if (Validator.isNumber(name) &&
932 !PropsValues.USER_GROUPS_NAME_ALLOW_NUMERIC) {
933
934 throw new UserGroupNameException();
935 }
936
937 try {
938 UserGroup userGroup = userGroupFinder.findByC_N(companyId, name);
939
940 if (userGroup.getUserGroupId() != userGroupId) {
941 throw new DuplicateUserGroupException();
942 }
943 }
944 catch (NoSuchUserGroupException nsuge) {
945 }
946 }
947
948 }