Commit 4456892d authored by Matija Obreza's avatar Matija Obreza

Added support for IS_NULL, IS_NOT_NULL and NOT filtering

parent c72e9c4d
/*
* Copyright 2017 Global Crop Diversity Trust
* Copyright 2018 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -18,6 +18,7 @@ package org.genesys.blocks.model.filters;
import java.util.Set;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.dsl.EntityPathBase;
import org.apache.commons.collections4.CollectionUtils;
import org.genesys.blocks.model.AuditedVersionedModel;
......@@ -26,7 +27,7 @@ import org.genesys.blocks.model.QAuditedVersionedModel;
/**
* {@link AuditedVersionedModel} match by sample filters.
*/
public abstract class AuditedVersionedModelFilter extends VersionedModelFilter {
public abstract class AuditedVersionedModelFilter<T extends AuditedVersionedModelFilter<T, R>, R extends AuditedVersionedModel> extends VersionedModelFilter<T, R> {
/** The created by. */
public Set<Long> createdBy;
......@@ -43,11 +44,12 @@ public abstract class AuditedVersionedModelFilter extends VersionedModelFilter {
/**
* Builds the query.
*
* @param instance the instance
* @param auditedVersionedModel the audited versioned model
* @param builder the builder
*/
protected void buildQuery(final QAuditedVersionedModel auditedVersionedModel, final BooleanBuilder builder) {
super.buildQuery(auditedVersionedModel._super, builder);
protected void buildQuery(final EntityPathBase<R> instance, final QAuditedVersionedModel auditedVersionedModel, final BooleanBuilder builder) {
super.buildQuery(instance, auditedVersionedModel._super, builder);
if (CollectionUtils.isNotEmpty(createdBy)) {
builder.and(auditedVersionedModel.createdBy.in(createdBy));
......
......@@ -15,22 +15,26 @@
*/
package org.genesys.blocks.model.filters;
import java.lang.reflect.Field;
import java.util.Set;
import org.apache.commons.collections4.CollectionUtils;
import org.genesys.blocks.model.BasicModel;
import org.genesys.blocks.model.QBasicModel;
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;
/**
* {@link BasicModel} match by sample filters.
*/
public abstract class BasicModelFilter {
public abstract class BasicModelFilter<T extends BasicModelFilter<T, R>, R extends BasicModel> {
private static final ObjectMapper jsonizer = new ObjectMapper();
......@@ -42,6 +46,16 @@ public abstract class BasicModelFilter {
/** The id. */
public Set<Long> id;
/** The negative filters, but don't de-/serialize it's own NOT-properties */
@JsonIgnoreProperties({ "NOT", "NULL", "NOTNULL" })
public T NOT;
/** Names of properties to test with .isNull() */
public Set<String> NULL;
/** Names of properties to test with .isNotNull() */
public Set<String> NOTNULL;
/**
* Builds the DSL query.
*
......@@ -52,13 +66,25 @@ public abstract class BasicModelFilter {
/**
* Builds the query.
*
* @param instance the instance of Q-type of <R>
* @param basicModel the basic model
* @param builder the builder
*/
protected void buildQuery(final QBasicModel basicModel, final BooleanBuilder builder) {
protected void buildQuery(final EntityPathBase<R> instance, final QBasicModel basicModel, final BooleanBuilder builder) {
if (CollectionUtils.isNotEmpty(id)) {
builder.and(basicModel.id.in(id));
}
if (NULL != null && !NULL.isEmpty()) {
Class<?> clazz = instance.getClass();
NULL.forEach(nullProp -> builder.and(getProperty(instance, clazz, nullProp).isNull()));
}
if (NOTNULL != null && !NOTNULL.isEmpty()) {
Class<?> clazz = instance.getClass();
NOTNULL.forEach(notNullProp -> builder.and(getProperty(instance, clazz, notNullProp).isNotNull()));
}
if (NOT != null) {
builder.and(NOT.buildQuery().not());
}
}
// @Override
......@@ -85,6 +111,19 @@ public abstract class BasicModelFilter {
// return true;
// }
private SimpleExpression<?> getProperty(final EntityPathBase<R> instance, final Class<?> clazz, final String nullProp) {
try {
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) {
throw new RuntimeException("Error accessing field " + nullProp + " for isNull() in " + instance.getClass());
}
}
@Override
public String toString() {
try {
......
......@@ -19,15 +19,17 @@ import java.util.Set;
import java.util.UUID;
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;
/**
* {@link UuidModel} match by sample filters.
*/
public abstract class UuidModelFilter extends AuditedVersionedModelFilter {
public abstract class UuidModelFilter<T extends UuidModelFilter<T, R>, R extends AuditedVersionedModel> extends AuditedVersionedModelFilter<T, R> {
/** The created by. */
public Set<UUID> uuid;
......@@ -35,11 +37,12 @@ public abstract class UuidModelFilter extends AuditedVersionedModelFilter {
/**
* Builds the query.
*
* @param instance the instance
* @param uuidModel the uuid model
* @param builder the builder
*/
protected void buildQuery(final QUuidModel uuidModel, final BooleanBuilder builder) {
super.buildQuery(uuidModel._super, builder);
protected void buildQuery(final EntityPathBase<R> instance, final QUuidModel uuidModel, final BooleanBuilder builder) {
super.buildQuery(instance, uuidModel._super, builder);
if (CollectionUtils.isNotEmpty(uuid)) {
builder.and(uuidModel.uuid.in(uuid));
......
/*
* Copyright 2017 Global Crop Diversity Trust
* Copyright 2018 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -18,6 +18,7 @@ package org.genesys.blocks.model.filters;
import java.util.Set;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.dsl.EntityPathBase;
import org.apache.commons.collections4.CollectionUtils;
import org.genesys.blocks.model.QVersionedModel;
......@@ -26,7 +27,7 @@ import org.genesys.blocks.model.VersionedModel;
/**
* {@link VersionedModel} match by sample filters.
*/
public abstract class VersionedModelFilter extends BasicModelFilter {
public abstract class VersionedModelFilter<T extends VersionedModelFilter<T, R>, R extends VersionedModel> extends BasicModelFilter<T, R> {
/** The version. */
public Set<Integer> version;
......@@ -37,11 +38,12 @@ public abstract class VersionedModelFilter extends BasicModelFilter {
/**
* Builds the query.
*
* @param instance the instance
* @param versionedModel the versioned model
* @param builder the builder
*/
protected void buildQuery(final QVersionedModel versionedModel, final BooleanBuilder builder) {
super.buildQuery(versionedModel._super, builder);
protected void buildQuery(final EntityPathBase<R> instance, final QVersionedModel versionedModel, final BooleanBuilder builder) {
super.buildQuery(instance, versionedModel._super, builder);
if (CollectionUtils.isNotEmpty(version)) {
builder.and(versionedModel.version.in(version));
}
......
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