Search in sources :

Example 1 with XObjectDeletedEvent

use of com.xpn.xwiki.internal.event.XObjectDeletedEvent in project xwiki-platform by xwiki.

the class AnnotationEventGeneratorEventListener method onEvent.

@Override
public void onEvent(Event event, Object source, Object data) {
    // Don`t rely on the context from the data parameter.
    XWikiContext context = (XWikiContext) execution.getContext().getProperty("xwikicontext");
    String currentWiki = context.getWikiId();
    try {
        XWikiDocument document = (XWikiDocument) source;
        String wikiOfAffectedDocument = document.getDocumentReference().getWikiReference().getName();
        // Always work on the wiki of the source document. The Annotation Application's configuration looks at the
        // context to provide values for the current wiki. Objects could be modified cross-wiki and the context
        // database might not be right.
        context.setWikiId(wikiOfAffectedDocument);
        // Only work if the Annotations Application is installed on the wiki.
        if (!annotationConfiguration.isInstalled()) {
            return;
        }
        // Extract the BaseObjectReference to be able to inspect the XClassReference.
        BaseObjectReference objectReference = getBaseObjectReference((XObjectEvent) event);
        DocumentReference objectClassReference = objectReference.getXClassReference();
        // Only interested in objects that are of the same class as the currently configured annotation class.
        if (!objectClassReference.equals(annotationConfiguration.getAnnotationClassReference())) {
            return;
        }
        // The object is needed for the final check. See below.
        BaseObject object = document.getXObject(objectReference);
        // Build the new event to launch using the current document reference and object number.
        Event newEvent = null;
        String documentReference = defaultEntityReferenceSerializer.serialize(document.getDocumentReference());
        String number = String.valueOf(objectReference.getObjectNumber());
        if (event instanceof XObjectAddedEvent) {
            newEvent = new AnnotationAddedEvent(documentReference, number);
        } else if (event instanceof XObjectUpdatedEvent) {
            newEvent = new AnnotationUpdatedEvent(documentReference, number);
        } else if (event instanceof XObjectDeletedEvent) {
            // Current document might be deleted. Always use the original document for *Deleted events.
            object = document.getOriginalDocument().getXObject(objectReference);
            newEvent = new AnnotationDeletedEvent(documentReference, number);
        }
        // Handle specially the default annotations class which coincides with the default comments class. We need
        // to avoid mistaking comments for annotations.
        DocumentReference defaultCommentsClassReference = context.getWiki().getCommentsClass(context).getDocumentReference();
        if (defaultCommentsClassReference.equals(object.getXClassReference())) {
            // A comment is considered an annotation when it has a text selection.
            String selection = object.getStringValue(Annotation.SELECTION_FIELD);
            if (selection == null || selection.trim().length() == 0) {
                // This is a simple comment. Skip it.
                return;
            }
        }
        // Launch the new event.
        observationManager.get().notify(newEvent, source, context);
    } catch (Exception e) {
        logger.error("Failed to handle event of type [{}]", event.getClass().getName(), e);
    } finally {
        // Restore the context database.
        context.setWikiId(currentWiki);
    }
}
Also used : AnnotationUpdatedEvent(org.xwiki.annotation.event.AnnotationUpdatedEvent) XObjectAddedEvent(com.xpn.xwiki.internal.event.XObjectAddedEvent) XWikiContext(com.xpn.xwiki.XWikiContext) BaseObject(com.xpn.xwiki.objects.BaseObject) XObjectUpdatedEvent(com.xpn.xwiki.internal.event.XObjectUpdatedEvent) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) BaseObjectReference(com.xpn.xwiki.objects.BaseObjectReference) AnnotationAddedEvent(org.xwiki.annotation.event.AnnotationAddedEvent) AnnotationAddedEvent(org.xwiki.annotation.event.AnnotationAddedEvent) XObjectEvent(com.xpn.xwiki.internal.event.XObjectEvent) AnnotationUpdatedEvent(org.xwiki.annotation.event.AnnotationUpdatedEvent) XObjectUpdatedEvent(com.xpn.xwiki.internal.event.XObjectUpdatedEvent) XObjectAddedEvent(com.xpn.xwiki.internal.event.XObjectAddedEvent) XObjectDeletedEvent(com.xpn.xwiki.internal.event.XObjectDeletedEvent) AnnotationDeletedEvent(org.xwiki.annotation.event.AnnotationDeletedEvent) Event(org.xwiki.observation.event.Event) AnnotationDeletedEvent(org.xwiki.annotation.event.AnnotationDeletedEvent) XObjectDeletedEvent(com.xpn.xwiki.internal.event.XObjectDeletedEvent) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 2 with XObjectDeletedEvent

use of com.xpn.xwiki.internal.event.XObjectDeletedEvent 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)

Aggregations

XWikiContext (com.xpn.xwiki.XWikiContext)2 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)2 XObjectAddedEvent (com.xpn.xwiki.internal.event.XObjectAddedEvent)2 XObjectDeletedEvent (com.xpn.xwiki.internal.event.XObjectDeletedEvent)2 XObjectUpdatedEvent (com.xpn.xwiki.internal.event.XObjectUpdatedEvent)2 DocumentReference (org.xwiki.model.reference.DocumentReference)2 XWikiException (com.xpn.xwiki.XWikiException)1 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)1 AbstractAttachmentEvent (com.xpn.xwiki.internal.event.AbstractAttachmentEvent)1 AttachmentAddedEvent (com.xpn.xwiki.internal.event.AttachmentAddedEvent)1 AttachmentDeletedEvent (com.xpn.xwiki.internal.event.AttachmentDeletedEvent)1 AttachmentUpdatedEvent (com.xpn.xwiki.internal.event.AttachmentUpdatedEvent)1 EntityEvent (com.xpn.xwiki.internal.event.EntityEvent)1 XObjectEvent (com.xpn.xwiki.internal.event.XObjectEvent)1 XObjectPropertyAddedEvent (com.xpn.xwiki.internal.event.XObjectPropertyAddedEvent)1 XObjectPropertyDeletedEvent (com.xpn.xwiki.internal.event.XObjectPropertyDeletedEvent)1 XObjectPropertyUpdatedEvent (com.xpn.xwiki.internal.event.XObjectPropertyUpdatedEvent)1 BaseObject (com.xpn.xwiki.objects.BaseObject)1 BaseObjectReference (com.xpn.xwiki.objects.BaseObjectReference)1 AnnotationAddedEvent (org.xwiki.annotation.event.AnnotationAddedEvent)1