application features of CAP Java itself is based on the same extension and plugin-mechanisms as one would use to build an external plugin:
cds init books --add java,samplescd books
mvn spring-boot:run
{
"@context":"$metadata#Books",
"@metadataEtag":"W/\"7966b04ab5aa9c3bf779789e1d6bbd08b9a7645ba87661c80464af5e80d8de04\"",
"value":[
{"ID":1,"title":"Wuthering Heights","stock":100},
{"ID":2,"title":"Jane Eyre (discounted)","stock":500}
]
}<dependency> in the Maven build descriptor. So, create a new directory named cds-emoji-plugin-java and cd to that directory.pom.xml in the new directory:pom.xml<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sap.example.cds.plugins</groupId>
<artifactId>cds-emoji-plugin-java</artifactId>
<packaging>jar</packaging>
<version>0.1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<cds.services.version>1.34.0</cds.services.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.sap.cds</groupId>
<artifactId>cds-services-bom</artifactId>
<version>${cds.services.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>com.sap.cds</groupId>
<artifactId>cds-services-api</artifactId>
</dependency>
</dependencies>
</project>mkdir -p src/main/java/com/sap/example/cds
mkdir -p src/main/resources/META-INF/servicessrc/main/java/com/sap/example/cds directory. The first is the actual implementation of the plugin. Basically it looks pretty much the same as a custom handler in your CAP Java application would look like. To append the emoji, we use CAP Java's powerful data processor:src/main/java/com/sap/example/cds/EmojiHandler.java:package com.sap.example.cds;
import com.sap.cds.CdsDataProcessor;
import com.sap.cds.services.cds.ApplicationService;
import com.sap.cds.services.cds.CdsReadEventContext;
import com.sap.cds.services.handler.EventHandler;
import com.sap.cds.services.handler.annotations.After;
import com.sap.cds.services.handler.annotations.ServiceName;
@ServiceName(value = "*", type = ApplicationService.class)
public class EmojiHandler implements EventHandler {
private static final String EMOJI_ANNOTATION_NAME = "@emoji";
@After
public void decorateEmoji(CdsReadEventContext context) {
CdsDataProcessor.create()
.addConverter(
(path, element, type) -> element.findAnnotation(EMOJI_ANNOTATION_NAME).isPresent(),
(path, element, value) -> value + "🙃")
.process(context.getResult());
}
}@After handler that will listen for READ events for all entities on all ApplicationServices. In case a read entity contains an element annotated with @emoji it will be appended with the 🙃 emoji. That's all.CdsRuntimeConfiguration interface. This implementation will be called by the CAP Java runtime and contains the logic needed to create the EmojiHandler class above. Luckily, it's pretty easy to be created:src/main/java/com/sap/example/cds/EmojiHandlerRuntimeConfiguration.java:package com.sap.example.cds;
import com.sap.cds.services.runtime.CdsRuntimeConfiguration;
import com.sap.cds.services.runtime.CdsRuntimeConfigurer;
public class EmojiHandlerRuntimeConfiguration implements CdsRuntimeConfiguration {
@Override
public void eventHandlers(CdsRuntimeConfigurer configurer) {
configurer.eventHandler(new EmojiHandler());
}
}src/main/resources/META-INF/services/com.sap.cds.services.runtime.CdsRuntimeConfiguration:com.sap.example.cds.EmojiHandlerRuntimeConfigurationmvn installsrv/pom.xml) of the application and add the following snippet to the <dependencies> section:<dependency>
<groupId>com.sap.example.cds.plugins</groupId>
<artifactId>cds-emoji-plugin-java</artifactId>
<version>0.1.0-SNAPSHOT</version>
<scope>runtime</scope>
</dependency>title of the entity Books with @emoji:namespace my.bookshop;
entity Books {
key ID : Integer;
@emoji
title : String;
author : String;
stock : Integer;
}\uD83D\uDE43).CdsRuntimeConfiguration and Java's Service Loader mechanism.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
| User | Count |
|---|---|
| 49 | |
| 49 | |
| 29 | |
| 23 | |
| 21 | |
| 15 | |
| 15 | |
| 14 | |
| 13 | |
| 13 |