SAP ABAP Repository – CRUD OData Service

In this chapter we are going to create a CRUD (Create, Read, Update and Delete) OData service using the ABAP Repository and several source tables associated. Implementing concepts like CDS views, ABAP programing, SAP Gateway Service, eclipse IDE and more.

The data model selected is the famous “Flight” model integrated in the shared virtual machine, using the tables SAIRPORT (Airport data) and SCITAIRP (City Airport data) in the OData service and SGEOCITY (Geography City data) as prerequisite.

000 RELATIONAL DATA MODEL

The Flight model in the virtual machine is empty (all tables), for that reason we need to insert manually some rows.

Let’s begin!

Prerequisites:

  • Insert manually SGEOCITY data like this:

071_pre

  • Insert manually SAIRPORT data like this:

081

Step-by-Step:

Core Data Service View

In eclipse IDE, open the ABAP perspective.

00

Connect to ABAP project and create a new ABAP Repository Object in the ZTEST Package.

01

Select Data Definition from Core Data Services folder and press Next.

02

Put the name ZCDS_SAIRPORT for Airport data, put a description and press Next.

034

Choose the transport NPLK900026 and Finish.

044

Put this code in the CDS created:

@AbapCatalog.sqlViewName: ‘ZV_CDS_SAIRPORT’
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: ‘Airport Info View’
define view ZCDS_SAIRPORT as select from sairport {
sairport.id,
sairport.name,
sairport.time_zone
}

Your CDS should look like the image below. Save and activate it.

054

Repeat the CDS view creation for another cds named ZCDS_SCITAIRP for City Airport data.

035

Put this code in the CDS created:

@AbapCatalog.sqlViewName: ‘ZV_CDS_SCITAIRP’
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: ‘City Airport Info View’
define view ZCDS_SCITAIRP as select from scitairp {
scitairp.city,
scitairp.country,
scitairp.airport,
scitairp.mastercity
}

Your CDS should look like the image below. Save and activate it.

055

OData Service – Creation

Open the transaction SEGW using the Run ABAP Application.

061

We need to create a new SAP Gateway project name ZFLIGHT_CRUD.

071

Import the CDS views created using the structures related.

081

For Airport, select AirportInfo as name and ZV_CDS_SAIRPORT as ABAP Structure, click on Next.

093

Select all the fields less MANDT and press Next.

103

Select ID as key and Finish.

113

Repeat the CDS Import for another entity named AirportCityInfo and ZV_CDS_SCITAIRP as ABAP Structure, click on Next.

094

Select all the fields less MANDT and press Next.

104

Select CITY and AIRPORT as key and Finish.

114

We need to create the association between the entities created. Right click on Associations folder and select Create.

121

Name: ToCity

  • Principal Entity:
    • Name: AirportInfo
    • Cardinality: 1
    • Navigation: ToCity
  • Depend Entity:
    • Name: AirportCityInfo
    • Cardinality: 1..n
    • Navigation: ToAirport

Press Next.

132

Select Principal Key: Id and Dependent Property: Airport. Press Next.

142

The association should looks like this. Press Finish.

152

Save all the changes and click on Generate Runtime Objects.

161

The system shows you the default names of some objects that will create (don’t change). Press OK.

171

In Service Implementation right click on AirportInfoSet and select Map to Data Source.

181

Target: Local, Click on button

191

Type: DDIC Data Dictionary, Model: ZV_CDS_SAIRPORT.

203

Your Map should look like this. Press OK.

213

For each map created is necessary put the ELEMENT in every Property field. You can use the Generate Mapping button or drag and drop each field. The AirportInfoSet’s mapping the relationship should looks like this.

223

Repeat the Map process for AirportCityInfoSet using the Structure ZV_CDS_SCITAIRP.

214

The AirportCityInfoSet mapping relationship should looks like this. Save all the changes and click on Generate Runtime Objects button once again.

224

In Runtime Artifacts right click on ZCL_ZFLIGHT_01_DPC_EXT and select Go to ABAP Workbench.

241

We need to redefine the methods to Create, Update and Delete for each entity.

252

Put this code in the method create of AirportInfoSet.

method AIRPORTINFOSET_CREATE_ENTITY.
**TRY.
*CALL METHOD SUPER->AIRPORTINFOSET_CREATE_ENTITY
* EXPORTING
* IV_ENTITY_NAME =
* IV_ENTITY_SET_NAME =
* IV_SOURCE_NAME =
* IT_KEY_TAB =
** IO_TECH_REQUEST_CONTEXT =
* IT_NAVIGATION_PATH =
** IO_DATA_PROVIDER =
** IMPORTING
** ER_ENTITY =
* .
** CATCH /IWBEP/CX_MGW_BUSI_EXCEPTION .
** CATCH /IWBEP/CX_MGW_TECH_EXCEPTION .
**ENDTRY.
data: wa_cds_sairport type ZV_CDS_SAIRPORT,
wa_sairport type SAIRPORT,
it_sairport type SAIRPORT.
IO_DATA_PROVIDER->READ_ENTRY_DATA( importing es_data = WA_CDS_SAIRPORT ).
IF WA_CDS_SAIRPORT is NOT INITIAL.
SELECT single * from SAIRPORT into WA_SAIRPORT where id = WA_CDS_SAIRPORT-ID.
if sy-subrc ne 0.
MOVE-CORRESPONDING WA_CDS_SAIRPORT to IT_SAIRPORT.
INSERT SAIRPORT from IT_SAIRPORT.
IF sy-SUBRC eq 0.
move WA_CDS_SAIRPORT to ER_ENTITY.
ELSE.
RAISE EXCEPTION TYPE /IWBEP/CX_MGW_BUSI_EXCEPTION
EXPORTING
TEXTID = /IWBEP/CX_MGW_BUSI_EXCEPTION=>BUSINESS_ERROR
MESSAGE = ‘Error Inserting – Something is wrong with the Airport creation’.
ENDIF.
else.
RAISE EXCEPTION TYPE /IWBEP/CX_MGW_BUSI_EXCEPTION
EXPORTING
TEXTID = /IWBEP/CX_MGW_BUSI_EXCEPTION=>BUSINESS_ERROR
MESSAGE = ‘Error Inserting – Airport Already Exist’.
endif.
ELSE.
RAISE EXCEPTION TYPE /IWBEP/CX_MGW_BUSI_EXCEPTION
EXPORTING
TEXTID = /IWBEP/CX_MGW_BUSI_EXCEPTION=>BUSINESS_ERROR
MESSAGE = ‘Error Inserting – Please send the Airport Info’.
ENDIF.
endmethod.

Should looks like this:

262

Put this code in the method update of AirportInfoSet.

method AIRPORTINFOSET_UPDATE_ENTITY.
**TRY.
*CALL METHOD SUPER->AIRPORTINFOSET_UPDATE_ENTITY
* EXPORTING
* IV_ENTITY_NAME =
* IV_ENTITY_SET_NAME =
* IV_SOURCE_NAME =
* IT_KEY_TAB =
** IO_TECH_REQUEST_CONTEXT =
* IT_NAVIGATION_PATH =
** IO_DATA_PROVIDER =
** IMPORTING
** ER_ENTITY =
* .
** CATCH /IWBEP/CX_MGW_BUSI_EXCEPTION .
** CATCH /IWBEP/CX_MGW_TECH_EXCEPTION .
**ENDTRY.
data: wa_cds_sairport type ZV_CDS_SAIRPORT,
wa_sairport type SAIRPORT,
it_sairport type SAIRPORT.
IO_DATA_PROVIDER->READ_ENTRY_DATA( importing es_data = WA_CDS_SAIRPORT ).
IF WA_CDS_SAIRPORT is NOT INITIAL.
SELECT single * from SAIRPORT into WA_SAIRPORT where id = WA_CDS_SAIRPORT-ID.
if sy-subrc eq 0.
MOVE-CORRESPONDING WA_CDS_SAIRPORT to IT_SAIRPORT.
UPDATE SAIRPORT from IT_SAIRPORT.
IF sy-SUBRC eq 0.
move WA_CDS_SAIRPORT to ER_ENTITY.
ELSE.
RAISE EXCEPTION TYPE /IWBEP/CX_MGW_BUSI_EXCEPTION
EXPORTING
TEXTID = /IWBEP/CX_MGW_BUSI_EXCEPTION=>BUSINESS_ERROR
MESSAGE = ‘Error Updating – Something is wrong with the Airport Update’.
ENDIF.
else.
RAISE EXCEPTION TYPE /IWBEP/CX_MGW_BUSI_EXCEPTION
EXPORTING
TEXTID = /IWBEP/CX_MGW_BUSI_EXCEPTION=>BUSINESS_ERROR
MESSAGE = ‘Error Updating – The Airport Doesn´t Exist’.
endif.
ELSE.
RAISE EXCEPTION TYPE /IWBEP/CX_MGW_BUSI_EXCEPTION
EXPORTING
TEXTID = /IWBEP/CX_MGW_BUSI_EXCEPTION=>BUSINESS_ERROR
MESSAGE = ‘Error Updating – Please send the Airport Info to update’.
ENDIF.
endmethod.

Should looks like this:

272

Put this code in the method delete of AirportInfoSet.

method AIRPORTINFOSET_DELETE_ENTITY.
**TRY.
*CALL METHOD SUPER->AIRPORTINFOSET_DELETE_ENTITY
* EXPORTING
* IV_ENTITY_NAME =
* IV_ENTITY_SET_NAME =
* IV_SOURCE_NAME =
* IT_KEY_TAB =
** IO_TECH_REQUEST_CONTEXT =
* IT_NAVIGATION_PATH =
* .
** CATCH /IWBEP/CX_MGW_BUSI_EXCEPTION .
** CATCH /IWBEP/CX_MGW_TECH_EXCEPTION .
**ENDTRY.
data: it_sairport TYPE SAIRPORT,
it_pair_key TYPE /IWBEP/S_MGW_NAME_VALUE_PAIR,
c_key TYPE string VALUE ‘Id’.
READ TABLE IT_KEY_TAB into it_pair_key with KEY NAME = C_KEY.
IF IT_PAIR_KEY IS NOT INITIAL.
SELECT SINGLE * FROM SAIRPORT INTO IT_SAIRPORT WHERE id = it_pair_key-value.
IF IT_SAIRPORT is NOT INITIAL.
DELETE FROM SAIRPORT WHERE ID = it_pair_key-value.
IF sy-SUBRC eq 0.
ELSE.
RAISE EXCEPTION TYPE /IWBEP/CX_MGW_BUSI_EXCEPTION
EXPORTING
TEXTID = /IWBEP/CX_MGW_BUSI_EXCEPTION=>BUSINESS_ERROR
MESSAGE = ‘Error Deleting – Something is wrong with the Airport Deletion’.
ENDIF.
ELSE.
RAISE EXCEPTION TYPE /IWBEP/CX_MGW_BUSI_EXCEPTION
EXPORTING
TEXTID = /IWBEP/CX_MGW_BUSI_EXCEPTION=>BUSINESS_ERROR
MESSAGE = ‘Error Deleting – The Airport Doesn´t Exist’.
ENDIF.
ELSE.
RAISE EXCEPTION TYPE /IWBEP/CX_MGW_BUSI_EXCEPTION
EXPORTING
TEXTID = /IWBEP/CX_MGW_BUSI_EXCEPTION=>BUSINESS_ERROR
MESSAGE = ‘Error Deleting – The Airport key field requested is empty’.
ENDIF.
endmethod.

Should looks like this:

282

Put this code in the method create of AirportCityInfoSet.

method AIRPORTCITYINFOS_CREATE_ENTITY.
**TRY.
*CALL METHOD SUPER->AIRPORTCITYINFOS_CREATE_ENTITY
* EXPORTING
* IV_ENTITY_NAME =
* IV_ENTITY_SET_NAME =
* IV_SOURCE_NAME =
* IT_KEY_TAB =
** IO_TECH_REQUEST_CONTEXT =
* IT_NAVIGATION_PATH =
** IO_DATA_PROVIDER =
** IMPORTING
** ER_ENTITY =
* .
** CATCH /IWBEP/CX_MGW_BUSI_EXCEPTION .
** CATCH /IWBEP/CX_MGW_TECH_EXCEPTION .
**ENDTRY.
data: wa_cds_scitairp type ZV_CDS_SCITAIRP,
wa_scitairp type SCITAIRP,
it_scitairp type SCITAIRP.
IO_DATA_PROVIDER->READ_ENTRY_DATA( importing es_data = WA_CDS_SCITAIRP ).
IF WA_CDS_SCITAIRP is NOT INITIAL.
SELECT single * from SCITAIRP into WA_SCITAIRP where AIRPORT = WA_CDS_SCITAIRP-AIRPORT.
if sy-subrc ne 0.
MOVE-CORRESPONDING WA_CDS_SCITAIRP to IT_SCITAIRP.
INSERT SCITAIRP from IT_SCITAIRP.
IF sy-SUBRC eq 0.
move WA_CDS_SCITAIRP to ER_ENTITY.
ELSE.
RAISE EXCEPTION TYPE /IWBEP/CX_MGW_BUSI_EXCEPTION
EXPORTING
TEXTID = /IWBEP/CX_MGW_BUSI_EXCEPTION=>BUSINESS_ERROR
MESSAGE = ‘Error Inserting – Something is wrong with the City Airport creation’.
ENDIF.
else.
RAISE EXCEPTION TYPE /IWBEP/CX_MGW_BUSI_EXCEPTION
EXPORTING
TEXTID = /IWBEP/CX_MGW_BUSI_EXCEPTION=>BUSINESS_ERROR
MESSAGE = ‘Error Inserting – City Airport Already Exist’.
endif.
ELSE.
RAISE EXCEPTION TYPE /IWBEP/CX_MGW_BUSI_EXCEPTION
EXPORTING
TEXTID = /IWBEP/CX_MGW_BUSI_EXCEPTION=>BUSINESS_ERROR
MESSAGE = ‘Error Inserting – Please send the City Airport Info’.
ENDIF.
endmethod.

Should looks like this:

263

Put this code in the method update of AirportCityInfoSet.

method AIRPORTCITYINFOS_UPDATE_ENTITY.
**TRY.
*CALL METHOD SUPER->AIRPORTCITYINFOS_UPDATE_ENTITY
* EXPORTING
* IV_ENTITY_NAME =
* IV_ENTITY_SET_NAME =
* IV_SOURCE_NAME =
* IT_KEY_TAB =
** IO_TECH_REQUEST_CONTEXT =
* IT_NAVIGATION_PATH =
** IO_DATA_PROVIDER =
** IMPORTING
** ER_ENTITY =
* .
** CATCH /IWBEP/CX_MGW_BUSI_EXCEPTION .
** CATCH /IWBEP/CX_MGW_TECH_EXCEPTION .
**ENDTRY.
data: wa_cds_scitairp type ZV_CDS_SCITAIRP,
wa_scitairp type SCITAIRP,
it_scitairp type SCITAIRP.
IO_DATA_PROVIDER->READ_ENTRY_DATA( importing es_data = WA_CDS_SCITAIRP ).
IF WA_CDS_SCITAIRP is NOT INITIAL.
SELECT single * from SCITAIRP into WA_SCITAIRP where AIRPORT = WA_CDS_SCITAIRP-AIRPORT.
if sy-subrc eq 0.
MOVE-CORRESPONDING WA_CDS_SCITAIRP to IT_SCITAIRP.
UPDATE SCITAIRP from IT_SCITAIRP.
IF sy-SUBRC eq 0.
move WA_CDS_SCITAIRP to ER_ENTITY.
ELSE.
RAISE EXCEPTION TYPE /IWBEP/CX_MGW_BUSI_EXCEPTION
EXPORTING
TEXTID = /IWBEP/CX_MGW_BUSI_EXCEPTION=>BUSINESS_ERROR
MESSAGE = ‘Error Updating – Something is wrong with the City Airport Update’.
ENDIF.
else.
RAISE EXCEPTION TYPE /IWBEP/CX_MGW_BUSI_EXCEPTION
EXPORTING
TEXTID = /IWBEP/CX_MGW_BUSI_EXCEPTION=>BUSINESS_ERROR
MESSAGE = ‘Error Updating – The City Airport Doesn´t Exist’.
endif.
ELSE.
RAISE EXCEPTION TYPE /IWBEP/CX_MGW_BUSI_EXCEPTION
EXPORTING
TEXTID = /IWBEP/CX_MGW_BUSI_EXCEPTION=>BUSINESS_ERROR
MESSAGE = ‘Error Updating – Please send the City Airport Info to update’.
ENDIF.
endmethod.

Should looks like this:

273

Put this code in the method delete of AirportCityInfoSet.

method AIRPORTCITYINFOS_DELETE_ENTITY.
**TRY.
*CALL METHOD SUPER->AIRPORTCITYINFOS_DELETE_ENTITY
* EXPORTING
* IV_ENTITY_NAME =
* IV_ENTITY_SET_NAME =
* IV_SOURCE_NAME =
* IT_KEY_TAB =
** IO_TECH_REQUEST_CONTEXT =
* IT_NAVIGATION_PATH =
* .
** CATCH /IWBEP/CX_MGW_BUSI_EXCEPTION .
** CATCH /IWBEP/CX_MGW_TECH_EXCEPTION .
**ENDTRY.
data: it_scitairp TYPE SCITAIRP,
it_pair_key TYPE /IWBEP/S_MGW_NAME_VALUE_PAIR,
c_key TYPE string VALUE ‘Airport’.
READ TABLE IT_KEY_TAB into it_pair_key with KEY NAME = C_KEY.
IF IT_PAIR_KEY IS NOT INITIAL.
SELECT SINGLE * FROM SCITAIRP INTO IT_SCITAIRP WHERE Airport = it_pair_key-value.
IF IT_SCITAIRP is NOT INITIAL.
DELETE FROM SCITAIRP WHERE Airport = it_pair_key-value.
IF sy-SUBRC eq 0.
ELSE.
RAISE EXCEPTION TYPE /IWBEP/CX_MGW_BUSI_EXCEPTION
EXPORTING
TEXTID = /IWBEP/CX_MGW_BUSI_EXCEPTION=>BUSINESS_ERROR
MESSAGE = ‘Error Deleting – Something is wrong with the City Airport Deletion’.
ENDIF.
ELSE.
RAISE EXCEPTION TYPE /IWBEP/CX_MGW_BUSI_EXCEPTION
EXPORTING
TEXTID = /IWBEP/CX_MGW_BUSI_EXCEPTION=>BUSINESS_ERROR
MESSAGE = ‘Error Deleting – The City Airport Doesn´t Exist’.
ENDIF.
ELSE.
RAISE EXCEPTION TYPE /IWBEP/CX_MGW_BUSI_EXCEPTION
EXPORTING
TEXTID = /IWBEP/CX_MGW_BUSI_EXCEPTION=>BUSINESS_ERROR
MESSAGE = ‘Error Deleting – The City Airport key field requested is empty’.
ENDIF.
endmethod.

Should looks like this:

283

Save all and activate it.

Back to the SAP Gateway and double click on Service Maintenance, select GW_HUB and press Register.

291

To register your project as service, your info should looks like the image below and the package related is ZTEST. Press OK.

301

Check the registration status as green and press SAP Gateway Client. You are going to redirect to the Services Manager. Press Yes

310

OData Service – Testing

Airport test cases

To get all the data in the Airport’s source put this url /sap/opu/odata/SAP/ZFLIGHT_CRUD_SRV_01/AirportInfoSet?$format=json, select the method GET and Press Execute. Check the result and the status code should be 200.

322

To insert a new row in the Airport’s source put this url /sap/opu/odata/SAP/ZFLIGHT_CRUD_SRV_01/AirportInfoSet and the code below, select the method POST and Press Execute. Check the result and the status code should be 201.

{
“d” : {
“Id” : “ACA”,
“Name” : “Acapulco, Mexico”,
“TimeZone” : “UTC-6”
}
}

332

To get a specific Airport put this url /sap/opu/odata/SAP/ZFLIGHT_CRUD_SRV_01/AirportInfoSet(‘ACA’)?$format=json, select the method GET and Press Execute. Check the result and the status code should be 200.

342

To update a row in the Airport’s source put this url /sap/opu/odata/SAP/ZFLIGHT_CRUD_SRV_01/AirportInfoSet(‘ACA’) and the code below, select the method PUT and Press Execute. Check the result and the status code should be 204.

{
“d” : {
“Id” : “ACA”,
“Name” : “Acapulco 2, Mexico”,
“TimeZone” : “UTC-1”
}
}

352

To delete a row in the Airport’s source put this url /sap/opu/odata/SAP/ZFLIGHT_CRUD_SRV_01/AirportInfoSet(‘ACA’), select the method DELETE and Press Execute. Check the result and the status code should be 204.

362

Airport City test cases

To get all the data in the Airport City’s source put this url /sap/opu/odata/SAP/ZFLIGHT_CRUD_SRV_01/AirportCityInfoSet?$format=json, select the method GET and Press Execute. Check the result and the status code should be 200.

323

To insert a new row in the Airport City’s source put this url /sap/opu/odata/SAP/ZFLIGHT_CRUD_SRV_01/AirportCityInfoSet and the code below, select the method POST and Press Execute. Check the result and the status code should be 201.

{
“d” : {
“City” : “NEW YORK”,
“Country” : “US”,
“Airport” : “EWR”,
“Mastercity” : “NEW YORK”
}
}

333

To get a specific Airport City put this url /sap/opu/odata/SAP/ZFLIGHT_CRUD_SRV_01/AirportCityInfoSet(City=’NEW%20YORK’,Airport=’JFK’)?$format=json, select the method GET and Press Execute. Check the result and the status code should be 200.

343

To update a row in the Airport City’s source put this url /sap/opu/odata/SAP/ZFLIGHT_CRUD_SRV_01/AirportCityInfoSet(City=’NEW%20YORK’,Airport=’JFK’)  and the code below, select the method PUT and Press Execute. Check the result and the status code should be 204.

{
“d” : {
“City” : “NEW YORK”,
“Country” : “US”,
“Airport” : “JFK”,
“Mastercity” : “SAN FRANCISCO”
}
}

353

To delete a row in the Airport City’s source put this url /sap/opu/odata/SAP/ZFLIGHT_CRUD_SRV_01/AirportCityInfoSet(City=’NEW%20YORK’,Airport=’JFK’), select the method DELETE and Press Execute. Check the result and the status code should be 204.

363

Your OData Service with multiple source tables is working perfect in SAP ABAP Repository.

spanish-version

 

Advertisements

One thought on “SAP ABAP Repository – CRUD OData Service

  1. Pingback: SAP HANA Cloud Linked to Our On-Premise SAP NW AS ABAP and BW 7.50 Virtual Machine | Innovators at Heart

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s