You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@sling.apache.org by Christophe Lombart <ch...@gmail.com> on 2009/07/01 17:22:48 UTC
Resource properties
For the Sling explorer app, I would like to get the maximum of information
on the resource properties.
I'm using the following code in my esp script :
var valueMap = resource.adaptTo(Packages.java.util.Map);
var propertyIterator = valueMap.keySet().iterator();
After that, I can loop on the propertyIterator to get all properties. That
works fine but I have 3 questions :
1. Is it possible to get the type of a property ?
2. how can I check if the property is a single or multi value prop ?
3. If it is a multi value prop, how can I get the values ?
br,
Christophe
Re: Resource properties
Posted by Felix Meschberger <fm...@gmail.com>.
Hi,
Christophe Lombart schrieb:
> Thanks for the tips.
> Maybe, the Sling explorer has to use (display) an indicator to make the
> difference between the JCR resource & other kind of resources. By this way,
> we can display different kind of info in function of the resource type.
Makes sense to me.
Regards
Felix
>
>
>
> 2009/7/2 Felix Meschberger <fm...@gmail.com>
>
>> Hi,
>>
>> Juan José Vázquez Delgado schrieb:
>>> Hi,
>>>
>>>> var prop = valueMap.get("theName", Packages.javax.jcr.Property);
>>>> if (prop != null) {
>>>> ....
>>>> }
>>> This only applies to JCR based resources, isn´t it?. AFAIK, there is
>>> no a standard way to retrieve the type and multivalue feature from
>>> non-JCR based resources.. Am I right?.
>> Yes, it is ok for this method to return null if a value exists which may
>> not be converted to a JCR Property instance. Therefore the null check.
>>
>> For non-JCR properties, I would say (in Java speak) something like this
>> might work:
>>
>> boolean isMulti = false;
>> Class type = null;
>>
>> Object value = map.get("theName");
>> if (value == null) {
>> ...
>> } else {
>> type = value.getClass();
>> if (type.isArray()) {
>> isMulti = true;
>> type = type.getComponentType();
>> }
>>
>> Regards
>> Felix
>>
>>
>
Re: Resource properties
Posted by Juan José Vázquez Delgado <ju...@gmail.com>.
>> > Thanks for the tips.
>> > Maybe, the Sling explorer has to use (display) an indicator to make the
>> > difference between the JCR resource & other kind of resources. By this
>> way,
>> > we can display different kind of info in function of the resource type.
>>
>> This sounds we´ll ended up with hundreds of "if..then", one by
>> resource type we have. What about a certain kind of pluggable renderer
>> based on the resource type?. Each renderer might be an OSGi bundle or
>> be included in the corresponding resource provider. WDYT?
>
>
> Yes This is a good plan for this application.
> In the first time, I will share my work asap without this option.
> Later, we will check together how we will implement this kind of
> renderers.
Great!.
Juanjo.
Re: Resource properties
Posted by Christophe Lombart <ch...@gmail.com>.
2009/7/2 Juan José Vázquez Delgado <ju...@gmail.com>
> > Thanks for the tips.
> > Maybe, the Sling explorer has to use (display) an indicator to make the
> > difference between the JCR resource & other kind of resources. By this
> way,
> > we can display different kind of info in function of the resource type.
>
> This sounds we´ll ended up with hundreds of "if..then", one by
> resource type we have. What about a certain kind of pluggable renderer
> based on the resource type?. Each renderer might be an OSGi bundle or
> be included in the corresponding resource provider. WDYT?
Yes This is a good plan for this application.
In the first time, I will share my work asap without this option.
Later, we will check together how we will implement this kind of
renderers.
>
>
> Regards,
>
> Juanjo.
>
Re: Resource properties
Posted by Juan José Vázquez Delgado <ju...@gmail.com>.
> Thanks for the tips.
> Maybe, the Sling explorer has to use (display) an indicator to make the
> difference between the JCR resource & other kind of resources. By this way,
> we can display different kind of info in function of the resource type.
This sounds we´ll ended up with hundreds of "if..then", one by
resource type we have. What about a certain kind of pluggable renderer
based on the resource type?. Each renderer might be an OSGi bundle or
be included in the corresponding resource provider. WDYT?
Regards,
Juanjo.
Re: Resource properties
Posted by Christophe Lombart <ch...@gmail.com>.
Thanks for the tips.
Maybe, the Sling explorer has to use (display) an indicator to make the
difference between the JCR resource & other kind of resources. By this way,
we can display different kind of info in function of the resource type.
2009/7/2 Felix Meschberger <fm...@gmail.com>
> Hi,
>
> Juan José Vázquez Delgado schrieb:
> > Hi,
> >
> >> var prop = valueMap.get("theName", Packages.javax.jcr.Property);
> >> if (prop != null) {
> >> ....
> >> }
> >
> > This only applies to JCR based resources, isn´t it?. AFAIK, there is
> > no a standard way to retrieve the type and multivalue feature from
> > non-JCR based resources.. Am I right?.
>
> Yes, it is ok for this method to return null if a value exists which may
> not be converted to a JCR Property instance. Therefore the null check.
>
> For non-JCR properties, I would say (in Java speak) something like this
> might work:
>
> boolean isMulti = false;
> Class type = null;
>
> Object value = map.get("theName");
> if (value == null) {
> ...
> } else {
> type = value.getClass();
> if (type.isArray()) {
> isMulti = true;
> type = type.getComponentType();
> }
>
> Regards
> Felix
>
>
Re: Resource properties
Posted by Felix Meschberger <fm...@gmail.com>.
Hi,
Juan José Vázquez Delgado schrieb:
> Hi,
>
>> var prop = valueMap.get("theName", Packages.javax.jcr.Property);
>> if (prop != null) {
>> ....
>> }
>
> This only applies to JCR based resources, isn´t it?. AFAIK, there is
> no a standard way to retrieve the type and multivalue feature from
> non-JCR based resources.. Am I right?.
Yes, it is ok for this method to return null if a value exists which may
not be converted to a JCR Property instance. Therefore the null check.
For non-JCR properties, I would say (in Java speak) something like this
might work:
boolean isMulti = false;
Class type = null;
Object value = map.get("theName");
if (value == null) {
...
} else {
type = value.getClass();
if (type.isArray()) {
isMulti = true;
type = type.getComponentType();
}
Regards
Felix
Re: Resource properties
Posted by Juan José Vázquez Delgado <ju...@gmail.com>.
Hi,
> var prop = valueMap.get("theName", Packages.javax.jcr.Property);
> if (prop != null) {
> ....
> }
This only applies to JCR based resources, isn´t it?. AFAIK, there is
no a standard way to retrieve the type and multivalue feature from
non-JCR based resources.. Am I right?.
Regards,
Juanjo.
Re: Resource properties
Posted by Felix Meschberger <fm...@gmail.com>.
Hi,
Christophe Lombart schrieb:
> For the Sling explorer app, I would like to get the maximum of information
> on the resource properties. I'm using the following code in my esp script
> :
>
> var valueMap = resource.adaptTo(Packages.java.util.Map);
You might want to do:
var valueMap =
Packages.org.apache.sling.api.resource.ResourceUtil.getValueMap(resource);
This gives you a ValueMap
> var propertyIterator = valueMap.keySet().iterator();
> After that, I can loop on the propertyIterator to get all properties. That
> works fine but I have 3 questions :
>
try
var prop = valueMap.get("theName", Packages.javax.jcr.Property);
if (prop != null) {
....
}
> 1. Is it possible to get the type of a property ?
var type = prop.getType();
> 2. how can I check if the property is a single or multi value prop ?
var isMulti = prop.getDefinition().isMultiple();
> 3. If it is a multi value prop, how can I get the values ?
var vals = prop.getValues();
Or better yet, the ValueMap offers you transparent conversion as in :
var stringVals = valueMap.get("theName", java.lang.String[]);
This gives you either null or a String[] regardless of whether the
property is really multi-val or not and what the actual property type is.
HTH
Regards
Felix
>
>
> br,
> Christophe
>
Resource properties
Posted by Christophe Lombart <ch...@gmail.com>.
For the Sling explorer app, I would like to get the maximum of information
on the resource properties. I'm using the following code in my esp script
:
var valueMap = resource.adaptTo(Packages.java.util.Map);
var propertyIterator = valueMap.keySet().iterator();
After that, I can loop on the propertyIterator to get all properties. That
works fine but I have 3 questions :
1. Is it possible to get the type of a property ?
2. how can I check if the property is a single or multi value prop ?
3. If it is a multi value prop, how can I get the values ?
br,
Christophe