public static void annotationToDocumentModel(Annotation annotation, DocumentModel documentModel) { commentToDocumentModel(annotation, documentModel); documentModel.setPropertyValue(ANNOTATION_XPATH_PROPERTY, annotation.getXpath()); }
@Override protected Annotation readEntity(JsonNode jn) { Annotation annotation = new AnnotationImpl(); fillCommentEntity(jn, annotation); annotation.setXpath(jn.get(ANNOTATION_XPATH).textValue()); return annotation; }
@Override protected void writeEntityBody(Annotation entity, JsonGenerator jg) throws IOException { writeCommentEntity(entity, jg); jg.writeStringField(ANNOTATION_XPATH, entity.getXpath()); // Write permissions of current user on the annotation, // which are the ones granted on the annotated document CoreSession session = ctx.getSession(null).getSession(); NuxeoPrincipal principal = session.getPrincipal(); PermissionProvider permissionProvider = Framework.getService(PermissionProvider.class); Collection<String> permissions = CoreInstance.doPrivileged(session, s -> { return s.filterGrantedPermissions(principal, new IdRef(entity.getParentId()), Arrays.asList(permissionProvider.getPermissions())); }); jg.writeArrayFieldStart(ANNOTATION_PERMISSIONS); for (String permission : permissions) { jg.writeString(permission); } jg.writeEndArray(); } }
@Override public void updateExternalAnnotation(CoreSession session, String entityId, Annotation annotation) throws CommentNotFoundException, CommentSecurityException { NuxeoPrincipal principal = session.getPrincipal(); if (!principal.isAdministrator() && !annotation.getAuthor().equals(principal.getName())) { throw new CommentSecurityException("The user " + session.getPrincipal().getName() + " can not edit annotations of document " + annotation.getParentId()); } DocumentModel annotationModel = getAnnotationModel(session, entityId); if (annotationModel == null) { throw new CommentNotFoundException("The external annotation " + entityId + " does not exist."); } Comments.annotationToDocumentModel(annotation, annotationModel); if (annotation instanceof ExternalEntity) { Comments.externalEntityToDocumentModel((ExternalEntity) annotation, annotationModel); } session.saveDocument(annotationModel); }
@Override public Annotation createAnnotation(CoreSession session, Annotation annotation) throws CommentSecurityException { String parentId = annotation.getParentId(); if (!session.hasPermission(new IdRef(parentId), SecurityConstants.READ)) { throw new CommentSecurityException("The user " + session.getPrincipal().getName() + " can not create annotations on document " + parentId); } return CoreInstance.doPrivileged(session, s -> { // Create base comment in the annotation DocumentModel docToAnnotate = s.getDocument(new IdRef(annotation.getParentId())); DocumentModel annotationModel = s.createDocumentModel(ANNOTATION_DOC_TYPE); Comments.annotationToDocumentModel(annotation, annotationModel); if (annotation instanceof ExternalEntity) { annotationModel.addFacet(EXTERNAL_ENTITY_FACET); Comments.externalEntityToDocumentModel((ExternalEntity) annotation, annotationModel); } annotationModel = Framework.getService(CommentManager.class).createComment(docToAnnotate, annotationModel); return Comments.newAnnotation(annotationModel); }); }
@Override public void updateAnnotation(CoreSession session, String annotationId, Annotation annotation) throws CommentNotFoundException, CommentSecurityException { IdRef annotationRef = new IdRef(annotationId); if (!session.exists(annotationRef)) { throw new CommentNotFoundException("The annotation " + annotationId + " does not exist."); } NuxeoPrincipal principal = session.getPrincipal(); if (!principal.isAdministrator() && !annotation.getAuthor().equals(principal.getName())) { throw new CommentSecurityException("The user " + principal.getName() + " can not edit annotations of document " + annotation.getParentId()); } CoreInstance.doPrivileged(session, s -> { DocumentModel annotationModel = s.getDocument(annotationRef); Comments.annotationToDocumentModel(annotation, annotationModel); if (annotation instanceof ExternalEntity) { Comments.externalEntityToDocumentModel((ExternalEntity) annotation, annotationModel); } s.saveDocument(annotationModel); }); }
public static void documentModelToAnnotation(DocumentModel documentModel, Annotation annotation) { documentModelToComment(documentModel, annotation); annotation.setXpath((String) documentModel.getPropertyValue(ANNOTATION_XPATH_PROPERTY)); }