Announcement

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

    Changing datetime serialization in transformRequest causes problems

    SmartClient Version: SNAPSHOT_v9.1d_2013-09-08/LGPL Development Only (built 2013-09-08)

    Firefox 14

    I need to have my datatime sent to my rest server in in the .toSerializeableDate format. ie instead of this
    2013-10-02T21:02:00 I need 2013-10-02 21:02:00

    I have subclassed the RestDatasource to so I can set default properties and add a couple of things to transform request.

    I have this for my add request
    Code:
            if (dsRequest.operationType == "add") {
                // fixDates(dsRequest.data)
                var values = dsRequest.data
                // debugger;
                for (var field in values) {
                    if (isc.isA.Date(values[field])) {
                        values[field] = values[field].toSerializeableDate();
                    }
                }
                //  debugger;
            }
    But if I use this it messes up Oldvalues


    Here is an example of the post reqest data with working formatRequest with format date field stuff commented out

    Initial Add new record request
    Code:
    { "dataSource":"Contact", "operationType":"add", "componentId":"contactsForm", "data":{ "date":"2013-10-03", "firstName":"aa" }, "oldValues":{ } }
    All is well

    Then if I try to update this record after I get the new record Id and Version from the server, this is what I expect and is working all of the Oldvalues are persisted and only changed fields are sent. The version for the update I am getting from oldValues, I will post the full transformRequest at end.

    Code:
    { "dataSource":"Contact", "operationType":"update", "componentId":"contactsForm", "data":{ "firstName":"aabbb", "@rid":"#9:83", "@version":1 }, "oldValues":{ "date":"2013-10-02", "firstName":"aa", "@rid":"#9:83", "@version":1 } }
    Now if I do the same add/ then update same record with the format date stuff in the add request here is the data when I try to send an update request
    Code:
    { "dataSource":"Contact", "operationType":"update", "componentId":"contactsForm", "data":{ "firstName":"aaabbb", "@rid":"#9:82", "@version":0 }, "oldValues":{ "date":"2013-10-03" } }
    So you can see the oldValues is wrong as well as the data

    So not sure what is going on but ultimately I just need to format the datetime json sent to the server in a format my server understands.

    Here is the full sublcassed RestDataSource

    Code:
    isc.defineClass("oRestDataSource", RestDataSource);
    isc.oRestDataSource.addProperties({
        dataFormat: "json",
        jsonSuffix: "",
        jsonPrefix: "",
        sparseUpdates: true,
        prettyPrint: true,
        dropExtraFields: true,
        fetchDataURL: "http://localhost:2480/scRequest/RestTester",
        addDataURL: "http://localhost:2480/scRequest/RestTester",
        updateDataURL: "http://localhost:2480/scRequest/RestTester",
        removeDataURL: "http://localhost:2480/scRequest/RestTester",
    
        operationBindings: [
            {operationType: "fetch", dataProtocol: "postMessage", requestProperties: {httpMethod: "POST", httpHeaders: {"Authorization": "Basic YWRtaW46YWRtaW4="}}},
            {operationType: "add", dataProtocol: "postMessage", requestProperties: {httpMethod: "POST", httpHeaders: {"Authorization": "Basic YWRtaW46YWRtaW4="}}},
            {operationType: "remove", dataProtocol: "postMessage", requestProperties: {httpMethod: "POST", httpHeaders: {"Authorization": "Basic YWRtaW46YWRtaW4="}}},
            {operationType: "update", dataProtocol: "postMessage", requestProperties: {httpMethod: "POST", httpHeaders: {"Authorization": "Basic YWRtaW46YWRtaW4="}}}
        ],
        transformRequest: function (dsRequest) {
            //debugger;
            if (dsRequest.operationType == "update") {
                debugger;
                var versionFld = dsRequest.oldValues["@version"] || 0;
                dsRequest.data["@version"] = versionFld;
                // fixDates(dsRequest.data)
                var values = dsRequest.data
                // debugger;
                for (var field in values) {
                    if (isc.isA.Date(values[field])) {
                        values[field] = values[field].toSerializeableDate();
                    }
                }
                  debugger;
            }
            if (dsRequest.operationType == "add") {
                // fixDates(dsRequest.data)
                var values = dsRequest.data
                // debugger;
                for (var field in values) {
                    if (isc.isA.Date(values[field])) {
                        values[field] = values[field].toSerializeableDate();
                    }
                }
                  debugger;
            }
    
            return this.Super("transformRequest", arguments);
        }
    })

    Thanks,

    Dan

    #2
    You could avoid this by manipulating both values and oldValues in the same way.

    However bear in mind that, if you are trying to adapt to pre-existing services, the documentation recommends that you start from DataSource, not RestDataSource.

    If this is the only change you intend to make to RestDataSource, you might just proceed, but note that you have broken correct handling of Dates by doing this - see the Date and Time Format and Storage overview.

    Comment


      #3
      This is the only change to the rest adapter, I have already adjusted the backend server to send and receive json data formatted to Rest Api specs. The datetime is more difficult to change on the server and this seems like it should be an easy client side fix.

      I am not sure how I need to manipulate the the values and oldvalues to get this working. Could you provide a snippet example?

      Also why is it breaking, the Rest datasource works fine if I do not try to change the date format, I already use transformRequest to add a field and call super and everything works. How is changing just the format of the field sent back a json breaking the whole rest Datasource?

      Thanks,
      Dan

      Comment


        #4
        Do the same thing to oldValues as to values. If you don't, you've broken the ability of the RestDataSource to know which fields have changed.

        Make sure you've read the linked Date and Time overview. As it stands, you've broken the protocol in other ways, even if you fix your values/oldValues issue.

        Comment


          #5
          From your advice took another look at the server it is a Java Database/Rest server and was able to just change the datetime format to yyyy-MM-dd'T'HH:mm:ssZ. However the server uses the java SimpleDateFormat which is now set to the above format but the RestDataSource sends datetime without an offset, so if I remove the Z it works, but I can't have two different formats on the server its either yyyy-MM-dd'T'HH:mm:ssZ or yyyy-MM-dd'T'HH:mm:ss. Shouldn't the RestDataSource send the clients offset along with the datetime? I actually posted another question http://forums.smartclient.com/showthread.php?t=28102

          regarding this, I just wanted to thank you for your help and let you know I am trying to modify the server to the appropriate RestDataSource format.

          Thanks,
          Dan
          Last edited by delebash; 3 Oct 2013, 17:53. Reason: post link

          Comment


            #6
            RestDataSource always transmits dates in UTC, which is broadly considered a best practice. Just use SimpleDateFormat in UTC locale to parse, and there's no need to worry about offsets.

            Comment

            Working...
            X