public static void annotationToDocumentModel(Annotation annotation, DocumentModel documentModel) { commentToDocumentModel(annotation, documentModel); documentModel.setPropertyValue(ANNOTATION_XPATH_PROPERTY, annotation.getXpath()); }
public static Annotation newAnnotation(DocumentModel annotationModel) { Annotation annotation = new AnnotationImpl(); documentModelToAnnotation(annotationModel, annotation); documentModelToExternalEntity(annotationModel, (ExternalEntity) annotation); return annotation; } }
public static Comment newComment(DocumentModel commentModel) { Comment comment = new CommentImpl(); documentModelToComment(commentModel, comment); documentModelToExternalEntity(commentModel, (ExternalEntity) comment); return comment; }
@Override public Comment createComment(CoreSession session, Comment comment) throws CommentNotFoundException, CommentSecurityException { DocumentRef commentRef = new IdRef(comment.getParentId()); if (!session.exists(commentRef)) { throw new CommentNotFoundException("The document " + comment.getParentId() + " does not exist."); } DocumentModel docToComment = session.getDocument(commentRef); DocumentModel commentModel = session.createDocumentModel(COMMENT_DOC_TYPE); commentModel.setPropertyValue("dc:created", Calendar.getInstance()); Comments.commentToDocumentModel(comment, commentModel); if (comment instanceof ExternalEntity) { commentModel.addFacet(EXTERNAL_ENTITY_FACET); Comments.externalEntityToDocumentModel((ExternalEntity) comment, commentModel); } DocumentModel createdCommentModel = createComment(docToComment, commentModel); return Comments.newComment(createdCommentModel); }
@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 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 Comment getComment(CoreSession session, String commentId) throws CommentNotFoundException, CommentSecurityException { DocumentRef commentRef = new IdRef(commentId); if (!session.exists(commentRef)) { throw new CommentNotFoundException("The document " + commentId + " does not exist."); } DocumentModel commentModel = session.getDocument(commentRef); return Comments.newComment(commentModel); }
@Override public Annotation getExternalAnnotation(CoreSession session, String entityId) throws CommentNotFoundException, CommentSecurityException { DocumentModel annotationModel = getAnnotationModel(session, entityId); if (annotationModel == null) { throw new CommentNotFoundException("The external annotation " + entityId + " does not exist."); } String parentId = (String) annotationModel.getPropertyValue(COMMENT_PARENT_ID); if (!session.hasPermission(new IdRef(parentId), SecurityConstants.READ)) { throw new CommentSecurityException("The user " + session.getPrincipal().getName() + " does not have access to the annotations of document " + parentId); } return Comments.newAnnotation(annotationModel); }
public static void documentModelToAnnotation(DocumentModel documentModel, Annotation annotation) { documentModelToComment(documentModel, annotation); annotation.setXpath((String) documentModel.getPropertyValue(ANNOTATION_XPATH_PROPERTY)); }
@Override public Comment updateExternalComment(CoreSession session, String entityId, Comment comment) throws CommentNotFoundException { DocumentModel commentModel = getExternalCommentModel(session, entityId); if (commentModel == null) { throw new CommentNotFoundException("The external comment " + entityId + " does not exist."); } NuxeoPrincipal principal = session.getPrincipal(); if (!principal.isAdministrator() && !comment.getAuthor().equals(principal.getName())) { throw new CommentSecurityException( "The user " + principal.getName() + " can not edit comments of document " + comment.getParentId()); } return CoreInstance.doPrivileged(session, s -> { Comments.commentToDocumentModel(comment, commentModel); if (comment instanceof ExternalEntity) { Comments.externalEntityToDocumentModel((ExternalEntity) comment, commentModel); } s.saveDocument(commentModel); return Comments.newComment(commentModel); }); }
@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); }); }
@Override public Comment getExternalComment(CoreSession session, String entityId) throws CommentNotFoundException { DocumentModel commentModel = getExternalCommentModel(session, entityId); if (commentModel == null) { throw new CommentNotFoundException("The external comment " + entityId + " does not exist."); } String parentId = (String) commentModel.getPropertyValue(COMMENT_PARENT_ID); if (!session.hasPermission(getAncestorRef(session, commentModel), SecurityConstants.READ)) { throw new CommentSecurityException("The user " + session.getPrincipal().getName() + " does not have access to the comments of document " + parentId); } return Framework.doPrivileged(() -> Comments.newComment(commentModel)); }
@Override public Annotation getAnnotation(CoreSession session, String annotationId) throws CommentNotFoundException, CommentSecurityException { DocumentRef annotationRef = new IdRef(annotationId); if (!session.exists(annotationRef)) { throw new CommentNotFoundException("The document " + annotationId + " does not exist."); } NuxeoPrincipal principal = session.getPrincipal(); return CoreInstance.doPrivileged(session, s -> { DocumentModel annotationModel = s.getDocument(annotationRef); String parentId = (String) annotationModel.getPropertyValue(COMMENT_PARENT_ID); if (!s.hasPermission(principal, new IdRef(parentId), SecurityConstants.READ)) { throw new CommentSecurityException("The user " + principal.getName() + " does not have access to the annotations of document " + parentId); } return Comments.newAnnotation(annotationModel); }); }
@Override public Comment updateComment(CoreSession session, String commentId, Comment comment) throws CommentNotFoundException { IdRef commentRef = new IdRef(commentId); if (!CoreInstance.doPrivileged(session, s -> {return s.exists(commentRef);})) { throw new CommentNotFoundException("The comment " + commentId + " does not exist."); } NuxeoPrincipal principal = session.getPrincipal(); if (!principal.isAdministrator() && !comment.getAuthor().equals(principal.getName())) { throw new CommentSecurityException( "The user " + principal.getName() + " can not edit comments of document " + comment.getParentId()); } return CoreInstance.doPrivileged(session, s -> { // Initiate Modification Date if it is not done yet if (comment.getModificationDate() == null) { comment.setModificationDate(Instant.now()); } DocumentModel commentModel = s.getDocument(commentRef); Comments.commentToDocumentModel(comment, commentModel); if (comment instanceof ExternalEntity) { Comments.externalEntityToDocumentModel((ExternalEntity) comment, commentModel); } s.saveDocument(commentModel); return Comments.newComment(commentModel); }); }
@Override public Comment getComment(CoreSession session, String commentId) throws CommentNotFoundException, CommentSecurityException { DocumentRef commentRef = new IdRef(commentId); // Parent document can be a comment, check existence as a privileged user if (!CoreInstance.doPrivileged(session, s -> {return s.exists(commentRef);})) { throw new CommentNotFoundException("The comment " + commentId + " does not exist."); } NuxeoPrincipal principal = session.getPrincipal(); return CoreInstance.doPrivileged(session, s -> { DocumentModel commentModel = s.getDocument(commentRef); DocumentRef documentRef = getAncestorRef(s, commentModel); if (!s.hasPermission(principal, documentRef, SecurityConstants.READ)) { throw new CommentSecurityException("The user " + principal.getName() + " does not have access to the comments of document " + documentRef.reference()); } return Comments.newComment(commentModel); }); }
String path = getCommentContainerPath(s, parentId); DocumentModel commentModel = s.createDocumentModel(path, COMMENT_NAME, COMMENT_DOC_TYPE); Comments.commentToDocumentModel(comment, commentModel); if (comment instanceof ExternalEntity) { commentModel.addFacet(EXTERNAL_ENTITY_FACET); Comments.externalEntityToDocumentModel((ExternalEntity) comment, commentModel); commentModel = s.createDocument(commentModel); notifyEvent(s, CommentEvents.COMMENT_ADDED, s.getDocument(docRef), commentModel); return Comments.newComment(commentModel); });