Implement a Gateway service (Part 3) – CRUD Operations

Now we can implement the rest of the CRUD(Create, Read, Update and Delete) operations. We will do it for the entityset SUBJECTSSET.

  • Create – Redefine the method SUBJECTSSET_CREATE_ENTITY

METHOD subjectsset_create_entity.
DATAls_subject LIKE er_entity.

io_data_provider->read_entry_dataIMPORTING es_data ls_subject ).

ls_subjectmandt symandt.

er_entity ls_subject.

INSERT INTO zsubjects VALUES ls_subject.
ENDMETHOD.

  • Read – Redefine the method SUBJECTSSET_GET_ENTITY

method SUBJECTSSET_GET_ENTITY.

DATAls_key_tab LIKE LINE OF it_key_tab,
lv_id      TYPE zsubjectsid,
lv_subject TYPE zsubjectssubject.

CLEAR ls_key_tab.
READ TABLE it_key_tab INTO ls_key_tab
WITH KEY name ‘Id’.
IF sysubrc EQ 0.
lv_id ls_key_tabvalue.
ENDIF.

CLEAR ls_key_tab.
READ TABLE it_key_tab INTO ls_key_tab
WITH KEY name ‘Subject’.
IF sysubrc EQ 0.
lv_subject ls_key_tabvalue.
ENDIF.

SELECT SINGLE INTO CORRESPONDING FIELDS OF er_entity
FROM zsubjects
WHERE id lv_id
AND subject lv_subject.
IF sysubrc NE 0.
CLEAR er_entity.
ENDIF.

endmethod.

 

  • Update – Redefine the method SUBJECTSSET_UPDATE_ENTITY

METHOD subjectsset_update_entity.
    DATAls_data TYPE zcl_zstudents_mpc=>ts_subjects.
    io_data_provider->read_entry_dataIMPORTING es_data ls_data ).
    ls_datamandt symandt.
    UPDATE zsubjects FROM ls_data.
ENDMETHOD.

  • Delete – Redefine the method SUBJECTSSET_DELETE_ENTITY

METHOD subjectsset_delete_entity.

    DATAls_key_tab TYPE /iwbep/s_mgw_name_value_pair,
          lv_id      TYPE zsubjectsid,
          lv_subject TYPE zsubjectssubject.

    READ TABLE it_key_tab WITH KEY name ‘Id’ INTO ls_key_tab.
    IF sysubrc 0.
      lv_id ls_key_tabvalue.
    ENDIF.

    READ TABLE it_key_tab WITH KEY name ‘Subject’ INTO ls_key_tab.
    IF sysubrc 0.
      lv_subject ls_key_tabvalue.
    ENDIF.

    DELETE FROM zsubjects WHERE id lv_id AND subject lv_subject.

ENDMETHOD.

Advertisements

Important SAP Gateway transactions

 

tx Description
 SEGW  Service Builder
/IWFND/MAINT_SERVICE Maintain services
/IWFND/GW_CLIENT Execute and test services
/IWFND/ERROR_LOG Error Log for Gateway
/IWFND/TRACES Trace tool
/IWFND/APPS_LOG Application log
/IWBEP/REG_SERVICE Register services. Functionality is built-in SEGW

Note: Always precede transactions starting with / with /n.

Implement a Gateway service (Part 2) – Coding the Query operation

After having defined the Data Model in part1, you have to implement the business logic in ABAP to retrieve the data from the backend.

  • Expand the Runtime Artifacts folder.
  • Double-click on the Data Provider Extension Class, namely ZCL_ZSTUDENTS_DPC_EXT. Double-click on the class again to open the Class Builder.
  • Go to change mode.
  • Select the method STUDENTHEADERSET_GET_ENTITYSET and click on Redefine Method.

Screenshot_20180126_170237

  • Some placeholder code appears in the method as shown below.

Screenshot_20180126_170516

  • Delete the placeholder code and type the code shown below. Then, activate.

21

SELECT FROM zstudent
INTO CORRESPONDING FIELDS OF TABLE et_entityset.

IF et_entityset IS INITIAL.
mo_context->get_message_container)->add_message(
EXPORTING
iv_msg_type   symsgty
iv_msg_id     symsgid
iv_msg_number symsgno
iv_msg_v1     ‘No records in db’
).

RAISE EXCEPTION TYPE /iwbep/cx_mgw_busi_exception
EXPORTING
textid            /iwbep/cx_mgw_busi_exception=>business_error
message_container mo_context->get_message_container).
ENDIF.

 

  • The Redefinitions folder shows that the method has been successfully redefined.

11

  • Check the output again for the entity set StudentHeaderSet. Data is now fetched from the backend table as expected.

12

 

Implement a Gateway service (Part 1) – Define a data model based on Data Dictionary Structure

We are going to define a Gateway service in the Service Builder SEGW.

The first stage of implementing a service is to define the data model. For that, we will create custom z tables from scratch.

  • Create 2 tables named ZStudent and ZSubjects as shown below.

3.png

22

 

  • In transaction SEGW, create a new project.

Screenshot_20180126_124330

  • Give the project name ZStudents and a description.

Screenshot_20180126_124406

  • Right-click on Data Model node and choose Import->DDIC Structure. We are not creating entity types from scratch as the data tables are already created in the Data Dictionary in the previous step.

Screenshot_20180126_124653

  • Enter a Name, ABAP Structure, select Create Default Empty Set and click on next.

Screenshot_20180126_124757

  • Select all the fields to include in the service for the student and click on Next.

23

  • Specify the field ID as key.  Click on Finish.

24

  • The result is as shown below. The Entity Type, Entity Set and the Service Implementation have been generated.

Screenshot_20180126_131143

  • Similarly, create the child Entity type and Entity Se. Import another DDIC object for Subjects.

Screenshot_20180126_132025

  • Click on the Generate Runtime Objects icon. The Service Builder will generate Model Provider classes and Data Provider classes.

5

  • Open the Service Maintenance node and click on Register. Specify the local package, the External Service Name and click on Ok.

Screenshot_20180126_132334

  • Click on Maintain to open the transaction /IWFND/MAINT_SERVICE.

6

Select the service from the Service Catalog and in the ICF Nodes section, click on SAP Gateway Client to test the service.

7

The URI of the service is /sap/opu/odata/sap/ZSTUDENTS_SRV/. You can use various oData Query options such as $metadata, ?format=xml and sap-ds-debug=true.

  • Click on Execute to test the service.

8

  • Click on Add URI Option and choose $metadata and sap-ds-debug=true options.
  • Execute to see the results below.

9

Do note that at this point we can only access the metadata of the service. The service will not return any data yet. If you try to get the data for an entity set, you will get an error as shown below.

Screenshot_20180126_170256

Business data retrieval logic will be implemented in part 2.

SAP Gateway sample service for GW100 practice

You have the SAP Handbook GW100 and looking for the gateway practice sample service GW100_T_BASICGW_SRV. No worries. The service is installed by default in SAP Netweaver 7.51 innovation package and is named GWSAMPLE_BASIC instead.

Use transaction SEGW and open the project /IWBEP/GWSAMPLE_BASIC.

Technical Service Name: ZGWSAMPLE_BASIC

External Service Name: GWSAMPLE_BASIC

Namespace: IWBEP

 

Accessing the service via URI:

  • Service Document
http://<HOST>:<PORT>/sap/opu/odata/IWBEP/GWSAMPLE_BASIC/?format=xml
  • Service Metadata Document
http://<HOST>:<PORT>/sap/opu/odata/IWBEP/GWSAMPLE_BASIC/?$metadata

 

Examples

Accessing the service with the parameter sap-ds-debug=true, which renders the response as an HTML page which can be viewed in a browser.

URI: http://localhost:8000/sap/opu/odata/IWBEP/GWSAMPLE_BASIC/?sap-ds-debug=true

accessing the service with sap-ds-debug.png

URI with $metadata

http://localhost:8000/sap/opu/odata/IWBEP/GWSAMPLE_BASIC/$metadata?sap-ds-debug=true

service with metadata.png

 

Gateway Service Architecture

A gateway service is built using 2 ABAP classes:  Data Provider Class and Model Provider Class.

Data Provider Class – contains the implementation of the service’s functionality.
Model Provider Class – provides metadata about the service.

These 2 classes do not communicate with each other. They are linked using configuration.

Two configuration wrappers are created for each of the above classes.

Service Group – configuration wrapper for the Data Provider Class. Contains the internal and external service name. Note that the external name can be different and need not start with Z. By default, SAP sets both of them as the same.

Technical Model Object – configuration wrapper for the Model Provider Class.

A service is complete when we associate the Service Group and the Technical Model Object. This is shown in the diagram below.

48

Finally, the service needs to be added to the service catalog.

Use of Sybase’s Unwired Platform is not mandatory for consuming Gateway services. Any language can be used, as long as it supports read data from XML and via HTTPS.