aemjcrslingdamxpathnodeiterator

CQ Jcr node how to resolve Sling:OrderedFolder jcr:primarytype and get children nodes


enter image description here

the path for above structure's parent: pdf folder, is -> /content/dam/example/corporate/pdf and pdf folder's value of jcr:primarytype is Sling:OrderedFolder ,so does the other two folder '2013' '2014'... enter image description here

if the parent 'pdf' is a page type, i know exactly how to iterate through a page structure and retrieve children node's property information by using jcr node api.

but unfortunately 'pdf' is an orderedfolder type...

what i need now is, to iterate through the structure and retrieve ALL PDFs path and store into a ArrayList, for example, the path of '2013exampleOne.pdf' node is /content/dam/example/corporate/pdf/2013exampleOne.pdf , and path of '2014examplePDFTwo.pdf' is /content/dam/example/corporate/pdf/2014examplePDFTwo.pdf ...etc... (this list may server other part of the application to download pdf later, but this has nothing to do with my current question...)

here is my own code for attemping (please notice, this is only part of my servlet, ):

   @Override
        protected void doGet(final SlingHttpServletRequest request,
                final SlingHttpServletResponse response) throws ServletException,
                IOException {
....//some more code
....
....
....
            final ResourceResolver resourceResolver = request.getResourceResolver();
            Node pdfJcrNode = resourceResolver.resolve("/content/dam/example/corporate/pdf").adaptTo(Node.class);

            NodeIterator yearChildrenNodes;
            try {
                yearChildrenNodes = pdfJcrNode.getNodes();
                while(yearChildrenNodes.hasNext()){
                    Node yearItemNode = yearChildrenNodes.nextNode();
                    while(yearItemNode.getNodes().hasNext()){
                        String pdfNodeAssetString = yearItemNode.getNodes().nextNode().getProperty("jcr:primaryType").getString();
                        if(pdfNodeAssetString.equals("dam:Asset")){
                            pdfPathList.add(yearItemNode.getNodes().nextNode().getPath().toString());
                        }
                    }
                }
            } catch (RepositoryException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
....
....
....
....//some more code
}

once again, the path of the parent folder 'pdf' is /content/dam/example/corporate/pdf

any suggestions to help me? thanks

ps: please provide code example to help, cheers


Solution

  • Why not use a query?

    XPATH:

    /jcr:root/content/dam/example/corporate/pdf//*[jcr:primaryType='dam:Asset']
    

    You can use the QueryManager like this:

    Session session = resourceResolver.adaptTo(Session.class)   
    QueryManager qm = session.getWorkspace().getQueryManager();
    Query q = qm.createQuery("/jcr:root/content/dam/example/corporate/pdf//*[jcr:primaryType='dam:Asset']", "xpath");
    QueryResult result = q.execute();
    NodeIterator nIt = result.getNodes();
    

    With this you can iterate over the Nodes.

    Also small side note to your code, you are using nextNode() twice. So you are iterating further without checking if there is a nextNode.