private void marchTree(List<PermissibleObject> comments, PermissibleObjectTreeNode node) { if (node.getObject() != null) { comments.add(node.getObject()); } // let's sort the children so the list makes sense List<PermissibleObject> permissibleObjects = new ArrayList<PermissibleObject>(node.getChildren().keySet()); Collections.sort(permissibleObjects); for (PermissibleObject object : permissibleObjects) { marchTree(comments, node.getChildren().get(object)); } }
private PermissibleObjectTreeNode findParentCommentNode( PermissibleObjectTreeNode rootNode, PermissibleObjectTreeNode node) { if (node.getObject().equals(rootNode.getObject())) { return rootNode; } for (PermissibleObject obj : rootNode.getChildren().keySet()) { if (obj.equals(((Comment) node.getObject()).getParentComment())) { return rootNode.getChildren().get(obj); } PermissibleObjectTreeNode possibleParent = findParentCommentNode(rootNode.getChildren().get(obj), node); if (possibleParent != null) { return possibleParent; } } return null; }
private List<PermissibleObject> sortComments(List<PermissibleObject> comments) { List<Comment> commentSet = new ArrayList<Comment>(); for (PermissibleObject obj : comments) { Comment comment = (Comment) obj; commentSet.add(comment); Comment parent = comment.getParentComment(); do { if (parent != null) { if (!commentSet.contains(parent)) { commentSet.add(parent); } parent = parent.getParentComment(); } } while (parent != null); } // create bare nodes for each element PermissibleObjectTreeNode rootNode = new PermissibleObjectTreeNode(); for (Comment comment : commentSet) { PermissibleObjectTreeNode node = new PermissibleObjectTreeNode(); node.setObject(comment); if (comment.getParentComment() == null) { if (!rootNode.getChildren().containsKey(comment)) { rootNode.getChildren().put(comment, node); } } else { // try to find parent in rootNode tree PermissibleObjectTreeNode parent = findParentCommentNode(rootNode, node); if (parent == null) { // this nodes parent cannot be found, let's add all of his parents List<Comment> parentComments = new ArrayList<Comment>(); Comment parentComment = comment.getParentComment(); do { if (parentComment != null) { parentComments.add(parentComment); parentComment = parentComment.getParentComment(); } } while (parent != null); if (parentComments.size() == 0) { rootNode.getChildren().put(comment, node); } else { // reverse the order of the list and add/find existing parents Collections.reverse(parentComments); for (Comment myParentComment : parentComments) { PermissibleObjectTreeNode myParentCommentNode = new PermissibleObjectTreeNode(); myParentCommentNode.setObject(myParentComment); PermissibleObjectTreeNode parentParent = findParentCommentNode(rootNode, myParentCommentNode); if (parentParent == null) { rootNode.getChildren().put(myParentComment, myParentCommentNode); } else { if (!parentParent.getChildren().containsKey(myParentComment)) { parentParent.getChildren().put(myParentComment, myParentCommentNode); } } } // we better find it now parent = findParentCommentNode(rootNode, node); parent.getChildren().put(comment, node); } } else { if (!parent.getChildren().containsKey(comment)) { parent.getChildren().put(comment, node); } } } } // march the tree ArrayList<PermissibleObject> list = new ArrayList<PermissibleObject>(); marchTree(list, rootNode); return list; }