Redefining an SAP Gateway Service

Objective

After completing this lesson, you will be able to redefine SAP Gateway services

SAP Gateway Service Extension Details

Development flow: SAP Gateway service redefinition

By redefining an existing SAP Gateway service, you create a new service that inherits all functionality of the original one. This is used to extend an SAP-delivered service in the customer namespace or if you want to create a new version of the service keeping the original one.

SAP Gateway Services Redefinition Process

Development steps: SAP Gateway service redefinition

The development starts with the creation of a new project. Data model definition and service implementation are performed in just one step by choosing RedefineOData Service (SAP GW) from the context menu of the data model folder. After generating the runtime artifacts, the implementation can be tested.

The real extension starts with changing the data model, which does not only include beautification but also adding additional data model elements like entity types or associations. New elements will get available after generating again the runtime artifacts. Then the implementation adding the new functionality can follow.

Screenshot flow about importing properties defined in append structure in SEGW

Usually entity types delivered in SAP-services are bound to DDIC structures. If you want to add additional properties to such an entity type, you must first add these as new components to the DDIC structure by using include or append structures. For adding these new components as properties, right-click the entity type and choose ImportProperties.

Screenshot flow of Import Properties wizard

The Import Properties Wizard displays components added via include or append structures as normal properties. Select them and change the name and label as you wish. It is even possible to add new key components.

Screenshots of provider classes in a redefinition in SE80

As usual, you get a set of data and model provider classes. In the model provider base class, the new method get_extended_model is used to set the technical model and service, which should be to imported. The model provider extension class can be used to enhance the data model as usual.

The data provider base class inherits the original data provider extension class. That means that all die implementation of the original SAP Gateway service, the generated part as well as the hand-written one, is directly available in the new service. Enhancements can be implemented as usual by redefining the methods in the new extension class – but also by generating code in the new base class.

Adding Additional Entities

The SAP Gateway Service Builder does not generate CRUD methods if you add additional entity sets to a service that has been created using redefinition. You must add these methods yourself and redefine the generic CRUD methods in the DPC_EXT to call your methods.

To redefine a generic method, perform the following steps:

  1. Check for the name of the new entity set.
  2. Call the super method for all other entity sets.

As an example, the /iwbep/if_mgw_appl_srv_runtime~get_entityset may look like this:

Code Snippet
12345678910
METHOD /iwbep/if_mgw_appl_srv_runtime~get_entityset. ... CASE io_tech_request_context->get_entity_set_name( ). WHEN '<NewEntitySet>'. me-><NewEntitySet>_get_entity( ... ). " The method you created WHEN OTHERS. super->/iwbep/if_mgw_appl_srv_runtime~get_entityset( ... ). ENDCASE. ... ENDMETHOD.

The other methods are implemented in a comparable way.

SAP Gateway Service Extension Options

Registration options for a redefinition

When registering the redefinition, you have two options:

  • Register a new service and preserve the original one
  • Replace the original service (since AS ABAP 7.40 SP08)

The first option enables you to continue using the original app and use a new extended app in parallel with it. The second option makes it impossible to use the original app anymore, because it involves replacing the project behind the original service with the redefinition. Both are valid options, depending on the use case.

An example for a separate service would be an extension that is only allowed to be used by a few users in a company. Those users get additional authorization to use the extended service getting additional data and functions defined in the extension. All others continue to use the original service.

An example for a replaced service would be an extension of some mandatory field, which should be used by all users. If the original service would be used to create data sets without the mandatory field of the extension, invalid data sets would be created in the database.

Screenshots of service extension options in SEGW

When generating the runtime objects for the first time, a checkbox offers the possibility to overwrite the original base service with the new extended one. By choosing the checkbox, the new service will be registered using the name of the original one making the original one inaccessible. The checkbox can only be chosen once.

Caution

Choose the checkbox wisely because it cannot easily be undone.

Simple Field Extensibility

Source code implementing simple field extensibility

When using simple field extensibility (SFE) (available since AS ABAP 7.40 SP10), SAP Gateway classes (MPC and DPC) consider the fields in the extension includes in the DDIC structure at runtime. The result is that the entity type is extended automatically with components added to the append or include structures. No redefinition project is needed to add properties to an entity type.

Prerequisites are the following:

  • The entity type is based on a DDIC structure.
  • There is an extension include for the DDIC structure.
  • In the define method of the MPC_EXT, the add_auto_expand_include method is called for the entity type .

For the last prerequisite, the define method must be redefined in the MPC_EXT. Call the super-method and get the entity type from the model attribute. The add_auto_expand_include method only needs the name of the DDIC structure of the entity type and if conversion exists should be propagated.

The following block provides the example source code in a copy-friendly way:

Code Snippet
12345678910
METHOD define. super->define( ). model->get_entity_type( iv_entity_name = 'PurchaseOrder' ) ->add_auto_expand_include( EXPORTING iv_include_name = 'SEPM_REF_APPS_PO_APV_PO' iv_bind_conversions = 'X' ). ENDMETHOD.

The new properties are generated based on the DDIC structure components. If you for any reason are not fine with the result, you may set the details of the property by implementing the Business Add-In (BAdI)/iwbep/if_mgw_bd_modify_model~modify_auto_expand_properties.

In the following example, the precision of WEIGHT_TOTAL is increased to "16":

Code Snippet
123456789101112
METHOD /iwbep/if_mgw_bd_modify_model~modify_auto_expand_properties. DATA: ls_property LIKE LINE OF it_properties. IF iv_include_name EQ 'SEPM_REF_APPS_PO_APV_PO' AND iv_entity_type_name EQ 'PurchaseOrder'. LOOP AT it_properties INTO ls_property. IF ls_property-internal_name = 'WEIGHT_TOTAL'. ls_property-property->set_precision( 16 ). ENDIF. ENDLOOP. ENDIF. ENDMETHOD.

The DPC should be implemented in a way that it can handle the retrieval of the data of the extended DDIC structure automatically, for example by using corresponding.

Properties that have been created by using SFE will contain the annotation sap:is-extension-field="true" in the metadata of the service.

How to Open the Extended Approve Purchase Order App

Watch this video to see, how the extended Approve Purchase Order app differs from the original one:

Extend an SAP Gateway Service of an SAP Fiori Application

Business Example

You are a developer or solution architect in your company. You need to extend the SAP Gateway service of an existing SAP Fiori application, add a new properties to an entity, and enhance the ABAP code.

Template:
none
Solution:
GW100_S_EXTEND (SAP Gateway Project)

Note

This exercise requires an SAP Learning system. Login information are provided by your system setup guide.

Note

Whenever the values or object names in this exercise include ##, replace ## with the number of your user.

Task 1: Redefine an SAP Gateway Service using all Entities

Steps

  1. In the SAP Gateway Service Builder of your SAP S/4HANA (S4H) system, create a new project named ZGW100_##_EXTEND in your ZGW100_##_BES package.

    1. In the SAP Easy Access of your S4H, search for SAP Gateway Service Builder or start transaction SEGW.

    2. In the SAP Gateway Service Builder, choose Create Project.

    3. In the Create Project popup, enter the following values:

      FieldValue
      ProjectZGW100_##_EXTEND
      Descriptionyour choice
      PackageZGW100_##_BES
    4. Choose Continue.

    5. Choose Save.

    6. In the Prompt for transportable workbench request popup, in the Request field, select the transport request provided to you.

    7. Choose Continue.

  2. Redefine the EPM_REF_APPS_PO_APV_SRV service using all entities.

    1. In the context menu of Data Model, choose RedefineOData Service (SAP GW).

    2. In the Redefine Service popup, in the Technical Service Name field, enter EPM_REF_APPS_PO_APV_SRV by using the value help.

    3. Choose Next.

    4. Choose Select All.

    5. Choose Finish.

    6. Choose Save.

Task 2: Extend a Data Model and Generate a new Redefined Service

Steps

  1. In the SAP Gateway Service Builder of your S4H, add the WEIGHT_TOTAL property with label Weight and WEIGHT_UNIT with label Weight Unit to the PurchaseOrder entity.

    1. In the SAP Gateway Service Builder of your S4H, expand the Data ModelEntity Types node.

    2. Right-click PurchaseOrder and choose ImportProperties.

    3. Select the checkboxes for WEIGHT_TOTAL and WEIGHT_UNIT.

    4. Choose Next.

    5. Change the Label for WEIGHT_TOTAL to Weight Total and the one for WEIGHT_UNIT to Weight Unit.

    6. Choose Finish.

  2. Generate the runtime objects in your ZGW100_##_BES package creating the new service ZGW100_##_EXTEND_SRV.

    Caution

    Do NOT overwrite the base service.
    1. Choose Generate Runtime Objects.

    2. In the Model and Service Definition popup, to accept the proposed names, choose Continue.

      Caution

      Do NOT select the Overwrite Base/Extended Service checkbox.
    3. In the Create Object Directory popup, in the Package field, enter ZGW100_##_BES.

    4. Choose Save.

    5. In the Prompt for transportable workbench request popup, in the Request field, select the transport request provided to you.

    6. Choose Continue.

Task 3: Redefine and Implement a Method of a Redefined Service

Steps

  1. In the SAP Gateway Service Builder of your S4H, redefine the PURCHASEORDERS_GET_ENTITY method in your ZCL_ZGW100_##_EXTEND_DPC_EXT class to return the total weight of all products of a purchase order in addition. For that you need to join the suitable fields of purchase order items (CDS view sepm_i_purchaseorderitem_e) and products (CDS view sepm_i_product_e).

    Hint

    You may use the function module UNIT_CONVERSION_SIMPLE for converting the different weight units.
    1. In the SAP Gateway Service Builder of your S4H, expand the Runtime Artifacts node.

    2. In the context menu of the data provider extension class ZCL_ZGW100_##_EXTEND_DPC_EXT, choose Go To ABAP Workbench.

    3. In the Class Builder, choose Display <-> Change.

    4. Place the cursor in the PURCHASEORDERS_GET_ENTITY method and choose Redefine.

    5. Write the following code or copy it from the solution:

      Code Snippet
      1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
      METHOD purchaseorders_get_entity. DATA: BEGIN OF ls_po_product_weight, purchaseorder TYPE sepm_i_purchaseorderitem_e-purchaseorder, purchaseorderitem TYPE sepm_i_purchaseorderitem_e-purchaseorderitem, product TYPE sepm_i_purchaseorderitem_e-product, quantity TYPE sepm_i_purchaseorderitem_e-quantity, weight TYPE sepm_i_product_e-weight, weightunit TYPE sepm_i_product_e-weightunit, END OF ls_po_product_weight, lt_po_product_weights LIKE SORTED TABLE OF ls_po_product_weight WITH UNIQUE KEY purchaseorder purchaseorderitem, lv_weight_total TYPE sepm_ref_apps_po_apv_po-weight_total. CONSTANTS: lcv_weight_unit TYPE snwd_dim_unit VALUE 'KG'. super->purchaseorders_get_entity( EXPORTING iv_entity_name = iv_entity_name iv_entity_set_name = iv_entity_set_name iv_source_name = iv_source_name it_key_tab = it_key_tab it_navigation_path = it_navigation_path io_tech_request_context = io_tech_request_context IMPORTING er_entity = er_entity es_response_context = es_response_context ). * Fill the fields for weight and weight unit SELECT FROM sepm_i_purchaseorderitem_e AS poi INNER JOIN sepm_i_product_e AS pd ON poi~product = pd~product FIELDS poi~purchaseorder, poi~purchaseorderitem, poi~product, poi~quantity, pd~weight, pd~weightunit WHERE poi~purchaseorder = @er_entity-po_id INTO CORRESPONDING FIELDS OF TABLE @lt_po_product_weights. CLEAR lv_weight_total. LOOP AT lt_po_product_weights INTO ls_po_product_weight. " Convert weight to one unique weight unit IF ls_po_product_weight-weightunit <> lcv_weight_unit. CALL FUNCTION 'UNIT_CONVERSION_SIMPLE' EXPORTING input = ls_po_product_weight-weight unit_in = ls_po_product_weight-weightunit unit_out = lcv_weight_unit IMPORTING output = ls_po_product_weight-weight EXCEPTIONS conversion_not_found = 1 division_by_zero = 2 input_invalid = 3 output_invalid = 4 overflow = 5 type_invalid = 6 units_missing = 7 unit_in_not_found = 8 unit_out_not_found = 9 OTHERS = 10. IF sy-subrc <> 0. " Message Container mo_context->get_message_container( )->add_message_text_only( EXPORTING iv_msg_type = /iwbep/if_message_container =>gcs_message_type-error iv_msg_text = 'Conversion of weight failed'(con) ). 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. ENDIF. lv_weight_total = lv_weight_total + ls_po_product_weight-weight * ls_po_product_weight-quantity. ENDLOOP. er_entity-weight_total = lv_weight_total. er_entity-weight_unit = lcv_weight_unit. ENDMETHOD.
    6. Choose Activate.

    7. In the Inactive Objects popup, select all objects and choose Continue.

    8. Choose Exit.

Task 4: Register and Test the Redefined Service

Steps

  1. In the SAP Gateway Service Builder of your S4H, register and activate the service in your S4H using your ZGW100_##_FES package.

    1. In the SAP Gateway Service Builder of your S4H, double-click the Service Maintenance node.

    2. Right-click the Local_GW system.

    3. Choose Register.

    4. In the Select System Alias (Hub to Backend) popup, in the System Alias field, enter LOCAL.

    5. Choose Continue.

    6. In the Add Service popup, in the Package Assignment field, enter ZGW100_##_FES.

    7. Choose Continue.

    8. For every popup, choose the transport request provided to you.

    9. Choose Save.

  2. In the SAP Gateway Client of your S4H, test if the read operation for a purchase order contains WeightTotal and WeightUnit.

    1. Choose SAP Gateway Client.

    2. In the SAP Gateway Client, select HTTPS as Protocol.

    3. Choose Entity Set.

    4. In the Entity Sets popup, double-click PurchaseOrders.

      Example

      /sap/opu/odata/SAP/ZGW100_##_EXTEND_SRV/PurchaseOrders
    5. Choose Execute.

      Result

      The HTTP Response displays all purchase orders.

      Note

      The WeightTotal and WeightUnit is empty for all purchase orders because you only implemented the read method. In our application scenario, the weight is not needed in the query method.
    6. In the Request URI field, add the key of a purchase order to the URI.

      Example

      /sap/opu/odata/SAP/ZGW100_##_EXTEND_SRV/PurchaseOrders('300000000')
    7. Choose Execute.

      Result

      The HTTP Response displays the WeightTotal and WeightUnit for the purchase order.

Log in to track your progress & complete quizzes