Search in sources :

Example 16 with DocumentDeletedEvent

use of org.xwiki.bridge.event.DocumentDeletedEvent 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 17 with DocumentDeletedEvent

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

the class SolrIndexEventListener method onEvent.

@Override
public void onEvent(Event event, Object source, Object data) {
    try {
        if (event instanceof DocumentUpdatedEvent) {
            XWikiDocument document = (XWikiDocument) source;
            if (Locale.ROOT.equals(document.getLocale())) {
                // Index all the translations of a document when its default translation has been updated because
                // the default translation holds meta data shared by all translations (attachments, objects).
                indexTranslations(document, (XWikiContext) data);
            } else {
                // Index only the updated translation.
                this.solrIndexer.get().index(document.getDocumentReferenceWithLocale(), false);
            }
        } else if (event instanceof DocumentCreatedEvent) {
            XWikiDocument document = (XWikiDocument) source;
            if (!Locale.ROOT.equals(document.getLocale())) {
                // If a new translation is added to a document reindex the whole document (could be optimized a bit
                // by reindexing only the parent locales but that would always include objects and attachments
                // anyway)
                indexTranslations(document, (XWikiContext) data);
            } else {
                this.solrIndexer.get().index(document.getDocumentReferenceWithLocale(), false);
            }
        } else if (event instanceof DocumentDeletedEvent) {
            XWikiDocument document = ((XWikiDocument) source).getOriginalDocument();
            // We must pass the document reference with the REAL locale because when the indexer is going to delete
            // the document from the Solr index (later, on a different thread) the real locale won't be accessible
            // anymore since the XWiki document has been already deleted from the database. The real locale (taken
            // from the XWiki document) is used to compute the id of the Solr document when the document reference
            // locale is ROOT (i.e. for default document translations).
            // Otherwise the document won't be deleted from the Solr index (because the computed id won't match any
            // document from the Solr index) and we're going to have deleted documents that are still in the Solr
            // index. These documents will be filtered from the search results but not from the facet counts.
            // See XWIKI-10003: Cache problem with Solr facet filter results count
            this.solrIndexer.get().delete(new DocumentReference(document.getDocumentReference(), document.getRealLocale()), false);
        } else if (event instanceof AttachmentUpdatedEvent || event instanceof AttachmentAddedEvent) {
            XWikiDocument document = (XWikiDocument) source;
            String fileName = ((AbstractAttachmentEvent) event).getName();
            XWikiAttachment attachment = document.getAttachment(fileName);
            this.solrIndexer.get().index(attachment.getReference(), false);
        } else if (event instanceof AttachmentDeletedEvent) {
            XWikiDocument document = ((XWikiDocument) source).getOriginalDocument();
            String fileName = ((AbstractAttachmentEvent) event).getName();
            XWikiAttachment attachment = document.getAttachment(fileName);
            this.solrIndexer.get().delete(attachment.getReference(), false);
        } else if (event instanceof XObjectUpdatedEvent || event instanceof XObjectAddedEvent) {
            EntityEvent entityEvent = (EntityEvent) event;
            this.solrIndexer.get().index(entityEvent.getReference(), false);
        } else if (event instanceof XObjectDeletedEvent) {
            EntityEvent entityEvent = (EntityEvent) event;
            this.solrIndexer.get().delete(entityEvent.getReference(), false);
        } else if (event instanceof XObjectPropertyUpdatedEvent || event instanceof XObjectPropertyAddedEvent) {
            EntityEvent entityEvent = (EntityEvent) event;
            this.solrIndexer.get().index(entityEvent.getReference(), false);
        } else if (event instanceof XObjectPropertyDeletedEvent) {
            EntityEvent entityEvent = (EntityEvent) event;
            this.solrIndexer.get().delete(entityEvent.getReference(), false);
        } else if (event instanceof WikiDeletedEvent) {
            String wikiName = (String) source;
            WikiReference wikiReference = new WikiReference(wikiName);
            this.solrIndexer.get().delete(wikiReference, false);
        }
    } catch (Exception e) {
        this.logger.error("Failed to handle event [{}] with source [{}]", event, source, e);
    }
}
Also used : XObjectPropertyUpdatedEvent(com.xpn.xwiki.internal.event.XObjectPropertyUpdatedEvent) AttachmentAddedEvent(com.xpn.xwiki.internal.event.AttachmentAddedEvent) XObjectAddedEvent(com.xpn.xwiki.internal.event.XObjectAddedEvent) XObjectPropertyDeletedEvent(com.xpn.xwiki.internal.event.XObjectPropertyDeletedEvent) DocumentCreatedEvent(org.xwiki.bridge.event.DocumentCreatedEvent) XWikiContext(com.xpn.xwiki.XWikiContext) DocumentUpdatedEvent(org.xwiki.bridge.event.DocumentUpdatedEvent) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) XWikiException(com.xpn.xwiki.XWikiException) XObjectUpdatedEvent(com.xpn.xwiki.internal.event.XObjectUpdatedEvent) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) DocumentDeletedEvent(org.xwiki.bridge.event.DocumentDeletedEvent) XObjectPropertyAddedEvent(com.xpn.xwiki.internal.event.XObjectPropertyAddedEvent) EntityEvent(com.xpn.xwiki.internal.event.EntityEvent) AttachmentUpdatedEvent(com.xpn.xwiki.internal.event.AttachmentUpdatedEvent) AbstractAttachmentEvent(com.xpn.xwiki.internal.event.AbstractAttachmentEvent) XObjectDeletedEvent(com.xpn.xwiki.internal.event.XObjectDeletedEvent) AttachmentDeletedEvent(com.xpn.xwiki.internal.event.AttachmentDeletedEvent) WikiDeletedEvent(org.xwiki.bridge.event.WikiDeletedEvent) WikiReference(org.xwiki.model.reference.WikiReference) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 18 with DocumentDeletedEvent

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

the class SolrIndexEventListenerTest method onDocumentDeleted.

@Test
public void onDocumentDeleted() throws Exception {
    DocumentReference documentReference = new DocumentReference("aWiki", "aSpace", "aPage");
    XWikiDocument document = mock(XWikiDocument.class);
    when(document.getOriginalDocument()).thenReturn(document);
    when(document.getDocumentReference()).thenReturn(documentReference);
    when(document.getRealLocale()).thenReturn(Locale.FRENCH);
    mocker.getComponentUnderTest().onEvent(new DocumentDeletedEvent(), document, null);
    verify(indexer).delete(new DocumentReference(documentReference, Locale.FRENCH), false);
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) DocumentDeletedEvent(org.xwiki.bridge.event.DocumentDeletedEvent) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 19 with DocumentDeletedEvent

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

the class WikiMacroEventListener method onEvent.

@Override
public void onEvent(Event event, Object source, Object data) {
    if (event instanceof AbstractDocumentEvent) {
        DocumentModelBridge document = (DocumentModelBridge) source;
        DocumentReference documentReference = document.getDocumentReference();
        if (event instanceof DocumentCreatedEvent || event instanceof DocumentUpdatedEvent) {
            registerMacro(documentReference);
        } else if (event instanceof DocumentDeletedEvent) {
            unregisterMacro(documentReference);
        }
    }
}
Also used : DocumentDeletedEvent(org.xwiki.bridge.event.DocumentDeletedEvent) DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) AbstractDocumentEvent(org.xwiki.bridge.event.AbstractDocumentEvent) DocumentCreatedEvent(org.xwiki.bridge.event.DocumentCreatedEvent) DocumentUpdatedEvent(org.xwiki.bridge.event.DocumentUpdatedEvent) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 20 with DocumentDeletedEvent

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

the class RealtimeNotificationGenerator method getWatchListEvent.

/**
 * @param event the current event
 * @param currentDoc the affected document
 * @param context the context of the event
 * @return the {@link WatchListEvent} to use to notify watchers of the current document
 */
private WatchListEvent getWatchListEvent(Event event, XWikiDocument currentDoc, XWikiContext context) {
    String type = null;
    DocumentReference documentReference = currentDoc.getDocumentReference();
    DocumentReference userReference = context.getUserReference();
    String version = currentDoc.getVersion();
    Date date = currentDoc.getDate();
    if (event instanceof DocumentCreatedEvent) {
        type = WatchListEventType.CREATE;
    } else if (event instanceof DocumentUpdatedEvent) {
        type = WatchListEventType.UPDATE;
    } else if (event instanceof DocumentDeletedEvent) {
        version = currentDoc.getOriginalDocument().getVersion();
        type = WatchListEventType.DELETE;
    }
    WatchListEvent watchListEvent = new WatchListEvent(documentReference, type, userReference, version, date);
    return watchListEvent;
}
Also used : DocumentDeletedEvent(org.xwiki.bridge.event.DocumentDeletedEvent) WatchListEvent(org.xwiki.watchlist.internal.api.WatchListEvent) DocumentCreatedEvent(org.xwiki.bridge.event.DocumentCreatedEvent) DocumentUpdatedEvent(org.xwiki.bridge.event.DocumentUpdatedEvent) DocumentReference(org.xwiki.model.reference.DocumentReference) Date(java.util.Date)

Aggregations

DocumentDeletedEvent (org.xwiki.bridge.event.DocumentDeletedEvent)28 DocumentUpdatedEvent (org.xwiki.bridge.event.DocumentUpdatedEvent)19 DocumentCreatedEvent (org.xwiki.bridge.event.DocumentCreatedEvent)17 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)14 Test (org.junit.Test)12 DocumentReference (org.xwiki.model.reference.DocumentReference)12 XWikiContext (com.xpn.xwiki.XWikiContext)8 Event (org.xwiki.observation.event.Event)6 ArrayList (java.util.ArrayList)5 DocumentDeletingEvent (org.xwiki.bridge.event.DocumentDeletingEvent)5 BaseObject (com.xpn.xwiki.objects.BaseObject)3 Date (java.util.Date)3 ObservationManager (org.xwiki.observation.ObservationManager)3 XWikiException (com.xpn.xwiki.XWikiException)2 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)2 AttachmentAddedEvent (com.xpn.xwiki.internal.event.AttachmentAddedEvent)2 AttachmentDeletedEvent (com.xpn.xwiki.internal.event.AttachmentDeletedEvent)2 AttachmentUpdatedEvent (com.xpn.xwiki.internal.event.AttachmentUpdatedEvent)2 List (java.util.List)2 DocumentModelBridge (org.xwiki.bridge.DocumentModelBridge)2