Commit f20a864d authored by Matija Obreza's avatar Matija Obreza

Merge branch 'fix-for-453-server' into 'master'

Updated BasicModelFilter to fix issues with NULL and NOTNULL

See merge request genesys-pgr/application-blocks!50
parents 81d78e38 9fa2323c
......@@ -20,6 +20,9 @@ import java.lang.reflect.Field;
import java.util.HashSet;
import java.util.Set;
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;
......@@ -81,11 +84,25 @@ public abstract class BasicModelFilter<T extends BasicModelFilter<T, R>, R exten
}
if (NULL != null && !NULL.isEmpty()) {
final Class<?> clazz = instance.getClass();
NULL.forEach(nullProp -> builder.and(getProperty(instance, clazz, nullProp).isNull()));
NULL.forEach(nullProp -> {
DslExpression expression = getProperty(instance, clazz, nullProp);
if (expression instanceof SimpleExpression) {
builder.and(((SimpleExpression) expression).isNull());
} else if (expression instanceof CollectionPathBase) {
builder.and(((CollectionPathBase) expression).size().eq(0));
}
});
}
if (NOTNULL != null && !NOTNULL.isEmpty()) {
final Class<?> clazz = instance.getClass();
NOTNULL.forEach(notNullProp -> builder.and(getProperty(instance, clazz, notNullProp).isNotNull()));
NOTNULL.forEach(notNullProp -> {
DslExpression expression = getProperty(instance, clazz, notNullProp);
if (expression instanceof SimpleExpression) {
builder.and(((SimpleExpression) expression).isNotNull());
} else if (expression instanceof CollectionPathBase) {
builder.and(((CollectionPathBase) expression).size().gt(0));
}
});
}
if (NOT != null) {
builder.and(NOT.buildPredicate().not());
......@@ -145,15 +162,52 @@ public abstract class BasicModelFilter<T extends BasicModelFilter<T, R>, R exten
// return true;
// }
private SimpleExpression<?> getProperty(final EntityPathBase<R> instance, final Class<?> clazz, final String nullProp) {
private DslExpression<?> getProperty(final EntityPathBase<R> instance, final Class<?> clazz, final String nullProp) {
try {
final Field prop = clazz.getField(nullProp);
if (SimpleExpression.class.isAssignableFrom(prop.getType())) {
return (SimpleExpression<?>) prop.get(instance);
// build path for nesting filters
if (nullProp.contains(".")) {
String paths[] = nullProp.split("\\.");
PathBuilder<?> pathBuilder = new PathBuilder<>(instance.getType(), instance.getMetadata());
Class clazzToCheck = clazz;
for (String path: paths) {
Field field = clazzToCheck.getField(path);
if (CollectionPathBase.class.isAssignableFrom(field.getType())) {
pathBuilder.getSet(path, field.getDeclaringClass());
} else {
pathBuilder.getSimple(path, field.getDeclaringClass());
}
clazzToCheck = field.getType();
}
return pathBuilder;
} else {
throw new NoSuchFieldException("Property " + nullProp + " is not a SimpleExpression");
final Field prop = clazz.getField(nullProp);
if (SimpleExpression.class.isAssignableFrom(prop.getType())) {
return (SimpleExpression<?>) prop.get(instance);
} else {
throw new NoSuchFieldException("Property " + nullProp + " is not a SimpleExpression");
}
}
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
// test if nested objects have required property
// will test only the first level of nesting
if (e instanceof NoSuchFieldException) {
Field fields[] = clazz.getDeclaredFields();
for (Field field: fields) {
Class superClazz = field.getType().getSuperclass();
if (superClazz != null && EntityPathBase.class.isAssignableFrom(superClazz)) {
try {
Field result = field.getType().getField(nullProp);
PathBuilder<?> pathBuilder = new PathBuilder<>(instance.getType(), instance.getMetadata());
if (CollectionPathBase.class.isAssignableFrom(result.getType())) {
return pathBuilder.get(field.getName()).getSet(nullProp, result.getDeclaringClass());
}
return pathBuilder.get(field.getName()).getSimple(nullProp, result.getDeclaringClass());
} catch (NoSuchFieldException e1) {
}
}
}
}
throw new RuntimeException("Error accessing field " + nullProp + " for isNull() in " + instance.getClass());
}
}
......
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