12
12
import org .gridsuite .shortcircuit .server .dto .ResourceFilter ;
13
13
import org .gridsuite .shortcircuit .server .dto .ShortCircuitLimits ;
14
14
import org .gridsuite .shortcircuit .server .entities .*;
15
+ import org .gridsuite .shortcircuit .server .service .ShortCircuitRunContext ;
15
16
import org .gridsuite .shortcircuit .server .utils .FaultResultSpecificationBuilder ;
16
17
import org .gridsuite .shortcircuit .server .utils .FeederResultSpecificationBuilder ;
17
18
import org .slf4j .Logger ;
18
19
import org .slf4j .LoggerFactory ;
19
20
import org .springframework .beans .factory .annotation .Autowired ;
20
- import org .springframework .data .domain .Page ;
21
- import org .springframework .data .domain .Pageable ;
21
+ import org .springframework .data .domain .*;
22
22
import org .springframework .data .jpa .domain .Specification ;
23
23
import org .springframework .stereotype .Repository ;
24
24
import org .springframework .transaction .annotation .Transactional ;
@@ -43,6 +43,12 @@ public class ShortCircuitAnalysisResultRepository {
43
43
private final FaultResultRepository faultResultRepository ;
44
44
private final FeederResultRepository feederResultRepository ;
45
45
46
+ private static final String DEFAULT_FAULT_RESULT_SORT_COLUMN = "faultResultUuid" ;
47
+
48
+ private static final String DEFAULT_FEEDER_RESULT_SORT_COLUMN = "feederResultUuid" ;
49
+
50
+ private static final Sort .Direction DEFAULT_SORT_DIRECTION = Sort .Direction .ASC ;
51
+
46
52
@ Autowired
47
53
public ShortCircuitAnalysisResultRepository (GlobalStatusRepository globalStatusRepository ,
48
54
ResultRepository resultRepository ,
@@ -161,10 +167,12 @@ public void insertStatus(List<UUID> resultUuids, String status) {
161
167
}
162
168
163
169
@ Transactional
164
- public void insert (UUID resultUuid , ShortCircuitAnalysisResult result , Map < String , ShortCircuitLimits > allCurrentLimits , String status ) {
170
+ public void insert (UUID resultUuid , ShortCircuitAnalysisResult result , ShortCircuitRunContext runContext , String status ) {
165
171
Objects .requireNonNull (resultUuid );
166
- if (result != null && !result .getFaultResults ().stream ().map (FaultResult ::getStatus ).allMatch (FaultResult .Status .NO_SHORT_CIRCUIT_DATA ::equals )) {
167
- resultRepository .save (toResultEntity (resultUuid , result , allCurrentLimits ));
172
+ if (result != null && (runContext .getBusId () != null ||
173
+ !result .getFaultResults ().stream ().map (FaultResult ::getStatus ).allMatch (FaultResult .Status .NO_SHORT_CIRCUIT_DATA ::equals ))
174
+ ) {
175
+ resultRepository .save (toResultEntity (resultUuid , result , runContext .getShortCircuitLimits ()));
168
176
}
169
177
globalStatusRepository .save (toStatusEntity (resultUuid , status ));
170
178
}
@@ -209,10 +217,10 @@ public Optional<ShortCircuitAnalysisResultEntity> findWithFaultResults(UUID resu
209
217
public Optional <ShortCircuitAnalysisResultEntity > findFullResults (UUID resultUuid ) {
210
218
Objects .requireNonNull (resultUuid );
211
219
Optional <ShortCircuitAnalysisResultEntity > result = resultRepository .findWithFaultResultsAndLimitViolationsByResultUuid (resultUuid );
212
- if (! result .isPresent ()) {
220
+ if (result .isEmpty ()) {
213
221
return result ;
214
222
}
215
- // using the the Hibernate First-Level Cache or Persistence Context
223
+ // using the Hibernate First-Level Cache or Persistence Context
216
224
// cf.https://vladmihalcea.com/spring-data-jpa-multiplebagfetchexception/
217
225
if (!result .get ().getFaultResults ().isEmpty ()) {
218
226
resultRepository .findWithFaultResultsAndFeederResultsByResultUuid (resultUuid );
@@ -224,14 +232,14 @@ public Optional<ShortCircuitAnalysisResultEntity> findFullResults(UUID resultUui
224
232
public Optional <ShortCircuitAnalysisResultEntity > findResultsWithLimitViolations (UUID resultUuid ) {
225
233
Objects .requireNonNull (resultUuid );
226
234
Optional <ShortCircuitAnalysisResultEntity > result = resultRepository .findWithFaultResultsAndLimitViolationsByResultUuid (resultUuid );
227
- if (! result .isPresent ()) {
235
+ if (result .isEmpty ()) {
228
236
return result ;
229
237
}
230
238
List <UUID > faultResultsUuidWithLimitViolations = result .get ().getFaultResults ().stream ()
231
239
.filter (fr -> !fr .getLimitViolations ().isEmpty ())
232
240
.map (FaultResultEntity ::getFaultResultUuid )
233
241
.collect (Collectors .toList ());
234
- // using the the Hibernate First-Level Cache or Persistence Context
242
+ // using the Hibernate First-Level Cache or Persistence Context
235
243
// cf.https://vladmihalcea.com/spring-data-jpa-multiplebagfetchexception/
236
244
if (!result .get ().getFaultResults ().isEmpty ()) {
237
245
faultResultRepository .findAllWithFeederResultsByFaultResultUuidIn (faultResultsUuidWithLimitViolations );
@@ -247,7 +255,7 @@ public Page<FaultResultEntity> findFaultResultsPage(ShortCircuitAnalysisResultEn
247
255
// HHH000104: firstResult/maxResults specified with collection fetch; applying in memory!
248
256
// cf. https://vladmihalcea.com/fix-hibernate-hhh000104-entity-fetch-pagination-warning-message/
249
257
// We must separate in two requests, one with pagination the other one with Join Fetch
250
- Page <FaultResultEntity > faultResultsPage = faultResultRepository .findAll (specification , pageable );
258
+ Page <FaultResultEntity > faultResultsPage = faultResultRepository .findAll (specification , addDefaultSort ( pageable , DEFAULT_FAULT_RESULT_SORT_COLUMN ) );
251
259
if (faultResultsPage .hasContent () && mode != FaultResultsMode .BASIC ) {
252
260
appendLimitViolationsAndFeederResults (faultResultsPage );
253
261
}
@@ -258,7 +266,7 @@ public Page<FaultResultEntity> findFaultResultsPage(ShortCircuitAnalysisResultEn
258
266
public Page <FeederResultEntity > findFeederResultsPage (ShortCircuitAnalysisResultEntity result , List <ResourceFilter > resourceFilters , Pageable pageable ) {
259
267
Objects .requireNonNull (result );
260
268
Specification <FeederResultEntity > specification = FeederResultSpecificationBuilder .buildSpecification (result .getResultUuid (), resourceFilters );
261
- return feederResultRepository .findAll (specification , pageable );
269
+ return feederResultRepository .findAll (specification , addDefaultSort ( pageable , DEFAULT_FEEDER_RESULT_SORT_COLUMN ) );
262
270
}
263
271
264
272
@ Transactional (readOnly = true )
@@ -270,15 +278,15 @@ public Page<FaultResultEntity> findFaultResultsWithLimitViolationsPage(ShortCirc
270
278
// HHH000104: firstResult/maxResults specified with collection fetch; applying in memory!
271
279
// cf. https://vladmihalcea.com/fix-hibernate-hhh000104-entity-fetch-pagination-warning-message/
272
280
// We must separate in two requests, one with pagination the other one with Join Fetch
273
- Page <FaultResultEntity > faultResultsPage = faultResultRepository .findAll (specification , pageable );
281
+ Page <FaultResultEntity > faultResultsPage = faultResultRepository .findAll (specification , addDefaultSort ( pageable , DEFAULT_FAULT_RESULT_SORT_COLUMN ) );
274
282
if (faultResultsPage .hasContent ()) {
275
283
appendLimitViolationsAndFeederResults (faultResultsPage );
276
284
}
277
285
return faultResultsPage ;
278
286
}
279
287
280
288
private void appendLimitViolationsAndFeederResults (Page <FaultResultEntity > pagedFaultResults ) {
281
- // using the the Hibernate First-Level Cache or Persistence Context
289
+ // using the Hibernate First-Level Cache or Persistence Context
282
290
// cf.https://vladmihalcea.com/spring-data-jpa-multiplebagfetchexception/
283
291
if (!pagedFaultResults .isEmpty ()) {
284
292
List <UUID > faultResultsUuids = pagedFaultResults .stream ()
@@ -299,4 +307,14 @@ public String findStatus(UUID resultUuid) {
299
307
return null ;
300
308
}
301
309
}
310
+
311
+ private Pageable addDefaultSort (Pageable pageable , String defaultSortColumn ) {
312
+ if (pageable .isPaged () && pageable .getSort ().getOrderFor (defaultSortColumn ) == null ) {
313
+ //if it's already sorted by our defaultColumn we don't add another sort by the same column
314
+ Sort finalSort = pageable .getSort ().and (Sort .by (DEFAULT_SORT_DIRECTION , defaultSortColumn ));
315
+ return PageRequest .of (pageable .getPageNumber (), pageable .getPageSize (), finalSort );
316
+ }
317
+ //nothing to do if the request is not paged
318
+ return pageable ;
319
+ }
302
320
}
0 commit comments