001: /**
002: * LibreSource
003: * Copyright (C) 2004-2008 Artenum SARL / INRIA
004: * http://www.libresource.org - contact@artenum.com
005: *
006: * This file is part of the LibreSource software,
007: * which can be used and distributed under license conditions.
008: * The license conditions are provided in the LICENSE.TXT file
009: * at the root path of the packaging that enclose this file.
010: * More information can be found at
011: * - http://dev.libresource.org/home/license
012: *
013: * Initial authors :
014: *
015: * Guillaume Bort / INRIA
016: * Francois Charoy / Universite Nancy 2
017: * Julien Forest / Artenum
018: * Claude Godart / Universite Henry Poincare
019: * Florent Jouille / INRIA
020: * Sebastien Jourdain / INRIA / Artenum
021: * Yves Lerumeur / Artenum
022: * Pascal Molli / Universite Henry Poincare
023: * Gerald Oster / INRIA
024: * Mariarosa Penzi / Artenum
025: * Gerard Sookahet / Artenum
026: * Raphael Tani / INRIA
027: *
028: * Contributors :
029: *
030: * Stephane Bagnier / Artenum
031: * Amadou Dia / Artenum-IUP Blois
032: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
033: */package org.libresource.wiki.ejb;
034:
035: import org.libresource.Libresource;
036: import org.libresource.LibresourceEvent;
037: import org.libresource.LibresourceException;
038: import org.libresource.LibresourceResourceIdentifier;
039: import org.libresource.LibresourceResourceValue;
040: import org.libresource.LibresourceServiceBase;
041:
042: import org.libresource.kernel.KernelConstants;
043: import org.libresource.kernel.LibresourceSecurityException;
044: import org.libresource.kernel.URINotExistException;
045: import org.libresource.kernel.interfaces.KernelService;
046:
047: import org.libresource.membership.MembershipConstants;
048: import org.libresource.membership.interfaces.MembershipService;
049:
050: import org.libresource.search.LibresourceIndexableContent;
051:
052: import org.libresource.wiki.HistoryEntry;
053: import org.libresource.wiki.LibresourceWikiException;
054: import org.libresource.wiki.PageExportHandler;
055: import org.libresource.wiki.PageImportHandler;
056: import org.libresource.wiki.WikiConstants;
057: import org.libresource.wiki.ejb.model.PageResourceValue;
058: import org.libresource.wiki.interfaces.PageResourceLocal;
059: import org.libresource.wiki.util.PageResourceUtil;
060:
061: import org.libresource.xml.LibresourceExportHandler;
062: import org.libresource.xml.LibresourceImportHandler;
063:
064: import java.net.URI;
065:
066: import java.util.Date;
067: import java.util.TreeSet;
068:
069: /**
070: * The Libresource Wiki service
071: *
072: * @libresource.service name="LibresourceWiki" depends="Kernel, Membership,
073: * LibresourceCore, LibresourceForum"
074: */
075: public abstract class LibresourceWikiServiceBean extends
076: LibresourceServiceBase {
077: /**
078: * @ejb.interface-method
079: * @ejb.transaction type="Required"
080: */
081: public void createPage(URI uri, String name, boolean version)
082: throws LibresourceWikiException,
083: LibresourceSecurityException {
084: try {
085: KernelService kernelService = (KernelService) Libresource
086: .getService(KernelConstants.SERVICE);
087:
088: if (!kernelService.checkSecurity(uri,
089: KernelConstants.SECURITY_CREATE)) {
090: throw new LibresourceSecurityException(uri,
091: KernelConstants.SECURITY_CREATE);
092: }
093:
094: systemCreatePage(uri, name, version);
095: } catch (LibresourceSecurityException se) {
096: ctx.setRollbackOnly();
097: throw se;
098: } catch (Exception e) {
099: ctx.setRollbackOnly();
100: throw new LibresourceWikiException("Error in createPage : "
101: + e.getMessage(), e);
102: }
103: }
104:
105: /**
106: * @ejb.interface-method
107: * @ejb.transaction type="Required"
108: */
109: public void systemCreatePage(URI uri, String name, boolean version)
110: throws LibresourceWikiException,
111: LibresourceSecurityException {
112: try {
113: KernelService kernelService = (KernelService) Libresource
114: .getService(KernelConstants.SERVICE);
115: MembershipService membershipService = (MembershipService) Libresource
116: .getService(MembershipConstants.SERVICE);
117:
118: PageResourceLocal pageResourceLocal = PageResourceUtil
119: .getLocalHome().create(
120: name,
121: membershipService.getProfile(
122: kernelService
123: .getConnectedResource())
124: .getFullName(), version);
125: kernelService.systemBind(pageResourceLocal
126: .getLibresourceResourceIdentifier(), uri, name);
127:
128: // event
129: LibresourceEvent event = new LibresourceEvent(uri,
130: pageResourceLocal
131: .getLibresourceResourceIdentifier(),
132: kernelService.getConnectedResource(),
133: WikiConstants.EVENT_PAGE_CREATE);
134: Libresource.throwEvent(event);
135: } catch (LibresourceSecurityException se) {
136: ctx.setRollbackOnly();
137: throw se;
138: } catch (Exception e) {
139: ctx.setRollbackOnly();
140: throw new LibresourceWikiException("Error in createPage : "
141: + e.getMessage(), e);
142: }
143: }
144:
145: /**
146: * @ejb.interface-method
147: * @ejb.transaction type="Required"
148: */
149: public void editPage(URI uri, String name, boolean versionable)
150: throws LibresourceWikiException,
151: LibresourceSecurityException, URINotExistException {
152: try {
153: KernelService kernelService = (KernelService) Libresource
154: .getService(KernelConstants.SERVICE);
155:
156: if (!kernelService.checkSecurity(uri,
157: KernelConstants.SECURITY_UPDATE)) {
158: throw new LibresourceSecurityException(uri,
159: KernelConstants.SECURITY_UPDATE);
160: }
161:
162: systemEditPage(uri, name, versionable);
163: } catch (LibresourceSecurityException se) {
164: ctx.setRollbackOnly();
165: throw se;
166: } catch (URINotExistException se) {
167: ctx.setRollbackOnly();
168: throw se;
169: } catch (Exception e) {
170: ctx.setRollbackOnly();
171: throw new LibresourceWikiException("Error in editPage : "
172: + e.getMessage(), e);
173: }
174: }
175:
176: /**
177: * @ejb.interface-method
178: * @ejb.transaction type="Required"
179: */
180: public void systemEditPage(URI uri, String name, boolean versionable)
181: throws LibresourceWikiException,
182: LibresourceSecurityException, URINotExistException {
183: try {
184: KernelService kernelService = (KernelService) Libresource
185: .getService(KernelConstants.SERVICE);
186: LibresourceResourceIdentifier resourceIdentifier = kernelService
187: .lookup(uri);
188:
189: PageResourceLocal pageResourceLocal = (PageResourceLocal) Libresource
190: .findResource(resourceIdentifier,
191: PageResourceLocal.class);
192: pageResourceLocal.setName(name);
193: pageResourceLocal.setVersionable(versionable);
194:
195: if (!versionable) {
196: pageResourceLocal.cleanHistory();
197: }
198:
199: // update node
200: kernelService.setShortName(uri, name);
201: kernelService.setUpdateDate(uri, new Date());
202:
203: // event
204: LibresourceEvent event = new LibresourceEvent(uri,
205: resourceIdentifier, kernelService
206: .getConnectedResource(),
207: WikiConstants.EVENT_PAGE_EDIT);
208: Libresource.throwEvent(event);
209:
210: // indexation
211: Libresource.index(uri);
212: } catch (LibresourceSecurityException se) {
213: ctx.setRollbackOnly();
214: throw se;
215: } catch (URINotExistException se) {
216: ctx.setRollbackOnly();
217: throw se;
218: } catch (Exception e) {
219: ctx.setRollbackOnly();
220: throw new LibresourceWikiException("Error in editPage : "
221: + e.getMessage(), e);
222: }
223: }
224:
225: /**
226: * @ejb.interface-method
227: * @ejb.transaction type="Required"
228: */
229: public void editPageContent(URI uri, String content, String comment)
230: throws LibresourceWikiException,
231: LibresourceSecurityException, URINotExistException {
232: try {
233: KernelService kernelService = (KernelService) Libresource
234: .getService(KernelConstants.SERVICE);
235:
236: if (!kernelService.checkSecurity(uri,
237: KernelConstants.SECURITY_UPDATE)) {
238: throw new LibresourceSecurityException(uri,
239: KernelConstants.SECURITY_UPDATE);
240: }
241:
242: systemEditPageContent(uri, content, comment);
243: } catch (LibresourceSecurityException se) {
244: ctx.setRollbackOnly();
245: throw se;
246: } catch (URINotExistException se) {
247: ctx.setRollbackOnly();
248: throw se;
249: } catch (Exception e) {
250: ctx.setRollbackOnly();
251: throw new LibresourceWikiException(
252: "Error in editPageContent : " + e.getMessage(), e);
253: }
254: }
255:
256: /**
257: * @ejb.interface-method
258: * @ejb.transaction type="Required"
259: */
260: public void systemEditPageContent(URI uri, String content,
261: String comment) throws LibresourceWikiException,
262: LibresourceSecurityException, URINotExistException {
263: try {
264: KernelService kernelService = (KernelService) Libresource
265: .getService(KernelConstants.SERVICE);
266: MembershipService membershipService = (MembershipService) Libresource
267: .getService(MembershipConstants.SERVICE);
268: LibresourceResourceIdentifier resourceIdentifier = kernelService
269: .lookup(uri);
270:
271: PageResourceLocal pageResourceLocal = (PageResourceLocal) Libresource
272: .findResource(resourceIdentifier,
273: PageResourceLocal.class);
274: String user = membershipService.getProfile(
275: kernelService.getConnectedResource()).getFullName();
276:
277: pageResourceLocal.setContent(content);
278: pageResourceLocal.setLastEdited(new Date());
279: pageResourceLocal.setLastEditor(user);
280:
281: // add current version to history
282: if (pageResourceLocal.getVersionable()) {
283: pageResourceLocal.saveState(user, comment);
284: }
285:
286: // update node
287: kernelService.setUpdateDate(uri, new Date());
288:
289: // event
290: LibresourceEvent event = new LibresourceEvent(uri,
291: resourceIdentifier, kernelService
292: .getConnectedResource(),
293: WikiConstants.EVENT_PAGE_EDIT_CONTENT, comment);
294: Libresource.throwEvent(event);
295:
296: // indexation
297: Libresource.index(uri);
298: } catch (LibresourceSecurityException se) {
299: ctx.setRollbackOnly();
300: throw se;
301: } catch (URINotExistException se) {
302: ctx.setRollbackOnly();
303: throw se;
304: } catch (Exception e) {
305: ctx.setRollbackOnly();
306: throw new LibresourceWikiException(
307: "Error in editPageContent : " + e.getMessage(), e);
308: }
309: }
310:
311: /**
312: * @ejb.interface-method
313: * @ejb.transaction type="Required"
314: */
315: public void editPageContent(URI uri, String content, String editor,
316: Date editionDate) throws LibresourceWikiException,
317: LibresourceSecurityException, URINotExistException {
318: try {
319: KernelService kernelService = (KernelService) Libresource
320: .getService(KernelConstants.SERVICE);
321:
322: if (!kernelService.checkSecurity(uri,
323: KernelConstants.SECURITY_UPDATE)) {
324: throw new LibresourceSecurityException(uri,
325: KernelConstants.SECURITY_UPDATE);
326: }
327:
328: systemEditPageContent(uri, content, editor, editionDate);
329: } catch (LibresourceSecurityException se) {
330: ctx.setRollbackOnly();
331: throw se;
332: } catch (URINotExistException se) {
333: ctx.setRollbackOnly();
334: throw se;
335: } catch (Exception e) {
336: ctx.setRollbackOnly();
337: throw new LibresourceWikiException(
338: "Error in editPageContent : " + e.getMessage(), e);
339: }
340: }
341:
342: /**
343: * @ejb.interface-method
344: * @ejb.transaction type="Required"
345: */
346: public void systemEditPageContent(URI uri, String content,
347: String editor, Date editionDate)
348: throws LibresourceWikiException,
349: LibresourceSecurityException, URINotExistException {
350: try {
351: KernelService kernelService = (KernelService) Libresource
352: .getService(KernelConstants.SERVICE);
353: LibresourceResourceIdentifier resourceIdentifier = kernelService
354: .lookup(uri);
355:
356: PageResourceLocal pageResourceLocal = (PageResourceLocal) Libresource
357: .findResource(resourceIdentifier,
358: PageResourceLocal.class);
359:
360: pageResourceLocal.setContent(content);
361: pageResourceLocal.setLastEdited(editionDate);
362: pageResourceLocal.setLastEditor(editor);
363:
364: // update node
365: kernelService.setUpdateDate(uri, new Date());
366:
367: // event
368: LibresourceEvent event = new LibresourceEvent(uri,
369: resourceIdentifier, kernelService
370: .getConnectedResource(),
371: WikiConstants.EVENT_PAGE_EDIT_CONTENT);
372: Libresource.throwEvent(event);
373:
374: // indexation
375: Libresource.index(uri);
376: } catch (LibresourceSecurityException se) {
377: ctx.setRollbackOnly();
378: throw se;
379: } catch (URINotExistException se) {
380: ctx.setRollbackOnly();
381: throw se;
382: } catch (Exception e) {
383: ctx.setRollbackOnly();
384: throw new LibresourceWikiException(
385: "Error in editPageContent : " + e.getMessage(), e);
386: }
387: }
388:
389: /**
390: * @ejb.interface-method
391: * @ejb.transaction type="Required"
392: */
393: public void deletePage(URI uri) throws LibresourceWikiException,
394: LibresourceSecurityException, URINotExistException {
395: try {
396: KernelService kernelService = (KernelService) Libresource
397: .getService(KernelConstants.SERVICE);
398: LibresourceResourceIdentifier resourceIdentifier = kernelService
399: .lookup(uri);
400:
401: // event
402: LibresourceEvent event = new LibresourceEvent(uri,
403: resourceIdentifier, kernelService
404: .getConnectedResource(),
405: WikiConstants.EVENT_PAGE_DELETE);
406: Libresource.throwEvent(event);
407:
408: Libresource.checkType(resourceIdentifier,
409: PageResourceLocal.class);
410: kernelService.deleteURI(uri);
411: } catch (LibresourceSecurityException se) {
412: ctx.setRollbackOnly();
413: throw se;
414: } catch (URINotExistException se) {
415: ctx.setRollbackOnly();
416: throw se;
417: } catch (Exception e) {
418: ctx.setRollbackOnly();
419: throw new LibresourceWikiException("Error in deletePage : "
420: + e.getMessage(), e);
421: }
422: }
423:
424: /**
425: * @ejb.interface-method
426: * @ejb.transaction type="Supports"
427: */
428: public PageResourceValue getPage(URI uri)
429: throws LibresourceWikiException,
430: LibresourceSecurityException, URINotExistException {
431: try {
432: KernelService kernelService = (KernelService) Libresource
433: .getService(KernelConstants.SERVICE);
434: LibresourceResourceIdentifier resourceIdentifier = kernelService
435: .lookup(uri);
436: PageResourceLocal pageResourceLocal = (PageResourceLocal) Libresource
437: .findResource(resourceIdentifier,
438: PageResourceLocal.class);
439: PageResourceValue resourceValue = pageResourceLocal
440: .getPageResourceValue();
441: resourceValue.setUri(kernelService.normalizeURI(uri));
442:
443: return resourceValue;
444: } catch (LibresourceSecurityException se) {
445: throw se;
446: } catch (URINotExistException se) {
447: throw se;
448: } catch (Exception e) {
449: throw new LibresourceWikiException("Error in getPage : "
450: + e.getMessage(), e);
451: }
452: }
453:
454: /**
455: * @ejb.interface-method
456: * @ejb.transaction type="Supports"
457: */
458: public HistoryEntry getPageVersion(URI uri, int version)
459: throws LibresourceWikiException,
460: LibresourceSecurityException, URINotExistException {
461: try {
462: KernelService kernelService = (KernelService) Libresource
463: .getService(KernelConstants.SERVICE);
464: LibresourceResourceIdentifier resourceIdentifier = kernelService
465: .lookup(uri);
466: PageResourceLocal pageResourceLocal = (PageResourceLocal) Libresource
467: .findResource(resourceIdentifier,
468: PageResourceLocal.class);
469:
470: return pageResourceLocal.getPageVersion(version);
471: } catch (LibresourceSecurityException se) {
472: throw se;
473: } catch (URINotExistException se) {
474: throw se;
475: } catch (Exception e) {
476: throw new LibresourceWikiException(
477: "Error in getPageVersion : " + e.getMessage(), e);
478: }
479: }
480:
481: /**
482: * @ejb.interface-method
483: * @ejb.transaction type="Required"
484: */
485: public void restorePageVersion(URI uri, int version)
486: throws LibresourceWikiException,
487: LibresourceSecurityException, URINotExistException {
488: HistoryEntry versionEntry = getPageVersion(uri, version);
489:
490: if (versionEntry != null) {
491: editPageContent(uri, versionEntry.getContent(),
492: "restored from version " + version);
493: }
494: }
495:
496: /**
497: * @ejb.interface-method
498: * @ejb.transaction type="Supports"
499: */
500: public TreeSet getPageHistory(URI uri, int nbEntries)
501: throws LibresourceWikiException,
502: LibresourceSecurityException, URINotExistException {
503: try {
504: KernelService kernelService = (KernelService) Libresource
505: .getService(KernelConstants.SERVICE);
506: LibresourceResourceIdentifier resourceIdentifier = kernelService
507: .lookup(uri);
508: PageResourceLocal pageResourceLocal = (PageResourceLocal) Libresource
509: .findResource(resourceIdentifier,
510: PageResourceLocal.class);
511:
512: return pageResourceLocal.getHistory(nbEntries);
513: } catch (LibresourceSecurityException se) {
514: throw se;
515: } catch (URINotExistException se) {
516: throw se;
517: } catch (Exception e) {
518: throw new LibresourceWikiException(
519: "Error in getPageHistory : " + e.getMessage(), e);
520: }
521: }
522:
523: /**
524: * @ejb.interface-method
525: * @ejb.transaction type="Required"
526: */
527: public void importPageHistory(URI uri, TreeSet history)
528: throws LibresourceWikiException,
529: LibresourceSecurityException, URINotExistException {
530: try {
531: KernelService kernelService = (KernelService) Libresource
532: .getService(KernelConstants.SERVICE);
533: LibresourceResourceIdentifier resourceIdentifier = kernelService
534: .lookup(uri);
535: PageResourceLocal pageResourceLocal = (PageResourceLocal) Libresource
536: .findResource(resourceIdentifier,
537: PageResourceLocal.class);
538:
539: if (!kernelService.checkSecurity(uri,
540: KernelConstants.SECURITY_UPDATE)) {
541: throw new LibresourceSecurityException(uri,
542: KernelConstants.SECURITY_UPDATE);
543: }
544:
545: pageResourceLocal.importHistory(history);
546:
547: // update node
548: kernelService.setUpdateDate(uri, new Date());
549: } catch (LibresourceSecurityException se) {
550: throw se;
551: } catch (URINotExistException se) {
552: throw se;
553: } catch (Exception e) {
554: throw new LibresourceWikiException(
555: "Error in importPageHistory : " + e.getMessage(), e);
556: }
557: }
558:
559: /**
560: * @ejb.interface-method
561: * @ejb.transaction type="Supports"
562: */
563: public PageResourceValue[] listPagesAt(URI uri)
564: throws LibresourceWikiException,
565: LibresourceSecurityException, URINotExistException {
566: try {
567: KernelService kernelService = (KernelService) Libresource
568: .getService(KernelConstants.SERVICE);
569: LibresourceResourceValue[] pages = kernelService
570: .listResourcesAt(
571: uri,
572: PageResourceLocal.LIBRESOURCE_RESOURCE_SERVICE,
573: PageResourceLocal.LIBRESOURCE_RESOURCE_TYPE);
574: PageResourceValue[] pageResourceValues = new PageResourceValue[pages.length];
575:
576: for (int i = 0; i < pageResourceValues.length; i++) {
577: LibresourceResourceIdentifier resourceIdentifier = pages[i]
578: .getLibresourceResourceIdentifier();
579: pageResourceValues[i] = ((PageResourceLocal) Libresource
580: .findResource(resourceIdentifier))
581: .getPageResourceValue();
582: pageResourceValues[i].setUri(kernelService
583: .normalizeURI(pages[i].getUri()));
584: }
585:
586: return pageResourceValues;
587: } catch (LibresourceSecurityException se) {
588: throw se;
589: } catch (URINotExistException se) {
590: throw se;
591: } catch (Exception e) {
592: throw new LibresourceWikiException(
593: "Error in listPagesAt : " + e.getMessage(), e);
594: }
595: }
596:
597: // libresource service
598:
599: /**
600: * @ejb.interface-method
601: * @ejb.transaction type="Supports"
602: */
603: public LibresourceIndexableContent getIndexableContent(
604: LibresourceResourceIdentifier resourceIdentifier)
605: throws LibresourceException {
606: try {
607: try {
608: Libresource.checkType(resourceIdentifier,
609: PageResourceLocal.class);
610: } catch (LibresourceException e) {
611: return null;
612: }
613:
614: LibresourceIndexableContent content = new LibresourceIndexableContent();
615: PageResourceLocal pageResourceLocal = (PageResourceLocal) Libresource
616: .findResource(resourceIdentifier);
617: content.addContentPart(pageResourceLocal.getName());
618:
619: if (pageResourceLocal.getContent() != null) {
620: content.addContentPart(pageResourceLocal.getContent()
621: .replaceAll("\\{.*\\}", ""));
622: }
623:
624: return content;
625: } catch (Exception e) {
626: throw new LibresourceException(
627: "error in LibresourceWiki.getIndexableContent("
628: + resourceIdentifier + ")", e);
629: }
630: }
631:
632: /**
633: * @ejb.interface-method
634: * @ejb.transaction type="Supports"
635: */
636: public String[] listAvailablesEvents(
637: LibresourceResourceIdentifier resourceIdentifier)
638: throws LibresourceException {
639: if (resourceIdentifier.getService().equals(
640: WikiConstants.SERVICE)) {
641: if (resourceIdentifier.getResourceType().equals(
642: WikiConstants.RESOURCE)) {
643: return new String[] { WikiConstants.EVENT_PAGE_CREATE,
644: WikiConstants.EVENT_PAGE_EDIT,
645: WikiConstants.EVENT_PAGE_EDIT_CONTENT };
646: }
647: }
648:
649: return super .listAvailablesPermissions(resourceIdentifier);
650: }
651:
652: /**
653: * @ejb.interface-method
654: * @ejb.transaction type="Supports"
655: */
656: public LibresourceExportHandler getXmlExportHandlerForResource(
657: URI uri) throws LibresourceException {
658: try {
659: KernelService kernelService = (KernelService) Libresource
660: .getService(KernelConstants.SERVICE);
661: LibresourceResourceIdentifier resourceIdentifier = kernelService
662: .lookup(uri);
663:
664: try {
665: Libresource.checkType(resourceIdentifier,
666: PageResourceLocal.class);
667:
668: return new PageExportHandler(uri);
669: } catch (Exception e) {
670: //
671: }
672:
673: return super .getXmlExportHandlerForResource(uri);
674: } catch (Exception e) {
675: throw new LibresourceException(
676: "Can't obtain exportHandler for uri " + uri, e);
677: }
678: }
679:
680: /**
681: * @ejb.interface-method
682: * @ejb.transaction type="Supports"
683: */
684: public LibresourceImportHandler getXmlImportHandler(String type)
685: throws LibresourceException {
686: if (type.equals(WikiConstants.RESOURCE)) {
687: return new PageImportHandler();
688: }
689:
690: return super.getXmlImportHandler(type);
691: }
692: }
|