Commit f5f1a31a authored by Maxym Borodenko's avatar Maxym Borodenko

Updated BasicModelFilter to fix issues with NULL and NOTNULL

parent 6090c3d8
......@@ -20,6 +20,7 @@ import java.lang.reflect.Field;
import java.util.HashSet;
import java.util.Set;
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;
......@@ -140,13 +141,44 @@ public abstract class BasicModelFilter<T extends BasicModelFilter<T, R>, R exten
private SimpleExpression<?> getProperty(final EntityPathBase<R> instance, final Class<?> clazz, final String nullProp) {
try {
// 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);
pathBuilder = pathBuilder.get(path);
clazzToCheck = field.getType();
}
return pathBuilder;
} else {
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.getType().getField(nullProp);
PathBuilder<?> pathBuilder = new PathBuilder<>(instance.getType(), instance.getMetadata());
return pathBuilder.get(field.getName()).get(nullProp);
} 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