Starting to support results different from Resources

This commit is contained in:
Luca Frosini 2021-10-18 12:24:57 +02:00
parent 49c361a342
commit 485b20ac05
3 changed files with 67 additions and 26 deletions

View File

@ -1,5 +1,6 @@
package org.gcube.informationsystem.resourceregistry.query.json;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
@ -33,26 +34,35 @@ public class JsonQuery {
private static Logger logger = LoggerFactory.getLogger(JsonQuery.class);
public StringBuffer createQuery(JsonNode jsonQuery) throws SchemaNotFoundException, InvalidQueryException {
JsonQueryResource jsonQueryResource = new JsonQueryResource(jsonQuery);
jsonQueryResource.setEntryPoint(true);
jsonQueryResource.setDirection(Direction.OUT);
return jsonQueryResource.analize(new StringBuffer());
protected ObjectMapper objectMapper;
protected JsonNode jsonQuery;
protected JsonQueryERElement entryPoint;
public JsonQuery() {
this.objectMapper = new ObjectMapper();
}
public String query(String jsonQuery) throws InvalidQueryException, ResourceRegistryException {
public void setJsonQuery(JsonNode jsonQuery) {
this.jsonQuery = jsonQuery;
}
public void setJsonQuery(String jsonQuery) throws InvalidQueryException {
try {
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(jsonQuery);
return query(jsonNode, objectMapper);
} catch(ResourceRegistryException e) {
throw e;
} catch(Exception e) {
throw new InvalidQueryException(e.getMessage());
this.jsonQuery = objectMapper.readTree(jsonQuery);
} catch (IOException e) {
throw new InvalidQueryException(e);
}
}
public String query(JsonNode jsonQuery, ObjectMapper objectMapper) throws InvalidQueryException, ResourceRegistryException {
public StringBuffer createQuery() throws SchemaNotFoundException, InvalidQueryException {
entryPoint = new JsonQueryResource(jsonQuery);
entryPoint.setEntryPoint(true);
entryPoint.setDirection(Direction.OUT);
return entryPoint.analize(new StringBuffer());
}
public String query() throws InvalidQueryException, ResourceRegistryException {
ODatabaseDocument current = ContextUtility.getCurrentODatabaseDocumentFromThreadLocal();
ODatabaseDocument oDatabaseDocument = null;
try {
@ -63,7 +73,7 @@ public class JsonQuery {
oDatabaseDocument = securityContext.getDatabaseDocument(PermissionMode.READER);
oDatabaseDocument.begin();
StringBuffer stringBuffer = createQuery(jsonQuery);
StringBuffer stringBuffer = createQuery();
stringBuffer.append(" limit :limit");
Map<String, Object> map = new HashMap<>();
@ -81,13 +91,22 @@ public class JsonQuery {
JsonNode jsonNodeResult = null;
ElementManagement<?,?> erManagement = ElementManagementUtility.getERManagement(securityContext, oDatabaseDocument,
element);
// To support polymorphism we do not include @class="TypeName" in query. So we need post processing filtering of results
entryPoint.getType();
erManagement.getTypeName();
if(erManagement instanceof ResourceManagement) {
jsonNodeResult = erManagement.serializeAsJsonNode();
arrayNode.add(jsonNodeResult);
}else {
// Got relations and facets because the query does not include @class="TypeName" to support polymorphism
// Got relations and facets because the query does to support polymorphism
}
} catch(ResourceRegistryException e) {
logger.error("Unable to correctly serialize {}. It will be excluded from results. {}",
element.toString(), Utility.SHOULD_NOT_OCCUR_ERROR_MESSAGE);

View File

@ -305,7 +305,8 @@ public class Access extends BaseRest {
checkIncludeInstancesContexts();
JsonQuery jsonQueryManager = new JsonQuery();
return jsonQueryManager.query(jsonQuery);
jsonQueryManager.setJsonQuery(jsonQuery);
return jsonQueryManager.query();
}
/*

View File

@ -30,7 +30,9 @@ public class JsonQueryTest extends ContextTest {
@Test
public void testCreateQuery() throws Exception {
public void testJsonQueries() throws Exception {
ContextTest.setContextByName(DEVVRE);
File queriesDirectory = getQueriesDirectory();
for(int i=1; i<4; i++) {
@ -40,7 +42,8 @@ public class JsonQueryTest extends ContextTest {
logger.info("Going to test the following JSON query {}", jsonNode.toString());
JsonQuery jsonQuery = new JsonQuery();
StringBuffer createdStringBuffer = jsonQuery.createQuery(jsonNode);
jsonQuery.setJsonQuery(jsonNode);
StringBuffer createdStringBuffer = jsonQuery.createQuery();
logger.info("Created Query from JSON: {}", createdStringBuffer.toString());
@ -56,21 +59,39 @@ public class JsonQueryTest extends ContextTest {
Assert.assertTrue(createdStringBuffer.toString().compareTo(expectedStringBuffer.toString())==0);
String result = jsonQuery.query();
logger.info("Result : {}", result);
}
}
@Test
public void testQuery() throws Exception {
ContextTest.setContextByName(DEVVRE);
URL url = JsonQueryTest.class.getResource("query.json");
File queryFile = new File(url.toURI());
//@Test
public void testSingleCreateQuery() throws Exception {
File queriesDirectory = getQueriesDirectory();
File jsonQueryFile = new File(queriesDirectory, "query3.json");
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(queryFile);
JsonNode jsonNode = objectMapper.readTree(jsonQueryFile);
logger.info("Going to test the following JSON query {}", jsonNode.toString());
JsonQuery jsonQuery = new JsonQuery();
String res = jsonQuery.query(jsonNode, objectMapper);
jsonQuery.setJsonQuery(jsonNode);
StringBuffer createdStringBuffer = jsonQuery.createQuery();
logger.info("Created Query from JSON: {}", createdStringBuffer.toString());
}
//@Test
public void testSingleQuery() throws Exception {
ContextTest.setContextByName(DEVVRE);
File queriesDirectory = getQueriesDirectory();
File jsonQueryFile = new File(queriesDirectory, "query3.json");
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(jsonQueryFile);
logger.info("Going to test the following JSON query {}", jsonNode.toString());
JsonQuery jsonQuery = new JsonQuery();
jsonQuery.setJsonQuery(jsonNode);
String res = jsonQuery.query();
logger.info(res);
}