Search in sources :

Example 1 with Event

use of org.xwiki.observation.event.Event 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 Event

use of org.xwiki.observation.event.Event in project xwiki-platform by xwiki.

the class WikiDescriptorListenerTest method onDocumentUpdatedEvent.

@Test
public void onDocumentUpdatedEvent() throws Exception {
    XWikiDocument document = mock(XWikiDocument.class);
    XWikiDocument originalDocument = mock(XWikiDocument.class);
    when(document.getOriginalDocument()).thenReturn(originalDocument);
    Event event = new DocumentUpdatedEvent();
    List<BaseObject> objects = new ArrayList<>();
    BaseObject object = mock(BaseObject.class);
    objects.add(object);
    when(originalDocument.getXObjects(WikiDescriptorListener.SERVER_CLASS)).thenReturn(objects);
    DocumentReference documentReference = new DocumentReference("mainWiki", "XWiki", "XWikiServerSubwikiA");
    when(originalDocument.getDocumentReference()).thenReturn(documentReference);
    when(wikiDescriptorDocumentHelper.getWikiIdFromDocumentReference(documentReference)).thenReturn("subwikia");
    DefaultWikiDescriptor descriptor = new DefaultWikiDescriptor("subwikia", "alias");
    when(cache.getFromId("subwikia")).thenReturn(descriptor);
    // New objects
    List<BaseObject> newObjects = new ArrayList<>();
    BaseObject newObject = mock(BaseObject.class);
    newObjects.add(newObject);
    when(document.getXObjects(WikiDescriptorListener.SERVER_CLASS)).thenReturn(newObjects);
    DefaultWikiDescriptor newDescriptor = new DefaultWikiDescriptor("subwikia", "newAlias");
    when(builder.buildDescriptorObject(newObjects, document)).thenReturn(newDescriptor);
    // Test
    mocker.getComponentUnderTest().onEvent(event, document, null);
    // Verify
    verify(cache).remove(descriptor.getId(), descriptor.getAliases());
    verify(cache).add(newDescriptor);
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) ArrayList(java.util.ArrayList) Event(org.xwiki.observation.event.Event) DocumentUpdatedEvent(org.xwiki.bridge.event.DocumentUpdatedEvent) DocumentDeletedEvent(org.xwiki.bridge.event.DocumentDeletedEvent) DocumentUpdatedEvent(org.xwiki.bridge.event.DocumentUpdatedEvent) DefaultWikiDescriptor(org.xwiki.wiki.internal.descriptor.DefaultWikiDescriptor) DocumentReference(org.xwiki.model.reference.DocumentReference) BaseObject(com.xpn.xwiki.objects.BaseObject) Test(org.junit.Test)

Example 3 with Event

use of org.xwiki.observation.event.Event in project xwiki-platform by xwiki.

the class WikiDescriptorListenerTest method onDocumentDeletedEvent.

@Test
public void onDocumentDeletedEvent() throws Exception {
    XWikiDocument document = mock(XWikiDocument.class);
    XWikiDocument originalDocument = mock(XWikiDocument.class);
    when(document.getOriginalDocument()).thenReturn(originalDocument);
    Event event = new DocumentDeletedEvent();
    List<BaseObject> objects = new ArrayList<>();
    BaseObject object = mock(BaseObject.class);
    objects.add(object);
    when(originalDocument.getXObjects(WikiDescriptorListener.SERVER_CLASS)).thenReturn(objects);
    DocumentReference documentReference = new DocumentReference("mainWiki", "XWiki", "XWikiServerSubwikiA");
    when(originalDocument.getDocumentReference()).thenReturn(documentReference);
    when(wikiDescriptorDocumentHelper.getWikiIdFromDocumentReference(documentReference)).thenReturn("subwikia");
    DefaultWikiDescriptor descriptor = new DefaultWikiDescriptor("subwikia", "alias");
    when(cache.getFromId("subwikia")).thenReturn(descriptor);
    // Test
    mocker.getComponentUnderTest().onEvent(event, document, null);
    // Verify
    verify(cache).remove(descriptor.getId(), descriptor.getAliases());
    verify(cache, never()).add(any(DefaultWikiDescriptor.class));
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) DocumentDeletedEvent(org.xwiki.bridge.event.DocumentDeletedEvent) ArrayList(java.util.ArrayList) Event(org.xwiki.observation.event.Event) DocumentUpdatedEvent(org.xwiki.bridge.event.DocumentUpdatedEvent) DocumentDeletedEvent(org.xwiki.bridge.event.DocumentDeletedEvent) DefaultWikiDescriptor(org.xwiki.wiki.internal.descriptor.DefaultWikiDescriptor) DocumentReference(org.xwiki.model.reference.DocumentReference) BaseObject(com.xpn.xwiki.objects.BaseObject) Test(org.junit.Test)

Example 4 with Event

use of org.xwiki.observation.event.Event in project xwiki-platform by xwiki.

the class DefaultVelocityManager method initialize.

@Override
public void initialize() throws InitializationException {
    this.observation.addListener(new EventListener() {

        @Override
        public void onEvent(Event event, Object source, Object data) {
            if (event instanceof TemplateEvent) {
                TemplateEvent templateEvent = (TemplateEvent) event;
                DefaultVelocityManager.this.velocityFactory.removeVelocityEngine(templateEvent.getId());
            }
        }

        @Override
        public String getName() {
            return DefaultVelocityManager.class.getName();
        }

        @Override
        public List<Event> getEvents() {
            return EVENTS;
        }
    });
    // Set reserved bindings
    // "context" is a reserved binding in JSR223 world
    this.reservedBindings.add("context");
    // Macros directive
    this.reservedBindings.add("macro");
    // Foreach directive
    this.reservedBindings.add("foreach");
    this.reservedBindings.add(this.velocityConfiguration.getProperties().getProperty(RuntimeConstants.COUNTER_NAME, RuntimeSingleton.getString(RuntimeConstants.COUNTER_NAME)));
    this.reservedBindings.add(this.velocityConfiguration.getProperties().getProperty(RuntimeConstants.HAS_NEXT_NAME, RuntimeSingleton.getString(RuntimeConstants.HAS_NEXT_NAME)));
    // Evaluate directive
    this.reservedBindings.add("evaluate");
    // TryCatch directive
    this.reservedBindings.add("exception");
    this.reservedBindings.add("try");
    // Default directive
    this.reservedBindings.add("define");
    // The name of the context variable used for the template-level scope
    this.reservedBindings.add("template");
}
Also used : TemplateEvent(org.xwiki.template.event.TemplateEvent) TemplateDeletedEvent(org.xwiki.template.event.TemplateDeletedEvent) TemplateUpdatedEvent(org.xwiki.template.event.TemplateUpdatedEvent) TemplateEvent(org.xwiki.template.event.TemplateEvent) Event(org.xwiki.observation.event.Event) List(java.util.List) EventListener(org.xwiki.observation.EventListener)

Example 5 with Event

use of org.xwiki.observation.event.Event in project xwiki-platform by xwiki.

the class ScriptClassLoaderHandlerListener method getEvents.

@Override
public List<Event> getEvents() {
    List<Event> events = new LinkedList<Event>();
    events.add(new ScriptEvaluatingEvent());
    events.add(new ScriptEvaluatedEvent());
    return events;
}
Also used : ScriptEvaluatedEvent(org.xwiki.script.event.ScriptEvaluatedEvent) ScriptEvaluatingEvent(org.xwiki.script.event.ScriptEvaluatingEvent) ScriptEvaluatedEvent(org.xwiki.script.event.ScriptEvaluatedEvent) ScriptEvaluatingEvent(org.xwiki.script.event.ScriptEvaluatingEvent) CancelableEvent(org.xwiki.observation.event.CancelableEvent) Event(org.xwiki.observation.event.Event) LinkedList(java.util.LinkedList)

Aggregations

Event (org.xwiki.observation.event.Event)54 Test (org.junit.Test)48 DocumentDeletedEvent (org.xwiki.bridge.event.DocumentDeletedEvent)23 DocumentUpdatedEvent (org.xwiki.bridge.event.DocumentUpdatedEvent)23 DocumentCreatedEvent (org.xwiki.bridge.event.DocumentCreatedEvent)21 ArticleCreatingEvent (com.celements.blog.observation.event.ArticleCreatingEvent)9 BaseObject (com.xpn.xwiki.objects.BaseObject)9 ArticleCreatedEvent (com.celements.blog.observation.event.ArticleCreatedEvent)8 ArticleDeletedEvent (com.celements.blog.observation.event.ArticleDeletedEvent)8 ArticleDeletingEvent (com.celements.blog.observation.event.ArticleDeletingEvent)8 BlogCreatedEvent (com.celements.blog.observation.event.BlogCreatedEvent)8 BlogCreatingEvent (com.celements.blog.observation.event.BlogCreatingEvent)8 BlogDeletedEvent (com.celements.blog.observation.event.BlogDeletedEvent)8 BlogDeletingEvent (com.celements.blog.observation.event.BlogDeletingEvent)8 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)8 ArticleUpdatingEvent (com.celements.blog.observation.event.ArticleUpdatingEvent)7 ArrayList (java.util.ArrayList)7 ArticleUpdatedEvent (com.celements.blog.observation.event.ArticleUpdatedEvent)6 BlogUpdatedEvent (com.celements.blog.observation.event.BlogUpdatedEvent)6 BlogUpdatingEvent (com.celements.blog.observation.event.BlogUpdatingEvent)6