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