On 22.12.10 09:12, "danisevsky" <danisev...@gmail.com> wrote:
>Hi, could you tell me what is the best way how to get count of >subfolders? There may be thousands so I think recursion: > > private int countAllChildren(int c, Node node) { > try { > NodeIterator iterator = node.getNodes(); > while (iterator.hasNext()) { > Node child = (Node) iterator.next(); > if (child.isNodeType("nt:folder")) { > c = countAllChildren(++c, child); > } > } > return c; > } catch (RepositoryException e) { > throw new RuntimeException(e); > } > } > >is not good choice. You don't have to do that yourself, there is the TraversingItemVisitor in the JCR API for that [1]. Have a look at its source though, as the API docs seem to omit the all-important protected methods entering/leaving(). You'd extend from it and increment the count on each entering(Node node, int level) call. Performance or better memory consumption is not an issue unless you have an unusually high tree depth (hundreds of thousands or millions maybe, very very unrealistic for all content I know ;-)). >I would like to use JCR-SQL2 query language, but every query what I >tried didn't work, e.g.: > >SELECT * FROM [nt:folder] WHERE ISCHILDNODE(["+node.getPath()+"]) > >or > >SELECT * FROM [nt:folder] as fld WHERE fld.[jcr:path] LIKE >'"+node.getPath()+"/%' Yeah, SQL-2 is sometimes a bit tricky. In Xpath it would be simple as doing /my/path//* and counting the rows in the returned iterator (getSize() will most likely be -1 as optimization). But this will probably be not as efficient as the traversal approach (search index does not speed up those queries, and you only get all the overhead of the search). Regards, Alex -- Alexander Klimetschek Developer // Adobe (Day) // Berlin - Basel