In this exercise, you enable draft handling in your business object and in your OData UI service. To properly support optimistic concurrency control in the OData service, you extend the data model with a timestamp field that is updated during every save of a draft.
Note
In this exercise, replace ## with your group number.
Solution
| Repository Object Type | Repository Object ID |
|---|
| Database Table | /LRN/437D_TRAVEL |
| CDS Data Definition (Model) | /LRN/437D_R_TRAVEL |
| CDS Data Definition (Projection) | /LRN/437D_C_TRAVEL |
| CDS Metadata Extension | /LRN/437D_C_TRAVEL |
| CDS Behavior Definition (Model) | /LRN/437D_R_TRAVEL |
Task 1: Define a Timestamp Field for Draft Changes
Extend your database table Z##_TRAVEL with a timestamp field for changes to the draft instance (suggested name: loc_changed_at).
Add the field to your data model view entity Z##_R_Travel and annotate it with the necessary sub annotation of the @Semantics annotation to ensure that the runtime updates the field during every save of a draft.
Add the field to the field mapping in the behavior definition on data model level Z##_R_TRAVEL, disallow direct changes and use it as the etag field for optimistic concurrency control.
Add the field to your projection view entity Z##_C_Travel and extend the metadata extension, to keep the field hidden in the OData UI service.
Steps
Edit the definition of your database table Z##_TRAVEL. At the end of the field list, add a loc_changed_at field based on data element ABP_LOCINST_LASTCHANGE_TSTMPL.
Open the definition of your database table.
At the end of the field list, add the following code:
1
loc_changed_at : abp_locinst_lastchange_tstmpl;
Activate the database table.
Press Ctrl + F3 to activate the development object.
Edit the definition of your data model view entity Z##_R_Travel. Add the new table field to the element list and specify the alias name LocChangedAt.
Adjust the code as follows:
12345
@Semantics.systemDateTime.lastChangedAt: true
changed_at as ChangedAt,
@Semantics.user.lastChangedBy: true
changed_by as ChangedBy,
loc_changed_at as LocChangedAt
Add annotation @Semantics.systemDateTime.localInstanceLastChangedAt: true to the new view element and activate the data definition.
Adjust the code as follows:
123456
@Semantics.systemDateTime.lastChangedAt: true
changed_at as ChangedAt,
@Semantics.user.lastChangedBy: true
changed_by as ChangedBy,
@Semantics.systemDateTime.localInstanceLastChangedAt: true
loc_changed_at as LocChangedAt
Press Ctrl + F3 to activate the development object.
Edit the behavior definition on data model level Z##_R_TRAVEL. After the etag master addition of the define behavior statement, replace the timestamp for changes to the active data ChangedAt with the new timestamp field LocChangedAt.
Adjust the code as follows:
1
etag master LocChangedAt //ChangedAt
Locate the FIELD statement for the STATUS field and the administrative fields, and add the new field.
Adjust the code as follows:
12345
field ( readonly )
Status,
ChangedAt,
ChangedBy,
LocChangedAt;
Scroll down to the mapping for statement and add the mapping for the new field. Then activate the behavior definition.
Adjust the code as follows:
123
ChangedAt = changed_at;
ChangedBy = changed_by;
LocChangedAt = loc_changed_at;
Press Ctrl + F3 to activate the development object.
Edit the definition of your projection view entity Z##_C_Travel. At the end of the element list, add the new element and activate the data definition.
Adjust the code as follows:
123
ChangedAt,
ChangedBy,
LocChangedAt
Press Ctrl + F3 to activate the development object.
Edit the metadata extension for the projection view Z##_C_TRAVEL. Add the new element LocChangedAt with annotation @UI.hidden: true, and activate the metadata extension.
Adjust the code as follows:
12345678
@UI.hidden: true
ChangedAt;
@UI.hidden: true
ChangedBy;
@UI.hidden: true
LocChangedAt;
Press Ctrl + F3 to activate the development object.
Task 2: Enable Draft-Handling in the Business Object
In the behavior definition on data model level Z##_R_TRAVEL, add the necessary statement to enable draft handling. Define and generate a draft table for the travel data (suggested name: Z##_TRAVEL_D). Use the timestamp for changes to the active data ChangedAt as the total etag field and add the draft actions to the behavior definition.
Steps
Edit the behavior definition on data model level Z##_R_TRAVEL and add the with draft; statement.
Adjust the code as follows:
123456
managed implementation in class zbp_##_r_travel unique;
strict ( 2 );
with draft;
define behavior for Z##_R_Travel alias Travel
Scroll down to the DEFINE BEHAVIOR statement. After persistent table z##_travel add draft table, followed by the name of the draft table that you want to create.
Adjust the code as follows:
1234
define behavior for Z##_R_Travel alias Travel
persistent table z##_travel
draft table z##_travel_d
lock master
Use a quick fix to generate the draft table.
Place the cursor on the name of the draft table.
Press Ctrl + 1 to invoke the quick assist proposals.
From the list of available quick fixes, choose Create draft table z##_travel_d for entity z##_r_travel.
If desired, adapt the generated description for the draft table and choose Next.
Confirm the transport request and choose Finish.
Analyze the generated database table, then activate it.
The definition of the database table is already opened after the previous step.
Press Ctrl + F3 to activate the development object.
Return to the behavior definition. After lock master, add total etag followed by the name of the timestamp field for changes to the active data (ChangedAt).
Adjust the code as follows:
123456789
define behavior for Z##_R_Travel alias Travel
persistent table z##_travel
draft table z##_travel_d
lock master
total etag ChangedAt
authorization master ( instance )
etag master LocChangedAt // ChangedAt
early numbering
{
Explicitly declare the draft actions Edit, Activate, Discard, Resume, and the draft determine action Prepare.
For now, ignore the warnings regarding the missing addition optimized for the Activate action and the missing assignment of the validations to the draft action Prepare.
Add the following code:
12345
draft action Edit;
draft action Activate;
draft action Discard;
draft action Resume;
draft determine action Prepare;
Activate the behavior definition.
Press Ctrl + F3 to activate the development object.