Search in sources :

Example 6 with NoSuchEntityFieldException

use of com.github.tfaga.lynx.exceptions.NoSuchEntityFieldException in project lynx by TFaga.

the class JPAUtils method createWhereQueryInternal.

// Temporary methods to not break the public API
private static CriteriaWhereQuery createWhereQueryInternal(CriteriaBuilder cb, Root<?> r, QueryParameters q) {
    Predicate predicate = cb.conjunction();
    Boolean containsToMany = false;
    for (QueryFilter f : q.getFilters()) {
        Predicate np = null;
        try {
            CriteriaField criteriaField = getCriteriaField(f.getField(), r);
            if (criteriaField.containsToMany()) {
                containsToMany = true;
            }
            Path entityField = criteriaField.getPath();
            if (entityField.getModel() == null || !((Attribute) entityField.getModel()).getPersistentAttributeType().equals(Attribute.PersistentAttributeType.BASIC)) {
                continue;
            }
            @SuppressWarnings("unchecked") Path<String> stringField = (Path<String>) entityField;
            @SuppressWarnings("unchecked") Path<Date> dateField = (Path<Date>) entityField;
            @SuppressWarnings("unchecked") Path<Comparable> compField = (Path<Comparable>) entityField;
            switch(f.getOperation()) {
                case EQ:
                    if (f.getDateValue() != null && entityField.getJavaType().equals(Date.class)) {
                        np = cb.equal(entityField, f.getDateValue());
                    } else if (f.getValue() != null) {
                        np = cb.equal(entityField, getValueForPath(entityField, f.getValue()));
                    }
                    break;
                case EQIC:
                    if (entityField.getJavaType().equals(String.class) && f.getValue() != null) {
                        np = cb.equal(cb.lower(stringField), f.getValue().toLowerCase());
                    }
                    break;
                case NEQ:
                    if (f.getDateValue() != null && entityField.getJavaType().equals(Date.class)) {
                        np = cb.notEqual(entityField, f.getDateValue());
                    } else if (f.getValue() != null) {
                        np = cb.notEqual(entityField, getValueForPath(entityField, f.getValue()));
                    }
                    break;
                case NEQIC:
                    if (entityField.getJavaType().equals(String.class) && f.getValue() != null) {
                        np = cb.notEqual(cb.lower(stringField), f.getValue().toLowerCase());
                    }
                    break;
                case LIKE:
                    if (entityField.getJavaType().equals(String.class) && f.getValue() != null) {
                        np = cb.like(stringField, f.getValue());
                    }
                    break;
                case LIKEIC:
                    if (entityField.getJavaType().equals(String.class) && f.getValue() != null) {
                        np = cb.like(cb.lower(stringField), f.getValue().toLowerCase());
                    }
                    break;
                case GT:
                    if (Date.class.isAssignableFrom(entityField.getJavaType()) || Number.class.isAssignableFrom(entityField.getJavaType()) || String.class.isAssignableFrom(entityField.getJavaType())) {
                        if (f.getDateValue() != null && entityField.getJavaType().equals(Date.class)) {
                            np = cb.greaterThan(dateField, f.getDateValue());
                        } else if (f.getValue() != null) {
                            np = cb.greaterThan(compField, (Comparable) getValueForPath(stringField, f.getValue()));
                        }
                    }
                    break;
                case GTE:
                    if (Date.class.isAssignableFrom(entityField.getJavaType()) || Number.class.isAssignableFrom(entityField.getJavaType()) || String.class.isAssignableFrom(entityField.getJavaType())) {
                        if (f.getDateValue() != null && entityField.getJavaType().equals(Date.class)) {
                            np = cb.greaterThanOrEqualTo(dateField, f.getDateValue());
                        } else if (f.getValue() != null) {
                            np = cb.greaterThanOrEqualTo(compField, (Comparable) getValueForPath(stringField, f.getValue()));
                        }
                    }
                    break;
                case LT:
                    if (Date.class.isAssignableFrom(entityField.getJavaType()) || Number.class.isAssignableFrom(entityField.getJavaType()) || String.class.isAssignableFrom(entityField.getJavaType())) {
                        if (f.getDateValue() != null && entityField.getJavaType().equals(Date.class)) {
                            np = cb.lessThan(dateField, f.getDateValue());
                        } else if (f.getValue() != null) {
                            np = cb.lessThan(compField, (Comparable) getValueForPath(stringField, f.getValue()));
                        }
                    }
                    break;
                case LTE:
                    if (Date.class.isAssignableFrom(entityField.getJavaType()) || Number.class.isAssignableFrom(entityField.getJavaType()) || String.class.isAssignableFrom(entityField.getJavaType())) {
                        if (f.getDateValue() != null && entityField.getJavaType().equals(Date.class)) {
                            np = cb.lessThanOrEqualTo(dateField, f.getDateValue());
                        } else if (f.getValue() != null) {
                            np = cb.lessThanOrEqualTo(compField, (Comparable) getValueForPath(stringField, f.getValue()));
                        }
                    }
                    break;
                case IN:
                    np = stringField.in(f.getValues().stream().filter(Objects::nonNull).map(s -> getValueForPath(entityField, s)).collect(Collectors.toList()));
                    break;
                case INIC:
                    if (entityField.getJavaType().equals(String.class)) {
                        np = cb.lower(stringField).in(f.getValues().stream().filter(Objects::nonNull).map(String::toLowerCase).collect(Collectors.toList()));
                    }
                    break;
                case NIN:
                    np = cb.not(stringField.in(f.getValues().stream().filter(Objects::nonNull).map(s -> getValueForPath(entityField, s)).collect(Collectors.toList())));
                    break;
                case NINIC:
                    if (entityField.getJavaType().equals(String.class)) {
                        np = cb.not(cb.lower(stringField).in(f.getValues().stream().filter(Objects::nonNull).map(String::toLowerCase).collect(Collectors.toList())));
                    }
                    break;
                case ISNULL:
                    np = cb.isNull(entityField);
                    break;
                case ISNOTNULL:
                    np = cb.isNotNull(entityField);
                    break;
            }
        } catch (IllegalArgumentException e) {
            throw new NoSuchEntityFieldException(e.getMessage(), f.getField(), r.getJavaType().getSimpleName());
        }
        if (np != null) {
            predicate = cb.and(predicate, np);
        }
    }
    return new CriteriaWhereQuery(predicate, containsToMany);
}
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) QueryFilter(com.github.tfaga.lynx.beans.QueryFilter) CriteriaWhereQuery(com.github.tfaga.lynx.beans.CriteriaWhereQuery) CriteriaField(com.github.tfaga.lynx.beans.CriteriaField) NoSuchEntityFieldException(com.github.tfaga.lynx.exceptions.NoSuchEntityFieldException)

Example 7 with NoSuchEntityFieldException

use of com.github.tfaga.lynx.exceptions.NoSuchEntityFieldException 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

NoSuchEntityFieldException (com.github.tfaga.lynx.exceptions.NoSuchEntityFieldException)7 QueryParameters (com.github.tfaga.lynx.beans.QueryParameters)6 QueryFilter (com.github.tfaga.lynx.beans.QueryFilter)4 Test (org.junit.Test)4 CriteriaField (com.github.tfaga.lynx.beans.CriteriaField)3 Field (java.lang.reflect.Field)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 Tuple (javax.persistence.Tuple)3 CriteriaWhereQuery (com.github.tfaga.lynx.beans.CriteriaWhereQuery)2 QueryOrder (com.github.tfaga.lynx.beans.QueryOrder)2 OrderDirection (com.github.tfaga.lynx.enums.OrderDirection)2 InvalidEntityFieldException (com.github.tfaga.lynx.exceptions.InvalidEntityFieldException)2 InvalidFieldValueException (com.github.tfaga.lynx.exceptions.InvalidFieldValueException)2 CriteriaFilter (com.github.tfaga.lynx.interfaces.CriteriaFilter)2 ZonedDateTime (java.time.ZonedDateTime)2 DateTimeParseException (java.time.format.DateTimeParseException)2 java.util (java.util)2 Logger (java.util.logging.Logger)2 Collectors (java.util.stream.Collectors)2 EntityManager (javax.persistence.EntityManager)2