Quantcast
Channel: SCN : All Content - SAP Gateway
Viewing all articles
Browse latest Browse all 2823

Prototyping “eTag Based” locking within SAP UI5 and SAP Gateway

$
0
0

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 ?

 

 

Definiton


Locking :  Ensure that a user don’t change already updated data.

 

Prototypscenario

prototypescenario.gif

 

 

 

Backend implementation

 

1. Persistence and Datatypes

1.1. Table

table.gif

1.2. Source structure for SAP Gateway Service generation

structure.gif

 

SAP Gateway Definition

The OData Service definition is strate foreward only the ETag Property definition is special.

etag definition.gif

 

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_set.gif

 

……_get_entity : Return given employee

get_entoty.gif

 

….._update_entity : Update given employee

update_entity.gif

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

create_entry.gif

 

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.

 

get_entity_test.gif

 

create.gif

 

If you send now a Update or DeleteRequest with the needed etag the following happens:

  1. SAP Gateway call the ….._get_entity method and read the attribute bound to the eTag Property.
  2. SAP Gateway compare the eTag given in the Request with the eTag returned from the ….._get_entity method.
  3. 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.

 

process flow.gif

 

In the screenshot below you see the eTag provided in the Request and the new eTag after the update.

update_gwclient.gif

 

Below the Status_code 412 if you try to change changed data.

fail.gif

 

 

Frontend Implementation

The frontend implementation is a strate foreward implementation.

 

  1. onInit function

onInit.gif

Bind the OData Entity to the Overview Table

 

  1. onRow function

onRow.gif

Called if the user select a row to change the data and bind the element to a simpleform to change the data.

 

  1. onUpdate function

update.gif

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.

Javascript.gif

 

 

I you have any question or you have a better way to get the etag on client side etc. please send a comment.

 

Kind regards,

 

Gerald


Viewing all articles
Browse latest Browse all 2823

Trending Articles