Commit 1fa20213 authored by Matija Obreza's avatar Matija Obreza

Merge branch 'server-481-accession-filter-active-records-by-default-fix-not-filter' into 'master'

Fixed appearing of default values in the NOT filter

Closes genesys-ui#612

See merge request genesys-pgr/application-blocks!61
parents eb827573 ca1faeba
......@@ -17,11 +17,25 @@ package org.genesys.blocks.model.filters;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
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.genesys.blocks.model.BasicModel;
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.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.querydsl.core.types.ExpressionUtils;
import com.querydsl.core.types.Predicate;
import com.querydsl.core.types.dsl.CollectionPathBase;
......@@ -47,10 +60,12 @@ import com.querydsl.core.types.dsl.SimpleExpression;
public abstract class BasicModelFilter<T extends BasicModelFilter<T, R>, R extends BasicModel> {
private static final ObjectMapper jsonizer = new ObjectMapper();
private static final ObjectMapper nonDefault = new ObjectMapper();
static {
// Any objectMapper configuration goes here
jsonizer.setSerializationInclusion(Include.NON_NULL);
nonDefault.setSerializationInclusion(Include.NON_DEFAULT);
}
/** The id. */
......@@ -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. */
@JsonIgnoreProperties({ "NOT", "NULL", "NOTNULL" })
@JsonSerialize(using = NoDefaultValuesSerializer.class)
@JsonDeserialize(using = NonDefaultDeserializer.class)
public T NOT;
/** Names of properties to test with .isNull() */
......@@ -66,9 +83,8 @@ 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.
*
......@@ -85,7 +101,7 @@ public abstract class BasicModelFilter<T extends BasicModelFilter<T, R>, R exten
* @param basicModel the basic model
* @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<>();
if (CollectionUtils.isNotEmpty(id)) {
predicates.add(basicModel.id.in(id));
......@@ -168,7 +184,7 @@ public abstract class BasicModelFilter<T extends BasicModelFilter<T, R>, R exten
String paths[] = nullProp.split("\\.");
PathBuilder<?> pathBuilder = new PathBuilder<>(instance.getType(), instance.getMetadata());
Class<?> clazzToCheck = clazz;
for (String path: paths) {
for (String path : paths) {
Field field = clazzToCheck.getField(path);
if (CollectionPathBase.class.isAssignableFrom(field.getType())) {
pathBuilder.getSet(path, field.getDeclaringClass());
......@@ -191,7 +207,7 @@ public abstract class BasicModelFilter<T extends BasicModelFilter<T, R>, R exten
// will test only the first level of nesting
if (e instanceof NoSuchFieldException) {
Field fields[] = clazz.getDeclaredFields();
for (Field field: fields) {
for (Field field : fields) {
Class<?> superClazz = field.getType().getSuperclass();
if (superClazz != null && EntityPathBase.class.isAssignableFrom(superClazz)) {
try {
......@@ -267,7 +283,6 @@ public abstract class BasicModelFilter<T extends BasicModelFilter<T, R>, R exten
}
return NOTNULL;
}
/**
* 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
toClear.addAll(filter.NOTNULL);
}
for (String path: toClear) {
for (String path : toClear) {
try {
filter.clearFilter(path, false);
} catch (NoSuchFieldException | IllegalAccessException e) {
......@@ -296,4 +311,56 @@ public abstract class BasicModelFilter<T extends BasicModelFilter<T, R>, R exten
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