Added the possibility of getting a facet as result

This commit is contained in:
Luca Frosini 2021-10-19 12:56:45 +02:00
parent c8d9007121
commit e810a6c06d
7 changed files with 63 additions and 38 deletions

View File

@ -31,11 +31,13 @@ public class JsonQueryConsistsOf extends JsonQueryERElement{
protected StringBuffer analize(StringBuffer stringBuffer) throws SchemaException, ResourceRegistryException { protected StringBuffer analize(StringBuffer stringBuffer) throws SchemaException, ResourceRegistryException {
StringBuffer consistsOfBuffer = new StringBuffer(); StringBuffer consistsOfBuffer = new StringBuffer();
if(!jsonNode.has(ConsistsOf.SOURCE_PROPERTY)) {
consistsOfBuffer.append("TRAVERSE "); consistsOfBuffer.append("TRAVERSE ");
consistsOfBuffer.append(direction.opposite().name().toLowerCase()); consistsOfBuffer.append(direction.opposite().name().toLowerCase());
consistsOfBuffer.append("V(\""); consistsOfBuffer.append("V(\"");
consistsOfBuffer.append(requestedResourceType); consistsOfBuffer.append(requestedResourceType);
consistsOfBuffer.append("\") FROM ( "); // Open ( 1 consistsOfBuffer.append("\") FROM ( "); // Open ( 1
}
int size = jsonNode.size(); int size = jsonNode.size();
if(size > 2) { if(size > 2) {
@ -60,10 +62,17 @@ public class JsonQueryConsistsOf extends JsonQueryERElement{
stringBuffer = buffer; stringBuffer = buffer;
} }
if(jsonNode.has(ConsistsOf.TARGET_PROPERTY)) {
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); stringBuffer = jsonQueryFacet.analize(stringBuffer);
} else if(jsonNode.has(ConsistsOf.SOURCE_PROPERTY)) {
JsonNode resourceJsonNode = jsonNode.get(ConsistsOf.SOURCE_PROPERTY);
JsonQueryResource jsonQueryResource = new JsonQueryResource(resourceJsonNode);
jsonQueryResource.setEntryPoint(entryPoint);
stringBuffer = jsonQueryResource.analize(stringBuffer);
}
consistsOfBuffer.append(stringBuffer); consistsOfBuffer.append(stringBuffer);
consistsOfBuffer.append(")"); // Close ) 2 consistsOfBuffer.append(")"); // Close ) 2
@ -74,7 +83,9 @@ public class JsonQueryConsistsOf extends JsonQueryERElement{
addWhereConstraint(jsonNode, consistsOfBuffer, null); addWhereConstraint(jsonNode, consistsOfBuffer, null);
} }
if(!jsonNode.has(ConsistsOf.SOURCE_PROPERTY)) {
consistsOfBuffer.append(")"); // Close ) 1 consistsOfBuffer.append(")"); // Close ) 1
}
return consistsOfBuffer; return consistsOfBuffer;

View File

@ -12,6 +12,7 @@ import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegis
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.SchemaException; import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaNotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaNotFoundException;
import org.gcube.informationsystem.resourceregistry.instances.base.ElementManagement;
import org.gcube.informationsystem.resourceregistry.types.TypesCache; import org.gcube.informationsystem.resourceregistry.types.TypesCache;
public abstract class JsonQueryERElement { public abstract class JsonQueryERElement {
@ -90,6 +91,10 @@ public abstract class JsonQueryERElement {
continue; continue;
} }
if(fieldName.startsWith(ElementManagement.UNDERSCORE)) {
continue;
}
if(first) { if(first) {
first = false; first = false;
}else { }else {

View File

@ -2,35 +2,50 @@ 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.resourceregistry.api.exceptions.ResourceRegistryException; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
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.SchemaException; import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaNotFoundException;
/** /**
* @author Luca Frosini (ISTI - CNR) * @author Luca Frosini (ISTI - CNR)
*/ */
public class JsonQueryFacet extends JsonQueryERElement{ public class JsonQueryFacet extends JsonQueryERElement{
public final static String _IN = "_in";
public JsonQueryFacet(JsonNode jsonQuery) throws SchemaException, ResourceRegistryException { public JsonQueryFacet(JsonNode jsonQuery) throws SchemaException, ResourceRegistryException {
super(jsonQuery, AccessType.FACET); super(jsonQuery, AccessType.FACET);
} }
@Override @Override
protected StringBuffer analize(StringBuffer stringBuffer) throws SchemaNotFoundException, InvalidQueryException { protected StringBuffer analize(StringBuffer stringBuffer) throws SchemaException, ResourceRegistryException {
StringBuffer newBuffer = new StringBuffer(); StringBuffer newBuffer = new StringBuffer();
boolean entry = entryPoint;
if(jsonNode.has(_IN)) {
if(!entryPoint) {
throw new InvalidQueryException(_IN + " property cannot be used in a facet if it is not the entry object");
}
JsonNode consistsOfNode = jsonNode.get(_IN);
JsonQueryConsistsOf jsonQueryConsistsOf = new JsonQueryConsistsOf(consistsOfNode);
jsonQueryConsistsOf.setEntryPoint(entryPoint);
jsonQueryConsistsOf.setDirection(Direction.OUT);
stringBuffer = jsonQueryConsistsOf.analize(stringBuffer);
entry = false;
}
int size = jsonNode.size(); int size = jsonNode.size();
newBuffer.append("SELECT FROM "); newBuffer.append("SELECT FROM ");
if(!entryPoint) { if(!entry) {
newBuffer.append("( "); newBuffer.append("( ");
newBuffer.append("TRAVERSE inV(\""); newBuffer.append("TRAVERSE inV(\"");
} }
newBuffer.append(type); newBuffer.append(type);
if(!entryPoint) { if(!entry) {
newBuffer.append("\") FROM ( "); newBuffer.append("\") FROM ( ");
newBuffer.append(stringBuffer); newBuffer.append(stringBuffer);
newBuffer.append(")"); newBuffer.append(")");

View File

@ -20,7 +20,6 @@ public class JsonQueryResource extends JsonQueryERElement {
@Override @Override
protected StringBuffer analize(StringBuffer stringBuffer) throws SchemaException, ResourceRegistryException { protected StringBuffer analize(StringBuffer stringBuffer) throws SchemaException, ResourceRegistryException {
if(!entryPoint) { if(!entryPoint) {
StringBuffer newBuffer = new StringBuffer(); StringBuffer newBuffer = new StringBuffer();
newBuffer.append("TRAVERSE "); newBuffer.append("TRAVERSE ");
@ -62,30 +61,13 @@ public class JsonQueryResource extends JsonQueryERElement {
initFound = true; // Must be set after the cycle and not before initFound = true; // Must be set after the cycle and not before
} }
if(!initFound) { if(!initFound) {
if(entryPoint) { if(entryPoint) {
stringBuffer = new StringBuffer(); stringBuffer = new StringBuffer();
stringBuffer.append("SELECT FROM "); stringBuffer.append("SELECT FROM ");
stringBuffer.append(type); stringBuffer.append(type);
if(jsonNode.has(IdentifiableElement.HEADER_PROPERTY)) {
} }
} }
// else {
// StringBuffer newBuffer = new StringBuffer();
// newBuffer.append("TRAVERSE ");
// newBuffer.append(direction.name().toLowerCase());
// newBuffer.append("V(\"");
// newBuffer.append(type);
// newBuffer.append("\") FROM ( ");
// newBuffer.append(stringBuffer);
// newBuffer.append(")");
// stringBuffer = newBuffer;
// }
}
if(jsonNode.has(IdentifiableElement.HEADER_PROPERTY)) { if(jsonNode.has(IdentifiableElement.HEADER_PROPERTY)) {
StringBuffer newBuffer = new StringBuffer(); StringBuffer newBuffer = new StringBuffer();

View File

@ -28,14 +28,13 @@ public class JsonQueryTest extends ContextTest {
return new File(resourcesDirectory, "queries"); return new File(resourcesDirectory, "queries");
} }
@Test @Test
public void testJsonQueries() throws Exception { public void testJsonQueries() throws Exception {
ContextTest.setContextByName(DEVVRE); ContextTest.setContextByName(DEVVRE);
File queriesDirectory = getQueriesDirectory(); File queriesDirectory = getQueriesDirectory();
for(int i=1; i<4; i++) { for(int i=1; i<5; i++) {
File jsonQueryFile = new File(queriesDirectory, "query" + i + ".json"); File jsonQueryFile = new File(queriesDirectory, "query" + i + ".json");
ObjectMapper objectMapper = new ObjectMapper(); ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(jsonQueryFile); JsonNode jsonNode = objectMapper.readTree(jsonQueryFile);
@ -62,13 +61,12 @@ public class JsonQueryTest extends ContextTest {
String result = jsonQuery.query(); String result = jsonQuery.query();
logger.info("Result : {}", result); logger.info("Result : {}", result);
} }
} }
@Test // @Test
public void testSingleCreateQuery() throws Exception { public void testSingleCreateQuery() throws Exception {
File queriesDirectory = getQueriesDirectory(); File queriesDirectory = getQueriesDirectory();
File jsonQueryFile = new File(queriesDirectory, "facet.json"); File jsonQueryFile = new File(queriesDirectory, "query4.json");
ObjectMapper objectMapper = new ObjectMapper(); ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(jsonQueryFile); JsonNode jsonNode = objectMapper.readTree(jsonQueryFile);
logger.info("Going to test the following JSON query {}", jsonNode.toString()); logger.info("Going to test the following JSON query {}", jsonNode.toString());

View File

@ -0,0 +1,13 @@
{
"@class": "StateFacet",
"value": "down",
"_in": {
"@class": "ConsistsOf",
"source" : {
"@class" : "EService",
"header": {
"uuid": "0255b7ec-e3da-4071-b456-9a2907ece1db"
}
}
}
}

View File

@ -0,0 +1 @@
SELECT FROM ( TRAVERSE inV("StateFacet") FROM ( TRAVERSE outE("ConsistsOf") FROM ( SELECT FROM ( SELECT FROM EService) WHERE header.uuid="0255b7ec-e3da-4071-b456-9a2907ece1db"))) WHERE value="down"