Search in sources :

Example 1 with AnnotationAddedEvent

use of org.xwiki.annotation.event.AnnotationAddedEvent 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 AnnotationAddedEvent

use of org.xwiki.annotation.event.AnnotationAddedEvent in project xwiki-platform by xwiki.

the class ActivityStreamImpl method onEvent.

@Override
public void onEvent(Event event, Object source, Object data) {
    // Do not record some ignored events
    ObservationContext observationContext = Utils.getComponent(ObservationContext.class);
    if (observationContext.isIn(IGNORED_EVENTS)) {
        return;
    }
    XWikiDocument currentDoc = (XWikiDocument) source;
    XWikiDocument originalDoc = currentDoc.getOriginalDocument();
    XWikiContext context = (XWikiContext) data;
    String wiki = context.getWikiId();
    String msgPrefix = "activitystream.event.";
    String streamName = getStreamName(currentDoc.getSpace(), context);
    // If we haven't found a stream to store the event or if both currentDoc and originalDoc are null: exit
    if (streamName == null) {
        return;
    }
    Execution executionContext = Utils.getComponent(Execution.class);
    // Take events into account only once in a cluster
    if (!Utils.getComponent(RemoteObservationManagerContext.class).isRemoteState() && !executionContext.getContext().hasProperty(AbstractEventStreamEvent.EVENT_LOOP_CONTEXT_LOCK_PROPERTY)) {
        executionContext.getContext().setProperty(AbstractEventStreamEvent.EVENT_LOOP_CONTEXT_LOCK_PROPERTY, true);
        String eventType;
        String displayTitle;
        String additionalIdentifier = null;
        if (event instanceof DocumentCreatedEvent) {
            eventType = ActivityEventType.CREATE;
            displayTitle = currentDoc.getRenderedTitle(context);
        } else if (event instanceof DocumentUpdatedEvent) {
            eventType = ActivityEventType.UPDATE;
            displayTitle = originalDoc.getRenderedTitle(context);
        } else if (event instanceof DocumentDeletedEvent) {
            eventType = ActivityEventType.DELETE;
            displayTitle = originalDoc.getRenderedTitle(context);
            // When we receive a DELETE event, the given document is blank and does not have version & hidden tag
            // properly set.
            currentDoc.setVersion(originalDoc.getVersion());
            currentDoc.setHidden(originalDoc.isHidden());
        } else if (event instanceof CommentAddedEvent) {
            eventType = ActivityEventType.ADD_COMMENT;
            displayTitle = currentDoc.getRenderedTitle(context);
            additionalIdentifier = ((CommentAddedEvent) event).getIdentifier();
        } else if (event instanceof CommentDeletedEvent) {
            eventType = ActivityEventType.DELETE_COMMENT;
            displayTitle = currentDoc.getRenderedTitle(context);
            additionalIdentifier = ((CommentDeletedEvent) event).getIdentifier();
        } else if (event instanceof CommentUpdatedEvent) {
            eventType = ActivityEventType.UPDATE_COMMENT;
            displayTitle = currentDoc.getRenderedTitle(context);
            additionalIdentifier = ((CommentUpdatedEvent) event).getIdentifier();
        } else if (event instanceof AttachmentAddedEvent) {
            eventType = ActivityEventType.ADD_ATTACHMENT;
            displayTitle = currentDoc.getRenderedTitle(context);
            additionalIdentifier = ((AttachmentAddedEvent) event).getName();
        } else if (event instanceof AttachmentDeletedEvent) {
            eventType = ActivityEventType.DELETE_ATTACHMENT;
            displayTitle = currentDoc.getRenderedTitle(context);
            additionalIdentifier = ((AttachmentDeletedEvent) event).getName();
        } else if (event instanceof AttachmentUpdatedEvent) {
            eventType = ActivityEventType.UPDATE_ATTACHMENT;
            displayTitle = currentDoc.getRenderedTitle(context);
            additionalIdentifier = ((AttachmentUpdatedEvent) event).getName();
        } else if (event instanceof AnnotationAddedEvent) {
            eventType = ActivityEventType.ADD_ANNOTATION;
            displayTitle = currentDoc.getRenderedTitle(context);
            additionalIdentifier = ((AnnotationAddedEvent) event).getIdentifier();
        } else if (event instanceof AnnotationDeletedEvent) {
            eventType = ActivityEventType.DELETE_ANNOTATION;
            displayTitle = currentDoc.getRenderedTitle(context);
            additionalIdentifier = ((AnnotationDeletedEvent) event).getIdentifier();
        } else {
            // update annotation
            eventType = ActivityEventType.UPDATE_ANNOTATION;
            displayTitle = currentDoc.getRenderedTitle(context);
            additionalIdentifier = ((AnnotationUpdatedEvent) event).getIdentifier();
        }
        List<String> params = new ArrayList<String>();
        params.add(displayTitle);
        if (additionalIdentifier != null) {
            params.add(additionalIdentifier);
        }
        try {
            addDocumentActivityEvent(streamName, currentDoc, eventType, msgPrefix + eventType, params, context);
        } catch (ActivityStreamException e) {
            LOGGER.error("Exception while trying to add a document activity event, updated document: [" + wiki + ":" + currentDoc + "]");
        }
        executionContext.getContext().removeProperty(AbstractEventStreamEvent.EVENT_LOOP_CONTEXT_LOCK_PROPERTY);
    }
}
Also used : AttachmentAddedEvent(com.xpn.xwiki.internal.event.AttachmentAddedEvent) DocumentCreatedEvent(org.xwiki.bridge.event.DocumentCreatedEvent) ArrayList(java.util.ArrayList) XWikiContext(com.xpn.xwiki.XWikiContext) ActivityStreamException(com.xpn.xwiki.plugin.activitystream.api.ActivityStreamException) DocumentUpdatedEvent(org.xwiki.bridge.event.DocumentUpdatedEvent) CommentUpdatedEvent(com.xpn.xwiki.internal.event.CommentUpdatedEvent) RemoteObservationManagerContext(org.xwiki.observation.remote.RemoteObservationManagerContext) CommentDeletedEvent(com.xpn.xwiki.internal.event.CommentDeletedEvent) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) DocumentDeletedEvent(org.xwiki.bridge.event.DocumentDeletedEvent) CommentAddedEvent(com.xpn.xwiki.internal.event.CommentAddedEvent) Execution(org.xwiki.context.Execution) AnnotationAddedEvent(org.xwiki.annotation.event.AnnotationAddedEvent) AttachmentUpdatedEvent(com.xpn.xwiki.internal.event.AttachmentUpdatedEvent) ObservationContext(org.xwiki.observation.ObservationContext) AnnotationDeletedEvent(org.xwiki.annotation.event.AnnotationDeletedEvent) AttachmentDeletedEvent(com.xpn.xwiki.internal.event.AttachmentDeletedEvent)

Aggregations

XWikiContext (com.xpn.xwiki.XWikiContext)2 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)2 AnnotationAddedEvent (org.xwiki.annotation.event.AnnotationAddedEvent)2 AnnotationDeletedEvent (org.xwiki.annotation.event.AnnotationDeletedEvent)2 AttachmentAddedEvent (com.xpn.xwiki.internal.event.AttachmentAddedEvent)1 AttachmentDeletedEvent (com.xpn.xwiki.internal.event.AttachmentDeletedEvent)1 AttachmentUpdatedEvent (com.xpn.xwiki.internal.event.AttachmentUpdatedEvent)1 CommentAddedEvent (com.xpn.xwiki.internal.event.CommentAddedEvent)1 CommentDeletedEvent (com.xpn.xwiki.internal.event.CommentDeletedEvent)1 CommentUpdatedEvent (com.xpn.xwiki.internal.event.CommentUpdatedEvent)1 XObjectAddedEvent (com.xpn.xwiki.internal.event.XObjectAddedEvent)1 XObjectDeletedEvent (com.xpn.xwiki.internal.event.XObjectDeletedEvent)1 XObjectEvent (com.xpn.xwiki.internal.event.XObjectEvent)1 XObjectUpdatedEvent (com.xpn.xwiki.internal.event.XObjectUpdatedEvent)1 BaseObject (com.xpn.xwiki.objects.BaseObject)1 BaseObjectReference (com.xpn.xwiki.objects.BaseObjectReference)1 ActivityStreamException (com.xpn.xwiki.plugin.activitystream.api.ActivityStreamException)1 ArrayList (java.util.ArrayList)1 AnnotationUpdatedEvent (org.xwiki.annotation.event.AnnotationUpdatedEvent)1 DocumentCreatedEvent (org.xwiki.bridge.event.DocumentCreatedEvent)1