package org.gcube.informationsystem.resourceregistry.utils; import java.util.UUID; import org.gcube.com.fasterxml.jackson.databind.JsonNode; import org.gcube.com.fasterxml.jackson.databind.ObjectMapper; import org.gcube.com.fasterxml.jackson.databind.node.ObjectNode; import org.gcube.informationsystem.base.reference.Element; import org.gcube.informationsystem.base.reference.IdentifiableElement; import org.gcube.informationsystem.base.reference.properties.PropertyElement; import org.gcube.informationsystem.model.reference.entities.Entity; import org.gcube.informationsystem.model.reference.relations.Relation; import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; import org.gcube.informationsystem.resourceregistry.contexts.ContextUtility; import org.gcube.informationsystem.resourceregistry.contexts.security.AdminSecurityContext; import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityContext.PermissionMode; import org.gcube.informationsystem.resourceregistry.instances.base.ElementManagementUtility; import org.gcube.informationsystem.serialization.ElementMapper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.arcadedb.database.Document; import com.arcadedb.database.MutableEmbeddedDocument; import com.arcadedb.graph.Edge; import com.arcadedb.graph.Vertex; 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 DBUtility { private static final Logger logger = LoggerFactory.getLogger(DBUtility.class); public static final String DB_TYPE_PROPERTY = "@type"; public static final String SHOULD_NOT_OCCUR_ERROR_MESSAGE = "This is really strange and should not occur. Please contact the system administrator."; public static JsonNode toJsonNode(Document document) throws ResourceRegistryException { try { ObjectMapper objectMapper = new ObjectMapper(); String string = toJsonString(document); ObjectNode objectNode = (ObjectNode) objectMapper.readTree(string); return objectNode; } catch(Exception e) { throw new ResourceRegistryException(e); } } public static String replaceType(String s) { s = s.replace("\"" + DB_TYPE_PROPERTY + "\"", "\"" + Element.TYPE_PROPERTY + "\""); return s; } public static String toJsonString(Document document) { String ret = document.toJSON().toString(); // The above method set the type in @class property // We want to use the property set in Element.TYPE_PROPERTY ret = replaceType(ret); return ret; } public static String getAsStringForLogging(Document document) { return document.toJSON().toString(); } public static String getAsStringForException(Document document) { return toJsonString(document); } public static El getElementByUUIDAsAdmin(String elementType, UUID uuid, Class clz) throws NotFoundException, ResourceRegistryException { RemoteDatabase adminDatabase = null; // RemoteDatabase current = ContextUtility.getCurrentODatabaseDocumentFromThreadLocal(); try { AdminSecurityContext adminSecurityContext = ContextUtility.getAdminSecurityContext(); adminDatabase = adminSecurityContext.getRemoteDatabase(PermissionMode.READER); return DBUtility.getElementByUUID(adminDatabase, elementType, uuid, clz); } finally { if(adminDatabase != null) { adminDatabase.close(); } // if(current!=null) { // current.activateOnCurrentThread(); // } } } public static El getElementByUUID(RemoteDatabase database, String elementType, UUID uuid, Class clz) throws NotFoundException, ResourceRegistryException { if(elementType == null || elementType.compareTo("") == 0) { if(Vertex.class.isAssignableFrom(clz)) { elementType = Entity.NAME; } if(Edge.class.isAssignableFrom(clz)) { elementType = Relation.NAME; } } // TODO Rewrite using Gremlin String select = "SELECT FROM " + elementType + " WHERE " + IdentifiableElement.ID_PROPERTY + " = \"" + uuid.toString() + "\""; ResultSet resultSet = database.command("sql", select); if(resultSet == null || !resultSet.hasNext()) { String error = String.format("No %s with UUID %s was found", elementType, uuid.toString()); logger.debug(error); throw new NotFoundException(error); } Result result = resultSet.next(); @SuppressWarnings("unchecked") El element = (El) ElementManagementUtility.getElementFromOptional(result.getElement()); logger.trace("{} with id {} is : {}", elementType, uuid.toString(), DBUtility.getAsStringForLogging(element)); if(resultSet.hasNext()) { throw new ResourceRegistryException("Found more than one " + elementType + " with uuid " + uuid.toString() + ". This is a fatal error please contact Admnistrator"); } return element; } public static

P getPropertyDocument(Class

clz, Document element, String property) throws ResourceRegistryException { try { MutableEmbeddedDocument document = (MutableEmbeddedDocument) element.getEmbedded(property); P e = ElementMapper.unmarshal(clz, DBUtility.toJsonString(document)); return e; } catch(Exception ex) { String error = String.format("Error while getting %s from %s", property, getAsStringForException(element)); throw new ResourceRegistryException(error, ex); } } }