Skip to content

Commit 6e9ef33

Browse files
authored
add default sorting for paginated requests (#75)
Signed-off-by: Abdelsalem <[email protected]>
1 parent 35e05d9 commit 6e9ef33

File tree

4 files changed

+228
-62
lines changed

4 files changed

+228
-62
lines changed

src/main/java/org/gridsuite/shortcircuit/server/repositories/ShortCircuitAnalysisResultRepository.java

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
import org.slf4j.Logger;
1919
import org.slf4j.LoggerFactory;
2020
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.*;
2322
import org.springframework.data.jpa.domain.Specification;
2423
import org.springframework.stereotype.Repository;
2524
import org.springframework.transaction.annotation.Transactional;
@@ -44,6 +43,12 @@ public class ShortCircuitAnalysisResultRepository {
4443
private final FaultResultRepository faultResultRepository;
4544
private final FeederResultRepository feederResultRepository;
4645

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+
4752
@Autowired
4853
public ShortCircuitAnalysisResultRepository(GlobalStatusRepository globalStatusRepository,
4954
ResultRepository resultRepository,
@@ -212,10 +217,10 @@ public Optional<ShortCircuitAnalysisResultEntity> findWithFaultResults(UUID resu
212217
public Optional<ShortCircuitAnalysisResultEntity> findFullResults(UUID resultUuid) {
213218
Objects.requireNonNull(resultUuid);
214219
Optional<ShortCircuitAnalysisResultEntity> result = resultRepository.findWithFaultResultsAndLimitViolationsByResultUuid(resultUuid);
215-
if (!result.isPresent()) {
220+
if (result.isEmpty()) {
216221
return result;
217222
}
218-
// using the the Hibernate First-Level Cache or Persistence Context
223+
// using the Hibernate First-Level Cache or Persistence Context
219224
// cf.https://vladmihalcea.com/spring-data-jpa-multiplebagfetchexception/
220225
if (!result.get().getFaultResults().isEmpty()) {
221226
resultRepository.findWithFaultResultsAndFeederResultsByResultUuid(resultUuid);
@@ -227,14 +232,14 @@ public Optional<ShortCircuitAnalysisResultEntity> findFullResults(UUID resultUui
227232
public Optional<ShortCircuitAnalysisResultEntity> findResultsWithLimitViolations(UUID resultUuid) {
228233
Objects.requireNonNull(resultUuid);
229234
Optional<ShortCircuitAnalysisResultEntity> result = resultRepository.findWithFaultResultsAndLimitViolationsByResultUuid(resultUuid);
230-
if (!result.isPresent()) {
235+
if (result.isEmpty()) {
231236
return result;
232237
}
233238
List<UUID> faultResultsUuidWithLimitViolations = result.get().getFaultResults().stream()
234239
.filter(fr -> !fr.getLimitViolations().isEmpty())
235240
.map(FaultResultEntity::getFaultResultUuid)
236241
.collect(Collectors.toList());
237-
// using the the Hibernate First-Level Cache or Persistence Context
242+
// using the Hibernate First-Level Cache or Persistence Context
238243
// cf.https://vladmihalcea.com/spring-data-jpa-multiplebagfetchexception/
239244
if (!result.get().getFaultResults().isEmpty()) {
240245
faultResultRepository.findAllWithFeederResultsByFaultResultUuidIn(faultResultsUuidWithLimitViolations);
@@ -250,7 +255,7 @@ public Page<FaultResultEntity> findFaultResultsPage(ShortCircuitAnalysisResultEn
250255
// HHH000104: firstResult/maxResults specified with collection fetch; applying in memory!
251256
// cf. https://vladmihalcea.com/fix-hibernate-hhh000104-entity-fetch-pagination-warning-message/
252257
// 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));
254259
if (faultResultsPage.hasContent() && mode != FaultResultsMode.BASIC) {
255260
appendLimitViolationsAndFeederResults(faultResultsPage);
256261
}
@@ -261,7 +266,7 @@ public Page<FaultResultEntity> findFaultResultsPage(ShortCircuitAnalysisResultEn
261266
public Page<FeederResultEntity> findFeederResultsPage(ShortCircuitAnalysisResultEntity result, List<ResourceFilter> resourceFilters, Pageable pageable) {
262267
Objects.requireNonNull(result);
263268
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));
265270
}
266271

267272
@Transactional(readOnly = true)
@@ -273,15 +278,15 @@ public Page<FaultResultEntity> findFaultResultsWithLimitViolationsPage(ShortCirc
273278
// HHH000104: firstResult/maxResults specified with collection fetch; applying in memory!
274279
// cf. https://vladmihalcea.com/fix-hibernate-hhh000104-entity-fetch-pagination-warning-message/
275280
// 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));
277282
if (faultResultsPage.hasContent()) {
278283
appendLimitViolationsAndFeederResults(faultResultsPage);
279284
}
280285
return faultResultsPage;
281286
}
282287

283288
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
285290
// cf.https://vladmihalcea.com/spring-data-jpa-multiplebagfetchexception/
286291
if (!pagedFaultResults.isEmpty()) {
287292
List<UUID> faultResultsUuids = pagedFaultResults.stream()
@@ -302,4 +307,14 @@ public String findStatus(UUID resultUuid) {
302307
return null;
303308
}
304309
}
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+
}
305320
}

0 commit comments

Comments
 (0)