This Blog explains how you can leverage native SAP HANA development artifacts with CAP. In particular we look at the use of Calculation Views, inside Cloud Application Programming (CAP) Applications. This includes OData access to Calculation Views.

Solution:

Pre-requisites: Set Up SAP Business Application Studio for Development

  • Launch the Business Application Studio (BAS) and choose Create Dev Space

 NOTE: In the SAP BTP trial and free tier you are limited to only two Dev Spaces and only one can be active at a time. If you have performed other tutorials, you might already have reached your maximum. In that case you might have to delete one of the other dev spaces in order to continue with this tutorial.

  •  Select the Full stack application and select the necessary extensions as shown in the image below and click on the create dev space.

Selection of the type – Full Stack Application in Business Application Studio

 

It will then create a dev space. Although it takes couple of minutes to start. Once it is RUNNING, you can click on CAP_DEMO and start creating your projects.

Newly created DEV space in BAS

 

  • Add Cloud Foundry LOGIN Connection to your space ( 1.Plugin 2. F1- 3. CF )

Login to Cloud Foundry, there are several ways to login to cloud foundry

  1. In the plugins, go to cloud foundry icon and click on the right arrow as shown in the picture.

Cloud%20Foundry%20Login%20in%20BAS

Cloud Foundry Login in BAS

 

Fill in the credentials & necessary details such as Cloud Foundry Organization & Cloud Foundry Space.

Enter%20Credentials%20and%20Dev%20Space%20details

Enter Credentials and Dev Space details

2. Using the Artifact Wizard by clicking on F1.

Cloud%20Foundry%20login%20using%20Artifact%20Wizard%20%28F1%29

Cloud Foundry login using Artifact Wizard (F1)

 

3. Using the terminal: Execute the command,

 CF LOGIN

Terminal – CF LOGIN

  • Create CAP Project : Once you have logged in to Cloud Foundry,  Start with the creation of CAP Project by clicking on the Start from Template  ( Help > Get Started > Start from Template) and click on CAP Project.

Choose%20from%20the%20template

Choose from the template ( CAP Project )

Set your runtime as node.js and add the features that’s selected in the image below and click on finish.

 

It will then create a CAP Project with some sample folders and files.

Project%20Explorer

Project Explorer

  • Adding HANA Target to the Project : Open new terminal (Terminal > New Terminal) and execute the commandCDS ADD HANA

    Terminal%20%3A%20CDS%20ADD%20HANAYou can see the dependencies in the mta.yaml file.

mta.yaml%20file

mta.yaml file

  • Adjust the content in the files mta.yaml & package.json

Now change the path from gen/db -> db

Change the path in mta.yaml

Now, change the cds section of the package.json to the below block of code.

Changes in the file package.json

Before & After the addition of CDS Section in package.json

  • Install the dependencies

Open the terminal and execute

  • npm install ( NOTE: Skip the step if already installed )
  • npm install -g hana-cli (Open Source Utility called hana-cli )
  • hana-cli createModule

Install%20the%20dependencies

Install the dependencies

(OPTIONAL) You can clone your git repository or continue with the project with next steps.

Initialize%20the%20Git%20Repository

Initialize the Git Repository

  • View the files added from the CAP template
  1.  src > data-model.cds

data-model.cds

data-model.cds

       2. srv  > cat-service.cds

cat-service.cds

cat-service.cds

Run the following command

cds build

cds build

CDS artifacts are now converted to hdbview and hdbtable artifacts and you can find them in the src folder.

CDS%20Artifacts%20in%20the%20Explorer

CDS Artifacts in the Explorer

 

Deploy these objects into HANA database creating tables and views. Bind the project to a database connection and HDI container instance. Click on the bind icon.

The connections with respect to CAP & HANA tooling are distinct. They do not share the same connection. Hence, binding must be done at 2 different points : (a)  SAP HANA Project  (b) Run Configurations.

Binding%20the%20HANA%20Project

(a) Binding the HANA Project

  • Create Service Instance

Select an SAP HANA Service and choose from the list displayed.

SAP%20HANA%20Service

SAP HANA Service

Go to the plugin Run Configuration and bind the connection.

%28b%29%20Binding%20in%20Run%20Configuration

(b) Binding in Run Configuration

Select ‘Yes’ to the below dialog box.

  • Run the CAP Project

Once deployed, go to Run Configurations, and click on the run button. This will give an another dialog box to open ‘Bookshop-1’ a new tab.

Run%20the%20CAP%20Project

Run the CAP Project

Application running at port 4004

If you click on the $metadata, you can view the technical description of the service.

$metadata

Click on Fiori Preview, attributes have to be selected by clicking on the gear icon ⚙️.

Fiori%20Preview

Fiori Preview

  • Create Calculation View 

Create a calculation view, click on F1 which in turn will open a wizard to create the database artifact.

Database Artifact Wizard – Create Calculation View

In the Calculation View Editor, Click on the + icon on the projection and add the table. On the right side, there is an icon to expand the details of the projection. By clicking on it, it opens the panel and here you can map the table attributes, by double clicking on the MY_BOOKSHOP_BOOKS header.

Calculation View Editor

 

Once deployed, you can view the Calculation View in the Database explorer.

Database%20Explorer

Database Explorer

  • Edit the .cds files ( data-model.cds & cat-service.cds )

data-model.cds

cat-service.cds

  • Create Synonym

Create. hdbsynonym in your src folder

Database Artifact Wizard – Create Synonym

Click on <click_to_add> and enter the synonym name, MY_BOOKSHOP_CV_BOOK and click on the object field, the below table opens.  Enter ** on the text field and choose the calculation view cv_book and click finish.

 

If you open the cap.hdbsynonym in the text editor, it will be as follows.

Synonym%20-%20cap.synonym

Synonym – cap.synonym

Deploy the project by executing the command cds deploy -2 hana. You can refresh the browser. You can find the new cv_book entity.

Application with new entity

 

Conclusion: With some settings inside the development environment, it is possible to combine the use of HANA Native artifacts like Calculation Views with HDI generated code artifacts of CAP to build applications.

Following are some of the quick links:

SAP HANA Cloud Overview

About CAP

SAP HANA Deployment Infrastructure

Follow this tutorial : Combine CAP with SAP HANA Cloud to Create Full-Stack Applications

Please share your valuable feedback.

Sara Sampaio

Sara Sampaio

Author Since: March 10, 2022

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x