[xwiki-users] Setting an object date property from a String.
Hi xwiki users, I have a problem setting an object date property from a String. The code I am doing is : ($startDate is a string I got from the request) #if($context.getUser()!="XWiki.XWikiGuest") #set($releaseDoc=$xwiki.getDocument("Project.${releaseid}")) #set($releaseObj=$releaseDoc.getObject("XWiki.ReleaseClass")) $releaseObj.set("startDate",$datetool.toDate($startDate)) $releaseDoc.save() ${releaseObj.startDate} #else NoRight #end This piece of code is placed on the Skin Object and I call it using Ajax (I use an InPlaceEditor from scriptaculous) What I got in result from this is NoRight if I am not connected but a wiki page with this template does not exist if i am logged in. I don't understand what is missing. I tried using Groovy, but I got the Groovy code in response, it was not executed. If someone can point out my error, I am all heard. Thanks in advance 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
Ok, I ended up successfully setting my date parameter from a string combining velocity and groovy. Here is what I've done : Created a Groovy class on a page named Project.DateParser : /* Groovy Class : Date parser#* */ import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; class DateParser { Date parse(toParse) { def formatter = new SimpleDateFormat("dd/MM/yyyy"); return formatter.parse(toParse); } } /* *# */ Then I called it from my velocity script : ##Create an instance of my parser #set($dateParser = $xwiki.parseGroovyFromPage("Project.DateParser")) ##Set the value from this parser calling its parse method $releaseObj.set("startDate",$dateParser.parse($date)) ##Save the doc $releaseDoc.save() ##Write the saved property ${releaseObj.startDate} Now it works really fine. I can edit my date parameters with Ajax and that's pretty cool stuff. Jean Jean Couteau a écrit :
Hi xwiki users,
I have a problem setting an object date property from a String.
The code I am doing is :
($startDate is a string I got from the request)
#if($context.getUser()!="XWiki.XWikiGuest") #set($releaseDoc=$xwiki.getDocument("Project.${releaseid}")) #set($releaseObj=$releaseDoc.getObject("XWiki.ReleaseClass")) $releaseObj.set("startDate",$datetool.toDate($startDate)) $releaseDoc.save() ${releaseObj.startDate} #else NoRight #end
This piece of code is placed on the Skin Object and I call it using Ajax (I use an InPlaceEditor from scriptaculous)
What I got in result from this is NoRight if I am not connected but a wiki page with this template does not exist if i am logged in. I don't understand what is missing.
I tried using Groovy, but I got the Groovy code in response, it was not executed.
If someone can point out my error, I am all heard.
Thanks in advance
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
On Mon, Sep 21, 2009 at 1:45 AM, Jean Couteau <[email protected]> wrote:
/* Groovy Class : Date parser#* */ import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; class DateParser { Date parse(toParse) { def formatter = new SimpleDateFormat("dd/MM/yyyy"); return formatter.parse(toParse); } }
I did the same thing for a similar problem. You can also keep a copy of the dateformatter on the groovy class, instead of recreating it each time... and you might also want to consider setting the timezone to something consistent, like GMT.
import org.apache.commons.io.FileUtils
import java.text.SimpleDateFormat import java.util.TimeZone import java.lang.Double
class My_Groovy {
def xwiki def context def SimpleDateFormat df
void setObjects(param_xwiki, param_context) {
this.xwiki = param_xwiki this.context = param_context this.df = new SimpleDateFormat('HH:mm:ss.SSS'); this.df.setTimeZone(TimeZone.getTimeZone("GMT")); } ... this.df.format( ... ) ... } It's too bad there's no way of doing "new SimpleDateFormat" directly out of velocity (or any kind of object creation). Then there would be no need for the extra complication, and memory footprint, and loading times, all for a very trivial use of groovy. It'd be nice if there was a generic way to dynamically load a class out of velocity, call "new" on it, etc. This might even obviate the need for many of Xwiki's simpler Java plugins that just provide access to an existing Java class as $class, and then let you call static methods on that as $class.myStaticMethod(). Niels http://nielsmayer.com
On Sep 22, 2009, at 4:35 AM, Niels Mayer wrote:
On Mon, Sep 21, 2009 at 1:45 AM, Jean Couteau <[email protected]> wrote:
/* Groovy Class : Date parser#* */ import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; class DateParser { Date parse(toParse) { def formatter = new SimpleDateFormat("dd/MM/yyyy"); return formatter.parse(toParse); } }
I did the same thing for a similar problem. You can also keep a copy of the dateformatter on the groovy class, instead of recreating it each time... and you might also want to consider setting the timezone to something consistent, like GMT.
import org.apache.commons.io.FileUtils
import java.text.SimpleDateFormat
import java.util.TimeZone
import java.lang.Double
class My_Groovy {
def xwiki
def context
def SimpleDateFormat df
void setObjects(param_xwiki, param_context) {
this.xwiki = param_xwiki
this.context = param_context
this.df = new SimpleDateFormat('HH:mm:ss.SSS');
this.df.setTimeZone(TimeZone.getTimeZone("GMT"));
}
...
this.df.format( ... )
...
}
It's too bad there's no way of doing "new SimpleDateFormat" directly out of velocity (or any kind of object creation). Then there would be no need for the extra complication, and memory footprint, and loading times, all for a very trivial use of groovy. It'd be nice if there was a generic way to dynamically load a class out of velocity, call "new" on it, etc. This might even obviate the need for many of Xwiki's simpler Java plugins that just provide access to an existing Java class as $class, and then let you call static methods on that as $class.myStaticMethod().
Isn't this good enough: http://velocity.apache.org/tools/devel/javadoc/org/apache/velocity/tools/gen... http://velocity.apache.org/tools/devel/javadoc/org/apache/velocity/tools/gen... ? Thanks -Vincent
participants (3)
-
Jean Couteau -
Niels Mayer -
Vincent Massol