Commit 9fa2323c authored by Maxym Borodenko's avatar Maxym Borodenko

Handling for arrays

parent f5f1a31a
...@@ -20,6 +20,8 @@ import java.lang.reflect.Field; ...@@ -20,6 +20,8 @@ import java.lang.reflect.Field;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; 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 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;
...@@ -82,11 +84,25 @@ public abstract class BasicModelFilter<T extends BasicModelFilter<T, R>, R exten ...@@ -82,11 +84,25 @@ 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 -> 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()) { if (NOTNULL != null && !NOTNULL.isEmpty()) {
final Class<?> clazz = instance.getClass(); 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) { if (NOT != null) {
builder.and(NOT.buildPredicate().not()); builder.and(NOT.buildPredicate().not());
...@@ -139,7 +155,7 @@ public abstract class BasicModelFilter<T extends BasicModelFilter<T, R>, R exten ...@@ -139,7 +155,7 @@ public abstract class BasicModelFilter<T extends BasicModelFilter<T, R>, R exten
// return true; // 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 { try {
// build path for nesting filters // build path for nesting filters
if (nullProp.contains(".")) { if (nullProp.contains(".")) {
...@@ -148,7 +164,11 @@ public abstract class BasicModelFilter<T extends BasicModelFilter<T, R>, R exten ...@@ -148,7 +164,11 @@ public abstract class BasicModelFilter<T extends BasicModelFilter<T, R>, R exten
Class clazzToCheck = clazz; Class clazzToCheck = clazz;
for (String path: paths) { for (String path: paths) {
Field field = clazzToCheck.getField(path); Field field = clazzToCheck.getField(path);
pathBuilder = pathBuilder.get(path); if (CollectionPathBase.class.isAssignableFrom(field.getType())) {
pathBuilder.getSet(path, field.getDeclaringClass());
} else {
pathBuilder.getSimple(path, field.getDeclaringClass());
}
clazzToCheck = field.getType(); clazzToCheck = field.getType();
} }
return pathBuilder; return pathBuilder;
...@@ -169,10 +189,12 @@ public abstract class BasicModelFilter<T extends BasicModelFilter<T, R>, R exten ...@@ -169,10 +189,12 @@ public abstract class BasicModelFilter<T extends BasicModelFilter<T, R>, R exten
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.getType().getField(nullProp); Field result = field.getType().getField(nullProp);
PathBuilder<?> pathBuilder = new PathBuilder<>(instance.getType(), instance.getMetadata()); PathBuilder<?> pathBuilder = new PathBuilder<>(instance.getType(), instance.getMetadata());
return pathBuilder.get(field.getName()).get(nullProp); 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) { } catch (NoSuchFieldException e1) {
} }
......
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