Search in sources :

Example 1 with DocumentDeletingEvent

use of org.xwiki.bridge.event.DocumentDeletingEvent in project xwiki-platform by xwiki.

the class LegacyNotificationDispatcher method onEvent.

@Override
public void onEvent(Event event, Object source, Object data) {
    XWikiDocument document = (XWikiDocument) source;
    XWikiDocument originalDocument = document.getOriginalDocument();
    XWikiContext context = (XWikiContext) data;
    XWikiNotificationManager manager = getNotificationManager((XWikiContext) data);
    if (manager != null) {
        if (event instanceof DocumentCreatedEvent) {
            manager.verify(document, originalDocument, XWikiDocChangeNotificationInterface.EVENT_NEW, context);
        } else if (event instanceof DocumentUpdatedEvent) {
            manager.verify(document, originalDocument, XWikiDocChangeNotificationInterface.EVENT_CHANGE, context);
        } else if (event instanceof DocumentCreatingEvent) {
            manager.preverify(document, originalDocument, XWikiDocChangeNotificationInterface.EVENT_NEW, context);
        } else if (event instanceof DocumentUpdatingEvent) {
            manager.preverify(document, originalDocument, XWikiDocChangeNotificationInterface.EVENT_CHANGE, context);
        } else if (event instanceof DocumentDeletedEvent) {
            manager.verify(new XWikiDocument(document.getDocumentReference()), document, XWikiDocChangeNotificationInterface.EVENT_DELETE, context);
        } else if (event instanceof DocumentDeletingEvent) {
            manager.preverify(document, new XWikiDocument(document.getDocumentReference()), XWikiDocChangeNotificationInterface.EVENT_DELETE, context);
        } else if (event instanceof ActionExecutedEvent) {
            manager.verify(document, ((ActionExecutedEvent) event).getActionName(), context);
        } else if (event instanceof ActionExecutingEvent) {
            manager.preverify(document, ((ActionExecutingEvent) event).getActionName(), context);
        }
    } else {
        this.logger.error("Can't find old [XWikiNotificationManager] system");
    }
}
Also used : DocumentUpdatingEvent(org.xwiki.bridge.event.DocumentUpdatingEvent) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) DocumentDeletedEvent(org.xwiki.bridge.event.DocumentDeletedEvent) DocumentCreatedEvent(org.xwiki.bridge.event.DocumentCreatedEvent) DocumentDeletingEvent(org.xwiki.bridge.event.DocumentDeletingEvent) ActionExecutedEvent(org.xwiki.bridge.event.ActionExecutedEvent) XWikiContext(com.xpn.xwiki.XWikiContext) XWikiNotificationManager(com.xpn.xwiki.notify.XWikiNotificationManager) DocumentUpdatedEvent(org.xwiki.bridge.event.DocumentUpdatedEvent) ActionExecutingEvent(org.xwiki.bridge.event.ActionExecutingEvent) DocumentCreatingEvent(org.xwiki.bridge.event.DocumentCreatingEvent)

Example 2 with DocumentDeletingEvent

use of org.xwiki.bridge.event.DocumentDeletingEvent in project xwiki-platform by xwiki.

the class XWikiMockitoTest method deleteDocument.

@Test
public void deleteDocument() throws Exception {
    final DocumentReference documentReference = new DocumentReference("wiki", "Space", "Page");
    XWikiDocument document = mock(XWikiDocument.class);
    when(document.getDocumentReference()).thenReturn(documentReference);
    final XWikiDocument originalDocument = mock(XWikiDocument.class);
    when(document.getOriginalDocument()).thenReturn(originalDocument);
    this.xwiki.deleteDocument(document, this.context);
    ObservationManager observation = this.mocker.getInstance(ObservationManager.class);
    ArgumentMatcher<XWikiDocument> matcher = new ArgumentMatcher<XWikiDocument>() {

        @Override
        public boolean matches(XWikiDocument argument) {
            return argument.getDocumentReference().equals(documentReference) && argument.getOriginalDocument() == originalDocument;
        }
    };
    // Make sure the right events have been sent
    verify(observation).notify(eq(new DocumentDeletingEvent(documentReference)), argThat(matcher), same(this.context));
    verify(observation).notify(eq(new DocumentDeletedEvent(documentReference)), argThat(matcher), same(this.context));
    verifyNoMoreInteractions(observation);
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) DocumentDeletedEvent(org.xwiki.bridge.event.DocumentDeletedEvent) ArgumentMatcher(org.mockito.ArgumentMatcher) DocumentDeletingEvent(org.xwiki.bridge.event.DocumentDeletingEvent) ObservationManager(org.xwiki.observation.ObservationManager) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 3 with DocumentDeletingEvent

use of org.xwiki.bridge.event.DocumentDeletingEvent in project xwiki-platform by xwiki.

the class LegacyNotificationDispatcher method getEvents.

@Override
public List<Event> getEvents() {
    return new ArrayList<Event>() {

        {
            add(new DocumentDeletedEvent());
            add(new DocumentCreatedEvent());
            add(new DocumentUpdatedEvent());
            add(new DocumentDeletingEvent());
            add(new DocumentCreatingEvent());
            add(new DocumentUpdatingEvent());
            add(new ActionExecutedEvent());
            add(new ActionExecutingEvent());
        }
    };
}
Also used : DocumentUpdatingEvent(org.xwiki.bridge.event.DocumentUpdatingEvent) DocumentDeletedEvent(org.xwiki.bridge.event.DocumentDeletedEvent) ArrayList(java.util.ArrayList) DocumentCreatedEvent(org.xwiki.bridge.event.DocumentCreatedEvent) DocumentDeletingEvent(org.xwiki.bridge.event.DocumentDeletingEvent) ActionExecutedEvent(org.xwiki.bridge.event.ActionExecutedEvent) DocumentUpdatedEvent(org.xwiki.bridge.event.DocumentUpdatedEvent) ActionExecutingEvent(org.xwiki.bridge.event.ActionExecutingEvent) DocumentCreatingEvent(org.xwiki.bridge.event.DocumentCreatingEvent)

Example 4 with DocumentDeletingEvent

use of org.xwiki.bridge.event.DocumentDeletingEvent in project xwiki-platform by xwiki.

the class XWikiTest method testDeleteDocumentSendsObservationEvents.

/**
 * We only verify here that the deleteDocument API calls the Observation component.
 */
public void testDeleteDocumentSendsObservationEvents() throws Exception {
    Mock mockListener = mock(EventListener.class);
    mockListener.stubs().method("getName").will(returnValue("testlistener"));
    DocumentReference ref = new DocumentReference("xwikitest", "Another", "Document");
    mockListener.expects(once()).method("getEvents").will(returnValue(Arrays.asList(new DocumentDeletedEvent(ref), new DocumentDeletingEvent(ref))));
    ObservationManager om = getComponentManager().getInstance(ObservationManager.class);
    om.addListener((EventListener) mockListener.proxy());
    XWikiDocument document = new XWikiDocument(new DocumentReference("xwikitest", "Another", "Document"));
    document.setContent("the content");
    // Not expectation on mock Listener since we're not subscribed to Document save events
    this.xwiki.saveDocument(document, getContext());
    // Ensure that the onEvent method has been called before and after the deletion
    mockListener.expects(once()).method("onEvent").with(isA(DocumentDeletingEvent.class), isA(XWikiDocument.class), isA(XWikiContext.class));
    mockListener.expects(once()).method("onEvent").with(isA(DocumentDeletedEvent.class), isA(XWikiDocument.class), isA(XWikiContext.class));
    this.xwiki.deleteDocument(document, false, getContext());
}
Also used : DocumentDeletedEvent(org.xwiki.bridge.event.DocumentDeletedEvent) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) DocumentDeletingEvent(org.xwiki.bridge.event.DocumentDeletingEvent) ObservationManager(org.xwiki.observation.ObservationManager) Mock(org.jmock.Mock) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 5 with DocumentDeletingEvent

use of org.xwiki.bridge.event.DocumentDeletingEvent in project xwiki-platform by xwiki.

the class XWiki method deleteDocument.

public void deleteDocument(XWikiDocument doc, boolean totrash, XWikiContext context) throws XWikiException {
    String currentWiki = null;
    currentWiki = context.getWikiId();
    try {
        context.setWikiId(doc.getDocumentReference().getWikiReference().getName());
        // The source document is a new empty XWikiDocument to follow
        // DocumentUpdatedEvent policy: source document in new document and the old version is available using
        // doc.getOriginalDocument()
        XWikiDocument blankDoc = new XWikiDocument(doc.getDocumentReference());
        // Again to follow general event policy, new document author is the user who modified the document
        // (here the modification is delete)
        blankDoc.setOriginalDocument(doc.getOriginalDocument());
        blankDoc.setAuthorReference(context.getUserReference());
        blankDoc.setContentAuthorReference(context.getUserReference());
        ObservationManager om = getObservationManager();
        // an XWikiDocument as source and an XWikiContext as data.
        if (om != null) {
            om.notify(new DocumentDeletingEvent(doc.getDocumentReference()), blankDoc, context);
        }
        if (hasRecycleBin(context) && totrash) {
            // Extract any existing batchId from the context.
            String batchId = Utils.getComponent(BatchOperationExecutor.class).getCurrentBatchId();
            // Save to recycle bin together with any determined batch ID.
            getRecycleBinStore().saveToRecycleBin(doc, context.getUser(), new Date(), batchId, context, true);
        }
        getStore().deleteXWikiDoc(doc, context);
        try {
            // an XWikiDocument as source and an XWikiContext as data.
            if (om != null) {
                om.notify(new DocumentDeletedEvent(doc.getDocumentReference()), blankDoc, context);
            }
        } catch (Exception ex) {
            LOGGER.error("Failed to send document delete notifications for document [{}]", doc.getDocumentReference(), ex);
        }
    } finally {
        context.setWikiId(currentWiki);
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) DocumentDeletedEvent(org.xwiki.bridge.event.DocumentDeletedEvent) DocumentDeletingEvent(org.xwiki.bridge.event.DocumentDeletingEvent) ObservationManager(org.xwiki.observation.ObservationManager) ParseGroovyFromString(com.xpn.xwiki.internal.render.groovy.ParseGroovyFromString) IncludeServletAsString(com.xpn.xwiki.web.includeservletasstring.IncludeServletAsString) Date(java.util.Date) WikiManagerException(org.xwiki.wiki.manager.WikiManagerException) IOException(java.io.IOException) JobException(org.xwiki.job.JobException) ParseException(org.xwiki.rendering.parser.ParseException) QueryException(org.xwiki.query.QueryException) URIException(org.apache.commons.httpclient.URIException) InvocationTargetException(java.lang.reflect.InvocationTargetException) HibernateException(org.hibernate.HibernateException) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) NamingException(javax.naming.NamingException) FileNotFoundException(java.io.FileNotFoundException) MalformedURLException(java.net.MalformedURLException) BatchOperationExecutor(org.xwiki.refactoring.batch.BatchOperationExecutor)

Aggregations

DocumentDeletedEvent (org.xwiki.bridge.event.DocumentDeletedEvent)5 DocumentDeletingEvent (org.xwiki.bridge.event.DocumentDeletingEvent)5 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)4 ObservationManager (org.xwiki.observation.ObservationManager)3 ActionExecutedEvent (org.xwiki.bridge.event.ActionExecutedEvent)2 ActionExecutingEvent (org.xwiki.bridge.event.ActionExecutingEvent)2 DocumentCreatedEvent (org.xwiki.bridge.event.DocumentCreatedEvent)2 DocumentCreatingEvent (org.xwiki.bridge.event.DocumentCreatingEvent)2 DocumentUpdatedEvent (org.xwiki.bridge.event.DocumentUpdatedEvent)2 DocumentUpdatingEvent (org.xwiki.bridge.event.DocumentUpdatingEvent)2 DocumentReference (org.xwiki.model.reference.DocumentReference)2 XWikiContext (com.xpn.xwiki.XWikiContext)1 ParseGroovyFromString (com.xpn.xwiki.internal.render.groovy.ParseGroovyFromString)1 XWikiNotificationManager (com.xpn.xwiki.notify.XWikiNotificationManager)1 IncludeServletAsString (com.xpn.xwiki.web.includeservletasstring.IncludeServletAsString)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 MalformedURLException (java.net.MalformedURLException)1 ArrayList (java.util.ArrayList)1