1

I want to check that whether my page contains a particular resource or not. If the resource is only once on the page then that can be check by using

page.getContentResource("yourResource");

If it is null means resource is not present. But problem is I have multiple resource on my page.like yourResource_1,yourResource_2,yourResource_3... and if i delete the yourResource but still i have yourResource_1,yourResource_2,yourResource_3. then also i returns null because it is only checking for "yourResource"

any suggestion ?

user2142786
  • 1,484
  • 9
  • 41
  • 74

3 Answers3

1

If you do not know the name(s) of all the potential child resources of your page's jcr:content node, you can use the snippet below to check if the jcr:content node has any children.

import javax.jcr.Node

boolean hasChildren = page.getContentResource().adaptTo(Node.class).hasNodes();
0

Small remark, getContentResource only returns immediate children of jcr:content, though you can use a relative path like par/yourResource. I think the only way to to this is a recursive drill down the resource and check for the resourceType. Something on the line of this (untested):

private boolean checkType(Resource res) {
    if (res.isResourceType("yourResource") {
        return true;
    }
    boolean hasType = false;
    Iterator<Resource> children = res.listChildren();
    while (children.hasNext()) {
        hasType |= checkType(children.next());
    }
    return hasType;
}
Thomas
  • 6,325
  • 4
  • 30
  • 65
  • ok i'll try it.. do u have any idea about this one. http://stackoverflow.com/questions/30587983/authorisation-issue-while-accessing-a-page-from-repository-in-cq5 – user2142786 Jun 02 '15 at 07:03
0

You can adaptTo() ValueMap and check if the map has any properties i.e. isEmpty():

chrysler
  • 448
  • 3
  • 5