Search in sources :

Example 1 with EntityReference

use of org.xwiki.model.reference.EntityReference in project xwiki-platform by xwiki.

the class DefaultIOTargetService method getSourceSyntax.

@Override
public String getSourceSyntax(String reference) throws IOServiceException {
    try {
        EntityReference ref = referenceResolver.resolve(reference, EntityType.DOCUMENT);
        EntityReference docRef = ref.extractReference(EntityType.DOCUMENT);
        if (docRef != null) {
            // doc
            return dab.getTranslatedDocumentInstance(new DocumentReference(docRef)).getSyntax().toIdString();
        } else {
            return dab.getDocumentSyntaxId(reference);
        }
    } catch (Exception e) {
        throw new IOServiceException("An exception has occurred while getting the syntax of the source for " + reference, e);
    }
}
Also used : IOServiceException(org.xwiki.annotation.io.IOServiceException) EntityReference(org.xwiki.model.reference.EntityReference) DocumentReference(org.xwiki.model.reference.DocumentReference) ParseException(org.xwiki.rendering.parser.ParseException) IOServiceException(org.xwiki.annotation.io.IOServiceException) TransformationException(org.xwiki.rendering.transformation.TransformationException)

Example 2 with EntityReference

use of org.xwiki.model.reference.EntityReference in project xwiki-platform by xwiki.

the class AnnotationEventGeneratorEventListener method getBaseObjectReference.

/**
 * @param objectEvent the event involving an object
 * @return the {@link BaseObjectReference} of the object corresponding to the object event
 */
private BaseObjectReference getBaseObjectReference(XObjectEvent objectEvent) {
    EntityReference objectReference = objectEvent.getReference();
    BaseObjectReference baseObjectReference = null;
    if (objectReference instanceof BaseObjectReference) {
        baseObjectReference = (BaseObjectReference) objectEvent.getReference();
    } else {
        baseObjectReference = new BaseObjectReference(objectEvent.getReference());
    }
    return baseObjectReference;
}
Also used : BaseObjectReference(com.xpn.xwiki.objects.BaseObjectReference) EntityReference(org.xwiki.model.reference.EntityReference)

Example 3 with EntityReference

use of org.xwiki.model.reference.EntityReference 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 4 with EntityReference

use of org.xwiki.model.reference.EntityReference 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 5 with EntityReference

use of org.xwiki.model.reference.EntityReference in project xwiki-platform by xwiki.

the class IndexedObjectReferenceTest method testObjectNumberWhenIndexNotNumber.

@Test
public void testObjectNumberWhenIndexNotNumber() {
    IndexedObjectReference reference = new IndexedObjectReference(new EntityReference("XWiki.Class[number]", EntityType.OBJECT, new EntityReference("Page", EntityType.DOCUMENT, new EntityReference("Space", EntityType.SPACE, new EntityReference("wiki", EntityType.WIKI)))));
    assertEquals("XWiki.Class[number]", reference.getClassName());
    assertNull(reference.getObjectNumber());
}
Also used : EntityReference(org.xwiki.model.reference.EntityReference) Test(org.junit.Test)

Aggregations

EntityReference (org.xwiki.model.reference.EntityReference)338 Test (org.junit.Test)157 DocumentReference (org.xwiki.model.reference.DocumentReference)107 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)39 BaseObject (com.xpn.xwiki.objects.BaseObject)38 ArrayList (java.util.ArrayList)27 XWikiContext (com.xpn.xwiki.XWikiContext)24 WikiReference (org.xwiki.model.reference.WikiReference)24 SpaceReference (org.xwiki.model.reference.SpaceReference)23 LocalDocumentReference (org.xwiki.model.reference.LocalDocumentReference)18 XWikiException (com.xpn.xwiki.XWikiException)17 EntityType (org.xwiki.model.EntityType)11 BaseClass (com.xpn.xwiki.objects.classes.BaseClass)10 EntityReferenceProvider (org.xwiki.model.reference.EntityReferenceProvider)9 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)8 XDOM (org.xwiki.rendering.block.XDOM)8 URL (java.net.URL)7 AttachmentReference (org.xwiki.model.reference.AttachmentReference)7 HashMap (java.util.HashMap)6 Before (org.junit.Before)6