In this blog post, we will demonstrate how to extend SAP ODM model using the SAP CDS Graphical Modeler and SAP Business Application Studio, and also show you how to generate the OData services based on the extended model through CDS projection.
Introduction to SAP CDS Graphical Modeler
SAP CDS Graphical Modeler is a graphical tool to allow the CAP CDS developer to visually design the CDS model, and it is a predefined extension for the “SAP Cloud Business Application” dev space for SAP Business Application Studio. So in order to use the CDS Graphical Modeler, you just need to create a dev space of “SAP Cloud Business Application” in the SAP Business Application Studio.
As you see, when you create the dev space, “CDS Graphical Modeler” has already been included.
Prepare Your Dev Space
Once your dev space is created, you will need to wait for your dev space to be up and running.
And after that, enter the dev space and you will see the dev space home page from the SAP Business Application Studio.
Now let’s create a sample CAP project using the project wizard. Click “Start from template” from the welcome page and launch the project wizard.
Please select “CAP Project” from the template in the project wizard
Click “Start” button and enter the CAP project details in the next page of the project wizard. Please check “Basic Sample Files” and ensure the project wizard to generate sample CDS files for you.
Click “Finish” button to close the wizard and the dev space will refresh again and this time the newly CAP project folder will be in the project tree.
Then we need to include SAP ODM model by adding ODM dependencies to the package.json file in the project.
Save the file and let’s build this project. Open a terminal through “Terminal” -> “New Terminal” menu of the application studio, and type “npm install” in the command line console.
After the build is successful, open data-model.cds from the db folder using the CDS Graphical Modeler:
Create an Extension for ODM WorkforcePerson Entity
In order to create the extension of ODM model, we need to first import the ODM cds file to our data-model.cds in the project. You can do so by dragging “node_modules/@sap/odm/dist/workforce/WorkforcePerson.cds” from the file explorer in the left pane into the CDS Graphical Modeler:
Then confirm the import dialog and finish importing the WorkforcePerson.cds to the data-model.cds. You can leave the “Alias” field empty for now.
Then we can create an extension for ODM WorkforcePerson entity by clicking the third button in the toolbar for the CDS Graphical Modeler.
And after that in the “create extension” dialog select “sap.odm.workforce.WorkforcePerson” in the Type dropdown list.
and in the “New Property” dialog, add 3 extended properties to this extension.
Close the dialog and you will see the extension now contains the extended properties you just created.
Now you can check if the extension has taken effective on the original WorkforcePerson entity in the ODM namespace. You can navigate to the “sap.odm.workforce” namespace in the namespace dropdown list on top of the property sheet.
Then you can see the 3 newly added properties have been included in the ODM WorkforcePerson entity.
We can now create a projection for the ODM WorkforcePerso entity in the cat-service.cds in the srv folder. Open the srv/cat-service.cds file. You can see that the Book projection already exists in the CatalogService.
Now let’s create a new service called “AdminService” where we will create the projection. You can do so by clicking “<New Service>” menu item in the namespace dropdown:
And then enter “AdminService” in the “New Service” dialog as the name of the service and finish creating the service.
After the service has been created, please also drag “node_modules/@sap/odm/dist/workforce/WorkforcePerson.cds” into the AdminService in the CDS Graphical Modeler, similar to what you have done to import the WorkforcePerson.cds to the db/data-model.cds.
Then we can create a projection by clicking the second button in the toolbar.
In the “New Projection” dialog, select “sap.odm.workforce.WorkforcePerson” as the projection base type. Please ensure to exclude “personalDetail” and “profileDetail” while creating the projection to avoid an odata service generation error for this version of ODM.
Click “Create” and finish creating the projection. Now you should be able to see the newly create projection in AdminService.
Please also note that the 3 newly added properties have shown up in the projection. This is expected because cat-service.cds has already included db/data-model.cds where we have already extended the ODM WorkforcePerson entity through a CDS extension.
Run a Local OData Service
Now let’s generate a local odata service and check its odata metadata to ensure everything is working properly. Open a terminal again and type “cds watch” and press enter.
After the service is generated you will see below popup dialog to indicate the exposed port information in the SAP Business Application Studio.
Click “Expose and Open” button to dismiss this dialog. Now you should be able to see another browser tab is opened that allows you to check the odata metadata.
Click “/admin/$metadata” URL and check the OData metadata. Now locate the “WorkforcePerson” entity type in the OData metadata document, you should be able to see the 3 newly added properties in the “WorkforcePerson” entity type:
With this odata services, we have extended the original ODM WorkforcePerson entity successfully by adding 3 additional new properties, and the user can build the frontend application to consume this extended odata model now.
As a summary, in this blog post we first show you how to import the ODM model into the CAP project and how to import the ODM WorkforcePerson entity into the data model for this project. And then we demonstrate how to create an extension for the ODM WorkforcePerson, and then create a projection on this extended ODM model in the newly created CDS service. Finally, we have demonstrated how to run a local OData service based on the CAP project through “cds watch” and show you the extended OData model in the OData metadata document.