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 int hashCode() { int result = getComment() != null ? getComment().hashCode() : 0; result = 31 * result + (getUsername() != null ? getUsername().hashCode() : 0); result = 31 * result + (int) (getTimestamp() ^ (getTimestamp() >>> 32)); return result; }
@SuppressWarnings("unchecked") public String asJson() { return asJSONObject().toJSONString(); }
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); } }
@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()); }
@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 public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } AlertComment that = (AlertComment) o; if (getTimestamp() != that.getTimestamp()) { return false; } if (getComment() != null ? !getComment().equals(that.getComment()) : that.getComment() != null) { return false; } return getUsername() != null ? getUsername().equals(that.getUsername()) : that.getUsername() == null; }
@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()); }
@Test public void testAddComment() throws Exception { Document document = createAndIndexDocument("testAddCommentAndPatch"); // comment on the document String commentText = "New Comment"; String commentUser = "test_user"; long commentTimestamp = 152630493050L; Document withComment = addAlertComment(document.getGuid(), commentText, commentUser, commentTimestamp); { // validate that the comment was made on the returned document List<AlertComment> comments = getComments(withComment); Assert.assertEquals(1, comments.size()); Assert.assertEquals(commentText, comments.get(0).getComment()); Assert.assertEquals(commentUser, comments.get(0).getUsername()); Assert.assertEquals(commentTimestamp, comments.get(0).getTimestamp()); } { // validate that the comment was made on the indexed document Document indexed = findUpdatedDoc(withComment.getDocument(), withComment.getGuid(), SENSOR_NAME); List<AlertComment> comments = getComments(indexed); Assert.assertEquals(1, comments.size()); Assert.assertEquals(commentText, comments.get(0).getComment()); Assert.assertEquals(commentUser, comments.get(0).getUsername()); Assert.assertEquals(commentTimestamp, comments.get(0).getTimestamp()); } }
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; }
@Test public void testRemoveComments() throws Exception { String guid = "testRemoveComments"; createAndIndexDocument(guid); // add a comment on the document Document withComments = addAlertComment(guid, "comment", "user1", 1526401584951L); Assert.assertEquals(1, getComments(withComments).size()); // ensure the comment was added to the document in the index Document indexedWithComments = findUpdatedDoc(withComments.getDocument(), withComments.getGuid(), withComments.getSensorType()); Assert.assertEquals(1, getComments(indexedWithComments).size()); // remove a comment from the document AlertComment toRemove = getComments(withComments).get(0); Document noComments = removeAlertComment(guid, toRemove.getComment(), toRemove.getUsername(), toRemove.getTimestamp()); Assert.assertEquals(0, getComments(noComments).size()); // ensure the comment was removed from the index Document indexedNoComments = findUpdatedDoc(noComments.getDocument(), withComments.getGuid(), withComments.getSensorType()); Assert.assertEquals(0, getComments(indexedNoComments).size()); }
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> commentMap : commentsMap) { comments.add(new AlertComment(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())); Document newVersion = new Document(latest); if (comments.size() > 0) {
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()));