From 5b3db43248ad0fa49080bf6add0dceb84761d109 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Tue, 19 Oct 2021 22:18:41 +0200 Subject: [PATCH] Improving code --- .../query/json/base/JsonQueryERElement.java | 101 ++++-------------- .../json/base/QueryConditionalOperator.java | 14 +-- .../query/json/base/QueryLogicalOperator.java | 6 +- .../json/base/entities/JsonQueryEntity.java | 18 ++++ .../json/base/entities/JsonQueryFacet.java | 6 +- .../json/base/entities/JsonQueryResource.java | 7 +- .../base/relations/JsonQueryConsistsOf.java | 2 +- .../base/relations/JsonQueryIsRelatedTo.java | 3 +- 8 files changed, 59 insertions(+), 98 deletions(-) create mode 100644 src/main/java/org/gcube/informationsystem/resourceregistry/query/json/base/entities/JsonQueryEntity.java diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/query/json/base/JsonQueryERElement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/query/json/base/JsonQueryERElement.java index d6d3200..bebf110 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/query/json/base/JsonQueryERElement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/query/json/base/JsonQueryERElement.java @@ -6,16 +6,14 @@ import java.util.Set; import org.gcube.com.fasterxml.jackson.databind.JsonNode; import org.gcube.com.fasterxml.jackson.databind.ObjectMapper; +import org.gcube.com.fasterxml.jackson.databind.node.ObjectNode; import org.gcube.informationsystem.base.reference.AccessType; import org.gcube.informationsystem.base.reference.Direction; import org.gcube.informationsystem.base.reference.Element; -import org.gcube.informationsystem.model.reference.entities.Resource; -import org.gcube.informationsystem.model.reference.relations.Relation; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; import org.gcube.informationsystem.resourceregistry.api.exceptions.query.InvalidQueryException; import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaException; import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaNotFoundException; -import org.gcube.informationsystem.resourceregistry.instances.base.ElementManagement; import org.gcube.informationsystem.resourceregistry.types.TypesCache; public abstract class JsonQueryERElement { @@ -76,101 +74,44 @@ public abstract class JsonQueryERElement { public abstract StringBuffer analize(StringBuffer stringBuffer) throws SchemaNotFoundException, InvalidQueryException, SchemaException, ResourceRegistryException; - protected StringBuffer addWhereConstraint(JsonNode jsonNode, StringBuffer stringBuffer, String fieldNamePrefix) throws InvalidQueryException { - Iterator iterator = jsonNode.fieldNames(); + protected StringBuffer addConstraint(JsonNode jsonNode, QueryLogicalOperator queryLogicalOperator, String fieldNamePrefix) throws InvalidQueryException { + StringBuffer stringBuffer = new StringBuffer(); - // ObjectNode objectNode = jsonNode.deepCopy(); - //objectNode.remove(fieldNames); + ObjectNode objectNode = jsonNode.deepCopy(); + objectNode.remove(fieldNamesToRemove); + + Iterator iterator = objectNode.fieldNames(); + + if(queryLogicalOperator==null) { + queryLogicalOperator = QueryLogicalOperator.AND; + } boolean first = true; while(iterator.hasNext()) { String fieldName = iterator.next(); - if(fieldName.compareTo(Element.CLASS_PROPERTY)==0) { - continue; - } - - if(fieldName.compareTo(Relation.TARGET_PROPERTY)==0) { - continue; - } - - if(fieldName.compareTo(Relation.SOURCE_PROPERTY)==0) { - continue; - } - - if(fieldName.compareTo(Resource.CONSISTS_OF_PROPERTY)==0) { - continue; - } - - if(fieldName.compareTo(Resource.IS_RELATED_TO_PROPERTY)==0) { - continue; - } - - if(fieldName.startsWith(ElementManagement.UNDERSCORE)) { - continue; - } - if(first) { first = false; }else { - stringBuffer.append(" AND "); + stringBuffer.append(queryLogicalOperator.getLogicalOperator()); } -// if(QueryLogicalOperator.getOperators().contains(fieldName)) { -// QueryLogicalOperator queryLogicalOperator = QueryLogicalOperator.getQueryLogicalOperator(fieldName); -// stringBuffer.append(queryLogicalOperator.getLogicalOperator()); -// } - - stringBuffer.append(getNameValueToMatch(jsonNode, fieldName, fieldNamePrefix)); + if(QueryLogicalOperator.getOperators().contains(fieldName)) { + QueryLogicalOperator innnerQueryLogicalOperator = QueryLogicalOperator.getQueryLogicalOperator(fieldName); + JsonNode node = objectNode.get(fieldName); + stringBuffer.append(addConstraint(node, innnerQueryLogicalOperator, fieldNamePrefix)); + }else { + stringBuffer.append(getNameValueToMatch(objectNode, fieldName, fieldNamePrefix)); + } } return stringBuffer; } - private StringBuffer getExpression(String fieldName, JsonNode jsonNode, String fieldNamePrefix) throws InvalidQueryException { - StringBuffer stringBuffer = new StringBuffer(); - - - JsonNode gotJoJsonNode = jsonNode.get(fieldName); - - if(gotJoJsonNode.isContainerNode()) { - - if(gotJoJsonNode.isArray()) { - throw new InvalidQueryException("Array not supported for " + fieldName); - } - - if(gotJoJsonNode.isObject()) { - - for(QueryConditionalOperator queryComparisonOperator : QueryConditionalOperator.values()) { - if(gotJoJsonNode.has(queryComparisonOperator.operator)) { - // TODO - } - } - - StringBuffer newPrefix = new StringBuffer(); - if(fieldNamePrefix!=null) { - newPrefix.append(fieldNamePrefix); - newPrefix.append("."); - } - newPrefix.append(fieldName); - return addWhereConstraint(gotJoJsonNode, stringBuffer, newPrefix.toString()); - } - - } - - addNameToCompare(stringBuffer, fieldName, fieldNamePrefix); - String value = jsonNode.get(fieldName).asText(); - addValueToMatch(stringBuffer, gotJoJsonNode, value); - - return stringBuffer; - } - - private StringBuffer getNameValueToMatch(JsonNode jsonNode, String fieldName, String fieldNamePrefix) throws InvalidQueryException { StringBuffer stringBuffer = new StringBuffer(); - JsonNode gotJoJsonNode = jsonNode.get(fieldName); if(gotJoJsonNode.isContainerNode()) { @@ -193,7 +134,9 @@ public abstract class JsonQueryERElement { newPrefix.append("."); } newPrefix.append(fieldName); - return addWhereConstraint(gotJoJsonNode, stringBuffer, newPrefix.toString()); + + stringBuffer.append(addConstraint(gotJoJsonNode, null, newPrefix.toString())); + return stringBuffer; } } diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/query/json/base/QueryConditionalOperator.java b/src/main/java/org/gcube/informationsystem/resourceregistry/query/json/base/QueryConditionalOperator.java index 6854216..7a24b81 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/query/json/base/QueryConditionalOperator.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/query/json/base/QueryConditionalOperator.java @@ -10,14 +10,14 @@ import java.util.Set; */ public enum QueryConditionalOperator { - $EQ("$eq", " = ", "Matches values that are equal to a specified value."), - $GT("$gt", " > ", "Matches values that are greater than a specified value."), - $GTE("$gte", " >= ", "Matches values that are greater than or equal to a specified value."), - $LT("$lt", " < ", "Matches values that are less than a specified value."), - $LTE("$lte", " <= ", "Matches values that are less than or equal to a specified value."), - $NE("$ne", " <> ", "Matches all values that are not equal to a specified value."), + EQ("$eq", " = ", "Matches values that are equal to a specified value."), + GT("$gt", " > ", "Matches values that are greater than a specified value."), + GTE("$gte", " >= ", "Matches values that are greater than or equal to a specified value."), + LT("$lt", " < ", "Matches values that are less than a specified value."), + LTE("$lte", " <= ", "Matches values that are less than or equal to a specified value."), + NE("$ne", " <> ", "Matches all values that are not equal to a specified value."), - $IN("$in", " IN ", "Matches any of the values specified in an array."); + IN("$in", " IN ", "Matches any of the values specified in an array."); protected final String operator; protected final String conditionalOperator; diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/query/json/base/QueryLogicalOperator.java b/src/main/java/org/gcube/informationsystem/resourceregistry/query/json/base/QueryLogicalOperator.java index af58878..19f19bd 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/query/json/base/QueryLogicalOperator.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/query/json/base/QueryLogicalOperator.java @@ -10,9 +10,9 @@ import java.util.Set; */ public enum QueryLogicalOperator { - $AND("$and", " AND ", "true if both the conditions are true"), - $OR("$or", " OR ", "true if at least one of the condition is true"), - $NOT("$not", " NOT ", "true if the condition is false."); + AND("$and", " AND ", "true if both the conditions are true"), + OR("$or", " OR ", "true if at least one of the condition is true"), + NOT("$not", " NOT ", "true if the condition is false."); protected final String operator; protected final String logicalOperator; diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/query/json/base/entities/JsonQueryEntity.java b/src/main/java/org/gcube/informationsystem/resourceregistry/query/json/base/entities/JsonQueryEntity.java new file mode 100644 index 0000000..be03b8c --- /dev/null +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/query/json/base/entities/JsonQueryEntity.java @@ -0,0 +1,18 @@ +package org.gcube.informationsystem.resourceregistry.query.json.base.entities; + +import org.gcube.com.fasterxml.jackson.databind.JsonNode; +import org.gcube.informationsystem.base.reference.AccessType; +import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; +import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaException; +import org.gcube.informationsystem.resourceregistry.query.json.base.JsonQueryERElement; + +/** + * @author Luca Frosini (ISTI - CNR) + */ +public abstract class JsonQueryEntity extends JsonQueryERElement { + + public JsonQueryEntity(JsonNode jsonQuery, AccessType accessType) throws SchemaException, ResourceRegistryException { + super(jsonQuery, accessType); + } + +} diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/query/json/base/entities/JsonQueryFacet.java b/src/main/java/org/gcube/informationsystem/resourceregistry/query/json/base/entities/JsonQueryFacet.java index 1966edc..3aaa552 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/query/json/base/entities/JsonQueryFacet.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/query/json/base/entities/JsonQueryFacet.java @@ -6,18 +6,18 @@ import org.gcube.informationsystem.base.reference.Direction; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; import org.gcube.informationsystem.resourceregistry.api.exceptions.query.InvalidQueryException; import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaException; -import org.gcube.informationsystem.resourceregistry.query.json.base.JsonQueryERElement; import org.gcube.informationsystem.resourceregistry.query.json.base.relations.JsonQueryConsistsOf; /** * @author Luca Frosini (ISTI - CNR) */ -public class JsonQueryFacet extends JsonQueryERElement{ +public class JsonQueryFacet extends JsonQueryEntity { public final static String _IN = "_in"; public JsonQueryFacet(JsonNode jsonQuery) throws SchemaException, ResourceRegistryException { super(jsonQuery, AccessType.FACET); + fieldNamesToRemove.add(JsonQueryFacet._IN); } @Override @@ -57,7 +57,7 @@ public class JsonQueryFacet extends JsonQueryERElement{ // Size 1 means that only '@class' property is present if(size > 1) { newBuffer.append(" WHERE "); - addWhereConstraint(jsonNode, newBuffer, null); + newBuffer.append(addConstraint(jsonNode, null, null)); } return newBuffer; diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/query/json/base/entities/JsonQueryResource.java b/src/main/java/org/gcube/informationsystem/resourceregistry/query/json/base/entities/JsonQueryResource.java index df09d15..b310855 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/query/json/base/entities/JsonQueryResource.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/query/json/base/entities/JsonQueryResource.java @@ -8,17 +8,18 @@ import org.gcube.informationsystem.base.reference.IdentifiableElement; import org.gcube.informationsystem.model.reference.entities.Resource; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaException; -import org.gcube.informationsystem.resourceregistry.query.json.base.JsonQueryERElement; import org.gcube.informationsystem.resourceregistry.query.json.base.relations.JsonQueryConsistsOf; import org.gcube.informationsystem.resourceregistry.query.json.base.relations.JsonQueryIsRelatedTo; /** * @author Luca Frosini (ISTI - CNR) */ -public class JsonQueryResource extends JsonQueryERElement { +public class JsonQueryResource extends JsonQueryEntity { public JsonQueryResource(JsonNode jsonQuery) throws SchemaException, ResourceRegistryException { super(jsonQuery, AccessType.RESOURCE); + fieldNamesToRemove.add(Resource.CONSISTS_OF_PROPERTY); + fieldNamesToRemove.add(Resource.IS_RELATED_TO_PROPERTY); } @Override @@ -77,7 +78,7 @@ public class JsonQueryResource extends JsonQueryERElement { newBuffer.append("SELECT FROM ( "); newBuffer.append(stringBuffer); newBuffer.append(") WHERE "); - addWhereConstraint(jsonNode, newBuffer, null); + newBuffer.append(addConstraint(jsonNode, null, null)); stringBuffer = newBuffer; } diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/query/json/base/relations/JsonQueryConsistsOf.java b/src/main/java/org/gcube/informationsystem/resourceregistry/query/json/base/relations/JsonQueryConsistsOf.java index 3758568..210fea2 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/query/json/base/relations/JsonQueryConsistsOf.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/query/json/base/relations/JsonQueryConsistsOf.java @@ -82,7 +82,7 @@ public class JsonQueryConsistsOf extends JsonQueryRelation { // Size 2 means that only '@class' and 'target' properties are present if(size > 2) { consistsOfBuffer.append(") WHERE "); // Close ) SELECT - addWhereConstraint(jsonNode, consistsOfBuffer, null); + consistsOfBuffer.append(addConstraint(jsonNode, null, null)); } if(!jsonNode.has(ConsistsOf.SOURCE_PROPERTY)) { diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/query/json/base/relations/JsonQueryIsRelatedTo.java b/src/main/java/org/gcube/informationsystem/resourceregistry/query/json/base/relations/JsonQueryIsRelatedTo.java index 30b42ac..031add0 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/query/json/base/relations/JsonQueryIsRelatedTo.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/query/json/base/relations/JsonQueryIsRelatedTo.java @@ -66,8 +66,7 @@ public class JsonQueryIsRelatedTo extends JsonQueryRelation { stringBuffer.append(" )"); // Close ) SELECT } stringBuffer.append(" WHERE "); - - stringBuffer = addWhereConstraint(jsonNode, buffer, null); + stringBuffer.append(addConstraint(jsonNode, null, null)); } return stringBuffer;