use of org.xwiki.annotation.io.IOServiceException in project xwiki-platform by xwiki.
the class DefaultIOService method getAnnotations.
/**
* {@inheritDoc}
* <p>
* This implementation retrieves all the objects of the annotation class in the document where target points to, and
* which have the target set to {@code target}.
* </p>
*
* @see org.xwiki.annotation.io.IOService#getAnnotations(String)
*/
@Override
public Collection<Annotation> getAnnotations(String target) throws IOServiceException {
try {
// 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
List<BaseObject> objects = document.getXObjects(configuration.getAnnotationClassReference());
// and build a list of Annotation objects
List<Annotation> result = new ArrayList<Annotation>();
if (objects == null) {
return Collections.<Annotation>emptySet();
}
for (BaseObject object : objects) {
// if it's not on the required target, ignore it
if (object == null || !localTargetId.equals(object.getStringValue(Annotation.TARGET_FIELD))) {
continue;
}
// use the object number as annotation id
result.add(loadAnnotationFromObject(object, deprecatedContext));
}
return result;
} catch (XWikiException e) {
throw new IOServiceException("An exception has occurred while loading the annotations", e);
}
}
use of org.xwiki.annotation.io.IOServiceException in project xwiki-platform by xwiki.
the class DefaultIOService method addAnnotation.
/**
* {@inheritDoc}
* <p>
* This implementation saves the added annotation in the document where the target of the annotation is.
* </p>
*
* @see org.xwiki.annotation.io.IOService#addAnnotation(String, org.xwiki.annotation.Annotation)
*/
@Override
public void addAnnotation(String target, Annotation annotation) throws IOServiceException {
try {
// extract the document name from the passed target
// by default the fullname is the passed target
String documentFullName = target;
EntityReference targetReference = referenceResolver.resolve(target, EntityType.DOCUMENT);
// try to get a document reference from the passed target reference
EntityReference docRef = targetReference.extractReference(EntityType.DOCUMENT);
if (docRef != null) {
documentFullName = serializer.serialize(docRef);
}
// now get the document with that name
XWikiContext deprecatedContext = getXWikiContext();
XWikiDocument document = deprecatedContext.getWiki().getDocument(documentFullName, deprecatedContext);
// create a new object in this document to hold the annotation
// Make sure to use a relative reference when creating the XObject, since we can`t use absolute references
// for an object's class. This avoids ugly log warning messages.
EntityReference annotationClassReference = configuration.getAnnotationClassReference();
annotationClassReference = annotationClassReference.removeParent(annotationClassReference.extractReference(EntityType.WIKI));
int id = document.createXObject(annotationClassReference, deprecatedContext);
BaseObject object = document.getXObject(configuration.getAnnotationClassReference(), id);
updateObject(object, annotation, deprecatedContext);
// and set additional data: author to annotation author, date to now and the annotation target
object.set(Annotation.DATE_FIELD, new Date(), deprecatedContext);
// TODO: maybe we shouldn't trust what we receive from the caller but set the author from the context.
// Or the other way around, set the author of the document from the annotations author.
object.set(Annotation.AUTHOR_FIELD, annotation.getAuthor(), deprecatedContext);
// ftm don't store the type of the reference since we only need to recognize the field, not to also read it.
if (targetReference.getType() == EntityType.OBJECT_PROPERTY || targetReference.getType() == EntityType.DOCUMENT) {
object.set(Annotation.TARGET_FIELD, localSerializer.serialize(targetReference), deprecatedContext);
} else {
object.set(Annotation.TARGET_FIELD, target, deprecatedContext);
}
// set the author of the document to the current user
document.setAuthor(deprecatedContext.getUser());
// Note: We make sure to only provide a few characters of contextual information in order to control the
// size of the comment (we display the first 30 characters).
deprecatedContext.getWiki().saveDocument(document, "Added annotation on \"" + StringUtils.abbreviate(annotation.getSelection(), 30) + "\"", deprecatedContext);
} catch (XWikiException e) {
throw new IOServiceException("An exception message has occurred while saving the annotation", e);
}
}
Aggregations