AnsweredAssumed Answered

A better, faster way to do this?

Question asked by hsjawanda on Sep 16, 2005
Hi all,

I want to get a hold of a folder node, and then construct a tree of all folder nodes (and only folders) under it. The method reproduced below is called with a NodeRef to the root node of the tree I want to construct, and it does the rest.


    private DefaultMutableTreeNode getTreeAt(NodeRef node) {
        if (!nodeService.getType(node).equals(ContentModel.TYPE_FOLDER))
            return null;
        List<ChildAssociationRef> children;
        int cntr, numChildren = 0;
        NodeRef child;
        CMSFolder folder = new CMSFolder(node, objectToString(nodeService
                .getProperty(node, ContentModel.PROP_NAME)));
        DefaultMutableTreeNode currTreeNode = new DefaultMutableTreeNode(folder);

        // Need to create a new list because the list returned by nodeService
        // is unmodifiable, hence unsortable.
        children = new ArrayList<ChildAssociationRef>(nodeService
                .getChildAssocs(node));
        numChildren = children.size();
        if (numChildren > 0) {
            if (numChildren > 1) {
                // Siblings are sorted in lexicographical order of their names
                // (ContentModel.PROP_NAME).
                Collections.sort(children, new ChildAssocRefComparator(
                        nodeService));
            }
            for (cntr = 0; cntr < numChildren; cntr++) {
                child = children.get(cntr).getChildRef();
                if (nodeService.getType(child).equals(ContentModel.TYPE_FOLDER)) {
                    currTreeNode.add(getTreeAt(child));
                }
            }
        }
        return currTreeNode;
    }

My question is: is there a better/faster way to do this? Primarily, I am concerned about all the calls to NodeService (there are more NodeService calls hidden inside the comparator too), which in turn equates to a lot of DB calls.

The folder tree I am generating could reach ~1000 folders, and this method will be called often, so performance is important.  Even with just ~15 folders, the initial call to getTreeAt() takes ~1 second to return.

The DefaultMutableTreeNode above is the javax.swing.tree.DefaultMutableTreeNode.

Thanks.

Outcomes