Search in sources :

Example 1 with IOServiceException

use of org.xwiki.annotation.io.IOServiceException 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 IOServiceException

use of org.xwiki.annotation.io.IOServiceException 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 IOServiceException

use of org.xwiki.annotation.io.IOServiceException 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 IOServiceException

use of org.xwiki.annotation.io.IOServiceException in project xwiki-platform by xwiki.

the class DefaultAnnotationService method addAnnotation.

@Override
public void addAnnotation(String target, String selection, String selectionContext, int offset, String author, Map<String, Object> metadata) throws AnnotationServiceException {
    try {
        // create the annotation with this data and send it to the storage service
        // TODO: also think of mapping the annotation on the document at add time and fail it if it's not mappable,
        // for extra security
        // TODO: normalize spaces at this level
        String leftContext = selectionContext.substring(0, offset);
        String rightContext = selectionContext.substring(offset + selection.length());
        Annotation annotation = new Annotation(selection, leftContext, rightContext);
        annotation.setAuthor(author);
        // skip these fields as we don't want to overwrite them with whatever is in this map. Setters should be used
        // for these values or constructor
        Collection<String> skippedFields = Arrays.asList(new String[] { Annotation.SELECTION_FIELD, Annotation.SELECTION_LEFT_CONTEXT_FIELD, Annotation.SELECTION_RIGHT_CONTEXT_FIELD, Annotation.ORIGINAL_SELECTION_FIELD, Annotation.AUTHOR_FIELD, Annotation.STATE_FIELD });
        for (Map.Entry<String, Object> field : metadata.entrySet()) {
            if (!skippedFields.contains(field.getKey())) {
                annotation.set(field.getKey(), field.getValue());
            }
        }
        ioService.addAnnotation(target, annotation);
    } catch (IOServiceException e) {
        throw new AnnotationServiceException("An exception occurred when accessing the storage services", e);
    }
}
Also used : IOServiceException(org.xwiki.annotation.io.IOServiceException) AnnotationServiceException(org.xwiki.annotation.AnnotationServiceException) Map(java.util.Map) Annotation(org.xwiki.annotation.Annotation)

Example 5 with IOServiceException

use of org.xwiki.annotation.io.IOServiceException in project xwiki-platform by xwiki.

the class DefaultIOService method removeAnnotation.

/**
 * {@inheritDoc}
 * <p>
 * This implementation deletes the annotation object with the object number indicated by {@code annotationID} from
 * the document indicated by {@code target}, if its stored target matches the passed target.
 * </p>
 *
 * @see org.xwiki.annotation.io.IOService#removeAnnotation(String, String)
 */
@Override
public void removeAnnotation(String target, String annotationID) throws IOServiceException {
    try {
        if (annotationID == null || target == null) {
            return;
        }
        EntityReference targetReference = referenceResolver.resolve(target, EntityType.DOCUMENT);
        // get the target identifier and the document name from the parsed reference
        String localTargetId = target;
        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);
        if (document.isNew()) {
            // if the document doesn't exist already skip it
            return;
        }
        // and the document object on it
        BaseObject annotationObject = document.getXObject(configuration.getAnnotationClassReference(), Integer.valueOf(annotationID.toString()));
        // if object exists and its target matches the requested target, delete it
        if (annotationObject != null && localTargetId.equals(annotationObject.getStringValue(Annotation.TARGET_FIELD))) {
            document.removeObject(annotationObject);
            document.setAuthor(deprecatedContext.getUser());
            deprecatedContext.getWiki().saveDocument(document, "Deleted annotation " + annotationID, deprecatedContext);
        }
    } catch (NumberFormatException e) {
        throw new IOServiceException("An exception has occurred while parsing the annotation id", e);
    } catch (XWikiException e) {
        throw new IOServiceException("An exception has occurred while removing the annotation", 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)

Aggregations

IOServiceException (org.xwiki.annotation.io.IOServiceException)7 EntityReference (org.xwiki.model.reference.EntityReference)6 XWikiContext (com.xpn.xwiki.XWikiContext)5 XWikiException (com.xpn.xwiki.XWikiException)5 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)5 BaseObject (com.xpn.xwiki.objects.BaseObject)5 Annotation (org.xwiki.annotation.Annotation)3 ArrayList (java.util.ArrayList)2 Date (java.util.Date)1 Map (java.util.Map)1 AnnotationServiceException (org.xwiki.annotation.AnnotationServiceException)1 DocumentReference (org.xwiki.model.reference.DocumentReference)1 ParseException (org.xwiki.rendering.parser.ParseException)1 TransformationException (org.xwiki.rendering.transformation.TransformationException)1