Search in sources :

Example 46 with QueryFilter

use of com.github.tfaga.lynx.beans.QueryFilter in project lynx by TFaga.

the class JPAUtilsFiltersTest method testEqic.

@Test
public void testEqic() {
    QueryFilter qf = new QueryFilter();
    qf.setField("firstname");
    qf.setOperation(FilterOperation.EQIC);
    qf.setValue("jULIa");
    QueryParameters q = new QueryParameters();
    q.getFilters().add(qf);
    List<User> users = JPAUtils.queryEntities(em, User.class, q);
    Assert.assertNotNull(users);
    Assert.assertEquals(2, users.size());
}
Also used : QueryFilter(com.github.tfaga.lynx.beans.QueryFilter) User(com.github.tfaga.lynx.test.entities.User) QueryParameters(com.github.tfaga.lynx.beans.QueryParameters) Test(org.junit.Test)

Example 47 with QueryFilter

use of com.github.tfaga.lynx.beans.QueryFilter in project lynx by TFaga.

the class JPAUtilsFiltersTest method testMultipleFilters.

@Test
public void testMultipleFilters() {
    QueryParameters q = new QueryParameters();
    QueryFilter qf = new QueryFilter();
    qf.setField("firstname");
    qf.setOperation(FilterOperation.IN);
    qf.getValues().add("Bruce");
    qf.getValues().add("Karen");
    qf.getValues().add("Sandra");
    qf.getValues().add("Laura");
    q.getFilters().add(qf);
    qf = new QueryFilter();
    qf.setField("email");
    qf.setOperation(FilterOperation.LIKE);
    qf.setValue("%@mozilla.org");
    q.getFilters().add(qf);
    List<User> users = JPAUtils.queryEntities(em, User.class, q);
    Assert.assertNotNull(users);
    Assert.assertEquals(2, users.size());
}
Also used : QueryFilter(com.github.tfaga.lynx.beans.QueryFilter) User(com.github.tfaga.lynx.test.entities.User) QueryParameters(com.github.tfaga.lynx.beans.QueryParameters) Test(org.junit.Test)

Example 48 with QueryFilter

use of com.github.tfaga.lynx.beans.QueryFilter in project lynx by TFaga.

the class JPAUtilsFiltersTest method testSingleIntegerFilter.

@Test
public void testSingleIntegerFilter() {
    QueryFilter qf = new QueryFilter();
    qf.setField("role");
    qf.setOperation(FilterOperation.GT);
    qf.setValue("0");
    QueryParameters q = new QueryParameters();
    q.getFilters().add(qf);
    List<User> users = JPAUtils.queryEntities(em, User.class, q);
    Assert.assertNotNull(users);
    Assert.assertEquals(47, users.size());
    Assert.assertEquals(1, users.get(0).getRole().intValue());
}
Also used : QueryFilter(com.github.tfaga.lynx.beans.QueryFilter) User(com.github.tfaga.lynx.test.entities.User) QueryParameters(com.github.tfaga.lynx.beans.QueryParameters) Test(org.junit.Test)

Example 49 with QueryFilter

use of com.github.tfaga.lynx.beans.QueryFilter 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)

Aggregations

QueryFilter (com.github.tfaga.lynx.beans.QueryFilter)49 Test (org.junit.Test)47 QueryParameters (com.github.tfaga.lynx.beans.QueryParameters)44 User (com.github.tfaga.lynx.test.entities.User)28 Project (com.github.tfaga.lynx.test.entities.Project)10 Date (java.util.Date)6 NoSuchEntityFieldException (com.github.tfaga.lynx.exceptions.NoSuchEntityFieldException)3 OrderDirection (com.github.tfaga.lynx.enums.OrderDirection)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 CriteriaField (com.github.tfaga.lynx.beans.CriteriaField)1 CriteriaWhereQuery (com.github.tfaga.lynx.beans.CriteriaWhereQuery)1 QueryOrder (com.github.tfaga.lynx.beans.QueryOrder)1 FilterOperation (com.github.tfaga.lynx.enums.FilterOperation)1 QueryFormatError (com.github.tfaga.lynx.enums.QueryFormatError)1 InvalidEntityFieldException (com.github.tfaga.lynx.exceptions.InvalidEntityFieldException)1 InvalidFieldValueException (com.github.tfaga.lynx.exceptions.InvalidFieldValueException)1