Commit ca1faeba authored by Viacheslav Pavlov's avatar Viacheslav Pavlov Committed by Matija Obreza

Fixed appearing of default values in the NOT filter

parent eb827573
...@@ -17,11 +17,25 @@ package org.genesys.blocks.model.filters; ...@@ -17,11 +17,25 @@ package org.genesys.blocks.model.filters;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.BeanProperty;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.deser.ContextualDeserializer;
import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.CollectionUtils;
import org.genesys.blocks.model.BasicModel; import org.genesys.blocks.model.BasicModel;
import org.genesys.blocks.model.QBasicModel; import org.genesys.blocks.model.QBasicModel;
...@@ -29,7 +43,6 @@ import org.genesys.blocks.model.QBasicModel; ...@@ -29,7 +43,6 @@ import org.genesys.blocks.model.QBasicModel;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude.Include; import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.querydsl.core.types.ExpressionUtils; import com.querydsl.core.types.ExpressionUtils;
import com.querydsl.core.types.Predicate; import com.querydsl.core.types.Predicate;
import com.querydsl.core.types.dsl.CollectionPathBase; import com.querydsl.core.types.dsl.CollectionPathBase;
...@@ -47,10 +60,12 @@ import com.querydsl.core.types.dsl.SimpleExpression; ...@@ -47,10 +60,12 @@ import com.querydsl.core.types.dsl.SimpleExpression;
public abstract class BasicModelFilter<T extends BasicModelFilter<T, R>, R extends BasicModel> { public abstract class BasicModelFilter<T extends BasicModelFilter<T, R>, R extends BasicModel> {
private static final ObjectMapper jsonizer = new ObjectMapper(); private static final ObjectMapper jsonizer = new ObjectMapper();
private static final ObjectMapper nonDefault = new ObjectMapper();
static { static {
// Any objectMapper configuration goes here // Any objectMapper configuration goes here
jsonizer.setSerializationInclusion(Include.NON_NULL); jsonizer.setSerializationInclusion(Include.NON_NULL);
nonDefault.setSerializationInclusion(Include.NON_DEFAULT);
} }
/** The id. */ /** The id. */
...@@ -58,6 +73,8 @@ public abstract class BasicModelFilter<T extends BasicModelFilter<T, R>, R exten ...@@ -58,6 +73,8 @@ public abstract class BasicModelFilter<T extends BasicModelFilter<T, R>, R exten
/** The negative filters, but don't de-/serialize it's own NOT-properties. */ /** The negative filters, but don't de-/serialize it's own NOT-properties. */
@JsonIgnoreProperties({ "NOT", "NULL", "NOTNULL" }) @JsonIgnoreProperties({ "NOT", "NULL", "NOTNULL" })
@JsonSerialize(using = NoDefaultValuesSerializer.class)
@JsonDeserialize(using = NonDefaultDeserializer.class)
public T NOT; public T NOT;
/** Names of properties to test with .isNull() */ /** Names of properties to test with .isNull() */
...@@ -66,9 +83,8 @@ public abstract class BasicModelFilter<T extends BasicModelFilter<T, R>, R exten ...@@ -66,9 +83,8 @@ public abstract class BasicModelFilter<T extends BasicModelFilter<T, R>, R exten
/** Names of properties to test with .isNotNull() */ /** Names of properties to test with .isNotNull() */
public Set<String> NOTNULL; public Set<String> NOTNULL;
public abstract List<Predicate> collectPredicates(); public abstract List<Predicate> collectPredicates();
/** /**
* Builds the DSL predicate. * Builds the DSL predicate.
* *
...@@ -85,7 +101,7 @@ public abstract class BasicModelFilter<T extends BasicModelFilter<T, R>, R exten ...@@ -85,7 +101,7 @@ public abstract class BasicModelFilter<T extends BasicModelFilter<T, R>, R exten
* @param basicModel the basic model * @param basicModel the basic model
* @return list of predicates * @return list of predicates
*/ */
protected List<Predicate> collectPredicates(final EntityPathBase<R> instance, final QBasicModel basicModel) { protected List<Predicate> collectPredicates(final EntityPathBase<R> instance, final QBasicModel basicModel) {
List<Predicate> predicates = new ArrayList<>(); List<Predicate> predicates = new ArrayList<>();
if (CollectionUtils.isNotEmpty(id)) { if (CollectionUtils.isNotEmpty(id)) {
predicates.add(basicModel.id.in(id)); predicates.add(basicModel.id.in(id));
...@@ -168,7 +184,7 @@ public abstract class BasicModelFilter<T extends BasicModelFilter<T, R>, R exten ...@@ -168,7 +184,7 @@ public abstract class BasicModelFilter<T extends BasicModelFilter<T, R>, R exten
String paths[] = nullProp.split("\\."); String paths[] = nullProp.split("\\.");
PathBuilder<?> pathBuilder = new PathBuilder<>(instance.getType(), instance.getMetadata()); PathBuilder<?> pathBuilder = new PathBuilder<>(instance.getType(), instance.getMetadata());
Class<?> clazzToCheck = clazz; Class<?> clazzToCheck = clazz;
for (String path: paths) { for (String path : paths) {
Field field = clazzToCheck.getField(path); Field field = clazzToCheck.getField(path);
if (CollectionPathBase.class.isAssignableFrom(field.getType())) { if (CollectionPathBase.class.isAssignableFrom(field.getType())) {
pathBuilder.getSet(path, field.getDeclaringClass()); pathBuilder.getSet(path, field.getDeclaringClass());
...@@ -191,7 +207,7 @@ public abstract class BasicModelFilter<T extends BasicModelFilter<T, R>, R exten ...@@ -191,7 +207,7 @@ public abstract class BasicModelFilter<T extends BasicModelFilter<T, R>, R exten
// will test only the first level of nesting // will test only the first level of nesting
if (e instanceof NoSuchFieldException) { if (e instanceof NoSuchFieldException) {
Field fields[] = clazz.getDeclaredFields(); Field fields[] = clazz.getDeclaredFields();
for (Field field: fields) { for (Field field : fields) {
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 {
...@@ -267,7 +283,6 @@ public abstract class BasicModelFilter<T extends BasicModelFilter<T, R>, R exten ...@@ -267,7 +283,6 @@ public abstract class BasicModelFilter<T extends BasicModelFilter<T, R>, R exten
} }
return NOTNULL; return NOTNULL;
} }
/** /**
* Prepare filter for use. NULL and NOTNULLs will clear any actual values * Prepare filter for use. NULL and NOTNULLs will clear any actual values
...@@ -286,7 +301,7 @@ public abstract class BasicModelFilter<T extends BasicModelFilter<T, R>, R exten ...@@ -286,7 +301,7 @@ public abstract class BasicModelFilter<T extends BasicModelFilter<T, R>, R exten
toClear.addAll(filter.NOTNULL); toClear.addAll(filter.NOTNULL);
} }
for (String path: toClear) { for (String path : toClear) {
try { try {
filter.clearFilter(path, false); filter.clearFilter(path, false);
} catch (NoSuchFieldException | IllegalAccessException e) { } catch (NoSuchFieldException | IllegalAccessException e) {
...@@ -296,4 +311,56 @@ public abstract class BasicModelFilter<T extends BasicModelFilter<T, R>, R exten ...@@ -296,4 +311,56 @@ public abstract class BasicModelFilter<T extends BasicModelFilter<T, R>, R exten
return filter; return filter;
} }
/**
* Used to deserialize NOT filter without default values
*
* @param <Y> type of filter
*/
static class NonDefaultDeserializer<Y extends BasicModelFilter<?, ?>> extends JsonDeserializer<Y> implements ContextualDeserializer {
private Class<Y> targetClass;
@Override
public Y deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
Y parsed = (Y) p.getCodec().readValue(p, targetClass);
Y defaultFilter = null;
try {
defaultFilter = targetClass.newInstance();
for (Field f : targetClass.getDeclaredFields()) {
if (Modifier.isPublic(f.getModifiers()) && f.get(defaultFilter) != null && f.get(defaultFilter).equals(f.get(parsed))) {
f.set(parsed, null);
}
}
} catch (InstantiationException | IllegalAccessException e) {
throw new RuntimeException("Parsing of filter failed, e: " + e.getMessage());
}
return parsed;
}
@Override
public JsonDeserializer<?> createContextual(DeserializationContext deserializationContext, BeanProperty beanProperty) throws JsonMappingException {
final JavaType type;
if (beanProperty != null)
type = beanProperty.getType();
else {
type = deserializationContext.getContextualType();
}
this.targetClass = (Class<Y>) type.getRawClass();
return this;
}
}
/**
* Used to serialize and ignore default values of NOT filter
*
* @param <Y> type of filter
*/
static class NoDefaultValuesSerializer<Y extends BasicModelFilter<?, ?>> extends JsonSerializer<Y> {
@Override
public void serialize(Y value, JsonGenerator gen, SerializerProvider provider) throws IOException {
gen.writeRawValue(BasicModelFilter.nonDefault.writeValueAsString(value));
}
}
} }
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