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

> How to do that?
> In fact, how to access fields of my objects "AnnotationClass"?
> 
> Thank for your time.
> 
> Ps: in AnnotationPlugin i have this method:
> 
> public BaseClass getAnnotationClass(XWikiContext context) throws 
> XWikiException {
>       XWikiDocument doc;
>       XWiki xwiki = context.getWiki();
>       boolean needsUpdate = false;
> 
>       try {
>           doc = xwiki.getDocument("XWiki.AnnotationClass", context);
>       } catch (Exception e) {
>           doc = new XWikiDocument();
>           doc.setSpace("XWiki");
>           doc.setName("AnnotationClass");
>           needsUpdate = true;
>       }
> 
>       BaseClass bclass = doc.getxWikiClass();
>       bclass.setName("XWiki.AnnotationClass");
>       needsUpdate |= bclass.addTextField("author", "Author", 30);
>       needsUpdate |= bclass.addDateField("created", "Created", 
> "dd/MM/yyyy");
>       needsUpdate |= bclass.addTextAreaField("comment", "Comment", 40, 5);
>       needsUpdate |= bclass.addTextAreaField("selection", "Selection", 
> 40, 5);
>       needsUpdate |= bclass.addTextAreaField("reformulation", 
> "Reformulation", 40, 5);
>       needsUpdate |= bclass.addNumberField("begin", "Begin", 100, 
> "integer");
>       needsUpdate |= bclass.addNumberField("end", "End", 100, "integer");
>       needsUpdate |= bclass.addTextField("emotion", "Emotion", 30);
>       needsUpdate |= bclass.addTextField("judgmentUniversel", 
> "JudgmentUniversel", 30);
>       needsUpdate |= bclass.addTextField("judgmentParticulier", 
> "JudgmentParticulier", 30);
>       needsUpdate |= bclass.addTextField("domain", "Domain", 30);
> 
>       String content = doc.getContent();
>       if ((content == null) || (content.equals(""))) {
>           needsUpdate = true;
>           doc.setContent("1 AnnotationClass");
>       }
> 
>       if (needsUpdate)
>           xwiki.saveDocument(doc, context);
>       return bclass;
>   }

_______________________________________________
devs mailing list
[email protected]
http://lists.xwiki.org/mailman/listinfo/devs

Reply via email to