Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    Converting DB integer field to date

    Once again, the legacy DB I'm working with throws me a curve. There are tables with "date" fields which are actually stored as integers in the format yyyymmdd. I need to use these in the SmartClient UI as normal dates. It sounds like a problem SimpleType was intended to solve but I can't figure out how to use it.

    I started to experiment by putting the SimpleType element at the top of my DataSource ds.xml file as in the demo, but that gets me a null pointer exception when trying to use the DataSource.

    <SimpleType name="zipCodeUS" inheritsFrom="text">
    <validators>
    <validator type="regexp"
    expression="^\d{5}(-\d{4})?$"/>
    </validators>
    </SimpleType>

    Is a SimpleType the right approach? If so, can you give me an example?

    #2
    You probably want to transform these fields into actual Dates rather than try to build a SimpleType for them. Two ways to do this: server-side modification of the DSResponse after it comes from the DB (scan through and parse into normal Java Dates) or client-side implementation of a FieldValueExtractor. The former is probably simpler.

    Comment


      #3
      I know how to to scan the DSResponse and parse into Java Dates and can do the reverse when it comes to updates or inserts. The question is, how do I recognize the fields that are the type that need this conversion? I could add an attribute to the field element in the data source, but how do I get access to it in my subclass of SQLDataSource? That's when I thought a new SimpleType might be the way to go since I know how to access the field type from the data source.

      I tried creating a SimpleType by adding this to my entry point html page and added the parsing on the server side.

      isc.SimpleType.create({
      name:"date8",inheritsFrom:"date"
      });

      I can verify that the parsing is working and sending back a Java Date in the DSResponse, but it is being rendered as a text field whose contents are the date in long format. If I try to change the field contents I get a validation message "Must be a valid date." no matter what format I enter it in.

      Comment


        #4
        Got it working, thanks. Now I just need to read up on date formatting and see how to control long form versus short form date display.

        Comment


          #5
          As with custom DataSource properties, you can add custom properties on a per-field basis and retrieve them via DSField.getProperty(). So you could add some flag to the field definition indicating this special conversion was necessary, like convertStringDate=true or similar.

          Comment


            #6
            DSField.getProperty() works great. However, I'm running into a problem with the server side validation on an update. On the server side when I receive an update request I'm converting the Date to Integer in yyyymmdd format and then calling super.execute() to do the rest of the standard processing. But that throws this error:

            === 2009-12-09 22:30:00,717 [l0-2] WARN RequestContext - dsRequest.execute() failed:
            java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
            at com.isomorphic.util.Validator$isDate.validate(Validator.java:316)

            I think I need to send the unmodified data through the validator and then modify it before the actual SQL UPDATE occurs. I see there are DataSource.validate(map) and DataSource.validateDSRequest(DSRequest) methods but I'm not sure how to use them. Can I call one of those first and return early if there is a validation error? And if not can I then convert the Dates to Integer and call super.execute()?

            Is there any documentation for SQLDataSource that I can refer to? I hate to be subclassing a class I don't really understand very well.

            Comment


              #7
              Hi Jay,

              If you explicitly validate the request yourself by calling DataSource.validateDSRequest(dsRequest), validation will not be attempted a second time when you call execute via Super().

              There are some holes in the SGWT documentation relative to SmartClient that we're fixing, but don't worry, everything you're calling is a supported API.

              Comment

              Working...
              X