On Thu, Sep 13, 2012 at 12:14 AM, Jerome Velociter <[email protected]> wrote:
Hi Paul,
On 09/12/2012 11:47 PM, Paul Libbrecht wrote:
hello fellow developers,
at Curriki, we are starting to process XWiki objects in the background using subclasses of AbstractXWikiRunnable.
Currently I do the following:
// setup XWikiContext xcontext = (XWikiContext) Utils.getComponent(Execution.class).getContext() .getProperty(XWikiContext.EXECUTIONCONTEXT_KEY); Context context = new Context(xcontext); XWiki xwiki = new com.xpn.xwiki.api.XWiki(context.getXWiki(), xcontext);
// fetch background info Document d = xwiki.getDocument(DOCNAME_x);
Object obj = d.getObject("DOCNAME_class"); // modify something
// save d.save("comment");
However, I wonder if this is the best practice:
There is no official best practice actually so it's going to be my personal point of view.
- is it expensive to construct a context and xwiki supposing this setup code is run multiple times?
As far as I know there is not much overhead to construct the api wrapper.
Not by creating the API wrapper itself but keep in mind that every time you call some of the script API methods (like save and getDocument in your example) you will have a right test behind the scene for many of them.
- is it preferrable to work with the com.xpn.xwiki.* classes?
IMO it mostly depends if you want right test or not. For example in your example your save will not work if whatever user you have in your context does not have edit right on that document (same thing with getDocument and view right). Sometimes that's exactly what you want (we use the public API a lot in the REST resources for example) and sometime it's a daemon thread which doesn't care about rights. If you are in a Java code where you know you can do everything and don't care about the rights you should use the com.xpn.xwiki.* classes. Those class have the same level of retro compatibility than the api.* classes so that should not be an issue.
Now I tend to prefer working as much as possible with api.* classes rather than internal classes, for no specific reason other than the fact they are proper APIs vs. de facto APIs and that I'm sure I have the same behavior as when writing scripts.
- is DocReference behaving much differently than fullName if we have a single wiki?
Yes in the sense it handles escaping properly. It matters if you don't have control over document names, it matters less if you do have this control (for example you generate the name automatically or clean user input with defined rules). I try to use the reference (a.k.a new model) API as often as possible, but I admit sometimes it feels just too much when you know you're not having escaping issues or multiwiki issues. Those times I do things like xwiki.getDocument("XWiki.XWikiPreferences") and just live with it happily :) Of course I'm speaking about my experience of using XWiki for my own needs as an API consumer, not about the development of the XWiki platform.
Not much to add here but I think I'm using document reference way more than Jerome even wen I "control the name" ;) When you do xwiki.getDocument("XWiki.XWikiPreferences") a resolver is called to parse ir and resolve the current wiki to create the DocumentReference so it's better to create a DocumentReference directly when you know all the three parts.
That was my 2 cents, hope it helped having perspective on what others do, Jerome
Thanks in advance.
Paul _______________________________________________ devs mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/devs
-- Peace, —Jerome
_______________________________________________ devs mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/devs
-- Thomas Mortagne