18
18
import org .slf4j .Logger ;
19
19
import org .slf4j .LoggerFactory ;
20
20
import org .springframework .beans .factory .annotation .Autowired ;
21
- import org .springframework .data .domain .Page ;
22
- import org .springframework .data .domain .Pageable ;
21
+ import org .springframework .data .domain .*;
23
22
import org .springframework .data .jpa .domain .Specification ;
24
23
import org .springframework .stereotype .Repository ;
25
24
import org .springframework .transaction .annotation .Transactional ;
@@ -44,6 +43,12 @@ public class ShortCircuitAnalysisResultRepository {
44
43
private final FaultResultRepository faultResultRepository ;
45
44
private final FeederResultRepository feederResultRepository ;
46
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
+
47
52
@ Autowired
48
53
public ShortCircuitAnalysisResultRepository (GlobalStatusRepository globalStatusRepository ,
49
54
ResultRepository resultRepository ,
@@ -212,10 +217,10 @@ public Optional<ShortCircuitAnalysisResultEntity> findWithFaultResults(UUID resu
212
217
public Optional <ShortCircuitAnalysisResultEntity > findFullResults (UUID resultUuid ) {
213
218
Objects .requireNonNull (resultUuid );
214
219
Optional <ShortCircuitAnalysisResultEntity > result = resultRepository .findWithFaultResultsAndLimitViolationsByResultUuid (resultUuid );
215
- if (! result .isPresent ()) {
220
+ if (result .isEmpty ()) {
216
221
return result ;
217
222
}
218
- // using the the Hibernate First-Level Cache or Persistence Context
223
+ // using the Hibernate First-Level Cache or Persistence Context
219
224
// cf.https://vladmihalcea.com/spring-data-jpa-multiplebagfetchexception/
220
225
if (!result .get ().getFaultResults ().isEmpty ()) {
221
226
resultRepository .findWithFaultResultsAndFeederResultsByResultUuid (resultUuid );
@@ -227,14 +232,14 @@ public Optional<ShortCircuitAnalysisResultEntity> findFullResults(UUID resultUui
227
232
public Optional <ShortCircuitAnalysisResultEntity > findResultsWithLimitViolations (UUID resultUuid ) {
228
233
Objects .requireNonNull (resultUuid );
229
234
Optional <ShortCircuitAnalysisResultEntity > result = resultRepository .findWithFaultResultsAndLimitViolationsByResultUuid (resultUuid );
230
- if (! result .isPresent ()) {
235
+ if (result .isEmpty ()) {
231
236
return result ;
232
237
}
233
238
List <UUID > faultResultsUuidWithLimitViolations = result .get ().getFaultResults ().stream ()
234
239
.filter (fr -> !fr .getLimitViolations ().isEmpty ())
235
240
.map (FaultResultEntity ::getFaultResultUuid )
236
241
.collect (Collectors .toList ());
237
- // using the the Hibernate First-Level Cache or Persistence Context
242
+ // using the Hibernate First-Level Cache or Persistence Context
238
243
// cf.https://vladmihalcea.com/spring-data-jpa-multiplebagfetchexception/
239
244
if (!result .get ().getFaultResults ().isEmpty ()) {
240
245
faultResultRepository .findAllWithFeederResultsByFaultResultUuidIn (faultResultsUuidWithLimitViolations );
@@ -250,7 +255,7 @@ public Page<FaultResultEntity> findFaultResultsPage(ShortCircuitAnalysisResultEn
250
255
// HHH000104: firstResult/maxResults specified with collection fetch; applying in memory!
251
256
// cf. https://vladmihalcea.com/fix-hibernate-hhh000104-entity-fetch-pagination-warning-message/
252
257
// We must separate in two requests, one with pagination the other one with Join Fetch
253
- Page <FaultResultEntity > faultResultsPage = faultResultRepository .findAll (specification , pageable );
258
+ Page <FaultResultEntity > faultResultsPage = faultResultRepository .findAll (specification , addDefaultSort ( pageable , DEFAULT_FAULT_RESULT_SORT_COLUMN ) );
254
259
if (faultResultsPage .hasContent () && mode != FaultResultsMode .BASIC ) {
255
260
appendLimitViolationsAndFeederResults (faultResultsPage );
256
261
}
@@ -261,7 +266,7 @@ public Page<FaultResultEntity> findFaultResultsPage(ShortCircuitAnalysisResultEn
261
266
public Page <FeederResultEntity > findFeederResultsPage (ShortCircuitAnalysisResultEntity result , List <ResourceFilter > resourceFilters , Pageable pageable ) {
262
267
Objects .requireNonNull (result );
263
268
Specification <FeederResultEntity > specification = FeederResultSpecificationBuilder .buildSpecification (result .getResultUuid (), resourceFilters );
264
- return feederResultRepository .findAll (specification , pageable );
269
+ return feederResultRepository .findAll (specification , addDefaultSort ( pageable , DEFAULT_FEEDER_RESULT_SORT_COLUMN ) );
265
270
}
266
271
267
272
@ Transactional (readOnly = true )
@@ -273,15 +278,15 @@ public Page<FaultResultEntity> findFaultResultsWithLimitViolationsPage(ShortCirc
273
278
// HHH000104: firstResult/maxResults specified with collection fetch; applying in memory!
274
279
// cf. https://vladmihalcea.com/fix-hibernate-hhh000104-entity-fetch-pagination-warning-message/
275
280
// We must separate in two requests, one with pagination the other one with Join Fetch
276
- Page <FaultResultEntity > faultResultsPage = faultResultRepository .findAll (specification , pageable );
281
+ Page <FaultResultEntity > faultResultsPage = faultResultRepository .findAll (specification , addDefaultSort ( pageable , DEFAULT_FAULT_RESULT_SORT_COLUMN ) );
277
282
if (faultResultsPage .hasContent ()) {
278
283
appendLimitViolationsAndFeederResults (faultResultsPage );
279
284
}
280
285
return faultResultsPage ;
281
286
}
282
287
283
288
private void appendLimitViolationsAndFeederResults (Page <FaultResultEntity > pagedFaultResults ) {
284
- // using the the Hibernate First-Level Cache or Persistence Context
289
+ // using the Hibernate First-Level Cache or Persistence Context
285
290
// cf.https://vladmihalcea.com/spring-data-jpa-multiplebagfetchexception/
286
291
if (!pagedFaultResults .isEmpty ()) {
287
292
List <UUID > faultResultsUuids = pagedFaultResults .stream ()
@@ -302,4 +307,14 @@ public String findStatus(UUID resultUuid) {
302
307
return null ;
303
308
}
304
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
+ }
305
320
}
0 commit comments