antoine wrote:
> Sergiu Dumitriu a écrit :
>> antoine SEILLES wrote:
>>   
>>> I have an AnnotationPluginApi.java class and i have a method to get all 
>>> the annotations associated to a document:
>>> public Vector getAnnotations(Document doc){
>>> return doc.getObjects("AnnotationClass");
>>>   }
>>>
>>> Now i'd like to have a method to get all the annotations associated to a 
>>> document having a specific value specified in parameters for one field :
>>> public Vector getAnnotations(Document doc, String field, String value)
>>> Wich should return all the annotations having the value "value" for the 
>>> field "field".
>>>     
>> There is no 1-call way to do this. We have a method that returns one 
>> object based on a property=value constraint (see 
>> com.xpn.xwiki.api.Document#getObject(String, String, String)).
>>
>> So, there are two ways of doing this. One is to write an HQL query for 
>> this, and the other is to manually iterate over the objects.
>>
>> The query approach is much too low level, and depends on the storage 
>> being based on hibernate, so I'll leave this out of the discussion.
>>
>> The second method would be like this:
>>
>> public Vector getAnnotations(Document doc, String field, String value)
>> {
>>    Vector result = new Vector();
>>    Vector allObjects = doc.getObjects(classname);
>>    if (allObjects == null || allObjects.size() == 0) {
>>      return result;
>>    } else {
>>      for (Iterator it = allObjects.iterator(); it.hasNext();) {
>>        BaseObject obj = (BaseObject) it.next();
>>        if (value.equals(((BaseProperty) obj.get(key)).getValue())) {
>>          result.add(newObjectApi(obj, getXWikiContext()));
>>        }
>>      }
>>    }
>>    return result;
>> }
>>
>> You should also add some checks to see if the field, value, and obj.get 
>> are not null, otherwise you might get some exceptions.
>>
>> Sergiu
>>   
> Well i have tried this :
> 
>  public Vector getAnnotations(Document doc, String field, String value) 
> throws XWikiException
>     {
>        Vector result = new Vector();
>        Vector allObjects = doc.getDocument().getObjects("AnnotationClass");
>        if (allObjects == null || allObjects.size() == 0) {
>          return result;
>        } else {
>            int limit = allObjects.size();
>          for (int i=0; i<limit;i++)
>          {
>              BaseObject obj = (BaseObject)allObjects.get(i);
>            if (value.equalsIgnoreCase((String)((BaseProperty) 
> obj.get(field)).getValue()))
>            {
>              result.add(obj);
>            }
>          }
>        }
>        return result;
>     }
> 
> But when i call getAnnotations(Document doc, String field, String value) 
> in velocity i have an error: "Error number 4001 in 4: Error while 
> parsing velocity page Wrapped Exception: Invocation of method 
> 'getRenderedContent' in class com.xpn.xwiki.api.Document threw exception 
> java.lang.NullPointerException @ [28,44"
> 
> And when i try that:
> 
> * *public Vector getAnnotations(Document doc, String field, String 
> value) throws XWikiException
>     {
>        Vector result = new Vector();
>        Vector allObjects = doc.getObjects("AnnotationClass");
>        if (allObjects == null || allObjects.size() == 0) {
>          return result;
>        } else {
>            int limit = allObjects.size();
>          for (int i=0; i<limit;i++)
>          {
>              BaseObject obj = (BaseObject)allObjects.get(i);
>            if (value.equalsIgnoreCase((String)((BaseProperty) 
> obj.get(field)).getValue()))
>            {
>              result.add(obj);
>            }
>          }
>        }
>        return result;
>     }
> 
> I have a cast error : cannot cast Object into BaseObject.
> Problem is BaseObject obj = (BaseObject)allObjects.get(i);
> 

Here, allObjects contains API wrappers, and not internal objects, so you 
don't have com.xpn.xwiki.object.BaseObject there, but 
com.xpn.xwiki.api.Object

In that case you must use this check:
if (value.equalsIgnoreCase((String)((Property) 
obj.getProperty(field)).getValue()))

The problem is that now you will get the same NullPointerException as 
above. I told you to check if the result of obj.get is null, so you must 
do something like:

Property prop = (Property) obj.getProperty(field);
if (prop != null && value.equalsIgnoreCase(prop.getValue())) {


> Anybody has an idea?
> 


-- 
Sergiu Dumitriu
http://purl.org/net/sergiu/
_______________________________________________
devs mailing list
[email protected]
http://lists.xwiki.org/mailman/listinfo/devs

Reply via email to