1+ package com .sap .cap .incident_management .handler ;
2+
3+ import java .util .List ;
4+ import java .util .Locale ;
5+ import java .util .logging .Level ;
6+ import com .sap .cds .services .*;
7+ import org .slf4j .Logger ;
8+ import org .slf4j .LoggerFactory ;
9+ import org .springframework .stereotype .Component ;
10+
11+ import com .sap .cds .services .cds .CqnService ;
12+ import com .sap .cds .services .handler .EventHandler ;
13+ import com .sap .cds .services .handler .annotations .*;
14+ import com .sap .cds .services .handler .annotations .ServiceName ;
15+
16+ import cds .gen .processorservice .Incidents ;
17+ import cds .gen .processorservice .Urgency ;
18+ import cds .gen .processorservice .ProcessorService_ ;
19+ import cds .gen .processorservice .Incidents_ ;
20+
21+ @ Component
22+ @ ServiceName (ProcessorService_ .CDS_NAME )
23+ public class ProcessorServiceHandler implements EventHandler {
24+
25+ /*
26+ * Change the urgency of an incident to "high" if the title contains the word "urgent"
27+ */
28+ private static final Logger logger = LoggerFactory .getLogger (ProcessorServiceHandler .class );
29+ @ Before (event = CqnService .EVENT_CREATE )
30+ public void ensureHighUrgencyForIncidentsWithUrgentInTitle (List <Incidents > incidents ) {
31+ for (Incidents incident : incidents ) {
32+ System .out .println (incident .getTitle ());
33+ if (incident .getTitle ().toLowerCase (Locale .ENGLISH ).contains ("urgent" ) &&
34+ incident .getUrgencyCode () == null || !incident .getUrgencyCode ().equals ("H" )) {
35+ incident .setUrgencyCode ("H" );
36+ logger .info ("Adjusted Urgency for incident '{}' to 'HIGH'." , incident .getTitle ());
37+ }
38+
39+ }
40+ }
41+ /*
42+ * Handler to avoid updating a "closed" incident
43+ */
44+ @ Before (event = CqnService .EVENT_UPDATE )
45+ public void onUpdate (Incidents incident ) {
46+
47+ if (incident .getStatusCode ().equals ("C" )){
48+ throw new ServiceException (ErrorStatuses .CONFLICT , "Can't modify a closed incident" );
49+ }
50+
51+ }
52+
53+
54+
55+
56+
57+ }
0 commit comments