SAP Home Learn Build Integrate Model Operate Extend with AI ConnectTutorial navigator Knowledge Graph API Devtoberfest Developer Advocates App Space

Manage my Account SAP Devs YouTube ↗ Learnings ↗ Community ↗ Provide Feedback ↗
Logout
⤢ Open full site

Extend the Bookstore with Custom Code

Extend the previously built bookstore with custom coding, for example, to validate requests.

Overview

🎓 beginner 20 min. SAP Cloud Application Programming ModelBeginnerSAP Business Technology PlatformJavaSAP Business Application Studio

You will learn

  • How to use the CAP Java SDK
  • How to use the debugger in SAP Business Application Studio
René Jeglinsky R René Jeglinsky December 10, 2024
Created by December 10, 2024
Contributors

Prerequisites

Steps

Intro

In the previous tutorial, you have built the data model and exposed the services of your bookstore application. In this tutorial, you will extend the bookstore with custom code to calculate the total and netAmount elements of the Orders and OrderItems entity. In addition, when creating an order the available stock of a book will be checked and decreased.

Step 1 Define custom handler for `OrdersService`

In one of the previous tutorials, you have already seen how to register a custom event handler to handle READ or CREATE events of an entity. You used the @On annotation, which replaces the default handling of an event that is provided by the CAP Java runtime.

As we want to augment the default handling now, we’ll use the @Before and @After annotations. Event handlers registered using the @Before annotation are meant to perform validation of the input entity data. This makes it possible to validate the available stock of a particular book before creating an order. In contrast, event handlers registered using the @After annotation can post-process returned entities. This is useful for calculating the total and netAmount elements after reading orders or their items from the database.

First of all, a new Java class for your event handler methods needs to be defined:

  1. From the terminal, stop your application if it’s still running using CTRL+C.

  2. Go to srv/src/main/java/customer/bookstore and create a new folder called handlers.

    handlers package
    handlers package

  3. In the created package, create the OrdersService.java file with the following content and make sure you Save the file:

Java
package customer.bookstore.handlers;

import cds.gen.ordersservice.OrdersService_;
import com.sap.cds.services.handler.EventHandler;
import com.sap.cds.services.handler.annotations.ServiceName;

import org.springframework.stereotype.Component;

@Component
@ServiceName(OrdersService_.CDS_NAME)
public class OrdersService implements EventHandler {
//Replace this comment with the code of Step 2 of this tutorial
}

OrdersService class overview
OrdersService class overview

If you see validation errors in your editor, open the context menu on your pom.xml in the srv directory and select Reload Project. That regenerates the classes and makes them available.

Step 2 Decrease stock when posting order
+
Step 3 Test handler
+
Step 4 Calculate `netAmount` of order item
+
Step 5 Calculate total amount of order
+
Step 6 Test the handler
+

Resources

Discussion

Share feedback on this tutorial or join the conversation in SAP Community.

Submit detailed feedback Discuss in Community
Steps
Step 1 of 6
1. Define custom handler for `OrdersService` 2. Decrease stock when posting order 3. Test handler 4. Calculate `netAmount` of order item 5. Calculate total amount of order 6. Test the handler

Learn more →