I have done similar things in the past using a flat database structure to
build a nested structure.

public class Node {
   private Integer nodeId; // stored in database
   private String description; // stored in database
   private Integer parentId; // stored in database
   private List<Node> childNodes = new ArrayList<Node>(); // calculated
using parentId
}

public class NodeService {
   private List<Node> getAllNodesUnjoined() {
      // get all records from the flat database table
   }

   // use the flat structure to build a nested structure
   public Node getTree() {
      List<Node> nodes = getAllNodesUnjoined();
      Map<Integer, Node> nodesById = new HashMap();
      Node rootNode = null;
      for (Node node : nodes) {
         if (node.getParentId() == null) {
            if (rootNode != null) {
               throw new RuntimeException("More than one root node found");
            }
            rootNode = node;
         }
         nodesById.put(node.getNodeId(), node);
      }
      if (rootNode == null) {
         throw new RuntimeException("No root node found");
      }
      for (Node node : nodes) {
         if (node.getParentId() != null) {
            Node parent = nodesById.get(node.getParentId());
            parent.getChildNodes().add(node);
         }
      }
      return rootNode;
   }
}

On Tuesday, 7 February 2012, George Christman <gchrist...@cardaddy.com>
wrote:
> Hello, I was wondering if someone could help point me in the right
direction
> regarding the db and query needed to generate a database driven tree grid.
>
> This is some code I found between Tapestry-Jquery and JumpStart. I'm not
> sure what the best approach would be to generate this recursive query.
>
> Thanks in advance.
>
>    static {
>        ROOT.addChild(
>                new TreeExample("Renault").addChild(
>                new TreeExample("Mégane")).addChild(
>                new TreeExample("Clio")
>                .addChildrenNamed("Clio Campus", "Clio Sport")))
>                .addChild(new
> TreeExample("Ferarri").addChildrenNamed("F430", "California"));
>
>
>                TreeExample numbers = new TreeExample("Numbers");
>                for (int i = 0; i < 10000; i++) {
>                        numbers.addChild(new
TreeExample(Integer.toString(i)));
>                }
>                ROOT.addChild(numbers);
>    }
>
> --
> View this message in context:
http://tapestry.1045711.n5.nabble.com/Tapestry-TreeGrid-tp5462126p5462126.html
> Sent from the Tapestry - User mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscr...@tapestry.apache.org
> For additional commands, e-mail: users-h...@tapestry.apache.org
>
>

Reply via email to