91 lines
3.3 KiB
Java
91 lines
3.3 KiB
Java
package org.gcube.informationsystem.resourceregistry.queries;
|
|
|
|
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.resourceregistry.api.exceptions.ResourceRegistryException;
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.queries.InvalidQueryException;
|
|
import org.gcube.informationsystem.resourceregistry.contexts.ContextUtility;
|
|
import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityContext;
|
|
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.utils.DBUtility;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import com.arcadedb.database.Document;
|
|
import com.arcadedb.query.sql.executor.Result;
|
|
import com.arcadedb.query.sql.executor.ResultSet;
|
|
import com.arcadedb.remote.RemoteDatabase;
|
|
|
|
/**
|
|
* @author Luca Frosini (ISTI - CNR)
|
|
*/
|
|
public class QueryImpl implements Query {
|
|
|
|
private static Logger logger = LoggerFactory.getLogger(QueryImpl.class);
|
|
|
|
@Override
|
|
public String query(String query, boolean raw) throws InvalidQueryException {
|
|
RemoteDatabase oDatabaseDocument = null;
|
|
RemoteDatabase current = ContextUtility.getCurrentODatabaseDocumentFromThreadLocal();
|
|
|
|
try {
|
|
SecurityContext securityContext = ContextUtility.getCurrentSecurityContext();
|
|
|
|
oDatabaseDocument = securityContext.getRemoteDatabase(PermissionMode.READER);
|
|
oDatabaseDocument.begin();
|
|
|
|
logger.debug("Going to execute query '{} limit {}'", query);
|
|
|
|
ResultSet resultSet = oDatabaseDocument.command("sql", query);
|
|
|
|
ObjectMapper objectMapper = new ObjectMapper();
|
|
ArrayNode arrayNode = objectMapper.createArrayNode();
|
|
|
|
while(resultSet.hasNext()) {
|
|
Result result = resultSet.next();
|
|
|
|
try {
|
|
JsonNode jsonNode = null;
|
|
if(raw) {
|
|
if(result.isElement()) {
|
|
Document element = ElementManagementUtility.getElementFromOptional(result.getElement());
|
|
jsonNode = DBUtility.toJsonNode(element);
|
|
}else {
|
|
ObjectMapper mapper = new ObjectMapper();
|
|
jsonNode = mapper.readTree(DBUtility.toJsonString(result));
|
|
}
|
|
} else {
|
|
Document element = ElementManagementUtility.getElementFromOptional(result.getElement());
|
|
ElementManagement<?,?> erManagement = ElementManagementUtility.getERManagement(securityContext, oDatabaseDocument,
|
|
element);
|
|
erManagement.setAsEntryPoint();
|
|
jsonNode = erManagement.serializeAsJsonNode();
|
|
}
|
|
arrayNode.add(jsonNode);
|
|
|
|
} catch(ResourceRegistryException e) {
|
|
logger.error("Unable to correctly serialize {}. It will be excluded from results. {}",
|
|
DBUtility.toJsonString(result), DBUtility.SHOULD_NOT_OCCUR_ERROR_MESSAGE);
|
|
}
|
|
}
|
|
|
|
return objectMapper.writeValueAsString(arrayNode);
|
|
|
|
} catch(Exception e) {
|
|
throw new InvalidQueryException(e.getMessage());
|
|
} finally {
|
|
if(oDatabaseDocument != null) {
|
|
oDatabaseDocument.close();
|
|
}
|
|
// if(current!=null) {
|
|
// current.activateOnCurrentThread();
|
|
// }
|
|
}
|
|
|
|
}
|
|
|
|
}
|