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

View File

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

View File

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