Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
import java.lang.reflect.Constructor;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.EnumSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;

Expand Down Expand Up @@ -420,26 +422,66 @@ else if (Object.class.isAssignableFrom(type)) {
if (filter.getCriterias().contains(PredicateFilter.Criteria.LOCATE)) {
return cb.gt(cb.locate(from.<String>get(filter.getField()), value.toString()), 0);
}
else if (filter.getCriterias().contains(PredicateFilter.Criteria.EQ)) {
else {
Object object = value;

try {
Constructor<?> constructor = type.getConstructor(Object.class);
if(constructor != null) {
Object arg = NullValue.class.isInstance(value) ? null : value;
object = constructor.newInstance(arg);
}
} catch (Exception e) {
e.printStackTrace();
if(List.class.isInstance(value)) {
object = getValues(object, type);
} else {
object = getValue(object, type);
}

return cb.equal(from.get(filter.getField()), object);
}

if (filter.getCriterias().contains(PredicateFilter.Criteria.EQ)) {
return cb.equal(from.get(filter.getField()), object);
}
else if (filter.getCriterias().contains(PredicateFilter.Criteria.IN)
|| filter.getCriterias().contains(PredicateFilter.Criteria.NIN)
) {
CriteriaBuilder.In<Object> in = cb.in(from.get(filter.getField()));

if(List.class.isInstance(object)) {
List.class.cast(object)
.forEach(in::value);
} else {
in.value(object);
}

if(filter.getCriterias().contains(PredicateFilter.Criteria.NIN)) {
return cb.not(in);
}

return in;
}
}
}

throw new IllegalArgumentException("Unsupported field type " + type + " for field " + predicateFilter.getField());
}

private Object getValue(Object object, Class<?> type) {
try {
Constructor<?> constructor = type.getConstructor(Object.class);
if(constructor != null) {
Object arg = NullValue.class.isInstance(object) ? null : object;
return constructor.newInstance(arg);
}
} catch (Exception e) {
e.printStackTrace();
}

return object;
}

private Object getValues(Object object, Class<?> type) {
List<Object> objects = new ArrayList<>();

for (Object value : List.class.cast(object).toArray()) {
objects.add(getValue(value, type));
}

return objects;
}

/**
* Makes predicate for field of primitive type
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -449,4 +449,137 @@ public void queryProcessVariablesWhereWithEQDoubleSearchCriteria() {
}


@Test
public void queryProcessVariablesWhereWithINSingleValueSearchCriteria() {
//given
String query = "query {" +
" TaskVariables(where: {"
+ "value: {IN: 1.2345}"
+ "}) {" +
" select {" +
" name" +
" value" +
" }" +
" }" +
"}";

String expected = "{TaskVariables={select=[{name=variable5, value=1.2345}]}}";

//when
Object result = executor.execute(query).getData();

// then
assertThat(result.toString()).isEqualTo(expected);
}

@Test
public void queryProcessVariablesWhereWithINListValueSearchCriteria() {
//given
String query = "query {" +
" TaskVariables(where: {"
+ "value: {IN: [1.2345]}"
+ "}) {" +
" select {" +
" name" +
" value" +
" }" +
" }" +
"}";

String expected = "{TaskVariables={select=[{name=variable5, value=1.2345}]}}";

//when
Object result = executor.execute(query).getData();

// then
assertThat(result.toString()).isEqualTo(expected);
}

@Test
public void queryProcessVariablesWhereWithNINListValueSearchCriteria() {
//given
String query = "query {" +
" TaskVariables(where: {"
+ "value: {NIN: [null]}"
+ "}) {" +
" select {" +
" name" +
" value" +
" }" +
" }" +
"}";

String expected = "{TaskVariables={select=["
+ "{name=variable1, value=data}, "
+ "{name=variable2, value=true}, "
+ "{name=variable4, value={key=data}}, "
+ "{name=variable5, value=1.2345}, "
+ "{name=variable6, value=12345}, "
+ "{name=variable7, value=[1, 2, 3, 4, 5]}]}}";

//when
Object result = executor.execute(query).getData();

// then
assertThat(result.toString()).isEqualTo(expected);
}

@Test
public void queryProcessVariablesWhereWithNINSingleValueSearchCriteria() {
//given
String query = "query {" +
" TaskVariables(where: {"
+ "value: {NIN: null}"
+ "}) {" +
" select {" +
" name" +
" value" +
" }" +
" }" +
"}";

String expected = "{TaskVariables={select=["
+ "{name=variable1, value=data}, "
+ "{name=variable2, value=true}, "
+ "{name=variable4, value={key=data}}, "
+ "{name=variable5, value=1.2345}, "
+ "{name=variable6, value=12345}, "
+ "{name=variable7, value=[1, 2, 3, 4, 5]}]}}";

//when
Object result = executor.execute(query).getData();

// then
assertThat(result.toString()).isEqualTo(expected);
}

@Test
public void queryProcessVariablesWhereWithINListTypedValueSearchCriteria() {
//given
String query = "query {" +
" TaskVariables(where: {"
+ "value: {IN: [null, true, \"data\", 12345, 1.2345]}"
+ "}) {" +
" select {" +
" name" +
" value" +
" }" +
" }" +
"}";

String expected = "{TaskVariables={select=["
+ "{name=variable1, value=data}, "
+ "{name=variable2, value=true}, "
+ "{name=variable3, value=null}, "
+ "{name=variable5, value=1.2345}, "
+ "{name=variable6, value=12345}"
+ "]}}";

//when
Object result = executor.execute(query).getData();

// then
assertThat(result.toString()).isEqualTo(expected);
}

}