7.3 Modeling an OData Service
OData model creation is at the heart of application development in SAP Gateway. In this section, we’ll give you an overview of the OData service modeling methods and discuss in detail the different modeling phases for an OData service.
There are two ways to create OData services with SAP Gateway:
-
Service development
This is a code-based option and is very flexible for developing highly efficient and specialized services. -
Service generation
This is a quicker approach to building a service with a lot less effort. There are three options to generate SAP Gateway services with this approach, which you can choose from (see Figure 7.22):
In the service-creation process, there are three main phases (see Figure 7.22):
-
Phase 1: Data model definition
In this phase, you define your model (i.e., define the entity types, entity sets, and associations your service will use). -
Phase 2: Service implementation
In this phase, you implement the operations supported by the service. If you’re using the RFC/BOR Generator, then the implementation takes place by mapping the OData model to the methods of the RFC function module or a BOR object. -
Phase 3: Service maintenance
In this last phase, you publish the service. This is usually a one-time activity (i.e., you don’t have to revisit this step, even if you update the model or implementation).
Figure 7.22 OData Service-Creation Process
Application developers can then create services in several ways and implement them by binding to the existing data sources or new data sources.
We covered the basic concepts of OData and SAP Gateway Service Builder in the previous sections. In the next sections, we’ll look at the different steps for the service development and service-generation processes.
7.3.1 Importing OData Services
To reduce the time required to create entity types and complex types in your data model, SAP Gateway Service Builder provides various import options, used for both the service-development and service-generation processes. In this section, we’ll look at the options for importing OData services. Under the Data Model folder (shown in Figure 7.23), you’ll see different entries in which you can create different OData entities.
The first step to model an OData service is to create entity types and assign them some properties. You can create the entity types manually, you can import them (from the context menu, under Import, choose Data Model from File, DDIC Structure, RFC/BOR Interface, or Search Help), or you can redefine the services (from the context menu, under Redefine, choose BOPF, OData, BW Query, or GenIL).
In the sections that follow, we’ll walk through the different functions shown in Figure 7.23 that can be used to import a data model: Data Model from File, DDIC Structure, RFC/BOR Interface, and Search Help.
Figure 7.23 Import Functions
Data Model from File
The Data Model from File function allows you to import the data model from files such as EDMX files (created using Microsoft Visual Studio) or XML files (metadata files). An EDMX file is an XML file that defines the conceptual model, a storage model, and the mapping between these models. To import the EDMX file, right-click Data Model and choose Import • Data Model from File. This opens the File Import wizard.
Follow these steps to use the wizard:
-
Click Browse to import the data model file (see Figure 7.24).
Figure 7.24 Importing File
-
Select the data model file, then click Open (see Figure 7.25). Click Next.
Figure 7.25 Selecting EDMX File
-
Click Allow to grant access to the file (see Figure 7.26).
Figure 7.26 Granting Access
- Click Finish, and you’ll see the data model imported into the project (see Figure 7.27).
Figure 7.27 Imported Data Model
DDIC Structure
These data dictionary objects connect the database layer and the SAP application layer. For every transparent table, there is a physical table on the database. When a table is activated, a physical table definition is created in the database that the table definition stores in the ABAP Data Dictionary (DDIC). You can import views, database tables, or structures using the DDIC structure import method. You can then reuse the structure to develop new entity types and complex types easily, reducing the development time significantly.
To use this option, follow these steps:
- Right-click Data Model, then choose Import • DDIC Structure to open the DDIC Structure Import wizard.
-
Enter the Name, and select an ABAP Structure. Click Next (see Figure 7.28).
Figure 7.28 Importing DDIC Structure
-
Select the parameters/fields and click Next (see Figure 7.29).
Figure 7.29 Selecting Parameters
- Click Finish, and you’ll see the data model imported into the project (see Figure 7.30).
RFC/BOR Interface
The ABAP back-end system includes powerful tools to generate services quickly based on data from different SAP applications. Standard SAP APIs such as RFCs, BAPIs, or Business Object Repository (BOR) make it easier to get data from different SAP Business Suite applications. This approach is commonly used, because most SAP Gateway OData services are based on remote function modules. This function again reduces the development time needed to create entity types in your data model. Using this function, you can import existing data sources and create new entity types easily by reusing the imported sources.
To import the RFC/BOR, right-click Data Model, then choose Import • RFC/BOR Interface. This opens the RFC/BOR Import wizard. At the end of this section, we’ll show you how to create an OData service using this method (see Figure 7.31).
Figure 7.31 Importing RFC/BOR Interface
Search Help
The Search Help function allows you to import and reuse an existing search help from the system to create a new entity type. To enable the import of search help, follow these steps:
- Right-click Data Model, then choose Import • Search Help. This opens the Import from Search Help wizard.
-
Enter the Entity Type Name, then select the Import Search Help field. Click Next (see Figure 7.32).
Figure 7.32 Selecting Search Help
-
Select the parameters and click Next (see Figure 7.33).
Figure 7.33 Selecting Parameters
- Click Finish, and you’ll see the search help imported into the project (see Figure 7.34).
So far, we’ve reviewed the different import methods available in SAP Gateway Service Builder. Next, let’s build a data model using one of the import methods and generate an OData service.
Exercise
In this exercise, we’ll show you how to create a data model and generate an OData service by importing the RFC/BOR interface. In this scenario, we’ll be using a Sales Order Header data (BOR) example.
Create a Project
The first step is to create a project by following these steps:
- Log in to the ABAP back-end server, and run Transaction SEGW.
-
Create a new project by clicking
.
- Enter the Project name and Description, then click Local Object (see Figure 7.35).
Figure 7.35 New Project
Import the BOR Interface to Create the Entity Types
Next, you need to import the BOR interface. Follow these steps:
-
Right-click on Data Model, select Import, then click RFC/BOR Interface (see Figure 7.36).
Figure 7.36 Importing RFC/BOR
-
As shown in Figure 7.37, enter the Entity Type Name.
Figure 7.37 Selecting BOR
- From the Type dropdown, select Business Object Repository.
- Click on the Name field and press (F4) to get the list of BOR methods.
- From the list shown in Figure 7.38, expand EpmSalesOrder, then select GetList.
-
Click Continue
.
-
You should now see the selected BOR method in the Name field. Click Next (see Figure 7.39).
Figure 7.39 BOR Method
-
Select Soheaderdata(), then click Next (see Figure 7.40).
Figure 7.40 Sales Order Header
-
In the next screen, you’ll see all the entities of Soheaderdata; Sales Order ID (SO_ID) is automatically selected as a Key. Click Finish, and make sure there are no errors (see Figure 7.41).
Figure 7.41 Sales Order ID
The Entity Sets and the Service Implementation are automatically created, as shown in Figure 7.42.Figure 7.42 New Project
-
Save your project by clicking
.
Map to Data Source
In this step, you map the parameters of the data source with the properties of the entity set. Follow these steps:
-
Right-click GetEntitySet(Query), then select Map to Data Source (see Figure 7.43).
Figure 7.43 Map Entity to Data Source
- As shown in Figure 7.44, from the Type dropdown list, select Business Objects Repository.
- Enter “EmpSalesOrder.GetList” in the Name field.
-
Click Continue
.
-
Click on the Propose Mapping button to map the fields, and you’re automatically provided with the mapping proposal between entity sets and the data source. SAP Gateway Service Builder automatically performs certain checks to verify the mapping (see Figure 7.45).
Figure 7.45 Propose Mapping
-
Save your project by clicking
.
Generate Runtime Objects
The next step is to generate runtime objects:
-
Select the project, and then click Generate
. On the next screen, you’ll see the model and service definition details.
-
Click Continue
, or press (Enter) (see Figure 7.46).
- On the Create Object Directory Entry screen, click Local Object (see Figure 7.47).
Figure 7.47 Saving Local Object
You should now be able to see the generated MPC, DPC, and service registration details under the Runtime Artifacts folder (see Figure 7.48).
Figure 7.48 Runtime Artifacts
Register the OData Service
In this step, you’ll register the service to the SAP Gateway hub. If the SAP Gateway deployment is a hub deployment, then you must register the technical detail service with the SAP Gateway system. The following are the steps to register a service with the system. If your deployment is an embedded deployment (i.e., SAP Gateway components are deployed in the SAP Business Suite), then you can skip this step because the technical details will be registered automatically.
Follow these steps:
-
Right-click the SAP Gateway hub system under the Service Maintenance folder, and select Register (see Figure 7.49).
Figure 7.49 Registering Service
-
Press (F4) to select the system, select the SAP Gateway system, and click Continue
.
-
Select Local Object, then click Continue
(see Figure 7.50).
Figure 7.50 Selecting Package
Activate the OData Service
Now that you’ve registered the service, it’s time to activate it in the SAP Gateway server. Follow these steps:
- Run Transaction IWFND/MAINT_SERVICE.
- Click the Add Selected Services button.
- Enter “LOCAL” in the System Alias field.
- Enter “ZSALES*” as the External Service Name and press (Enter).
-
Select ZSALESORDER_HEADER_SRV, then click the Add Selected Services button (see Figure 7.51).
Figure 7.51 Adding Selected Service
-
Select Local Object, then click Continue
(see Figure 7.52).
-
In the pop-up that appears, click Continue
(see Figure 7.53).
Figure 7.53 Information Pop-up
Test the OData Service
Now that you’ve successfully modeled, generated, and registered the service, you need to test it. Follow these steps:
-
Go back to the Activate and Maintain Services screen by clicking
.
- Filter the service that you just activated by clicking the Filter button.
-
On the Filter for Service Catalog screen, enter “ZSALESORDER_HEADER_SRV” in the External Service Name field, then click Continue
(see Figure 7.54).
Figure 7.54 Service Name
- Select the service and click the Gateway Client button, as shown in Figure 7.55.
-
The SAP Gateway client opens with the Sales Order Header service. Click the Execute button (see Figure 7.56).
Figure 7.56 Testing Service
You should now receive the HTTP response with the status code 200 (see Figure 7.57).Figure 7.57 HTTP Response
- To view the sales order data, add SoheaderdataSet to the following Request URI: /sap/opu/odata/sap/ZSALESORDER_HEADER_SRV/ (see Figure 7.58). Click Execute.
In the HTTP Response area, you’ll see the sales order header information with the status code 200 (see Figure 7.59).
Figure 7.59 Sales Order Header Set
In this section, you learned how to create a data model by using import methods. In the next section, we’ll show you a different way to generate an OData model, by redefining an existing service.
7.3.2 Redefining OData Services
Using SAP Gateway Service Builder, you can redefine a service from an existing OData service that was created using SAP Gateway and from the external services developed using different back-end frameworks (SAP BW query service, GenIL Service). In this section, we’ll give an overview of each of these functions. Figure 7.60 shows the dropdown to access the Redefine option.
Figure 7.60 Redefine Functions
You use redefining techniques when you want to extend an existing data model by adding new entities, adding a new property to an existing entity, or adding an association or navigation property. Using these redefining methods, you can quickly and easily build a data model and extend it in SAP Gateway Service Builder. After the extended data model is generated, SAP Gateway Service Builder generates a new OData service with the new changes you added in the data model.
In the sections that follow, we’ll go through the options shown in Figure 7.60.
BOPF Service
The BOPF service allows you to consume business objects in the Business Object Processing Framework (BOPF) by using SAP Gateway BOPF Integration (GBI). This framework is based on object-oriented ABAP, which provides a set of generic services and functionalities to speed up and modularize development. To use this option, follow these steps:
- Right-click Data Model and choose Redefine • BOPF Service. This opens the BOPF Configuration wizard.
- Select the Business Object and the Default Query, then click Next (see Figure 7.61).
-
In the Service Registration area, enter descriptions in the Description fields for the Model Name and the Service Name. Click Next (see Figure 7.62).
Figure 7.62 Entering Descriptions
-
Select the fields and click Finish. You should now see the fields from the business objects added to your project (see Figure 7.63).
Figure 7.63 Redefining BOPF
OData Service (GW)
Using the OData Service (GW) function, you can extend an existing OData service created in an SAP Gateway OData service. You can not only extend the existing OData service but also overwrite an existing one by using this function in SAP Gateway Service Builder. In Chapter 9, Section 9.2, we’ll explain the step-by-step process of how to extend a transactional app by redefining it using this method.
To access this option, right-click Data Model and choose Redefine • OData Service (GW). This opens the OData Service wizard (see Figure 7.64).
Figure 7.64 OData Service
BW Query Service
Using the BW Query Service function, you can create SAP Fiori apps on an SAP BW query. The SAP BW Easy Queries feature provides an external interface for accessing analytic queries in SAP BW. At the end of this section, we’ll show you how to create a data model and how to generate an OData service using this function.
To use this option, right-click Data Model and choose Redefine • BW Query Service. This opens the Service Generator for BW Query Service wizard (Figure 7.65). The Query Name field is where you enter the name of the SAP BW query. We’ll discuss this method in greater detail in an exercise at the end of this section.
Figure 7.65 SAP BW Query Service
GenIL Service
Generic Interaction Layer (GenIL) is the connection between the Business Object Layer (BOL) and the back-end business engine. This layer handles the data transfer from the BOL to the APIs of the business engine. By using this function in SAP Gateway Service Builder, you can create a data model from this framework.
To access this option, right-click Data Model and choose Redefine • GenIL Service. Using the input help will list all the GenIL objects that are available (see Figure 7.66). A plug-in in SAP Gateway Service Builder provides a wizard that will guide you through the steps to create a service using this framework.
OData Service
This function lets you create a data model on an existing OData service. This function is different from the OData Service (GW) option, because the latter will let you redefine an OData service generated using SAP Gateway. In this function, you define the HTTP Destination and the Service Namespace.
To use this function, follow these steps:
- Right-click Data Model and choose Redefine • OData Service. This opens the External Service wizard.
-
Enter the HTTP Destination, Service Namespace, and Service Name. Click Next (see Figure 7.67).
Figure 7.67 Service Name
OData Service Example
We used an example OData service: http://services.odata.org/Northwind/Northwind.svc. You need to define this RFC destination using Transaction SM59.
- Enter descriptions in the Description fields for the model and service, then click Next (see Figure 7.68).
- Select the fields and click Finish on the last screen to create a data model in the project.
Figure 7.68 Service Registration Details
Next, we’ll round out this section with an example exercise.
Exercise
Let’s look at a use case in which a customer needs a custom SAP Fiori app on an SAP BEx query. To achieve this, you first need to understand how to generate an OData service based on an SAP BEx query, which we’ll explain next. We made a copy of the 0FIN_PA_T10_Q0001 query and saved it as Z0FIN_PA_T10_Q0001. We’ll be using this query in the remainder of this section.
The following sections walk through the individual steps.
Enable External Access to an SAP BEx Query
The first step is to enable the SAP BW query for external access:
- Open the SAP BEx query Z0FIN_PA_T10_Q0001 in the SAP BEx query designer.
- Go to the Extended tab in the Properties of the query.
- Check the By Easy Query box, then save the query (see Figure 7.69).
Figure 7.69 Allowing External Access
Generate OData Service
In this step, you’ll generate an OData service on top of an SAP BEx query:
- Log in to the front-end server, and run Transaction SEGW.
-
Create a new project by clicking
.
-
Enter the project technical name and description, then click Continue
(see Figure 7.70).
Figure 7.70 Create Project
-
Right-click the Data Model folder and choose Redefine • BW Query Service (see Figure 7.71).
Figure 7.71 Selecting SAP BW Query Service
- Select Controller for Easy Queries (SAP BW) from the Access Type dropdown.
-
Click
to open the list of queries (see Figure 7.72).
Figure 7.72 Query Name
-
You should now see the query for which you set the easy query property, as shown in Figure 7.73.
Figure 7.73 Selecting SAP BW Query
-
Click Continue
.
- You should now see the SAP BEx query in the Query Name field. Click the Next button.
-
Enter descriptions for the model name and service name, then click Next (see Figure 7.74).
Figure 7.74 Entering Description
-
Select all services by clicking
, then click Finish (see Figure 7.75).
Figure 7.75 Selecting All Objects
You should now see the data model with the Entity Types, Associations, Entity Sets, and so on (see Figure 7.76). -
Generate the runtime objects by selecting the project folder and clicking Generate
.
-
You’ll see the technical details of the MOD, DPC, and service. Click Continue
(see Figure 7.77).
- Select Local Object.
Add the Service to SAP Gateway
In this section, you’ll add the service to SAP Gateway. Follow these steps:
- Run Transaction IWFND/MAINT_SERVICE.
- Click Add Service.
- Search for the external service named “ZNETSALES_SRV”.
-
Select ZNETSALES_SRV, then click the Add Selected Services button (see Figure 7.78).
Figure 7.78 Adding Selected Service
-
Click Continue
(see Figure 7.79).
Figure 7.79 Adding Service
-
Click Continue
again to close the information pop-up.
Test the Service
In this section, you’ll finally test the service. Follow these steps:
- Click Back to return to the Activate and Maintain screen.
- Filter Service Catalog for “ZNETSALES_SRV”.
-
Select the service, then click Gateway Client (see Figure 7.80).
Figure 7.80 Testing Service
- Click Execute to test the service (see Figure 7.81).
Figure 7.81 SAP BW Query Elements
7.3.3 Include SAP Gateway OData Service
The methods we discussed in Section 7.3.1 and Section 7.3.2 are limited to the scope of the underlying data source. The third way to create an OData service is through mashups. The Include function is used when you want to combine your existing data model with a different data model; for example, you can mash up two or more services to form a completely new service without changing the existing service. You can access the Include Service Creation wizard by right-clicking Data Model and choosing Include • OData Service (GW) (see Figure 7.82).
Figure 7.82 Include Services
Enter the Technical Service Name and Version, then click Continue (see Figure 7.83).
Figure 7.83 Technical Service Name