Search in sources :

Example 1 with InvalidEntityFieldException

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;
}
Also used : CriteriaFilter(com.github.tfaga.lynx.interfaces.CriteriaFilter) java.util(java.util) ZonedDateTime(java.time.ZonedDateTime) TypedQuery(javax.persistence.TypedQuery) InvalidEntityFieldException(com.github.tfaga.lynx.exceptions.InvalidEntityFieldException) NoSuchEntityFieldException(com.github.tfaga.lynx.exceptions.NoSuchEntityFieldException) EntityType(javax.persistence.metamodel.EntityType) QueryFilter(com.github.tfaga.lynx.beans.QueryFilter) CriteriaField(com.github.tfaga.lynx.beans.CriteriaField) javax.persistence.criteria(javax.persistence.criteria) Metamodel(javax.persistence.metamodel.Metamodel) QueryParameters(com.github.tfaga.lynx.beans.QueryParameters) Tuple(javax.persistence.Tuple) SingularAttribute(javax.persistence.metamodel.SingularAttribute) Attribute(javax.persistence.metamodel.Attribute) OrderDirection(com.github.tfaga.lynx.enums.OrderDirection) EntityManager(javax.persistence.EntityManager) Field(java.lang.reflect.Field) Logger(java.util.logging.Logger) CriteriaWhereQuery(com.github.tfaga.lynx.beans.CriteriaWhereQuery) Collectors(java.util.stream.Collectors) InvocationTargetException(java.lang.reflect.InvocationTargetException) DateTimeParseException(java.time.format.DateTimeParseException) InvalidFieldValueException(com.github.tfaga.lynx.exceptions.InvalidFieldValueException) TupleElement(javax.persistence.TupleElement) InvalidEntityFieldException(com.github.tfaga.lynx.exceptions.InvalidEntityFieldException) CriteriaField(com.github.tfaga.lynx.beans.CriteriaField) NoSuchEntityFieldException(com.github.tfaga.lynx.exceptions.NoSuchEntityFieldException)

Aggregations

CriteriaField (com.github.tfaga.lynx.beans.CriteriaField)1 CriteriaWhereQuery (com.github.tfaga.lynx.beans.CriteriaWhereQuery)1 QueryFilter (com.github.tfaga.lynx.beans.QueryFilter)1 QueryParameters (com.github.tfaga.lynx.beans.QueryParameters)1 OrderDirection (com.github.tfaga.lynx.enums.OrderDirection)1 InvalidEntityFieldException (com.github.tfaga.lynx.exceptions.InvalidEntityFieldException)1 InvalidFieldValueException (com.github.tfaga.lynx.exceptions.InvalidFieldValueException)1 NoSuchEntityFieldException (com.github.tfaga.lynx.exceptions.NoSuchEntityFieldException)1 CriteriaFilter (com.github.tfaga.lynx.interfaces.CriteriaFilter)1 Field (java.lang.reflect.Field)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ZonedDateTime (java.time.ZonedDateTime)1 DateTimeParseException (java.time.format.DateTimeParseException)1 java.util (java.util)1 Logger (java.util.logging.Logger)1 Collectors (java.util.stream.Collectors)1 EntityManager (javax.persistence.EntityManager)1 Tuple (javax.persistence.Tuple)1 TupleElement (javax.persistence.TupleElement)1 TypedQuery (javax.persistence.TypedQuery)1