@Override public Document removeCommentFromAlert(CommentAddRemoveRequest request, Document latest) throws IOException { if (latest == null || latest.getDocument() == null) { throw new IOException(String.format("Unable to remove comment. Document with guid %s cannot be found.", request.getGuid())); } @SuppressWarnings("unchecked") List<Map<String, Object>> commentMap = (List<Map<String, Object>>) latest.getDocument() .get(COMMENTS_FIELD); // Can't remove anything if there's nothing there if (commentMap == null) { throw new IOException(String.format("Unable to remove comment. Document with guid %s has no comments.", request.getGuid())); } List<Map<String, Object>> originalComments = new ArrayList<>(commentMap); List<AlertComment> comments = new ArrayList<>(); for (Map<String, Object> commentStr : originalComments) { comments.add(new AlertComment(commentStr)); } comments.remove( new AlertComment(request.getComment(), request.getUsername(), request.getTimestamp())); List<String> commentsAsJson = comments.stream().map(AlertComment::asJson) .collect(Collectors.toList()); Document newVersion = new Document(latest); newVersion.getDocument().put(COMMENTS_FIELD, commentsAsJson); return update(newVersion, Optional.empty()); }
@Override @SuppressWarnings("unchecked") public Document addCommentToAlert(CommentAddRemoveRequest request, Document latest) throws IOException { if (latest == null || latest.getDocument() == null) { throw new IOException(String.format("Unable to add comment. Document with guid %s cannot be found.", request.getGuid())); } List<Map<String, Object>> commentsField = (List<Map<String, Object>>) latest.getDocument() .getOrDefault(COMMENTS_FIELD, new ArrayList<>()); List<Map<String, Object>> originalComments = new ArrayList<>(commentsField); originalComments.add( new AlertComment(request.getComment(), request.getUsername(), request.getTimestamp()) .asMap()); Document newVersion = new Document(latest); newVersion.getDocument().put(COMMENTS_FIELD, originalComments); return update(newVersion, Optional.empty()); }
comments.remove(new AlertComment(request.getComment(), request.getUsername(), request.getTimestamp())); Document newVersion = new Document(latest); if (comments.size() > 0) {
new AlertComment(request.getComment(), request.getUsername(), request.getTimestamp())); List<Map<String, Object>> commentsFinal = alertComments.stream().map(AlertComment::asMap) .collect(Collectors.toList());
@Override @SuppressWarnings("unchecked") public Document addCommentToAlert(CommentAddRemoveRequest request, Document latest) throws IOException { if (latest == null || latest.getDocument() == null) { throw new IOException(String.format("Unable to add comment. Document with guid %s cannot be found.", request.getGuid())); } List<Map<String, Object>> comments = (List<Map<String, Object>>) latest.getDocument() .getOrDefault(COMMENTS_FIELD, new ArrayList<>()); List<Map<String, Object>> originalComments = new ArrayList<>(comments); // Convert all comments back to raw JSON before updating. List<Map<String, Object>> commentsMap = new ArrayList<>(); for (Map<String, Object> comment : originalComments) { commentsMap.add(new AlertComment(comment).asMap()); } commentsMap.add(new AlertComment( request.getComment(), request.getUsername(), request.getTimestamp()) .asMap()); Document newVersion = new Document(latest); newVersion.getDocument().put(COMMENTS_FIELD, commentsMap); return update(newVersion, Optional.empty()); }
@Override public Document addCommentToAlert(CommentAddRemoveRequest request, Document latest) throws IOException { if (latest == null || latest.getDocument() == null) { throw new IOException(String.format("Unable to add comment. Document with guid %s cannot be found.", request.getGuid())); } @SuppressWarnings("unchecked") List<Map<String, Object>> comments = (List<Map<String, Object>>) latest.getDocument() .getOrDefault(COMMENTS_FIELD, new ArrayList<>()); List<Map<String, Object>> originalComments = new ArrayList<>(comments); // Convert all comments back to raw JSON before updating. List<String> commentStrs = new ArrayList<>(); for (Map<String, Object> comment : originalComments) { commentStrs.add(new AlertComment(comment).asJson()); } commentStrs.add(new AlertComment( request.getComment(), request.getUsername(), request.getTimestamp() ).asJson()); Document newVersion = new Document(latest); newVersion.getDocument().put(COMMENTS_FIELD, commentStrs); return update(newVersion, Optional.empty()); }