22
33import com .park .utmstack .domain .application_events .enums .ApplicationEventType ;
44import com .park .utmstack .domain .incident .UtmIncident ;
5- import com .park .utmstack .service .application_events .ApplicationEventService ;
65import com .park .utmstack .service .dto .incident .*;
7- import com .park .utmstack .service .incident .UtmIncidentAlertService ;
86import com .park .utmstack .service .incident .UtmIncidentQueryService ;
97import com .park .utmstack .service .incident .UtmIncidentService ;
10- import com .park .utmstack .util .UtilResponse ;
8+ import com .park .utmstack .util .exceptions . NoAlertsProvidedException ;
119import com .park .utmstack .web .rest .errors .BadRequestAlertException ;
1210import com .park .utmstack .web .rest .util .HeaderUtil ;
1311import com .park .utmstack .web .rest .util .PaginationUtil ;
14- import org . slf4j . Logger ;
15- import org . slf4j .LoggerFactory ;
12+ import lombok . RequiredArgsConstructor ;
13+ import lombok . extern . slf4j .Slf4j ;
1614import org .springframework .data .domain .Page ;
1715import org .springframework .data .domain .Pageable ;
1816import org .springframework .http .HttpHeaders ;
19- import org .springframework .http .HttpStatus ;
2017import org .springframework .http .ResponseEntity ;
2118import org .springframework .util .CollectionUtils ;
2219import org .springframework .web .bind .annotation .*;
23- import tech . jhipster . web . util . ResponseUtil ;
20+ import com . park . utmstack . aop . logging . AuditEvent ;
2421
2522import javax .validation .Valid ;
2623import java .net .URI ;
2724import java .net .URISyntaxException ;
2825import java .util .List ;
2926import java .util .Optional ;
30- import java .util .stream .Collectors ;
3127
3228/**
3329 * REST controller for managing UtmIncident.
3430 */
3531@ RestController
32+ @ RequiredArgsConstructor
33+ @ Slf4j
3634@ RequestMapping ("/api" )
3735public class UtmIncidentResource {
38- private final String CLASS_NAME = "UtmIncidentResource" ;
39- private final Logger log = LoggerFactory .getLogger (UtmIncidentResource .class );
4036
4137 private static final String ENTITY_NAME = "utmIncident" ;
4238
4339 private final UtmIncidentService utmIncidentService ;
4440
45- private final UtmIncidentAlertService utmIncidentAlertService ;
46-
4741 private final UtmIncidentQueryService utmIncidentQueryService ;
48- private final ApplicationEventService applicationEventService ;
49-
50- public UtmIncidentResource (UtmIncidentService utmIncidentService ,
51- UtmIncidentAlertService utmIncidentAlertService ,
52- UtmIncidentQueryService utmIncidentQueryService ,
53- ApplicationEventService applicationEventService ) {
54- this .utmIncidentService = utmIncidentService ;
55- this .utmIncidentAlertService = utmIncidentAlertService ;
56- this .utmIncidentQueryService = utmIncidentQueryService ;
57- this .applicationEventService = applicationEventService ;
58- }
42+
5943
6044 /**
6145 * Creates a new incident based on the provided details.
@@ -75,38 +59,21 @@ public UtmIncidentResource(UtmIncidentService utmIncidentService,
7559 * @throws IllegalArgumentException if the input data is invalid.
7660 */
7761 @ PostMapping ("/utm-incidents" )
62+ @ AuditEvent (
63+ attemptType = ApplicationEventType .INCIDENT_CREATION_ATTEMPT ,
64+ attemptMessage = "Attempt to create a new incident initiated" ,
65+ successType = ApplicationEventType .INCIDENT_CREATION_SUCCESS ,
66+ successMessage = "Incident created successfully"
67+ )
7868 public ResponseEntity <UtmIncident > createUtmIncident (@ Valid @ RequestBody NewIncidentDTO newIncidentDTO ) {
7969 final String ctx = ".createUtmIncident" ;
80- try {
81- if (CollectionUtils .isEmpty (newIncidentDTO .getAlertList ())) {
82- String msg = ctx + ": A new incident has to have at least one alert related" ;
83- log .error (msg );
84- applicationEventService .createEvent (msg , ApplicationEventType .ERROR );
85- return UtilResponse .buildErrorResponse (HttpStatus .BAD_REQUEST , msg );
86- }
87-
88- List <String > alertIds = newIncidentDTO .getAlertList ().stream ()
89- .map (RelatedIncidentAlertsDTO ::getAlertId )
90- .collect (Collectors .toList ());
91-
92- List <String > alertsFound = utmIncidentAlertService .existsAnyAlert (alertIds );
93-
94- if (!alertsFound .isEmpty ()) {
95- String alertIdsList = String .join (", " , alertIds );
96- String msg = "Some alerts are already linked to another incident. Alert IDs: " + alertIdsList + ". Check the related incidents for more details." ;
97- log .error (msg );
98- applicationEventService .createEvent (ctx + ": " + msg , ApplicationEventType .ERROR );
99- return UtilResponse .buildErrorResponse (HttpStatus .CONFLICT , utmIncidentAlertService .formatAlertMessage (alertsFound ));
100- }
101-
102-
103- return ResponseEntity .ok (utmIncidentService .createIncident (newIncidentDTO ));
104- } catch (Exception e ) {
105- String msg = ctx + ": " + e .getMessage ();
106- log .error (msg );
107- applicationEventService .createEvent (msg , ApplicationEventType .ERROR );
108- return UtilResponse .buildErrorResponse (HttpStatus .INTERNAL_SERVER_ERROR , msg );
70+
71+ if (CollectionUtils .isEmpty (newIncidentDTO .getAlertList ())) {
72+ String msg = ctx + ": A new incident has to have at least one alert related" ;
73+ throw new NoAlertsProvidedException (ctx + ": " + msg );
10974 }
75+
76+ return ResponseEntity .ok (utmIncidentService .createIncident (newIncidentDTO ));
11077 }
11178
11279 /**
@@ -124,36 +91,22 @@ public ResponseEntity<UtmIncident> createUtmIncident(@Valid @RequestBody NewInci
12491 * @throws URISyntaxException if the Location URI syntax is incorrect
12592 */
12693 @ PostMapping ("/utm-incidents/add-alerts" )
94+ @ AuditEvent (
95+ attemptType = ApplicationEventType .INCIDENT_ALERT_ADD_ATTEMPT ,
96+ attemptMessage = "Attempt to add alerts to incident initiated" ,
97+ successType = ApplicationEventType .INCIDENT_ALERT_ADD_SUCCESS ,
98+ successMessage = "Alerts added to incident successfully"
99+ )
127100 public ResponseEntity <UtmIncident > addAlertsToUtmIncident (@ Valid @ RequestBody AddToIncidentDTO addToIncidentDTO ) throws URISyntaxException {
128- final String ctx = ".addAlertsToUtmIncident" ;
129- try {
130- log .debug ("REST request to save UtmIncident : {}" , addToIncidentDTO );
131- if (CollectionUtils .isEmpty (addToIncidentDTO .getAlertList ())) {
132- throw new BadRequestAlertException ("Add utmIncident cannot already have an empty related alerts" , ENTITY_NAME , "alertList" );
133- }
134- List <String > alertIds = addToIncidentDTO .getAlertList ().stream ()
135- .map (RelatedIncidentAlertsDTO ::getAlertId )
136- .collect (Collectors .toList ());
137-
138- List <String > alertsFound = utmIncidentAlertService .existsAnyAlert (alertIds );
139-
140- if (!alertsFound .isEmpty ()) {
141- String alertIdsList = String .join (", " , alertIds );
142- String msg = "Some alerts are already linked to another incident. Alert IDs: " + alertIdsList + ". Check the related incidents for more details." ;
143- log .error (msg );
144- applicationEventService .createEvent (ctx + ": " + msg , ApplicationEventType .ERROR );
145- return UtilResponse .buildErrorResponse (HttpStatus .CONFLICT , utmIncidentAlertService .formatAlertMessage (alertsFound ));
146- }
147- UtmIncident result = utmIncidentService .addAlertsIncident (addToIncidentDTO );
148- return ResponseEntity .created (new URI ("/api/utm-incidents/add-alerts" + result .getId ()))
101+
102+ if (CollectionUtils .isEmpty (addToIncidentDTO .getAlertList ())) {
103+ throw new NoAlertsProvidedException ("Add utmIncident cannot already have an empty related alerts" );
104+ }
105+
106+ UtmIncident result = utmIncidentService .addAlertsIncident (addToIncidentDTO );
107+ return ResponseEntity .created (new URI ("/api/utm-incidents/add-alerts" + result .getId ()))
149108 .headers (HeaderUtil .createEntityCreationAlert (ENTITY_NAME , result .getId ().toString ()))
150109 .body (result );
151- } catch (Exception e ) {
152- String msg = ctx + ": " + e .getMessage ();
153- log .error (msg );
154- applicationEventService .createEvent (msg , ApplicationEventType .ERROR );
155- return ResponseEntity .status (HttpStatus .INTERNAL_SERVER_ERROR ).headers (HeaderUtil .createFailureAlert (CLASS_NAME , null , msg )).body (null );
156- }
157110 }
158111
159112 /**
@@ -166,23 +119,22 @@ public ResponseEntity<UtmIncident> addAlertsToUtmIncident(@Valid @RequestBody Ad
166119 * @throws URISyntaxException if the Location URI syntax is incorrect
167120 */
168121 @ PutMapping ("/utm-incidents/change-status" )
169- public ResponseEntity <UtmIncident > updateUtmIncident (@ Valid @ RequestBody UtmIncident utmIncident ) throws URISyntaxException {
170- final String ctx = ".updateUtmIncident" ;
171- try {
172- log .debug ("REST request to update UtmIncident : {}" , utmIncident );
173- if (utmIncident .getId () == null ) {
174- throw new BadRequestAlertException ("Invalid id" , ENTITY_NAME , "idnull" );
175- }
176- UtmIncident result = utmIncidentService .changeStatus (utmIncident );
177- return ResponseEntity .ok ()
122+ @ AuditEvent (
123+ attemptType = ApplicationEventType .INCIDENT_UPDATE_ATTEMPT ,
124+ attemptMessage = "Attempt to update incident status initiated" ,
125+ successType = ApplicationEventType .INCIDENT_UPDATE_SUCCESS ,
126+ successMessage = "Incident status updated successfully"
127+ )
128+ public ResponseEntity <UtmIncident > updateUtmIncident (@ Valid @ RequestBody UtmIncident utmIncident ) {
129+
130+ if (utmIncident .getId () == null ) {
131+ throw new BadRequestAlertException ("Invalid id" , ENTITY_NAME , "idnull" );
132+ }
133+ UtmIncident result = utmIncidentService .changeStatus (utmIncident );
134+
135+ return ResponseEntity .ok ()
178136 .headers (HeaderUtil .createEntityUpdateAlert (ENTITY_NAME , utmIncident .getId ().toString ()))
179137 .body (result );
180- } catch (Exception e ) {
181- String msg = ctx + ": " + e .getMessage ();
182- log .error (msg );
183- applicationEventService .createEvent (msg , ApplicationEventType .ERROR );
184- return ResponseEntity .status (HttpStatus .INTERNAL_SERVER_ERROR ).headers (HeaderUtil .createFailureAlert (CLASS_NAME , null , msg )).body (null );
185- }
186138 }
187139
188140 /**
@@ -194,18 +146,10 @@ public ResponseEntity<UtmIncident> updateUtmIncident(@Valid @RequestBody UtmInci
194146 */
195147 @ GetMapping ("/utm-incidents" )
196148 public ResponseEntity <List <UtmIncident >> getAllUtmIncidents (UtmIncidentCriteria criteria , Pageable pageable ) {
197- final String ctx = ".getAllUtmIncidents" ;
198- try {
199- log .debug ("REST request to get UtmIncidents by criteria: {}" , criteria );
200- Page <UtmIncident > page = utmIncidentQueryService .findByCriteria (criteria , pageable );
201- HttpHeaders headers = PaginationUtil .generatePaginationHttpHeaders (page , "/api/utm-incidents" );
202- return ResponseEntity .ok ().headers (headers ).body (page .getContent ());
203- } catch (Exception e ) {
204- String msg = ctx + ": " + e .getMessage ();
205- log .error (msg );
206- applicationEventService .createEvent (msg , ApplicationEventType .ERROR );
207- return ResponseEntity .status (HttpStatus .INTERNAL_SERVER_ERROR ).headers (HeaderUtil .createFailureAlert (CLASS_NAME , null , msg )).body (null );
208- }
149+
150+ Page <UtmIncident > page = utmIncidentQueryService .findByCriteria (criteria , pageable );
151+ HttpHeaders headers = PaginationUtil .generatePaginationHttpHeaders (page , "/api/utm-incidents" );
152+ return ResponseEntity .ok ().headers (headers ).body (page .getContent ());
209153 }
210154
211155 /**
@@ -215,15 +159,7 @@ public ResponseEntity<List<UtmIncident>> getAllUtmIncidents(UtmIncidentCriteria
215159 */
216160 @ GetMapping ("/utm-incidents/users-assigned" )
217161 public ResponseEntity <List <IncidentUserAssignedDTO >> getAllUserAssigned () {
218- final String ctx = ".getAllUserAssigned" ;
219- try {
220- return ResponseEntity .ok ().body (utmIncidentQueryService .getAllUsersAssigned ());
221- } catch (Exception e ) {
222- String msg = ctx + ": " + e .getMessage ();
223- log .error (msg );
224- applicationEventService .createEvent (msg , ApplicationEventType .ERROR );
225- return ResponseEntity .status (HttpStatus .INTERNAL_SERVER_ERROR ).headers (HeaderUtil .createFailureAlert (CLASS_NAME , null , msg )).body (null );
226- }
162+ return ResponseEntity .ok ().body (utmIncidentQueryService .getAllUsersAssigned ());
227163 }
228164
229165 /**
@@ -234,16 +170,9 @@ public ResponseEntity<List<IncidentUserAssignedDTO>> getAllUserAssigned() {
234170 */
235171 @ GetMapping ("/utm-incidents/{id}" )
236172 public ResponseEntity <UtmIncident > getUtmIncident (@ PathVariable Long id ) {
237- final String ctx = ".getUtmIncident" ;
238- try {
239- log .debug ("REST request to get UtmIncident : {}" , id );
240- Optional <UtmIncident > utmIncident = utmIncidentService .findOne (id );
241- return ResponseUtil .wrapOrNotFound (utmIncident );
242- } catch (Exception e ) {
243- String msg = ctx + ": " + e .getMessage ();
244- log .error (msg );
245- applicationEventService .createEvent (msg , ApplicationEventType .ERROR );
246- return ResponseEntity .status (HttpStatus .INTERNAL_SERVER_ERROR ).headers (HeaderUtil .createFailureAlert (CLASS_NAME , null , msg )).body (null );
247- }
173+
174+ Optional <UtmIncident > utmIncident = utmIncidentService .findOne (id );
175+ return tech .jhipster .web .util .ResponseUtil .wrapOrNotFound (utmIncident );
176+
248177 }
249178}
0 commit comments