001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.service.impl;
022:
023: import com.liferay.portal.DuplicateGroupException;
024: import com.liferay.portal.GroupFriendlyURLException;
025: import com.liferay.portal.GroupNameException;
026: import com.liferay.portal.NoSuchGroupException;
027: import com.liferay.portal.NoSuchLayoutSetException;
028: import com.liferay.portal.NoSuchRoleException;
029: import com.liferay.portal.PortalException;
030: import com.liferay.portal.RequiredGroupException;
031: import com.liferay.portal.SystemException;
032: import com.liferay.portal.kernel.util.GetterUtil;
033: import com.liferay.portal.kernel.util.OrderByComparator;
034: import com.liferay.portal.kernel.util.StringPool;
035: import com.liferay.portal.kernel.util.StringUtil;
036: import com.liferay.portal.kernel.util.Validator;
037: import com.liferay.portal.model.Group;
038: import com.liferay.portal.model.Layout;
039: import com.liferay.portal.model.LayoutSet;
040: import com.liferay.portal.model.LayoutTypePortlet;
041: import com.liferay.portal.model.Organization;
042: import com.liferay.portal.model.Resource;
043: import com.liferay.portal.model.Role;
044: import com.liferay.portal.model.User;
045: import com.liferay.portal.model.UserGroup;
046: import com.liferay.portal.model.impl.GroupImpl;
047: import com.liferay.portal.model.impl.LayoutImpl;
048: import com.liferay.portal.model.impl.ResourceImpl;
049: import com.liferay.portal.model.impl.RoleImpl;
050: import com.liferay.portal.security.permission.PermissionCacheUtil;
051: import com.liferay.portal.service.base.GroupLocalServiceBaseImpl;
052: import com.liferay.portal.util.PortalUtil;
053: import com.liferay.portal.util.PropsUtil;
054: import com.liferay.portal.util.comparator.GroupNameComparator;
055: import com.liferay.util.Normalizer;
056:
057: import java.util.ArrayList;
058: import java.util.Iterator;
059: import java.util.LinkedHashMap;
060: import java.util.List;
061:
062: /**
063: * <a href="GroupLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
064: *
065: * @author Brian Wing Shun Chan
066: * @author Alexander Chow
067: *
068: */
069: public class GroupLocalServiceImpl extends GroupLocalServiceBaseImpl {
070:
071: public Group addGroup(long userId, String className, long classPK,
072: String name, String description, int type,
073: String friendlyURL, boolean active) throws PortalException,
074: SystemException {
075:
076: return addGroup(userId, className, classPK,
077: GroupImpl.DEFAULT_LIVE_GROUP_ID, name, description,
078: type, friendlyURL, active);
079: }
080:
081: public Group addGroup(long userId, String className, long classPK,
082: long liveGroupId, String name, String description,
083: int type, String friendlyURL, boolean active)
084: throws PortalException, SystemException {
085:
086: // Group
087:
088: User user = userPersistence.findByPrimaryKey(userId);
089: className = GetterUtil.getString(className);
090: long classNameId = PortalUtil.getClassNameId(className);
091:
092: if ((classNameId <= 0) || (classPK <= 0)) {
093: validateName(0, user.getCompanyId(), name);
094: }
095:
096: friendlyURL = getFriendlyURL(classNameId, friendlyURL);
097:
098: validateFriendlyURL(0, user.getCompanyId(), friendlyURL);
099:
100: long groupId = counterLocalService.increment();
101:
102: if ((classNameId > 0) && (classPK > 0)) {
103: name = String.valueOf(groupId);
104: }
105:
106: Group group = groupPersistence.create(groupId);
107:
108: group.setCompanyId(user.getCompanyId());
109: group.setCreatorUserId(userId);
110: group.setClassNameId(classNameId);
111: group.setClassPK(classPK);
112: group.setParentGroupId(GroupImpl.DEFAULT_PARENT_GROUP_ID);
113: group.setLiveGroupId(liveGroupId);
114: group.setName(name);
115: group.setDescription(description);
116: group.setType(type);
117: group.setFriendlyURL(friendlyURL);
118: group.setActive(active);
119:
120: groupPersistence.update(group);
121:
122: // Layout sets
123:
124: layoutSetLocalService.addLayoutSet(groupId, true);
125:
126: layoutSetLocalService.addLayoutSet(groupId, false);
127:
128: if ((classNameId <= 0) && (classPK <= 0)
129: && !user.isDefaultUser()) {
130:
131: // Resources
132:
133: resourceLocalService.addResources(group.getCompanyId(), 0,
134: 0, Group.class.getName(), group.getGroupId(),
135: false, false, false);
136:
137: // Community roles
138:
139: Role role = roleLocalService.getRole(group.getCompanyId(),
140: RoleImpl.COMMUNITY_OWNER);
141:
142: userGroupRoleLocalService.addUserGroupRoles(userId,
143: groupId, new long[] { role.getRoleId() });
144:
145: // User
146:
147: userLocalService.addGroupUsers(group.getGroupId(),
148: new long[] { userId });
149: } else if (className.equals(Organization.class.getName())
150: && !user.isDefaultUser()) {
151:
152: // Resources
153:
154: resourceLocalService.addResources(group.getCompanyId(), 0,
155: 0, Group.class.getName(), group.getGroupId(),
156: false, false, false);
157: }
158:
159: return group;
160: }
161:
162: public void addRoleGroups(long roleId, long[] groupIds)
163: throws PortalException, SystemException {
164:
165: rolePersistence.addGroups(roleId, groupIds);
166:
167: PermissionCacheUtil.clearCache();
168: }
169:
170: public void addUserGroups(long userId, long[] groupIds)
171: throws PortalException, SystemException {
172:
173: userPersistence.addGroups(userId, groupIds);
174:
175: User user = userPersistence.findByPrimaryKey(userId);
176:
177: Role role = rolePersistence.findByC_N(user.getCompanyId(),
178: RoleImpl.COMMUNITY_MEMBER);
179:
180: for (int i = 0; i < groupIds.length; i++) {
181: long groupId = groupIds[i];
182:
183: userGroupRoleLocalService.addUserGroupRoles(userId,
184: groupId, new long[] { role.getRoleId() });
185: }
186:
187: PermissionCacheUtil.clearCache();
188: }
189:
190: public void checkSystemGroups(long companyId)
191: throws PortalException, SystemException {
192:
193: long defaultUserId = userLocalService
194: .getDefaultUserId(companyId);
195:
196: String[] systemGroups = PortalUtil.getSystemGroups();
197:
198: for (int i = 0; i < systemGroups.length; i++) {
199: Group group = null;
200:
201: try {
202: group = groupFinder.findByC_N(companyId,
203: systemGroups[i]);
204: } catch (NoSuchGroupException nsge) {
205: String friendlyURL = null;
206:
207: if (systemGroups[i].equals(GroupImpl.GUEST)) {
208: friendlyURL = "/guest";
209: }
210:
211: group = addGroup(defaultUserId, null, 0,
212: systemGroups[i], null,
213: GroupImpl.TYPE_COMMUNITY_OPEN, friendlyURL,
214: true);
215: }
216:
217: if (group.getName().equals(GroupImpl.GUEST)) {
218: LayoutSet layoutSet = layoutSetLocalService
219: .getLayoutSet(group.getGroupId(), false);
220:
221: if (layoutSet.getPageCount() == 0) {
222: addDefaultLayouts(group);
223: }
224: }
225: }
226: }
227:
228: public void deleteGroup(long groupId) throws PortalException,
229: SystemException {
230:
231: Group group = groupPersistence.findByPrimaryKey(groupId);
232:
233: if (PortalUtil.isSystemGroup(group.getName())) {
234: throw new RequiredGroupException();
235: }
236:
237: // Layout sets
238:
239: try {
240: layoutSetLocalService.deleteLayoutSet(groupId, true);
241: } catch (NoSuchLayoutSetException nslse) {
242: }
243:
244: try {
245: layoutSetLocalService.deleteLayoutSet(groupId, false);
246: } catch (NoSuchLayoutSetException nslse) {
247: }
248:
249: // Role
250:
251: try {
252: Role role = roleLocalService.getGroupRole(group
253: .getCompanyId(), groupId);
254:
255: roleLocalService.deleteRole(role.getRoleId());
256: } catch (NoSuchRoleException nsre) {
257: }
258:
259: // Group roles
260:
261: userGroupRoleLocalService
262: .deleteUserGroupRolesByGroupId(groupId);
263:
264: // Membership requests
265:
266: membershipRequestLocalService.deleteMembershipRequests(group
267: .getGroupId());
268:
269: // Blogs
270:
271: blogsEntryLocalService.deleteEntries(groupId);
272: blogsStatsUserLocalService.deleteStatsUserByGroupId(groupId);
273:
274: // Bookmarks
275:
276: bookmarksFolderLocalService.deleteFolders(groupId);
277:
278: // Calendar
279:
280: calEventLocalService.deleteEvents(groupId);
281:
282: // Document library
283:
284: dlFolderLocalService.deleteFolders(groupId);
285:
286: // Image gallery
287:
288: igFolderLocalService.deleteFolders(groupId);
289:
290: // Journal
291:
292: journalArticleLocalService.deleteArticles(groupId);
293: journalTemplateLocalService.deleteTemplates(groupId);
294: journalStructureLocalService.deleteStructures(groupId);
295:
296: // Message boards
297:
298: mbBanLocalService.deleteBansByGroupId(groupId);
299: mbCategoryLocalService.deleteCategories(groupId);
300: mbStatsUserLocalService.deleteStatsUserByGroupId(groupId);
301:
302: // Polls
303:
304: pollsQuestionLocalService.deleteQuestions(groupId);
305:
306: // Shopping
307:
308: shoppingCartLocalService.deleteGroupCarts(groupId);
309: shoppingCategoryLocalService.deleteCategories(groupId);
310: shoppingCouponLocalService.deleteCoupons(groupId);
311: shoppingOrderLocalService.deleteOrders(groupId);
312:
313: // Software catalog
314:
315: scFrameworkVersionLocalService.deleteFrameworkVersions(groupId);
316: scProductEntryLocalService.deleteProductEntries(groupId);
317:
318: // Wiki
319:
320: wikiNodeLocalService.deleteNodes(groupId);
321:
322: // Resources
323:
324: Iterator itr = resourceFinder.findByC_P(group.getCompanyId(),
325: String.valueOf(groupId)).iterator();
326:
327: while (itr.hasNext()) {
328: Resource resource = (Resource) itr.next();
329:
330: resourceLocalService.deleteResource(resource);
331: }
332:
333: long organizationClassNameId = PortalUtil
334: .getClassNameId(Organization.class.getName());
335:
336: if (((group.getClassNameId() <= 0) && (group.getClassPK() <= 0))
337: || (group.getClassNameId() == organizationClassNameId)) {
338:
339: resourceLocalService.deleteResource(group.getCompanyId(),
340: Group.class.getName(),
341: ResourceImpl.SCOPE_INDIVIDUAL, group.getGroupId());
342: }
343:
344: // Group
345:
346: groupPersistence.remove(groupId);
347:
348: // Permission cache
349:
350: PermissionCacheUtil.clearCache();
351: }
352:
353: public Group getFriendlyURLGroup(long companyId, String friendlyURL)
354: throws PortalException, SystemException {
355:
356: if (Validator.isNull(friendlyURL)) {
357: throw new NoSuchGroupException();
358: }
359:
360: friendlyURL = getFriendlyURL(friendlyURL);
361:
362: return groupPersistence.findByC_F(companyId, friendlyURL);
363: }
364:
365: public Group getGroup(long groupId) throws PortalException,
366: SystemException {
367:
368: return groupPersistence.findByPrimaryKey(groupId);
369: }
370:
371: public Group getGroup(long companyId, String name)
372: throws PortalException, SystemException {
373:
374: return groupFinder.findByC_N(companyId, name);
375: }
376:
377: public Group getOrganizationGroup(long companyId,
378: long organizationId) throws PortalException,
379: SystemException {
380:
381: long classNameId = PortalUtil
382: .getClassNameId(Organization.class);
383:
384: return groupPersistence.findByC_C_C(companyId, classNameId,
385: organizationId);
386: }
387:
388: public List getOrganizationsGroups(List organizations)
389: throws PortalException, SystemException {
390:
391: List organizationGroups = new ArrayList();
392:
393: for (int i = 0; i < organizations.size(); i++) {
394: Organization organization = (Organization) organizations
395: .get(i);
396:
397: Group group = organization.getGroup();
398:
399: organizationGroups.add(group);
400: }
401:
402: return organizationGroups;
403: }
404:
405: public List getRoleGroups(long roleId) throws PortalException,
406: SystemException {
407:
408: return rolePersistence.getGroups(roleId);
409: }
410:
411: public Group getStagingGroup(long liveGroupId)
412: throws PortalException, SystemException {
413:
414: return groupPersistence.findByLiveGroupId(liveGroupId);
415: }
416:
417: public Group getUserGroup(long companyId, long userId)
418: throws PortalException, SystemException {
419:
420: long classNameId = PortalUtil.getClassNameId(User.class);
421:
422: return groupPersistence.findByC_C_C(companyId, classNameId,
423: userId);
424: }
425:
426: public Group getUserGroupGroup(long companyId, long userGroupId)
427: throws PortalException, SystemException {
428:
429: long classNameId = PortalUtil.getClassNameId(UserGroup.class);
430:
431: return groupPersistence.findByC_C_C(companyId, classNameId,
432: userGroupId);
433: }
434:
435: public List getUserGroups(long userId) throws PortalException,
436: SystemException {
437:
438: return userPersistence.getGroups(userId);
439: }
440:
441: public List getUserGroupsGroups(List userGroups)
442: throws PortalException, SystemException {
443:
444: List userGroupGroups = new ArrayList();
445:
446: for (int i = 0; i < userGroups.size(); i++) {
447: UserGroup userGroup = (UserGroup) userGroups.get(i);
448:
449: Group group = userGroup.getGroup();
450:
451: userGroupGroups.add(group);
452: }
453:
454: return userGroupGroups;
455: }
456:
457: public boolean hasRoleGroup(long roleId, long groupId)
458: throws PortalException, SystemException {
459:
460: return rolePersistence.containsGroup(roleId, groupId);
461: }
462:
463: public boolean hasUserGroup(long userId, long groupId)
464: throws SystemException {
465:
466: if (groupFinder.countByG_U(groupId, userId) > 0) {
467: return true;
468: } else {
469: return false;
470: }
471: }
472:
473: public List search(long companyId, String name, String description,
474: LinkedHashMap params, int begin, int end)
475: throws SystemException {
476:
477: return groupFinder.findByC_N_D(companyId, name, description,
478: params, begin, end, null);
479: }
480:
481: public List search(long companyId, String name, String description,
482: LinkedHashMap params, int begin, int end,
483: OrderByComparator obc) throws SystemException {
484:
485: if (obc == null) {
486: obc = new GroupNameComparator(true);
487: }
488:
489: return groupFinder.findByC_N_D(companyId, name, description,
490: params, begin, end, obc);
491: }
492:
493: public int searchCount(long companyId, String name,
494: String description, LinkedHashMap params)
495: throws SystemException {
496:
497: return groupFinder.countByC_N_D(companyId, name, description,
498: params);
499: }
500:
501: public void setRoleGroups(long roleId, long[] groupIds)
502: throws PortalException, SystemException {
503:
504: rolePersistence.setGroups(roleId, groupIds);
505:
506: PermissionCacheUtil.clearCache();
507: }
508:
509: public void unsetRoleGroups(long roleId, long[] groupIds)
510: throws PortalException, SystemException {
511:
512: rolePersistence.removeGroups(roleId, groupIds);
513:
514: PermissionCacheUtil.clearCache();
515: }
516:
517: public void unsetUserGroups(long userId, long[] groupIds)
518: throws PortalException, SystemException {
519:
520: userGroupRoleLocalService
521: .deleteUserGroupRoles(userId, groupIds);
522:
523: userPersistence.removeGroups(userId, groupIds);
524:
525: PermissionCacheUtil.clearCache();
526: }
527:
528: public Group updateGroup(long groupId, String name,
529: String description, int type, String friendlyURL,
530: boolean active) throws PortalException, SystemException {
531:
532: Group group = groupPersistence.findByPrimaryKey(groupId);
533:
534: long classNameId = group.getClassNameId();
535: long classPK = group.getClassPK();
536: friendlyURL = getFriendlyURL(classNameId, friendlyURL);
537:
538: if ((classNameId <= 0) || (classPK <= 0)) {
539: validateName(group.getGroupId(), group.getCompanyId(), name);
540: }
541:
542: if (PortalUtil.isSystemGroup(group.getName())
543: && !group.getName().equals(name)) {
544:
545: throw new RequiredGroupException();
546: }
547:
548: validateFriendlyURL(group.getGroupId(), group.getCompanyId(),
549: friendlyURL);
550:
551: group.setName(name);
552: group.setDescription(description);
553: group.setType(type);
554: group.setFriendlyURL(friendlyURL);
555: group.setActive(active);
556:
557: groupPersistence.update(group);
558:
559: return group;
560: }
561:
562: public Group updateGroup(long groupId, String typeSettings)
563: throws PortalException, SystemException {
564:
565: Group group = groupPersistence.findByPrimaryKey(groupId);
566:
567: group.setTypeSettings(typeSettings);
568:
569: groupPersistence.update(group);
570:
571: return group;
572: }
573:
574: protected void addDefaultLayouts(Group group)
575: throws PortalException, SystemException {
576:
577: long defaultUserId = userLocalService.getDefaultUserId(group
578: .getCompanyId());
579: String name = PropsUtil
580: .get(PropsUtil.DEFAULT_GUEST_LAYOUT_NAME);
581:
582: String friendlyURL = StringPool.BLANK;
583:
584: if (Validator.isNotNull(group.getFriendlyURL())) {
585: friendlyURL = PropsUtil
586: .get(PropsUtil.DEFAULT_GUEST_FRIENDLY_URL);
587: }
588:
589: friendlyURL = getFriendlyURL(friendlyURL);
590:
591: Layout layout = layoutLocalService.addLayout(defaultUserId,
592: group.getGroupId(), false,
593: LayoutImpl.DEFAULT_PARENT_LAYOUT_ID, name,
594: StringPool.BLANK, StringPool.BLANK,
595: LayoutImpl.TYPE_PORTLET, false, friendlyURL);
596:
597: LayoutTypePortlet layoutTypePortlet = (LayoutTypePortlet) layout
598: .getLayoutType();
599:
600: String layoutTemplateId = PropsUtil
601: .get(PropsUtil.DEFAULT_GUEST_LAYOUT_TEMPLATE_ID);
602:
603: layoutTypePortlet.setLayoutTemplateId(0, layoutTemplateId,
604: false);
605:
606: for (int i = 0; i < 10; i++) {
607: String columnId = "column-" + i;
608: String portletIds = PropsUtil
609: .get(PropsUtil.DEFAULT_GUEST_LAYOUT_COLUMN + i);
610:
611: layoutTypePortlet.addPortletIds(0, StringUtil
612: .split(portletIds), columnId, false);
613: }
614:
615: layoutLocalService.updateLayout(layout.getGroupId(), layout
616: .isPrivateLayout(), layout.getLayoutId(), layout
617: .getTypeSettings());
618: }
619:
620: protected String getFriendlyURL(String friendlyURL) {
621: return Normalizer.normalizeToAscii(friendlyURL.trim()
622: .toLowerCase());
623: }
624:
625: protected String getFriendlyURL(long classNameId, String friendlyURL)
626: throws PortalException, SystemException {
627:
628: if (classNameId > 0) {
629: long userClassNameId = PortalUtil
630: .getClassNameId(User.class);
631:
632: if (classNameId == userClassNameId) {
633: return StringPool.BLANK;
634: }
635: }
636:
637: return getFriendlyURL(GetterUtil.getString(friendlyURL));
638: }
639:
640: protected void validateFriendlyURL(long groupId, long companyId,
641: String friendlyURL) throws PortalException, SystemException {
642:
643: if (Validator.isNotNull(friendlyURL)) {
644: int exceptionType = LayoutImpl
645: .validateFriendlyURL(friendlyURL);
646:
647: if (exceptionType != -1) {
648: throw new GroupFriendlyURLException(exceptionType);
649: }
650:
651: try {
652: Group group = groupPersistence.findByC_F(companyId,
653: friendlyURL);
654:
655: if ((groupId <= 0) || (group.getGroupId() != groupId)) {
656: throw new GroupFriendlyURLException(
657: GroupFriendlyURLException.DUPLICATE);
658: }
659: } catch (NoSuchGroupException nsge) {
660: }
661:
662: String screenName = friendlyURL;
663:
664: if (screenName.startsWith(StringPool.SLASH)) {
665: screenName = friendlyURL.substring(1, friendlyURL
666: .length());
667: }
668:
669: User user = userPersistence.fetchByC_SN(companyId,
670: screenName);
671:
672: if (user != null) {
673: throw new GroupFriendlyURLException(
674: GroupFriendlyURLException.DUPLICATE);
675: }
676: }
677: }
678:
679: protected void validateName(long groupId, long companyId,
680: String name) throws PortalException, SystemException {
681:
682: if ((Validator.isNull(name)) || (Validator.isNumber(name))
683: || (name.indexOf(StringPool.COMMA) != -1)
684: || (name.indexOf(StringPool.STAR) != -1)) {
685:
686: throw new GroupNameException();
687: }
688:
689: try {
690: Group group = groupFinder.findByC_N(companyId, name);
691:
692: if ((groupId <= 0) || (group.getGroupId() != groupId)) {
693: throw new DuplicateGroupException();
694: }
695: } catch (NoSuchGroupException nsge) {
696: }
697: }
698:
699: }
|