use of org.xwiki.annotation.Annotation in project xwiki-platform by xwiki.
the class AbstractAnnotationRESTResource method filterAnnotations.
/**
* Helper method to filter a set of annotations according to the criteria in the passed annotation request. The
* fields in the filter of the request will be interpreted as a filter for equality with the value in the actual
* annotation, and all the fields conditions will be put together with an "or" operation.
*
* @param annotations the collection of annotations to filter
* @param request the request according which to filter
* @return the filtered collection of annotations
*/
protected Collection<Annotation> filterAnnotations(Collection<Annotation> annotations, AnnotationRequest request) {
Collection<Annotation> result = new ArrayList<Annotation>();
Map<String, List<String>> filters = new HashMap<String, List<String>>();
for (AnnotationField annotationField : request.getFilter().getFields()) {
String filterName = annotationField.getName();
List<String> values = filters.get(filterName);
if (values == null) {
values = new ArrayList<String>();
filters.put(filterName, values);
}
if (annotationField.getValue() != null) {
values.add(annotationField.getValue());
}
}
if (filters.size() == 0) {
return annotations;
}
for (Annotation ann : annotations) {
boolean matches = true;
for (Map.Entry<String, List<String>> filter : filters.entrySet()) {
Object annotationValue = ann.get(filter.getKey());
// if the values is not set or is not among the requested values,
if (annotationValue == null || !filter.getValue().contains(annotationValue.toString())) {
// it doesn't match and exit
matches = false;
break;
}
}
// if it matches in the end, add it to the results
if (matches) {
result.add(ann);
}
}
return result;
}
use of org.xwiki.annotation.Annotation in project xwiki-platform by xwiki.
the class AbstractAnnotationRESTResource method getSuccessResponseWithAnnotatedContent.
/**
* Builds an annotation response containing the annotated content along with the annotation stubs, according to the
* requirements in the passed annotations request.
*
* @param request the annotations request
* @param documentName the name of the document to provide an annotated response for
* @return an annotation response with the annotated content and the annotation stubs
* @throws AnnotationServiceException in case something goes wrong handling the annotations
* @throws XWikiException in case something goes wrong manipulating the xwiki context & documents
*/
protected AnnotationResponse getSuccessResponseWithAnnotatedContent(String documentName, AnnotationRequest request) throws AnnotationServiceException, XWikiException {
ObjectFactory factory = new ObjectFactory();
AnnotationResponse response = factory.createAnnotationResponse();
// get the annotations on this content
Collection<Annotation> annotations = annotationService.getAnnotations(documentName);
// filter them according to the request
Collection<Annotation> filteredAnnotations = filterAnnotations(annotations, request);
// render the document with the filtered annotations on it
String renderedHTML = renderDocumentWithAnnotations(documentName, null, DEFAULT_ACTION, filteredAnnotations);
// prepare the annotated content
AnnotatedContent annotatedContentResponse = factory.createAnnotatedContent();
annotatedContentResponse.getAnnotations().addAll(prepareAnnotationStubsSet(filteredAnnotations, request.getRequest().getFields()));
annotatedContentResponse.setContent(renderedHTML);
// set the annotated content along with the return code in the response and return it
response.setAnnotatedContent(annotatedContentResponse);
response.setResponseCode(0);
return response;
}
use of org.xwiki.annotation.Annotation in project xwiki-platform by xwiki.
the class AnnotationMaintainerTest method assertSameAnnotations.
/**
* Helper method to test if the two passed annotation lists contain the same annotations.
*
* @param expected the expected list of annotations
* @param actual the actual list of annotations
*/
private void assertSameAnnotations(List<Annotation> expected, List<Annotation> actual) {
assertTrue(expected.size() == actual.size());
for (Annotation actualAnn : actual) {
Annotation expectedAnn = getAnnotation(actualAnn.getId(), expected);
assertNotNull(expectedAnn);
assertEquals(expectedAnn.getSelection(), actualAnn.getSelection());
assertEquals(expectedAnn.getSelectionLeftContext(), actualAnn.getSelectionLeftContext());
assertEquals(expectedAnn.getSelectionRightContext(), actualAnn.getSelectionRightContext());
assertEquals(expectedAnn.getState(), actualAnn.getState());
assertEquals(expectedAnn.getOriginalSelection(), actualAnn.getOriginalSelection());
}
}
use of org.xwiki.annotation.Annotation 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.Annotation in project xwiki-platform by xwiki.
the class SingleAnnotationRESTResource method doUpdate.
/**
* Updates the specified annotation with the values of the fields in received collection.
*
* @param space the space of the document to update the annotation from
* @param page the name of the document to update the annotation from
* @param wiki the wiki of the document to update the annotation from
* @param id the id of the annotation to update
* @param updateRequest the request to update the annotation pointed by the id
* @return a annotation response for which the response code will be 0 in case of success and non-zero otherwise
* @throws XWikiRestException when failing to parse space
*/
@PUT
public AnnotationResponse doUpdate(@PathParam("spaceName") String space, @PathParam("pageName") String page, @PathParam("wikiName") String wiki, @PathParam("id") String id, AnnotationUpdateRequest updateRequest) throws XWikiRestException {
try {
DocumentReference documentReference = new DocumentReference(wiki, parseSpaceSegments(space), page);
// Initialize the context with the correct value.
updateContext(documentReference);
String documentName = referenceSerializer.serialize(documentReference);
// check access to this function
if (!annotationRightService.canEditAnnotation(id, documentName, getXWikiUser())) {
throw new WebApplicationException(Status.UNAUTHORIZED);
}
// id from the url
Annotation newAnnotation = new Annotation(id);
// fields from the posted content
for (AnnotationField field : updateRequest.getAnnotation().getFields()) {
newAnnotation.set(field.getName(), field.getValue());
}
// overwrite author if any was set because we know better who's logged in
newAnnotation.setAuthor(getXWikiUser());
// and update
annotationService.updateAnnotation(documentName, newAnnotation);
// and then return the annotated content, as specified by the annotation request
AnnotationResponse response = getSuccessResponseWithAnnotatedContent(documentName, updateRequest);
return response;
} catch (XWikiException e) {
getLogger().error(e.getMessage(), e);
return getErrorResponse(e);
} catch (AnnotationServiceException e) {
getLogger().error(e.getMessage(), e);
return getErrorResponse(e);
}
}
Aggregations