Quantcast
Channel: SCN : All Content - SAP Gateway
Viewing all articles
Browse latest Browse all 2823

OData service development with SAP Gateway - code-based service development - Part I

$
0
0

Introduction

 

In this blog I would like to show the basics of OData service development with SAP Gateway when using code based service implementation as it is shown in the upcoming SAP CodeJam events about OData service development with SAP Gateway.


Though the recommended approach for OData service development in the most recent SAP NetWeaver releases is to use CDS views there are still a number of use cases where code based service implementation is a valid option.

  1. Your SAP backend system is based on a SAP NetWeaver release 7.31 or earlier
  2. Your system is based on SAP NetWeaver 7.40 or later but the business logic you want to re-use has been implemented in ABAP code
  3. It is not possible to implement your business logic in CDS views

 

As an example we will use a simple OData model based that consist out of SalesOrders and SalesOrderItems with the option to navigate from a SalesOrder to the correponding line items.

 

 

Prequisites

 

The examples shown in this blog are based on the latest SAP NetWeaver release 750so that we can compare the different implementation approaches

 

 

Creating a basic OData service

 

We will perform the following steps:

 

  • Use the SAP Gateway Service Builder to create a new project
  • Import a DDIC structures for SalesOrder to create the data model
  • Generate, register and test the OData service using the Gateway Client
  • Perform a first implementation of the GET_ENTITYSET method

 

Create a new Service Builder project

 

  • Start the Gateway Service Builder by running transaction SEGW.

    image010.png

  • Click on the Create Project button
    image011.png
  • Provide the following information:

     

    Project: ZE2E100_XX

    Description: ZE2E100_XX

    Service Package: $TMP

     

    Note: Replace XX with your session-id / group number.

     

    image012.png

     

     

     

    • Press Continue
    • Press Save

    Import a DDIC structures for SalesOrder


    • Right-click on Data Model and select Import --> DDIC Structure.


    image013.png

    • In the first step of the wizard provide:

      In the field Name, enter SalesOrder
      In the field ABAP Structure, enter SEPM_ISOE.
      Click
      Next.

      image014.png

    • In the second step of the wizard provide:

      Select the checkbox for SEPM_ISOE.
      Deselect the checkbox for MANDT,
      Click Next.

      image015.png

    • In the third screen of the wizard

      Select the checkbox Is Key for the field SALESORDER.
      Click Finish.
      image016.png
    • Expand Folder Properties and examine the entity SalesOrder and the entity set SalesOrderSet.

      (For example the property names, the Edm Core Type values and the ABAP field names)
      image017.png
    • Press Save
    • Press the Check Project Consistency button.
    • Verify that no errors were found.
    • Press the Generate Runtime Objectsimage018.pngbutton.

      Note: Using the Generate Runtime Objects button automatically saves the project.

    • Leave all values as defaulted and press Enter.

    image019.png

    • Choose Local Object in the Create Object Directory Entry popup.
    • Verify that the generation was successful.

    image021.png


    Now you have created an (empty) service implementation in the SAP backend.

     

     

    Register and Test the OData service using the Gateway client

     

    • Expand the node Service Maintenance and right-click on GW_HUB and select Register.

    image022.png

    • In the Add Service popup leave all values as defaulted and press the Local Object button to assign the artifacts to the $tmp package. Then press Enter to continue

    image023.png

    • Double-click on the GW_HUB entry under Service Maintenance. Verify that the registration status on the right-hand side is showing a green traffic light.

    image024.png

    • In the navigation tree right-click on GW_HUB and select SAP Gateway Client.

      Alternatively click on the SAP Gateway Client button in the detail section.
      image025.png
    • Confirm the warning popup that warns you that you will be redirected to the selected system. Select Yes.
    • This opens up the SAP Gateway Client in a new screen. The URI of the service document is already defaulted.
      Press the Execute button.

    image027.png

    • As a result you get the service document of your OData service in the HTTP response window

    image028.png

    • Press the Button “Entity Sets” and select SalesOrderSet.

    image029.png

    • After pressing Execute button which executes the following URI

      /sap/opu/odata/SAP/ZE2E100_XX_SRV/SalesOrderSet

      you see an error message that indicates that the method SALESORDERSET_GET_ENTITYSET has not yet been implemented.

    image030.png



    Provision the GET_ENTITYSET method of the service using ABAP code

     

    • Open the Service Builder Project again (if you have closed it)
    • Expand the node Service Implementation and expand SalesOrderSet.
      Now right-click on GetEntitySet(Query) and choose
      Go to ABAP Workbench.

    image031.png

    • Confirm the warning

      “Operation SALESORDERSET_GET_ENTITYSET has not yet been implemented”
    • Switch to edit mode, scroll down to the method SALESORDERSET_GET_ENTITYSET and make sure to select it.

      Click on the Redefine Method button.

    image033.png

    • Use Copy&Paste to copy the entire coding shown below into the salesorderset_get_entityset method. Please note: This is a new syntax for ABAP SQL.

    select * from sepm_i_salesorder_e into corresponding fields of table @et_entityset.
    image034.png

      • Info: The replaced coding selects data from the CDS view sepm_i_salesorder_e.

        The results are filled into the corresponding fields of the return structure et_entityset of the get-entityset method.
        This works out of the box since the structure SEPM_ISOE was used to create the entity type using DDIC import. This structure is the so called sqlViewName of the CDS view sepm_i_salesorder_e. The source code of the DDL source can be viewed using the report RUTDDLSSHOW.
    • Click on Activate.
    • Confirm the activation popup.
    • Navigate back to the Service Builder main screen using the green back image035.pngbutton multiple times.
    • In the navigation tree right-click on GW_HUB and select SAP Gateway Client.

      Alternatively click on the SAP Gateway Client button in the detail section.

    image036.png

    • This opens up the SAP Gateway Client in a new screen. The URI of the service document is already defaulted.
      Press the Button “Entity Sets” and select BusinessPartnerSet
      The request URI field will contain the following value:

      /sap/opu/odata/SAP/ZE2E100_<XX>_SRV/SalesOrderSet

     

             (Replace ‘<XX>’ by your group number.)

     

             After pressing Execute button you see a list of sales orders.

     

    image037.png

     

    • You can retrieve the number of business partners by adding “/$count” to the request URI

      /sap/opu/odata/SAP/ZE2E100_<XX>_SRV/SalesOrderSet/$count
      Replace ‘<XX>’ by your group number and press Execute


    image038.png


    Add support for $filter to your code

     

    • Open the Service Builder Project again
    • Expand the node Service Implementation and BusinessPartnerSet and choose the entry GetEntitySet (Query).
      Now right-click on GetEntitySet (Query) and choose
      Go to ABAP Workbench.

    image039.png

    • This time the ABAP editor will open the method salesorderset_get_entityset  immediately.
      Switch to the change mode and replace the code with the code shown below.
      The code retrieves the $filter statement as an osql statement for the where clause.
      The data is retrieved and inserted into the return table et_entityset.


    data: lv_osql_where_clause type string.
    lv_osql_where_clause
    = io_tech_request_context->get_osql_where_clause( ).
    select * from sepm_i_salesorder_e
    into corresponding fields of table @et_entityset
    where (lv_osql_where_clause
    ).


    image040.png

    • Click on Activate.
    • Navigate back to the Service Builder main screen using the back button
    • In the navigation tree right-click on GW_HUB and select SAP Gateway Client.

      Alternatively click on the SAP Gateway Client button in the detail section.
    • This opens up the SAP Gateway Client in a new screen. The URI of the service document is already defaulted.
    • Replace the URI with the following:

      /sap/opu/odata/SAP/ZE2E100_XX_SRV/SalesOrderSet?$filter=Grossamountintransaccurrency ge 100000&$select=Salesorder,Grossamountintransaccurrency,Transactioncurrency&$format=json

      Replace ‘<XX>’ by your group number and press Execute

      This will deliver a list of four sales orders whose gross amount exceeds 100.000 Euro and will only show the sales order number and the gross amount.

      500000005
      500000030
      500000034
      500000045

    image041.png


    Add support for additional query options to your code

     

     

    • Open the Service Builder Project again
    • Expand the node Service Implementation and right-click on SalesOrderSet and choose the entry GetEntitySet (Query).
      Now right-click on GetEntitySet (Query) and choose
      Go to ABAP Workbench.


    • This time the ABAP editor will open the method salesorderset_get_entityset immediately.
    • Switch to the edit mode.
      Replace the code with the code shown below.
      The code retrieves the values for $top and $skip and the $filter statement as an osql statement for the where clause.The data is retrieved and in the end it is checked whether $inlinecount is used (which is a default for SAPUI5)


     

        data: lv_osql_where_clause type string,
    lv_top              
    type i,
    lv_skip             
    type i,
    lv_max_index        
    type i,
    n                   
    type i.
    *- get number of records requested
    lv_top
    = io_tech_request_context->get_top( ).
    *- get number of lines that should be skipped
    lv_skip
    = io_tech_request_context->get_skip( ).
    *- value for maxrows must only be calculated if the request also contains a $top
    if lv_top is not initial.
    lv_max_index
    = lv_top + lv_skip.
    endif.
    lv_osql_where_clause
    = io_tech_request_context->get_osql_where_clause( ).

    select * from sepm_i_salesorder_e
    into corresponding fields of table @et_entityset
    up to @lv_max_index rows
    where (lv_osql_where_clause).
    *- skipping entries specified by $skip
    if lv_skip is not initial.
    delete et_entityset to lv_skip.
    endif.
    *-  Inlinecount - get the total numbers of entries that fit to the where clause
    if io_tech_request_context->has_inlinecount( ) = abap_true.
    select count(*from   sepm_i_salesorder_e where (lv_osql_where_clause) .
    es_response_context
    -inlinecount = sy-dbcnt.
    else.
    clear es_response_context-inlinecount.
    endif.

    • Click on Activate.
    • Navigate back to the Service Builder main screen using the back button  multiple times.
    • In the navigation tree right-click on GW_HUB and select SAP Gateway Client.

      Alternatively click on the SAP Gateway Client button in the detail section.
    • This opens up the SAP Gateway Client in a new screen. The URI of the service document is already defaulted.
    • Replace the URI with the following:

      /sap/opu/odata/SAP/ZE2E100_XX_SRV/SalesOrderSet?$filter=Grossamountintransaccurrency ge 100000&$select=Salesorder,Grossamountintransaccurrency,Transactioncurrency&$top=2&$skip=1&$inlinecount=allpages&$format=json

      Replace ‘<XX>’ by your group number and press Execute
      This will deliver a list of only 2 sales orders whose gross amount exceeds 100.000 EUR. The first sales order 500000005 is skipped and only the first 2 of the rest of the list (highlighted in bold) are shown.
      The inlinecount however shows that in total 40 sales orders would fit to the filter criteria.

      500000005
      500000030 <--
      500000034 <--
      500000045

      Please note:
      $skip, $top and $inlinecount are used for client side paging. SAPUI5 uses this to calculate how many pages one has to navigate through.


    image043.png



    What is now left is the implementation of the GET_ENTITY method of the entity set SalesOrderSet and the modelling and implementation of the navigation between sales order header and the line items.


    This will be described in the second part of this blog post OData service development with SAP Gateway - code-based service development - Part II because the editor refused to let me add any additional screen shots at this point .


    Viewing all articles
    Browse latest Browse all 2823

    Trending Articles



    <script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>