Updating a ListGrid/RestDataSource from transformResponse using DSDataFormat.CUSTOM

Hi there,

I have an existing application that already had some custom XML responses to the show/add/update/remove operations, so even if I’m using a RestDataSource, I’m trying to be able to reuse the same XML from the existing application. I understand I could modify the XML to look like what RestDataSource accepts by default, but I’m willing to test how hard it is to make it the other way, as we already have plenty of applications with custom XML.

I’ve been able to use transformResponse to process the data that is returned from the request to the server and extract the proper data, but the grid goes back to displaying the old data until I do a full refresh, so I’m probably missing some action to update the datasource. I’ve found many threads on how to transform the response, but I have found none where the record data in the datasource is actually reloaded.

The code I’m using is the following:

      DataSourceField code = new DataSourceField("Code", FieldType.TEXT);
      DataSourceField name = new DataSourceField("Name", FieldType.TEXT);
      DataSourceField description = new DataSourceField("Description", FieldType.TEXT);
        ...
        protected void transformResponse(DSResponse response, DSRequest dsRequest, Object data)
        {
          DSOperationType operationType = dsRequest.getOperationType();
          if (operationType == DSOperationType.UPDATE)
          {
            ListGridRecord[] theRecordArray = new ListGridRecord[1];
            theRecordArray[0] = new ListGridRecord();
            theRecordArray[0].setAttribute("Code", XMLTools.selectString(data, ".../ITEM/@Code"));
            theRecordArray[0].setAttribute("Name", XMLTools.selectString(data, ".../ITEM/@Name"));
            theRecordArray[0].setAttribute("Description", XMLTools.selectString(data, ".../ITEM/@Description"));
            response.setData(theRecordArray);
            // What else to get the grid to use this record?
          }
          else
          {
            super.transformResponse(response, dsRequest, data);
          }

I’ve checked that the transform response method is called and that the data is successfully retrieved. No error messages are displayed at the development console or hosted mode GWT window. Should I update directly the data through a reference to the datasource? I though setting it at the response would be enough, as I did not see anything else being done in the snippets I found online.

Any idea about what I’m missing?

Thanks in advance!
S!

PD: XMLPaths of data have been cut down for brevity’s sake
Environment:
GWT 1.6.4
SmartGWT 1.1
Java 6 update 13
Windows XP

Setting the data in the DSResponse is enough. Enable the ResultSet log category in the Developer Console to see what’s happening - the most common errors are failure to define a primaryKey, mismatched primary key in the response, or returning a record that no longer matches the current filter criteria.

Hi,

Some more testing, I changed the transformResponse code to handle also the DSOperationType.FETCH case to see if it worked and it did. The code is now simpler:

          if (operationType == DSOperationType.FETCH)
          {
            Record[] theRecords = this.recordsFromXML(XMLTools.selectNodes(data, "...ITEM_QUERY/ITEM"));
            response.setData(theRecords);
          }
          else if (operationType == DSOperationType.UPDATE)
          {
            Record[] theRecords = this.recordsFromXML(XMLTools.selectNodes(data, "...ITEM_UPDATED/ITEM"));
            response.setData(theRecords);
          }

Fecth operation works, but after editing, the grid seems to go back to a “cache version” of the cell and it is not updated. All the logs seems to indicate the response is being transformed fine like:

22:16:10.359:XRP3:INFO:xmlSelect:selectNodes: expression: /XML_APP/ITEM_UPDATE/ITEM_UPDATED/ITEM returned Array[1]: 0ms

I set the ResultSet level to debug but no message from that category is generated in any case, not even during the fetch operation which is kind of suspicious.

I have also tried using response.setInvalidateCache(true); but no joy.
The datasource has a primary key defined:

      DataSourceField code = new DataSourceField("Code", FieldType.TEXT);
      code.setPrimaryKey(true);
      code.setCanEdit(false);

I have checked that the response returns the same Code as the edited item and there is no filter in the grid. There are just 4 rows and all of them are displayed.

Any other checks I should perform?
Thanks for your help,
S!

The ResultSet log is what you need to enable, and you definitely have not successfully enabled it. Try again, make sure the Developer Console you are using is actually associated with the page when you change the setting.

I set the ResultSet log level as I have done with the other levels and there is just one page.
I can read at the console

Global Log Priorities updated: Logging messages at priority 'Debug' and above for category 'ResultSet'.

But no message is ever produced for that category, no matter what operation I perform with the ListGrids. On the other hand, I can see, for example, messages like these

23:15:44.203:DEBUG:ListGrid:isc_OID_9:delaying adjustOverflow: childDraw
23:15:44.203:DEBUG:ListGrid:isc_OID_9:delaying adjustOverflow: childMoved

when I set exactly the same way the ListGrid log category to Debug. It is just the ResultSet category that produces nothing :(.

Is there any other way to set the log level for a category?

S!

Then most likely you aren’t really using databinding, no ResultSet is involved. Probably, instead of calling ListGrid.fetchData(), which causes a ResultSet to be created to manage the dataset, you have instead called ListGrid.setData() after directly fetching the data yourself. Don’t do this; see any sample that uses a DataSource for correct usage.

Edited: I’m not getting the new data

Right, I did not know using setData(…) would cause that.

I used it just because I had understood it was the recommended way to update a ListGrid after its Tab had been selected, as fetchData() goes straight to the cache. So now my problem is that selecting a tab I am not getting the new data. But gathering all the pieces, this is the code I am trying to use now to update the data when the tab is selected:

           selectedGrid.getDataSource().fetchData(null, new DSCallback()
          {
            public void execute(DSResponse response, Object rawData, DSRequest request)
            {
              ListGridRecord[] rows = null;
              if (rawData == null)
              {
                rows = new ListGridRecord[] {};
              }
              else
              {
                Record[] theRecords = itemsDataSource.recordsFromXML(XMLTools.selectNodes(rawData, ".../ITEM"));
                // I used to do that
                // myGrid.setData(theRecords);
                // Now I'm trying with that
                response.setInvalidateCache(true);
                response.setData(theRecords);
              }
            }
          });

But I don’t get the new data in the grid. Any idead how I can get the Grid to refresh the data?

Thanks!
S!

Just for reference, I got it to work by invalidating the ListGrid’s cache before calling fetchData(). Like this:

      @Override
      public void onTabSelected(TabSelectedEvent event)
      {
        Tab selectedTab = event.getTab();
        ListGrid selectedGrid = (ListGrid) selectedTab.getPane();
        selectedGrid.invalidateCache();
        selectedGrid.getDataSource().fetchData();
      }

However, for some reason it needs itemsGrid.setAutoFetchData(true); otherwise no record is painted. Still investigating why.

S!

Hi,

First - use
selectedGrid.fetchData();
instead of
selectedGrid.getDataSource().fetchData();

It is big difference: if you call fetchData on datasource (second example) grid is not updated - it is up to you what to do with fetched data; if you call fetchData on data bound component (first example) it will call datasource’s fetchData in turn and will update it’s own cache and view.

Second - you have to manually call itemsGrid.fetchData() (IMPORTANT - NOT itemsGrid.getDataSource().fetchData(); ) if you do not use auto fetch
functionality.

PS. Not sure - but as far as I know - grid will automatically fetch data after invalidateCache().

Alius

Grrrrreat! It works great now.

Thank you very much, for some reason, I had thought the Grid did not have that method, my bad.

From my tests, it seems that fetchData() it is indeed necessary after invalidateCache(). Otherwise no data is fetched.

Thanks again,
S!