Jeremi

Aller au contenu | Aller au menu | Aller à la recherche

Mot clé - OneDayOneFunction

Fil des billets - Fil des commentaires

jeudi, septembre 21 2006

Function of the day: set()

Today we will see the set function

Requirements :

  • XWiki classes
  • XWiki Objects
  • createNewObject / newObject
  • use

We have seen yesterday how to create an object. Today, we will see how to set values by using the function Document.set(). You've seen, we can use "set" with the combination of "use", but he can also be used without. The function will search the first object with this fieldname and set the value.

Prototype in Groovy :

Document.set("fieldName", "value")

Example : We will check if an object Comment exist in the current document. If no, we create it. After, we will set all his values, and save the document.

#if (!$doc.getObject("XWiki.XWikiComments"))
 $doc.createNewObject("XWiki.XWikiComments")
 Object Added
#end
$doc.set("author", "Jeremi")
$doc.set("date", "14/10/2042 00:00:00")
$doc.set("comment", "This is my first comment using the use() function")
$doc.set("author", "Jeremi")
$doc.save()
Object saved

vendredi, septembre 15 2006

Function of the day: use()

Today we will see the use function

Requirements :

  • XWiki classes
  • XWiki Objects
  • createNewObject / newObject

This function is very helpfull when you deal with the objects. It allows you to select an object of a document by giving the name of his class and eventually his number. If there is multiple object of this class and you don't specify the number of it, it automatically selects the first one.

In Groovy :

Document.use("ClassName")
Document.use("ClassName", nbOfTheObject)

Example : We will check if an object Comment exist in the current document. If no, we create it. After, we will set all his values, and save the document.

#if (!$doc.getObject("XWiki.XWikiComments"))
 $doc.createNewObject("XWiki.XWikiComments")
 Object Added
#end
$doc.use("XWiki.XWikiComments")
$doc.set("author", "Jeremi")
$doc.set("date", "14/10/2042 00:00:00")
$doc.set("comment", "This is my first comment using the use() function")
$doc.set("author", "Jeremi")
$doc.save()
Object saved

You need to reload the document to see the modification.

mardi, septembre 12 2006

Function of the day: newObject / createNewObject

Today we will see 2 functions for the price of one :-) the newObject / createNewObject function Requirements :

  • XWiki classes
  • XWiki Objects

This functions (newObject / createNewObject) allows you to create a new Object easilly. The first one return the number of the object, and the second directly the object.

The prototype in Groovy:

int Document.createNewObject("classname")
Object Document.newObject("classname")

Example : We will just add an Object Comment to the current Document

$doc.createNewObject("XWiki.XWikiComments")
$doc.save()

or

#set($obj = $doc.newObject("XWiki.XWikiComments"))
$obj.set("author", "Jeremi")
$obj.set("date", "14/10/2042 00:00:00")
$obj.set("comment", "This is my first comment using the use() function")
$obj.set("author", "Jeremi")
$doc.save()

You need to reload the document to see the modification. Be carefull, every time you will reload the page, he will create a new comment.

jeudi, septembre 7 2006

Function of the Day: Document.save()

An Easy one for today :

Document.save()

This function allow to save a document if you have the right of editing it. It save it with the right of the person who see the wiki.

Sample in velocity:

#set($tmpDoc = $xwiki.getDocument("Test.Jeremi"))
$tmpDoc.setContent("test")
$tmpDoc.save()

One Day, One function

Like it's been a long time i haven't blog, i'll try to do a new thing : One Day, One Function. I will write the documentation of a function, or a tip on the API of XWiki. I'll add it to the javadoc of the code soon after. It will be published every day i'm at the XWiki Office at 12h42. I hope to be able to do it :-)

The best thing is if you can complete it, correct or add example.