|
| 1 | +# Create a crititcality plugin for CAP Java |
| 2 | + |
| 3 | +## Prerequisites |
| 4 | + |
| 5 | +**In case you are using SAP Business Application Studio or SAP Build Code you can skip this section.** |
| 6 | + |
| 7 | +You need to have the following tools installed: |
| 8 | + |
| 9 | +* Java >=17 (internally, we use sapmachine but any other vendor should work, too) |
| 10 | +* Maven (some recent 3.x release) |
| 11 | +* Your Java IDE of choice. Ideally with CDS Tooling installed. This would be VS Code or IntelliJ Idea Ultimate. |
| 12 | + |
| 13 | +## Requirement |
| 14 | + |
| 15 | +Write a CAP Java plugin that provides a handler that can detect CDS enum values annotated with `@criticality.*` and |
| 16 | +ets the integer value according to the [criticality OData vocabulary](https://sap.github.io/odata-vocabularies/vocabularies/UI.html#CriticalityType) |
| 17 | +to an `criticality` element of the same entity. |
| 18 | + |
| 19 | +Bones points: this works for expanded entities, too. |
| 20 | + |
| 21 | +## Create a new plain Java project with Maven |
| 22 | + |
| 23 | +Use the Maven quickstart archetype to generate a plain, empty Java project: |
| 24 | + |
| 25 | +``` |
| 26 | +mvn archetype:generate -DgroupId= -DartifactId=criticality -DarchetypeGroupId=com.sap.capire -DarchetypeArtifactId=maven-archetype-quickstart -Dversion=1.0-SNAPSHOT -DinteractiveMode=false |
| 27 | +``` |
| 28 | + |
| 29 | + |
| 30 | +## Add needed dependencies |
| 31 | + |
| 32 | +Replace the generated pom.xml with the following content. It contains the needed dependencies and the correct Java version: |
| 33 | + |
| 34 | +``` |
| 35 | +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 36 | + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> |
| 37 | + <modelVersion>4.0.0</modelVersion> |
| 38 | + <groupId>com.sap.capire</groupId> |
| 39 | + <artifactId>criticality</artifactId> |
| 40 | + <packaging>jar</packaging> |
| 41 | + <version>1.0-SNAPSHOT</version> |
| 42 | + <name>criticality</name> |
| 43 | + <url>http://maven.apache.org</url> |
| 44 | + <properties> |
| 45 | + <maven.compiler.release>17</maven.compiler.release> |
| 46 | + <cds.services.version>2.10.0</cds.services.version> |
| 47 | + </properties> |
| 48 | + <dependencyManagement> |
| 49 | + <dependencies> |
| 50 | + <dependency> |
| 51 | + <groupId>com.sap.cds</groupId> |
| 52 | + <artifactId>cds-services-bom</artifactId> |
| 53 | + <version>${cds.services.version}</version> |
| 54 | + <type>pom</type> |
| 55 | + <scope>import</scope> |
| 56 | + </dependency> |
| 57 | + </dependencies> |
| 58 | + </dependencyManagement> |
| 59 | + <dependencies> |
| 60 | + <dependency> |
| 61 | + <groupId>org.springframework.boot</groupId> |
| 62 | + <artifactId>spring-boot-autoconfigure</artifactId> |
| 63 | + <version>3.2.6</version> |
| 64 | + </dependency> |
| 65 | + <dependency> |
| 66 | + <groupId>com.sap.cds</groupId> |
| 67 | + <artifactId>cds-services-api</artifactId> |
| 68 | + <version>2.10.0</version> |
| 69 | + </dependency> |
| 70 | + <dependency> |
| 71 | + <groupId>junit</groupId> |
| 72 | + <artifactId>junit</artifactId> |
| 73 | + <version>3.8.1</version> |
| 74 | + <scope>test</scope> |
| 75 | + </dependency> |
| 76 | + </dependencies> |
| 77 | +</project> |
| 78 | +``` |
| 79 | + |
| 80 | +Perform the initial build with `mvn compile`. |
| 81 | + |
| 82 | +## Spring Boot Auto Configuration for the handler class |
| 83 | + |
| 84 | +Use Spring Boot autoconfiguration to register the handler automatically as soon the dependency is added to the pom.xml. |
| 85 | + |
| 86 | +## Implement the handler |
| 87 | + |
| 88 | +Write the handler. Use the following resources as a reference: |
| 89 | + |
| 90 | +* https://cap.cloud.sap/docs/java/reflection-api You use the reflection API to introspect the application's CDS model in |
| 91 | + order to find relevant entities and elements. |
| 92 | +* https://cap.cloud.sap/docs/java/cds-data In the handler, you need to traverse the data in the result and act according |
| 93 | + to the information you discovered in the CDS model. |
| 94 | + |
| 95 | +## Install the handler to the local Maven repo |
| 96 | +In order to consume the new plugin from e.g. the Incidents App you need to install it to the local Maven repo. The `source:jar` |
| 97 | +goal adds the source code to the jar as well. You might need it for debugging. 😈 |
| 98 | + |
| 99 | +``` |
| 100 | +mvn source:jar install |
| 101 | +``` |
| 102 | + |
| 103 | +## Adjust the model of the target application |
| 104 | + |
| 105 | +Clone the Incidents App for Java: https://github.com/recap-conf/incidents-app-java |
| 106 | + |
| 107 | +Create a `criticality.cds file` in the `db` module and paste the following content: |
| 108 | + |
| 109 | +```cds |
| 110 | +using {sap.capire.incidents as my} from './schema'; |
| 111 | +
|
| 112 | +annotate my.Status with { |
| 113 | + code @criticality { |
| 114 | + new @criticality.Neutral; |
| 115 | + assigned @criticality.Critical; |
| 116 | + in_process @criticality.Critical; |
| 117 | + on_hold @criticality.Negative; |
| 118 | + resolved @criticality.Positive; |
| 119 | + closed @criticality.Positive; |
| 120 | + }; |
| 121 | +}; |
| 122 | +
|
| 123 | +extend my.Urgency { |
| 124 | + criticality : Integer; |
| 125 | +}; |
| 126 | +
|
| 127 | +annotate my.Urgency with { |
| 128 | + code @criticality { |
| 129 | + high @criticality.Negative; |
| 130 | + medium @criticality.Critical; |
| 131 | + }; |
| 132 | +}; |
| 133 | +``` |
| 134 | + |
| 135 | +Add the dependency of the just created plugin to your `srv/pom.xml`: |
| 136 | + |
| 137 | +```xml |
| 138 | +<dependency> |
| 139 | + <groupId>com.sap.capire</groupId> |
| 140 | + <artifactId>criticality</artifactId> |
| 141 | + <version>1.0-SNAPSHOT</version> |
| 142 | +</dependency> |
| 143 | +``` |
| 144 | + |
| 145 | +Start the application and execute some HTTP requests: |
| 146 | + |
| 147 | +```http |
| 148 | +GET http://localhost:8080/odata/v4/ProcessorService/Urgency |
| 149 | +Authorization: basic YWxpY2U6 |
| 150 | +
|
| 151 | +### |
| 152 | +
|
| 153 | +GET http://localhost:8080/odata/v4/ProcessorService/Incidents?$expand=urgency |
| 154 | +Authorization: basic YWxpY2U6 |
| 155 | +``` |
0 commit comments