C-create, R-read, U-update, D-delete OData Services Creation Using ABAP CDS Views

Introduction

This article describes the fastest way of OData Services creation using ABAP Core Data Services. Details on @OData.publish annotation utilization, reference to Data Source CDS-Entity and import from DDIC Structure are given. Create, Update and Delete actions are shown on the custom transparent table data just to simplify the article and not to move focus from OData Service creation to business process requirements implementation.

“R” Type OData Service Creation. With using of @OData.publish annotation in ABAP CDS

Let us assume that we have any data table (probably in the custom namespace, just to simplify the demo case and get focused on OData service only, and not on SAP functional modules and class usage)

table_zxeq1

It is possible to create ABAP CDS view for it using SAP HANA Studio

table_sh1

During CDS activation OData service will be generated in the background.

Service document looks like this:

table_1.0-encoding

“R” Type OData Service Creation. Using reference on ABAP CDS

Let us assume that we have any data table (probably in custom namespace, just to simplify demo case and get focused on OData service only, and not on SAP functional modules and class usage)

table_zxeq1

It is possible to create ABAP CDS view for it using SAP HANA Studio. Please consider that in this case there is no @OData annotation in use

table_sh1_1

During our next step we will Create Gateway Project via transaction code SEGW and refer it to the created in the previous step ABAP CDS

table_gateway_project
table_cds

SAP will generate runtime objects based on reference to ABAP CDS

table_sh14

The service document looks like this:

table_sample

“CRUD” Type OData Service Creation

Let us assume that we have any data table (probably in custom namespace, just to simplify the demo case and get focused on OData service only, and not on SAP functional modules and class usage)

transparent_table

It is possible to create ABAP CDS view for it using SAP HANA Studio (no @OData annotation is in use)

table_lexprmnt01

During our next step we will Create Gateway Project via transaction code SEGW and import DDIC Structure (use @AbapCatalog.sqlViewName value from ABAP CDS, please see the picture above)

segw
create_an_entity_type
select_parameter

If you are using complex CDS with associations (for Master-Details view, for example) you should additionally create Associations and Navigation Properties. This section will be added.

SAP will generate runtime objects

table_model_and_service_definition

For “CRUD” type of OData Services we should set parameters of Entity Set and then implement each of them.

entity_sets

Navigate to ABAP Workbench and redefine methods of *DPC_EXT class for CRUD operations

table_getentityset

Here it is possible to point out SELECT statement directly to ABAP CDS for Get_EntitySet method

METHOD experimentset_get_entityset.
SELECT *
FROM zxeq1_lexprmnt01
INTO CORRESPONDING FIELDS OF TABLE @et_entityset
ORDER BY PRIMARY KEY.
ENDMETHOD.

If you set Searchable parameter of Entity Set then ABAP code implementation should look like the following

METHOD experimentset_get_entityset.
DATA: lv_osql_where_clause TYPE string.
"Prepare where clause
lv_osql_where_clause = io_tech_request_context->get_osql_where_clause( ).
"Select data
SELECT *
FROM zxeq1_lexprmnt01
INTO CORRESPONDING FIELDS OF TABLE @et_entityset
WHERE (lv_osql_where_clause)
ORDER BY PRIMARY KEY.
ENDMETHOD.

If you additionally set Pageable parameter of Entity Set then ABAP code should look like the following

METHOD experimentset_get_entityset.
DATA: lv_osql_where_clause TYPE string,
lv_top               TYPE i,
lv_skip              TYPE i,
lv_max_index         TYPE i.
"Prepare top and skip
lv_top = io_tech_request_context->get_top( ).
lv_skip = io_tech_request_context->get_skip( ).
IF lv_top IS NOT INITIAL.
lv_max_index = lv_top + lv_skip.
ENDIF.
"Prepare where clause
lv_osql_where_clause = io_tech_request_context->get_osql_where_clause( ).
"Select data
SELECT *
FROM zxeq1_lexprmnt01
INTO CORRESPONDING FIELDS OF TABLE @et_entityset
UP TO @lv_max_index ROWS
WHERE (lv_osql_where_clause)
ORDER BY PRIMARY KEY.
"Process skip
IF lv_skip IS NOT INITIAL.
DELETE et_entityset TO lv_skip.
ENDIF.
"Process inline couter
IF io_tech_request_context->has_inlinecount( ) = abap_true.
SELECT COUNT(*)
FROM zxeq1_lexprmnt01 WHERE (lv_osql_where_clause).
es_response_context-inlinecount = sy-dbcnt.
ELSE.
CLEAR es_response_context-inlinecount.
ENDIF.
ENDMETHOD.

In Get_Entity method we should use ABAP CDS and other data type related objects and key field names

METHOD experimentset_get_entity.
DATA: ls_data       TYPE zcl_xeq1s_exprmnt01p_mpc=>ts_experiment.
"Convert keys to data
CALL METHOD io_tech_request_context->get_converted_keys
IMPORTING
es_key_values = ls_data.
"Select data by keys
SELECT SINGLE *
INTO CORRESPONDING FIELDS OF @er_entity
FROM zxeq1_lexprmnt01
WHERE uname = @ls_data-uname.
ENDMETHOD.

If you set Creatable parameter of Entity Set then you should implement Create_Entity method and ABAP code could look like the following. In our current example we are using database table as target to INSERT data (in more complex and realistic business case SAP BAPI or functional modules or ABAP Classes should be used here to implement required business logic)

METHOD experimentset_create_entity.
DATA: ls_data       TYPE zxeq1_dexprmnt01.
"Insert data in database
MOVE-CORRESPONDING er_entity TO ls_data.
INSERT zxeq1_dexprmnt01
FROM @ls_data.
ENDMETHOD.

If you set Updatable parameter of Entity Set then you should implement Update_Entity method like the following sample. In our current example we are using database table as target to UPDATE data (in more complex and realistic business case SAP BAPI or functional modules or ABAP Classes should be used here to implement required business logic)

METHOD experimentset_update_entity.
DATA: ls_data       TYPE zxeq1_dexprmnt01.
"Update data in database
MOVE-CORRESPONDING er_entity TO ls_data.
UPDATE zxeq1_dexprmnt01
FROM @ls_data.
ENDMETHOD.

If you set Deletable parameter of Entity Set then you should implement Delete_Entity method like the following sample. In our current example we are using database table as target to DELETE data (in more complex and realistic business case SAP BAPI or functional modules or ABAP Classes should be used here to implement required business logic)

METHOD experimentset_delete_entity.
DATA: ls_data       TYPE zcl_xeq1s_exprmnt01p_mpc=>ts_experiment.
"Convert keys to data
CALL METHOD io_tech_request_context->get_converted_keys
IMPORTING
es_key_values = ls_data.
"Delete data from database
DELETE FROM zxeq1_dexprmnt01
WHERE uname = @ls_data-uname.
ENDMETHOD.

As a result, there is the following OData Service Document created



	
table_searchable

Conclusion

This article shows a fast and simple way of OData Service creation with full CRUD functions support based on ABAP CDS technology. It is possible to see that wrapper for CRUD OData Service based on ABAP CDS could be created as fast as a few hours activity. What is needed is simply to add ABAP logic inside it that is mostly the same kind of activities we’ve done in ABAP development before web services usage. Additionally, OData Service created on ABAP CDS views is ready to be consumed by SAP HTML5 (UI5) Application and could either be deployed to your company local SAP Fiori Launchpad or to SAP Fiori Cloud Launchpad (Portal) from SAP Cloud Platform.

P.S. Please consider that in case of SAP Fiori Cloud Launchpad usage we have to wrap OData Service using SAP Cloud Platform OData Provisioning Service (that is covered in another article).

Leave a Comment

Contact Us

Please do not hesitate to contact us, so one of our experts will get back to you asap!

Not readable? Change text. captcha txt

Start typing and press Enter to search