Skip to content
This repository has been archived by the owner on Oct 9, 2021. It is now read-only.

Commit

Permalink
Add some links to the cap.cloud.sap docu (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
agoerler authored and beckermarc committed Oct 2, 2019
1 parent 9173a7a commit b06df56
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
8 changes: 4 additions & 4 deletions exercises-java/exercise1/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ First things first, you need to set up your development environment and check th

For this exercise, we'll use the new SAP Application Studio as the development tool of choice. SAP Application Studio provides a web-based Visual Studio Code-like experience. So it's like VS Code, but for your browser. What's great about using SAP Application Studio? You get an editor, useful extensions and all the tools required to develop CAP applications and full access to the terminal.

To make sure everything is set up correctly, this exercise also includes how to build & run a simple Hello World application. CAP supports both Java and Node.js development. But for this exercise, we'll be using Java. The CAP Java stack is able to tightly integrate with [Spring Boot](https://spring.io/projects/spring-boot), which provides a lot of features out of the box. This means, Spring Boot will be your runtime container.
To make sure everything is set up correctly, this exercise also includes how to build & run a simple Hello World application. CAP supports both Java and Node.js development. But for this exercise, we'll be using Java. The [CAP Java stack](https://cap.cloud.sap/docs/java/) is able to tightly integrate with [Spring Boot](https://spring.io/projects/spring-boot), which provides a lot of features out of the box. This means, Spring Boot will be your runtime container.

## Open SAP Application Studio and create your Dev Space

Expand Down Expand Up @@ -70,7 +70,7 @@ CAP applications use [Core Data Services](https://cap.cloud.sap/docs/cds/) (CDS)
- data structures by using [entity definitions](https://cap.cloud.sap/docs/cds/cdl#entities-views)
- how data structures are consumed by using [service definitions](https://cap.cloud.sap/docs/cds/cdl#services)

In this step, you'll define a simple service, which also defines it's own entity. In more complex applications, services usually expose projections on entities defined in the data model. You will see this later in [Exercise 2](../exercise2/README.md).
In this step, you'll define a simple service, which also defines its own entity. In more complex applications, services usually expose projections on entities defined in the data model. You will see this later in [Exercise 2](../exercise2/README.md).

1. Right-click on the `srv` folder, choose **New File**.

Expand Down Expand Up @@ -197,8 +197,8 @@ Later on in the session, you'll learn that CAP Java runtime can handle all CRUD

The event handler uses the following APIs, which are available for Service Providers in CAP Java:

* Event handler classes have to implement the marker interface `EventHandler` and register themselves as Spring Beans (`@Component`). The marker interface is important, because it enables the CAP Java runtime to identify these classes among all Spring Beans.
* Event handler methods are registered with `@On`, `@Before` or `@After` annotations. Every event, such as an entity creation, runs through these three phases. Each phase has a slightly different semantic. You will learn more about these semantics in [Exercise 4](../exercise4/README.md).
* [Event handler classes](https://cap.cloud.sap/docs/java/srv-impl#event-handler-classes) have to implement the marker interface `EventHandler` and register themselves as Spring Beans (`@Component`). The marker interface is important, because it enables the CAP Java runtime to identify these classes among all Spring Beans.
* [Event handler methods](https://cap.cloud.sap/docs/java/srv-impl#event-handler-methods) are registered with `@On`, `@Before` or `@After` annotations. Every event, such as an entity creation, runs through these three [phases](https://cap.cloud.sap/docs/java/srv-impl#event-phases). Each phase has a slightly different semantic. You will learn more about these semantics in [Exercise 4](../exercise4/README.md).
* The annotation `@ServiceName` specifies the default service name all event handler methods apply to. Here this is `AdminService`, as this was also the name when defining the service in the CDS model.
* Event handler methods get an event-specific event context parameter, which provides access to the input parameters of the event and the ability to set the result. For example, let's look at the `CdsCreateEventContext context` parameter. The event we are extending is the `CREATE` event. The type of the context variable is specific to this extended `CREATE` event. The `onCreate` method returns `void`, as the result is set by running: `context.setResult(…)`.

Expand Down
4 changes: 2 additions & 2 deletions exercises-java/exercise2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Let's explain these imports and keywords in more detail:

### The `localized` keyword

The `localized` keyword can be used to mark elements, which require translation. The ability to store translations for different languages and to store a default fallback translation is automatically handled by the CDS model for you. You will see this in action in more detail in [Exercise 3](../exercise3/README.md).
The `localized` keyword can be used to mark elements, which require translation. The ability to store translations for different languages and to store a default fallback translation is automatically handled by CDS for you. You will see this in action in more detail in [Exercise 3](../exercise3/README.md).

### Associations and Compositions

Expand Down Expand Up @@ -135,7 +135,7 @@ Let's deploy the domain model to a database. Now, you will use SQLite, a light-w

## Use CAP's generic persistence handling

The CAP Java stack provides out-of-the-box capabilities to store and retrieve entities from a database. Therefore no custom coding is required for this. The entities defined in your `AdminService` will be automatically served via OData and you can just delete the `AdminService.java` file that was created earlier.
The CAP Java stack has a [Persistence Service](https://cap.cloud.sap/docs/java/srv-run#the-persistence-service) that provides out-of-the-box capabilities to store and retrieve entities from a database. Therefore no custom coding is required for this. The entities defined in your `AdminService` will be automatically served via OData and you can just delete the `AdminService.java` file that was created earlier.

1. Delete the `AdminService.java` file in the `handlers` folder, which you have created in [Exercise 1](../exercise1/README.md).

Expand Down
12 changes: 6 additions & 6 deletions exercises-java/exercise4/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ Some artifacts that are generated during the exercise are not included in the ch

## Defining a custom handler for the `OrdersService`

In [Exercise 1](../exercise1/README.md) you have already seen how to define a custom event handler to handle `READ` or `CREATE` events of an entity. We used the `@On` annotation, which replaces the default handling of an event that is provided by the CAP Java runtime.
In [Exercise 1](../exercise1/README.md) you have already seen how to register a [custom event handler](https://cap.cloud.sap/docs/java/srv-impl#registering-event-handlers) to handle `READ` or `CREATE` events of an entity. We 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 will 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.
As we want to augment the default handling now, we will use the `@Before` and `@After` annotations. Event handlers registered using the [`@Before`](https://cap.cloud.sap/docs/java/srv-impl#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`](https://cap.cloud.sap/docs/java/srv-impl#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:

Expand Down Expand Up @@ -114,10 +114,10 @@ You will now add a method to the `OrdersService` Java class to decrease the stoc

Let's break down what is happening:
- The method `validateBookAndDecreaseStock` is registered using the `@Before` annotation. This means the method is called before the `OrderItems` entities are persisted. The annotation also specifies, that the method should be called whenever an entity `OrderItems` is created.
- The method has a parameter `items` which gives access to the list of `OrderItems`. The interface used here is generated by CAP Java. It generates a POJO interface for each entity defined in the CDS model.
- The method has a parameter `items` which gives access to the list of `OrderItems`. The interface used here is generated by CAP Java. It generates a [POJO interface](https://cap.cloud.sap/docs/java/result-handling) for each entity defined in the CDS model.
- The `CqnSelect sel` variable defines a database query to retrieve the book that is referenced by the order item. The query is performed and the returned entity data is accessed using the POJO interface for `Books`.
- After that the available stock of the book is compared against the ordered amount. If enough stock is available the stock is decreased on the book and the book is updated within the database.
- As order items can also be created via a deep-insert on the `Orders` entity, the same validation is triggered by the `validateBookAndDecreaseStockViaOrders` method.
- As order items can also be created via a [deep insert](https://cap.cloud.sap/docs/java/srv-run#deep-insert--upsert) on the `Orders` entity, the same validation is triggered by the `validateBookAndDecreaseStockViaOrders` method.

It is important to note, that the CAP Java stack automatically takes care of combining all database queries and updates in a single transaction. This means, that if the creation of the order item fails for some reason, the stock of the book will not be decreased.

Expand Down Expand Up @@ -198,12 +198,12 @@ Next, let's add a method to the `OrdersService` Java class to calculate the `net
Let's break it down again:
- The method `calculateNetAmount` is registered using the `@After` annotation. This means the method is called after the `OrderItems` entities have been read from the database. The annotation also specified, that the method should be called whenever an entity `OrderItems` is read or created.
- The method has a parameter `items` that gives access to all `OrderItems` entities that were either read or created.
- The `CqnSelect sel` variable defines a database query, to retrieve the book that is referenced by the order item. The query is performed and the returned entity data is accessed using the POJO interface for `Books`.
- The `CqnSelect sel` variable defines a database [query](https://cap.cloud.sap/docs/java/cds-ql), to retrieve the book that is referenced by the order item. The query is [executed](https://cap.cloud.sap/docs/java/srv-run#query-execution) and the [query result](https://cap.cloud.sap/docs/java/result-handling) is accessed using the POJO interface for `Books`.
- In the last line the net amount of the order item is calculated, based on the price of the book and the amount of books ordered.

### Testing the handler

1. In SAP Application Studio stop your application if it is still running by clicking on the stop icon in the **Debug** side panel.
1. In SAP Application Studio stop your application if it is still running by clicking on the red stop icon in the **Debug** side panel.

2. Choose the **Run Configuration** icon on the side panel of SAP Application Studio.

Expand Down

0 comments on commit b06df56

Please sign in to comment.