diff --git a/irs-policy-store/src/main/java/org/eclipse/tractusx/irs/policystore/services/PolicyPagingService.java b/irs-policy-store/src/main/java/org/eclipse/tractusx/irs/policystore/services/PolicyPagingService.java index dd6c5f17e..1270a2daf 100644 --- a/irs-policy-store/src/main/java/org/eclipse/tractusx/irs/policystore/services/PolicyPagingService.java +++ b/irs-policy-store/src/main/java/org/eclipse/tractusx/irs/policystore/services/PolicyPagingService.java @@ -86,37 +86,11 @@ private Comparator getComparator(final Pageable pageable) { final List sort = pageable.getSort().stream().toList(); for (final Sort.Order order : sort) { - - Comparator fieldComparator; - final String property = order.getProperty(); - if (property.equals("bpn")) { - fieldComparator = Comparator.comparing(PolicyWithBpn::bpn); - } else if (property.equals("validUntil")) { - fieldComparator = Comparator.comparing(p -> p.policy().getValidUntil()); - } else if (property.equals("policyId")) { - fieldComparator = Comparator.comparing(p -> p.policy().getPolicyId()); - } else if (property.equals("createdOn")) { - fieldComparator = Comparator.comparing(p -> p.policy().getCreatedOn()); - } else if (property.equals("action")) { - fieldComparator = Comparator.comparing(p -> { - final List permissions = p.policy().getPermissions(); - return permissions.isEmpty() ? null : permissions.get(0).getAction(); - }); - } else { - log.warn("Sorting by field '{}' is not supported", order.getProperty()); - throw new IllegalArgumentException("Sorting by this field is not supported"); - } - - if (getSortDirection(pageable, order.getProperty()) == Sort.Direction.DESC) { - fieldComparator = fieldComparator.reversed(); - } - if (comparator == null) { - comparator = fieldComparator; + comparator = getComparator(pageable, order); } else { - comparator = comparator.thenComparing(fieldComparator); + comparator = comparator.thenComparing(getComparator(pageable, order)); } - } if (comparator == null) { @@ -126,6 +100,34 @@ private Comparator getComparator(final Pageable pageable) { return comparator; } + private Comparator getComparator(final Pageable pageable, final Sort.Order order) { + Comparator fieldComparator; + final String property = order.getProperty(); + if ("bpn".equalsIgnoreCase(property)) { + fieldComparator = Comparator.comparing(PolicyWithBpn::bpn); + } else if ("validUntil".equalsIgnoreCase(property)) { + fieldComparator = Comparator.comparing(p -> p.policy().getValidUntil()); + } else if ("policyId".equalsIgnoreCase(property)) { + fieldComparator = Comparator.comparing(p -> p.policy().getPolicyId()); + } else if ("createdOn".equalsIgnoreCase(property)) { + fieldComparator = Comparator.comparing(p -> p.policy().getCreatedOn()); + } else if ("action".equalsIgnoreCase(property)) { + fieldComparator = Comparator.comparing(p -> { + final List permissions = p.policy().getPermissions(); + return permissions.isEmpty() ? null : permissions.get(0).getAction(); + }); + } else { + log.warn("Sorting by field '{}' is not supported", order.getProperty()); + throw new IllegalArgumentException("Sorting by this field is not supported"); + } + + if (getSortDirection(pageable, order.getProperty()) == Sort.Direction.DESC) { + fieldComparator = fieldComparator.reversed(); + } + + return fieldComparator; + } + public Sort.Direction getSortDirection(final Pageable pageable, final String fieldName) { if (pageable.getSort().isUnsorted()) {