天天看點

SAP CDS view自學教程之五:如何開發支援Odata navigation的CDS view

So far we have a working CDS view ready for us to create a UI5 application on top of it via Smart Template in WebIDE within just a couple of minutes. Once done, the UI5 application will display the data from our CDS view like below. For step by step how to achieve this, please refer to this blog: Step by Step to create CDS view through SmartTemplate + WebIDE.

SAP CDS view自學教程之五:如何開發支援Odata navigation的CDS view

How is navigation implemented among CDS views

In this part, let’s create CDS view which supports node navigation in OData service. The previous CDS view we created has a flat structure which only have a root node. Now let’s create a series of CDS views:

(1) A CDS view which contains two fields: spfli.connid and spfli.carrid. This view acts as the root node of the corresponding OData service model from semantic point of view. This view can support navigation from itself to the defined children node.

(2) A CDS view which acts as the navigation target from previously defined “root” view. Besides the two fields from sflight.connid and sflight.carrid which correspond to the root view, it has additional new field sflight.fldate.

OData navigation means suppose currently I am in the context of spfli.connid = 0001 and spfli.carrid ( data record with yellow ), and through navigation I can get all its dependent data in red color. We will see how this navigation would be performed later.

SAP CDS view自學教程之五:如何開發支援Odata navigation的CDS view

(3) A CDS view which exposes the two fields connid and carrid from root view and the associated data from child view.

This view is called “consumption” view and used to published as OData service.

Source code of view #1:

SAP CDS view自學教程之五:如何開發支援Odata navigation的CDS view
Source code of view #2:
SAP CDS view自學教程之五:如何開發支援Odata navigation的CDS view
Source code of view #3:
SAP CDS view自學教程之五:如何開發支援Odata navigation的CDS view
Activate all of these three CDS views. Since the third consumption view has annotation @OData.publish: true, once activated there will be an OData service automatically generated:
SAP CDS view自學教程之五:如何開發支援Odata navigation的CDS view

How to test navigation

First check the response from OData metadata request via url/sap/opu/odata/sap/ZFLIGHT_COM_CDS/$metadata in gateway client.

You should find two AssociationSets generated based on corresponding annotation in CDS views.

SAP CDS view自學教程之五:如何開發支援Odata navigation的CDS view
The entityset Zflight_Com has type Zflight_ComType, which has the navigation Property “to_Item”. Now we can test the navigation.
SAP CDS view自學教程之五:如何開發支援Odata navigation的CDS view
First we get the root node’s content via url:/sap/opu/odata/sap/ZFLIGHT_COM_CDS/Zflight_Com(connid=’0400′,carrid=’LH’) .
SAP CDS view自學教程之五:如何開發支援Odata navigation的CDS view
And in the response, we are told that the correct url for navigation from current node to its child node is just to append the navigation property defined in metadata, toItem, to the end of url, that is,/sap/opu/odata/sap/ZFLIGHT_COM_CDS/Zflight_Com(connid=’0400′,carrid=’LH’)/to_Item .
SAP CDS view自學教程之五:如何開發支援Odata navigation的CDS view

How the navigation is implemented in ABAP side

Set the breakpoint in the method below and re-trigger the navigation operation.

Check the generated SQL statement in variable statement in line 27.

SAP CDS view自學教程之五:如何開發支援Odata navigation的CDS view

SELECT "Zsflight_Child"."CARRID" AS "CARRID", "Zsflight_Child"."CONNID" AS "CONNID", "Zsflight_Child"."FLDATE" AS "FLDATE" FROM "ZSFLIGHTCHILD" AS "Zsflight_Child"
WHERE "Zsflight_Child"."CARRID" = ? AND "Zsflight_Child"."CONNID" = ? AND "Zsflight_Child"."MANDT" = '001' WITH PARAMETERS( 'LOCALE' = 'CASE_INSENSITIVE' )      

The value for two placeholders ( ? ) are stored in me->parameters->param_tab:

SAP CDS view自學教程之五:如何開發支援Odata navigation的CDS view

And check response in et_flag_data:

SAP CDS view自學教程之五:如何開發支援Odata navigation的CDS view