You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@turbine.apache.org by Michael Mainguy <ma...@mich.com> on 2003/04/10 15:18:37 UTC

Date Type input w/mysql

I'm new to turbine and I'm having a bit of a problem entering dates. 
They don't show up!  Torque seems to be working ok, I can manually set
the date (as a Date Object) on my OM Classes and everything saves fine. 
But when I try and do the call from an Input from a form, the date just
gets set to null.  
If I have an Input name="StartDate" value="2001-01-01" (or
"2001/01/01","01/01/2001", whatever)

>>>>Doesn't work
  Project item = new Project();
  data.getParameters().setProperties(item);
  item.save();

<<<<Does work
  Project item = new Project();
  data.getParameters().setProperties(item);
  item.setStartDate(new Date());
  item.save();

It appears the mapping to/from Dates isn't going very smoothly.  Any
insights?



-- 
Michael Mainguy
(248)935-8507
mainguym@mich.com



---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-user-help@jakarta.apache.org


Re: Date Type input w/mysql

Posted by Michael Mainguy <ma...@mich.com>.
Excellent,
  I can now edit dates using the Selector, thanks for the info.  

I guess I need to rethink where I am going at this point.  I've written 
an ant task (anakia clone) that  generates some default the Screen
classes, action classes, and some default view templates based on the
content of your torque schema and some templates (much like how the sql
generator in torque works).  The problem is I was just going to stuff a
default value object in the template for each table and that won't
readily work in this case.  

It looks like I'll have to use a little indirection in my screen to
stuff a dummy class into the template context instead of directly
stuffing the value object in there.  I guess that's probably better
anyway, but seems like it will make things more confusing.

Anyone think of a better (simpler) way of doing this?  It seems like
some of the built in stuff should be able to fairly easily do this with,
perhaps I just haven't found the right widget.


On Thu, 2003-04-10 at 10:02, Jeffery Painter wrote:
> 
> I wrote a util class to deal with this...
> 
> whenever you need a sqldate, create the LocalDate object using 
> setDate(Date now) and then you will be able to pull localDate.sqlDate() to 
> get a nice date for mysql... just change the SQLpattern to your database 
> needs. I use this when building actions that rely on using dates as bounds 
> for a query... 
> 
> eg.
> --------------------------------------------------------------------------------------
> 
>         Date startDate = data.getParameters().getDate("dateReportStart");
>         String sDate = "";
>         Date endDate = data.getParameters().getDate("dateReportEnd");
>         String eDate = "";
> 
>         LocalDate ld = new LocalDate();
>          ld.setDate(startDate);
>          sDate = ld.sqlDate();
>          ld.setDate(endDate);
>          eDate = ld.sqlDate();
> 
>         Log.info("Run report based on date range between: " + startDate + " and " + endDate );
>         criteria.add(OrdersPeer.DATE_ORDERED, (Object) sDate, Criteria.GREATER_EQUAL );
>         criteria.add(OrdersPeer.DATE_ORDERED, (Object) eDate, Criteria.LESS_EQUAL );
>         context.put("orders", Orders.generateShipExport(prd, criteria) );
> 
> --------------------------------------------------------------------------------------
> 
> showDate is also helpful when displaying nicely formatted dates across the 
> board in your application. I usually create a niceDate() function in my OM 
> classes which will then pull through util.LocalDate to render nicely 
> formatted in velocity.. then if you need to change the overall formatting, 
> you only need to adjust util.LocalDate
> 
> saving dates should not require all this effor however... I would suggest 
> you look to DateSelector for generating the date fields in your input 
> form.
> 
> I generally use the following method in my screen class
> 
>   public void doBuildTemplate( RunData data, Context context )
>   {
>     context.put("dateToSend", getDateSelector("dateToSend"));
>   }
> 
> 
>   private String getDateSelector(String name)
>   {
>         DateSelector ds = new DateSelector(name);
>         return ds.ecsOutput().toString();
>   }
> 
> 
> HTH,
> Jeff Painter
> painter@kiasoft.com
> 
> -------------------------------------------------------------------------
> package com.kiasoft.util;
> 
> /*
>  * Present dates in nice format
>  */
> 
> import java.util.*;
> import java.text.*;
> 
> public class LocalDate {
> 
>    // initial old date of Jan 1, 2000 -> default date
>    private static Date oldDate = new Date(100, 0, 1);
>    private static Date showDate = new Date();
>    private static Locale usLocale = new Locale("en", "US");
>    private static String pattern = "EEE, MMM d, yyyy K:mm a";
>    // default compat with mysql date format
>    private static String SQLpattern = "yyyy-MM-dd H:mm:ss";
> 
>    static public String display() {
>       SimpleDateFormat formatter;
>       formatter = new SimpleDateFormat(pattern, usLocale);
>       return formatter.format(showDate);
>    }
> 
>    public void setDate(Date v)
>    {
>         if ( v != null ) {
>          this.showDate = v;
>         } else {
>          this.showDate = oldDate;
>         }
>         return;
>    }
> 
>    public String sqlDate()
>    {
>         SimpleDateFormat formatter;
>         formatter = new SimpleDateFormat(SQLpattern, usLocale);
>         return formatter.format(showDate);
>    }
> }
> -------------------------------------------------------------------------
> 
> 
> On Thu, 10 Apr 2003, Michael Mainguy wrote:
> 
> > I'm new to turbine and I'm having a bit of a problem entering dates. 
> > They don't show up!  Torque seems to be working ok, I can manually set
> > the date (as a Date Object) on my OM Classes and everything saves fine. 
> > But when I try and do the call from an Input from a form, the date just
> > gets set to null.  
> > If I have an Input name="StartDate" value="2001-01-01" (or
> > "2001/01/01","01/01/2001", whatever)
> > 
> > >>>>Doesn't work
> >   Project item = new Project();
> >   data.getParameters().setProperties(item);
> >   item.save();
> > 
> > <<<<Does work
> >   Project item = new Project();
> >   data.getParameters().setProperties(item);
> >   item.setStartDate(new Date());
> >   item.save();
> > 
> > It appears the mapping to/from Dates isn't going very smoothly.  Any
> > insights?
> > 
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: turbine-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: turbine-user-help@jakarta.apache.org
> 
-- 
Michael Mainguy
(248)935-8507
mainguym@mich.com



---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-user-help@jakarta.apache.org


Re: Date Type input w/mysql

Posted by Jeffery Painter <pa...@kiasoft.com>.
I wrote a util class to deal with this...

whenever you need a sqldate, create the LocalDate object using 
setDate(Date now) and then you will be able to pull localDate.sqlDate() to 
get a nice date for mysql... just change the SQLpattern to your database 
needs. I use this when building actions that rely on using dates as bounds 
for a query... 

eg.
--------------------------------------------------------------------------------------

        Date startDate = data.getParameters().getDate("dateReportStart");
        String sDate = "";
        Date endDate = data.getParameters().getDate("dateReportEnd");
        String eDate = "";

        LocalDate ld = new LocalDate();
         ld.setDate(startDate);
         sDate = ld.sqlDate();
         ld.setDate(endDate);
         eDate = ld.sqlDate();

        Log.info("Run report based on date range between: " + startDate + " and " + endDate );
        criteria.add(OrdersPeer.DATE_ORDERED, (Object) sDate, Criteria.GREATER_EQUAL );
        criteria.add(OrdersPeer.DATE_ORDERED, (Object) eDate, Criteria.LESS_EQUAL );
        context.put("orders", Orders.generateShipExport(prd, criteria) );

--------------------------------------------------------------------------------------

showDate is also helpful when displaying nicely formatted dates across the 
board in your application. I usually create a niceDate() function in my OM 
classes which will then pull through util.LocalDate to render nicely 
formatted in velocity.. then if you need to change the overall formatting, 
you only need to adjust util.LocalDate

saving dates should not require all this effor however... I would suggest 
you look to DateSelector for generating the date fields in your input 
form.

I generally use the following method in my screen class

  public void doBuildTemplate( RunData data, Context context )
  {
    context.put("dateToSend", getDateSelector("dateToSend"));
  }


  private String getDateSelector(String name)
  {
        DateSelector ds = new DateSelector(name);
        return ds.ecsOutput().toString();
  }


HTH,
Jeff Painter
painter@kiasoft.com

-------------------------------------------------------------------------
package com.kiasoft.util;

/*
 * Present dates in nice format
 */

import java.util.*;
import java.text.*;

public class LocalDate {

   // initial old date of Jan 1, 2000 -> default date
   private static Date oldDate = new Date(100, 0, 1);
   private static Date showDate = new Date();
   private static Locale usLocale = new Locale("en", "US");
   private static String pattern = "EEE, MMM d, yyyy K:mm a";
   // default compat with mysql date format
   private static String SQLpattern = "yyyy-MM-dd H:mm:ss";

   static public String display() {
      SimpleDateFormat formatter;
      formatter = new SimpleDateFormat(pattern, usLocale);
      return formatter.format(showDate);
   }

   public void setDate(Date v)
   {
        if ( v != null ) {
         this.showDate = v;
        } else {
         this.showDate = oldDate;
        }
        return;
   }

   public String sqlDate()
   {
        SimpleDateFormat formatter;
        formatter = new SimpleDateFormat(SQLpattern, usLocale);
        return formatter.format(showDate);
   }
}
-------------------------------------------------------------------------


On Thu, 10 Apr 2003, Michael Mainguy wrote:

> I'm new to turbine and I'm having a bit of a problem entering dates. 
> They don't show up!  Torque seems to be working ok, I can manually set
> the date (as a Date Object) on my OM Classes and everything saves fine. 
> But when I try and do the call from an Input from a form, the date just
> gets set to null.  
> If I have an Input name="StartDate" value="2001-01-01" (or
> "2001/01/01","01/01/2001", whatever)
> 
> >>>>Doesn't work
>   Project item = new Project();
>   data.getParameters().setProperties(item);
>   item.save();
> 
> <<<<Does work
>   Project item = new Project();
>   data.getParameters().setProperties(item);
>   item.setStartDate(new Date());
>   item.save();
> 
> It appears the mapping to/from Dates isn't going very smoothly.  Any
> insights?
> 



---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-user-help@jakarta.apache.org