protected static void writeCommentEntity(Comment entity, JsonGenerator jg) throws IOException { jg.writeStringField(COMMENT_ID_FIELD, entity.getId()); jg.writeStringField(COMMENT_PARENT_ID_FIELD, entity.getParentId()); jg.writeArrayFieldStart(COMMENT_ANCESTOR_IDS_FIELD); for (String ancestorId : entity.getAncestorIds()) { jg.writeString(ancestorId); } jg.writeEndArray(); jg.writeStringField(COMMENT_AUTHOR_FIELD, entity.getAuthor()); jg.writeStringField(COMMENT_TEXT_FIELD, entity.getText()); String creationDate = entity.getCreationDate() != null ? entity.getCreationDate().toString() : null; jg.writeStringField(COMMENT_CREATION_DATE_FIELD, creationDate); String modificationDate = entity.getModificationDate() != null ? entity.getModificationDate().toString() : null; jg.writeStringField(COMMENT_MODIFICATION_DATE_FIELD, modificationDate); if (entity instanceof ExternalEntity) { jg.writeStringField(EXTERNAL_ENTITY_ID, ((ExternalEntity) entity).getEntityId()); jg.writeStringField(EXTERNAL_ENTITY_ORIGIN, ((ExternalEntity) entity).getOrigin()); jg.writeStringField(EXTERNAL_ENTITY, ((ExternalEntity) entity).getEntity()); } }
public static void commentToDocumentModel(Comment comment, DocumentModel documentModel) { // Do not set ancestor ids as it is computed at document creation documentModel.setPropertyValue(COMMENT_AUTHOR, comment.getAuthor()); documentModel.setPropertyValue(COMMENT_TEXT, comment.getText()); documentModel.setPropertyValue(COMMENT_PARENT_ID, comment.getParentId()); Instant creationDate = comment.getCreationDate(); if (creationDate != null) { documentModel.setPropertyValue(COMMENT_CREATION_DATE, GregorianCalendar.from(ZonedDateTime.ofInstant(creationDate, ZoneId.systemDefault()))); } Instant modificationDate = comment.getModificationDate(); if (modificationDate != null) { documentModel.setPropertyValue(COMMENT_MODIFICATION_DATE, GregorianCalendar.from(ZonedDateTime.ofInstant(modificationDate, ZoneId.systemDefault()))); } }
@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); }); }