Commit 695991b5 authored by Viacheslav Pavlov's avatar Viacheslav Pavlov

Changed building of filter predicate to collecting them into list

fixed infinite loop
parent 4d6370f9
......@@ -16,13 +16,14 @@
package org.genesys.blocks.model.filters;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import com.querydsl.core.types.Predicate;
import org.apache.commons.collections4.CollectionUtils;
import org.genesys.blocks.model.AuditedVersionedModel;
import org.genesys.blocks.model.QAuditedVersionedModel;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.dsl.EntityPathBase;
/**
......@@ -46,27 +47,27 @@ public abstract class AuditedVersionedModelFilter<T extends AuditedVersionedMode
public DateFilter lastModifiedDate;
/**
* Builds the query.
* Collects list of filter predicates
*
* @param instance the instance
* @param instance the instance of Q-type of <em>R</em>
* @param auditedVersionedModel the audited versioned model
* @param builder the builder
* @return list of predicates
*/
protected void buildQuery(final EntityPathBase<R> instance, final QAuditedVersionedModel auditedVersionedModel, final BooleanBuilder builder) {
super.buildQuery(instance, auditedVersionedModel._super, builder);
protected List<Predicate> collectPredicates(final EntityPathBase<R> instance, final QAuditedVersionedModel auditedVersionedModel) {
List<Predicate> predicates = super.collectPredicates(instance, auditedVersionedModel._super);
if (CollectionUtils.isNotEmpty(createdBy)) {
builder.and(auditedVersionedModel.createdBy.in(createdBy));
predicates.add(auditedVersionedModel.createdBy.in(createdBy));
}
if (CollectionUtils.isNotEmpty(lastModifiedBy)) {
builder.and(auditedVersionedModel.lastModifiedBy.in(lastModifiedBy));
predicates.add(auditedVersionedModel.lastModifiedBy.in(lastModifiedBy));
}
if (createdDate != null) {
builder.and(createdDate.buildQuery(auditedVersionedModel.createdDate));
predicates.add(createdDate.buildQuery(auditedVersionedModel.createdDate));
}
if (lastModifiedDate != null) {
builder.and(lastModifiedDate.buildQuery(auditedVersionedModel.lastModifiedDate));
predicates.add(lastModifiedDate.buildQuery(auditedVersionedModel.lastModifiedDate));
}
return predicates;
}
/**
......
......@@ -17,9 +17,12 @@ package org.genesys.blocks.model.filters;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
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;
......@@ -31,7 +34,6 @@ 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.BooleanBuilder;
import com.querydsl.core.types.Predicate;
import com.querydsl.core.types.dsl.EntityPathBase;
import com.querydsl.core.types.dsl.SimpleExpression;
......@@ -64,32 +66,38 @@ public abstract class BasicModelFilter<T extends BasicModelFilter<T, R>, R exten
/** Names of properties to test with .isNotNull() */
public Set<String> NOTNULL;
public abstract List<Predicate> collectPredicates();
/**
* Builds the DSL predicate.
*
* @return the predicate
*/
public abstract Predicate buildPredicate();
public Predicate buildPredicate() {
return ExpressionUtils.allOf(collectPredicates());
}
/**
* Builds the query.
* Collects list of filter predicates
*
* @param instance the instance of Q-type of <em>R</em>
* @param basicModel the basic model
* @param builder the builder
* @return list of predicates
*/
protected void buildPredicate(final EntityPathBase<R> instance, final QBasicModel basicModel, final BooleanBuilder builder) {
protected List<Predicate> collectPredicates(final EntityPathBase<R> instance, final QBasicModel basicModel) {
List<Predicate> predicates = new ArrayList<>();
if (CollectionUtils.isNotEmpty(id)) {
builder.and(basicModel.id.in(id));
predicates.add(basicModel.id.in(id));
}
if (NULL != null && !NULL.isEmpty()) {
final Class<?> clazz = instance.getClass();
NULL.forEach(nullProp -> {
DslExpression expression = getProperty(instance, clazz, nullProp);
if (expression instanceof SimpleExpression) {
builder.and(((SimpleExpression) expression).isNull());
predicates.add(((SimpleExpression) expression).isNull());
} else if (expression instanceof CollectionPathBase) {
builder.and(((CollectionPathBase) expression).size().eq(0));
predicates.add(((CollectionPathBase) expression).size().eq(0));
}
});
}
......@@ -98,15 +106,16 @@ public abstract class BasicModelFilter<T extends BasicModelFilter<T, R>, R exten
NOTNULL.forEach(notNullProp -> {
DslExpression expression = getProperty(instance, clazz, notNullProp);
if (expression instanceof SimpleExpression) {
builder.and(((SimpleExpression) expression).isNotNull());
predicates.add(((SimpleExpression) expression).isNotNull());
} else if (expression instanceof CollectionPathBase) {
builder.and(((CollectionPathBase) expression).size().gt(0));
predicates.add(((CollectionPathBase) expression).size().gt(0));
}
});
}
if (NOT != null) {
builder.and(NOT.buildPredicate().not());
predicates.add(ExpressionUtils.anyOf(NOT.collectPredicates()).not());
}
return predicates;
}
public void clearFilter(String jsonPath) throws NoSuchFieldException, IllegalAccessException {
......
......@@ -16,15 +16,16 @@
package org.genesys.blocks.model.filters;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import com.querydsl.core.types.Predicate;
import org.apache.commons.collections4.CollectionUtils;
import org.genesys.blocks.model.AuditedVersionedModel;
import org.genesys.blocks.model.QUuidModel;
import org.genesys.blocks.model.UuidModel;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.dsl.EntityPathBase;
/**
......@@ -39,18 +40,18 @@ public abstract class UuidModelFilter<T extends UuidModelFilter<T, R>, R extends
public Set<UUID> uuid;
/**
* Builds the query.
* Collects list of filter predicates
*
* @param instance the instance
* @param instance the instance of Q-type of <em>R</em>
* @param uuidModel the uuid model
* @param builder the builder
* @return list of predicates
*/
protected void buildQuery(final EntityPathBase<R> instance, final QUuidModel uuidModel, final BooleanBuilder builder) {
super.buildQuery(instance, uuidModel._super, builder);
protected List<Predicate> collectPredicates(final EntityPathBase<R> instance, final QUuidModel uuidModel) {
List<Predicate> predicates = super.collectPredicates(instance, uuidModel._super);
if (CollectionUtils.isNotEmpty(uuid)) {
builder.and(uuidModel.uuid.in(uuid));
predicates.add(uuidModel.uuid.in(uuid));
}
return predicates;
}
/**
......
......@@ -16,13 +16,14 @@
package org.genesys.blocks.model.filters;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import com.querydsl.core.types.Predicate;
import org.apache.commons.collections4.CollectionUtils;
import org.genesys.blocks.model.QVersionedModel;
import org.genesys.blocks.model.VersionedModel;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.dsl.EntityPathBase;
/**
......@@ -40,20 +41,21 @@ public abstract class VersionedModelFilter<T extends VersionedModelFilter<T, R>,
public Boolean active;
/**
* Builds the query.
* Collects list of filter predicates
*
* @param instance the instance
* @param instance the instance of Q-type of <em>R</em>
* @param versionedModel the versioned model
* @param builder the builder
* @return list of predicates
*/
protected void buildQuery(final EntityPathBase<R> instance, final QVersionedModel versionedModel, final BooleanBuilder builder) {
super.buildPredicate(instance, versionedModel._super, builder);
protected List<Predicate> collectPredicates(final EntityPathBase<R> instance, QVersionedModel versionedModel) {
List<Predicate> predicates = super.collectPredicates(instance, versionedModel._super);
if (CollectionUtils.isNotEmpty(version)) {
builder.and(versionedModel.version.in(version));
predicates.add(versionedModel.version.in(version));
}
if (active != null) {
builder.and(versionedModel.active.eq(active));
predicates.add(versionedModel.active.eq(active));
}
return predicates;
}
/**
......
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