Iterate the property in efficient way | Community
Skip to main content
JakeCham
Level 6
July 13, 2023
Solved

Iterate the property in efficient way

  • July 13, 2023
  • 3 replies
  • 1664 views

Hi Team, I have a requirement where my client wants one of my component to be appeared in customize way in page source(not the way it is rendering on page source as per the component rendering ) So I have to call a model class in footerlibs and iterate through the node of the component and making data available as he wished in page source. Node structure in jcr like below 

 

 

as per above screen shot assume my component is name is "Acomponent". So I want to get the data on Acomponent where the first time my code got it.as per above ss it is "jcr:content/par/Acomponent"

Node parentNode = here Im getting the current page path and getting the node till pagepath+jcr:content

if(null!=ParentNode){

  NodeIterator it = parentNode.getNodes("par*");

//instead of getting all the parentnodes including (par,custompar,par1) I want to get the node starting par (meant I want to get the par,par1 and par2 etc) and do the iteration further. But I tried NodeIterator it = parentNode.getNodes("par*"); and it is always getting Node named "par" only. here Im tryinng to skip of getting all the parent nodes like (custompar) for the efficiency of the code need to get the (par and par1). Any idea why it cant be done ?

}

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Tanika02

Hello @jakecham - 

 

Have you tried below approach : 

 

  • If the parentNode.getNodes("par*") call is only returning a single node named "par" instead of all nodes starting with "par", it could be due to the implementation of the JCR repository you are using.

 

 

if (parentNode != null) { NodeIterator it = parentNode.getNodes(); while (it.hasNext()) { Node node = it.nextNode(); String nodeName = node.getName(); // Check if the node starts with "par" and is not "custompar" if (nodeName.startsWith("par") && !nodeName.equals("custompar")) {\ } } }

 

  OR

 

import javax.jcr.NodeIterator; import javax.jcr.Session; import javax.jcr.query.Query; import javax.jcr.query.QueryManager; // Assuming you have access to the JCR Session Session session = ...; // Construct the SQL2 query to retrieve nodes starting with "par" String queryStatement = "SELECT * FROM [nt:base] WHERE ISDESCENDANTNODE('" + parentNode.getPath() + "') AND NAME() LIKE 'par%' AND NAME() <> 'custompar'"; try { QueryManager queryManager = session.getWorkspace().getQueryManager(); Query query = queryManager.createQuery(queryStatement, Query.JCR_SQL2); NodeIterator result = query.execute().getNodes(); while (result.hasNext()) { Node node = result.nextNode(); // Perform your desired logic with the node // For example, access properties or iterate through child nodes } } catch (Exception e) { // Handle any exceptions that may occur during the query execution }

 

3 replies

Sudheer_Sundalam
Community Advisor
Community Advisor
July 13, 2023

@jakecham ,

 

Try "getChildNodes()" with pattern. 

NodeIterator it = parentNode.getChildNodes("par*");

If the above doesn't work, please take a look at JcrUtils opensource library

public static Iterable<javax.jcr.Node> getChildNodes(javax.jcr.Node node, String pattern)JcrUtils.getChildNodes(parentNode, "par*");

 

Hope this helps!

 

JakeCham
JakeChamAuthor
Level 6
July 13, 2023

Hi @sudheer_sundalam 

 

getChileNode method is not there in my 6.4sp.

 

To go on below way can u explain how to iterate this to get a Node please ?

JcrUtils.getChildNodes(parentNode, "par*");

 

 

 

Sudheer_Sundalam
Community Advisor
Community Advisor
July 13, 2023

Here is the example:

 

Node parentNode = resourceResolver.getResource("path").adaptTo(Node.class); Iterator<Node> childNodes = JcrUtils.getChildNodes(parentNode, "par*").iterator(); while(childNodes.hasNext()) { Node node = childNodes.next(); //Do your stuff }
DPrakashRaj
Community Advisor
Community Advisor
July 13, 2023

Why don’t you run a query to filter the node based on sling:resourceType to be your component path for your page.once you get the reference of all of your component node you can iterate them any way as you want 

Tanika02
Tanika02Accepted solution
Level 7
July 14, 2023

Hello @jakecham - 

 

Have you tried below approach : 

 

  • If the parentNode.getNodes("par*") call is only returning a single node named "par" instead of all nodes starting with "par", it could be due to the implementation of the JCR repository you are using.

 

 

if (parentNode != null) { NodeIterator it = parentNode.getNodes(); while (it.hasNext()) { Node node = it.nextNode(); String nodeName = node.getName(); // Check if the node starts with "par" and is not "custompar" if (nodeName.startsWith("par") && !nodeName.equals("custompar")) {\ } } }

 

  OR

 

import javax.jcr.NodeIterator; import javax.jcr.Session; import javax.jcr.query.Query; import javax.jcr.query.QueryManager; // Assuming you have access to the JCR Session Session session = ...; // Construct the SQL2 query to retrieve nodes starting with "par" String queryStatement = "SELECT * FROM [nt:base] WHERE ISDESCENDANTNODE('" + parentNode.getPath() + "') AND NAME() LIKE 'par%' AND NAME() <> 'custompar'"; try { QueryManager queryManager = session.getWorkspace().getQueryManager(); Query query = queryManager.createQuery(queryStatement, Query.JCR_SQL2); NodeIterator result = query.execute().getNodes(); while (result.hasNext()) { Node node = result.nextNode(); // Perform your desired logic with the node // For example, access properties or iterate through child nodes } } catch (Exception e) { // Handle any exceptions that may occur during the query execution }

 

JakeCham
JakeChamAuthor
Level 6
July 14, 2023

Thank you @tanika02 .. This helps..