[xwiki-devs] Creating new pages from components with no old xwiki core dependency
Hi, Is it possible to write XWiki components that create new pages without having as a dependency the old xwiki core? Let's say the component currently has the following code snippet: XWiki xwiki = xwikiContext.getWiki(); XWikiDocument newDocument = new XWikiDocument(); newDocument.setFullName(documentName, xwikiContext); newDocument.setContent(templateDoc.getContent()); newDocument.setSyntaxId("xwiki/2.0"); newDocument.setCreator(xwikiContext.getLocalUser()); newDocument.setAuthor(xwikiContext.getLocalUser()); newDocument.setTitle(someString); xwiki.saveDocument(newDocument, xwikiContext); How should the code be written so it will be easier to change when the code refactoring comes to an end? Thanks, Anamaria
Hi Anamaria, On Tue, Jan 5, 2010 at 8:20 PM, Anamaria Stoica <[email protected]>wrote:
Hi,
Is it possible to write XWiki components that create new pages without having as a dependency the old xwiki core?
Let's say the component currently has the following code snippet:
XWiki xwiki = xwikiContext.getWiki(); XWikiDocument newDocument = new XWikiDocument(); newDocument.setFullName(documentName, xwikiContext); newDocument.setContent(templateDoc.getContent()); newDocument.setSyntaxId("xwiki/2.0"); newDocument.setCreator(xwikiContext.getLocalUser()); newDocument.setAuthor(xwikiContext.getLocalUser()); newDocument.setTitle(someString);
xwiki.saveDocument(newDocument, xwikiContext);
How should the code be written so it will be easier to change when the code refactoring comes to an end?
You can use the xwiki-bridge module to accomplish this. Now I haven't used the new xwiki-model component myself a lot but if you have a look at DocumentAccessBridge in xwiki-bridge component, you'll see how to do this. - Asiri
Hi Anamaria, On 01/05/2010 04:50 PM, Anamaria Stoica wrote:
Hi,
Is it possible to write XWiki components that create new pages without having as a dependency the old xwiki core?
Let's say the component currently has the following code snippet:
XWiki xwiki = xwikiContext.getWiki(); XWikiDocument newDocument = new XWikiDocument(); newDocument.setFullName(documentName, xwikiContext); newDocument.setContent(templateDoc.getContent()); newDocument.setSyntaxId("xwiki/2.0"); newDocument.setCreator(xwikiContext.getLocalUser()); newDocument.setAuthor(xwikiContext.getLocalUser()); newDocument.setTitle(someString);
xwiki.saveDocument(newDocument, xwikiContext);
In xwiki, when you do getDocument(String docName) you will get a document even if the document does not exist, you'll get a new one. You can test this by checking XWikiDocument#isNew(). So, normally it will create a new document if you do DocumentAccessBridge#getDocument(documentName); however it should not be saved until a saveDocument() is called on that document. The way to do this with the bridge is to set values for the properties of that document or objects in it and it will be saved. There is no explicit saveDocument in the bridge because every operation on the bridge should be 'atomic', no operations should leave things changed and not persisted. I don't really like the approach I just described, because it's very hacky, and you won't be able to set all the properties you need anyway, you'll need to add a bunch of functions to the bridge.
How should the code be written so it will be easier to change when the code refactoring comes to an end?
What you can do to make things easier for you, is to isolate all the code that needs to handle storage (creating documents, objects or others) to a single service in your code, and make only the implementation of that service depend on the old core. This way, when the refactoring will all be done, you only need to change your storage related impl and you're done. Happy coding, Anca
Thanks, Anamaria _______________________________________________ devs mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/devs
participants (3)
-
Anamaria Stoica -
Anca Luca -
Asiri Rathnayake