protected static Comment fillCommentEntity(JsonNode jn, Comment comment) { // don't read id from given JsonNode, if needed it is read from path comment.setParentId(jn.get(COMMENT_PARENT_ID_FIELD).textValue()); comment.setAuthor(jn.get(COMMENT_AUTHOR_FIELD).textValue()); comment.setText(jn.get(COMMENT_TEXT_FIELD).textValue()); JsonNode creationDateNode = jn.get(COMMENT_CREATION_DATE_FIELD); Instant creationDate = creationDateNode != null && !creationDateNode.isNull() ? Instant.parse(creationDateNode.textValue()) : null; comment.setCreationDate(creationDate); JsonNode modificationDateNode = jn.get(COMMENT_MODIFICATION_DATE_FIELD); Instant modificationDate = modificationDateNode != null && !modificationDateNode.isNull() ? Instant.parse(modificationDateNode.textValue()) : null; comment.setModificationDate(modificationDate); if (jn.has(EXTERNAL_ENTITY_ID)) { ExternalEntity externalEntity = (ExternalEntity) comment; externalEntity.setEntityId(jn.get(EXTERNAL_ENTITY_ID).textValue()); externalEntity.setOrigin(jn.get(EXTERNAL_ENTITY_ORIGIN).textValue()); externalEntity.setEntity(jn.get(EXTERNAL_ENTITY).textValue()); } return comment; } }
@SuppressWarnings("unchecked") public static void documentModelToComment(DocumentModel documentModel, Comment comment) { comment.setId(documentModel.getId()); comment.setAuthor((String) documentModel.getPropertyValue(COMMENT_AUTHOR)); comment.setText((String) documentModel.getPropertyValue(COMMENT_TEXT)); Collection<String> ancestorIds = (Collection<String>) documentModel.getPropertyValue(COMMENT_ANCESTOR_IDS); ancestorIds.forEach(comment::addAncestorId); String parentId = (String) documentModel.getPropertyValue(COMMENT_PARENT_ID); comment.setParentId(parentId); Calendar creationDate = (Calendar) documentModel.getPropertyValue(COMMENT_CREATION_DATE); if (creationDate != null) { comment.setCreationDate(creationDate.toInstant()); } Calendar modificationDate = (Calendar) documentModel.getPropertyValue(COMMENT_MODIFICATION_DATE); if (modificationDate != null) { comment.setModificationDate(modificationDate.toInstant()); } }
@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); }); }