ABAP RESTful Programming Model - V1
ABAP RESTful Programming Model - V1
Service Consumption
Our semantic data model is defined by Core Data Services (CDS). Core Data Services
are views on top of the dictionary tables.
In the CDS layer you can use and manipulate data that is persisted in the database.
The projection is a subset of the fields of the underlying data model, that are relevant
for the application. For example UI annotations would be part of a projection view.
With the service definition you are able to define which data is exposed as a business
service.
To develop a read-only application you need to carry out the steps contain in the
dashed rectangle.
To develop additional custom logic such as validations and actions the steps in the
dashed rectangle need to be carried out.
Create Table Persistence and Generate Data
Step 1: Create ABAP package
Name: ZTRAVEL_APP_XXX
Click Next >.
ZLOCAL will be added as software component. Click Next >.
Select Create new request and enter a request description.
Right-click on Favorite Packages and select Add Package.
Search for ZTRAVEL_APP_XXX , select it and click OK to add your package in the favorite
packages section.
Create a new database table:
Name: ZTRAVEL_XXX
Click Next >.
Click Finish to create your transport request.
Replace your code with following:
@AbapCatalog.enhancementCategory : #NOT_EXTENSIBLE
@AbapCatalog.tableCategory : #TRANSPARENT
@AbapCatalog.deliveryClass : #A
@AbapCatalog.dataMaintenance : #LIMITED
travel_id : /dmo/travel_id;
agency_id : /dmo/agency_id;
customer_id : /dmo/customer_id;
begin_date : /dmo/begin_date;
end_date : /dmo/end_date;
@Semantics.amount.currencyCode : 'ztravel_xxx.currency_code'
booking_fee : /dmo/booking_fee;
@Semantics.amount.currencyCode : 'ztravel_xxx.currency_code'
total_price : /dmo/total_price;
currency_code : /dmo/currency_code;
description : /dmo/description;
overall_status : /dmo/overall_status;
created_by : syuname;
created_at : timestampl;
last_changed_by : syuname;
last_changed_at : timestampl;
}
Save and activate.
Name: ZCL_GENERATE_TRAVEL_DATA_XXX
Click Next >.
Click Finish to create your transport request.
Replace your code with following:
PUBLIC
FINAL
CREATE PUBLIC.
PUBLIC SECTION.
INTERFACES IF_OO_ADT_CLASSRUN.
PROTECTED SECTION.
PRIVATE SECTION.
ENDCLASS.
CLASS ZCL_GENERATE_TRAVEL_DATA_XXX IMPLEMENTATION.
METHOD IF_OO_ADT_CLASSRUN~MAIN.
itab = VALUE #(
description = 'Enter your comments here' overall_status = 'A' created_by = 'MUSTERMANN' created_at =
'20190613111129.2391370' last_changed_by = 'MUSTERMANN' last_changed_at = '20190711140753.1472620' )
description = 'Enter your comments here' overall_status = 'X' created_by = 'MUSTERFRAU' created_at =
'20190613105654.4296640' last_changed_by = 'MUSTERFRAU' last_changed_at = '20190613111041.2251330' )
).
out->write( sy-dbcnt ).
ENDCLASS.
Switch to your database table and press F8 to see your data.
Name: ZI_TRAVEL_M_XXX
Click Next >.
Click Finish to use your transport request.
Define root view for ZI_TRAVEL_M_XXX and database table as source.
Your result should look like this. Replace your code with following:
@AbapCatalog.sqlViewName: 'ZVI_TRAVEL_M_XXX'
@AbapCatalog.compiler.compareFilter: true
@AbapCatalog.preserveKey: true
@AccessControl.authorizationCheck: #CHECK
/* Associations */
key mykey,
travel_id,
agency_id,
customer_id,
begin_date,
end_date,
@Semantics.amount.currencyCode: 'currency_code'
booking_fee,
@Semantics.amount.currencyCode: 'currency_code'
total_price,
@Semantics.currencyCode: true
currency_code,
overall_status,
description,
@Semantics.user.createdBy: true
created_by,
@Semantics.systemDateTime.createdAt: true
created_at,
@Semantics.user.lastChangedBy: true
last_changed_by,
@Semantics.systemDateTime.lastChangedAt: true
last_changed_at,
/* Public associations */
_Agency,
_Customer,
_Currency
Name: ZC_TRAVEL_M_XXX
Click Next >.
Click Finish to use your transport request.
Define root view entity for ZC_TRAVEL_M_XXX .
Your result should look like this. Replace your code with following:
@AccessControl.authorizationCheck: #CHECK
@UI: {
@Search.searchable: true
as projection on ZI_TRAVEL_M_XXX
{
purpose: #STANDARD,
type: #IDENTIFICATION_REFERENCE,
label: 'Travel',
position: 10 } ]
@UI.hidden: true
@UI: {
@Search.defaultSearchElement: true
travel_id as TravelID,
@UI: {
identification: [ { position: 20 } ],
selectionField: [ { position: 20 } ] }
@Search.defaultSearchElement: true
agency_id as AgencyID,
_Agency.Name as AgencyName,
@UI: {
lineItem: [ { position: 30, importance: #HIGH } ],
identification: [ { position: 30 } ],
selectionField: [ { position: 30 } ] }
@ObjectModel.text.element: ['CustomerName']
@Search.defaultSearchElement: true
customer_id as CustomerID,
@UI.hidden: true
_Customer.LastName as CustomerName,
@UI: {
identification: [ { position: 40 } ] }
begin_date as BeginDate,
@UI: {
identification: [ { position: 41 } ] }
end_date as EndDate,
@UI: {
@Semantics.amount.currencyCode: 'CurrencyCode'
total_price as TotalPrice,
@Consumption.valueHelpDefinition: [{entity: {name: 'I_Currency', element: 'Currency' }}]
currency_code as CurrencyCode,
@UI: {
overall_status as TravelStatus,
description as Description,
@UI.hidden: true
last_changed_at as LastChangedAt
Name: ZUI_C_TRAVEL_M_XXX
Click Next >.
Name: ZUI_C_TRAVEL_M_XXX
Click Next >.
Click Finish to use your transport request.
Activate your service binding.
1. Now your service binding is created and you are able to see your service with its
entities and associations.
Your columns are created. The annotation ensures that all columns are already
selected. As you can see, buttons like, create and update are missing. Therefore you
need to define your behavior definition.
Create Behavior Definition for Managed Scenario
Click Next >.
Click Finish to use your transport request.
Replace your code with following.
lock master
{
// administrative fields (read only)
create;
update;
delete;
}
Now the behavior definition is created and determines the create, update and delete
functionality for travel booking.
Click Next >.
Click Finish to use your transport request.
Replace your code with following:
projection;
use etag
use create;
use update;
use delete;
Now switch to your service binding and double click on TravelProcessor .
The create and delete button appears on the UI because of the managed scenario. You
can create and edit travel bookings or you’ re able to delete existing ones.
Enhance Behavior With Action and Validation
Switch to your behavior definition ZI_TRAVEL_M_XXX and add following action and validation
to your coding:
// validations
// determination
{ create; }
Your result should look like this. Replace your code with following:
managed implementation in class ZCL_BP_I_TRAVEL_M_XXX unique;
lock master
update;
delete;
// validations
// determination
{ create; }
projection;
use etag
use create;
use update;
use delete;
Click Next >.
PUBLIC
ABSTRACT
FINAL
ENDCLASS.
ENDCLASS.
*"* use this source file for the definition and implementation of
*"* declarations
PRIVATE SECTION.
ENDCLASS.
**********************************************************************
*
**********************************************************************
METHOD validate_customer.
RESULT DATA(lt_travel).
%msg = new_message( id =
/dmo/cx_flight_legacy=>customer_unkown-msgid
number = /dmo/cx_flight_legacy=>customer_unkown-
msgno
v1 = ls_travel-customer_id
severity = if_abap_behv_message=>severity-error )
ENDIF.
ENDLOOP.
ENDMETHOD.
**********************************************************************
**********************************************************************
METHOD validate_dates.
end_date = if_abap_behv=>mk-on ) ) )
RESULT DATA(lt_travel_result).
%msg = new_message( id =
/dmo/cx_flight_legacy=>end_date_before_begin_date-msgid
number =
/dmo/cx_flight_legacy=>end_date_before_begin_date-msgno
v1 = ls_travel_result-begin_date
v2 = ls_travel_result-end_date
v3 = ls_travel_result-travel_id
severity = if_abap_behv_message=>severity-error )
%element-begin_date = if_abap_behv=>mk-on
number =
/dmo/cx_flight_legacy=>begin_date_before_system_date-msgno
severity = if_abap_behv_message=>severity-error )
%element-begin_date = if_abap_behv=>mk-on
ENDIF.
ENDLOOP.
ENDMETHOD.
********************************************************************************
* Implements travel action (in our case: for setting travel overall_status to completed)
********************************************************************************
METHOD set_status_completed.
" Modify in local mode: BO-related updates that are not relevant for authorization
checks
ENTITY travel
%control-overall_status = if_abap_behv=>mk-on ) )
FAILED failed
REPORTED reported.
ENTITY travel
%control = VALUE #(
agency_id = if_abap_behv=>mk-on
customer_id = if_abap_behv=>mk-on
begin_date = if_abap_behv=>mk-on
end_date = if_abap_behv=>mk-on
booking_fee = if_abap_behv=>mk-on
total_price = if_abap_behv=>mk-on
currency_code = if_abap_behv=>mk-on
overall_status = if_abap_behv=>mk-on
description = if_abap_behv=>mk-on
created_by = if_abap_behv=>mk-on
created_at = if_abap_behv=>mk-on
last_changed_by = if_abap_behv=>mk-on
last_changed_at = if_abap_behv=>mk-on
)))
RESULT DATA(lt_travel).
%param = travel
) ).
ENDMETHOD.
********************************************************************************
********************************************************************************
METHOD get_features.
( %key = keyval-%key
%control-overall_status = if_abap_behv=>mk-on
))
RESULT DATA(lt_travel_result).
( %key = ls_travel-%key
) ).
ENDMETHOD.
METHOD calculatetravelkey.
lv_max_travel_id = lv_max_travel_id + 1.
ENTITY Travel
travel_id = lv_max_travel_id ) )
REPORTED DATA(ls_reported).
ENDLOOP.
ENDMETHOD.
ENDCLASS.
The behavior implementation is created for travel booking. By using the managed
approach, the implementation of create, update and delete is done automatically.
The accept travel button appears. Create a new travel booking with the booking status
O. O stands for open. Save your travel booking and you are able to accept your created
travel booking.
Hint: If your accept travel button does not appear, wait a few minutes or deactivate your
service binding and activate it again.
Create and Deploy HTML5 and SAP Fiori Launchpad Site
Modules
If you are using your trial user, then login to your SAP Cloud Platform trial cockpit and
select Launch SAP Web IDE.
Organization: <your_organization>
Space: <your_space>
Click Save.
Add ABAP service to multi-target application
Select your project MTA_Project_XXX > New > SAP Cloud Platform Service.
If you are using the SAP Cloud Platform trial cockpit, search for ABAP, select abap-
Title: TRAVEL_APP_XXX
Namespace: namespace_xxx
Click Next.
Select SAP Cloud Platform Service, then click on your resource.
Logon to SAP Cloud Platform ABAP environment trial or SAP Cloud Platform ABAP
environment and switch back to SAP Web IDE.
Select your resource ZUI_C_TRAVEL_M_XXX and click Next.
Check Selected Service Metadata and click Next.
Open SAP Web IDE and right-click on your project MTA_Project_XXX and
select New > SAP Fiori Launchpad Site Module.
Action: display
Create inbound tile:
Title: Travel_App_XXX
Subtitle: Travel_booking_application
icon: sap-icon://sap-logo-shape
Right click on MTA_Project_XXX and select Build > Build with Cloud MTA Build Tool
(recommended).
Open mta_archives .
Organization: <your_organization>
Space: <your_space>
Click Deploy.
Select mta-project-xxx-approuter .
Click on your application routes to open your application in your launchpad.
Logon to your SAP Cloud Platform ABAP environment trial or SAP Cloud Platform
ABAP environment system.
You application is now available as a tile in the launchpad. Select your
application Travel_App_XXX .
Click Go to see your result.
Check your result.