From 0812a0c94cfa9de2507c02e7007196caf12047bb Mon Sep 17 00:00:00 2001 From: "luca.frosini" Date: Fri, 24 Nov 2023 17:44:27 +0100 Subject: [PATCH] Improving code --- .../contexts/entities/ContextManagement.java | 12 ++--- .../queries/json/base/JsonQueryERElement.java | 24 ++++----- ...lOperator.java => ComparisonOperator.java} | 50 +++++++++++++------ .../queries/operators/LogicalOperator.java | 26 +++++----- .../operators/MatemathicsOperator.java | 18 +++---- 5 files changed, 73 insertions(+), 57 deletions(-) rename src/main/java/org/gcube/informationsystem/resourceregistry/queries/operators/{ConditionalOperator.java => ComparisonOperator.java} (77%) diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/contexts/entities/ContextManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/contexts/entities/ContextManagement.java index 35c757b..b676080 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/contexts/entities/ContextManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/contexts/entities/ContextManagement.java @@ -32,7 +32,7 @@ import org.gcube.informationsystem.resourceregistry.contexts.relations.IsParentO import org.gcube.informationsystem.resourceregistry.contexts.security.ContextSecurityContext; import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityContext; import org.gcube.informationsystem.resourceregistry.instances.base.entities.EntityElementManagement; -import org.gcube.informationsystem.resourceregistry.queries.operators.ConditionalOperator; +import org.gcube.informationsystem.resourceregistry.queries.operators.ComparisonOperator; import org.gcube.informationsystem.resourceregistry.queries.operators.LogicalOperator; import org.gcube.informationsystem.resourceregistry.requests.RequestUtility; import org.gcube.informationsystem.resourceregistry.requests.ServerRequestInfo; @@ -142,13 +142,13 @@ public class ContextManagement extends EntityElementManagement0) { @@ -303,7 +303,7 @@ public abstract class JsonQueryERElement { if(MatemathicsOperator.getOperators().contains(fieldName)) { --size; setProjection(true); - MatemathicsOperator mo = MatemathicsOperator.getMatemathicsOperator(fieldName); + MatemathicsOperator mo = MatemathicsOperator.getOperator(fieldName); String fieldToEmit = mo.generateFieldToEmit(jsonNode, getAlias(true)); addFieldToEmit(fieldToEmit); return null; @@ -313,23 +313,19 @@ public abstract class JsonQueryERElement { StringBuffer stringBuffer = new StringBuffer(); if(LogicalOperator.getOperators().contains(fieldName)) { - LogicalOperator queryLogicalOperator = LogicalOperator.getQueryLogicalOperator(fieldName); + LogicalOperator queryLogicalOperator = LogicalOperator.getOperator(fieldName); stringBuffer.append("("); stringBuffer.append(addConstraints(jsonNode, queryLogicalOperator, fieldNamePrefix)); stringBuffer.append(")"); return stringBuffer; } - if(ConditionalOperator.getOperators().contains(fieldName)) { - ConditionalOperator queryConditionalOperator = ConditionalOperator.getQueryComparisonOperator(fieldName); - - if(queryConditionalOperator == ConditionalOperator.IN) { - throw new UnsupportedOperationException(); - } + if(ComparisonOperator.getOperators().contains(fieldName)) { + ComparisonOperator comparisonOperator = ComparisonOperator.getOperator(fieldName); String key = getKey(null, fieldNamePrefix); String value = getValue(jsonNode); - stringBuffer.append(queryConditionalOperator.addCondition(key, value)); + stringBuffer.append(comparisonOperator.addCondition(key, value)); return stringBuffer; } @@ -353,12 +349,12 @@ public abstract class JsonQueryERElement { if(jsonNode.isTextual() || jsonNode.isNumber()) { String key = getKey(fieldName, fieldNamePrefix); String value = getValue(jsonNode); - stringBuffer.append(ConditionalOperator.EQ.addCondition(key, value)); + stringBuffer.append(ComparisonOperator.EQ.addCondition(key, value)); } if(jsonNode.isNull()) { String key = getKey(fieldName, null); - stringBuffer.append(ConditionalOperator.IS.addCondition(key, null)); + stringBuffer.append(ComparisonOperator.IS.addCondition(key, null)); } return stringBuffer; diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/queries/operators/ConditionalOperator.java b/src/main/java/org/gcube/informationsystem/resourceregistry/queries/operators/ComparisonOperator.java similarity index 77% rename from src/main/java/org/gcube/informationsystem/resourceregistry/queries/operators/ConditionalOperator.java rename to src/main/java/org/gcube/informationsystem/resourceregistry/queries/operators/ComparisonOperator.java index 0155c3d..b01a3c6 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/queries/operators/ConditionalOperator.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/queries/operators/ComparisonOperator.java @@ -5,6 +5,8 @@ import java.util.HashSet; import java.util.Map; import java.util.Set; +import org.apache.commons.lang.NotImplementedException; +import org.gcube.com.fasterxml.jackson.databind.JsonNode; import org.gcube.informationsystem.types.PropertyTypeName.BaseTypeGroup; /** @@ -14,7 +16,7 @@ import org.gcube.informationsystem.types.PropertyTypeName.BaseTypeGroup; * https://www.orientdb.com/docs/3.0.x/sql/SQL-Syntax.html#comparison-operators * https://www.orientdb.com/docs/3.0.x/sql/SQL-Syntax.html#boolean-operators */ -public enum ConditionalOperator { +public enum ComparisonOperator { EQ("_eq", " = ", 2, BaseTypeGroup.ANY, "Matches values that are equal to a specified value. E.g. `name = 'Luke'`"), GT("_gt", " > ", 2, BaseTypeGroup.ANY, "Matches values that are greater than a specified value. "), @@ -43,13 +45,13 @@ public enum ConditionalOperator { protected final String operatorKey; protected final int numberOfOperand; - protected final String dbConditionalOperator; + protected final String dbOperator; protected final BaseTypeGroup allowed; protected final String description; - private ConditionalOperator(String operatorKey, String conditionalOperator, int numberOfOperand, BaseTypeGroup allowed, String description) { + private ComparisonOperator(String operatorKey, String dbOperator, int numberOfOperand, BaseTypeGroup allowed, String description) { this.operatorKey = operatorKey; - this.dbConditionalOperator = conditionalOperator; + this.dbOperator = dbOperator; this.numberOfOperand = numberOfOperand; this.allowed = allowed; this.description = description; @@ -59,8 +61,8 @@ public enum ConditionalOperator { return operatorKey; } - public String getDbConditionalOperator() { - return dbConditionalOperator; + public String getDbOperator() { + return dbOperator; } public String getDescription() { @@ -68,31 +70,49 @@ public enum ConditionalOperator { } private static Set operators; - private static Map operatorByKey; + private static Map operatorByKey; static { - ConditionalOperator.operators = new HashSet<>(); - ConditionalOperator.operatorByKey = new HashMap<>(); + ComparisonOperator.operators = new HashSet<>(); + ComparisonOperator.operatorByKey = new HashMap<>(); - for(ConditionalOperator queryComparisonOperator : ConditionalOperator.values()) { - ConditionalOperator.operators.add(queryComparisonOperator.getOperatorKey()); - ConditionalOperator.operatorByKey.put(queryComparisonOperator.getOperatorKey(), queryComparisonOperator); + for(ComparisonOperator queryComparisonOperator : ComparisonOperator.values()) { + ComparisonOperator.operators.add(queryComparisonOperator.getOperatorKey()); + ComparisonOperator.operatorByKey.put(queryComparisonOperator.getOperatorKey(), queryComparisonOperator); } } public static Set getOperators() { - return ConditionalOperator.operators; + return ComparisonOperator.operators; } - public static ConditionalOperator getQueryComparisonOperator(String key) { + public static ComparisonOperator getOperator(String key) { return operatorByKey.get(key); } public StringBuffer addCondition(String... operands) { StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append(operands[0]); - stringBuffer.append(getDbConditionalOperator()); + stringBuffer.append(getDbOperator()); stringBuffer.append(operands[1]); return stringBuffer; } + + public static String getValue(JsonNode jsonNode) { + StringBuffer stringBuffer = new StringBuffer(); + + String value = jsonNode.asText(); + if(jsonNode.isNumber()) { + stringBuffer.append(value); + } else { + stringBuffer.append("\""); + stringBuffer.append(value); + stringBuffer.append("\""); + } + return stringBuffer.toString(); + } + + public StringBuffer addCondition(JsonNode jn, String fieldNamePrefix) { + throw new NotImplementedException(); + } } diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/queries/operators/LogicalOperator.java b/src/main/java/org/gcube/informationsystem/resourceregistry/queries/operators/LogicalOperator.java index 46536a7..d843256 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/queries/operators/LogicalOperator.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/queries/operators/LogicalOperator.java @@ -15,22 +15,22 @@ public enum LogicalOperator { 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; + protected final String operatorKey; + protected final String dbOperator; protected final String description; - private LogicalOperator(String operator, String logicalOperator, String description) { - this.operator = operator; - this.logicalOperator = logicalOperator; + private LogicalOperator(String operatorKey, String dbOperator, String description) { + this.operatorKey = operatorKey; + this.dbOperator = dbOperator; this.description = description; } - public String getOperator() { - return operator; + public String getOperatorKey() { + return operatorKey; } - public String getLogicalOperator() { - return logicalOperator; + public String getDbOperator() { + return dbOperator; } public String getDescription() { @@ -44,9 +44,9 @@ public enum LogicalOperator { LogicalOperator.operators = new HashSet<>(); LogicalOperator.operatorByKey = new HashMap<>(); - for(LogicalOperator queryLogicalOperator : LogicalOperator.values()) { - LogicalOperator.operators.add(queryLogicalOperator.getOperator()); - LogicalOperator.operatorByKey.put(queryLogicalOperator.getOperator(), queryLogicalOperator); + for(LogicalOperator logicalOperator : LogicalOperator.values()) { + LogicalOperator.operators.add(logicalOperator.getOperatorKey()); + LogicalOperator.operatorByKey.put(logicalOperator.getOperatorKey(), logicalOperator); } } @@ -54,7 +54,7 @@ public enum LogicalOperator { return LogicalOperator.operators; } - public static LogicalOperator getQueryLogicalOperator(String key) { + public static LogicalOperator getOperator(String key) { return operatorByKey.get(key); } } diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/queries/operators/MatemathicsOperator.java b/src/main/java/org/gcube/informationsystem/resourceregistry/queries/operators/MatemathicsOperator.java index 367ca2d..b2650ce 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/queries/operators/MatemathicsOperator.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/queries/operators/MatemathicsOperator.java @@ -48,12 +48,12 @@ public enum MatemathicsOperator { public static final String AS_KEY = "as"; protected final String operatorKey; - protected final String dbMatemathicsOperator; + protected final String dbOperator; protected final String description; - private MatemathicsOperator(String operatorKey, String dbMatemathicsOperator, String description) { + private MatemathicsOperator(String operatorKey, String dbOperator, String description) { this.operatorKey = operatorKey; - this.dbMatemathicsOperator = dbMatemathicsOperator; + this.dbOperator = dbOperator; this.description = description; } @@ -61,8 +61,8 @@ public enum MatemathicsOperator { return operatorKey; } - protected String getDbMatemathicsOperator() { - return dbMatemathicsOperator; + protected String getDbOperator() { + return dbOperator; } public String getDescription() { @@ -86,7 +86,7 @@ public enum MatemathicsOperator { return operators; } - public static MatemathicsOperator getMatemathicsOperator(String key) { + public static MatemathicsOperator getOperator(String key) { return operatorByKey.get(key); } @@ -149,7 +149,7 @@ public enum MatemathicsOperator { if(jn.isObject()) { ObjectNode on = (ObjectNode) jn; String key = on.fieldNames().next(); - MatemathicsOperator mo = MatemathicsOperator.getMatemathicsOperator(key); + MatemathicsOperator mo = MatemathicsOperator.getOperator(key); ArrayNode an = (ArrayNode) on.get(key); buffer.append("("); buffer.append(mo.generateFieldToEmit(an, null, fieldPrefix)); @@ -163,13 +163,13 @@ public enum MatemathicsOperator { if(i<(size-1)) { buffer.append(" "); - buffer.append(dbMatemathicsOperator); + buffer.append(dbOperator); buffer.append(" "); if(fieldsSeparator!=null) { buffer.append("'"); buffer.append(fieldsSeparator); buffer.append("' "); - buffer.append(dbMatemathicsOperator); + buffer.append(dbOperator); buffer.append(" "); } }