use of com.github.tfaga.lynx.exceptions.InvalidEntityFieldException in project lynx by TFaga.
the class JPAUtils method createOrderQuery.
public static List<Order> createOrderQuery(CriteriaBuilder cb, Root<?> r, QueryParameters q, String id) {
List<Order> orders = new ArrayList<>();
q.getOrder().stream().filter(qo -> qo.getField() != null).forEach(qo -> {
try {
CriteriaField field = getCriteriaField(qo.getField(), r);
if (field.containsToMany()) {
throw new InvalidEntityFieldException("OneToMany and ManyToMany relations are not supported by the order query", qo.getField(), r.getJavaType().getSimpleName());
}
if (qo.getOrder() == OrderDirection.DESC) {
orders.add(cb.desc(field.getPath()));
} else {
orders.add(cb.asc(field.getPath()));
}
} catch (IllegalArgumentException e) {
throw new NoSuchEntityFieldException(e.getMessage(), qo.getField(), r.getJavaType().getSimpleName());
}
});
// Add sort by id for correct pagination when field has same values
if (id != null) {
orders.add(cb.asc(getCriteriaField(id, r).getPath()));
}
return orders;
}
Aggregations