[xwiki-devs] Creating structured objects
Hi, In one of your tutorials (referencing FAQ application) you have shown how to create custom classes, add properties to it and create objects of these classes. This was well understood and thanks for the easy to understand tutorial. Following the steps I create a space called aeternam and added a page SupplierClass under it. Further I create a page "Suppliers" under aeternam space, on which I pasted the velocity template code to create objects of the SupplierClass and view create objects. All works fine. Only problem is the objects are created under the parent SupplierClass and not under parent Suppliers. I have explicitly mentioned the parent using the code: #set( $class = "Supplier") #set( $defaultparent = "Suppliers") #set( $defaultweb = "aeternam") #set( $doc1new = $xwiki.getDocument("${doc.space}.${class}ClassSheet").isNew()) #set( $doc2new = $xwiki.getDocument("${doc.space}.${class}ClassTemplate").isNew()) #if (!($doc1new||$doc2new)) 1.1 Create a new Supplier To create a new supplier, you should first choose a name for your supplier. <form action="" id="newdoc" method="post"> <div> <input type="hidden" name="parent" value="${defaultparent}"/> <input type="hidden" name="template" value="${doc.web}.${class}ClassTemplate"/> <input type="hidden" name="sheet" value="1"/> Space: <input type="text" readonly="true" name="webname" value="${defaultweb}" size="8"/> Supplier: <input type="text" name="name" value="Supplier Name"/> <br/> <br/> ## TODO: remove this onclick <input type="button" value="Create this supplier" onclick='if (updateName(this.form.name)) {this.form.action="../../inline/" + this.form.webname.value + "/" + this.form.name.value; this.form.submit(); }'/> </div> </form> 1.1 Existing Suppliers #set ($sql = ", BaseObject as obj where obj.name=doc.fullName and obj.className='${doc.web}.${class}Class' and obj.name<>'${doc.web}.${class}ClassTemplate'") #foreach ($item in $xwiki.searchDocuments($sql)) * [$item] #end #end So what do I have to do to create objects of a class under some other parent page not under the default class page. Next after a document object is created I would like to update the same document, just like in create is there a template or tutorial that would help me in understanding how to update a object, so as to complete the CRUD flow. Thanks Sachin -- View this message in context: http://www.nabble.com/Creating-structured-objects-tp14268015p14268015.html Sent from the XWiki- Dev mailing list archive at Nabble.com.
One thing is see is that default parent has to be fully qualified page name. So it should be aeternam.Suppliers and not Suppliers. Even when I make this change still the object for supplier is created under aeternam.SupplierClass and not under aeternam.Suppliers. When I go and edit this new object (wiki) the on right I see parent as aeternam.SupplierClass. So now there I change this to aeternam.Suppliers, the created supplier object moves to Suppliers page. What I have to do is to create this object directly under aeternam.Suppliers. I believed setting: #set( $defaultparent = "aeternam.Suppliers") should have worked but its not. Thanks Sachin Sachn Mittal wrote:
Hi, In one of your tutorials (referencing FAQ application) you have shown how to create custom classes, add properties to it and create objects of these classes. This was well understood and thanks for the easy to understand tutorial. Following the steps I create a space called aeternam and added a page SupplierClass under it.
Further I create a page "Suppliers" under aeternam space, on which I pasted the velocity template code to create objects of the SupplierClass and view create objects.
All works fine. Only problem is the objects are created under the parent SupplierClass and not under parent Suppliers.
I have explicitly mentioned the parent using the code:
#set( $class = "Supplier") #set( $defaultparent = "Suppliers") #set( $defaultweb = "aeternam")
#set( $doc1new = $xwiki.getDocument("${doc.space}.${class}ClassSheet").isNew()) #set( $doc2new = $xwiki.getDocument("${doc.space}.${class}ClassTemplate").isNew())
#if (!($doc1new||$doc2new))
1.1 Create a new Supplier
To create a new supplier, you should first choose a name for your supplier.
<form action="" id="newdoc" method="post"> <div> <input type="hidden" name="parent" value="${defaultparent}"/> <input type="hidden" name="template" value="${doc.web}.${class}ClassTemplate"/> <input type="hidden" name="sheet" value="1"/> Space: <input type="text" readonly="true" name="webname" value="${defaultweb}" size="8"/> Supplier: <input type="text" name="name" value="Supplier Name"/> <br/> <br/> ## TODO: remove this onclick <input type="button" value="Create this supplier" onclick='if (updateName(this.form.name)) {this.form.action="../../inline/" + this.form.webname.value + "/" + this.form.name.value; this.form.submit(); }'/> </div> </form>
1.1 Existing Suppliers
#set ($sql = ", BaseObject as obj where obj.name=doc.fullName and obj.className='${doc.web}.${class}Class' and obj.name<>'${doc.web}.${class}ClassTemplate'") #foreach ($item in $xwiki.searchDocuments($sql)) * [$item] #end #end
So what do I have to do to create objects of a class under some other parent page not under the default class page.
Next after a document object is created I would like to update the same document, just like in create is there a template or tutorial that would help me in understanding how to update a object, so as to complete the CRUD flow.
Thanks Sachin
-- View this message in context: http://www.nabble.com/Creating-structured-objects-tp14268015p14273031.html Sent from the XWiki- Dev mailing list archive at Nabble.com.
Did you try setting the parent on the SuppliersTemplate document? Sachn Mittal wrote:
One thing is see is that default parent has to be fully qualified page name.
So it should be aeternam.Suppliers and not Suppliers.
Even when I make this change still the object for supplier is created under aeternam.SupplierClass and not under aeternam.Suppliers.
When I go and edit this new object (wiki) the on right I see parent as aeternam.SupplierClass. So now there I change this to aeternam.Suppliers, the created supplier object moves to Suppliers page.
What I have to do is to create this object directly under aeternam.Suppliers. I believed setting: #set( $defaultparent = "aeternam.Suppliers") should have worked but its not.
Thanks Sachin
Sachn Mittal wrote:
Hi, In one of your tutorials (referencing FAQ application) you have shown how to create custom classes, add properties to it and create objects of these classes. This was well understood and thanks for the easy to understand tutorial. Following the steps I create a space called aeternam and added a page SupplierClass under it.
Further I create a page "Suppliers" under aeternam space, on which I pasted the velocity template code to create objects of the SupplierClass and view create objects.
All works fine. Only problem is the objects are created under the parent SupplierClass and not under parent Suppliers.
I have explicitly mentioned the parent using the code:
#set( $class = "Supplier") #set( $defaultparent = "Suppliers") #set( $defaultweb = "aeternam")
#set( $doc1new = $xwiki.getDocument("${doc.space}.${class}ClassSheet").isNew()) #set( $doc2new = $xwiki.getDocument("${doc.space}.${class}ClassTemplate").isNew())
#if (!($doc1new||$doc2new))
1.1 Create a new Supplier
To create a new supplier, you should first choose a name for your supplier.
<form action="" id="newdoc" method="post"> <div> <input type="hidden" name="parent" value="${defaultparent}"/> <input type="hidden" name="template" value="${doc.web}.${class}ClassTemplate"/> <input type="hidden" name="sheet" value="1"/> Space: <input type="text" readonly="true" name="webname" value="${defaultweb}" size="8"/> Supplier: <input type="text" name="name" value="Supplier Name"/> <br/> <br/> ## TODO: remove this onclick <input type="button" value="Create this supplier" onclick='if (updateName(this.form.name)) {this.form.action="../../inline/" + this.form.webname.value + "/" + this.form.name.value; this.form.submit(); }'/> </div> </form>
1.1 Existing Suppliers
#set ($sql = ", BaseObject as obj where obj.name=doc.fullName and obj.className='${doc.web}.${class}Class' and obj.name<>'${doc.web}.${class}ClassTemplate'") #foreach ($item in $xwiki.searchDocuments($sql)) * [$item] #end #end
So what do I have to do to create objects of a class under some other parent page not under the default class page.
Next after a document object is created I would like to update the same document, just like in create is there a template or tutorial that would help me in understanding how to update a object, so as to complete the CRUD flow.
Thanks Sachin
This is the code in my following Class pages 1. aeternam.SupplierClass ## replace Main with the Space where you want your documents to be created ## replace the default parent with the one of your choice ## Save this template using the ?Save? button ##set( $class = $doc.name.substring(0,$doc.name.indexOf("Class"))) #set($defaultparent = "aeternam.Suppliers") #set($defaultweb = "aeternam") #includeForm("XWiki.ClassSheet") 2. aeternam.SupplierClassTemplate ## replace MyName with the real class name ## save this template using the save button at the top left #includeForm("aeternam.SupplierClassSheet") 3. aetername.SupplierClassSheet ## You can modify this page to customize the presentation of your object ## at first you should keep the default presentation. ## Save this template using the save button. 1 Document $doc.name ## Change class name to your class name #set($class = $doc.getObject("aeternam.SupplierClass").xWikiClass) #set($hasProps = false) #foreach($prop in $class.properties) #if($velocityCount == 1) #set($hasProps = true) <dl> #end <dt> ${prop.prettyName} </dt> <dd>$doc.display($prop.getName())</dd> #end #if($hasProps) </dl> #end So you can see that defaultParent is set in the SupplierClass page. Let me know if I am doing anything wrong. Thanks Sachin Sergiu Dumitriu-2 wrote:
Did you try setting the parent on the SuppliersTemplate document?
-- View this message in context: http://www.nabble.com/Creating-structured-objects-tp14268015p14292658.html Sent from the XWiki- Dev mailing list archive at Nabble.com.
on further investigation I have found that basic form responsible for creating the object based on some class template is: <form action="" id="newdoc" method="post"> <div> <input type="hidden" name="template" value="${doc.web}.${class}ClassTemplate"/> <input type="hidden" name="sheet" value="1"/> Space: <input type="text" readonly="true" name="webname" value="${defaultweb}" size="8"/> Parent: <input type="text" readonly="true" name="parent" value="${defaultparent}"/> Supplier: <input type="text" name="name" value="Supplier Name"/> <br/> <br/> ## TODO: remove this onclick <input type="button" value="Create this supplier" onclick='if (updateName(this.form.name)) {this.form.action="../../inline/" + this.form.webname.value + "/" + this.form.name.value; this.form.submit(); }'/> </div> </form> now parent can be specified while creating and its not static. also if you see on submit it directs me to a link like: xwiki/bin/inline/<space name>/ in the breadcrumb I see the current navigation path ie <parent page name>/. but while saving parent defaults to the <class name> and not the <parent page name>. I also saw in the form (doing view source of this page that is to be saved), and I did not see any input type with name parent. However I see the correct template name being passed to this new page about to be saved. I this some kind of bug. Also to verify if I add a input type parent in the MyClassSheet and add some static value to this then I can see this input type in this form and the new page is saved under this static page. So the whole point is before page creation, one should be dynamically able to pass the parent page name and that should be added as one of the input fields under the new object about to be created. Let me know how to achieve this. Thanks Sachin Sachn Mittal wrote:
This is the code in my following Class pages
1. aeternam.SupplierClass
## replace Main with the Space where you want your documents to be created ## replace the default parent with the one of your choice ## Save this template using the ?Save? button ##set( $class = $doc.name.substring(0,$doc.name.indexOf("Class"))) #set($defaultparent = "aeternam.Suppliers") #set($defaultweb = "aeternam") #includeForm("XWiki.ClassSheet")
2. aeternam.SupplierClassTemplate
## replace MyName with the real class name ## save this template using the save button at the top left #includeForm("aeternam.SupplierClassSheet")
3. aetername.SupplierClassSheet
## You can modify this page to customize the presentation of your object ## at first you should keep the default presentation. ## Save this template using the save button.
1 Document $doc.name
## Change class name to your class name #set($class = $doc.getObject("aeternam.SupplierClass").xWikiClass) #set($hasProps = false) #foreach($prop in $class.properties) #if($velocityCount == 1) #set($hasProps = true) <dl> #end <dt> ${prop.prettyName} </dt> <dd>$doc.display($prop.getName())</dd> #end #if($hasProps) </dl> #end
So you can see that defaultParent is set in the SupplierClass page.
Let me know if I am doing anything wrong.
Thanks Sachin
Sergiu Dumitriu-2 wrote:
Did you try setting the parent on the SuppliersTemplate document?
-- View this message in context: http://www.nabble.com/Creating-structured-objects-tp14268015p14295378.html Sent from the XWiki- Dev mailing list archive at Nabble.com.
participants (2)
-
Sachn Mittal -
Sergiu Dumitriu