Fixed projection

This commit is contained in:
luca.frosini 2023-11-29 14:26:02 +01:00
parent c324b3ab41
commit 30e8c84258
4 changed files with 54 additions and 17 deletions

View File

@ -2,11 +2,14 @@ package org.gcube.informationsystem.resourceregistry.queries.json;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
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.com.fasterxml.jackson.databind.node.ObjectNode;
import org.gcube.informationsystem.base.reference.AccessType;
import org.gcube.informationsystem.base.reference.Direction;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
@ -145,22 +148,52 @@ public class JsonQuery {
ArrayNode arrayNode = objectMapper.createArrayNode();
boolean projection = entryPoint.isProjection();
boolean first = true;
Set<String> keys = new HashSet<>();
while(resultSet.hasNext()) {
OResult oResult = resultSet.next();
OElement element = ElementManagementUtility.getElementFromOptional(oResult.getElement());
if(projection) {
if(first) {
keys = oResult.getPropertyNames();
first = false;
}
ObjectNode objectNode = objectMapper.createObjectNode();
for(String key : keys) {
Object value = oResult.getProperty(key);
if(value == null) {
objectNode.put(key, "");
}else if(value instanceof String) {
objectNode.put(key, (String) value);
}else if(value instanceof Integer) {
objectNode.put(key, (Integer) value);
}else if(value instanceof Long) {
objectNode.put(key, (Long) value);
}else {
objectNode.put(key, value.toString());
}
}
arrayNode.add(objectNode);
}else {
OElement element = ElementManagementUtility.getElementFromOptional(oResult.getElement());
try {
JsonNode jsonNodeResult = null;
ElementManagement<?,?> erManagement = ElementManagementUtility.getERManagement(securityContext, oDatabaseDocument,
element);
erManagement.setAsEntryPoint();
jsonNodeResult = erManagement.serializeAsJsonNode();
arrayNode.add(jsonNodeResult);
} catch(ResourceRegistryException e) {
logger.error("Unable to correctly serialize {}. It will be excluded from results. {}",
element.toString(), OrientDBUtility.SHOULD_NOT_OCCUR_ERROR_MESSAGE);
try {
JsonNode jsonNodeResult = null;
ElementManagement<?,?> erManagement = ElementManagementUtility.getERManagement(securityContext, oDatabaseDocument,
element);
erManagement.setAsEntryPoint();
jsonNodeResult = erManagement.serializeAsJsonNode();
arrayNode.add(jsonNodeResult);
} catch(ResourceRegistryException e) {
logger.error("Unable to correctly serialize {}. It will be excluded from results. {}",
element.toString(), OrientDBUtility.SHOULD_NOT_OCCUR_ERROR_MESSAGE);
}
}
}

View File

@ -291,6 +291,10 @@ public abstract class JsonQueryERElement {
StringBuffer b = new StringBuffer();
b.append(getAlias(true));
b.append(".");
if(fieldNamePrefix !=null) {
b.append(fieldNamePrefix);
b.append(".");
}
b.append(fieldNameToEmit);
b.append(" AS `");
b.append(nameOfFieldToEmit);

View File

@ -250,8 +250,8 @@ public class JsonQueryTest extends ContextTest {
Assert.assertTrue(compareQueries(createdStringBuffer, expectedStringBuffer));
// String result = jsonQuery.query();
// logger.info("Result : {}", result);
String result = jsonQuery.query();
logger.info("Result : {}", result);
}
@Test
@ -287,8 +287,8 @@ public class JsonQueryTest extends ContextTest {
logger.info("Expected Query from JSON: {}", expectedStringBuffer.toString());
Assert.assertTrue(compareQueries(createdStringBuffer, expectedStringBuffer));
//String result = jsonQuery.query();
//logger.info("Result : {}", result);
String result = jsonQuery.query();
logger.info("Result : {}", result);
}
}
}

View File

@ -27,4 +27,4 @@ RETURN
((memoryfacet020.size - memoryfacet020.used) + ' ' + memoryfacet020.unit) AS `HD Space Left`,
(memoryfacet030.size + ' ' + memoryfacet030.unit) AS `Mem. Available.`,
hostingnode0.id AS `ID`,
hostingnode0.lastUpdateTime AS `Last Update Time`
hostingnode0.metadata.lastUpdateTime AS `Last Update Time`