Sergiu Dumitriu a écrit :
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".
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())) {
Thanks. I have the following code and it works: 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++) { Object obj = ((Object)allObjects.get(i)); if(obj != null) { Property prop = (Property) obj.getProperty(field); if (prop != null && prop.getValue()!=null && value.equalsIgnoreCase((String)prop.getValue())) { result.add(obj); } } } } return result; }