Skip to content

Commit

Permalink
feat(policy-api):[eclipse-tractusx#639] split up method in order to r…
Browse files Browse the repository at this point in the history
…educe complexity
  • Loading branch information
dsmf committed Jul 3, 2024
1 parent b31a8e3 commit 9360ff0
Showing 1 changed file with 30 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,37 +86,11 @@ private Comparator<PolicyWithBpn> getComparator(final Pageable pageable) {

final List<Sort.Order> sort = pageable.getSort().stream().toList();
for (final Sort.Order order : sort) {

Comparator<PolicyWithBpn> 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<Permission> 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) {
Expand All @@ -126,6 +100,34 @@ private Comparator<PolicyWithBpn> getComparator(final Pageable pageable) {
return comparator;
}

private Comparator<PolicyWithBpn> getComparator(final Pageable pageable, final Sort.Order order) {
Comparator<PolicyWithBpn> 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<Permission> 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()) {
Expand Down

0 comments on commit 9360ff0

Please sign in to comment.