Defining a Data Model

Objectives

After completing this lesson, you will be able to:
  • Create an SAP Gateway project
  • Define a data model based on a template
  • Generate SAP Gateway runtime artifacts
  • Register an SAP Gateway service

SAP Gateway Project Creation

Development flow: Template based data model definition

Once you have created a project in the SAP Gateway Service Builder, you can start the data modeling process. Depending on your requirements, you can define a data model in the following ways:

Define Data Model Declaratively
You can manually define a new data model within your project — that is, you create the individual data model elements, such as entity types, entity sets, and associations, from scratch.
Import Data Model (EDMX)
You can reuse an existing data model by importing an XML-file formatted as EDMX (Entity Data Model Extension). This file can be created by external model editors like Microsoft Visual Studio or you can download the metadata of an existing OData service.
Import DDIC Structure
You can import a database view, a transparent table, or any other structure type from the ABAP Data Dictionary (DDIC) to create a new complex or entity type with ease.

We skip the declarative way for now and focus on the other two ways.

Without any service implementation, it is already possible to generate the runtime artifacts and register the service. The only function then provided by the service is to provide the metadata of the data model.

Development steps

The first development step is to create an SAP Gateway project in the SAP Gateway Service Builder. You open the SAP Gateway Service Builder by using the transaction code SEGW.

Screenshot about creating and SAP Gateway project in SEGW

A project bundles all artifacts of a service in one central location, helping you to organize the modeling process and service development. Since projects consolidate all related data, you can easily work on multiple projects in parallel and reuse data between projects before generating and activating the actual service. In addition, you can use projects to organize the movement of objects from one system environment to another ensuring that all artifacts remain together at all times.

When creating a project, you must define a name and description and set the project type. The latest and only valid project type today is the Service with SAP Annotations. All other project types are deprecated.

Hint

The name of the project should not exceed 18 characters. The reason is it that the project name is included in the names of the generated runtime artifacts. Longer project names will just be cut replacing the now missing characters by a number. But if the project name is fully readable in the runtime artifacts, it is easier to map these artifacts to the project.

As repository object, a project must be assigned to a package and transport request or saved as local object. As responsible person, the creator of the project is automatically set, but can also be changed later.

Template Based Data Model Definition

Development steps: Data model definition

The second development step is to define the data model of the service. All actions in the Data Model branch of the project result in ABAP code generated in the Model Provider Class (MPC).

Screenshot flow about importing a data model from file in SEGW

To import OData models that have been designed by OData modeling tools, you right-click the Data Model node and choose ImportData Model from File. You can also import OData models by importing the metadata file of an existing OData service.

When performing changes to such a model after it has been imported, you can perform a re-import. A wizard will support you when adding and removing elements that have changed in the model.

Models imported in that way are not linked to any ABAP components and therefore miss semantic information like translatable texts or value helps. There are also no DDIC types ready to be used in the ABAP source code. Only local types are generated.

Screenshot flow about importing a DDIC structure in SEGW

To reduce the time required to create entity types and complex types in your data model, you can import an existing DDIC structure. Although it is only a part of a complete model, in this way semantic information is included. In addition, the DDIC structures can be used in the ABAP source code.

You can import the following structure types into the SAP Gateway project:

  • Structures
  • Transparent tables
  • Views
Scrsenshot flow of Import from DDIC Structure wizard in SEGW

You can use the wizard to create entity types based on DDIC structures in an inside-out approach as follows:

  1. In the first screen, you enter a name and select if you want to create an entity or complex type.
  2. Using the value help, you select the DDIC structure to be imported.
  3. When the option Create Default Entity Set is selected, the wizard creates an entity set called <Name of entity type>Set, for example, ProductSet.
  4. In the next screen, you select the fields of the DDIC structure, which should become properties of the entity type.
  5. Finally you choose the key field(s). Every entity type in OData must have at least one key field. It is also possible to rename the properties of the entity type since the proposed values are generated based on the ABAP field names: The underscores in the field names are removed and the name segments are merged in a camel case notation.
Screenshots of entity type based on a structure type in SEGW

After the wizard is finished, you can find the entity type under Data ModelEntity Types. Beside the properties generated based on the field names of the structure type, the structure itself is bound to the entity type. This structure binding limits the possible parameters of the entity type to the fields available in the structure. That means new properties can only be added to the entity type, after they are created as fields in the bound structure.

A structure binding prevents changes in the data model that would cause errors in the source code by adding properties not part of the data types used in the implementation. In addition, when reusable units are called during runtime, the interfaces of these units must match the data types used for passing data.

Hint

A structure binding also sets the ABAP field names of the entity type based on the once of the structure type. This allows the use of MOVE-CORRESPONDING in the ABAP implementation to ease passing data between variables.

Runtime Artifacts Generation

Development steps: Runtime artifacts generation

The third development step is to generate the runtime artifacts. Now everything done so far somewhere in the SAP Gateway project will be interpreted and generated as source code and technical customizing.

Screenshot flow about generating runtime objects in SEGW

The first time you choose the Generate Runtime Objects for an SAP Gateway project, the Model and Service Definition popup appears. Here the names for the runtime artifacts can be set. The proposed names are based on the project name:

Model Provider Class
  • ZCL_<project name>_MPC_EXT
  • ZCL_<project name>_MPC
Data Provider Class
  • ZCL_<project name>_DPC_EXT
  • ZCL_<project name>_DPC
Service Registration
  • <project name>_MDL
  • <project name>_SRV

Project names, which would create too long runtime artifacts names, will just be cut replacing the now missing characters by a number. It is recommended to limit the project name to 18 characters. This makes it easier to map artifacts to a project.

Caution

The Model and Service Definition popup does only appear once. Therefore, choose the runtime artifact names wisely.
Screenshot flow about accessing the model provider class from SEGW in SE80

From the runtime artifacts in the SAP Gateway project, you can jump directly to the ABAP Workbench (SE80).

The model provider extension class inherits from the base class. The base class then inherits from abstract model classes of the SAP Gateway framework. These abstract classes provide basic implementation for every model provider class.

Screenshot and source code of the define and define_product methods of a model provider class

The model provider base class consists of generated code based on the settings done in the SAP Gateway Service Builder on project level. Today, most of the data model is defined in the project directly and therefore generated as ABAP code in the base class:

  • The DEFINE method is the central method called by the SAP Gateway framework when the data model is created. The method calls private methods of the base class generated based on artifacts defined in the project.
  • For every artifact like entity type, complex type, association, or function import, a specific method is generated following the naming rule DEFINE_<artifact>. The model attribute of the MPC provides methods to create all details of the artifact like creating entity types, properties, and entity sets.

By redefining the DEFINE method in the extension class, you can extend the data model with features not supported by the SAP Gateway Service Builder.

Screenshot flow about accessing the data provider class from SEGW in SE80

You can access the code of the data provider extension class from within the SAP Gateway Service Builder via the context menu of a class listed in the Runtime Artifacts branch.

The data provider extension class inherits from the base class. The base class then inherits from abstract data classes of the SAP Gateway framework. These abstract classes provide basic implementation for every data provider class.

Screenshot and source code of the get_entityset and productset_get_entityset methods of a data provider class

For each entity set in the data model, methods are generated in the base class to reflect the entity set’s CRUD and query operations. One set of central generic CRUD and query methods are delegating the requests to these generated methods.

For example, for an entity set called "ProductSet", the following methods are created:

  • PRODUCTSET_CREATE_ENTITY
  • PRODUCTSET_GET_ENTITY
  • PRODUCTSET_GET_ENTITYSET
  • PRODUCTSET_UPDATE_ENTITY
  • PRODUCTSET_DELETE_ENTITY

In the base class, all these generated methods raise an exception indicating that the respective OData operation is not yet implemented. This only changes if some wizard is used in the SAP Gateway Service Builder calling some reuse unit.

The data provider extension class inherits these methods. Here, you can redefine them to develop the data provisioning for the different request types. That is, to implement the service, you have to overwrite the appropriate methods from the base class in the extension class.

Caution

Never change any code in a base class, neither model nor data provider class. The next generation of runtime artifacts will just overwrite everything you developed. (You do this error only once.)

Service Registration

Development steps: Service registration and metadata test

The fourth development step is to register the service. This step must only be done once per system. Any changes to the data model or to the implementation afterward are automatically available in the registered service. The first thing done after the service registration is testing the metadata.

Screenshot of a service catalog entry in /IWFND/MAINT_SERVICE

To register a service means to create a service catalog entry based on the technical service of an SAP Gateway project. The name of the technical service acts as external service name used for calling OData requests. For services created in the customer name space, the name of the service catalog entry is also equal to the technical service name.

For services delivered by SAP, the external service name belongs to the SAP name space. But for most of these services, the SAP does not ship the service catalog entry. The reason is that the customer should decide, in which system these services should be registered. Because of this flexibility, the customers register the services delivered by SAP by themselves and, therefore, create the service catalog entry in the customer name space. The same applies to service provided by partners having an own name space.

The purpose of a service catalog entry is to forward the OData request coming in from the ICF to the implementation of the service. If this implementation resides in another system, a system alias is assigned to the service catalog entry and used as a placeholder for an RFC destination to the other system. This allows transporting the service catalog entry from a development system to a test or productive system reusing the same alias in every system. But for each system another RFC destination is assigned to the system alias to always call the suitable target system.

In an embedded deployment, the implementation resides in the same system as the service catalog entry. In this scenario, there are two possible ways to register a service:

  1. The system alias LOCAL can be used as the place holder for the RFC destination NONE, which just means that no other system is called but the current one.
  2. Since SAP_GWFND 7.50 SP04, the processing mode Co-deployed only can be set for the service catalog entry and no system alias is assigned. This means that the SAP Gateway runtime is called directly.
Screenshot of the SAP Gateway routing configuration in /IWFND/ROUTING

Since SAP_GWFND 7.51 SP05, the SAP Gateway Routing Configuration (/IWFND/ROUTING) allows the maintenance of SAP Gateway system aliases. System aliases are used for OData V2 and V4 services alike. By choosing a system alias in the list at the top, the OData V2 and V4 services registered using this alias are listed underneath. It is not possible to change these service catalog entries here. This can only be done in the /IWFND/MAINT_SERVICE and /IWFND/V4_ADMIN transactions.

Note

Before the SAP Gateway Routing Configuration, the system alias were maintained in a maintenance view accessible via the following path in the SAP Customizing Implementation Guide (SPRO):

ABAP Platform (formally SAP NetWeaver)SAP GatewayOData ChannelConfigurationConnection SettingsSAP Gateway to SAP SystemManage SAP System Aliases

Screenshot flow about how to register a service via SEGW

The SAP Gateway Service Builder enables you to register and maintain services in the SAP Gateway Service Maintenance. For this, you must expand the Service Maintenance node, select the SAP Gateway alias, and choose Register.

The Select System Alias popup is provided by the system connected via the SAP Gateway alias. The value help of the System Alias field provides a list of suitable system aliases maintained in the FES targeting the BES.

In the Add Service popup, the technical service and model names are already set. The External Service Name field is not changeable because it is defined based on the technical service. A service catalog entry must be assigned to a package or as local object. When the Continue button is chosen, one or more popups asking for a transport request will appear if a package was entered.

The latest and only valid mode today of the ICF node is Standard Mode. OAuth is an authentication option provided by the OData standard. Ask you system administrator if your system supports this.

Screenshot flow about how to register a service via /IWFND/MAINT_SERVICE

In the SAP Gateway Service Maintenance, you can register one or more SAP Gateway services at once. Choosing the Add Service button leads to a first empty list of available services. The System Alias field or selecting the Co-Deployed checkbox is mandatory to get a list of not yet registered services of the target system.

To register a service, you just need to choose the technical service name and the Add Service popup appears. You can also select multiple services via the checkbox in each line and choose the Add Selected Services button. The Add Services popup works in the same way as described before.

Note

An SAP Gateway service can only be registered once in a system. So, if a service you search for is not in the list, it may already be registered.

Define a Data Model Based on a Data Dictionary Structure

Business Example

You are a solution architect or developer in your company. You want to create a package and an SAP Gateway project. You need to import a data dictionary structure to create an entity and entity set. And you want to test the metadata in the SAP Gateway Client.

Template:
none
Solution:
GW100_S_DDICMODEL (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: Create an SAP Gateway Project

Steps

  1. In the ABAP Workbench in your SAP S/4HANA (S4H) system, create the ZGW100_##_BES package.

    1. In the SAP Easy Access of your S4H, search for ABAP Workbench or start transaction SE80.

    2. In the Object Category dropdown, in the navigation area, select Package.

    3. In the Object Name field, in the navigation area, enter ZGW100_##_BES and choose Enter.

    4. In the Create Package popup, choose Yes.

    5. Enter a short description of your choice.

    6. In the Application Component field, enter CA.

    7. Choose Continue.

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

    9. Choose Continue.

  2. In the SAP Gateway Service Builder of your S4H, create the ZGW100_##_STUDENT project and add it to your package ZGW100_##_BES.

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

    2. Choose Create Project.

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

      FieldValue
      ProjectZGW100_##_STUDENT
      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.

Task 2: Define an Entity based on a Data Dictionary Structure

Steps

  1. In the SAP Gateway Service Builder of your S4H, in your ZGW100_##_STUDENT project, import the data dictionary structure BAPI_EPM_PRODUCT_HEADER as entity Product. Select all fields except TAX_TARIF_CODE and set the PRODUCT_ID field as key.

    Note

    The field TAX_TARIF_CODE will be used in a later exercise.
    1. In the SAP Gateway Service Builder of your S4H, in the context menu of Data Model, choose ImportDDIC Structure.

    2. In the Import from DDIC Structure popup, in the Name field, enter Product

    3. In the ABAP Structure field, enter BAPI_EPM_PRODUCT_HEADER.

    4. Choose Next.

    5. To select all fields, select the BAPI_EPM_PRODUCT_HEADER checkbox.

    6. Deselect the checkbox, TAX_TARIF_CODE.

    7. Choose Next.

    8. For the PRODUCT_ID field, select the Is Key checkbox.

    9. Choose Finish.

    10. Choose Save.

  2. Examine the entity Product and the entity set ProductSet.

    1. Expand Data ModelEntity TypesProduct.

    2. Double-click Properties and examine the details on the right.

    3. Expand Data ModelEntity Sets.

    4. Double-click ProductSet and examine the details on the right.

  3. Generate the runtime objects of your project ZGW100_##_STUDENT in the ZGW100_##_BES package.

    1. Choose Generate Runtime Objects.

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

    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: Register an SAP Gateway Service using SAP Gateway Service Builder

Steps

  1. In the ABAP Workbench of your S4H, create the ZGW100_##_FES package.

    1. In the SAP Easy Access of your S4H, search for ABAP Workbench or start transaction SE80.

    2. In the Object Category dropdown in the navigation area, select Package.

    3. In the Object Name field in the navigation area, enter ZGW100_##_FES and choose Enter.

    4. In the Create Package popup, choose Yes.

    5. Enter a short description of your choice.

    6. In the Application Component field, enter CA.

    7. Choose Continue.

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

    9. Choose Continue.

  2. In the SAP Gateway Service Builder of your S4H, register and activate the service of your ZGW100_##_STUDENT project on your S4H using the LOCAL_GW system alias.

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

    2. Select the LOCAL_GW system on the right.

    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.

  3. In the SAP Gateway Client of your S4H, test the service’s metadata of your ZGW100_##_STUDENT project.

    1. In the SAP Gateway Service Builder of your S4H, choose SAP Gateway Client.

    2. In the Warning! popup, to be redirected to the selected system, choose Yes.

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

    4. Choose Execute.

      Example

      /sap/opu/odata/SAP/ZGW100_##_STUDENT_SRV/?$format=xml

      Result

      The HTTP Response displays the service document with product.
    5. Choose Add URI Option.

    6. In the Add URI Option popup, double-click $metadata.

      Example

      /sap/opu/odata/SAP/ZGW100_##_STUDENT_SRV/$metadata
    7. Choose Execute.

      Result

      The HTTP Response displays the metadata of product.

Log in to track your progress & complete quizzes