Datasource fetch and update with setValueXPath on fields

I am using a RestDataSource and the POSTMESSAGE protocol to fetch and update data in a datasource. The result on the fetch is nested XML for which I use the XPath support to select a nested value. This all works great. When I update a record in the datasource (with bounded form) it sends the update operation in XML. On the server side I will deserialize this into a java object and perform a save through Hibernate. But the XML for the update is different from the fetch result. This is due to the XPath value, which is returned as a property on the record level instead of the nested XML. Based on the xpath value it should be possible to reconstruct the original structure. Actually the datasource keeps the nested xml and sends it also back to the server on the update. See below for an example:

The response on the fetch request:

<response>  
   <status>0</status>  
   <startRow>0</startRow>  
   <endRow>2</endRow>  
   <totalRows>2</totalRows>  
   <data>    
      <item>      
         <id>1</id>      
         <name>Item 1</name>      
         <itemType>        
            <id>1</id>        
            <name>ItemType 1</name>        
         </itemType>    
      </item>    
      .....
   </data>
</response>

The update sends the following request:

<request>    
   <data>        
      <item>            
         <id>1</id>            
         <code>Item 1</code>            
         <itemTypeID>2</itemTypeID>            
         <itemType>            	
            <id>1</id>            	
            <name>ItemType 1</name>            	
         </itemType>        
      </item>    
   </data>    
   <oldValues>        
      .....
   </oldValues>    
   <dataSource>item</dataSource>    
   <operationType>update</operationType>    
</request>

Note the “itemTypeID” that is added which is the name of the datasourcefield that uses the following simple Xpath expression: “itemType/id”. From data integration point of view I think it would be very useful if the framework would make sure that the resulting XML data would be directly useable on the backend. And with the xpath it should be possible to reconstruct the data?

Maybe there is something I am overlooking to tell the datasource to manage this. I have also tried to override the transformRequest method (in order to change it programmatically but this method is never called (see other post). Please let me know whether this is possible at all, otherwise I need to implement some custom transformation on the server side.

Hi teseling,

We do something similar in our Java Server product (for Java beans) but not yet for consumption of XML. If you can’t use the server product and this affects you pervasively, consider implementing a transformRequest that uses the DataSource fields to reverse the valueXPath mapping. At least for simple cases this should be straightforward: you could use selectObjects to locate the JSON object in the request data that will be serialized into the XML that needs to be updated.

Thanks, the proposed solution by overriding transformRequest sounds like the best solution. I think indeed when using XML that the cleanest solution is to keep this transparent to the server by providing correct (hierarchical) XML to the server. I think this would also be a great improvement to the REST mechanism.

We would like to investigate this issue a bit further. Could you provide some more insight on the appropriate API calls. Is this possible with the current version of the SmartGWT API? Or are there some methods still missing? Any help would be greatly appreciated.

Yes, it should be possible with the existing APIs. The data you receive in transformRequest() of a RestDataSource (after calling Super) is flattened, you need to rearrange it so that it matches the hierarchy of the original XML based on reversing the valueXPaths, and then returned the rearranged data for SmartClient to serialize it as XML as normal.