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