Search in sources :

Example 1 with Annotation

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;
    }
}
Also used : Annotation(org.xwiki.annotation.Annotation)

Example 2 with Annotation

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);
    }
}
Also used : ArrayList(java.util.ArrayList) AlteredContent(org.xwiki.annotation.content.AlteredContent) Annotation(org.xwiki.annotation.Annotation)

Example 3 with Annotation

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;
}
Also used : Annotation(org.xwiki.annotation.Annotation) XWikiException(com.xpn.xwiki.XWikiException)

Example 4 with 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);
    }
}
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 Annotation

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;
}
Also used : ObjectFactory(org.xwiki.annotation.rest.model.jaxb.ObjectFactory) AnnotationField(org.xwiki.annotation.rest.model.jaxb.AnnotationField) ArrayList(java.util.ArrayList) AnnotationStub(org.xwiki.annotation.rest.model.jaxb.AnnotationStub) Annotation(org.xwiki.annotation.Annotation)

Aggregations

Annotation (org.xwiki.annotation.Annotation)14 ArrayList (java.util.ArrayList)5 XWikiException (com.xpn.xwiki.XWikiException)4 IOServiceException (org.xwiki.annotation.io.IOServiceException)3 AnnotationField (org.xwiki.annotation.rest.model.jaxb.AnnotationField)3 XWikiContext (com.xpn.xwiki.XWikiContext)2 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)2 BaseObject (com.xpn.xwiki.objects.BaseObject)2 Map (java.util.Map)2 AnnotationServiceException (org.xwiki.annotation.AnnotationServiceException)2 AnnotationResponse (org.xwiki.annotation.rest.model.jaxb.AnnotationResponse)2 ObjectFactory (org.xwiki.annotation.rest.model.jaxb.ObjectFactory)2 EntityReference (org.xwiki.model.reference.EntityReference)2 HashMap (java.util.HashMap)1 List (java.util.List)1 PUT (javax.ws.rs.PUT)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 AlteredContent (org.xwiki.annotation.content.AlteredContent)1 AnnotationEvent (org.xwiki.annotation.renderer.AnnotationEvent)1 AnnotatedContent (org.xwiki.annotation.rest.model.jaxb.AnnotatedContent)1