Improving code

This commit is contained in:
Luca Frosini 2021-10-19 22:18:41 +02:00
parent b339c9d49a
commit 5b3db43248
8 changed files with 59 additions and 98 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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)) {

View File

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