use of org.xwiki.annotation.Annotation in project xwiki-platform by xwiki.
the class XWikiAnnotationRightService method canEditAnnotation.
@Override
public boolean canEditAnnotation(String annotationId, String target, String userName) {
// if the user has edit right on the document represented by the target, or is the author of the annotation
try {
boolean hasEditRight = this.authorization.hasAccess(Right.EDIT, getUserReference(userName), getDocumentReference(target));
if (hasEditRight) {
return true;
}
// check if it's the author of the annotation
Annotation ann = annotationsStorageService.getAnnotation(target, annotationId);
return ann != null && ann.getAuthor().equals(userName);
} catch (Exception e) {
logException(e, target, userName);
return false;
}
}
use of org.xwiki.annotation.Annotation in project xwiki-platform by xwiki.
the class AbstractAnnotationMaintainer method updateAnnotations.
@Override
public void updateAnnotations(String target, String previousContent, String currentContent) throws MaintainerServiceException {
Collection<Annotation> annotations;
try {
annotations = ioService.getAnnotations(target);
if (annotations.size() == 0) {
// no annotations, nothing to do
return;
}
// store the annotations to save after update
List<Annotation> toUpdate = new ArrayList<Annotation>();
// produce the ptr of the previous and current, wrt to syntax
String syntaxId = ioContentService.getSourceSyntax(target);
String renderedPreviousContent = renderPlainText(previousContent, syntaxId);
String renderedCurrentContent = renderPlainText(currentContent, syntaxId);
// create the diffs
Collection<XDelta> differences = getDiffService().getDifferences(renderedPreviousContent, renderedCurrentContent);
// text space normalized version
if (differences.size() > 0) {
// compute the spaceless version of the renderedPreviousContent to be able to map the annotation on it
// (so that matching is done in the same way as for rendering), and then go back to the normalized
// version
AlteredContent spacelessRenderedPreviousContent = spaceStripperContentAlterer.alter(renderedPreviousContent);
// recompute properties for all annotations and store the ones to update
for (Annotation annotation : annotations) {
boolean wasUpdated = recomputeProperties(annotation, differences, renderedPreviousContent, spacelessRenderedPreviousContent, renderedCurrentContent);
if (wasUpdated) {
toUpdate.add(annotation);
}
}
}
// finally store the updates
ioService.updateAnnotations(target, toUpdate);
} catch (Exception e) {
throw new MaintainerServiceException("An exception occurred while updating annotations for content at " + target, e);
}
}
use of org.xwiki.annotation.Annotation in project xwiki-platform by xwiki.
the class DefaultIOService method loadAnnotationFromObject.
/**
* Helper function to load an annotation object from an xwiki object.
*
* @param object the xwiki object to load an annotation from
* @param deprecatedContext XWikiContext to make operations on xwiki data
* @return the Annotation instance for the annotation stored in BaseObject
*/
protected Annotation loadAnnotationFromObject(BaseObject object, XWikiContext deprecatedContext) {
// load the annotation with its ID, special handling of the state since it needs deserialization, special
// handling of the original selection which shouldn't be set if it's empty
Annotation annotation = new Annotation(object.getNumber() + "");
annotation.setState(AnnotationState.valueOf(object.getStringValue(Annotation.STATE_FIELD)));
String originalSelection = object.getStringValue(Annotation.ORIGINAL_SELECTION_FIELD);
if (originalSelection != null && originalSelection.length() > 0) {
annotation.setOriginalSelection(originalSelection);
}
Collection<String> skippedFields = Arrays.asList(new String[] { Annotation.ORIGINAL_SELECTION_FIELD, Annotation.STATE_FIELD });
// get all the props, filter those that need to be skipped and save the rest
for (String propName : object.getPropertyNames()) {
if (!skippedFields.contains(propName)) {
try {
annotation.set(propName, ((BaseProperty) object.get(propName)).getValue());
} catch (XWikiException e) {
this.logger.warn("Unable to get property " + propName + " from object " + object.getClassName() + "[" + object.getNumber() + "]. Will not be saved in the annotation.", e);
}
}
}
return annotation;
}
use of org.xwiki.annotation.Annotation 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.Annotation in project xwiki-platform by xwiki.
the class AbstractAnnotationRESTResource method prepareAnnotationStubsSet.
/**
* Helper function to translate a collection of annotations from the {@link Annotation} model to the JAXB model to
* be serialized for REST communication.
*
* @param annotations the annotations collection to be translated
* @param requestedFields the extra parameters that should be set for the prepared annotations
* @return translate set of org.xwiki.annotation.internal.annotation.Annotation to set of
* org.xwiki.annotation.internal.annotation.Annotation
*/
private Collection<AnnotationStub> prepareAnnotationStubsSet(Collection<Annotation> annotations, List<String> requestedFields) {
ObjectFactory factory = new ObjectFactory();
List<AnnotationStub> set = new ArrayList<AnnotationStub>();
for (Annotation xwikiAnnotation : annotations) {
AnnotationStub annotation = factory.createAnnotationStub();
annotation.setAnnotationId(xwikiAnnotation.getId());
annotation.setState(xwikiAnnotation.getState().toString());
// for all the requested extra fields, get them from the annotation and send them
for (String extraField : requestedFields) {
Object value = xwikiAnnotation.get(extraField);
AnnotationField field = new AnnotationField();
field.setName(extraField);
// value.toString() by default, null if value is missing
field.setValue(value != null ? value.toString() : null);
annotation.getFields().add(field);
}
set.add(annotation);
}
return set;
}
Aggregations