Improving code

This commit is contained in:
luca.frosini 2023-11-24 17:44:27 +01:00
parent 78ea9c2307
commit 0812a0c94c
5 changed files with 73 additions and 57 deletions

View File

@ -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 EntityElementManagement<Context, EntityTy
select.append(parentId);
select.append(" MAXDEPTH 1) WHERE ");
select.append(Context.NAME_PROPERTY);
select.append(ConditionalOperator.EQ.getDbConditionalOperator());
select.append(ComparisonOperator.EQ.getDbOperator());
select.append("\"");
select.append(getName());
select.append("\"");
select.append(LogicalOperator.AND.getLogicalOperator());
select.append(LogicalOperator.AND.getDbOperator());
select.append(IdentifiableElement.ID_PROPERTY);
select.append(ConditionalOperator.NE.getDbConditionalOperator());
select.append(ComparisonOperator.NE.getDbOperator());
select.append("\"");
select.append(parentContext.uuid);
select.append("\"");
@ -164,11 +164,11 @@ public class ContextManagement extends EntityElementManagement<Context, EntityTy
select.append(Context.NAME);
select.append(" WHERE ");
select.append(Context.NAME_PROPERTY);
select.append(ConditionalOperator.EQ.getDbConditionalOperator());
select.append(ComparisonOperator.EQ.getDbOperator());
select.append("\"");
select.append(getName());
select.append("\"");
select.append(LogicalOperator.AND.getLogicalOperator());
select.append(LogicalOperator.AND.getDbOperator());
select.append("in(\"");
select.append(IsParentOf.NAME);
select.append("\").size() = 0");

View File

@ -20,7 +20,7 @@ import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegis
import org.gcube.informationsystem.resourceregistry.api.exceptions.queries.InvalidQueryException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.types.SchemaException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.types.SchemaNotFoundException;
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.queries.operators.MatemathicsOperator;
import org.gcube.informationsystem.resourceregistry.queries.operators.ProjectionOperator;
@ -248,7 +248,7 @@ public abstract class JsonQueryERElement {
if(first) {
first = false;
}else {
stringBuffer.append(queryLogicalOperator.getLogicalOperator());
stringBuffer.append(queryLogicalOperator.getDbOperator());
}
stringBuffer.append(evBuffer);
}
@ -264,7 +264,7 @@ public abstract class JsonQueryERElement {
StringBuffer evBuffer = evaluateNode(node, null, fieldNamePrefix);
if(!first) {
stringBuffer.append(queryLogicalOperator.getLogicalOperator());
stringBuffer.append(queryLogicalOperator.getDbOperator());
}
if(evBuffer!=null && evBuffer.length()>0) {
@ -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;

View File

@ -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<String> operators;
private static Map<String,ConditionalOperator> operatorByKey;
private static Map<String,ComparisonOperator> 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<String> 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();
}
}

View File

@ -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);
}
}

View File

@ -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(" ");
}
}