Search in sources :

Example 1 with BaseObject

use of com.xpn.xwiki.objects.BaseObject 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 BaseObject

use of com.xpn.xwiki.objects.BaseObject in project xwiki-platform by xwiki.

the class DefaultIOService method getAnnotation.

@Override
public Annotation getAnnotation(String target, String annotationID) throws IOServiceException {
    try {
        if (annotationID == null || target == null) {
            return null;
        }
        // parse the target and extract the local reference serialized from it, by the same rules
        EntityReference targetReference = referenceResolver.resolve(target, EntityType.DOCUMENT);
        // build the target identifier for the annotation
        String localTargetId = target;
        // and the name of the document where it should be stored
        String docName = target;
        if (targetReference.getType() == EntityType.DOCUMENT || targetReference.getType() == EntityType.OBJECT_PROPERTY) {
            localTargetId = localSerializer.serialize(targetReference);
            docName = serializer.serialize(targetReference.extractReference(EntityType.DOCUMENT));
        }
        // get the document
        XWikiContext deprecatedContext = getXWikiContext();
        XWikiDocument document = deprecatedContext.getWiki().getDocument(docName, deprecatedContext);
        // and the annotation class objects in it
        // parse the annotation id as object index
        BaseObject object = document.getXObject(configuration.getAnnotationClassReference(), Integer.valueOf(annotationID.toString()));
        if (object == null || !localTargetId.equals(object.getStringValue(Annotation.TARGET_FIELD))) {
            return null;
        }
        // use the object number as annotation id
        return loadAnnotationFromObject(object, deprecatedContext);
    } catch (NumberFormatException e) {
        throw new IOServiceException("Could not parse annotation id " + annotationID, e);
    } catch (XWikiException e) {
        throw new IOServiceException("An exception has occurred while loading the annotation with id " + annotationID, e);
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) IOServiceException(org.xwiki.annotation.io.IOServiceException) EntityReference(org.xwiki.model.reference.EntityReference) XWikiContext(com.xpn.xwiki.XWikiContext) XWikiException(com.xpn.xwiki.XWikiException) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 3 with BaseObject

use of com.xpn.xwiki.objects.BaseObject in project xwiki-platform by xwiki.

the class DefaultIOService method updateAnnotations.

/**
 * {@inheritDoc}
 * <p>
 * Implementation which gets all the annotation class objects in the document pointed by the target, and matches
 * their ids against the ids in the passed collection of annotations. If they match, they are updated with the new
 * data in the annotations in annotation.
 * </p>
 *
 * @see org.xwiki.annotation.io.IOService#updateAnnotations(String, java.util.Collection)
 */
@Override
public void updateAnnotations(String target, Collection<Annotation> annotations) throws IOServiceException {
    try {
        EntityReference targetReference = referenceResolver.resolve(target, EntityType.DOCUMENT);
        // get the document name from the parsed reference
        String docName = target;
        if (targetReference.getType() == EntityType.DOCUMENT || targetReference.getType() == EntityType.OBJECT_PROPERTY) {
            docName = serializer.serialize(targetReference.extractReference(EntityType.DOCUMENT));
        }
        // get the document pointed to by the target
        XWikiContext deprecatedContext = getXWikiContext();
        XWikiDocument document = deprecatedContext.getWiki().getDocument(docName, deprecatedContext);
        List<String> updateNotifs = new ArrayList<String>();
        boolean updated = false;
        for (Annotation annotation : annotations) {
            // parse annotation id as string. If cannot parse, then ignore annotation, is not valid
            int annId = 0;
            try {
                annId = Integer.parseInt(annotation.getId());
            } catch (NumberFormatException e) {
                continue;
            }
            BaseObject object = document.getXObject(configuration.getAnnotationClassReference(), annId);
            if (object == null) {
                continue;
            }
            updated = updateObject(object, annotation, deprecatedContext) || updated;
            updateNotifs.add(annotation.getId());
        }
        if (updated) {
            // set the author of the document to the current user
            document.setAuthor(deprecatedContext.getUser());
            deprecatedContext.getWiki().saveDocument(document, "Updated annotations", deprecatedContext);
        }
    } catch (XWikiException e) {
        throw new IOServiceException("An exception has occurred while updating the annotation", e);
    }
}
Also used : ArrayList(java.util.ArrayList) XWikiContext(com.xpn.xwiki.XWikiContext) Annotation(org.xwiki.annotation.Annotation) BaseObject(com.xpn.xwiki.objects.BaseObject) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) IOServiceException(org.xwiki.annotation.io.IOServiceException) EntityReference(org.xwiki.model.reference.EntityReference) XWikiException(com.xpn.xwiki.XWikiException)

Example 4 with BaseObject

use of com.xpn.xwiki.objects.BaseObject in project xwiki-platform by xwiki.

the class DefaultModelBridgeTest method testCheckXObjectPresenceWithPresentXObject.

@Test
public void testCheckXObjectPresenceWithPresentXObject() throws Exception {
    XWikiDocument document = mock(XWikiDocument.class);
    List<String> xObjectTypes = Arrays.asList("type1");
    DocumentReference documentReferenceFromDocumentXObjects = mock(DocumentReference.class);
    Map<DocumentReference, List<BaseObject>> documentXObjects = new HashMap<DocumentReference, List<BaseObject>>() {

        {
            put(documentReferenceFromDocumentXObjects, Collections.EMPTY_LIST);
        }
    };
    DocumentReference resolvedType1 = mock(DocumentReference.class);
    LocalDocumentReference localDocumentReferenceType1 = mock(LocalDocumentReference.class);
    when(resolvedType1.getLocalDocumentReference()).thenReturn(localDocumentReferenceType1);
    when(this.documentReferenceResolver.resolve(eq("type1"))).thenReturn(resolvedType1);
    when(document.getXObjects()).thenReturn(documentXObjects);
    when(documentReferenceFromDocumentXObjects.getLocalDocumentReference()).thenReturn(localDocumentReferenceType1);
    assertTrue(this.mocker.getComponentUnderTest().checkXObjectPresence(xObjectTypes, document));
}
Also used : LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) HashMap(java.util.HashMap) List(java.util.List) DocumentReference(org.xwiki.model.reference.DocumentReference) LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference) BaseObject(com.xpn.xwiki.objects.BaseObject) Test(org.junit.Test)

Example 5 with BaseObject

use of com.xpn.xwiki.objects.BaseObject in project xwiki-platform by xwiki.

the class UntypedRecordableEventDescriptorComponentBuilderTest method testBuildComponent.

@Test
public void testBuildComponent() throws Exception {
    BaseObject baseObject = mock(BaseObject.class);
    XWikiDocument parentDocument = mock(XWikiDocument.class);
    DocumentReference documentReference = mock(DocumentReference.class);
    when(baseObject.getOwnerDocument()).thenReturn(parentDocument);
    when(parentDocument.getDocumentReference()).thenReturn(documentReference);
    // Ensure that the user rights are correctly checked
    when(this.authorizationManager.hasAccess(any(), any(), any())).thenReturn(true);
    List<WikiComponent> result = this.mocker.getComponentUnderTest().buildComponents(baseObject);
    assertEquals(1, result.size());
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) WikiComponent(org.xwiki.component.wiki.WikiComponent) DocumentReference(org.xwiki.model.reference.DocumentReference) BaseObject(com.xpn.xwiki.objects.BaseObject) Test(org.junit.Test)

Aggregations

BaseObject (com.xpn.xwiki.objects.BaseObject)484 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)309 DocumentReference (org.xwiki.model.reference.DocumentReference)225 Test (org.junit.Test)137 XWikiException (com.xpn.xwiki.XWikiException)102 XWikiContext (com.xpn.xwiki.XWikiContext)99 ArrayList (java.util.ArrayList)92 LocalDocumentReference (org.xwiki.model.reference.LocalDocumentReference)69 EntityReference (org.xwiki.model.reference.EntityReference)42 XWiki (com.xpn.xwiki.XWiki)41 BaseClass (com.xpn.xwiki.objects.classes.BaseClass)40 List (java.util.List)30 Date (java.util.Date)24 BaseProperty (com.xpn.xwiki.objects.BaseProperty)23 Document (com.xpn.xwiki.api.Document)22 HashMap (java.util.HashMap)21 QueryException (org.xwiki.query.QueryException)18 AbstractComponentTest (com.celements.common.test.AbstractComponentTest)16 IOException (java.io.IOException)16 Vector (java.util.Vector)16