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());
}
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());
}
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());
}
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);
}
Aggregations