Reuse a CAP Java Service
- How to reuse a CAP project through NPM packages
- How to load sample data using CSV files
- How to use the localized keyword
Now, that your products service is ready to be reused, you will build a bookstore application upon it.
In this tutorial you will create the model and the services of the bookstore application. After that you will initialize the database of your bookstore application with localized example data coming from CSV files.
You will then run your application – still without any custom coding required – and see the localization features of CAP in action.
- Step 1
From the products service that you created in the previous tutorial, we just want to reuse the domain and service models. For the bookstore, which you will develop in this tutorial, we need to create and initialize a bookstore project first.
Make sure you stopped your application from the previous tutorial.
-
From the main menu of SAP Business Application Studio, choose Terminal → New Terminal.
-
Before adding the
bookstoreproject, we need to make sure that you are in the projects folder. Both projects (products-serviceandbookstore) should be placed next to each other. Run the following command in the newly created terminal to go back to the projects folder:Shell/BashCopycd ~/projects -
Now that you are in the correct folder, run the following command:
Shell/BashCopycds init bookstore --add java -
To open the bookstore project in a new workspace go to File → Open Folder.
-
Choose bookstore from the project list and then OK.

If you see a notification asking if you want to synchronize the
Java classpath/configuration, choose Always.If you have any problem indication for any of the
pom.xmlfiles yet, don’t worry and ignore them for now.
-
- Step 2
As the
product-serviceshould be reused for the bookstore, you need to add a dependency between those two projects. Reusable models can be published as NPM modules and imported through dependencies in thepackage.jsonof a project.Make sure that you have followed all the sub-steps in the previous tutorial Set up for reuse before continuing.
First, we need to simulate a release of the
product-servicemodule, and consume this release in the bookstore application.-
From the main menu of SAP Business Application Studio, choose Terminal → New Terminal. Change into the bookstore directory by executing the following command in the terminal:
Shell/BashCopycd ~/projects/bookstoreThis step is optional if you are already in the right directory.
-
Install the reusable service project as npm dependency:
Shell/BashCopynpm install $(npm pack ../products-service -s)npm packcreates a tarball from theproducts-service, which is then directly used as a dependency in the bookstore application. Learn more aboutnpm pack.You will find a
sap-capire-products-1.0.0.tgzin the root folder of the bookstore project, which is the tarball file of theproducts-serviceproject. -
Install all other packages and simplify the overall dependency structure using
npm dedupe:Shell/BashCopynpm install && npm dedupeIf you open the
package.jsonof your bookstore project, you will see a dependency to@sap/capire-products.
What technology is used to reuse services between different applications in CAP?
-
- Step 3
Now that you have created your bookstore project, you need to define the domain model.
-
Within the
~/projects/bookstore/dbfolder, create a file calledschema.cds. -
Add the following code to your newly created
schema.cdsfile and make sure you Save the file:CDSCopynamespace sap.capire.bookstore; using { Currency, cuid, managed } from '@sap/cds/common'; using { sap.capire.products.Products } from '@sap/capire-products'; entity Books as projection on Products; extend Products with { // Note: we map Books to Products to allow reusing AdminService as is author : Association to Authors; } entity Authors : cuid { firstname : String(111); lastname : String(111); books : Association to many Books on books.author = $self; } @Capabilities.Updatable: false entity Orders : cuid, managed { items : Composition of many OrderItems on items.parent = $self; total : Decimal(9,2) @readonly; currency : Currency; } @Capabilities.Updatable: false entity OrderItems : cuid { parent : Association to Orders not null; book_ID : UUID; amount : Integer; netAmount : Decimal(9,2) @readonly; }
The domain model defines four entities:
-
Books -
Authors -
Orders -
OrderItems
Again the
Currency,cuidandmanagedtypes and aspects are imported, as described in the previous tutorial.In addition, it imports the
Productsentity, which is reused for theBooksentity. To establish the relation between books and authors, theProductsentity is extended with an additional association toAuthors.The
totalelement of theOrdersentity and thenetAmountelement of theOrderItemsentity are annotated with@readonly. This means the value of these elements cannot be set by a client. The value is calculated by custom code. You will implement this custom code in a later tutorial. Both of these entities are also annotated with@Capabilities.Updatable: false, which means that they cannot be updated, only created and deleted. -
- Step 4
You will now define the services, that should expose the entities you have defined in your domain model:
-
Within the
~/projects/bookstore/srvfolder, create a file calledservices.cds. -
Add the following code to the
services.cdsfile and make sure you Save the file:CDSCopyusing { sap.capire.bookstore as db } from '../db/schema'; // Define Books Service service BooksService { @readonly entity Books as projection on db.Books { *, category as genre } excluding { category, createdBy, createdAt, modifiedBy, modifiedAt }; @readonly entity Authors as projection on db.Authors; } // Define Orders Service service OrdersService { entity Orders as projection on db.Orders; entity OrderItems as projection on db.OrderItems; } // Reuse Admin Service using { AdminService } from '@sap/capire-products'; extend service AdminService with { entity Authors as projection on db.Authors; }
The
services.cdsfile defines three services:-
BooksService -
OrdersService -
AdminService
The
BooksServiceis used to provide a read-only view on theBooksandAuthorsdata. Modifications of these entities isn’t possible via this service.The
OrdersServiceallows to view, create, and delete orders.The
AdminServiceis reused from the products service. But we’ve added theAuthorsentity to it. It can be used to create, update, and delete products and authors.It’s considered best practice to define services with a single use-case in mind. For example, the
AdminServiceis meant for administrating Products, Authors and Categories, while theBooksServiceis meant for exposing a catalog of books and authors, hiding administrative data such as creation and modification times from the end user. -
- Step 5
After defining the domain model and the services that you want to expose, you could already start your application. But first, we’d like to add some sample data to your database. To do so, we’ll need to use some CSV files:
-
From your
bookstoreproject, right-click thedbfolder and choose New Folder. Call the folderdata. -
Go to the data folder by running the following command in the terminal:
Shell/BashCopycd ~/projects/bookstore/db/data -
Download CSV data for the Authors entity by running the following command in the terminal:
Shell/BashCopycurl https://raw.githubusercontent.com/SAP-samples/cloud-cap-samples/CAA160-final/bookstore/db/data/sap.capire.bookstore-Authors.csv -O -
Download CSV data for the Books entity by running the following command in the terminal:
Shell/BashCopycurl https://raw.githubusercontent.com/SAP-samples/cloud-cap-samples/CAA160-final/bookstore/db/data/sap.capire.bookstore-Books.csv -O -
Rename it with the following command:
Shell/BashCopymv sap.capire.bookstore-Books.csv sap.capire.products-Products.csv -
Download translated CSV data for the Books entity by running the following command in the terminal:
Shell/BashCopycurl https://raw.githubusercontent.com/SAP-samples/cloud-cap-samples/CAA160-final/bookstore/db/data/sap.capire.bookstore-Books_texts.csv -O -
Rename it with the following command:
Shell/BashCopymv sap.capire.bookstore-Books_texts.csv sap.capire.products-Products_texts.csv -
Download CSV data for the Categories entity by running the following command in the terminal:
Shell/BashCopycurl https://raw.githubusercontent.com/SAP-samples/cloud-cap-samples/CAA160-final/bookstore/db/data/sap.capire.products-Categories.csv -OYou should now have 4 CSV files with sample data. The files deliver initial data for the service that we reuse and for one entity that we created in the
bookstoreservice:sap.capire.products-Categories.csvsap.capire.products-Products.csvsap.capire.products-Products_texts.csvsap.capire.bookstore-Authors.csv
The name of the CSV has to match the pattern [namespace]-[entity name] exactly, otherwise the application will fail to start.
-
- Step 6
-
In the terminal, go to the root of the bookstore project:
Shell/BashCopycd ~/projects/bookstore -
Ensure that you have stopped all previously running applications (including the
products-serviceapplication) by usingCTRL+C. -
Start the application by running:
Shell/BashCopymvn spring-boot:run -
You will see a pop-up in the SAP Business Application Studio. Choose Open in a New Tab.
-
When you open the URL in a new tab, you will see a welcome page. To see the books data click on Books directly from the welcome page.
Instead of clicking on the Books entry on the welcome page, you could simple add
/odata/v4/BooksService/Booksto the URL.
-
To read the localized German example data, append the query parameter
?sap-locale=deto the URL. For example,<APP_URL>/odata/v4/BooksService/Books?sap-locale=de. Try to switch the language between German (de) and English (en).
Great Job! You have successfully developed the bookstore application and reused your products service from the previous tutorial.
In the next tutorial you will extend this application with custom code, to calculate the
totalandnetAmountelements of theOrdersandOrderItemsentity. In addition, you will add custom code that will decrease the stock in the Books entity whenever an order is created. -