0001: /**
0002: * LibreSource
0003: * Copyright (C) 2004-2008 Artenum SARL / INRIA
0004: * http://www.libresource.org - contact@artenum.com
0005: *
0006: * This file is part of the LibreSource software,
0007: * which can be used and distributed under license conditions.
0008: * The license conditions are provided in the LICENSE.TXT file
0009: * at the root path of the packaging that enclose this file.
0010: * More information can be found at
0011: * - http://dev.libresource.org/home/license
0012: *
0013: * Initial authors :
0014: *
0015: * Guillaume Bort / INRIA
0016: * Francois Charoy / Universite Nancy 2
0017: * Julien Forest / Artenum
0018: * Claude Godart / Universite Henry Poincare
0019: * Florent Jouille / INRIA
0020: * Sebastien Jourdain / INRIA / Artenum
0021: * Yves Lerumeur / Artenum
0022: * Pascal Molli / Universite Henry Poincare
0023: * Gerald Oster / INRIA
0024: * Mariarosa Penzi / Artenum
0025: * Gerard Sookahet / Artenum
0026: * Raphael Tani / INRIA
0027: *
0028: * Contributors :
0029: *
0030: * Stephane Bagnier / Artenum
0031: * Amadou Dia / Artenum-IUP Blois
0032: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0033: */package org.libresource.forum.ejb;
0034:
0035: import org.libresource.Libresource;
0036: import org.libresource.LibresourceEvent;
0037: import org.libresource.LibresourceException;
0038: import org.libresource.LibresourceResourceIdentifier;
0039: import org.libresource.LibresourceResourceValue;
0040: import org.libresource.LibresourceServiceBase;
0041:
0042: import org.libresource.forum.ForumConstants;
0043: import org.libresource.forum.ForumExportHandler;
0044: import org.libresource.forum.ForumImportHandler;
0045: import org.libresource.forum.LibresourceForumException;
0046: import org.libresource.forum.MessageComparator;
0047: import org.libresource.forum.MessageExportHandler;
0048: import org.libresource.forum.MessageImportHandler;
0049: import org.libresource.forum.ThreadComparator;
0050: import org.libresource.forum.ThreadExportHandler;
0051: import org.libresource.forum.ThreadImportHandler;
0052: import org.libresource.forum.ejb.model.ForumResourceValue;
0053: import org.libresource.forum.ejb.model.MessageResourceValue;
0054: import org.libresource.forum.ejb.model.ThreadResourceValue;
0055: import org.libresource.forum.ejb.model.ThreadWithLastPostResourceValue;
0056: import org.libresource.forum.interfaces.ForumResourceLocal;
0057: import org.libresource.forum.interfaces.MessageResourceLocal;
0058: import org.libresource.forum.interfaces.ThreadResourceLocal;
0059: import org.libresource.forum.util.ForumResourceUtil;
0060: import org.libresource.forum.util.MessageResourceUtil;
0061: import org.libresource.forum.util.ThreadResourceUtil;
0062:
0063: import org.libresource.kernel.KernelConstants;
0064: import org.libresource.kernel.LibresourceSecurityException;
0065: import org.libresource.kernel.URINotExistException;
0066: import org.libresource.kernel.interfaces.KernelService;
0067:
0068: import org.libresource.membership.MembershipConstants;
0069: import org.libresource.membership.interfaces.MembershipService;
0070:
0071: import org.libresource.search.LibresourceIndexableContent;
0072:
0073: import org.libresource.xml.LibresourceExportHandler;
0074: import org.libresource.xml.LibresourceImportHandler;
0075:
0076: import java.net.URI;
0077:
0078: import java.util.Arrays;
0079: import java.util.Date;
0080:
0081: /**
0082: * The Libresource Forum service
0083: *
0084: * @libresource.service name="LibresourceForum" depends="Kernel,Membership,
0085: * LibresourceCore"
0086: */
0087: public abstract class LibresourceForumServiceBean extends
0088: LibresourceServiceBase {
0089: /**
0090: * @ejb.interface-method
0091: * @ejb.transaction type="Required"
0092: */
0093: public void createMessage(URI uri, String title, String body)
0094: throws LibresourceForumException,
0095: LibresourceSecurityException {
0096: try {
0097: KernelService kernelService = (KernelService) Libresource
0098: .getService(KernelConstants.SERVICE);
0099: MembershipService membershipService = (MembershipService) Libresource
0100: .getService(MembershipConstants.SERVICE);
0101: MessageResourceLocal messageResourceLocal = MessageResourceUtil
0102: .getLocalHome().create(title, body);
0103: String userName = membershipService.getProfile()
0104: .getFullName();
0105: messageResourceLocal.setAuthorName(userName);
0106: kernelService.bind(messageResourceLocal
0107: .getLibresourceResourceIdentifier(), uri, title);
0108:
0109: // event
0110: LibresourceEvent event = new LibresourceEvent(uri,
0111: messageResourceLocal
0112: .getLibresourceResourceIdentifier(),
0113: kernelService.getConnectedResource(),
0114: ForumConstants.EVENT_MESSAGE_CREATE);
0115: Libresource.throwEvent(event);
0116: } catch (LibresourceSecurityException se) {
0117: ctx.setRollbackOnly();
0118: throw se;
0119: } catch (Exception e) {
0120: ctx.setRollbackOnly();
0121: throw new LibresourceForumException(
0122: "Error in createMessage : " + e.getMessage(), e);
0123: }
0124: }
0125:
0126: /**
0127: * @ejb.interface-method
0128: * @ejb.transaction type="Required"
0129: */
0130: public void createMessage(URI uri, String title, String body,
0131: String author, Date date) throws LibresourceForumException,
0132: LibresourceSecurityException {
0133: try {
0134: KernelService kernelService = (KernelService) Libresource
0135: .getService(KernelConstants.SERVICE);
0136: MessageResourceLocal messageResourceLocal = MessageResourceUtil
0137: .getLocalHome().create(title, body);
0138: messageResourceLocal.setAuthorName(author);
0139: messageResourceLocal.setDate(date);
0140: kernelService.bind(messageResourceLocal
0141: .getLibresourceResourceIdentifier(), uri, title);
0142:
0143: // event
0144: LibresourceEvent event = new LibresourceEvent(uri,
0145: messageResourceLocal
0146: .getLibresourceResourceIdentifier(),
0147: kernelService.getConnectedResource(),
0148: ForumConstants.EVENT_MESSAGE_CREATE);
0149: Libresource.throwEvent(event);
0150: } catch (LibresourceSecurityException se) {
0151: ctx.setRollbackOnly();
0152: throw se;
0153: } catch (Exception e) {
0154: ctx.setRollbackOnly();
0155: throw new LibresourceForumException(
0156: "Error in createMessage : " + e.getMessage(), e);
0157: }
0158: }
0159:
0160: /**
0161: * @ejb.interface-method
0162: * @ejb.transaction type="Required"
0163: */
0164: public void editMessage(URI uri, String title, String body)
0165: throws LibresourceForumException,
0166: LibresourceSecurityException, URINotExistException {
0167: try {
0168: KernelService kernelService = (KernelService) Libresource
0169: .getService(KernelConstants.SERVICE);
0170: LibresourceResourceIdentifier resourceIdentifier = kernelService
0171: .lookup(uri);
0172:
0173: if (!kernelService.checkSecurity(uri,
0174: KernelConstants.SECURITY_UPDATE)) {
0175: throw new LibresourceSecurityException(uri,
0176: KernelConstants.SECURITY_UPDATE);
0177: }
0178:
0179: MessageResourceLocal messageResourceLocal = (MessageResourceLocal) Libresource
0180: .findResource(resourceIdentifier,
0181: MessageResourceLocal.class);
0182: messageResourceLocal.setTitle(title);
0183: messageResourceLocal.setBody(body);
0184:
0185: // update node
0186: kernelService.setShortName(uri, title);
0187: kernelService.setUpdateDate(uri, new Date());
0188:
0189: // event
0190: LibresourceEvent event = new LibresourceEvent(uri,
0191: resourceIdentifier, kernelService
0192: .getConnectedResource(),
0193: ForumConstants.EVENT_MESSAGE_EDIT);
0194: Libresource.throwEvent(event);
0195:
0196: // indexation
0197: Libresource.index(uri);
0198: } catch (LibresourceSecurityException se) {
0199: ctx.setRollbackOnly();
0200: throw se;
0201: } catch (URINotExistException se) {
0202: ctx.setRollbackOnly();
0203: throw se;
0204: } catch (Exception e) {
0205: ctx.setRollbackOnly();
0206: throw new LibresourceForumException(
0207: "Error in editMessage : " + e.getMessage(), e);
0208: }
0209: }
0210:
0211: /**
0212: * @ejb.interface-method
0213: * @ejb.transaction type="Required"
0214: */
0215: public void deleteMessage(URI uri)
0216: throws LibresourceForumException,
0217: LibresourceSecurityException, URINotExistException {
0218: try {
0219: KernelService kernelService = (KernelService) Libresource
0220: .getService(KernelConstants.SERVICE);
0221: LibresourceResourceIdentifier resourceIdentifier = kernelService
0222: .lookup(uri);
0223:
0224: // event
0225: LibresourceEvent event = new LibresourceEvent(uri,
0226: resourceIdentifier, kernelService
0227: .getConnectedResource(),
0228: ForumConstants.EVENT_MESSAGE_DELETE);
0229: Libresource.throwEvent(event);
0230:
0231: Libresource.checkType(resourceIdentifier,
0232: MessageResourceLocal.class);
0233: kernelService.deleteURI(uri);
0234: } catch (LibresourceSecurityException se) {
0235: ctx.setRollbackOnly();
0236: throw se;
0237: } catch (URINotExistException se) {
0238: ctx.setRollbackOnly();
0239: throw se;
0240: } catch (Exception e) {
0241: ctx.setRollbackOnly();
0242: throw new LibresourceForumException(
0243: "Error in deleteMessage : " + e.getMessage(), e);
0244: }
0245: }
0246:
0247: /**
0248: * @ejb.interface-method
0249: * @ejb.transaction type="Supports"
0250: */
0251: public MessageResourceValue getMessage(URI uri)
0252: throws LibresourceForumException,
0253: LibresourceSecurityException, URINotExistException {
0254: try {
0255: KernelService kernelService = (KernelService) Libresource
0256: .getService(KernelConstants.SERVICE);
0257: LibresourceResourceIdentifier resourceIdentifier = kernelService
0258: .lookup(uri);
0259: MessageResourceLocal messageResourceLocal = (MessageResourceLocal) Libresource
0260: .findResource(resourceIdentifier,
0261: MessageResourceLocal.class);
0262: MessageResourceValue resourceValue = messageResourceLocal
0263: .getMessageResourceValue();
0264: resourceValue.setUri(kernelService.normalizeURI(uri));
0265:
0266: return resourceValue;
0267: } catch (LibresourceSecurityException se) {
0268: throw se;
0269: } catch (URINotExistException se) {
0270: throw se;
0271: } catch (Exception e) {
0272: throw new LibresourceForumException(
0273: "Error in getMessage : " + e.getMessage(), e);
0274: }
0275: }
0276:
0277: /**
0278: * @ejb.interface-method
0279: * @ejb.transaction type="Supports"
0280: */
0281: public MessageResourceValue[] listMessagesAt(URI uri)
0282: throws LibresourceForumException,
0283: LibresourceSecurityException, URINotExistException {
0284: try {
0285: KernelService kernelService = (KernelService) Libresource
0286: .getService(KernelConstants.SERVICE);
0287: LibresourceResourceValue[] pages = kernelService
0288: .listResourcesAt(
0289: uri,
0290: MessageResourceLocal.LIBRESOURCE_RESOURCE_SERVICE,
0291: MessageResourceLocal.LIBRESOURCE_RESOURCE_TYPE);
0292: MessageResourceValue[] messageResourceValues = new MessageResourceValue[pages.length];
0293:
0294: for (int i = 0; i < messageResourceValues.length; i++) {
0295: LibresourceResourceIdentifier resourceIdentifier = pages[i]
0296: .getLibresourceResourceIdentifier();
0297: messageResourceValues[i] = ((MessageResourceLocal) Libresource
0298: .findResource(resourceIdentifier))
0299: .getMessageResourceValue();
0300: messageResourceValues[i].setUri(kernelService
0301: .normalizeURI(pages[i].getUri()));
0302: }
0303:
0304: return messageResourceValues;
0305: } catch (LibresourceSecurityException se) {
0306: throw se;
0307: } catch (URINotExistException se) {
0308: throw se;
0309: } catch (Exception e) {
0310: throw new LibresourceForumException(
0311: "Error in listMessagesAt : " + e.getMessage(), e);
0312: }
0313: }
0314:
0315: /**
0316: * @ejb.interface-method
0317: * @ejb.transaction type="Required"
0318: */
0319: public void createThread(URI uri, String name)
0320: throws LibresourceForumException,
0321: LibresourceSecurityException {
0322: try {
0323: KernelService kernelService = (KernelService) Libresource
0324: .getService(KernelConstants.SERVICE);
0325: ThreadResourceLocal threadResourceLocal = ThreadResourceUtil
0326: .getLocalHome().create(name);
0327: kernelService.bind(threadResourceLocal
0328: .getLibresourceResourceIdentifier(), uri, name);
0329:
0330: // event
0331: LibresourceEvent event = new LibresourceEvent(uri,
0332: threadResourceLocal
0333: .getLibresourceResourceIdentifier(),
0334: kernelService.getConnectedResource(),
0335: ForumConstants.EVENT_THREAD_CREATE);
0336: Libresource.throwEvent(event);
0337: } catch (LibresourceSecurityException se) {
0338: ctx.setRollbackOnly();
0339: throw se;
0340: } catch (Exception e) {
0341: ctx.setRollbackOnly();
0342: throw new LibresourceForumException(
0343: "Error in createThread : " + e.getMessage(), e);
0344: }
0345: }
0346:
0347: /**
0348: * @ejb.interface-method
0349: * @ejb.transaction type="Required"
0350: */
0351: public void editThread(URI uri, String name)
0352: throws LibresourceForumException,
0353: LibresourceSecurityException, URINotExistException {
0354: try {
0355: KernelService kernelService = (KernelService) Libresource
0356: .getService(KernelConstants.SERVICE);
0357: LibresourceResourceIdentifier resourceIdentifier = kernelService
0358: .lookup(uri);
0359:
0360: if (!kernelService.checkSecurity(uri,
0361: KernelConstants.SECURITY_UPDATE)) {
0362: throw new LibresourceSecurityException(uri,
0363: KernelConstants.SECURITY_UPDATE);
0364: }
0365:
0366: ThreadResourceLocal threadResourceLocal = (ThreadResourceLocal) Libresource
0367: .findResource(resourceIdentifier,
0368: ThreadResourceLocal.class);
0369: threadResourceLocal.setName(name);
0370:
0371: // update node
0372: kernelService.setShortName(uri, name);
0373: kernelService.setUpdateDate(uri, new Date());
0374:
0375: // event
0376: LibresourceEvent event = new LibresourceEvent(uri,
0377: resourceIdentifier, kernelService
0378: .getConnectedResource(),
0379: ForumConstants.EVENT_THREAD_EDIT);
0380: Libresource.throwEvent(event);
0381:
0382: // indexation
0383: Libresource.index(uri);
0384: } catch (LibresourceSecurityException se) {
0385: ctx.setRollbackOnly();
0386: throw se;
0387: } catch (URINotExistException se) {
0388: ctx.setRollbackOnly();
0389: throw se;
0390: } catch (Exception e) {
0391: ctx.setRollbackOnly();
0392: throw new LibresourceForumException(
0393: "Error in editThread : " + e.getMessage(), e);
0394: }
0395: }
0396:
0397: /**
0398: * @ejb.interface-method
0399: * @ejb.transaction type="Required"
0400: */
0401: public void deleteThread(URI uri) throws LibresourceForumException,
0402: LibresourceSecurityException, URINotExistException {
0403: try {
0404: KernelService kernelService = (KernelService) Libresource
0405: .getService(KernelConstants.SERVICE);
0406: LibresourceResourceIdentifier resourceIdentifier = kernelService
0407: .lookup(uri);
0408:
0409: // event
0410: LibresourceEvent event = new LibresourceEvent(uri,
0411: resourceIdentifier, kernelService
0412: .getConnectedResource(),
0413: ForumConstants.EVENT_THREAD_DELETE);
0414: Libresource.throwEvent(event);
0415:
0416: Libresource.checkType(resourceIdentifier,
0417: ThreadResourceLocal.class);
0418: kernelService.deleteURI(uri);
0419: } catch (LibresourceSecurityException se) {
0420: ctx.setRollbackOnly();
0421: throw se;
0422: } catch (URINotExistException se) {
0423: ctx.setRollbackOnly();
0424: throw se;
0425: } catch (Exception e) {
0426: ctx.setRollbackOnly();
0427: throw new LibresourceForumException(
0428: "Error in deleteThread : " + e.getMessage(), e);
0429: }
0430: }
0431:
0432: /**
0433: * @ejb.interface-method
0434: * @ejb.transaction type="Supports"
0435: */
0436: public ThreadResourceValue getThread(URI uri)
0437: throws LibresourceForumException,
0438: LibresourceSecurityException, URINotExistException {
0439: try {
0440: KernelService kernelService = (KernelService) Libresource
0441: .getService(KernelConstants.SERVICE);
0442: LibresourceResourceIdentifier resourceIdentifier = kernelService
0443: .lookup(uri);
0444: ThreadResourceLocal threadResourceLocal = (ThreadResourceLocal) Libresource
0445: .findResource(resourceIdentifier,
0446: ThreadResourceLocal.class);
0447: ThreadResourceValue resourceValue = threadResourceLocal
0448: .getThreadResourceValue();
0449: resourceValue.setUri(kernelService.normalizeURI(uri));
0450:
0451: return resourceValue;
0452: } catch (LibresourceSecurityException se) {
0453: throw se;
0454: } catch (URINotExistException se) {
0455: throw se;
0456: } catch (Exception e) {
0457: throw new LibresourceForumException("Error in getThread : "
0458: + e.getMessage(), e);
0459: }
0460: }
0461:
0462: /**
0463: * @ejb.interface-method
0464: * @ejb.transaction type="Supports"
0465: */
0466: public ThreadWithLastPostResourceValue[] listThreadsAt(URI uri)
0467: throws LibresourceForumException,
0468: LibresourceSecurityException, URINotExistException {
0469: try {
0470: KernelService kernelService = (KernelService) Libresource
0471: .getService(KernelConstants.SERVICE);
0472: LibresourceResourceValue[] pages = kernelService
0473: .listResourcesAt(
0474: uri,
0475: ThreadResourceLocal.LIBRESOURCE_RESOURCE_SERVICE,
0476: ThreadResourceLocal.LIBRESOURCE_RESOURCE_TYPE);
0477: ThreadWithLastPostResourceValue[] threadResourceValues = new ThreadWithLastPostResourceValue[pages.length];
0478:
0479: for (int i = 0; i < threadResourceValues.length; i++) {
0480: LibresourceResourceIdentifier resourceIdentifier = pages[i]
0481: .getLibresourceResourceIdentifier();
0482: ThreadResourceValue threadResourceValue = ((ThreadResourceLocal) Libresource
0483: .findResource(resourceIdentifier))
0484: .getThreadResourceValue();
0485: threadResourceValue.setUri(kernelService
0486: .normalizeURI(pages[i].getUri()));
0487:
0488: MessageResourceValue[] messageResourceValues = listMessagesInThread(pages[i]
0489: .getUri());
0490:
0491: if (messageResourceValues.length > 0) {
0492: threadResourceValues[i] = new ThreadWithLastPostResourceValue(
0493: threadResourceValue,
0494: messageResourceValues.length,
0495: messageResourceValues[0]);
0496: } else {
0497: threadResourceValues[i] = new ThreadWithLastPostResourceValue(
0498: threadResourceValue,
0499: messageResourceValues.length, null);
0500: }
0501: }
0502:
0503: return threadResourceValues;
0504: } catch (LibresourceSecurityException se) {
0505: throw se;
0506: } catch (URINotExistException se) {
0507: throw se;
0508: } catch (Exception e) {
0509: throw new LibresourceForumException(
0510: "Error in listThreadsAt : " + e.getMessage(), e);
0511: }
0512: }
0513:
0514: /**
0515: * @ejb.interface-method
0516: * @ejb.transaction type="Required"
0517: */
0518: public void replyToThread(URI threadUri, String title, String body)
0519: throws LibresourceForumException,
0520: LibresourceSecurityException, URINotExistException {
0521: try {
0522: KernelService kernelService = (KernelService) Libresource
0523: .getService(KernelConstants.SERVICE);
0524:
0525: if (!kernelService.checkSecurity(threadUri,
0526: ForumConstants.SECURITY_POST)) {
0527: throw new LibresourceSecurityException(threadUri,
0528: ForumConstants.SECURITY_POST);
0529: }
0530:
0531: // String id = ThreadResourceUtil.generateGUID(threadUri);
0532: String id = Libresource.convertStringToId(title);
0533: URI msgUri = new URI(kernelService
0534: .normalizeAbsoluteURIPath(threadUri)
0535: + "/" + id);
0536: int suffix = 0;
0537:
0538: while (kernelService.exist(msgUri)) {
0539: msgUri = new URI(kernelService
0540: .normalizeAbsoluteURIPath(threadUri)
0541: + "/" + id + "_" + suffix);
0542: suffix++;
0543: }
0544:
0545: MembershipService membershipService = (MembershipService) Libresource
0546: .getService(MembershipConstants.SERVICE);
0547: MessageResourceLocal messageResourceLocal = MessageResourceUtil
0548: .getLocalHome().create(title, body);
0549: String userName = membershipService.getProfile(
0550: kernelService.getConnectedResource()).getFullName();
0551: messageResourceLocal.setAuthorName(userName);
0552: kernelService.systemCreateURI(msgUri);
0553: kernelService.systemChown(msgUri, kernelService
0554: .getOwner(threadUri), false);
0555: kernelService.systemBind(messageResourceLocal
0556: .getLibresourceResourceIdentifier(), msgUri, title);
0557: kernelService.systemCreateAcl(msgUri, kernelService
0558: .getConnectedResource(),
0559: new String[] { KernelConstants.SECURITY_READ });
0560:
0561: // update node
0562: kernelService.setUpdateDate(threadUri, new Date());
0563:
0564: // event
0565: LibresourceEvent event = new LibresourceEvent(msgUri,
0566: kernelService.lookup(msgUri), kernelService
0567: .getConnectedResource(),
0568: ForumConstants.EVENT_MESSAGE_CREATE);
0569: Libresource.throwEvent(event);
0570: event = new LibresourceEvent(threadUri, kernelService
0571: .lookup(threadUri), kernelService
0572: .getConnectedResource(),
0573: ForumConstants.EVENT_THREAD_REPLY,
0574: messageResourceLocal.getTitle());
0575: Libresource.throwEvent(event);
0576: } catch (LibresourceSecurityException se) {
0577: ctx.setRollbackOnly();
0578: throw se;
0579: } catch (URINotExistException se) {
0580: ctx.setRollbackOnly();
0581: throw se;
0582: } catch (Exception e) {
0583: ctx.setRollbackOnly();
0584: throw new LibresourceForumException(
0585: "Error in replyToThread : " + e.getMessage(), e);
0586: }
0587: }
0588:
0589: /**
0590: * @ejb.interface-method
0591: * @ejb.transaction type="Supports"
0592: */
0593: public MessageResourceValue[] listMessagesInThread(URI uri)
0594: throws LibresourceForumException,
0595: LibresourceSecurityException, URINotExistException {
0596: try {
0597: MessageResourceValue[] messageResourceValues = listMessagesAt(uri);
0598: Arrays.sort(messageResourceValues, new MessageComparator());
0599:
0600: return messageResourceValues;
0601: } catch (LibresourceSecurityException se) {
0602: throw se;
0603: } catch (URINotExistException se) {
0604: throw se;
0605: } catch (Exception e) {
0606: throw new LibresourceForumException(
0607: "Error in listMessagesInThread : " + e.getMessage(),
0608: e);
0609: }
0610: }
0611:
0612: /**
0613: * @ejb.interface-method
0614: * @ejb.transaction type="Required"
0615: */
0616: public void createForum(URI uri, String name, String description)
0617: throws LibresourceForumException,
0618: LibresourceSecurityException {
0619: try {
0620: KernelService kernelService = (KernelService) Libresource
0621: .getService(KernelConstants.SERVICE);
0622: ForumResourceLocal forumResourceLocal = ForumResourceUtil
0623: .getLocalHome().create(name, description);
0624: kernelService.bind(forumResourceLocal
0625: .getLibresourceResourceIdentifier(), uri, name);
0626:
0627: // event
0628: LibresourceEvent event = new LibresourceEvent(uri,
0629: forumResourceLocal
0630: .getLibresourceResourceIdentifier(),
0631: kernelService.getConnectedResource(),
0632: ForumConstants.EVENT_FORUM_CREATE);
0633: Libresource.throwEvent(event);
0634: } catch (LibresourceSecurityException se) {
0635: ctx.setRollbackOnly();
0636: throw se;
0637: } catch (Exception e) {
0638: ctx.setRollbackOnly();
0639: throw new LibresourceForumException(
0640: "Error in createForum : " + e.getMessage(), e);
0641: }
0642: }
0643:
0644: /**
0645: * @ejb.interface-method
0646: * @ejb.transaction type="Required"
0647: */
0648: public void editForum(URI uri, String name, String description)
0649: throws LibresourceForumException,
0650: LibresourceSecurityException, URINotExistException {
0651: try {
0652: KernelService kernelService = (KernelService) Libresource
0653: .getService(KernelConstants.SERVICE);
0654: LibresourceResourceIdentifier resourceIdentifier = kernelService
0655: .lookup(uri);
0656:
0657: if (!kernelService.checkSecurity(uri,
0658: KernelConstants.SECURITY_UPDATE)) {
0659: throw new LibresourceSecurityException(uri,
0660: KernelConstants.SECURITY_UPDATE);
0661: }
0662:
0663: ForumResourceLocal forumResourceLocal = (ForumResourceLocal) Libresource
0664: .findResource(resourceIdentifier,
0665: ForumResourceLocal.class);
0666: forumResourceLocal.setName(name);
0667: forumResourceLocal.setDescription(description);
0668:
0669: // update node
0670: kernelService.setShortName(uri, name);
0671: kernelService.setUpdateDate(uri, new Date());
0672:
0673: // event
0674: LibresourceEvent event = new LibresourceEvent(uri,
0675: resourceIdentifier, kernelService
0676: .getConnectedResource(),
0677: ForumConstants.EVENT_FORUM_EDIT);
0678: Libresource.throwEvent(event);
0679:
0680: // indexation
0681: Libresource.index(uri);
0682: } catch (LibresourceSecurityException se) {
0683: ctx.setRollbackOnly();
0684: throw se;
0685: } catch (URINotExistException se) {
0686: ctx.setRollbackOnly();
0687: throw se;
0688: } catch (Exception e) {
0689: ctx.setRollbackOnly();
0690: throw new LibresourceForumException("Error in editForum : "
0691: + e.getMessage(), e);
0692: }
0693: }
0694:
0695: /**
0696: * @ejb.interface-method
0697: * @ejb.transaction type="Required"
0698: */
0699: public void deleteForum(URI uri) throws LibresourceForumException,
0700: LibresourceSecurityException, URINotExistException {
0701: try {
0702: KernelService kernelService = (KernelService) Libresource
0703: .getService(KernelConstants.SERVICE);
0704: LibresourceResourceIdentifier resourceIdentifier = kernelService
0705: .lookup(uri);
0706:
0707: // event
0708: LibresourceEvent event = new LibresourceEvent(uri,
0709: resourceIdentifier, kernelService
0710: .getConnectedResource(),
0711: ForumConstants.EVENT_FORUM_DELETE);
0712: Libresource.throwEvent(event);
0713:
0714: Libresource.checkType(resourceIdentifier,
0715: ForumResourceLocal.class);
0716: kernelService.deleteURI(uri);
0717: } catch (LibresourceSecurityException se) {
0718: ctx.setRollbackOnly();
0719: throw se;
0720: } catch (URINotExistException se) {
0721: ctx.setRollbackOnly();
0722: throw se;
0723: } catch (Exception e) {
0724: ctx.setRollbackOnly();
0725: throw new LibresourceForumException(
0726: "Error in deleteForum : " + e.getMessage(), e);
0727: }
0728: }
0729:
0730: /**
0731: * @ejb.interface-method
0732: * @ejb.transaction type="Supports"
0733: */
0734: public ForumResourceValue getForum(URI uri)
0735: throws LibresourceForumException,
0736: LibresourceSecurityException, URINotExistException {
0737: try {
0738: KernelService kernelService = (KernelService) Libresource
0739: .getService(KernelConstants.SERVICE);
0740: LibresourceResourceIdentifier resourceIdentifier = kernelService
0741: .lookup(uri);
0742: ForumResourceLocal forumResourceLocal = (ForumResourceLocal) Libresource
0743: .findResource(resourceIdentifier,
0744: ForumResourceLocal.class);
0745: ForumResourceValue resourceValue = forumResourceLocal
0746: .getForumResourceValue();
0747: resourceValue.setUri(kernelService.normalizeURI(uri));
0748:
0749: return resourceValue;
0750: } catch (LibresourceSecurityException se) {
0751: throw se;
0752: } catch (URINotExistException se) {
0753: throw se;
0754: } catch (Exception e) {
0755: throw new LibresourceForumException("Error in getForum : "
0756: + e.getMessage(), e);
0757: }
0758: }
0759:
0760: /**
0761: * @ejb.interface-method
0762: * @ejb.transaction type="Supports"
0763: */
0764: public ForumResourceValue[] listForumsAt(URI uri)
0765: throws LibresourceForumException,
0766: LibresourceSecurityException, URINotExistException {
0767: try {
0768: KernelService kernelService = (KernelService) Libresource
0769: .getService(KernelConstants.SERVICE);
0770: LibresourceResourceValue[] pages = kernelService
0771: .listResourcesAt(
0772: uri,
0773: ForumResourceLocal.LIBRESOURCE_RESOURCE_SERVICE,
0774: ForumResourceLocal.LIBRESOURCE_RESOURCE_TYPE);
0775: ForumResourceValue[] forumResourceValues = new ForumResourceValue[pages.length];
0776:
0777: for (int i = 0; i < forumResourceValues.length; i++) {
0778: LibresourceResourceIdentifier resourceIdentifier = pages[i]
0779: .getLibresourceResourceIdentifier();
0780: forumResourceValues[i] = ((ForumResourceLocal) Libresource
0781: .findResource(resourceIdentifier))
0782: .getForumResourceValue();
0783: forumResourceValues[i].setUri(kernelService
0784: .normalizeURI(pages[i].getUri()));
0785: }
0786:
0787: return forumResourceValues;
0788: } catch (LibresourceSecurityException se) {
0789: throw se;
0790: } catch (URINotExistException se) {
0791: throw se;
0792: } catch (Exception e) {
0793: throw new LibresourceForumException(
0794: "Error in listForumsAt : " + e.getMessage(), e);
0795: }
0796: }
0797:
0798: /**
0799: * @ejb.interface-method
0800: * @ejb.transaction type="Required"
0801: */
0802: public void postNewThreadInForum(URI forumUri, String title,
0803: String body) throws LibresourceForumException,
0804: LibresourceSecurityException, URINotExistException {
0805: try {
0806: KernelService kernelService = (KernelService) Libresource
0807: .getService(KernelConstants.SERVICE);
0808:
0809: if (!kernelService.checkSecurity(forumUri,
0810: ForumConstants.SECURITY_POST)) {
0811: throw new LibresourceSecurityException(forumUri,
0812: ForumConstants.SECURITY_POST);
0813: }
0814:
0815: // String id = ForumResourceUtil.generateGUID(forumUri);
0816: String id = Libresource.convertStringToId(title);
0817: URI threadUri = new URI(kernelService
0818: .normalizeAbsoluteURIPath(forumUri)
0819: + "/" + id);
0820: int suffix = 0;
0821:
0822: while (kernelService.exist(threadUri)) {
0823: threadUri = new URI(kernelService
0824: .normalizeAbsoluteURIPath(forumUri)
0825: + "/" + id + "_" + suffix);
0826: suffix++;
0827: }
0828:
0829: kernelService.systemCreateURI(threadUri);
0830: kernelService.systemChown(threadUri, kernelService
0831: .getOwner(forumUri), false);
0832:
0833: ThreadResourceLocal threadResourceLocal = ThreadResourceUtil
0834: .getLocalHome().create(title);
0835: kernelService.systemBind(threadResourceLocal
0836: .getLibresourceResourceIdentifier(), threadUri,
0837: title);
0838: kernelService.systemCopyAcl(forumUri, threadUri);
0839:
0840: // id = ThreadResourceUtil.generateGUID(threadUri);
0841: URI msgUri = new URI(kernelService
0842: .normalizeAbsoluteURIPath(threadUri)
0843: + "/" + id);
0844: suffix = 0;
0845:
0846: while (kernelService.exist(msgUri)) {
0847: msgUri = new URI(kernelService
0848: .normalizeAbsoluteURIPath(threadUri)
0849: + "/" + id + "_" + suffix);
0850: suffix++;
0851: }
0852:
0853: kernelService.systemCreateURI(msgUri);
0854: kernelService.systemChown(msgUri, kernelService
0855: .getOwner(forumUri), false);
0856:
0857: MembershipService membershipService = (MembershipService) Libresource
0858: .getService(MembershipConstants.SERVICE);
0859: MessageResourceLocal messageResourceLocal = MessageResourceUtil
0860: .getLocalHome().create(title, body);
0861: String userName = membershipService.getProfile(
0862: kernelService.getConnectedResource()).getFullName();
0863: messageResourceLocal.setAuthorName(userName);
0864: kernelService.systemBind(messageResourceLocal
0865: .getLibresourceResourceIdentifier(), msgUri, title);
0866: kernelService.systemCreateAcl(msgUri, kernelService
0867: .getConnectedResource(),
0868: new String[] { KernelConstants.SECURITY_READ });
0869:
0870: // update node
0871: kernelService.setUpdateDate(forumUri, new Date());
0872:
0873: // event
0874: LibresourceEvent event = new LibresourceEvent(threadUri,
0875: kernelService.lookup(threadUri), kernelService
0876: .getConnectedResource(),
0877: ForumConstants.EVENT_THREAD_CREATE);
0878: Libresource.throwEvent(event);
0879: event = new LibresourceEvent(forumUri, kernelService
0880: .lookup(forumUri), kernelService
0881: .getConnectedResource(),
0882: ForumConstants.EVENT_FORUM_POST,
0883: threadResourceLocal.getName());
0884: Libresource.throwEvent(event);
0885: } catch (LibresourceSecurityException se) {
0886: ctx.setRollbackOnly();
0887: throw se;
0888: } catch (URINotExistException se) {
0889: ctx.setRollbackOnly();
0890: throw se;
0891: } catch (Exception e) {
0892: ctx.setRollbackOnly();
0893: throw new LibresourceForumException(
0894: "Error in postNewThreadInForum : " + e.getMessage(),
0895: e);
0896: }
0897: }
0898:
0899: /**
0900: * @ejb.interface-method
0901: * @ejb.transaction type="Supports"
0902: */
0903: public ThreadWithLastPostResourceValue[] listThreadsInForum(URI uri)
0904: throws LibresourceForumException,
0905: LibresourceSecurityException, URINotExistException {
0906: try {
0907: ThreadWithLastPostResourceValue[] threadResourceValues = listThreadsAt(uri);
0908: Arrays.sort(threadResourceValues, new ThreadComparator());
0909:
0910: return threadResourceValues;
0911: } catch (LibresourceSecurityException se) {
0912: throw se;
0913: } catch (URINotExistException se) {
0914: throw se;
0915: } catch (Exception e) {
0916: throw new LibresourceForumException(
0917: "Error in listThreadsInForum : " + e.getMessage(),
0918: e);
0919: }
0920: }
0921:
0922: // system
0923:
0924: /**
0925: * @ejb.interface-method
0926: * @ejb.transaction type="Required"
0927: */
0928: public void systemCreateMessage(URI uri, String title, String body)
0929: throws LibresourceForumException,
0930: LibresourceSecurityException {
0931: try {
0932: KernelService kernelService = (KernelService) Libresource
0933: .getService(KernelConstants.SERVICE);
0934: MembershipService membershipService = (MembershipService) Libresource
0935: .getService(MembershipConstants.SERVICE);
0936: MessageResourceLocal messageResourceLocal = MessageResourceUtil
0937: .getLocalHome().create(title, body);
0938: String userName = membershipService.getProfile()
0939: .getFullName();
0940: messageResourceLocal.setAuthorName(userName);
0941: kernelService.systemCreateURI(uri);
0942: kernelService.systemBind(messageResourceLocal
0943: .getLibresourceResourceIdentifier(), uri, title);
0944:
0945: // event
0946: LibresourceEvent event = new LibresourceEvent(uri,
0947: messageResourceLocal
0948: .getLibresourceResourceIdentifier(),
0949: kernelService.getConnectedResource(),
0950: ForumConstants.EVENT_MESSAGE_CREATE);
0951: Libresource.throwEvent(event);
0952: } catch (LibresourceSecurityException se) {
0953: ctx.setRollbackOnly();
0954: throw se;
0955: } catch (Exception e) {
0956: ctx.setRollbackOnly();
0957: throw new LibresourceForumException(
0958: "Error in systemCreateMessage : " + e.getMessage(),
0959: e);
0960: }
0961: }
0962:
0963: /**
0964: * @ejb.interface-method
0965: * @ejb.transaction type="Required"
0966: */
0967: public void systemCreateMessage(URI uri, String id, String title,
0968: String body, String author)
0969: throws LibresourceForumException,
0970: LibresourceSecurityException {
0971: try {
0972: KernelService kernelService = (KernelService) Libresource
0973: .getService(KernelConstants.SERVICE);
0974: MessageResourceLocal messageResourceLocal = MessageResourceUtil
0975: .getLocalHome().create(id, title, body);
0976: messageResourceLocal.setAuthorName(author);
0977: kernelService.systemCreateURI(uri);
0978: kernelService.systemBind(messageResourceLocal
0979: .getLibresourceResourceIdentifier(), uri, title);
0980:
0981: // event
0982: LibresourceEvent event = new LibresourceEvent(uri,
0983: messageResourceLocal
0984: .getLibresourceResourceIdentifier(),
0985: kernelService.getConnectedResource(),
0986: ForumConstants.EVENT_MESSAGE_CREATE);
0987: Libresource.throwEvent(event);
0988: } catch (LibresourceSecurityException se) {
0989: ctx.setRollbackOnly();
0990: throw se;
0991: } catch (Exception e) {
0992: ctx.setRollbackOnly();
0993: throw new LibresourceForumException(
0994: "Error in systemCreateMessage : " + e.getMessage(),
0995: e);
0996: }
0997: }
0998:
0999: // libresource service
1000:
1001: /**
1002: * @ejb.interface-method
1003: * @ejb.transaction type="Supports"
1004: */
1005: public LibresourceIndexableContent getIndexableContent(
1006: LibresourceResourceIdentifier resourceIdentifier)
1007: throws LibresourceException {
1008: try {
1009: try {
1010: Libresource.checkType(resourceIdentifier,
1011: MessageResourceLocal.class);
1012:
1013: LibresourceIndexableContent content = new LibresourceIndexableContent();
1014: MessageResourceLocal messageResourceLocal = (MessageResourceLocal) Libresource
1015: .findResource(resourceIdentifier);
1016: content.addContentPart(messageResourceLocal.getTitle());
1017: content.addContentPart(messageResourceLocal.getBody());
1018:
1019: return content;
1020: } catch (LibresourceException e) {
1021: //
1022: }
1023:
1024: try {
1025: Libresource.checkType(resourceIdentifier,
1026: ThreadResourceLocal.class);
1027:
1028: LibresourceIndexableContent content = new LibresourceIndexableContent();
1029: ThreadResourceLocal resourceLocal = (ThreadResourceLocal) Libresource
1030: .findResource(resourceIdentifier);
1031: content.addContentPart(resourceLocal.getName());
1032:
1033: return content;
1034: } catch (LibresourceException e) {
1035: //
1036: }
1037:
1038: try {
1039: Libresource.checkType(resourceIdentifier,
1040: ForumResourceLocal.class);
1041:
1042: LibresourceIndexableContent content = new LibresourceIndexableContent();
1043: ForumResourceLocal resourceLocal = (ForumResourceLocal) Libresource
1044: .findResource(resourceIdentifier);
1045: content.addContentPart(resourceLocal.getName());
1046: content.addContentPart(resourceLocal.getDescription());
1047:
1048: return content;
1049: } catch (LibresourceException e) {
1050: //
1051: }
1052:
1053: return null;
1054: } catch (Exception e) {
1055: throw new LibresourceException(
1056: "error in LibresourceForum.getIndexableContent("
1057: + resourceIdentifier + ")", e);
1058: }
1059: }
1060:
1061: /**
1062: * @ejb.interface-method
1063: * @ejb.transaction type="Supports"
1064: */
1065: public String[] listAvailablesPermissions(
1066: LibresourceResourceIdentifier resourceIdentifier)
1067: throws LibresourceException {
1068: if (resourceIdentifier.getService().equals(
1069: ForumConstants.SERVICE)) {
1070: if (resourceIdentifier.getResourceType().equals(
1071: ForumConstants.RESOURCE_FORUM)
1072: || resourceIdentifier.getResourceType().equals(
1073: ForumConstants.RESOURCE_THREAD)) {
1074: return new String[] { ForumConstants.SECURITY_POST };
1075: }
1076: }
1077:
1078: return super .listAvailablesPermissions(resourceIdentifier);
1079: }
1080:
1081: /**
1082: * @ejb.interface-method
1083: * @ejb.transaction type="Supports"
1084: */
1085: public String[] listAvailablesEvents(
1086: LibresourceResourceIdentifier resourceIdentifier)
1087: throws LibresourceException {
1088: if (resourceIdentifier.getService().equals(
1089: ForumConstants.SERVICE)) {
1090: if (resourceIdentifier.getResourceType().equals(
1091: ForumConstants.RESOURCE_FORUM)) {
1092: return new String[] {
1093: ForumConstants.EVENT_FORUM_CREATE,
1094: ForumConstants.EVENT_FORUM_EDIT,
1095: ForumConstants.EVENT_FORUM_POST };
1096: }
1097:
1098: if (resourceIdentifier.getResourceType().equals(
1099: ForumConstants.RESOURCE_THREAD)) {
1100: return new String[] {
1101: ForumConstants.EVENT_THREAD_CREATE,
1102: ForumConstants.EVENT_THREAD_EDIT,
1103: ForumConstants.EVENT_THREAD_REPLY };
1104: }
1105:
1106: if (resourceIdentifier.getResourceType().equals(
1107: ForumConstants.RESOURCE_MESSAGE)) {
1108: return new String[] {
1109: ForumConstants.EVENT_MESSAGE_CREATE,
1110: ForumConstants.EVENT_MESSAGE_EDIT };
1111: }
1112: }
1113:
1114: return super .listAvailablesPermissions(resourceIdentifier);
1115: }
1116:
1117: /**
1118: * @ejb.interface-method
1119: * @ejb.transaction type="Supports"
1120: */
1121: public LibresourceExportHandler getXmlExportHandlerForResource(
1122: URI uri) throws LibresourceException {
1123: try {
1124: KernelService kernelService = (KernelService) Libresource
1125: .getService(KernelConstants.SERVICE);
1126: LibresourceResourceIdentifier resourceIdentifier = kernelService
1127: .lookup(uri);
1128:
1129: try {
1130: Libresource.checkType(resourceIdentifier,
1131: ForumResourceLocal.class);
1132:
1133: return new ForumExportHandler(uri);
1134: } catch (Exception e) {
1135: //
1136: }
1137:
1138: try {
1139: Libresource.checkType(resourceIdentifier,
1140: ThreadResourceLocal.class);
1141:
1142: return new ThreadExportHandler(uri);
1143: } catch (Exception e) {
1144: //
1145: }
1146:
1147: try {
1148: Libresource.checkType(resourceIdentifier,
1149: MessageResourceLocal.class);
1150:
1151: return new MessageExportHandler(uri);
1152: } catch (Exception e) {
1153: //
1154: }
1155:
1156: return super .getXmlExportHandlerForResource(uri);
1157: } catch (Exception e) {
1158: throw new LibresourceException(
1159: "Can't obtain exportHandler for uri " + uri, e);
1160: }
1161: }
1162:
1163: /**
1164: * @ejb.interface-method
1165: * @ejb.transaction type="Supports"
1166: */
1167: public LibresourceImportHandler getXmlImportHandler(String type)
1168: throws LibresourceException {
1169: if (type.equals(ForumConstants.RESOURCE_FORUM)) {
1170: return new ForumImportHandler();
1171: }
1172:
1173: if (type.equals(ForumConstants.RESOURCE_THREAD)) {
1174: return new ThreadImportHandler();
1175: }
1176:
1177: if (type.equals(ForumConstants.RESOURCE_MESSAGE)) {
1178: return new MessageImportHandler();
1179: }
1180:
1181: return super.getXmlImportHandler(type);
1182: }
1183: }
|