I found no end-to-end example “How to use eTag based locking”. So implement a simple prototype which a share to you.
My question : Is there a better way to handle the eTag on the client side as is describe below ?
Locking : Ensure that a user don’t change already updated data.
1. Persistence and Datatypes
1.2. Source structure for SAP Gateway Service generation
SAP Gateway Definition
The OData Service definition is strate foreward only the ETag Property definition is special.
As you see: The service has only four attributes and LastChanged is the Timestamp updated during updates on the given record.
SAP Gateway Implementation
Four methods are implemented:
….._get_entityset : Return all employees
……_get_entity : Return given employee
….._update_entity : Update given employee
In the update method a new timestamp is calculated and written to db and send back to SAP Gateway framework.
….._create_entity : Create employee entry
How ETag works in SAP Gateway ?
The Response of a GET (Entity or EntitySet) contain for each object the etag property . In this prototype this is assigned to the LastChanged attribute.
If you send now a Update or DeleteRequest with the needed etag the following happens:
- SAP Gateway call the ….._get_entity method and read the attribute bound to the eTag Property.
- SAP Gateway compare the eTag given in the Request with the eTag returned from the ….._get_entity method.
- If both eTags are equal the …._update_entity method will called within this method the attribute bound to the eTag Property will set to a new value and the new value is given back to the caller.
If both eTags are not equal (Data changed between read and update) an exeption will returned to the caller.
In the screenshot below you see the eTag provided in the Request and the new eTag after the update.
Below the Status_code 412 if you try to change changed data.
The frontend implementation is a strate foreward implementation.
- onInit function
Bind the OData Entity to the Overview Table
- onRow function
Called if the user select a row to change the data and bind the element to a simpleform to change the data.
- onUpdate function
Called to send the updated data to the backend and set the eTag in the request.
Question : Is there a better way to get the etag ??
I only found it within the object in the __metadata object.
I you have any question or you have a better way to get the etag on client side etc. please send a comment.