Commit df1f2a06 authored by Matija Obreza's avatar Matija Obreza

Enhanced filter handling

- Using UUIDDeserializer in UuidModelFilter
- Filters also check for null values
- Java warnings fixed
parent dcae9786
......@@ -22,10 +22,6 @@ import java.util.HashSet;
import java.util.List;
import java.util.Set;
import com.querydsl.core.types.ExpressionUtils;
import com.querydsl.core.types.dsl.CollectionPathBase;
import com.querydsl.core.types.dsl.DslExpression;
import com.querydsl.core.types.dsl.PathBuilder;
import org.apache.commons.collections4.CollectionUtils;
import org.genesys.blocks.model.BasicModel;
import org.genesys.blocks.model.QBasicModel;
......@@ -34,8 +30,12 @@ import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.querydsl.core.types.ExpressionUtils;
import com.querydsl.core.types.Predicate;
import com.querydsl.core.types.dsl.CollectionPathBase;
import com.querydsl.core.types.dsl.DslExpression;
import com.querydsl.core.types.dsl.EntityPathBase;
import com.querydsl.core.types.dsl.PathBuilder;
import com.querydsl.core.types.dsl.SimpleExpression;
/**
......@@ -93,22 +93,22 @@ public abstract class BasicModelFilter<T extends BasicModelFilter<T, R>, R exten
if (NULL != null && !NULL.isEmpty()) {
final Class<?> clazz = instance.getClass();
NULL.forEach(nullProp -> {
DslExpression expression = getProperty(instance, clazz, nullProp);
DslExpression<?> expression = getProperty(instance, clazz, nullProp);
if (expression instanceof SimpleExpression) {
predicates.add(((SimpleExpression) expression).isNull());
predicates.add(((SimpleExpression<?>) expression).isNull());
} else if (expression instanceof CollectionPathBase) {
predicates.add(((CollectionPathBase) expression).size().eq(0));
predicates.add(((CollectionPathBase<?, ?, ?>) expression).size().eq(0));
}
});
}
if (NOTNULL != null && !NOTNULL.isEmpty()) {
final Class<?> clazz = instance.getClass();
NOTNULL.forEach(notNullProp -> {
DslExpression expression = getProperty(instance, clazz, notNullProp);
DslExpression<?> expression = getProperty(instance, clazz, notNullProp);
if (expression instanceof SimpleExpression) {
predicates.add(((SimpleExpression) expression).isNotNull());
predicates.add(((SimpleExpression<?>) expression).isNotNull());
} else if (expression instanceof CollectionPathBase) {
predicates.add(((CollectionPathBase) expression).size().gt(0));
predicates.add(((CollectionPathBase<?, ?, ?>) expression).size().gt(0));
}
});
}
......@@ -183,7 +183,7 @@ public abstract class BasicModelFilter<T extends BasicModelFilter<T, R>, R exten
if (nullProp.contains(".")) {
String paths[] = nullProp.split("\\.");
PathBuilder<?> pathBuilder = new PathBuilder<>(instance.getType(), instance.getMetadata());
Class clazzToCheck = clazz;
Class<?> clazzToCheck = clazz;
for (String path: paths) {
Field field = clazzToCheck.getField(path);
if (CollectionPathBase.class.isAssignableFrom(field.getType())) {
......@@ -208,7 +208,7 @@ public abstract class BasicModelFilter<T extends BasicModelFilter<T, R>, R exten
if (e instanceof NoSuchFieldException) {
Field fields[] = clazz.getDeclaredFields();
for (Field field: fields) {
Class superClazz = field.getType().getSuperclass();
Class<?> superClazz = field.getType().getSuperclass();
if (superClazz != null && EntityPathBase.class.isAssignableFrom(superClazz)) {
try {
Field result = field.getType().getField(nullProp);
......
......@@ -59,18 +59,23 @@ public class DateFilter {
public Predicate buildQuery(final TemporalExpression<Date> date) {
final BooleanBuilder and = new BooleanBuilder();
if (ge != null) {
and.and(date.isNotNull());
and.and(date.goe(ge));
}
if (gt != null) {
and.and(date.isNotNull());
and.and(date.gt(gt));
}
if (le != null) {
and.and(date.isNotNull());
and.and(date.loe(le));
}
if (lt != null) {
and.and(date.isNotNull());
and.and(date.lt(lt));
}
if (between != null && between.length == 2) {
and.and(date.isNotNull());
and.and(date.between(between[0], between[1]));
}
return and;
......
......@@ -77,21 +77,27 @@ public class NumberFilter<T extends Number & Comparable<?>> {
public BooleanBuilder buildQuery(final NumberPath<T> val) {
final BooleanBuilder and = new BooleanBuilder();
if (eq != null && !eq.isEmpty()) {
and.and(val.isNotNull());
and.and(val.in(eq));
}
if (gt != null) {
and.and(val.isNotNull());
and.and(val.gt(gt));
}
if (ge != null) {
and.and(val.isNotNull());
and.and(val.goe(ge));
}
if (lt != null) {
and.and(val.isNotNull());
and.and(val.lt(lt));
}
if (le != null) {
and.and(val.isNotNull());
and.and(val.loe(le));
}
if (between != null && between.length == 2) {
and.and(val.isNotNull());
and.and(val.between(between[0], between[1]));
}
return and;
......
......@@ -41,12 +41,15 @@ public class StringFilter {
public BooleanBuilder buildQuery(final StringPath val) {
final BooleanBuilder and = new BooleanBuilder();
if (eq != null) {
and.and(val.isNotNull());
and.and(val.eq(eq));
}
if (sw != null) {
and.and(val.isNotNull());
and.and(val.startsWith(sw));
}
if (contains != null) {
and.and(val.isNotNull());
and.and(val.contains(contains));
}
return and;
......
......@@ -20,6 +20,8 @@ import java.util.List;
import java.util.Set;
import java.util.UUID;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.deser.std.UUIDDeserializer;
import com.querydsl.core.types.Predicate;
import org.apache.commons.collections4.CollectionUtils;
import org.genesys.blocks.model.AuditedVersionedModel;
......@@ -37,6 +39,7 @@ import com.querydsl.core.types.dsl.EntityPathBase;
public abstract class UuidModelFilter<T extends UuidModelFilter<T, R>, R extends AuditedVersionedModel> extends AuditedVersionedModelFilter<T, R> {
/** The created by. */
@JsonDeserialize(contentUsing = UUIDDeserializer.class)
public Set<UUID> uuid;
/**
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment