Java: how to check if a page (defined as cq:Page) has a custom property? | Community
Skip to main content
jayv25585659
Level 8
Jan 25, 2018 5:52 AM UTC
Solved

Java: how to check if a page (defined as cq:Page) has a custom property?

To achieve my question above, I used querybuilder that retrieves custom pages and then I map these maps to a model file.

My problem is that in some instances, there are pages without this custom property (as an example, let's call it aNew which can be either true or false and of boolean property).

My issue is that when I "cast" the search result hits to my model, aNew automatically get "false" for pages that doesn't have this property.

Any ideas how it can be done?

Map<String, String> map = new HashMap<>();

map.put("path", "/content/data/fishes");

map.put("type", "cq:Page");

map.put("p.limit", "1");

QueryBuilder builder = getResourceResolver().adaptTo(QueryBuilder.class);

Query query = builder.createQuery(PredicateGroup.create(map), getResourceResolver().adaptTo(Session.class));

SearchResult searchResult = query.getResult();

if (searchResult.getTotalMatches() > 0) {

    List<Hit> hits = searchResult.getHits();

  

    Resource resource = hits.get(0).getResource();

    Course tempCourse = resource.adaptTo(Course.class);

}

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
smacdonald2008
smacdonald2008Accepted solution
Level 10
Jan 25, 2018 4:42 PM UTC

Convert to node and then invoke the method hasProperty - Node (Content Repository for Java Technology API Version 2.0)

This will tell you if the page has the custom property.

3 replies

smacdonald2008
smacdonald2008Accepted solution
Level 10
Jan 25, 2018 4:42 PM UTC

Convert to node and then invoke the method hasProperty - Node (Content Repository for Java Technology API Version 2.0)

This will tell you if the page has the custom property.

Translate
rmahendra
Employee
Jan 25, 2018 1:33 PM UTC

You can add a default value to the property in the Course sling model which will be returned if the property is not present.

@ValueMapValue

@Default(booleanValues = true)

boolean aNew;

Translate
edubey
Level 10
Jan 25, 2018 6:58 AM UTC

Query builder never modifies the JCR content

What do you mean by "Cast"?

false property would exists before executing the query.

Translate