Reorganized code to better manage JsonQuery

This commit is contained in:
Luca Frosini 2021-10-14 18:03:55 +02:00
parent 5a2035d21f
commit 4d62dc6a6a
6 changed files with 104 additions and 73 deletions

View File

@ -37,7 +37,6 @@ public class JsonQuery {
JsonQueryResource jsonQueryResource = new JsonQueryResource(jsonQuery);
jsonQueryResource.setEntryPoint(true);
jsonQueryResource.setDirection(Direction.OUT);
return jsonQueryResource.analize(new StringBuffer());
}

View File

@ -2,6 +2,7 @@ package org.gcube.informationsystem.resourceregistry.query.json;
import org.gcube.com.fasterxml.jackson.databind.JsonNode;
import org.gcube.informationsystem.base.reference.AccessType;
import org.gcube.informationsystem.base.reference.Direction;
import org.gcube.informationsystem.model.reference.relations.ConsistsOf;
import org.gcube.informationsystem.resourceregistry.api.exceptions.query.InvalidQueryException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaNotFoundException;
@ -15,6 +16,7 @@ public class JsonQueryConsistsOf extends JsonQueryERElement{
public JsonQueryConsistsOf(JsonNode jsonQuery) throws SchemaNotFoundException {
super(jsonQuery, AccessType.CONSISTS_OF);
this.direction = Direction.IN;
}
public String getRequestedResourceType() {
@ -27,46 +29,59 @@ public class JsonQueryConsistsOf extends JsonQueryERElement{
@Override
protected StringBuffer analize(StringBuffer stringBuffer) throws SchemaNotFoundException, InvalidQueryException {
StringBuffer newBuffer = new StringBuffer();
StringBuffer consistsOfBuffer = new StringBuffer();
consistsOfBuffer.append("TRAVERSE ");
consistsOfBuffer.append(direction.opposite().name().toLowerCase());
consistsOfBuffer.append("V(\"");
consistsOfBuffer.append(requestedResourceType);
consistsOfBuffer.append("\") FROM ("); // Open ( 1
int size = jsonNode.size();
if(size > 2) {
newBuffer.append("SELECT FROM ( ");
consistsOfBuffer.append("SELECT FROM ( "); // Open ( SELECT
}
newBuffer.append("TRAVERSE inE(\"");
newBuffer.append(type);
newBuffer.append("\") FROM ("); // Open (
consistsOfBuffer.append("TRAVERSE ");
consistsOfBuffer.append(direction.name().toLowerCase());
consistsOfBuffer.append("E(\"");
consistsOfBuffer.append(type);
consistsOfBuffer.append("\") FROM ("); // Open ( 2
if(!entryPoint) {
StringBuffer anotherBuffer = new StringBuffer();
anotherBuffer.append(" TRAVERSE outV(\"");
anotherBuffer.append(requestedResourceType);
anotherBuffer.append("\").outE(\"");
anotherBuffer.append(type);
anotherBuffer.append("\") FROM (");
anotherBuffer.append(stringBuffer);
anotherBuffer.append(")");
StringBuffer buffer = new StringBuffer();
buffer.append(" TRAVERSE ");
buffer.append(direction.opposite().name().toLowerCase());
buffer.append("V(\"");
buffer.append(requestedResourceType);
buffer.append("\").");
buffer.append(direction.opposite().name().toLowerCase());
buffer.append("E(\"");
buffer.append(type);
buffer.append("\") FROM (");
buffer.append(stringBuffer);
buffer.append(")");
stringBuffer = anotherBuffer;
stringBuffer = buffer;
}
JsonNode facetJsonNode = jsonNode.get(ConsistsOf.TARGET_PROPERTY);
JsonQueryFacet jsonQueryFacet = new JsonQueryFacet(facetJsonNode);
jsonQueryFacet.setEntryPoint(entryPoint);
stringBuffer = jsonQueryFacet.analize(stringBuffer);
newBuffer.append(jsonQueryFacet.analize(stringBuffer));
consistsOfBuffer.append(stringBuffer);
consistsOfBuffer.append(")"); // Close ) 2
newBuffer.append(")"); // Close )
// Size 2 means that only '@class' and 'target' properties present
// Size 2 means that only '@class' and 'target' properties are present
if(size > 2) {
newBuffer.append(") WHERE ");
addWhereConstraint(jsonNode, newBuffer, null);
consistsOfBuffer.append(") WHERE "); // Close ) SELECT
addWhereConstraint(jsonNode, consistsOfBuffer, null);
}
return newBuffer;
consistsOfBuffer.append(")"); // Close ) 1
return consistsOfBuffer;
}
}

View File

@ -36,6 +36,7 @@ public abstract class JsonQueryERElement {
public String getType() {
return type;
}
public Direction getDirection() {
return direction;
}

View File

@ -28,41 +28,73 @@ public class JsonQueryIsRelatedTo extends JsonQueryERElement{
@Override
protected StringBuffer analize(StringBuffer stringBuffer) throws SchemaNotFoundException, InvalidQueryException {
StringBuffer newBuffer = new StringBuffer();
StringBuffer isRelatedToBuffer = new StringBuffer();
int size = jsonNode.size();
if(size > 2) {
newBuffer.append("SELECT FROM ( ");
}
JsonNode targetJsonNode = jsonNode.get(ConsistsOf.SOURCE_PROPERTY);
JsonNode sourceJsonNode = jsonNode.get(ConsistsOf.TARGET_PROPERTY);
JsonNode sourceJsonNode = jsonNode.get(ConsistsOf.SOURCE_PROPERTY);
JsonNode targetJsonNode = jsonNode.get(ConsistsOf.TARGET_PROPERTY);
JsonNode resourceJsonNode = null;
Direction direction = null;
if(targetJsonNode!=null) {
newBuffer.append("TRAVERSE outE(\"");
resourceJsonNode = targetJsonNode;
// TODO Check
direction = Direction.IN;
}else if(sourceJsonNode!=null) {
newBuffer.append("TRAVERSE inE(\"");
resourceJsonNode = sourceJsonNode;
// TODO Check
direction = Direction.OUT;
}
newBuffer.append(type);
newBuffer.append("\") FROM ("); // Open (
newBuffer.append(stringBuffer);
newBuffer.append(")"); // Close )
isRelatedToBuffer.append("TRAVERSE ");
isRelatedToBuffer.append(direction.opposite().name().toLowerCase());
isRelatedToBuffer.append("V(\"");
isRelatedToBuffer.append(requestedResourceType);
isRelatedToBuffer.append("\") FROM ("); // Open ( 1
int size = jsonNode.size();
if(size > 2) {
isRelatedToBuffer.append("SELECT FROM ( "); // Open ( SELECT
}
isRelatedToBuffer.append("TRAVERSE ");
isRelatedToBuffer.append(direction.name().toLowerCase());
isRelatedToBuffer.append("E(\"");
isRelatedToBuffer.append(type);
isRelatedToBuffer.append("\") FROM ("); // Open ( 2
if(!entryPoint) {
StringBuffer buffer = new StringBuffer();
buffer.append(" TRAVERSE ");
buffer.append(direction.opposite().name().toLowerCase());
buffer.append("V(\"");
buffer.append(requestedResourceType);
buffer.append("\").");
buffer.append(direction.opposite().name().toLowerCase());
buffer.append("E(\"");
buffer.append(type);
buffer.append("\") FROM (");
buffer.append(stringBuffer);
buffer.append(")");
stringBuffer = buffer;
}
JsonQueryResource jsonQueryResource = new JsonQueryResource(resourceJsonNode);
jsonQueryResource.setDirection(direction);
stringBuffer = jsonQueryResource.analize(newBuffer);
jsonQueryResource.setEntryPoint(false);
stringBuffer = jsonQueryResource.analize(stringBuffer);
return stringBuffer;
isRelatedToBuffer.append(stringBuffer);
isRelatedToBuffer.append(")"); // Close ) 2
// Size 2 means that only '@class' and 'target'/'source' properties are present
if(size > 2) {
isRelatedToBuffer.append(") WHERE "); // Close ) SELECT
isRelatedToBuffer = addWhereConstraint(jsonNode, isRelatedToBuffer, null);
}
isRelatedToBuffer.append(")"); // Close ) 1
return isRelatedToBuffer;
}

View File

@ -3,6 +3,7 @@ package org.gcube.informationsystem.resourceregistry.query.json;
import org.gcube.com.fasterxml.jackson.databind.JsonNode;
import org.gcube.com.fasterxml.jackson.databind.node.ArrayNode;
import org.gcube.informationsystem.base.reference.AccessType;
import org.gcube.informationsystem.base.reference.Direction;
import org.gcube.informationsystem.model.reference.entities.Resource;
import org.gcube.informationsystem.resourceregistry.api.exceptions.query.InvalidQueryException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaNotFoundException;
@ -19,41 +20,23 @@ public class JsonQueryResource extends JsonQueryERElement {
@Override
protected StringBuffer analize(StringBuffer stringBuffer) throws SchemaNotFoundException, InvalidQueryException {
ArrayNode consistsOfArray = (ArrayNode) jsonNode.get(Resource.CONSISTS_OF_PROPERTY);
boolean initFound = false;
ArrayNode consistsOfArray = (ArrayNode) jsonNode.get(Resource.CONSISTS_OF_PROPERTY);
if(consistsOfArray!=null && consistsOfArray.size()>0) {
StringBuffer consistsOfBuffer = new StringBuffer();
initFound = true;
consistsOfBuffer.append("TRAVERSE ");
consistsOfBuffer.append(direction.name().toLowerCase());
consistsOfBuffer.append("V(\"");
consistsOfBuffer.append(type);
consistsOfBuffer.append("\") FROM (");
for(int i=0; i<consistsOfArray.size(); i++) {
JsonNode consistsOfJsonNode = consistsOfArray.get(i);
JsonQueryConsistsOf jsonQueryConsistsOf = new JsonQueryConsistsOf(consistsOfJsonNode);
jsonQueryConsistsOf.setRequestedResourceType(type);
jsonQueryConsistsOf.setDirection(Direction.IN);
jsonQueryConsistsOf.setEntryPoint(i==0);
stringBuffer = jsonQueryConsistsOf.analize(stringBuffer);
}
consistsOfBuffer.append(stringBuffer);
consistsOfBuffer.append(")");
stringBuffer = consistsOfBuffer;
}
ArrayNode isRelatedToArray = (ArrayNode) jsonNode.get(Resource.IS_RELATED_TO_PROPERTY);
if(isRelatedToArray!=null && isRelatedToArray.size()>0) {
StringBuffer isRelatedToBuffer = new StringBuffer();
for(int i=0; i<isRelatedToArray.size(); i++) {
JsonNode isRelatedToJsonNode = isRelatedToArray.get(i);
JsonQueryIsRelatedTo jsonQueryIsRelatedTo = new JsonQueryIsRelatedTo(isRelatedToJsonNode);
@ -61,15 +44,9 @@ public class JsonQueryResource extends JsonQueryERElement {
jsonQueryIsRelatedTo.setEntryPoint(!initFound && i==0);
stringBuffer = jsonQueryIsRelatedTo.analize(stringBuffer);
}
initFound = true;
isRelatedToBuffer.append(stringBuffer);
stringBuffer = isRelatedToBuffer;
initFound = true; // Must be set after the cycle and not before
}
if(!initFound) {
if(entryPoint) {
stringBuffer = new StringBuffer();
@ -77,7 +54,6 @@ public class JsonQueryResource extends JsonQueryERElement {
stringBuffer.append(type);
}else {
StringBuffer newBuffer = new StringBuffer();
newBuffer.append("TRAVERSE ");
newBuffer.append(direction.name().toLowerCase());
newBuffer.append("V(\"");

View File

@ -27,5 +27,13 @@
"endpoint": "http://pc-frosini.isti.cnr.it:8080/data-transfer-service/gcube/service"
}
}
],
"isRelatedTo" : [
{
"@class": "Activates",
"source": {
"@class": "HostingNode"
}
}
]
}