1
22
23 package com.liferay.portal.service.impl;
24
25 import com.liferay.portal.DuplicateGroupException;
26 import com.liferay.portal.GroupFriendlyURLException;
27 import com.liferay.portal.GroupNameException;
28 import com.liferay.portal.NoSuchGroupException;
29 import com.liferay.portal.NoSuchLayoutSetException;
30 import com.liferay.portal.NoSuchRoleException;
31 import com.liferay.portal.PortalException;
32 import com.liferay.portal.RequiredGroupException;
33 import com.liferay.portal.SystemException;
34 import com.liferay.portal.kernel.util.GetterUtil;
35 import com.liferay.portal.kernel.util.OrderByComparator;
36 import com.liferay.portal.kernel.util.StringPool;
37 import com.liferay.portal.kernel.util.StringUtil;
38 import com.liferay.portal.kernel.util.Validator;
39 import com.liferay.portal.model.Group;
40 import com.liferay.portal.model.Layout;
41 import com.liferay.portal.model.LayoutSet;
42 import com.liferay.portal.model.LayoutTypePortlet;
43 import com.liferay.portal.model.Organization;
44 import com.liferay.portal.model.Resource;
45 import com.liferay.portal.model.ResourceConstants;
46 import com.liferay.portal.model.Role;
47 import com.liferay.portal.model.User;
48 import com.liferay.portal.model.UserGroup;
49 import com.liferay.portal.model.impl.GroupImpl;
50 import com.liferay.portal.model.impl.LayoutImpl;
51 import com.liferay.portal.model.impl.RoleImpl;
52 import com.liferay.portal.security.permission.PermissionCacheUtil;
53 import com.liferay.portal.service.base.GroupLocalServiceBaseImpl;
54 import com.liferay.portal.util.PortalUtil;
55 import com.liferay.portal.util.PropsUtil;
56 import com.liferay.portal.util.PropsValues;
57 import com.liferay.portal.util.comparator.GroupNameComparator;
58 import com.liferay.util.Normalizer;
59
60 import java.util.ArrayList;
61 import java.util.Iterator;
62 import java.util.LinkedHashMap;
63 import java.util.List;
64 import java.util.Properties;
65
66
73 public class GroupLocalServiceImpl extends GroupLocalServiceBaseImpl {
74
75 public Group addGroup(
76 long userId, String className, long classPK, String name,
77 String description, int type, String friendlyURL, boolean active)
78 throws PortalException, SystemException {
79
80 return addGroup(
81 userId, className, classPK, GroupImpl.DEFAULT_LIVE_GROUP_ID, name,
82 description, type, friendlyURL, active);
83 }
84
85 public Group addGroup(
86 long userId, String className, long classPK, long liveGroupId,
87 String name, String description, int type, String friendlyURL,
88 boolean active)
89 throws PortalException, SystemException {
90
91
93 User user = userPersistence.findByPrimaryKey(userId);
94 className = GetterUtil.getString(className);
95 long classNameId = PortalUtil.getClassNameId(className);
96
97 long groupId = counterLocalService.increment();
98
99 friendlyURL = getFriendlyURL(groupId, classPK, friendlyURL);
100
101 validateFriendlyURL(
102 groupId, user.getCompanyId(), classNameId, classPK, friendlyURL);
103
104 Group group = groupPersistence.create(groupId);
105
106 group.setCompanyId(user.getCompanyId());
107 group.setCreatorUserId(userId);
108 group.setClassNameId(classNameId);
109 group.setClassPK(classPK);
110 group.setParentGroupId(GroupImpl.DEFAULT_PARENT_GROUP_ID);
111 group.setLiveGroupId(liveGroupId);
112 group.setName(name);
113 group.setDescription(description);
114 group.setType(type);
115 group.setFriendlyURL(friendlyURL);
116 group.setActive(active);
117
118 groupPersistence.update(group, false);
119
120
122 layoutSetLocalService.addLayoutSet(groupId, true);
123
124 layoutSetLocalService.addLayoutSet(groupId, false);
125
126 if ((classNameId <= 0) && (classPK <= 0) && !user.isDefaultUser()) {
127
128
130 resourceLocalService.addResources(
131 group.getCompanyId(), 0, 0, Group.class.getName(),
132 group.getGroupId(), false, false, false);
133
134
136 Role role = roleLocalService.getRole(
137 group.getCompanyId(), RoleImpl.COMMUNITY_OWNER);
138
139 userGroupRoleLocalService.addUserGroupRoles(
140 userId, groupId, new long[] {role.getRoleId()});
141
142
144 userLocalService.addGroupUsers(
145 group.getGroupId(), new long[] {userId});
146 }
147 else if (className.equals(Organization.class.getName()) &&
148 !user.isDefaultUser()) {
149
150
152 resourceLocalService.addResources(
153 group.getCompanyId(), 0, 0, Group.class.getName(),
154 group.getGroupId(), false, false, false);
155 }
156
157 return group;
158 }
159
160 public void addRoleGroups(long roleId, long[] groupIds)
161 throws PortalException, SystemException {
162
163 rolePersistence.addGroups(roleId, groupIds);
164
165 PermissionCacheUtil.clearCache();
166 }
167
168 public void addUserGroups(long userId, long[] groupIds)
169 throws PortalException, SystemException {
170
171 userPersistence.addGroups(userId, groupIds);
172
173 User user = userPersistence.findByPrimaryKey(userId);
174
175 Role role = rolePersistence.findByC_N(
176 user.getCompanyId(), RoleImpl.COMMUNITY_MEMBER);
177
178 for (int i = 0; i < groupIds.length; i++) {
179 long groupId = groupIds[i];
180
181 userGroupRoleLocalService.addUserGroupRoles(
182 userId, groupId, new long[] {role.getRoleId()});
183 }
184
185 PermissionCacheUtil.clearCache();
186 }
187
188 public void checkSystemGroups(long companyId)
189 throws PortalException, SystemException {
190
191 long defaultUserId = userLocalService.getDefaultUserId(companyId);
192
193 String[] systemGroups = PortalUtil.getSystemGroups();
194
195 for (int i = 0; i < systemGroups.length; i++) {
196 Group group = null;
197
198 try {
199 group = groupFinder.findByC_N(companyId, systemGroups[i]);
200 }
201 catch (NoSuchGroupException nsge) {
202 String friendlyURL = null;
203
204 if (systemGroups[i].equals(GroupImpl.GUEST)) {
205 friendlyURL = "/guest";
206 }
207
208 group = addGroup(
209 defaultUserId, null, 0, systemGroups[i], null,
210 GroupImpl.TYPE_COMMUNITY_OPEN, friendlyURL, true);
211 }
212
213 if (group.getName().equals(GroupImpl.GUEST)) {
214 LayoutSet layoutSet = layoutSetLocalService.getLayoutSet(
215 group.getGroupId(), false);
216
217 if (layoutSet.getPageCount() == 0) {
218 addDefaultLayouts(group);
219 }
220 }
221 }
222 }
223
224 public void deleteGroup(long groupId)
225 throws PortalException, SystemException {
226
227 Group group = groupPersistence.findByPrimaryKey(groupId);
228
229 if (PortalUtil.isSystemGroup(group.getName())) {
230 throw new RequiredGroupException();
231 }
232
233
235 try {
236 layoutSetLocalService.deleteLayoutSet(groupId, true);
237 }
238 catch (NoSuchLayoutSetException nslse) {
239 }
240
241 try {
242 layoutSetLocalService.deleteLayoutSet(groupId, false);
243 }
244 catch (NoSuchLayoutSetException nslse) {
245 }
246
247
249 try {
250 Role role = roleLocalService.getGroupRole(
251 group.getCompanyId(), groupId);
252
253 roleLocalService.deleteRole(role.getRoleId());
254 }
255 catch (NoSuchRoleException nsre) {
256 }
257
258
260 userGroupRoleLocalService.deleteUserGroupRolesByGroupId(groupId);
261
262
264 membershipRequestLocalService.deleteMembershipRequests(
265 group.getGroupId());
266
267
269 blogsEntryLocalService.deleteEntries(groupId);
270 blogsStatsUserLocalService.deleteStatsUserByGroupId(groupId);
271
272
274 bookmarksFolderLocalService.deleteFolders(groupId);
275
276
278 calEventLocalService.deleteEvents(groupId);
279
280
282 dlFolderLocalService.deleteFolders(groupId);
283
284
286 igFolderLocalService.deleteFolders(groupId);
287
288
290 journalArticleLocalService.deleteArticles(groupId);
291 journalTemplateLocalService.deleteTemplates(groupId);
292 journalStructureLocalService.deleteStructures(groupId);
293
294
296 mbBanLocalService.deleteBansByGroupId(groupId);
297 mbCategoryLocalService.deleteCategories(groupId);
298 mbStatsUserLocalService.deleteStatsUserByGroupId(groupId);
299
300
302 pollsQuestionLocalService.deleteQuestions(groupId);
303
304
306 shoppingCartLocalService.deleteGroupCarts(groupId);
307 shoppingCategoryLocalService.deleteCategories(groupId);
308 shoppingCouponLocalService.deleteCoupons(groupId);
309 shoppingOrderLocalService.deleteOrders(groupId);
310
311
313 scFrameworkVersionLocalService.deleteFrameworkVersions(groupId);
314 scProductEntryLocalService.deleteProductEntries(groupId);
315
316
318 wikiNodeLocalService.deleteNodes(groupId);
319
320
322 Iterator<Resource> itr = resourceFinder.findByC_P(
323 group.getCompanyId(), String.valueOf(groupId)).iterator();
324
325 while (itr.hasNext()) {
326 Resource resource = itr.next();
327
328 resourceLocalService.deleteResource(resource);
329 }
330
331 if (!group.isStagingGroup() &&
332 (group.isCommunity() || group.isOrganization())) {
333
334 resourceLocalService.deleteResource(
335 group.getCompanyId(), Group.class.getName(),
336 ResourceConstants.SCOPE_INDIVIDUAL, group.getGroupId());
337 }
338
339
341 groupPersistence.remove(groupId);
342
343
345 PermissionCacheUtil.clearCache();
346 }
347
348 public Group getFriendlyURLGroup(long companyId, String friendlyURL)
349 throws PortalException, SystemException {
350
351 if (Validator.isNull(friendlyURL)) {
352 throw new NoSuchGroupException();
353 }
354
355 friendlyURL = getFriendlyURL(friendlyURL);
356
357 return groupPersistence.findByC_F(companyId, friendlyURL);
358 }
359
360 public Group getGroup(long groupId)
361 throws PortalException, SystemException {
362
363 return groupPersistence.findByPrimaryKey(groupId);
364 }
365
366 public Group getGroup(long companyId, String name)
367 throws PortalException, SystemException {
368
369 return groupFinder.findByC_N(companyId, name);
370 }
371
372 public List<Group> getNullFriendlyURLGroups() throws SystemException {
373 return groupFinder.findByNullFriendlyURL();
374 }
375
376 public Group getOrganizationGroup(long companyId, long organizationId)
377 throws PortalException, SystemException {
378
379 long classNameId = PortalUtil.getClassNameId(Organization.class);
380
381 return groupPersistence.findByC_C_C(
382 companyId, classNameId, organizationId);
383 }
384
385 public List<Group> getOrganizationsGroups(List<Organization> organizations)
386 throws PortalException, SystemException {
387
388 List<Group> organizationGroups = new ArrayList<Group>();
389
390 for (int i = 0; i < organizations.size(); i++) {
391 Organization organization = organizations.get(i);
392
393 Group group = organization.getGroup();
394
395 organizationGroups.add(group);
396 }
397
398 return organizationGroups;
399 }
400
401 public List<Group> getRoleGroups(long roleId)
402 throws PortalException, SystemException {
403
404 return rolePersistence.getGroups(roleId);
405 }
406
407 public Group getStagingGroup(long liveGroupId)
408 throws PortalException, SystemException {
409
410 return groupPersistence.findByLiveGroupId(liveGroupId);
411 }
412
413 public Group getUserGroup(long companyId, long userId)
414 throws PortalException, SystemException {
415
416 long classNameId = PortalUtil.getClassNameId(User.class);
417
418 return groupPersistence.findByC_C_C(companyId, classNameId, userId);
419 }
420
421 public Group getUserGroupGroup(long companyId, long userGroupId)
422 throws PortalException, SystemException {
423
424 long classNameId = PortalUtil.getClassNameId(UserGroup.class);
425
426 return groupPersistence.findByC_C_C(
427 companyId, classNameId, userGroupId);
428 }
429
430 public List<Group> getUserGroups(long userId)
431 throws PortalException, SystemException {
432
433 return userPersistence.getGroups(userId);
434 }
435
436 public List<Group> getUserGroupsGroups(List<UserGroup> userGroups)
437 throws PortalException, SystemException {
438
439 List<Group> userGroupGroups = new ArrayList<Group>();
440
441 for (int i = 0; i < userGroups.size(); i++) {
442 UserGroup userGroup = userGroups.get(i);
443
444 Group group = userGroup.getGroup();
445
446 userGroupGroups.add(group);
447 }
448
449 return userGroupGroups;
450 }
451
452 public boolean hasRoleGroup(long roleId, long groupId)
453 throws PortalException, SystemException {
454
455 return rolePersistence.containsGroup(roleId, groupId);
456 }
457
458 public boolean hasUserGroup(long userId, long groupId)
459 throws SystemException {
460
461 if (groupFinder.countByG_U(groupId, userId) > 0) {
462 return true;
463 }
464 else {
465 return false;
466 }
467 }
468
469 public List<Group> search(
470 long companyId, String name, String description,
471 LinkedHashMap<String, Object> params, int begin, int end)
472 throws SystemException {
473
474 return groupFinder.findByC_N_D(
475 companyId, name, description, params, begin, end, null);
476 }
477
478 public List<Group> search(
479 long companyId, String name, String description,
480 LinkedHashMap<String, Object> params, int begin, int end,
481 OrderByComparator obc)
482 throws SystemException {
483
484 if (obc == null) {
485 obc = new GroupNameComparator(true);
486 }
487
488 return groupFinder.findByC_N_D(
489 companyId, name, description, params, begin, end, obc);
490 }
491
492 public int searchCount(
493 long companyId, String name, String description,
494 LinkedHashMap<String, Object> params)
495 throws SystemException {
496
497 return groupFinder.countByC_N_D(companyId, name, description, params);
498 }
499
500 public void setRoleGroups(long roleId, long[] groupIds)
501 throws PortalException, SystemException {
502
503 rolePersistence.setGroups(roleId, groupIds);
504
505 PermissionCacheUtil.clearCache();
506 }
507
508 public void unsetRoleGroups(long roleId, long[] groupIds)
509 throws PortalException, SystemException {
510
511 rolePersistence.removeGroups(roleId, groupIds);
512
513 PermissionCacheUtil.clearCache();
514 }
515
516 public void unsetUserGroups(long userId, long[] groupIds)
517 throws PortalException, SystemException {
518
519 userGroupRoleLocalService.deleteUserGroupRoles(userId, groupIds);
520
521 userPersistence.removeGroups(userId, groupIds);
522
523 PermissionCacheUtil.clearCache();
524 }
525
526 public Group updateFriendlyURL(long groupId, String friendlyURL)
527 throws PortalException, SystemException {
528
529 Group group = groupPersistence.findByPrimaryKey(groupId);
530
531 if (group.isUser()) {
532 User user = userPersistence.findByPrimaryKey(group.getClassPK());
533
534 friendlyURL = StringPool.SLASH + user.getScreenName();
535
536 if (group.getFriendlyURL().equals(friendlyURL)) {
537 return group;
538 }
539 }
540
541 friendlyURL = getFriendlyURL(groupId, group.getClassPK(), friendlyURL);
542
543 validateFriendlyURL(
544 group.getGroupId(), group.getCompanyId(), group.getClassNameId(),
545 group.getClassPK(), friendlyURL);
546
547 group.setFriendlyURL(friendlyURL);
548
549 groupPersistence.update(group, false);
550
551 return group;
552 }
553
554 public Group updateGroup(
555 long groupId, String name, String description, int type,
556 String friendlyURL, boolean active)
557 throws PortalException, SystemException {
558
559 Group group = groupPersistence.findByPrimaryKey(groupId);
560
561 long classNameId = group.getClassNameId();
562 long classPK = group.getClassPK();
563 friendlyURL = getFriendlyURL(groupId, classPK, friendlyURL);
564
565 if ((classNameId <= 0) || (classPK <= 0)) {
566 validateName(group.getGroupId(), group.getCompanyId(), name);
567 }
568
569 if (PortalUtil.isSystemGroup(group.getName()) &&
570 !group.getName().equals(name)) {
571
572 throw new RequiredGroupException();
573 }
574
575 validateFriendlyURL(
576 group.getGroupId(), group.getCompanyId(), group.getClassNameId(),
577 group.getClassPK(), friendlyURL);
578
579 group.setName(name);
580 group.setDescription(description);
581 group.setType(type);
582 group.setFriendlyURL(friendlyURL);
583 group.setActive(active);
584
585 groupPersistence.update(group, false);
586
587 return group;
588 }
589
590 public Group updateGroup(long groupId, String typeSettings)
591 throws PortalException, SystemException {
592
593 Group group = groupPersistence.findByPrimaryKey(groupId);
594
595 group.setTypeSettings(typeSettings);
596
597 groupPersistence.update(group, false);
598
599 return group;
600 }
601
602 public Group updateWorkflow(
603 long groupId, boolean workflowEnabled, int workflowStages,
604 String workflowRoleNames)
605 throws PortalException, SystemException {
606
607 Group group = groupPersistence.findByPrimaryKey(groupId);
608
609 Properties props = group.getTypeSettingsProperties();
610
611 props.setProperty("workflowEnabled", String.valueOf(workflowEnabled));
612
613 if (workflowEnabled) {
614 if (workflowStages < PropsValues.TASKS_DEFAULT_STAGES) {
615 workflowStages = PropsValues.TASKS_DEFAULT_STAGES;
616 }
617
618 if (Validator.isNull(workflowRoleNames)) {
619 workflowRoleNames = PropsValues.TASKS_DEFAULT_ROLE_NAMES;
620 }
621
622 props.setProperty("workflowStages", String.valueOf(workflowStages));
623 props.setProperty("workflowRoleNames", workflowRoleNames);
624 }
625
626 group.setTypeSettings(group.getTypeSettings());
627
628 groupPersistence.update(group, false);
629
630 if (!workflowEnabled) {
631 tasksProposalLocalService.deleteProposals(groupId);
632 }
633
634 return group;
635 }
636
637 protected void addDefaultLayouts(Group group)
638 throws PortalException, SystemException {
639
640 long defaultUserId = userLocalService.getDefaultUserId(
641 group.getCompanyId());
642 String name = PropsUtil.get(PropsUtil.DEFAULT_GUEST_LAYOUT_NAME);
643
644 String friendlyURL = PropsUtil.get(
645 PropsUtil.DEFAULT_GUEST_FRIENDLY_URL);
646
647 friendlyURL = getFriendlyURL(friendlyURL);
648
649 Layout layout = layoutLocalService.addLayout(
650 defaultUserId, group.getGroupId(), false,
651 LayoutImpl.DEFAULT_PARENT_LAYOUT_ID, name, StringPool.BLANK,
652 StringPool.BLANK, LayoutImpl.TYPE_PORTLET, false, friendlyURL);
653
654 LayoutTypePortlet layoutTypePortlet =
655 (LayoutTypePortlet)layout.getLayoutType();
656
657 String layoutTemplateId = PropsUtil.get(
658 PropsUtil.DEFAULT_GUEST_LAYOUT_TEMPLATE_ID);
659
660 layoutTypePortlet.setLayoutTemplateId(0, layoutTemplateId, false);
661
662 for (int i = 0; i < 10; i++) {
663 String columnId = "column-" + i;
664 String portletIds = PropsUtil.get(
665 PropsUtil.DEFAULT_GUEST_LAYOUT_COLUMN + i);
666
667 layoutTypePortlet.addPortletIds(
668 0, StringUtil.split(portletIds), columnId, false);
669 }
670
671 layoutLocalService.updateLayout(
672 layout.getGroupId(), layout.isPrivateLayout(), layout.getLayoutId(),
673 layout.getTypeSettings());
674 }
675
676 protected String getFriendlyURL(String friendlyURL) {
677 friendlyURL = GetterUtil.getString(friendlyURL);
678
679 return Normalizer.normalizeToAscii(friendlyURL.trim().toLowerCase());
680 }
681
682 protected String getFriendlyURL(
683 long groupId, long classPK, String friendlyURL) {
684
685 friendlyURL = getFriendlyURL(friendlyURL);
686
687 if (Validator.isNull(friendlyURL)) {
688 if (classPK > 0) {
689 friendlyURL = StringPool.SLASH + classPK;
690 }
691 else {
692 friendlyURL = StringPool.SLASH + groupId;
693 }
694 }
695
696 return friendlyURL;
697 }
698
699 protected void validateFriendlyURL(
700 long groupId, long companyId, long classNameId, long classPK,
701 String friendlyURL)
702 throws PortalException, SystemException {
703
704 if (Validator.isNull(friendlyURL)) {
705 return;
706 }
707
708 int exceptionType = LayoutImpl.validateFriendlyURL(friendlyURL);
709
710 if (exceptionType != -1) {
711 throw new GroupFriendlyURLException(exceptionType);
712 }
713
714 try {
715 Group group = groupPersistence.findByC_F(companyId, friendlyURL);
716
717 if (group.getGroupId() != groupId) {
718 throw new GroupFriendlyURLException(
719 GroupFriendlyURLException.DUPLICATE);
720 }
721 }
722 catch (NoSuchGroupException nsge) {
723 }
724
725 String groupIdFriendlyURL = friendlyURL.substring(1);
726
727 if (Validator.isNumber(groupIdFriendlyURL)) {
728 if (((classPK > 0) &&
729 (!groupIdFriendlyURL.equals(String.valueOf(classPK)))) ||
730 ((classPK == 0) &&
731 (!groupIdFriendlyURL.equals(String.valueOf(groupId))))) {
732
733 GroupFriendlyURLException gfurle =
734 new GroupFriendlyURLException(
735 GroupFriendlyURLException.POSSIBLE_DUPLICATE);
736
737 gfurle.setKeywordConflict(groupIdFriendlyURL);
738
739 throw gfurle;
740 }
741 }
742
743 String screenName = friendlyURL.substring(1);
744
745 User user = userPersistence.fetchByC_SN(companyId, screenName);
746
747 if (user != null) {
748 long userClassNameId = PortalUtil.getClassNameId(
749 User.class.getName());
750
751 if ((classNameId == userClassNameId) &&
752 (classPK == user.getUserId())) {
753 }
754 else {
755 throw new GroupFriendlyURLException(
756 GroupFriendlyURLException.DUPLICATE);
757 }
758 }
759 }
760
761 protected void validateName(long groupId, long companyId, String name)
762 throws PortalException, SystemException {
763
764 if ((Validator.isNull(name)) || (Validator.isNumber(name)) ||
765 (name.indexOf(StringPool.COMMA) != -1) ||
766 (name.indexOf(StringPool.STAR) != -1)) {
767
768 throw new GroupNameException();
769 }
770
771 try {
772 Group group = groupFinder.findByC_N(companyId, name);
773
774 if ((groupId <= 0) || (group.getGroupId() != groupId)) {
775 throw new DuplicateGroupException();
776 }
777 }
778 catch (NoSuchGroupException nsge) {
779 }
780 }
781
782 }