Search in sources :

Example 1 with ActivityStreamException

use of com.xpn.xwiki.plugin.activitystream.api.ActivityStreamException 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)

Example 2 with ActivityStreamException

use of com.xpn.xwiki.plugin.activitystream.api.ActivityStreamException in project xwiki-platform by xwiki.

the class ActivityStreamImpl method deleteActivityEvent.

@Override
public void deleteActivityEvent(ActivityEvent event, XWikiContext context) throws ActivityStreamException {
    boolean bTransaction = true;
    ActivityEventImpl evImpl = loadActivityEvent(event, true, context);
    String oriDatabase = context.getWikiId();
    if (useLocalStore()) {
        XWikiHibernateStore hibstore;
        // delete event from the local database
        if (context.getWikiId().equals(event.getWiki())) {
            hibstore = context.getWiki().getHibernateStore();
        } else {
            context.setWikiId(event.getWiki());
            hibstore = context.getWiki().getHibernateStore();
        }
        try {
            if (bTransaction) {
                hibstore.checkHibernate(context);
                bTransaction = hibstore.beginTransaction(context);
            }
            Session session = hibstore.getSession(context);
            session.delete(evImpl);
            if (bTransaction) {
                hibstore.endTransaction(context, true);
            }
        } catch (XWikiException e) {
            throw new ActivityStreamException();
        } finally {
            try {
                if (bTransaction) {
                    hibstore.endTransaction(context, false);
                }
                if (context.getWikiId().equals(oriDatabase)) {
                    context.setWikiId(oriDatabase);
                }
            } catch (Exception e) {
            // Do nothing.
            }
        }
    }
    if (useMainStore()) {
        // delete event from the main database
        context.setWikiId(context.getMainXWiki());
        XWikiHibernateStore hibstore = context.getWiki().getHibernateStore();
        try {
            if (bTransaction) {
                hibstore.checkHibernate(context);
                bTransaction = hibstore.beginTransaction(context);
            }
            Session session = hibstore.getSession(context);
            session.delete(evImpl);
            if (bTransaction) {
                hibstore.endTransaction(context, true);
            }
        } catch (XWikiException e) {
            throw new ActivityStreamException();
        } finally {
            try {
                if (bTransaction) {
                    hibstore.endTransaction(context, false);
                }
                context.setWikiId(oriDatabase);
            } catch (Exception e) {
            // Do nothing
            }
        }
    }
    this.sendEventStreamEvent(new EventStreamDeletedEvent(), event);
}
Also used : XWikiHibernateStore(com.xpn.xwiki.store.XWikiHibernateStore) EventStreamDeletedEvent(org.xwiki.eventstream.events.EventStreamDeletedEvent) ActivityStreamException(com.xpn.xwiki.plugin.activitystream.api.ActivityStreamException) XWikiException(com.xpn.xwiki.XWikiException) XWikiException(com.xpn.xwiki.XWikiException) MalformedURLException(java.net.MalformedURLException) ActivityStreamException(com.xpn.xwiki.plugin.activitystream.api.ActivityStreamException) Session(org.hibernate.Session)

Example 3 with ActivityStreamException

use of com.xpn.xwiki.plugin.activitystream.api.ActivityStreamException in project xwiki-platform by xwiki.

the class BridgeEventStream method getRelatedEvents.

@Override
public EventGroup getRelatedEvents(Event e) {
    XWikiContext context = getXWikiContext();
    ActivityStreamPlugin plugin = getPlugin(context);
    EventGroup result = new EventGroup();
    try {
        result.addEvents(convertActivitiesToEvents(plugin.getActivityStream().getRelatedEvents(eventConverter.convertEventToActivity(e), context)).toArray(new Event[0]));
    } catch (ActivityStreamException ex) {
    // Should not happen, and the eventual error was already reported downstream
    }
    return result;
}
Also used : XWikiContext(com.xpn.xwiki.XWikiContext) EventStreamDeletedEvent(org.xwiki.eventstream.events.EventStreamDeletedEvent) ActivityEvent(com.xpn.xwiki.plugin.activitystream.api.ActivityEvent) Event(org.xwiki.eventstream.Event) EventStreamAddedEvent(org.xwiki.eventstream.events.EventStreamAddedEvent) ActivityStreamException(com.xpn.xwiki.plugin.activitystream.api.ActivityStreamException) EventGroup(org.xwiki.eventstream.EventGroup) ActivityStreamPlugin(com.xpn.xwiki.plugin.activitystream.plugin.ActivityStreamPlugin)

Example 4 with ActivityStreamException

use of com.xpn.xwiki.plugin.activitystream.api.ActivityStreamException in project xwiki-platform by xwiki.

the class BridgeEventStream method addEvent.

@Override
public void addEvent(Event e) {
    try {
        XWikiContext context = getXWikiContext();
        ActivityStreamPlugin plugin = getPlugin(context);
        plugin.getActivityStream().addActivityEvent(eventConverter.convertEventToActivity(e), context);
        this.observationManager.notify(new EventStreamAddedEvent(), e);
    } catch (ActivityStreamException ex) {
    // Unlikely; nothing we can do
    }
}
Also used : EventStreamAddedEvent(org.xwiki.eventstream.events.EventStreamAddedEvent) XWikiContext(com.xpn.xwiki.XWikiContext) ActivityStreamException(com.xpn.xwiki.plugin.activitystream.api.ActivityStreamException) ActivityStreamPlugin(com.xpn.xwiki.plugin.activitystream.plugin.ActivityStreamPlugin)

Example 5 with ActivityStreamException

use of com.xpn.xwiki.plugin.activitystream.api.ActivityStreamException in project xwiki-platform by xwiki.

the class BridgeEventStream method deleteEvent.

@Override
public void deleteEvent(Event e) {
    try {
        XWikiContext context = getXWikiContext();
        ActivityStreamPlugin plugin = getPlugin(context);
        plugin.getActivityStream().deleteActivityEvent(eventConverter.convertEventToActivity(e), context);
        this.observationManager.notify(new EventStreamDeletedEvent(), e);
    } catch (ActivityStreamException ex) {
    // Unlikely; nothing we can do
    }
}
Also used : EventStreamDeletedEvent(org.xwiki.eventstream.events.EventStreamDeletedEvent) XWikiContext(com.xpn.xwiki.XWikiContext) ActivityStreamException(com.xpn.xwiki.plugin.activitystream.api.ActivityStreamException) ActivityStreamPlugin(com.xpn.xwiki.plugin.activitystream.plugin.ActivityStreamPlugin)

Aggregations

ActivityStreamException (com.xpn.xwiki.plugin.activitystream.api.ActivityStreamException)11 XWikiException (com.xpn.xwiki.XWikiException)6 XWikiContext (com.xpn.xwiki.XWikiContext)5 ActivityStreamPlugin (com.xpn.xwiki.plugin.activitystream.plugin.ActivityStreamPlugin)4 ActivityEvent (com.xpn.xwiki.plugin.activitystream.api.ActivityEvent)3 XWikiHibernateStore (com.xpn.xwiki.store.XWikiHibernateStore)3 ArrayList (java.util.ArrayList)3 Session (org.hibernate.Session)3 EventStreamDeletedEvent (org.xwiki.eventstream.events.EventStreamDeletedEvent)3 MalformedURLException (java.net.MalformedURLException)2 EventStreamAddedEvent (org.xwiki.eventstream.events.EventStreamAddedEvent)2 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)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 CommentAddedEvent (com.xpn.xwiki.internal.event.CommentAddedEvent)1 CommentDeletedEvent (com.xpn.xwiki.internal.event.CommentDeletedEvent)1 CommentUpdatedEvent (com.xpn.xwiki.internal.event.CommentUpdatedEvent)1 Date (java.util.Date)1 Query (org.hibernate.Query)1