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()); } }
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; } }
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); }); }
protected void writeRepliesSummary(CoreSession session, Comment entity, JsonGenerator jg) throws IOException { PartialList<Comment> comments = commentManager.getComments(session, entity.getId(), 1L, 0L, false); jg.writeNumberField(COMMENT_NUMBER_OF_REPLIES, comments.totalSize()); if (comments.size() > 0) { jg.writeStringField(COMMENT_LAST_REPLY_DATE, comments.get(0).getCreationDate().toString()); } } }
@Override public Comment createComment(CoreSession session, Comment comment) throws CommentNotFoundException, CommentSecurityException { String parentId = comment.getParentId(); DocumentRef docRef = new IdRef(parentId); throw new CommentNotFoundException("The document or comment " + comment.getParentId() + " does not exist."); if (comment.getCreationDate() == null) { comment.setCreationDate(Instant.now());
protected List<Comment> getAllComments(String annotationId, CommentManager commentManager, CoreSession session) { List<Comment> allComments = new ArrayList<>(); List<Comment> comments = commentManager.getComments(session, annotationId); for (Comment comment : comments) { allComments.addAll(getAllComments(comment.getId(), commentManager, session)); allComments.add(comment); } return allComments; }
@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 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); }
public List<DocumentModel> getComments(DocumentModel parent) { CoreSession session = docModel.getCoreSession(); List<Comment> comments = commentManager.getComments(session, parent.getId()); return CoreInstance.doPrivileged(session, s -> { return comments.stream().map(comment -> { DocumentModel commentModel = s.getDocument(new IdRef(comment.getId())); commentModel.detach(true); return commentModel; }).collect(Collectors.toList()); }); }
@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()); } }
protected void deleteCommentChildren(CoreSession coreSession, CommentManager commentManager, DocumentModel documentModel) { commentManager.getComments(coreSession, documentModel.getId()) .forEach(comment -> coreSession.removeDocument(new IdRef(comment.getId()))); }
@Override protected void writeEntityBody(Comment entity, JsonGenerator jg) throws IOException { writeCommentEntity(entity, jg); CoreSession session = ctx.getSession(null).getSession(); NuxeoPrincipal principal = session.getPrincipal(); PermissionProvider permissionProvider = Framework.getService(PermissionProvider.class); // Write permissions of current user on the annotation, // which are the ones granted on the commented document Collection<String> permissions = CoreInstance.doPrivileged(session, s -> { if (entity.getId() == null) { return Collections.emptyList(); } DocumentRef ancestorRef = new IdRef( (String) commentManager.getThreadForComment(s.getDocument(new IdRef(entity.getId()))) .getPropertyValue(COMMENT_PARENT_ID)); return s.filterGrantedPermissions(principal, ancestorRef, Arrays.asList(permissionProvider.getPermissions())); }); jg.writeArrayFieldStart(COMMENT_PERMISSIONS); for (String permission : permissions) { jg.writeString(permission); } jg.writeEndArray(); boolean includeRepliesSummary = ctx.getFetched(COMMENT_ENTITY_TYPE).contains(FETCH_REPLIES_SUMMARY); if (includeRepliesSummary) { writeRepliesSummary(session, entity, jg); } }