@@ -120,43 +120,48 @@ In this tutorial, you add some custom code to the CAP application. Depending on
120120 import com.sap.cds.services.persistence.PersistenceService;
121121 import org.slf4j.Logger;
122122 import org.slf4j.LoggerFactory;
123- import org.springframework.beans.factory.annotation.Autowired;
124123 import org.springframework.stereotype.Component;
125124 import java.util.List;
126125 import java.util.Locale;
127126
128127 @Component
129- @ServiceName (ProcessorService_ .CDS_NAME )
128+ @ServiceName (ProcessorService_ .CDS_NAME )
130129 public class ProcessorServiceHandler implements EventHandler {
131130
132- @Autowired
133- private PersistenceService db;
134- /*
135- * Change the urgency of an incident to "high" if the title contains the word "urgent"
136- */
137- private static final Logger logger = LoggerFactory .getLogger (ProcessorServiceHandler .class );
138- @Before (event = CqnService .EVENT_CREATE )
139- public void ensureHighUrgencyForIncidentsWithUrgentInTitle (List < Incidents > incidents ) {
140- for (Incidents incident : incidents) {
141- if (incident .getTitle ().toLowerCase (Locale .ENGLISH ).contains (" urgent" ) &&
142- incident .getUrgencyCode () == null || ! incident .getUrgencyCode ().equals (" H" )) {
143- incident .setUrgencyCode (" H" );
144- logger .info (" Adjusted Urgency for incident '{}' to 'HIGH'." , incident .getTitle ());
145- }
146-
147- }
148- }
149- /*
150- * Handler to avoid updating a "closed" incident
151- */
152- @Before (event = CqnService .EVENT_UPDATE )
153- public void onUpdate (Incidents incident ) {
154- Incidents in = db .run (Select .from ((Class< Incidents_> ) Incidents_ .class ).where (i - > i .ID ().eq (incident .getId ()))).single (Incidents .class );
155- if (in .getStatusCode ().equals (" C" )){
156- throw new ServiceException (ErrorStatuses .CONFLICT , " Can't modify a closed incident" );
131+ private static final Logger logger = LoggerFactory .getLogger (ProcessorServiceHandler .class );
132+
133+ private final PersistenceService db;
134+
135+ public ProcessorServiceHandler (PersistenceService db ) {
136+ this .db = db;
137+ }
138+
139+ /*
140+ * Change the urgency of an incident to "high" if the title contains the word "urgent"
141+ */
142+ @Before (event = CqnService .EVENT_CREATE )
143+ public void ensureHighUrgencyForIncidentsWithUrgentInTitle (List < Incidents > incidents ) {
144+ for (Incidents incident : incidents) {
145+ if (incident .getTitle ().toLowerCase (Locale .ENGLISH ).contains (" urgent" ) &&
146+ incident .getUrgencyCode () == null || ! incident .getUrgencyCode ().equals (" H" )) {
147+ incident .setUrgencyCode (" H" );
148+ logger .info (" Adjusted Urgency for incident '{}' to 'HIGH'." , incident .getTitle ());
157149 }
158150
159151 }
152+ }
153+
154+ /*
155+ * Handler to avoid updating a "closed" incident
156+ */
157+ @Before (event = CqnService .EVENT_UPDATE )
158+ public void ensureNoUpdateOnClosedIncidents (Incidents incident ) {
159+ Incidents in = db .run (Select .from (Incidents_ .class ).where (i - > i .ID ().eq (incident .getId ()))).single (Incidents .class );
160+ if (in .getStatusCode ().equals (" C" )) {
161+ throw new ServiceException (ErrorStatuses .CONFLICT , " Can't modify a closed incident" );
162+ }
163+
164+ }
160165
161166 }
162167 ` ` `
0 commit comments