Search in sources :

Example 6 with DocumentDeletedEvent

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

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

the class XWikiMockitoTest method deleteDocument.

@Test
public void deleteDocument() throws Exception {
    final DocumentReference documentReference = new DocumentReference("wiki", "Space", "Page");
    XWikiDocument document = mock(XWikiDocument.class);
    when(document.getDocumentReference()).thenReturn(documentReference);
    final XWikiDocument originalDocument = mock(XWikiDocument.class);
    when(document.getOriginalDocument()).thenReturn(originalDocument);
    this.xwiki.deleteDocument(document, this.context);
    ObservationManager observation = this.mocker.getInstance(ObservationManager.class);
    ArgumentMatcher<XWikiDocument> matcher = new ArgumentMatcher<XWikiDocument>() {

        @Override
        public boolean matches(XWikiDocument argument) {
            return argument.getDocumentReference().equals(documentReference) && argument.getOriginalDocument() == originalDocument;
        }
    };
    // Make sure the right events have been sent
    verify(observation).notify(eq(new DocumentDeletingEvent(documentReference)), argThat(matcher), same(this.context));
    verify(observation).notify(eq(new DocumentDeletedEvent(documentReference)), argThat(matcher), same(this.context));
    verifyNoMoreInteractions(observation);
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) DocumentDeletedEvent(org.xwiki.bridge.event.DocumentDeletedEvent) ArgumentMatcher(org.mockito.ArgumentMatcher) DocumentDeletingEvent(org.xwiki.bridge.event.DocumentDeletingEvent) ObservationManager(org.xwiki.observation.ObservationManager) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 8 with DocumentDeletedEvent

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

the class WikiDescriptorListener method onEvent.

@Override
public void onEvent(Event event, Object source, Object data) {
    XWikiDocument document = (XWikiDocument) source;
    // so then unregister them
    if (event instanceof DocumentDeletedEvent || event instanceof DocumentUpdatedEvent) {
        removeExistingDescriptor(document.getOriginalDocument());
    }
    // Register the new XWiki Server objects if any
    List<BaseObject> serverClassObjects = document.getXObjects(SERVER_CLASS);
    if (serverClassObjects != null && !serverClassObjects.isEmpty()) {
        DefaultWikiDescriptor descriptor = this.builder.buildDescriptorObject(serverClassObjects, document);
        if (descriptor != null) {
            this.cache.add(descriptor);
            this.cache.setWikiIds(null);
        }
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) DocumentDeletedEvent(org.xwiki.bridge.event.DocumentDeletedEvent) DocumentUpdatedEvent(org.xwiki.bridge.event.DocumentUpdatedEvent) DefaultWikiDescriptor(org.xwiki.wiki.internal.descriptor.DefaultWikiDescriptor) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 9 with DocumentDeletedEvent

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

the class DefaultWikiComponentManagerEventListenerTest method onDocumentDeleted.

@Test
public void onDocumentDeleted() throws Exception {
    DocumentModelBridge componentDocument = mock(DocumentModelBridge.class);
    when(componentDocument.getDocumentReference()).thenReturn(DOC_REFERENCE);
    mocker.getComponentUnderTest().onEvent(new DocumentDeletedEvent(DOC_REFERENCE), componentDocument, null);
    verify(this.wikiComponentManagerEventListenerHelper, times(1)).unregisterComponents(DOC_REFERENCE);
}
Also used : DocumentDeletedEvent(org.xwiki.bridge.event.DocumentDeletedEvent) DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) Test(org.junit.Test)

Example 10 with DocumentDeletedEvent

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

the class XClassPropertyEventGeneratorListener method onEvent.

@Override
public void onEvent(Event event, Object source, Object data) {
    XWikiDocument doc = (XWikiDocument) source;
    XWikiDocument originalDoc = doc.getOriginalDocument();
    XWikiContext context = (XWikiContext) data;
    if (event instanceof DocumentUpdatedEvent) {
        onDocumentUpdatedEvent(originalDoc, doc, context);
    } else if (event instanceof DocumentDeletedEvent) {
        onDocumentDeletedEvent(originalDoc, doc, context);
    } else if (event instanceof DocumentCreatedEvent) {
        onDocumentCreatedEvent(originalDoc, doc, context);
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) DocumentDeletedEvent(org.xwiki.bridge.event.DocumentDeletedEvent) DocumentCreatedEvent(org.xwiki.bridge.event.DocumentCreatedEvent) XWikiContext(com.xpn.xwiki.XWikiContext) DocumentUpdatedEvent(org.xwiki.bridge.event.DocumentUpdatedEvent)

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