Skip to content

feat: add equals criteria support for custom attributes types #133

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 5, 2019
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 @@ -16,6 +16,7 @@

package com.introproventures.graphql.jpa.query.schema.impl;

import java.lang.reflect.Constructor;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Collection;
Expand All @@ -31,6 +32,7 @@
import javax.persistence.criteria.Predicate;

import com.introproventures.graphql.jpa.query.schema.impl.PredicateFilter.Criteria;
import graphql.language.NullValue;

/**
* Supported types to build predicates for
Expand Down Expand Up @@ -413,9 +415,26 @@ else if(Collection.class.isAssignableFrom(type)) {
return from.join(filter.getField()).in(value);
} else if(type.isEnum()) {
return getEnumPredicate((Path<Enum<?>>) field, predicateFilter);
}
else if (filter.getCriterias().contains(PredicateFilter.Criteria.LOCATE)) {
return cb.gt(cb.locate(from.<String>get(filter.getField()), value.toString()), 0);
} // TODO need better detection mechanism
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)) {
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();
}

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

throw new IllegalArgumentException("Unsupported field type " + type + " for field " + predicateFilter.getField());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,12 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.introproventures.graphql.jpa.query.converter.model.JsonEntity;
import com.introproventures.graphql.jpa.query.converter.model.TaskVariableEntity;
import com.introproventures.graphql.jpa.query.converter.model.VariableValue;
import com.introproventures.graphql.jpa.query.schema.GraphQLExecutor;
import com.introproventures.graphql.jpa.query.schema.GraphQLSchemaBuilder;
import com.introproventures.graphql.jpa.query.schema.JavaScalars;
import com.introproventures.graphql.jpa.query.schema.JavaScalars.GraphQLObjectCoercing;
import com.introproventures.graphql.jpa.query.schema.impl.GraphQLJpaExecutor;
import com.introproventures.graphql.jpa.query.schema.impl.GraphQLJpaSchemaBuilder;
import graphql.schema.GraphQLScalarType;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -66,13 +64,9 @@ public GraphQLExecutor graphQLExecutor(final GraphQLSchemaBuilder graphQLSchemaB

@Bean
public GraphQLSchemaBuilder graphQLSchemaBuilder(final EntityManager entityManager) {

JavaScalars.register(JsonNode.class, new GraphQLScalarType("Json", "Json type", new GraphQLObjectCoercing()));
JavaScalars.register(VariableValue.class, new GraphQLScalarType("VariableValue", "VariableValue Type", new GraphQLObjectCoercing()));

return new GraphQLJpaSchemaBuilder(entityManager)
.name("HashMapSchema")
.description("Json Entity test schema");
.name("CustomAttributeConverterSchema")
.description("Custom Attribute Converter Schema");
}

}
Expand Down Expand Up @@ -121,6 +115,27 @@ public void criteriaTester() {
assertThat(result).isNotEmpty();
assertThat(result).hasSize(1);
}

@Test
@Transactional
public void criteriaTester2() {
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
CriteriaQuery<TaskVariableEntity> criteria = builder.createQuery(TaskVariableEntity.class);
Root<TaskVariableEntity> taskVariable = criteria.from(TaskVariableEntity.class);

Boolean object = new Boolean(true);

VariableValue<Boolean> variableValue = new VariableValue<>(object);
criteria.select(taskVariable)
.where(builder.equal(taskVariable.get("value"), variableValue));

// when:
List<?> result = entityManager.createQuery(criteria).getResultList();

// then:
assertThat(result).isNotEmpty();
assertThat(result).hasSize(1);
}

@Test // Problem with generating cast() in the where expression
@Transactional
Expand Down Expand Up @@ -312,5 +327,126 @@ public void queryProcessVariablesWhereSearchCriteria() {
// then
assertThat(result.toString()).isEqualTo(expected);
}

@Test
public void queryProcessVariablesWhereWithEQStringSearchCriteria() {
//given
String query = "query {" +
" TaskVariables(where: {"
+ "name: {EQ: \"variable1\"}"
+ "value: {EQ: \"data\"}"
+ "}) {" +
" select {" +
" name" +
" value" +
" }" +
" }" +
"}";

String expected = "{TaskVariables={select=[{name=variable1, value=data}]}}";

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

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

@Test
public void queryProcessVariablesWhereWithEQBooleanSearchCriteria() {
//given
String query = "query {" +
" TaskVariables(where: {"
+ "name: {EQ: \"variable2\"}"
+ "value: {EQ: true}"
+ "}) {" +
" select {" +
" name" +
" value" +
" }" +
" }" +
"}";

String expected = "{TaskVariables={select=[{name=variable2, value=true}]}}";

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

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

@Test
public void queryProcessVariablesWhereWithEQNullSearchCriteria() {
//given
String query = "query {" +
" TaskVariables(where: {"
+ "name: {EQ: \"variable3\"}"
+ "value: {EQ: null}"
+ "}) {" +
" select {" +
" name" +
" value" +
" }" +
" }" +
"}";

String expected = "{TaskVariables={select=[{name=variable3, value=null}]}}";

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

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

@Test
public void queryProcessVariablesWhereWithEQIntSearchCriteria() {
//given
String query = "query {" +
" TaskVariables(where: {"
+ "name: {EQ: \"variable6\"}"
+ "value: {EQ: 12345}"
+ "}) {" +
" select {" +
" name" +
" value" +
" }" +
" }" +
"}";

String expected = "{TaskVariables={select=[{name=variable6, value=12345}]}}";

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

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

@Test
public void queryProcessVariablesWhereWithEQDoubleSearchCriteria() {
//given
String query = "query {" +
" TaskVariables(where: {"
+ "name: {EQ: \"variable5\"}"
+ "value: {EQ: 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);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ insert into PROCESS_VARIABLE (create_time, execution_id, last_updated_time, name
insert into TASK_VARIABLE (create_time, execution_id, last_updated_time, name, process_instance_id, task_id, type, value) values
(CURRENT_TIMESTAMP, 'execution_id', CURRENT_TIMESTAMP, 'variable1', 0, '1', 'string', '{"value":"data"}'),
(CURRENT_TIMESTAMP, 'execution_id', CURRENT_TIMESTAMP, 'variable2', 0, '1', 'boolean', '{"value":true}'),
(CURRENT_TIMESTAMP, 'execution_id', CURRENT_TIMESTAMP, 'variable3', 0, '2', 'string', '{"value":null}'),
(CURRENT_TIMESTAMP, 'execution_id', CURRENT_TIMESTAMP, 'variable3', 0, '2', 'null', '{"value":null}'),
(CURRENT_TIMESTAMP, 'execution_id', CURRENT_TIMESTAMP, 'variable4', 0, '2', 'json', '{"value":{"key":"data"}}'),
(CURRENT_TIMESTAMP, 'execution_id', CURRENT_TIMESTAMP, 'variable5', 1, '4', 'double', '{"value":1.0}'),
(CURRENT_TIMESTAMP, 'execution_id', CURRENT_TIMESTAMP, 'variable6', 1, '4', 'json', '{"value":[1,2,3,4,5]}');
(CURRENT_TIMESTAMP, 'execution_id', CURRENT_TIMESTAMP, 'variable5', 1, '4', 'double', '{"value":1.2345}'),
(CURRENT_TIMESTAMP, 'execution_id', CURRENT_TIMESTAMP, 'variable6', 1, '4', 'int', '{"value":12345}'),
(CURRENT_TIMESTAMP, 'execution_id', CURRENT_TIMESTAMP, 'variable7', 1, '4', 'json', '{"value":[1,2,3,4,5]}');