Hello Everyone,
I have below use case:
EXISTING CODE:
private static final String MAIN_QUERY = "/jcr:root/content/data/myproject/products//*[@sling:resourceType='myproject/common/components/template1']";
Iterator<Resource> it = this.resourceResolver.findResources(String.format(MAIN_QUERY), XPATH);
int counter = 10000;
while (it.hasNext()) {
resource = it.next();
if (null != resource) {
parentResource = resource.getParent().getParent();
CustomAdaptor myVar = parentResource.adaptTo(CustomAdaptor.class);
}
counter++;
}
Structure in CRX/DE:
content/data/myproject/products/mainproduct1
/childproduct1.1
/childproduct1.2
/childproduct1.3
/childproduct1.4
content/data/myproject/products/mainproduct2
/childproduct2.1
/childproduct2.2
/childproduct2.3
/childproduct2.4
Here, mainproduct1 has sling:resourceType='myproject/common/components/template1 and childproduct1.1 has sling:resourceType='myproject/common/components/template2
NEW CHANGES:
I have to execute query inside mainproduct1/childproduct1/
So, I have created SubQuery for this as:
private static final String SUB_QUERY = "/jcr:root/content/data/myproject/products//*[@sling:resourceType='myproject/common/components/template2']";
Iterator<Resource> itSubProduct = resourceResolver.findResources(SUB_QUERY, "xpath");
while(itSubProduct.hasNext()) {
subResource = itsubProduct.next();
if (null != subResource) {
parentSubResource = subResource.getParent().getParent();
Node node = parentSubResource.adaptTo(Node.class);
if (node.hasNode("jcr:content/main")) {
Node dataNode = node.getNode("jcr:content/main");
if (dataNode.hasProperty("season")) {
myVal = dataNode.getProperty("season").getString();
}
}
}
}
Now, the problem here I am getting is:
Firstly control enters into first "WHILE" loop, do the processing based upon MAIN_QUERY and once it enters into second "WHILE" loop, it doing the processing for all the child nodes (childproduct1.1, childproduct1.2, childproduct2.1, childproduct2.2)
But I want it to process it only for (childproduct1.1, childproduct1.2) and when the second time it comes then do the processing for (childproduct2.1, childproduct2.2)
I have tried many options but not able to achieve this. Can anyone suggest some options please.
Is there any option to append xPath queries?
private static final String MAIN_QUERY = "/jcr:root/content/data/myproject/products//*[@sling:resourceType='myproject/common/components/template1']";
private static final String SUB_QUERY = "/jcr:root/content/data/myproject/products//*[@sling:resourceType='myproject/common/components/template2']";
like
private static final String SUB_QUERY = MAIN_QUERY + //*[@sling:resourceType='myproject/common/components/template2']";
Any help is much appreciated.