Danny Saksono
Danny Saksono

Reputation: 21

Primefaces Dynamic Treenode

I am trying to set up a dynamic treenode to accomplish a comment system like Reddit. If you are unfamiliar please refer to this: http://www.reddit.com/r/videos/comments/1wv4tt/guy_runs_on_camera_during_super_bowl_post_game/

What I have in the database now is a comment table that looks like this

id parent content
1 0       text1
2 0       text2
3 1       child of text 1
4 3       child to the child of text 1

Hence the comment panel will look like this

- text1
  - child of text1
    - child to the child of text 1
- text2

I could not visualize how to incorporate this table design with Primefaces Treenode Java code and it would be great if someone can point me to the right direction. I have tried search on the forums and google but couldn't find a solution that I can understand.

I probably should mention that since this is user generated content, I do not have a fixed structure in my table hence the backend code should be able to process this dynamically

I appreciate the help.

Thanks, bhoen

Upvotes: 1

Views: 3361

Answers (1)

hugoleonardomf
hugoleonardomf

Reputation: 31

I solved this way, using recursive method and the table above structure:

public TreeNode createDocuments() {
    TreeNode rootNode = new DefaultTreeNode(new Document(), null);
    List<Document> documentRootNodeList = dao.getDocumentsRoot();
    for (Document doc : documentRootNodeList) {
        TreeNode node = new DefaultTreeNode(doc, rootNode);
        createSubNode(doc, node);
    }
    return rootNode;
}

public void createSubNode(Document doc, TreeNode node) {
    List<Document> documentList = dao.getDocumentsNode(doc);
    for (Document subDoc : documentList) {
        TreeNode subNode = new DefaultTreeNode(subDoc, node);
        createSubNode(subDoc, subNode);
    }
}

Upvotes: 1

Related Questions