1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.service.impl;
24  
25  import com.liferay.counter.service.CounterLocalServiceUtil;
26  import com.liferay.portal.DuplicateGroupException;
27  import com.liferay.portal.GroupFriendlyURLException;
28  import com.liferay.portal.GroupNameException;
29  import com.liferay.portal.NoSuchGroupException;
30  import com.liferay.portal.NoSuchLayoutSetException;
31  import com.liferay.portal.NoSuchRoleException;
32  import com.liferay.portal.PortalException;
33  import com.liferay.portal.RequiredGroupException;
34  import com.liferay.portal.SystemException;
35  import com.liferay.portal.kernel.util.GetterUtil;
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.Role;
46  import com.liferay.portal.model.User;
47  import com.liferay.portal.model.UserGroup;
48  import com.liferay.portal.model.impl.GroupImpl;
49  import com.liferay.portal.model.impl.LayoutImpl;
50  import com.liferay.portal.model.impl.ResourceImpl;
51  import com.liferay.portal.model.impl.RoleImpl;
52  import com.liferay.portal.security.permission.PermissionCacheUtil;
53  import com.liferay.portal.service.LayoutLocalServiceUtil;
54  import com.liferay.portal.service.LayoutSetLocalServiceUtil;
55  import com.liferay.portal.service.ResourceLocalServiceUtil;
56  import com.liferay.portal.service.RoleLocalServiceUtil;
57  import com.liferay.portal.service.UserGroupRoleLocalServiceUtil;
58  import com.liferay.portal.service.UserLocalServiceUtil;
59  import com.liferay.portal.service.base.GroupLocalServiceBaseImpl;
60  import com.liferay.portal.service.persistence.GroupFinder;
61  import com.liferay.portal.service.persistence.GroupUtil;
62  import com.liferay.portal.service.persistence.ResourceFinder;
63  import com.liferay.portal.service.persistence.RoleUtil;
64  import com.liferay.portal.service.persistence.UserUtil;
65  import com.liferay.portal.util.PortalUtil;
66  import com.liferay.portal.util.PropsUtil;
67  import com.liferay.portlet.blogs.service.BlogsEntryLocalServiceUtil;
68  import com.liferay.portlet.blogs.service.BlogsStatsUserLocalServiceUtil;
69  import com.liferay.portlet.bookmarks.service.BookmarksFolderLocalServiceUtil;
70  import com.liferay.portlet.calendar.service.CalEventLocalServiceUtil;
71  import com.liferay.portlet.documentlibrary.service.DLFolderLocalServiceUtil;
72  import com.liferay.portlet.imagegallery.service.IGFolderLocalServiceUtil;
73  import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
74  import com.liferay.portlet.messageboards.service.MBBanLocalServiceUtil;
75  import com.liferay.portlet.messageboards.service.MBCategoryLocalServiceUtil;
76  import com.liferay.portlet.messageboards.service.MBStatsUserLocalServiceUtil;
77  import com.liferay.portlet.polls.service.PollsQuestionLocalServiceUtil;
78  import com.liferay.portlet.shopping.service.ShoppingCartLocalServiceUtil;
79  import com.liferay.portlet.wiki.service.WikiNodeLocalServiceUtil;
80  
81  import java.util.ArrayList;
82  import java.util.Iterator;
83  import java.util.LinkedHashMap;
84  import java.util.List;
85  
86  /**
87   * <a href="GroupLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
88   *
89   * @author Brian Wing Shun Chan
90   * @author Alexander Chow
91   *
92   */
93  public class GroupLocalServiceImpl extends GroupLocalServiceBaseImpl {
94  
95      public Group addGroup(
96              long userId, String className, long classPK, String name,
97              String description, String type, String friendlyURL, boolean active)
98          throws PortalException, SystemException {
99  
100         return addGroup(
101             userId, className, classPK, GroupImpl.DEFAULT_LIVE_GROUP_ID, name,
102             description, type, friendlyURL, active);
103     }
104 
105     public Group addGroup(
106             long userId, String className, long classPK, long liveGroupId,
107             String name, String description, String type, String friendlyURL,
108             boolean active)
109         throws PortalException, SystemException {
110 
111         // Group
112 
113         User user = UserUtil.findByPrimaryKey(userId);
114         long classNameId = PortalUtil.getClassNameId(className);
115 
116         if ((classNameId <= 0) || (classPK <= 0)) {
117             validateName(0, user.getCompanyId(), name);
118         }
119 
120         friendlyURL = getFriendlyURL(classNameId, friendlyURL);
121 
122         validateFriendlyURL(0, user.getCompanyId(), friendlyURL);
123 
124         long groupId = CounterLocalServiceUtil.increment();
125 
126         if ((classNameId > 0) && (classPK > 0)) {
127             name = String.valueOf(groupId);
128         }
129 
130         Group group = GroupUtil.create(groupId);
131 
132         group.setCompanyId(user.getCompanyId());
133         group.setCreatorUserId(userId);
134         group.setClassNameId(classNameId);
135         group.setClassPK(classPK);
136         group.setParentGroupId(GroupImpl.DEFAULT_PARENT_GROUP_ID);
137         group.setLiveGroupId(liveGroupId);
138         group.setName(name);
139         group.setDescription(description);
140         group.setType(type);
141         group.setFriendlyURL(friendlyURL);
142         group.setActive(active);
143 
144         GroupUtil.update(group);
145 
146         // Layout sets
147 
148         LayoutSetLocalServiceUtil.addLayoutSet(groupId, true);
149 
150         LayoutSetLocalServiceUtil.addLayoutSet(groupId, false);
151 
152         if ((classNameId <= 0) && (classPK <= 0) && !user.isDefaultUser()) {
153 
154             // Resources
155 
156             ResourceLocalServiceUtil.addResources(
157                 group.getCompanyId(), 0, 0, Group.class.getName(),
158                 group.getGroupId(), false, false, false);
159 
160             // Community roles
161 
162             Role role = RoleLocalServiceUtil.getRole(
163                 group.getCompanyId(), RoleImpl.COMMUNITY_OWNER);
164 
165             UserGroupRoleLocalServiceUtil.addUserGroupRoles(
166                 userId, groupId, new long[] {role.getRoleId()});
167 
168             // User
169 
170             UserLocalServiceUtil.addGroupUsers(
171                 group.getGroupId(), new long[] {userId});
172         }
173 
174         return group;
175     }
176 
177     public void addRoleGroups(long roleId, long[] groupIds)
178         throws PortalException, SystemException {
179 
180         RoleUtil.addGroups(roleId, groupIds);
181 
182         PermissionCacheUtil.clearCache();
183     }
184 
185     public void addUserGroups(long userId, long[] groupIds)
186         throws PortalException, SystemException {
187 
188         UserUtil.addGroups(userId, groupIds);
189 
190         PermissionCacheUtil.clearCache();
191     }
192 
193     public void checkSystemGroups(long companyId)
194         throws PortalException, SystemException {
195 
196         long defaultUserId = UserLocalServiceUtil.getDefaultUserId(
197             companyId);
198 
199         String[] systemGroups = PortalUtil.getSystemGroups();
200 
201         for (int i = 0; i < systemGroups.length; i++) {
202             Group group = null;
203 
204             try {
205                 group = GroupFinder.findByC_N(companyId, systemGroups[i]);
206             }
207             catch (NoSuchGroupException nsge) {
208                 String friendlyURL = null;
209 
210                 if (systemGroups[i].equals(GroupImpl.GUEST)) {
211                     friendlyURL = "/guest";
212                 }
213 
214                 group = addGroup(
215                     defaultUserId, null, 0, systemGroups[i], null, null,
216                     friendlyURL, true);
217             }
218 
219             if (group.getName().equals(GroupImpl.GUEST)) {
220                 LayoutSet layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
221                     group.getGroupId(), false);
222 
223                 if (layoutSet.getPageCount() == 0) {
224                     addDefaultLayouts(group);
225                 }
226             }
227         }
228     }
229 
230     public void deleteGroup(long groupId)
231         throws PortalException, SystemException {
232 
233         Group group = GroupUtil.findByPrimaryKey(groupId);
234 
235         if (PortalUtil.isSystemGroup(group.getName())) {
236             throw new RequiredGroupException();
237         }
238 
239         // Layout sets
240 
241         try {
242             LayoutSetLocalServiceUtil.deleteLayoutSet(groupId, true);
243         }
244         catch (NoSuchLayoutSetException nslse) {
245         }
246 
247         try {
248             LayoutSetLocalServiceUtil.deleteLayoutSet(groupId, false);
249         }
250         catch (NoSuchLayoutSetException nslse) {
251         }
252 
253         // Role
254 
255         try {
256             Role role = RoleLocalServiceUtil.getGroupRole(
257                 group.getCompanyId(), groupId);
258 
259             RoleLocalServiceUtil.deleteRole(role.getRoleId());
260         }
261         catch (NoSuchRoleException nsre) {
262         }
263 
264         // Group roles
265 
266         UserGroupRoleLocalServiceUtil.deleteUserGroupRolesByGroupId(groupId);
267 
268         // Blogs
269 
270         BlogsEntryLocalServiceUtil.deleteEntries(groupId);
271         BlogsStatsUserLocalServiceUtil.deleteStatsUserByGroupId(groupId);
272 
273         // Bookmarks
274 
275         BookmarksFolderLocalServiceUtil.deleteFolders(groupId);
276 
277         // Calendar
278 
279         CalEventLocalServiceUtil.deleteEvents(groupId);
280 
281         // Document library
282 
283         DLFolderLocalServiceUtil.deleteFolders(groupId);
284 
285         // Image gallery
286 
287         IGFolderLocalServiceUtil.deleteFolders(groupId);
288 
289         // Journal
290 
291         JournalArticleLocalServiceUtil.deleteArticles(groupId);
292 
293         // Message boards
294 
295         MBBanLocalServiceUtil.deleteBansByGroupId(groupId);
296         MBCategoryLocalServiceUtil.deleteCategories(groupId);
297         MBStatsUserLocalServiceUtil.deleteStatsUserByGroupId(groupId);
298 
299         // Polls
300 
301         PollsQuestionLocalServiceUtil.deleteQuestions(groupId);
302 
303         // Shopping
304 
305         ShoppingCartLocalServiceUtil.deleteGroupCarts(groupId);
306 
307         // Wiki
308 
309         WikiNodeLocalServiceUtil.deleteNodes(groupId);
310 
311         // Resources
312 
313         Iterator itr = ResourceFinder.findByC_P(
314             group.getCompanyId(), String.valueOf(groupId)).iterator();
315 
316         while (itr.hasNext()) {
317             Resource resource = (Resource)itr.next();
318 
319             ResourceLocalServiceUtil.deleteResource(resource);
320         }
321 
322         if ((group.getClassNameId() <= 0) && (group.getClassPK() <= 0)) {
323             ResourceLocalServiceUtil.deleteResource(
324                 group.getCompanyId(), Group.class.getName(),
325                 ResourceImpl.SCOPE_INDIVIDUAL, group.getGroupId());
326         }
327 
328         // Group
329 
330         GroupUtil.remove(groupId);
331 
332         // Permission cache
333 
334         PermissionCacheUtil.clearCache();
335     }
336 
337     public Group getFriendlyURLGroup(long companyId, String friendlyURL)
338         throws PortalException, SystemException {
339 
340         if (Validator.isNull(friendlyURL)) {
341             throw new NoSuchGroupException();
342         }
343 
344         friendlyURL = friendlyURL.toLowerCase();
345 
346         return GroupUtil.findByC_F(companyId, friendlyURL);
347     }
348 
349     public Group getGroup(long groupId)
350         throws PortalException, SystemException {
351 
352         return GroupUtil.findByPrimaryKey(groupId);
353     }
354 
355     public Group getGroup(long companyId, String name)
356         throws PortalException, SystemException {
357 
358         return GroupFinder.findByC_N(companyId, name);
359     }
360 
361     public Group getOrganizationGroup(long companyId, long organizationId)
362         throws PortalException, SystemException {
363 
364         long classNameId = PortalUtil.getClassNameId(Organization.class);
365 
366         return GroupUtil.findByC_C_C(companyId, classNameId, organizationId);
367     }
368 
369     public List getOrganizationsGroups(List organizations)
370         throws PortalException, SystemException {
371 
372         List organizationGroups = new ArrayList();
373 
374         for (int i = 0; i < organizations.size(); i++) {
375             Organization organization = (Organization)organizations.get(i);
376 
377             Group group = organization.getGroup();
378 
379             organizationGroups.add(group);
380         }
381 
382         return organizationGroups;
383     }
384 
385     public List getRoleGroups(long roleId)
386         throws PortalException, SystemException {
387 
388         return RoleUtil.getGroups(roleId);
389     }
390 
391     public Group getStagingGroup(long liveGroupId)
392         throws PortalException, SystemException {
393 
394         return GroupUtil.findByLiveGroupId(liveGroupId);
395     }
396 
397     public Group getUserGroup(long companyId, long userId)
398         throws PortalException, SystemException {
399 
400         long classNameId = PortalUtil.getClassNameId(User.class);
401 
402         return GroupUtil.findByC_C_C(companyId, classNameId, userId);
403     }
404 
405     public Group getUserGroupGroup(long companyId, long userGroupId)
406         throws PortalException, SystemException {
407 
408         long classNameId = PortalUtil.getClassNameId(UserGroup.class);
409 
410         return GroupUtil.findByC_C_C(companyId, classNameId, userGroupId);
411     }
412 
413     public List getUserGroupsGroups(List userGroups)
414         throws PortalException, SystemException {
415 
416         List userGroupGroups = new ArrayList();
417 
418         for (int i = 0; i < userGroups.size(); i++) {
419             UserGroup userGroup = (UserGroup)userGroups.get(i);
420 
421             Group group = userGroup.getGroup();
422 
423             userGroupGroups.add(group);
424         }
425 
426         return userGroupGroups;
427     }
428 
429     public boolean hasRoleGroup(long roleId, long groupId)
430         throws PortalException, SystemException {
431 
432         return RoleUtil.containsGroup(roleId, groupId);
433     }
434 
435     public boolean hasUserGroup(long userId, long groupId)
436         throws SystemException {
437 
438         if (GroupFinder.countByG_U(groupId, userId) > 0) {
439             return true;
440         }
441         else {
442             return false;
443         }
444     }
445 
446     public List search(
447             long companyId, String name, String description,
448             LinkedHashMap params, int begin, int end)
449         throws SystemException {
450 
451         return GroupFinder.findByC_N_D(
452             companyId, name, description, params, begin, end);
453     }
454 
455     public int searchCount(
456             long companyId, String name, String description,
457             LinkedHashMap params)
458         throws SystemException {
459 
460         return GroupFinder.countByC_N_D(companyId, name, description, params);
461     }
462 
463     public void setRoleGroups(long roleId, long[] groupIds)
464         throws PortalException, SystemException {
465 
466         RoleUtil.setGroups(roleId, groupIds);
467 
468         PermissionCacheUtil.clearCache();
469     }
470 
471     public void setUserGroups(long userId, long[] groupIds)
472         throws PortalException, SystemException {
473 
474         UserUtil.setGroups(userId, groupIds);
475 
476         PermissionCacheUtil.clearCache();
477     }
478 
479     public void unsetRoleGroups(long roleId, long[] groupIds)
480         throws PortalException, SystemException {
481 
482         RoleUtil.removeGroups(roleId, groupIds);
483 
484         PermissionCacheUtil.clearCache();
485     }
486 
487     public void unsetUserGroups(long userId, long[] groupIds)
488         throws PortalException, SystemException {
489 
490         UserUtil.removeGroups(userId, groupIds);
491 
492         PermissionCacheUtil.clearCache();
493     }
494 
495     public Group updateGroup(
496             long groupId, String name, String description, String type,
497             String friendlyURL, boolean active)
498         throws PortalException, SystemException {
499 
500         Group group = GroupUtil.findByPrimaryKey(groupId);
501 
502         long classNameId = group.getClassNameId();
503         long classPK = group.getClassPK();
504         friendlyURL = getFriendlyURL(classNameId, friendlyURL);
505 
506         if ((classNameId <= 0) || (classPK <= 0)) {
507             validateName(group.getGroupId(), group.getCompanyId(), name);
508         }
509 
510         if (PortalUtil.isSystemGroup(group.getName()) &&
511             !group.getName().equals(name)) {
512 
513             throw new RequiredGroupException();
514         }
515 
516         validateFriendlyURL(
517             group.getGroupId(), group.getCompanyId(), friendlyURL);
518 
519         group.setName(name);
520         group.setDescription(description);
521         group.setType(type);
522         group.setFriendlyURL(friendlyURL);
523         group.setActive(active);
524 
525         GroupUtil.update(group);
526 
527         return group;
528     }
529 
530     public Group updateGroup(long groupId, String typeSettings)
531         throws PortalException, SystemException {
532 
533         Group group = GroupUtil.findByPrimaryKey(groupId);
534 
535         group.setTypeSettings(typeSettings);
536 
537         GroupUtil.update(group);
538 
539         return group;
540     }
541 
542     protected void addDefaultLayouts(Group group)
543         throws PortalException, SystemException {
544 
545         long defaultUserId = UserLocalServiceUtil.getDefaultUserId(
546             group.getCompanyId());
547         String name = PropsUtil.get(PropsUtil.DEFAULT_GUEST_LAYOUT_NAME);
548 
549         String friendlyURL = StringPool.BLANK;
550 
551         if (Validator.isNotNull(group.getFriendlyURL())) {
552             friendlyURL = PropsUtil.get(PropsUtil.DEFAULT_GUEST_FRIENDLY_URL);
553         }
554 
555         friendlyURL = friendlyURL.toLowerCase();
556 
557         Layout layout = LayoutLocalServiceUtil.addLayout(
558             defaultUserId, group.getGroupId(), false,
559             LayoutImpl.DEFAULT_PARENT_LAYOUT_ID, name, StringPool.BLANK,
560             StringPool.BLANK, LayoutImpl.TYPE_PORTLET, false, friendlyURL);
561 
562         LayoutTypePortlet layoutTypePortlet =
563             (LayoutTypePortlet)layout.getLayoutType();
564 
565         String layoutTemplateId = PropsUtil.get(
566             PropsUtil.DEFAULT_GUEST_LAYOUT_TEMPLATE_ID);
567 
568         layoutTypePortlet.setLayoutTemplateId(0, layoutTemplateId, false);
569 
570         for (int i = 0; i < 10; i++) {
571             String columnId = "column-" + i;
572             String portletIds = PropsUtil.get(
573                 PropsUtil.DEFAULT_GUEST_LAYOUT_COLUMN + i);
574 
575             layoutTypePortlet.addPortletIds(
576                 0, StringUtil.split(portletIds), columnId, false);
577         }
578 
579         LayoutLocalServiceUtil.updateLayout(
580             layout.getGroupId(), layout.isPrivateLayout(), layout.getLayoutId(),
581             layout.getTypeSettings());
582     }
583 
584     protected String getFriendlyURL(long classNameId, String friendlyURL)
585         throws PortalException, SystemException {
586 
587         if (classNameId > 0) {
588             long userClassNameId = PortalUtil.getClassNameId(User.class);
589 
590             if (classNameId == userClassNameId) {
591                 return StringPool.BLANK;
592             }
593         }
594 
595         friendlyURL = GetterUtil.getString(friendlyURL);
596 
597         return friendlyURL.toLowerCase();
598     }
599 
600     protected void validateFriendlyURL(
601             long groupId, long companyId, String friendlyURL)
602         throws PortalException, SystemException {
603 
604         if (Validator.isNotNull(friendlyURL)) {
605             int exceptionType = LayoutImpl.validateFriendlyURL(friendlyURL);
606 
607             if (exceptionType != -1) {
608                 throw new GroupFriendlyURLException(exceptionType);
609             }
610 
611             try {
612                 Group group = GroupUtil.findByC_F(companyId, friendlyURL);
613 
614                 if ((groupId <= 0) || (group.getGroupId() != groupId)) {
615                     throw new GroupFriendlyURLException(
616                         GroupFriendlyURLException.DUPLICATE);
617                 }
618             }
619             catch (NoSuchGroupException nsge) {
620             }
621 
622             String screenName = friendlyURL;
623 
624             if (screenName.startsWith(StringPool.SLASH)) {
625                 screenName = friendlyURL.substring(1, friendlyURL.length());
626             }
627 
628             User user = UserUtil.fetchByC_SN(companyId, screenName);
629 
630             if (user != null) {
631                 throw new GroupFriendlyURLException(
632                     GroupFriendlyURLException.DUPLICATE);
633             }
634         }
635     }
636 
637     protected void validateName(long groupId, long companyId, String name)
638         throws PortalException, SystemException {
639 
640         if ((Validator.isNull(name)) || (Validator.isNumber(name)) ||
641             (name.indexOf(StringPool.COMMA) != -1) ||
642             (name.indexOf(StringPool.STAR) != -1)) {
643 
644             throw new GroupNameException();
645         }
646 
647         try {
648             Group group = GroupFinder.findByC_N(companyId, name);
649 
650             if ((groupId <= 0) || (group.getGroupId() != groupId)) {
651                 throw new DuplicateGroupException();
652             }
653         }
654         catch (NoSuchGroupException nsge) {
655         }
656     }
657 
658 }