Results are now post filtered to allow polymorphism

This commit is contained in:
Luca Frosini 2021-10-18 15:18:17 +02:00
parent 485b20ac05
commit 5486954750
2 changed files with 51 additions and 11 deletions

View File

@ -7,9 +7,12 @@ import java.util.Map;
import org.gcube.com.fasterxml.jackson.databind.JsonNode;
import org.gcube.com.fasterxml.jackson.databind.ObjectMapper;
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.base.reference.Element;
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;
import org.gcube.informationsystem.resourceregistry.api.rest.AccessPath;
import org.gcube.informationsystem.resourceregistry.contexts.ContextUtility;
@ -17,7 +20,8 @@ import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityCo
import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityContext.PermissionMode;
import org.gcube.informationsystem.resourceregistry.instances.base.ElementManagement;
import org.gcube.informationsystem.resourceregistry.instances.base.ElementManagementUtility;
import org.gcube.informationsystem.resourceregistry.instances.model.entities.ResourceManagement;
import org.gcube.informationsystem.resourceregistry.types.CachedType;
import org.gcube.informationsystem.resourceregistry.types.TypesCache;
import org.gcube.informationsystem.resourceregistry.utils.Utility;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -55,10 +59,42 @@ public class JsonQuery {
}
public StringBuffer createQuery() throws SchemaNotFoundException, InvalidQueryException {
entryPoint = new JsonQueryResource(jsonQuery);
public static JsonQueryERElement getJsonQueryERElement(JsonNode jsonQuery) throws SchemaNotFoundException, SchemaException, ResourceRegistryException {
String type = jsonQuery.get(Element.CLASS_PROPERTY).asText();
AccessType accessType = TypesCache.getInstance().getCachedType(type).getAccessType();
JsonQueryERElement jsonQueryERElement = null;
switch (accessType) {
case RESOURCE:
jsonQueryERElement = new JsonQueryResource(jsonQuery);
jsonQueryERElement.setDirection(Direction.OUT);
break;
case FACET:
jsonQueryERElement = new JsonQueryFacet(jsonQuery);
break;
case IS_RELATED_TO:
jsonQueryERElement = new JsonQueryIsRelatedTo(jsonQuery);
break;
case CONSISTS_OF:
jsonQueryERElement = new JsonQueryConsistsOf(jsonQuery);
break;
default:
throw new InvalidQueryException(String.format("%s is not querable", type.toString()));
}
return jsonQueryERElement;
}
public StringBuffer createQuery() throws SchemaException, InvalidQueryException, ResourceRegistryException {
entryPoint = getJsonQueryERElement(jsonQuery);
entryPoint.setEntryPoint(true);
entryPoint.setDirection(Direction.OUT);
return entryPoint.analize(new StringBuffer());
}
@ -94,17 +130,21 @@ public class JsonQuery {
// To support polymorphism we do not include @class="TypeName" in query. So we need post processing filtering of results
entryPoint.getType();
erManagement.getTypeName();
String requestedType = entryPoint.getType();
String gotType = erManagement.getTypeName();
if(erManagement instanceof ResourceManagement) {
if(requestedType.compareTo(gotType)==0) {
jsonNodeResult = erManagement.serializeAsJsonNode();
arrayNode.add(jsonNodeResult);
}else {
// Got relations and facets because the query does to support polymorphism
continue;
}
CachedType<?> cachedType = TypesCache.getInstance().getCachedType(gotType);
if(cachedType.getSuperTypes().contains(requestedType)) {
jsonNodeResult = erManagement.serializeAsJsonNode();
arrayNode.add(jsonNodeResult);
continue;
}
} catch(ResourceRegistryException e) {

View File

@ -80,7 +80,7 @@ public class JsonQueryTest extends ContextTest {
logger.info("Created Query from JSON: {}", createdStringBuffer.toString());
}
//@Test
@Test
public void testSingleQuery() throws Exception {
ContextTest.setContextByName(DEVVRE);
File queriesDirectory = getQueriesDirectory();