diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/query/json/JsonQueryIsRelatedTo.java b/src/main/java/org/gcube/informationsystem/resourceregistry/query/json/JsonQueryIsRelatedTo.java index 9ad2e76..af2f5b9 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/query/json/JsonQueryIsRelatedTo.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/query/json/JsonQueryIsRelatedTo.java @@ -26,75 +26,73 @@ public class JsonQueryIsRelatedTo extends JsonQueryERElement{ this.requestedResourceType = requestedResourceType; } + private StringBuffer traverseThisEdge(StringBuffer stringBuffer) throws InvalidQueryException { + StringBuffer buffer = new StringBuffer(); + + int size = jsonNode.size(); + if(size > 2) { + buffer.append("SELECT FROM ( "); // Open ( SELECT + } + + buffer.append("TRAVERSE "); + buffer.append(direction.opposite().name().toLowerCase()); + buffer.append("E(\""); + buffer.append(type); + buffer.append("\") FROM ("); + buffer.append(stringBuffer); + buffer.append(")"); + + + stringBuffer = buffer; + + // Size 2 means that only '@class' and 'target'/'source' properties are present + if(size > 2) { + stringBuffer.append(") WHERE "); // Close ) SELECT + stringBuffer = addWhereConstraint(jsonNode, buffer, null); + } + + return stringBuffer; + } + @Override protected StringBuffer analize(StringBuffer stringBuffer) throws SchemaNotFoundException, InvalidQueryException { - StringBuffer isRelatedToBuffer = new StringBuffer(); + JsonNode sourceJsonNode = jsonNode.get(ConsistsOf.SOURCE_PROPERTY); JsonNode targetJsonNode = jsonNode.get(ConsistsOf.TARGET_PROPERTY); JsonNode resourceJsonNode = null; - if(targetJsonNode!=null) { - resourceJsonNode = targetJsonNode; - direction = Direction.IN; - }else if(sourceJsonNode!=null) { + if(sourceJsonNode!=null) { resourceJsonNode = sourceJsonNode; direction = Direction.OUT; + } else if(targetJsonNode!=null) { + resourceJsonNode = targetJsonNode; + direction = Direction.IN; } - 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; - } + stringBuffer = traverseThisEdge(stringBuffer); JsonQueryResource jsonQueryResource = new JsonQueryResource(resourceJsonNode); jsonQueryResource.setDirection(direction); jsonQueryResource.setEntryPoint(false); stringBuffer = jsonQueryResource.analize(stringBuffer); + StringBuffer buffer = new StringBuffer(); + buffer.append(" TRAVERSE "); + buffer.append(direction.opposite().name().toLowerCase()); + buffer.append("V(\""); + buffer.append(requestedResourceType); + buffer.append("\")."); + buffer.append(direction.name().toLowerCase()); + buffer.append("E(\""); + buffer.append(type); + buffer.append("\") FROM ("); + buffer.append(stringBuffer); + buffer.append(")"); + stringBuffer = buffer; - 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; + return stringBuffer; } diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/query/json/JsonQueryResource.java b/src/main/java/org/gcube/informationsystem/resourceregistry/query/json/JsonQueryResource.java index de33b98..58b7b41 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/query/json/JsonQueryResource.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/query/json/JsonQueryResource.java @@ -30,7 +30,7 @@ public class JsonQueryResource extends JsonQueryERElement { JsonQueryConsistsOf jsonQueryConsistsOf = new JsonQueryConsistsOf(consistsOfJsonNode); jsonQueryConsistsOf.setRequestedResourceType(type); jsonQueryConsistsOf.setDirection(Direction.IN); - jsonQueryConsistsOf.setEntryPoint(i==0); + jsonQueryConsistsOf.setEntryPoint(entryPoint && i==0); stringBuffer = jsonQueryConsistsOf.analize(stringBuffer); } } @@ -41,7 +41,7 @@ public class JsonQueryResource extends JsonQueryERElement { JsonNode isRelatedToJsonNode = isRelatedToArray.get(i); JsonQueryIsRelatedTo jsonQueryIsRelatedTo = new JsonQueryIsRelatedTo(isRelatedToJsonNode); jsonQueryIsRelatedTo.setRequestedResourceType(type); - jsonQueryIsRelatedTo.setEntryPoint(!initFound && i==0); + jsonQueryIsRelatedTo.setEntryPoint(entryPoint && !initFound && i==0); stringBuffer = jsonQueryIsRelatedTo.analize(stringBuffer); } initFound = true; // Must be set after the cycle and not before diff --git a/src/test/java/org/gcube/informationsystem/resourceregistry/query/query.json b/src/test/java/org/gcube/informationsystem/resourceregistry/query/query.json index 0b1edf2..44f5464 100644 --- a/src/test/java/org/gcube/informationsystem/resourceregistry/query/query.json +++ b/src/test/java/org/gcube/informationsystem/resourceregistry/query/query.json @@ -32,7 +32,16 @@ { "@class": "Activates", "source": { - "@class": "HostingNode" + "@class": "HostingNode", + "consistsOf": [ + { + "@class": "ConsistsOf", + "target": { + "@class": "CPUFacet", + "vendor": "GenuineIntel" + } + } + ] } } ]