Expand my Community achievements bar.

SOLVED

Dealing with Querybuilder results

Avatar

Level 2

Hi, Newbie question here. When using the query builder interface and get a set of results. I use something like this:

for (Hit hit : result.getHits()) { ValueMap props = hit.getProperties(); String title = props.get("jcr:title").toString(); }

However if the property is an array (such as props.get("cq:tags") ), it returns what appears to be a generic object. How do I extract the string[] value from that object? to get the contents?

 

Thanks,

- Dmitry

1 Accepted Solution

Avatar

Correct answer by
Employee

Hi Dmitry,

The ValueMap interface handles type coercion automatically, you just need to tell it what to do:

String[] values = props.get("jcr:title", String[].class); // will return null if there's no property named "jcr:title"

OR

String[] values = props.get("jcr:title", new String[0]); // will return an empty array if jcr:title isn't a property name

In your example, you should be providing a default value. Otherwise, you could get a null pointer exception if the property doesn't exist.

Regards,
Justin

View solution in original post

2 Replies

Avatar

Correct answer by
Employee

Hi Dmitry,

The ValueMap interface handles type coercion automatically, you just need to tell it what to do:

String[] values = props.get("jcr:title", String[].class); // will return null if there's no property named "jcr:title"

OR

String[] values = props.get("jcr:title", new String[0]); // will return an empty array if jcr:title isn't a property name

In your example, you should be providing a default value. Otherwise, you could get a null pointer exception if the property doesn't exist.

Regards,
Justin