Wednesday 9 January 2019

ABAP on SAP Cloud Platform – ABAP Restful Programming Model

In this blog I am going to explain steps to develop Odata service based on ABAP on SAP Cloud Platform architecture using ABAP Core Data Service (CDS) views. The architecture overview is as follows:

SAP Cloud Platform, SAP ABAP Tutorial and Material, SAP ABAP Guides, SAP ABAP Certifications

In the above architecture from the left figure bottom to top you can see steps to develop the service and consume it in Fiori or other front end applications.

1. Data Modeling and Behavior
2. Business services Provisioning
3. Service consumption

1. Data Modeling and Behavior: 

Here modeling of CDS, Behavior definition and behavior implementation happens.

Behavior definition:  A behavior definition is defined using the Behavior Definition Language (BDL) and comprises capabilities and modelling aspects of the business object node or nodes, for example the supported operations (such as create, update, and delete actions) or the definition of lock dependencies between the parent and child nodes. In short behavior definition is used for transaction processing of an application.

Behavior Implementation:  Here implementation of create, update, delete etc methods of behavior definition happens in ABAP Class.

2. Business services provisioning:

Here service definition and  service implementation takes place:

Service definition: The service definition is a projection of the data model and the related behavior to be exposed.

Service Binding: The service binding implements a specific protocol and the kind of service to be offered for a consumer.

Service definition has one or more service bindings. You can use a service definition only in connection with at least one service binding.

3. Service consumption: 

Service consumption in the form of OData is consumed in Fiori or other front end applications.

Lets start developing an OData service for List Reporting based on the ABAP RESTful Programming model. I am using flight data model for Building the oData service.

Step1. Creating Data Definition for a CDS View:


@AbapCatalog.sqlViewName: 'ZICONRPM'
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Data Model of Flight Connections'
@UI.headerInfo.typeNamePlural: 'Connections'
@UI.headerInfo.typeName: 'Connection'
define view zi_con_rpm
  as select from /dmo/connection as Connections
{

      @UI.lineItem: [ { position: 10 } ]
  key carrier_id      as CarrierID,

      @UI.lineItem: [ { position: 20 } ]
  key connection_id   as ConnectionID,

      @UI.lineItem: [ { position: 30 } ]
      @UI.selectionField: [ { position: 10 } ]
      airport_from_id as DepartureAirport,

      @UI.lineItem: [ { position: 40 } ]
      @UI.selectionField: [ { position: 20 } ]
      airport_to_id   as DestinationAirport,

      @UI.lineItem: [ { position: 50 } ]
      departure_time  as DepartureTime,

      @UI.lineItem: [ { position: 60 } ]
      arrival_time    as ArrivalTime,

      @Semantics.quantity.unitOfMeasure: 'DistanceUnit'
      distance        as Distance,

      @Semantics.unitOfMeasure: true
      distance_unit   as DistanceUnit

}

In above source code “zi_con_rpm” is the CDS view selects data from  data source “/dmo/connection”

Step 2. Creating a service definition and expose CDS view for an OData service


Service definition defines the scope of OData service. In a service definition, you define the OData service to determine which CDS entities are part of the service. Use service definition wizard to create service definition as below:

SAP Cloud Platform, SAP ABAP Tutorial and Material, SAP ABAP Guides, SAP ABAP Certifications

Select service definition under node business services

SAP Cloud Platform, SAP ABAP Tutorial and Material, SAP ABAP Guides, SAP ABAP Certifications

Give name of the service definition and click on finish

SAP Cloud Platform, SAP ABAP Tutorial and Material, SAP ABAP Guides, SAP ABAP Certifications

add source code as below in the service definition:

@EndUserText.label: 'Service Definition for Managing Flights'
define service Zflight_sd_rpm {
  expose zi_con_rpm as Connections;
}

In the above source code “Zflight_sd_rpm” is the service definition name and “zi_con_rpm” is the CDS entity which is created in step1, this needs to be exposed for Odata service with keyword expose. This how you assign the scope of the OData service

one or more CDS entities can be exposed in the service definition.

Step 3. Creating a Service Binding


Service binding implements the protocol that is used for the OData service. It uses a service definition that projects the data models and their related behaviors to the service.

Using below wizard you can create service binding for service definition created in Step 2

SAP Cloud Platform, SAP ABAP Tutorial and Material, SAP ABAP Guides, SAP ABAP Certifications

Select Service Binding and click on Next:

SAP Cloud Platform, SAP ABAP Tutorial and Material, SAP ABAP Guides, SAP ABAP Certifications

Give name of Service Binding and service definition name for which binding needs to be created and click on Finish

SAP Cloud Platform, SAP ABAP Tutorial and Material, SAP ABAP Guides, SAP ABAP Certifications

In above “ZFLIGHT_SB_RPM”  is the service binding for service definition “ZFLIGHT_SD_RPM” created in Step 2.

The ABAP back end creates a service binding and stores it in the ABAP Repository.In the Project Explorer, the new service binding is added to the Business Services folder of the corresponding package node.

Click on the button Publish as shown below to publish Odata service locally which makes service ready for consumption

SAP Cloud Platform, SAP ABAP Tutorial and Material, SAP ABAP Guides, SAP ABAP Certifications

Odata service “ZFLIGHT_SD_RPM”  published locally and  on the left side of the form editor, the service list with the name, version of the OData V2 service is filled. The right side of the form editor shows the service information and lists the entity sets that are exposed for the service.

SAP Cloud Platform, SAP ABAP Tutorial and Material, SAP ABAP Guides, SAP ABAP Certifications

Double click on service URL on the left side to launch service URL in browser to verify the OData Metadata

SAP Cloud Platform, SAP ABAP Tutorial and Material, SAP ABAP Guides, SAP ABAP Certifications

Metadata of the service “ZFLIGHT_SB_RPM” can be viewed in the browser.

SAP Cloud Platform, SAP ABAP Tutorial and Material, SAP ABAP Guides, SAP ABAP Certifications

In order to preview the resulting UI service in Fiori Elements app view double click on the entity set Connections or right click to the context menu as shown below:

SAP Cloud Platform, SAP ABAP Tutorial and Material, SAP ABAP Guides, SAP ABAP Certifications

User interface for a Fiori elements app can viewed for OData service:

SAP Cloud Platform, SAP ABAP Tutorial and Material, SAP ABAP Guides, SAP ABAP Certifications

Before releases in order to view the results of modeled CDS view you need to go to WebIDE and create a project for smart template Fiori app and give OData service to view the List Report application but in the latest release of ABAP on SAP Cloud Platform  a plugin is configured to see the result preview of SAP Fiori Elements App.

The above steps guide you how to create read(R) only application by using Business Services Provisioning  (Service Definition and Service Binding ). In my next blogs I will show how to create transaction CUD application by using Data Modeling and Behavior ( Behavior definition and Behavior Implementation )

No comments:

Post a Comment