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 {
StringBuffer consistsOfBuffer = new StringBuffer();
consistsOfBuffer.append("TRAVERSE ");
consistsOfBuffer.append(direction.opposite().name().toLowerCase());
consistsOfBuffer.append("V(\"");
consistsOfBuffer.append(requestedResourceType);
consistsOfBuffer.append("\") FROM ( "); // Open ( 1
if(!jsonNode.has(ConsistsOf.SOURCE_PROPERTY)) {
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) {
@ -60,11 +62,18 @@ public class JsonQueryConsistsOf extends JsonQueryERElement{
stringBuffer = buffer;
}
JsonNode facetJsonNode = jsonNode.get(ConsistsOf.TARGET_PROPERTY);
JsonQueryFacet jsonQueryFacet = new JsonQueryFacet(facetJsonNode);
jsonQueryFacet.setEntryPoint(entryPoint);
stringBuffer = jsonQueryFacet.analize(stringBuffer);
if(jsonNode.has(ConsistsOf.TARGET_PROPERTY)) {
JsonNode facetJsonNode = jsonNode.get(ConsistsOf.TARGET_PROPERTY);
JsonQueryFacet jsonQueryFacet = new JsonQueryFacet(facetJsonNode);
jsonQueryFacet.setEntryPoint(entryPoint);
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(")"); // Close ) 2
@ -74,7 +83,9 @@ public class JsonQueryConsistsOf extends JsonQueryERElement{
addWhereConstraint(jsonNode, consistsOfBuffer, null);
}
consistsOfBuffer.append(")"); // Close ) 1
if(!jsonNode.has(ConsistsOf.SOURCE_PROPERTY)) {
consistsOfBuffer.append(")"); // Close ) 1
}
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.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 {
@ -90,6 +91,10 @@ public abstract class JsonQueryERElement {
continue;
}
if(fieldName.startsWith(ElementManagement.UNDERSCORE)) {
continue;
}
if(first) {
first = false;
}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.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.query.InvalidQueryException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaNotFoundException;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class JsonQueryFacet extends JsonQueryERElement{
public final static String _IN = "_in";
public JsonQueryFacet(JsonNode jsonQuery) throws SchemaException, ResourceRegistryException {
super(jsonQuery, AccessType.FACET);
}
@Override
protected StringBuffer analize(StringBuffer stringBuffer) throws SchemaNotFoundException, InvalidQueryException {
protected StringBuffer analize(StringBuffer stringBuffer) throws SchemaException, ResourceRegistryException {
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();
newBuffer.append("SELECT FROM ");
if(!entryPoint) {
if(!entry) {
newBuffer.append("( ");
newBuffer.append("TRAVERSE inV(\"");
}
newBuffer.append(type);
if(!entryPoint) {
if(!entry) {
newBuffer.append("\") FROM ( ");
newBuffer.append(stringBuffer);
newBuffer.append(")");

View File

@ -20,7 +20,6 @@ public class JsonQueryResource extends JsonQueryERElement {
@Override
protected StringBuffer analize(StringBuffer stringBuffer) throws SchemaException, ResourceRegistryException {
if(!entryPoint) {
StringBuffer newBuffer = new StringBuffer();
newBuffer.append("TRAVERSE ");
@ -62,31 +61,14 @@ public class JsonQueryResource extends JsonQueryERElement {
initFound = true; // Must be set after the cycle and not before
}
if(!initFound) {
if(entryPoint) {
stringBuffer = new StringBuffer();
stringBuffer.append("SELECT FROM ");
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)) {
StringBuffer newBuffer = new StringBuffer();
newBuffer.append("SELECT FROM ( ");

View File

@ -28,14 +28,13 @@ public class JsonQueryTest extends ContextTest {
return new File(resourcesDirectory, "queries");
}
@Test
public void testJsonQueries() throws Exception {
ContextTest.setContextByName(DEVVRE);
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");
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(jsonQueryFile);
@ -62,13 +61,12 @@ public class JsonQueryTest extends ContextTest {
String result = jsonQuery.query();
logger.info("Result : {}", result);
}
}
@Test
// @Test
public void testSingleCreateQuery() throws Exception {
File queriesDirectory = getQueriesDirectory();
File jsonQueryFile = new File(queriesDirectory, "facet.json");
File jsonQueryFile = new File(queriesDirectory, "query4.json");
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(jsonQueryFile);
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"