insertText() or setDefaultTemplate() ? and how ?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

insertText() or setDefaultTemplate() ? and how ?

Jean Couteau
Dear all,

I am trying to create a document from a page, with a template and an
object attached, but without having to quit the said page.

I generate fine the new document and the object with the values i want
for the properties, but i did not succeed in putting text in the
document, i tried using $newDoc.setDefaultTemplate and
$newDoc.insertText but no success at all.

I post here my piece of code, any hint will be appreciated (after a few
hours on this, i am a bit lost):

#if($createDomain!="")
  #set($newDoc=$xwiki.createDocument())
  #set($newName="Project."+$newDomainId)
  $newDoc.rename($newName)
  $newDoc.insertText("#includeForm(\"XWiki.DomainClassSheet\")","")
  #set($newDoc=$xwiki.getDocument($newName))
  #set($newObject=$newDoc.newObject("XWiki.DomainClass"))
  $newObject.set("id",$newDomainId)
  $newObject.set("name",$createDomain)
  $newDoc.save()
#end

Jean

--
----
Jean Couteau
Code Lutin - http://www.codelutin.com
44 Bd des Pas Enchantés - 44230 St-Sébastien/Loire
Tél : 02 40 50 29 28 - Fax : 09 59 92 29 28

_______________________________________________
users mailing list
[hidden email]
http://lists.xwiki.org/mailman/listinfo/users
Reply | Threaded
Open this post in threaded view
|

Re: insertText() or setDefaultTemplate() ? and how ?

Marius Dumitru Florea
Hi Jean,

Jean Couteau wrote:

> Dear all,
>
> I am trying to create a document from a page, with a template and an
> object attached, but without having to quit the said page.
>
> I generate fine the new document and the object with the values i want
> for the properties, but i did not succeed in putting text in the
> document, i tried using $newDoc.setDefaultTemplate and
> $newDoc.insertText but no success at all.
>
> I post here my piece of code, any hint will be appreciated (after a few
> hours on this, i am a bit lost):
>
> #if($createDomain!="")
>   #set($newDoc=$xwiki.createDocument())
>   #set($newName="Project."+$newDomainId)
>   $newDoc.rename($newName)

>   $newDoc.insertText("#includeForm(\"XWiki.DomainClassSheet\")","")

See http://tinyurl.com/c743xu . Maybe this will work:

$newDoc.setContent("#includeForm(\"XWiki.DomainClassSheet\")")

Hope this helps,
Marius

>   #set($newDoc=$xwiki.getDocument($newName))
>   #set($newObject=$newDoc.newObject("XWiki.DomainClass"))
>   $newObject.set("id",$newDomainId)
>   $newObject.set("name",$createDomain)
>   $newDoc.save()
> #end
>
> Jean
>
_______________________________________________
users mailing list
[hidden email]
http://lists.xwiki.org/mailman/listinfo/users
Reply | Threaded
Open this post in threaded view
|

Re: insertText() or setDefaultTemplate() ? and how ?

Jean-Vincent Drean-3
In reply to this post by Jean Couteau
On Mon, Jan 26, 2009 at 6:08 PM, Jean Couteau <[hidden email]> wrote:
> Dear all,
>
> I am trying to create a document from a page, with a template and an
> object attached, but without having to quit the said page.
>
> I generate fine the new document and the object with the values i want
> for the properties, but i did not succeed in putting text in the
> document, i tried using $newDoc.setDefaultTemplate and
> $newDoc.insertText but no success at all.

All the available methods are described (well, at least listed) in our javadoc.
http://platform.xwiki.org/xwiki/bin/download/DevGuide/API/xwiki-core-1.7.1-javadoc.zip/index.html
-> Document > void setContent(java.lang.String content)

>
> I post here my piece of code, any hint will be appreciated (after a few
> hours on this, i am a bit lost):
>
> #if($createDomain!="")
>  #set($newDoc=$xwiki.createDocument())
>  #set($newName="Project."+$newDomainId)

You can't manipulate strings like this with velocity, see bellow.

>  $newDoc.rename($newName)

You don't have to create then rename a document, see bellow.

>  $newDoc.insertText("#includeForm(\"XWiki.DomainClassSheet\")","")
>  #set($newDoc=$xwiki.getDocument($newName))
>  #set($newObject=$newDoc.newObject("XWiki.DomainClass"))
>  $newObject.set("id",$newDomainId)
>  $newObject.set("name",$createDomain)
>  $newDoc.save()
> #end
>

You should try:

#if($createDomain!="")
  #set($newDoc=$xwiki.getDocument("Project.${newDomainId}"))
  $newDoc.setContent("#includeForm(\"XWiki.DomainClassSheet\")")
  #set($newObject=$newDoc.newObject("XWiki.DomainClass"))
  $newObject.set("id", $newDomainId)
  $newObject.set("name",$createDomain)
  $newDoc.save()
#end

But I'm pretty sure you'll have a problem with the # in setContent,
IIRW I had this problem in the past.
Anyway what you should do here is creating a page with the default
content and object (and call it a template) and copy it in your
script.

#if($createDomain!="")
  $xwiki.copyDocument("Project.ProjectTemplate", "Project.${newDomainId}")
  #set($domainDoc = $xwiki."Project.${newDomainId}")
  #set($domainObj = $newDoc.getObject("XWiki.DomainClass"))
  $domainObj.set("id", $newDomainId)
  $domainObj.set("name",$createDomain)
  $domainDoc.save()
#end

JV.
_______________________________________________
users mailing list
[hidden email]
http://lists.xwiki.org/mailman/listinfo/users
Reply | Threaded
Open this post in threaded view
|

Re: insertText() or setDefaultTemplate() ? and how ?

Jean Couteau

> All the available methods are described (well, at least listed) in our javadoc.
> http://platform.xwiki.org/xwiki/bin/download/DevGuide/API/xwiki-core-1.7.1-javadoc.zip/index.html
> -> Document > void setContent(java.lang.String content)Jean-Vincent Drean a écrit :
>  
I ran throught the javadoc and thought of insertText but not setContent,
that is why I was a bit lost.

>> #if($createDomain!="")
>>  #set($newDoc=$xwiki.createDocument())
>>  #set($newName="Project."+$newDomainId)
>>    
>
> You can't manipulate strings like this with velocity, see bellow.
>  
Why ? It works quite well like that.
>
> You don't have to create then rename a document, see bellow.
>  
In the javadoc, the createDocument method does not have any parameter,
and without parameter, it is the Main.WebHome page that is affected,
that is why I renamed the page after the creation.

> Anyway what you should do here is creating a page with the default
> content and object (and call it a template) and copy it in your
> script.
>
> #if($createDomain!="")
>   $xwiki.copyDocument("Project.ProjectTemplate", "Project.${newDomainId}")
>   #set($domainDoc = $xwiki."Project.${newDomainId}")
>   #set($domainObj = $newDoc.getObject("XWiki.DomainClass"))
>   $domainObj.set("id", $newDomainId)
>   $domainObj.set("name",$createDomain)
>   $domainDoc.save()
> #end
>
>  
I tried putting my template in my page using setDefaultTemplate() but
with no success, I did not thought of the copy method. Thanks for the
hint. Anyway, here is the code I put (everything works fine):

#if($createDomain!="")
  $xwiki.copyDocument("XWiki.DomainClassTemplate", "Project.${newDomainId}")
  #set($domainDoc = $xwiki.getDocument("Project.${newDomainId}"))
  #set($domainObj = $domainDoc.newObject("XWiki.DomainClass"))
  $domainObj.set("id", $newDomainId)
  $domainObj.set("name",$createDomain)
  $domainDoc.save()
#end

By not putting the object in the template, I can choose not to have one
on the page (we never know) and the code is the same size.

Anyway, I ran through the javadoc, but what you said is true, the method
are only listed (and maybe not all the methods), so I had to guess on
the aim of methods and the goal of the parameters, with only their
names, that is a bit long and not intuitive, a small effort on the
javadoc can help a lot ;). I will try help on this if I have time once I
finish my project.
> JV.
> _______________________________________________
> users mailing list
> [hidden email]
> http://lists.xwiki.org/mailman/listinfo/users
>
>  


--
----
Jean Couteau
Code Lutin - http://www.codelutin.com
44 Bd des Pas Enchantés - 44230 St-Sébastien/Loire
Tél : 02 40 50 29 28 - Fax : 09 59 92 29 28

_______________________________________________
users mailing list
[hidden email]
http://lists.xwiki.org/mailman/listinfo/users