public void convertCommentsToRaw(Map<String,Object> source) { @SuppressWarnings("unchecked") List<Map<String, Object>> comments = (List<Map<String, Object>>) source.get(COMMENTS_FIELD); if (comments == null || comments.isEmpty()) { return; } List<String> asJson = new ArrayList<>(); for (Map<String, Object> comment : comments) { asJson.add((new AlertComment(comment)).asJson()); } source.put(COMMENTS_FIELD, asJson); } }
protected static void reformatComments(SolrDocument solrDocument, Map<String, Object> document) { // Make sure comments are in the proper format @SuppressWarnings("unchecked") List<String> commentStrs = (List<String>) solrDocument.get(COMMENTS_FIELD); if (commentStrs != null) { try { List<AlertComment> comments = new ArrayList<>(); for (String commentStr : commentStrs) { comments.add(new AlertComment(commentStr)); } document.put(COMMENTS_FIELD, comments.stream().map(AlertComment::asMap).collect(Collectors.toList())); } catch (ParseException e) { throw new IllegalStateException("Unable to parse comment", e); } } }
@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()); }
private List<AlertComment> getComments(Document withComment) { List<Map<String, Object>> commentsField = List.class.cast(withComment.getDocument().get(COMMENTS_FIELD)); List<AlertComment> comments = new ArrayList<>(); if(commentsField != null) { comments = commentsField .stream() .map(map -> new AlertComment(map)) .collect(Collectors.toList()); } return comments; }
List<AlertComment> comments = new ArrayList<>(); for (Map<String, Object> commentMap : commentsMap) { comments.add(new AlertComment(commentMap));
alertComments.add(new AlertComment(commentRaw)); new AlertComment(request.getComment(), request.getUsername(), request.getTimestamp())); List<Map<String, Object>> commentsFinal = alertComments.stream().map(AlertComment::asMap) .collect(Collectors.toList());
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())); Document newVersion = new Document(latest); if (comments.size() > 0) {
@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()); }
@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 @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.add(new AlertComment("New Comment", "test_user", 1526401584951L)); document.getDocument().put(COMMENTS_FIELD, comments.stream().map(AlertComment::asMap).collect( Collectors.toList())); comments.add(new AlertComment("New Comment 2", "test_user_2", 1526401584952L)); document.getDocument().put(COMMENTS_FIELD, comments.stream().map(AlertComment::asMap).collect( Collectors.toList())); comments.add(new AlertComment(commentOne)); document.getDocument().put(COMMENTS_FIELD, comments.stream().map(AlertComment::asMap).collect( Collectors.toList()));