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; package org.gcube.informationsystem.resourceregistry.query.json;
import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -33,26 +34,35 @@ public class JsonQuery {
private static Logger logger = LoggerFactory.getLogger(JsonQuery.class); private static Logger logger = LoggerFactory.getLogger(JsonQuery.class);
public StringBuffer createQuery(JsonNode jsonQuery) throws SchemaNotFoundException, InvalidQueryException { protected ObjectMapper objectMapper;
JsonQueryResource jsonQueryResource = new JsonQueryResource(jsonQuery); protected JsonNode jsonQuery;
jsonQueryResource.setEntryPoint(true); protected JsonQueryERElement entryPoint;
jsonQueryResource.setDirection(Direction.OUT);
return jsonQueryResource.analize(new StringBuffer()); 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 { try {
ObjectMapper objectMapper = new ObjectMapper(); this.jsonQuery = objectMapper.readTree(jsonQuery);
JsonNode jsonNode = objectMapper.readTree(jsonQuery); } catch (IOException e) {
return query(jsonNode, objectMapper); throw new InvalidQueryException(e);
} catch(ResourceRegistryException e) {
throw e;
} catch(Exception e) {
throw new InvalidQueryException(e.getMessage());
}
} }
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 current = ContextUtility.getCurrentODatabaseDocumentFromThreadLocal();
ODatabaseDocument oDatabaseDocument = null; ODatabaseDocument oDatabaseDocument = null;
try { try {
@ -63,7 +73,7 @@ public class JsonQuery {
oDatabaseDocument = securityContext.getDatabaseDocument(PermissionMode.READER); oDatabaseDocument = securityContext.getDatabaseDocument(PermissionMode.READER);
oDatabaseDocument.begin(); oDatabaseDocument.begin();
StringBuffer stringBuffer = createQuery(jsonQuery); StringBuffer stringBuffer = createQuery();
stringBuffer.append(" limit :limit"); stringBuffer.append(" limit :limit");
Map<String, Object> map = new HashMap<>(); Map<String, Object> map = new HashMap<>();
@ -81,13 +91,22 @@ public class JsonQuery {
JsonNode jsonNodeResult = null; JsonNode jsonNodeResult = null;
ElementManagement<?,?> erManagement = ElementManagementUtility.getERManagement(securityContext, oDatabaseDocument, ElementManagement<?,?> erManagement = ElementManagementUtility.getERManagement(securityContext, oDatabaseDocument,
element); 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) { if(erManagement instanceof ResourceManagement) {
jsonNodeResult = erManagement.serializeAsJsonNode(); jsonNodeResult = erManagement.serializeAsJsonNode();
arrayNode.add(jsonNodeResult); arrayNode.add(jsonNodeResult);
}else { }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) { } catch(ResourceRegistryException e) {
logger.error("Unable to correctly serialize {}. It will be excluded from results. {}", logger.error("Unable to correctly serialize {}. It will be excluded from results. {}",
element.toString(), Utility.SHOULD_NOT_OCCUR_ERROR_MESSAGE); element.toString(), Utility.SHOULD_NOT_OCCUR_ERROR_MESSAGE);

View File

@ -305,7 +305,8 @@ public class Access extends BaseRest {
checkIncludeInstancesContexts(); checkIncludeInstancesContexts();
JsonQuery jsonQueryManager = new JsonQuery(); 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 @Test
public void testCreateQuery() throws Exception { public void testJsonQueries() throws Exception {
ContextTest.setContextByName(DEVVRE);
File queriesDirectory = getQueriesDirectory(); File queriesDirectory = getQueriesDirectory();
for(int i=1; i<4; i++) { 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()); logger.info("Going to test the following JSON query {}", jsonNode.toString());
JsonQuery jsonQuery = new JsonQuery(); JsonQuery jsonQuery = new JsonQuery();
StringBuffer createdStringBuffer = jsonQuery.createQuery(jsonNode); jsonQuery.setJsonQuery(jsonNode);
StringBuffer createdStringBuffer = jsonQuery.createQuery();
logger.info("Created Query from JSON: {}", createdStringBuffer.toString()); 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); Assert.assertTrue(createdStringBuffer.toString().compareTo(expectedStringBuffer.toString())==0);
String result = jsonQuery.query();
logger.info("Result : {}", result);
} }
} }
@Test //@Test
public void testQuery() throws Exception { public void testSingleCreateQuery() throws Exception {
ContextTest.setContextByName(DEVVRE); File queriesDirectory = getQueriesDirectory();
URL url = JsonQueryTest.class.getResource("query.json"); File jsonQueryFile = new File(queriesDirectory, "query3.json");
File queryFile = new File(url.toURI());
ObjectMapper objectMapper = new ObjectMapper(); 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()); logger.info("Going to test the following JSON query {}", jsonNode.toString());
JsonQuery jsonQuery = new JsonQuery(); 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); logger.info(res);
} }