Search in sources :

Example 26 with DocumentUpdatedEvent

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

the class XWikiMockitoTest method rollbackFiresEvents.

/**
 * Verify that {@link XWiki#rollback(XWikiDocument, String, XWikiContext)} fires the right events.
 */
@Test
public void rollbackFiresEvents() throws Exception {
    ObservationManager observationManager = mocker.getInstance(ObservationManager.class);
    DocumentReference documentReference = new DocumentReference("wiki", "Space", "Page");
    XWikiDocument document = mock(XWikiDocument.class);
    when(document.getDocumentReference()).thenReturn(documentReference);
    XWikiDocument originalDocument = mock(XWikiDocument.class);
    // Mark the document as existing so that the roll-back method will fire an update event.
    when(originalDocument.isNew()).thenReturn(false);
    XWikiDocument result = mock(XWikiDocument.class);
    when(result.clone()).thenReturn(result);
    when(result.getDocumentReference()).thenReturn(documentReference);
    when(result.getOriginalDocument()).thenReturn(originalDocument);
    String revision = "3.5";
    when(this.documentRevisionProvider.getRevision(document, revision)).thenReturn(result);
    this.mocker.registerMockComponent(ContextualLocalizationManager.class);
    xwiki.rollback(document, revision, context);
    verify(observationManager).notify(new DocumentRollingBackEvent(documentReference, revision), result, context);
    verify(observationManager).notify(new DocumentUpdatingEvent(documentReference), result, context);
    verify(observationManager).notify(new DocumentUpdatedEvent(documentReference), result, context);
    verify(observationManager).notify(new DocumentRolledBackEvent(documentReference, revision), result, context);
}
Also used : DocumentUpdatingEvent(org.xwiki.bridge.event.DocumentUpdatingEvent) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) DocumentRollingBackEvent(org.xwiki.bridge.event.DocumentRollingBackEvent) DocumentRolledBackEvent(org.xwiki.bridge.event.DocumentRolledBackEvent) ObservationManager(org.xwiki.observation.ObservationManager) DocumentUpdatedEvent(org.xwiki.bridge.event.DocumentUpdatedEvent) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 27 with DocumentUpdatedEvent

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

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

the class SolrIndexEventListenerTest method onDocumentDefaultTranslationUpdated.

@Test
public void onDocumentDefaultTranslationUpdated() throws Exception {
    XWikiContext xcontext = mock(XWikiContext.class);
    XWikiDocument document = mock(XWikiDocument.class);
    when(document.getLocale()).thenReturn(Locale.ROOT);
    when(document.getTranslationLocales(xcontext)).thenReturn(Arrays.asList(Locale.FRENCH, Locale.GERMAN));
    DocumentReference documentReference = new DocumentReference("wiki", "Path", "Page");
    when(document.getDocumentReference()).thenReturn(documentReference);
    this.mocker.getComponentUnderTest().onEvent(new DocumentUpdatedEvent(), document, xcontext);
    verify(this.indexer, times(3)).index(any(EntityReference.class), any(Boolean.class));
    verify(this.indexer).index(documentReference, false);
    verify(this.indexer).index(new DocumentReference(documentReference, Locale.FRENCH), false);
    verify(this.indexer).index(new DocumentReference(documentReference, Locale.GERMAN), false);
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) EntityReference(org.xwiki.model.reference.EntityReference) XWikiContext(com.xpn.xwiki.XWikiContext) DocumentUpdatedEvent(org.xwiki.bridge.event.DocumentUpdatedEvent) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 29 with DocumentUpdatedEvent

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

the class XClassPropertyEventGeneratorListenerTest method testModifiedDocumentXClassPropertyAdded.

@Test
public void testModifiedDocumentXClassPropertyAdded() throws ComponentLookupException {
    this.xclass.addTextField("property", "Property", 30);
    final Event event = new XClassPropertyAddedEvent(this.xclass.getField("property").getReference());
    this.mocker.getComponentUnderTest().onEvent(new DocumentUpdatedEvent(this.document.getDocumentReference()), this.document, this.oldcore.getXWikiContext());
    // Make sure the listener generated a xobject added event
    verify(this.mockObservation).notify(eq(event), same(this.document), same(this.oldcore.getXWikiContext()));
}
Also used : DocumentCreatedEvent(org.xwiki.bridge.event.DocumentCreatedEvent) Event(org.xwiki.observation.event.Event) DocumentUpdatedEvent(org.xwiki.bridge.event.DocumentUpdatedEvent) DocumentDeletedEvent(org.xwiki.bridge.event.DocumentDeletedEvent) DocumentUpdatedEvent(org.xwiki.bridge.event.DocumentUpdatedEvent) Test(org.junit.Test)

Example 30 with DocumentUpdatedEvent

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

the class XClassPropertyEventGeneratorListenerTest method testModifiedDocumentXClassPropertyModified.

@Test
public void testModifiedDocumentXClassPropertyModified() throws ComponentLookupException {
    this.xclassOrigin.addTextField("property", "Property", 30);
    this.xclass.addTextField("property", "New Property", 30);
    final Event event = new XClassPropertyUpdatedEvent(this.xclassOrigin.getField("property").getReference());
    this.mocker.getComponentUnderTest().onEvent(new DocumentUpdatedEvent(this.document.getDocumentReference()), this.document, this.oldcore.getXWikiContext());
    // Make sure the listener generated a xobject added event
    verify(this.mockObservation).notify(eq(event), same(this.document), same(this.oldcore.getXWikiContext()));
}
Also used : DocumentCreatedEvent(org.xwiki.bridge.event.DocumentCreatedEvent) Event(org.xwiki.observation.event.Event) DocumentUpdatedEvent(org.xwiki.bridge.event.DocumentUpdatedEvent) DocumentDeletedEvent(org.xwiki.bridge.event.DocumentDeletedEvent) DocumentUpdatedEvent(org.xwiki.bridge.event.DocumentUpdatedEvent) Test(org.junit.Test)

Aggregations

DocumentUpdatedEvent (org.xwiki.bridge.event.DocumentUpdatedEvent)45 Test (org.junit.Test)30 DocumentDeletedEvent (org.xwiki.bridge.event.DocumentDeletedEvent)26 DocumentCreatedEvent (org.xwiki.bridge.event.DocumentCreatedEvent)25 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)16 DocumentReference (org.xwiki.model.reference.DocumentReference)13 Event (org.xwiki.observation.event.Event)13 XWikiContext (com.xpn.xwiki.XWikiContext)9 ArrayList (java.util.ArrayList)6 UntypedRecordableEventDescriptor (org.xwiki.eventstream.UntypedRecordableEventDescriptor)5 BaseObject (com.xpn.xwiki.objects.BaseObject)4 ObservationManager (org.xwiki.observation.ObservationManager)4 DocumentUpdatingEvent (org.xwiki.bridge.event.DocumentUpdatingEvent)3 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 Date (java.util.Date)2 DocumentModelBridge (org.xwiki.bridge.DocumentModelBridge)2 ActionExecutedEvent (org.xwiki.bridge.event.ActionExecutedEvent)2