[xwiki-devs] annotation plugin: how to access properties of a plugin
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". 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; }
Hi Antoine, To access properties you should do something like: Object obj = (Object) doc.getObjects("XWiki.AnnotationClass").get(0); doc.use(obj); doc.display("comment"); doc.getValue("comment"); doc.displat("comment","edit"); etc... Ludovic 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".
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
-- Ludovic Dubost Blog: http://blog.ludovic.org/ XWiki: http://www.xwiki.com Skype: ldubost GTalk: ldubost
Ludovic Dubost a écrit :
Hi Antoine,
To access properties you should do something like:
Object obj = (Object) doc.getObjects("XWiki.AnnotationClass").get(0); doc.use(obj); doc.display("comment"); doc.getValue("comment"); doc.displat("comment","edit");
etc...
Ludovic
Hi, That's a solution i had tested, to use velocity to access properties. But i had like to create my own method in java code. Indeed, i want to list all the annotation written by an user (or something like that) and so i'd like to implement a method getUserAnnotation(username) in java code and then call this method in velocity script. Thank for your help.
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".
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
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; }
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); Anybody has an idea? Thank. * *
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/
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; }
participants (4)
-
antoine -
antoine SEILLES -
Ludovic Dubost -
Sergiu Dumitriu