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.HashMap;
053 import java.util.LinkedHashMap;
054 import java.util.List;
055 import java.util.Map;
056
057
062 public class UserGroupLocalServiceImpl extends UserGroupLocalServiceBaseImpl {
063
064
071 public void addGroupUserGroups(long groupId, long[] userGroupIds)
072 throws SystemException {
073
074 groupPersistence.addUserGroups(groupId, userGroupIds);
075
076 PermissionCacheUtil.clearCache();
077 }
078
079
086 public void addTeamUserGroups(long teamId, long[] userGroupIds)
087 throws SystemException {
088
089 teamPersistence.addUserGroups(teamId, userGroupIds);
090
091 PermissionCacheUtil.clearCache();
092 }
093
094
114 public UserGroup addUserGroup(
115 long userId, long companyId, String name, String description)
116 throws PortalException, SystemException {
117
118 return addUserGroup(userId, companyId, name, description, null);
119 }
120
121
142 public UserGroup addUserGroup(
143 long userId, long companyId, String name, String description,
144 ServiceContext serviceContext)
145 throws PortalException, SystemException {
146
147
148
149 validate(0, companyId, name);
150
151 long userGroupId = counterLocalService.increment();
152
153 UserGroup userGroup = userGroupPersistence.create(userGroupId);
154
155 userGroup.setCompanyId(companyId);
156 userGroup.setParentUserGroupId(
157 UserGroupConstants.DEFAULT_PARENT_USER_GROUP_ID);
158 userGroup.setName(name);
159 userGroup.setDescription(description);
160 userGroup.setAddedByLDAPImport(
161 LDAPUserGroupTransactionThreadLocal.isOriginatesFromLDAP());
162 userGroup.setExpandoBridgeAttributes(serviceContext);
163
164 userGroupPersistence.update(userGroup);
165
166
167
168 groupLocalService.addGroup(
169 userId, GroupConstants.DEFAULT_PARENT_GROUP_ID,
170 UserGroup.class.getName(), userGroup.getUserGroupId(),
171 GroupConstants.DEFAULT_LIVE_GROUP_ID, String.valueOf(userGroupId),
172 null, 0, null, false, true, null);
173
174
175
176 resourceLocalService.addResources(
177 companyId, 0, userId, UserGroup.class.getName(),
178 userGroup.getUserGroupId(), false, false, false);
179
180
181
182 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
183 UserGroup.class);
184
185 indexer.reindex(userGroup);
186
187 return userGroup;
188 }
189
190
201 public void clearUserUserGroups(long userId) throws SystemException {
202 userPersistence.clearUserGroups(userId);
203
204 PermissionCacheUtil.clearCache();
205 }
206
207
218 public void copyUserGroupLayouts(long userGroupId, long userIds[])
219 throws PortalException, SystemException {
220
221 Map<String, String[]> parameterMap = getLayoutTemplatesParameters();
222
223 File[] files = exportLayouts(userGroupId, parameterMap);
224
225 try {
226 for (long userId : userIds) {
227 if (!userGroupPersistence.containsUser(userGroupId, userId)) {
228 importLayouts(userId, parameterMap, files[0], files[1]);
229 }
230 }
231 }
232 finally {
233 if (files[0] != null) {
234 files[0].delete();
235 }
236
237 if (files[1] != null) {
238 files[1].delete();
239 }
240 }
241 }
242
243
253 public void copyUserGroupLayouts(long userGroupIds[], long userId)
254 throws PortalException, SystemException {
255
256 for (long userGroupId : userGroupIds) {
257 if (!userGroupPersistence.containsUser(userGroupId, userId)) {
258 copyUserGroupLayouts(userGroupId, userId);
259 }
260 }
261 }
262
263
273 public void copyUserGroupLayouts(long userGroupId, long userId)
274 throws PortalException, SystemException {
275
276 Map<String, String[]> parameterMap = getLayoutTemplatesParameters();
277
278 File[] files = exportLayouts(userGroupId, parameterMap);
279
280 try {
281 importLayouts(userId, parameterMap, files[0], files[1]);
282 }
283 finally {
284 if (files[0] != null) {
285 files[0].delete();
286 }
287
288 if (files[1] != null) {
289 files[1].delete();
290 }
291 }
292 }
293
294
303 @Override
304 public UserGroup deleteUserGroup(long userGroupId)
305 throws PortalException, SystemException {
306
307 UserGroup userGroup = userGroupPersistence.findByPrimaryKey(
308 userGroupId);
309
310 return deleteUserGroup(userGroup);
311 }
312
313
322 @Override
323 public UserGroup deleteUserGroup(UserGroup userGroup)
324 throws PortalException, SystemException {
325
326 int count = userLocalService.getUserGroupUsersCount(
327 userGroup.getUserGroupId(), WorkflowConstants.STATUS_APPROVED);
328
329 if (count > 0) {
330 throw new RequiredUserGroupException();
331 }
332
333
334
335 expandoValueLocalService.deleteValues(
336 UserGroup.class.getName(), userGroup.getUserGroupId());
337
338
339
340 clearUserUserGroups(userGroup.getUserGroupId());
341
342
343
344 Group group = userGroup.getGroup();
345
346 groupLocalService.deleteGroup(group);
347
348
349
350 userGroupGroupRoleLocalService.deleteUserGroupGroupRolesByUserGroupId(
351 userGroup.getUserGroupId());
352
353
354
355 resourceLocalService.deleteResource(
356 userGroup.getCompanyId(), UserGroup.class.getName(),
357 ResourceConstants.SCOPE_INDIVIDUAL, userGroup.getUserGroupId());
358
359
360
361 userGroupPersistence.remove(userGroup);
362
363
364
365 PermissionCacheUtil.clearCache();
366
367 return userGroup;
368 }
369
370
379 public UserGroup getUserGroup(long companyId, String name)
380 throws PortalException, SystemException {
381
382 return userGroupPersistence.findByC_N(companyId, name);
383 }
384
385
392 public List<UserGroup> getUserGroups(long companyId)
393 throws SystemException {
394
395 return userGroupPersistence.findByCompanyId(companyId);
396 }
397
398
406 public List<UserGroup> getUserGroups(long[] userGroupIds)
407 throws PortalException, SystemException {
408
409 List<UserGroup> userGroups = new ArrayList<UserGroup>(
410 userGroupIds.length);
411
412 for (long userGroupId : userGroupIds) {
413 UserGroup userGroup = getUserGroup(userGroupId);
414
415 userGroups.add(userGroup);
416 }
417
418 return userGroups;
419 }
420
421
428 public List<UserGroup> getUserUserGroups(long userId)
429 throws SystemException {
430
431 return userPersistence.getUserGroups(userId);
432 }
433
434
443 public boolean hasGroupUserGroup(long groupId, long userGroupId)
444 throws SystemException {
445
446 return groupPersistence.containsUserGroup(groupId, userGroupId);
447 }
448
449
458 public boolean hasTeamUserGroup(long teamId, long userGroupId)
459 throws SystemException {
460
461 return teamPersistence.containsUserGroup(teamId, userGroupId);
462 }
463
464
492 public List<UserGroup> search(
493 long companyId, String keywords,
494 LinkedHashMap<String, Object> params, int start, int end,
495 OrderByComparator obc)
496 throws SystemException {
497
498 return userGroupFinder.findByKeywords(
499 companyId, keywords, params, start, end, obc);
500 }
501
502
532 public Hits search(
533 long companyId, String keywords,
534 LinkedHashMap<String, Object> params, int start, int end, Sort sort)
535 throws SystemException {
536
537 String name = null;
538 String description = null;
539 boolean andOperator = false;
540
541 if (Validator.isNotNull(keywords)) {
542 name = keywords;
543 description = keywords;
544 }
545 else {
546 andOperator = true;
547 }
548
549 if (params != null) {
550 params.put("keywords", keywords);
551 }
552
553 return search(
554 companyId, name, description, params, andOperator, start, end,
555 sort);
556 }
557
558
591 public Hits search(
592 long companyId, String name, String description,
593 LinkedHashMap<String, Object> params, boolean andSearch, int start,
594 int end, Sort sort)
595 throws SystemException {
596
597 try {
598 SearchContext searchContext = new SearchContext();
599
600 searchContext.setAndSearch(andSearch);
601
602 Map<String, Serializable> attributes =
603 new HashMap<String, Serializable>();
604
605 attributes.put("description", description);
606 attributes.put("name", name);
607
608 searchContext.setAttributes(attributes);
609
610 searchContext.setCompanyId(companyId);
611 searchContext.setEnd(end);
612
613 if (params != null) {
614 String keywords = (String)params.remove("keywords");
615
616 if (Validator.isNotNull(keywords)) {
617 searchContext.setKeywords(keywords);
618 }
619 }
620
621 QueryConfig queryConfig = new QueryConfig();
622
623 queryConfig.setHighlightEnabled(false);
624 queryConfig.setScoreEnabled(false);
625
626 searchContext.setQueryConfig(queryConfig);
627
628 if (sort != null) {
629 searchContext.setSorts(new Sort[] {sort});
630 }
631
632 searchContext.setStart(start);
633
634 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
635 UserGroup.class);
636
637 return indexer.search(searchContext);
638 }
639 catch (Exception e) {
640 throw new SystemException(e);
641 }
642 }
643
644
674 public List<UserGroup> search(
675 long companyId, String name, String description,
676 LinkedHashMap<String, Object> params, int start, int end,
677 OrderByComparator obc)
678 throws SystemException {
679
680 return userGroupFinder.findByC_N_D(
681 companyId, name, description, params, false, start, end, obc);
682 }
683
684
697 public int searchCount(
698 long companyId, String keywords,
699 LinkedHashMap<String, Object> params)
700 throws SystemException {
701
702 return userGroupFinder.countByKeywords(companyId, keywords, params);
703 }
704
705
719 public int searchCount(
720 long companyId, String name, String description,
721 LinkedHashMap<String, Object> params)
722 throws SystemException {
723
724 return userGroupFinder.countByC_N_D(
725 companyId, name, description, params, false);
726 }
727
728
738 public void setUserUserGroups(long userId, long[] userGroupIds)
739 throws PortalException, SystemException {
740
741 copyUserGroupLayouts(userGroupIds, userId);
742
743 userPersistence.setUserGroups(userId, userGroupIds);
744
745 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);
746
747 indexer.reindex(userId);
748
749 PermissionCacheUtil.clearCache();
750 }
751
752
759 public void unsetGroupUserGroups(long groupId, long[] userGroupIds)
760 throws SystemException {
761
762 List<Team> teams = teamPersistence.findByGroupId(groupId);
763
764 for (Team team : teams) {
765 teamPersistence.removeUserGroups(team.getTeamId(), userGroupIds);
766 }
767
768 userGroupGroupRoleLocalService.deleteUserGroupGroupRoles(
769 userGroupIds, groupId);
770
771 groupPersistence.removeUserGroups(groupId, userGroupIds);
772
773 PermissionCacheUtil.clearCache();
774 }
775
776
783 public void unsetTeamUserGroups(long teamId, long[] userGroupIds)
784 throws SystemException {
785
786 teamPersistence.removeUserGroups(teamId, userGroupIds);
787
788 PermissionCacheUtil.clearCache();
789 }
790
791
805 public UserGroup updateUserGroup(
806 long companyId, long userGroupId, String name, String description)
807 throws PortalException, SystemException {
808
809 return updateUserGroup(companyId, userGroupId, name, description, null);
810 }
811
812
827 public UserGroup updateUserGroup(
828 long companyId, long userGroupId, String name, String description,
829 ServiceContext serviceContext)
830 throws PortalException, SystemException {
831
832
833
834 validate(userGroupId, companyId, name);
835
836 UserGroup userGroup = userGroupPersistence.findByPrimaryKey(
837 userGroupId);
838
839 userGroup.setName(name);
840 userGroup.setDescription(description);
841 userGroup.setExpandoBridgeAttributes(serviceContext);
842
843 userGroupPersistence.update(userGroup);
844
845
846
847 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
848 UserGroup.class);
849
850 indexer.reindex(userGroup);
851
852 return userGroup;
853 }
854
855 protected File[] exportLayouts(
856 long userGroupId, Map<String, String[]> parameterMap)
857 throws PortalException, SystemException {
858
859 File[] files = new File[2];
860
861 UserGroup userGroup = userGroupPersistence.findByPrimaryKey(
862 userGroupId);
863
864 Group group = userGroup.getGroup();
865
866 if (userGroup.hasPrivateLayouts()) {
867 files[0] = layoutLocalService.exportLayoutsAsFile(
868 group.getGroupId(), true, null, parameterMap, null, null);
869 }
870
871 if (userGroup.hasPublicLayouts()) {
872 files[1] = layoutLocalService.exportLayoutsAsFile(
873 group.getGroupId(), false, null, parameterMap, null, null);
874 }
875
876 return files;
877 }
878
879 protected Map<String, String[]> getLayoutTemplatesParameters() {
880 Map<String, String[]> parameterMap =
881 new LinkedHashMap<String, String[]>();
882
883 parameterMap.put(
884 PortletDataHandlerKeys.CATEGORIES,
885 new String[] {Boolean.TRUE.toString()});
886 parameterMap.put(
887 PortletDataHandlerKeys.DATA_STRATEGY,
888 new String[] {PortletDataHandlerKeys.DATA_STRATEGY_MIRROR});
889 parameterMap.put(
890 PortletDataHandlerKeys.DELETE_MISSING_LAYOUTS,
891 new String[] {Boolean.FALSE.toString()});
892 parameterMap.put(
893 PortletDataHandlerKeys.DELETE_PORTLET_DATA,
894 new String[] {Boolean.FALSE.toString()});
895 parameterMap.put(
896 PortletDataHandlerKeys.LAYOUT_SET_SETTINGS,
897 new String[] {Boolean.FALSE.toString()});
898 parameterMap.put(
899 PortletDataHandlerKeys.LAYOUTS_IMPORT_MODE,
900 new String[] {PortletDataHandlerKeys.
901 LAYOUTS_IMPORT_MODE_CREATED_FROM_PROTOTYPE});
902 parameterMap.put(
903 PortletDataHandlerKeys.LOGO,
904 new String[] {Boolean.FALSE.toString()});
905 parameterMap.put(
906 PortletDataHandlerKeys.PERMISSIONS,
907 new String[] {Boolean.TRUE.toString()});
908 parameterMap.put(
909 PortletDataHandlerKeys.PORTLET_DATA,
910 new String[] {Boolean.TRUE.toString()});
911 parameterMap.put(
912 PortletDataHandlerKeys.PORTLET_DATA_ALL,
913 new String[] {Boolean.TRUE.toString()});
914 parameterMap.put(
915 PortletDataHandlerKeys.PORTLET_SETUP,
916 new String[] {Boolean.TRUE.toString()});
917 parameterMap.put(
918 PortletDataHandlerKeys.PORTLET_USER_PREFERENCES,
919 new String[] {Boolean.TRUE.toString()});
920 parameterMap.put(
921 PortletDataHandlerKeys.PORTLETS_MERGE_MODE,
922 new String[] {PortletDataHandlerKeys.
923 PORTLETS_MERGE_MODE_ADD_TO_BOTTOM});
924 parameterMap.put(
925 PortletDataHandlerKeys.THEME,
926 new String[] {Boolean.FALSE.toString()});
927 parameterMap.put(
928 PortletDataHandlerKeys.THEME_REFERENCE,
929 new String[] {Boolean.TRUE.toString()});
930 parameterMap.put(
931 PortletDataHandlerKeys.UPDATE_LAST_PUBLISH_DATE,
932 new String[] {Boolean.FALSE.toString()});
933 parameterMap.put(
934 PortletDataHandlerKeys.USER_ID_STRATEGY,
935 new String[] {UserIdStrategy.CURRENT_USER_ID});
936
937 return parameterMap;
938 }
939
940 protected void importLayouts(
941 long userId, Map<String, String[]> parameterMap,
942 File privateLayoutsFile, File publicLayoutsFile)
943 throws PortalException, SystemException {
944
945 User user = userPersistence.findByPrimaryKey(userId);
946
947 long groupId = user.getGroup().getGroupId();
948
949 if (privateLayoutsFile != null) {
950 layoutLocalService.importLayouts(
951 userId, groupId, true, parameterMap, privateLayoutsFile);
952 }
953
954 if (publicLayoutsFile != null) {
955 layoutLocalService.importLayouts(
956 userId, groupId, false, parameterMap, publicLayoutsFile);
957 }
958 }
959
960 protected void validate(long userGroupId, long companyId, String name)
961 throws PortalException, SystemException {
962
963 if (Validator.isNull(name) ||
964 (name.indexOf(CharPool.COMMA) != -1) ||
965 (name.indexOf(CharPool.STAR) != -1)) {
966
967 throw new UserGroupNameException();
968 }
969
970 if (Validator.isNumber(name) &&
971 !PropsValues.USER_GROUPS_NAME_ALLOW_NUMERIC) {
972
973 throw new UserGroupNameException();
974 }
975
976 try {
977 UserGroup userGroup = userGroupFinder.findByC_N(companyId, name);
978
979 if (userGroup.getUserGroupId() != userGroupId) {
980 throw new DuplicateUserGroupException();
981 }
982 }
983 catch (NoSuchUserGroupException nsuge) {
984 }
985 }
986
987 }