DRAFT Functionality In ABAP Restful Application Programming

DRAFT Functionality In ABAP Restful Application Programming

Introduction

In ABAP RAP (Restful Application Programming Model), the With_Draft functionality is used to handle draft data and enable the implementation of draft-enabled applications.


Drafts refer to intermediate or temporary versions of data that are being modified or created before they are finalized and saved.


The With_Draft functionality allows us to work with draft data in a consistent and controlled manner.


Application without Draft functionality


If we go in edit mode of record , we need to immediately save the changes to database layer or discard the editing and stay with current data.


No alt text provided for this image
No Draft Functionality


There is no option to save the data as draft so that later part it can be saved .


Not having draft functionality in an application can have several drawbacks and limitations.


Here are some common cons of not having draft functionality.


  • Lack of Data Safety: Without draft functionality, users have to modify data directly in the active version, leading to potential data integrity issues.


  • Limited Undo/Redo Capability: Draft functionality provides a natural way to handle undo and redo operations. Without drafts, users may struggle to revert changes or track the history of modifications.


  • Concurrent Access Conflicts: Without draft functionality, simultaneous modifications by multiple users can result in conflicts and data inconsistencies. There is no mechanism to control concurrent access or manage conflicts when two or more users try to modify the same data simultaneously


  • Lack of Collaboration: Drafts allow users to collaborate and share work before finalizing changes.




Let us implement Draft functionality

We need to have two field (last_chaged_at and local_last_changed_at) data in business object/CDS hierarchy.



?last_changed_at? ? ? ?: timestampl; - total ETag
? local_last_changed_at : abp_locinst_lastchange_tstmpl; -? Etag        


We will discuss about total Etag and Etag in detail.


Important annotations to use in root CDS.



? ? ? @Semantics.systemDateTime.lastChangedAt: true?
? ? ? last_changed_at,
? ? ? @Semantics.systemDateTime.localInstanceLastChangedAt:true 
? ? ? ?local_last_changed_at        


Once our VDM/BO is ready with field and annotations , we need to define WITH_DRAFT on behaviour defination level.


No alt text provided for this image
behaviour defination


RAP framework will throw error saying there is no draft table created yet.


mentioned draft table name like below.


No alt text provided for this image

we have defined the draft table , lets create it .


Press CNTRL+1 on draft table name and framework will auto create the draft table with exactly same field as BO.


No alt text provided for this image


No alt text provided for this image
Draft Table


Maintain below field in Behaviour defination.

  • LOCAL MASTER TOTAL ETAG
  • ETAG MASTER


No alt text provided for this image



In the context of ABAP RAP, the "Lock Master: Total ETag" behavior definition refers to a concurrency control mechanism used to handle concurrent access and modifications to draft data.


When a business object with draft functionality is modified by multiple users concurrently, there is a possibility of conflicts where one user's changes may overwrite another user's changes. To prevent such conflicts, the "Lock Master: Total ETag" behavior definition is used.


Here's an explanation of the key aspects of this behavior definition:

  1. ETag: An ETag (Entity Tag) is a unique identifier associated with a specific version of a resource. It is used to track changes to the resource and handle concurrency control. In the case of "Lock Master: Total ETag," the ETag represents the current version of the draft data.
  2. Lock Master: The lock master is the user or session that holds the lock on the draft data, indicating that they are currently working on it. The lock master is responsible for managing and saving the draft.
  3. Total ETag: The "Total ETag" refers to the combination of the ETag and the lock master's identifier. It uniquely identifies the draft data and its lock master.



The "Lock Master: Total ETag" behavior definition ensures that only the lock master, the user who initially acquired the lock, can modify and save the draft.


If another user tries to modify the same draft, the system checks the ETag to determine if it has been changed by the lock master.


If the ETag has changed, it indicates that the draft has been modified by someone else, and the system can handle the conflict appropriately (e.g., notify the user, prevent overwriting changes).




We need to add draft in projection behaviour defination as well like below.


No alt text provided for this image

Activate all including service binding and refresh the application.



Application enabled with Draft functionality

First thing that you will notice on the screen is filter bar named "Editing status".

No alt text provided for this image
That allow us to display the data on multiple criteria .

  • We can display all of the records.
  • We can display real time database record .
  • we can display records which we have drafted.
  • We can display record that is being manipulated by another user/locked record.
  • We can display record which are unchanged as of now.


Lets try editing one of the record and check if changes are getting saved as draft.

No alt text provided for this image
Display mode


Go in Edit mode.


No alt text provided for this image
Discard draft button appeared

Lets change Total price of travel from 750 to 950.

The moment we start making change in any of the field , framework will store the changes in draft table and on screen (below right corner) message would show draft updated.


No alt text provided for this image

We will not save changes in database . Lets see how application will display this data.


Lets go to main page and select all records hiding draft.

No alt text provided for this image

We have made changes in travel id 1 total price and saved it in draft .Draft price is 950 and real time data is 750 as shown on screen.


Draft data selection.

No alt text provided for this image

We can discard the draft if no longer needed.

Sachin Artani

SAP ABAP Developer | SAP RAP on BTP and S/4HANA | Also a Technical Content Creator ??♂?

11 个月

In lock master total etag, is it necessary to maintain only timestamp field of we can put any datatype's field? Does it work the same way with other datatype's field?

Martin Coetzee

ABAP | ABAP on HANA | CDS | SAP BTP

1 年

This is a really great explanation of etags, thank you.

Praseena Nair

SAP Lead Consultant- SAP ABAP, Fiori/UI5, S4HANA, OData specialist

1 年

Hi.. Nice blog. Is this draft functionality available for on-premise landscape as well?

回复

要查看或添加评论,请登录

Satya Prakash Tiwari的更多文章

社区洞察

其他会员也浏览了