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);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
Aggregations