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

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.