Menu

Default converter for TIME stereotype

2007-12-27
2012-11-26
  • Janesh Kodikara

    Janesh Kodikara - 2007-12-27

    Hi Javi
    I am using a default converter for TIME stereotype.
    Want to share same with OpenXava.Appreciate your comments to improve the same.

    package org.openxava.converters;

    import org.openxava.converters.IConverter;
    import org.openxava.converters.ConversionException;

    import java.sql.Time;

    /**
    * In java a <tt>String</tt> and in database a
    * <tt>java.sql.Time</tt>. <p>
    *
    * @author Janesh Kodikara
    */
    public class TimeUtilSQLConverter implements IConverter {

        private Time time;
        private String timeStamp;

        public Object toDB(Object o) throws ConversionException {

            if (o == null) return null;
            if (!(o instanceof String)) {
                throw new ConversionException("conversion_db_utiltime_expected");
            }

            timeStamp = o.toString() + ":00";
            time = Time.valueOf(timeStamp);
            return time;
        }

        public Object toJava(Object o) throws ConversionException {
            if (o == null) return null;
            if (!(o instanceof java.sql.Time)) {
                throw new ConversionException("conversion_java_sqltime_expected");
            }

            time = (Time) o;
            timeStamp = time.toString();
            timeStamp = timeStamp.substring(0, 5);

            return timeStamp;

        }

    }

    Following was added to converters.xml

        <for-stereotype stereotype="TIME"  converter-class="org.openxava.qamanager.converters.TimeUtilSQLConverter"
                        cmp-type="java.sql.Time"/>

    Thanking You
    Janesh

     
    • Javier Paniza

      Javier Paniza - 2007-12-27

      Hi Janesh,

      I put your converter in OpenXava codebase,
      it will be available for OX2.2.5.
      Thanks!

      I have done some little changes:
      1. I changed the name to StringTimeConverter for coherence with the other OX converters.
      2. I changed the message "conversion_db_utiltime_expected" by "conversion_db_string_expected"
      3. I didn't add the changes for [default-]converters.xml.

      By default, when you use "TIME" stereotype you want to work with String for manage time data,
      both at Java level and DB level. This case is:
      <property name="mytime" stereotype="TIME"/>

      If you want to work with java.sql.Time usually you use the Java type directly, and you don't use TIME stereotype, that is:
      <property name=mytime" type="java.sql.Time"/>
      In this way you use java.sql.Time for Java and DB.

      You converter is only for the case that you want to work with String at Java level and with java.sql.Time at DB level:

      <property name="mytime" stereotype="TIME"/>
      ...
      <property-mapping ... >
      <converter class="org.openxava.converters.StringTimeConverter"/>
      </property-mapping>

      I would use SimpleDateFormatter for converting from String to Time, but your implementation is OK, and I didn't touch it.

      Thanks again for your contribution.

      Cheers
      Javi

       
    • Janesh Kodikara

      Janesh Kodikara - 2007-12-27

      Hi Javi
      Thanks for the feedback
      Janesh

       

Log in to post a comment.