Improving code
This commit is contained in:
parent
78ea9c2307
commit
0812a0c94c
|
@ -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.ContextSecurityContext;
|
||||||
import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityContext;
|
import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityContext;
|
||||||
import org.gcube.informationsystem.resourceregistry.instances.base.entities.EntityElementManagement;
|
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.queries.operators.LogicalOperator;
|
||||||
import org.gcube.informationsystem.resourceregistry.requests.RequestUtility;
|
import org.gcube.informationsystem.resourceregistry.requests.RequestUtility;
|
||||||
import org.gcube.informationsystem.resourceregistry.requests.ServerRequestInfo;
|
import org.gcube.informationsystem.resourceregistry.requests.ServerRequestInfo;
|
||||||
|
@ -142,13 +142,13 @@ public class ContextManagement extends EntityElementManagement<Context, EntityTy
|
||||||
select.append(parentId);
|
select.append(parentId);
|
||||||
select.append(" MAXDEPTH 1) WHERE ");
|
select.append(" MAXDEPTH 1) WHERE ");
|
||||||
select.append(Context.NAME_PROPERTY);
|
select.append(Context.NAME_PROPERTY);
|
||||||
select.append(ConditionalOperator.EQ.getDbConditionalOperator());
|
select.append(ComparisonOperator.EQ.getDbOperator());
|
||||||
select.append("\"");
|
select.append("\"");
|
||||||
select.append(getName());
|
select.append(getName());
|
||||||
select.append("\"");
|
select.append("\"");
|
||||||
select.append(LogicalOperator.AND.getLogicalOperator());
|
select.append(LogicalOperator.AND.getDbOperator());
|
||||||
select.append(IdentifiableElement.ID_PROPERTY);
|
select.append(IdentifiableElement.ID_PROPERTY);
|
||||||
select.append(ConditionalOperator.NE.getDbConditionalOperator());
|
select.append(ComparisonOperator.NE.getDbOperator());
|
||||||
select.append("\"");
|
select.append("\"");
|
||||||
select.append(parentContext.uuid);
|
select.append(parentContext.uuid);
|
||||||
select.append("\"");
|
select.append("\"");
|
||||||
|
@ -164,11 +164,11 @@ public class ContextManagement extends EntityElementManagement<Context, EntityTy
|
||||||
select.append(Context.NAME);
|
select.append(Context.NAME);
|
||||||
select.append(" WHERE ");
|
select.append(" WHERE ");
|
||||||
select.append(Context.NAME_PROPERTY);
|
select.append(Context.NAME_PROPERTY);
|
||||||
select.append(ConditionalOperator.EQ.getDbConditionalOperator());
|
select.append(ComparisonOperator.EQ.getDbOperator());
|
||||||
select.append("\"");
|
select.append("\"");
|
||||||
select.append(getName());
|
select.append(getName());
|
||||||
select.append("\"");
|
select.append("\"");
|
||||||
select.append(LogicalOperator.AND.getLogicalOperator());
|
select.append(LogicalOperator.AND.getDbOperator());
|
||||||
select.append("in(\"");
|
select.append("in(\"");
|
||||||
select.append(IsParentOf.NAME);
|
select.append(IsParentOf.NAME);
|
||||||
select.append("\").size() = 0");
|
select.append("\").size() = 0");
|
||||||
|
|
|
@ -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.queries.InvalidQueryException;
|
||||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.types.SchemaException;
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.types.SchemaException;
|
||||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.types.SchemaNotFoundException;
|
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.LogicalOperator;
|
||||||
import org.gcube.informationsystem.resourceregistry.queries.operators.MatemathicsOperator;
|
import org.gcube.informationsystem.resourceregistry.queries.operators.MatemathicsOperator;
|
||||||
import org.gcube.informationsystem.resourceregistry.queries.operators.ProjectionOperator;
|
import org.gcube.informationsystem.resourceregistry.queries.operators.ProjectionOperator;
|
||||||
|
@ -248,7 +248,7 @@ public abstract class JsonQueryERElement {
|
||||||
if(first) {
|
if(first) {
|
||||||
first = false;
|
first = false;
|
||||||
}else {
|
}else {
|
||||||
stringBuffer.append(queryLogicalOperator.getLogicalOperator());
|
stringBuffer.append(queryLogicalOperator.getDbOperator());
|
||||||
}
|
}
|
||||||
stringBuffer.append(evBuffer);
|
stringBuffer.append(evBuffer);
|
||||||
}
|
}
|
||||||
|
@ -264,7 +264,7 @@ public abstract class JsonQueryERElement {
|
||||||
StringBuffer evBuffer = evaluateNode(node, null, fieldNamePrefix);
|
StringBuffer evBuffer = evaluateNode(node, null, fieldNamePrefix);
|
||||||
|
|
||||||
if(!first) {
|
if(!first) {
|
||||||
stringBuffer.append(queryLogicalOperator.getLogicalOperator());
|
stringBuffer.append(queryLogicalOperator.getDbOperator());
|
||||||
}
|
}
|
||||||
|
|
||||||
if(evBuffer!=null && evBuffer.length()>0) {
|
if(evBuffer!=null && evBuffer.length()>0) {
|
||||||
|
@ -303,7 +303,7 @@ public abstract class JsonQueryERElement {
|
||||||
if(MatemathicsOperator.getOperators().contains(fieldName)) {
|
if(MatemathicsOperator.getOperators().contains(fieldName)) {
|
||||||
--size;
|
--size;
|
||||||
setProjection(true);
|
setProjection(true);
|
||||||
MatemathicsOperator mo = MatemathicsOperator.getMatemathicsOperator(fieldName);
|
MatemathicsOperator mo = MatemathicsOperator.getOperator(fieldName);
|
||||||
String fieldToEmit = mo.generateFieldToEmit(jsonNode, getAlias(true));
|
String fieldToEmit = mo.generateFieldToEmit(jsonNode, getAlias(true));
|
||||||
addFieldToEmit(fieldToEmit);
|
addFieldToEmit(fieldToEmit);
|
||||||
return null;
|
return null;
|
||||||
|
@ -313,23 +313,19 @@ public abstract class JsonQueryERElement {
|
||||||
StringBuffer stringBuffer = new StringBuffer();
|
StringBuffer stringBuffer = new StringBuffer();
|
||||||
|
|
||||||
if(LogicalOperator.getOperators().contains(fieldName)) {
|
if(LogicalOperator.getOperators().contains(fieldName)) {
|
||||||
LogicalOperator queryLogicalOperator = LogicalOperator.getQueryLogicalOperator(fieldName);
|
LogicalOperator queryLogicalOperator = LogicalOperator.getOperator(fieldName);
|
||||||
stringBuffer.append("(");
|
stringBuffer.append("(");
|
||||||
stringBuffer.append(addConstraints(jsonNode, queryLogicalOperator, fieldNamePrefix));
|
stringBuffer.append(addConstraints(jsonNode, queryLogicalOperator, fieldNamePrefix));
|
||||||
stringBuffer.append(")");
|
stringBuffer.append(")");
|
||||||
return stringBuffer;
|
return stringBuffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(ConditionalOperator.getOperators().contains(fieldName)) {
|
if(ComparisonOperator.getOperators().contains(fieldName)) {
|
||||||
ConditionalOperator queryConditionalOperator = ConditionalOperator.getQueryComparisonOperator(fieldName);
|
ComparisonOperator comparisonOperator = ComparisonOperator.getOperator(fieldName);
|
||||||
|
|
||||||
if(queryConditionalOperator == ConditionalOperator.IN) {
|
|
||||||
throw new UnsupportedOperationException();
|
|
||||||
}
|
|
||||||
|
|
||||||
String key = getKey(null, fieldNamePrefix);
|
String key = getKey(null, fieldNamePrefix);
|
||||||
String value = getValue(jsonNode);
|
String value = getValue(jsonNode);
|
||||||
stringBuffer.append(queryConditionalOperator.addCondition(key, value));
|
stringBuffer.append(comparisonOperator.addCondition(key, value));
|
||||||
return stringBuffer;
|
return stringBuffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -353,12 +349,12 @@ public abstract class JsonQueryERElement {
|
||||||
if(jsonNode.isTextual() || jsonNode.isNumber()) {
|
if(jsonNode.isTextual() || jsonNode.isNumber()) {
|
||||||
String key = getKey(fieldName, fieldNamePrefix);
|
String key = getKey(fieldName, fieldNamePrefix);
|
||||||
String value = getValue(jsonNode);
|
String value = getValue(jsonNode);
|
||||||
stringBuffer.append(ConditionalOperator.EQ.addCondition(key, value));
|
stringBuffer.append(ComparisonOperator.EQ.addCondition(key, value));
|
||||||
}
|
}
|
||||||
|
|
||||||
if(jsonNode.isNull()) {
|
if(jsonNode.isNull()) {
|
||||||
String key = getKey(fieldName, null);
|
String key = getKey(fieldName, null);
|
||||||
stringBuffer.append(ConditionalOperator.IS.addCondition(key, null));
|
stringBuffer.append(ComparisonOperator.IS.addCondition(key, null));
|
||||||
}
|
}
|
||||||
|
|
||||||
return stringBuffer;
|
return stringBuffer;
|
||||||
|
|
|
@ -5,6 +5,8 @@ import java.util.HashSet;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
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;
|
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#comparison-operators
|
||||||
* https://www.orientdb.com/docs/3.0.x/sql/SQL-Syntax.html#boolean-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'`"),
|
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. "),
|
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 String operatorKey;
|
||||||
protected final int numberOfOperand;
|
protected final int numberOfOperand;
|
||||||
protected final String dbConditionalOperator;
|
protected final String dbOperator;
|
||||||
protected final BaseTypeGroup allowed;
|
protected final BaseTypeGroup allowed;
|
||||||
protected final String description;
|
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.operatorKey = operatorKey;
|
||||||
this.dbConditionalOperator = conditionalOperator;
|
this.dbOperator = dbOperator;
|
||||||
this.numberOfOperand = numberOfOperand;
|
this.numberOfOperand = numberOfOperand;
|
||||||
this.allowed = allowed;
|
this.allowed = allowed;
|
||||||
this.description = description;
|
this.description = description;
|
||||||
|
@ -59,8 +61,8 @@ public enum ConditionalOperator {
|
||||||
return operatorKey;
|
return operatorKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getDbConditionalOperator() {
|
public String getDbOperator() {
|
||||||
return dbConditionalOperator;
|
return dbOperator;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getDescription() {
|
public String getDescription() {
|
||||||
|
@ -68,31 +70,49 @@ public enum ConditionalOperator {
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Set<String> operators;
|
private static Set<String> operators;
|
||||||
private static Map<String,ConditionalOperator> operatorByKey;
|
private static Map<String,ComparisonOperator> operatorByKey;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
ConditionalOperator.operators = new HashSet<>();
|
ComparisonOperator.operators = new HashSet<>();
|
||||||
ConditionalOperator.operatorByKey = new HashMap<>();
|
ComparisonOperator.operatorByKey = new HashMap<>();
|
||||||
|
|
||||||
for(ConditionalOperator queryComparisonOperator : ConditionalOperator.values()) {
|
for(ComparisonOperator queryComparisonOperator : ComparisonOperator.values()) {
|
||||||
ConditionalOperator.operators.add(queryComparisonOperator.getOperatorKey());
|
ComparisonOperator.operators.add(queryComparisonOperator.getOperatorKey());
|
||||||
ConditionalOperator.operatorByKey.put(queryComparisonOperator.getOperatorKey(), queryComparisonOperator);
|
ComparisonOperator.operatorByKey.put(queryComparisonOperator.getOperatorKey(), queryComparisonOperator);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Set<String> getOperators() {
|
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);
|
return operatorByKey.get(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
public StringBuffer addCondition(String... operands) {
|
public StringBuffer addCondition(String... operands) {
|
||||||
StringBuffer stringBuffer = new StringBuffer();
|
StringBuffer stringBuffer = new StringBuffer();
|
||||||
stringBuffer.append(operands[0]);
|
stringBuffer.append(operands[0]);
|
||||||
stringBuffer.append(getDbConditionalOperator());
|
stringBuffer.append(getDbOperator());
|
||||||
stringBuffer.append(operands[1]);
|
stringBuffer.append(operands[1]);
|
||||||
return stringBuffer;
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -15,22 +15,22 @@ public enum LogicalOperator {
|
||||||
OR("_or", " OR ", "true if at least one of the condition is true"),
|
OR("_or", " OR ", "true if at least one of the condition is true"),
|
||||||
NOT("_not", " NOT ", "true if the condition is false.");
|
NOT("_not", " NOT ", "true if the condition is false.");
|
||||||
|
|
||||||
protected final String operator;
|
protected final String operatorKey;
|
||||||
protected final String logicalOperator;
|
protected final String dbOperator;
|
||||||
protected final String description;
|
protected final String description;
|
||||||
|
|
||||||
private LogicalOperator(String operator, String logicalOperator, String description) {
|
private LogicalOperator(String operatorKey, String dbOperator, String description) {
|
||||||
this.operator = operator;
|
this.operatorKey = operatorKey;
|
||||||
this.logicalOperator = logicalOperator;
|
this.dbOperator = dbOperator;
|
||||||
this.description = description;
|
this.description = description;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getOperator() {
|
public String getOperatorKey() {
|
||||||
return operator;
|
return operatorKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getLogicalOperator() {
|
public String getDbOperator() {
|
||||||
return logicalOperator;
|
return dbOperator;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getDescription() {
|
public String getDescription() {
|
||||||
|
@ -44,9 +44,9 @@ public enum LogicalOperator {
|
||||||
LogicalOperator.operators = new HashSet<>();
|
LogicalOperator.operators = new HashSet<>();
|
||||||
LogicalOperator.operatorByKey = new HashMap<>();
|
LogicalOperator.operatorByKey = new HashMap<>();
|
||||||
|
|
||||||
for(LogicalOperator queryLogicalOperator : LogicalOperator.values()) {
|
for(LogicalOperator logicalOperator : LogicalOperator.values()) {
|
||||||
LogicalOperator.operators.add(queryLogicalOperator.getOperator());
|
LogicalOperator.operators.add(logicalOperator.getOperatorKey());
|
||||||
LogicalOperator.operatorByKey.put(queryLogicalOperator.getOperator(), queryLogicalOperator);
|
LogicalOperator.operatorByKey.put(logicalOperator.getOperatorKey(), logicalOperator);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -54,7 +54,7 @@ public enum LogicalOperator {
|
||||||
return LogicalOperator.operators;
|
return LogicalOperator.operators;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static LogicalOperator getQueryLogicalOperator(String key) {
|
public static LogicalOperator getOperator(String key) {
|
||||||
return operatorByKey.get(key);
|
return operatorByKey.get(key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,12 +48,12 @@ public enum MatemathicsOperator {
|
||||||
public static final String AS_KEY = "as";
|
public static final String AS_KEY = "as";
|
||||||
|
|
||||||
protected final String operatorKey;
|
protected final String operatorKey;
|
||||||
protected final String dbMatemathicsOperator;
|
protected final String dbOperator;
|
||||||
protected final String description;
|
protected final String description;
|
||||||
|
|
||||||
private MatemathicsOperator(String operatorKey, String dbMatemathicsOperator, String description) {
|
private MatemathicsOperator(String operatorKey, String dbOperator, String description) {
|
||||||
this.operatorKey = operatorKey;
|
this.operatorKey = operatorKey;
|
||||||
this.dbMatemathicsOperator = dbMatemathicsOperator;
|
this.dbOperator = dbOperator;
|
||||||
this.description = description;
|
this.description = description;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -61,8 +61,8 @@ public enum MatemathicsOperator {
|
||||||
return operatorKey;
|
return operatorKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected String getDbMatemathicsOperator() {
|
protected String getDbOperator() {
|
||||||
return dbMatemathicsOperator;
|
return dbOperator;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getDescription() {
|
public String getDescription() {
|
||||||
|
@ -86,7 +86,7 @@ public enum MatemathicsOperator {
|
||||||
return operators;
|
return operators;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static MatemathicsOperator getMatemathicsOperator(String key) {
|
public static MatemathicsOperator getOperator(String key) {
|
||||||
return operatorByKey.get(key);
|
return operatorByKey.get(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -149,7 +149,7 @@ public enum MatemathicsOperator {
|
||||||
if(jn.isObject()) {
|
if(jn.isObject()) {
|
||||||
ObjectNode on = (ObjectNode) jn;
|
ObjectNode on = (ObjectNode) jn;
|
||||||
String key = on.fieldNames().next();
|
String key = on.fieldNames().next();
|
||||||
MatemathicsOperator mo = MatemathicsOperator.getMatemathicsOperator(key);
|
MatemathicsOperator mo = MatemathicsOperator.getOperator(key);
|
||||||
ArrayNode an = (ArrayNode) on.get(key);
|
ArrayNode an = (ArrayNode) on.get(key);
|
||||||
buffer.append("(");
|
buffer.append("(");
|
||||||
buffer.append(mo.generateFieldToEmit(an, null, fieldPrefix));
|
buffer.append(mo.generateFieldToEmit(an, null, fieldPrefix));
|
||||||
|
@ -163,13 +163,13 @@ public enum MatemathicsOperator {
|
||||||
|
|
||||||
if(i<(size-1)) {
|
if(i<(size-1)) {
|
||||||
buffer.append(" ");
|
buffer.append(" ");
|
||||||
buffer.append(dbMatemathicsOperator);
|
buffer.append(dbOperator);
|
||||||
buffer.append(" ");
|
buffer.append(" ");
|
||||||
if(fieldsSeparator!=null) {
|
if(fieldsSeparator!=null) {
|
||||||
buffer.append("'");
|
buffer.append("'");
|
||||||
buffer.append(fieldsSeparator);
|
buffer.append(fieldsSeparator);
|
||||||
buffer.append("' ");
|
buffer.append("' ");
|
||||||
buffer.append(dbMatemathicsOperator);
|
buffer.append(dbOperator);
|
||||||
buffer.append(" ");
|
buffer.append(" ");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue