resource-registry/src/main/java/org/gcube/informationsystem/resourceregistry/utils/DBUtility.java

142 lines
5.4 KiB
Java
Raw Normal View History

package org.gcube.informationsystem.resourceregistry.utils;
import java.util.UUID;
2020-07-07 17:15:22 +02:00
import org.gcube.com.fasterxml.jackson.databind.JsonNode;
import org.gcube.com.fasterxml.jackson.databind.ObjectMapper;
2023-04-26 21:44:03 +02:00
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;
2020-01-27 17:07:37 +01:00
import org.gcube.informationsystem.resourceregistry.contexts.ContextUtility;
import org.gcube.informationsystem.resourceregistry.contexts.security.AdminSecurityContext;
import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityContext.PermissionMode;
2021-02-10 15:54:52 +01:00
import org.gcube.informationsystem.resourceregistry.instances.base.ElementManagementUtility;
2023-02-07 16:27:10 +01:00
import org.gcube.informationsystem.serialization.ElementMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
2023-05-11 18:35:56 +02:00
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)
*/
2023-05-11 18:35:56 +02:00
public class DBUtility {
2023-05-11 18:35:56 +02:00
private static final Logger logger = LoggerFactory.getLogger(DBUtility.class);
2023-04-26 21:44:03 +02:00
2023-05-11 18:35:56 +02:00
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.";
2023-05-11 18:35:56 +02:00
public static JsonNode toJsonNode(Document document) throws ResourceRegistryException {
try {
ObjectMapper objectMapper = new ObjectMapper();
2023-05-11 18:35:56 +02:00
String string = toJsonString(document);
2023-04-26 21:44:03 +02:00
ObjectNode objectNode = (ObjectNode) objectMapper.readTree(string);
return objectNode;
} catch(Exception e) {
throw new ResourceRegistryException(e);
}
}
2023-04-26 21:44:03 +02:00
public static String replaceType(String s) {
2023-05-11 18:35:56 +02:00
s = s.replace("\"" + DB_TYPE_PROPERTY + "\"", "\"" + Element.TYPE_PROPERTY + "\"");
2023-04-26 21:44:03 +02:00
return s;
}
2023-05-11 18:35:56 +02:00
public static String toJsonString(Document document) {
String ret = document.toJSON().toString();
2023-04-26 21:44:03 +02:00
// The above method set the type in @class property
// We want to use the property set in Element.TYPE_PROPERTY
2023-04-26 21:44:03 +02:00
ret = replaceType(ret);
return ret;
}
2023-05-11 18:35:56 +02:00
public static String getAsStringForLogging(Document document) {
return document.toJSON().toString();
2023-04-26 21:44:03 +02:00
}
2023-05-11 18:35:56 +02:00
public static String getAsStringForException(Document document) {
return toJsonString(document);
}
2023-05-11 18:35:56 +02:00
public static <El extends Document> El getElementByUUIDAsAdmin(String elementType, UUID uuid,
Class<? extends El> clz) throws NotFoundException, ResourceRegistryException {
2023-05-11 18:35:56 +02:00
RemoteDatabase adminDatabase = null;
// RemoteDatabase current = ContextUtility.getCurrentODatabaseDocumentFromThreadLocal();
try {
AdminSecurityContext adminSecurityContext = ContextUtility.getAdminSecurityContext();
2023-05-11 18:35:56 +02:00
adminDatabase = adminSecurityContext.getRemoteDatabase(PermissionMode.READER);
return DBUtility.getElementByUUID(adminDatabase, elementType, uuid, clz);
} finally {
2023-05-11 18:35:56 +02:00
if(adminDatabase != null) {
adminDatabase.close();
}
2023-05-11 18:35:56 +02:00
// if(current!=null) {
// current.activateOnCurrentThread();
// }
}
}
2023-05-11 18:35:56 +02:00
public static <El extends Document> El getElementByUUID(RemoteDatabase database, String elementType, UUID uuid,
Class<? extends El> clz) throws NotFoundException, ResourceRegistryException {
if(elementType == null || elementType.compareTo("") == 0) {
2023-05-11 18:35:56 +02:00
if(Vertex.class.isAssignableFrom(clz)) {
elementType = Entity.NAME;
}
2023-05-11 18:35:56 +02:00
if(Edge.class.isAssignableFrom(clz)) {
elementType = Relation.NAME;
}
}
// TODO Rewrite using Gremlin
String select = "SELECT FROM " + elementType + " WHERE " + IdentifiableElement.ID_PROPERTY + " = \"" + uuid.toString() + "\"";
2023-05-11 18:35:56 +02:00
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());
2021-09-17 10:35:57 +02:00
logger.debug(error);
throw new NotFoundException(error);
}
2023-05-11 18:35:56 +02:00
Result result = resultSet.next();
@SuppressWarnings("unchecked")
2023-05-11 18:35:56 +02:00
El element = (El) ElementManagementUtility.getElementFromOptional(result.getElement());
2023-05-11 18:35:56 +02:00
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;
}
2023-05-11 18:35:56 +02:00
public static <P extends PropertyElement> P getPropertyDocument(Class<P> clz, Document element, String property)
throws ResourceRegistryException {
try {
2023-05-11 18:35:56 +02:00
MutableEmbeddedDocument document = (MutableEmbeddedDocument) element.getEmbedded(property);
P e = ElementMapper.unmarshal(clz, DBUtility.toJsonString(document));
return e;
} catch(Exception ex) {
2023-04-26 21:44:03 +02:00
String error = String.format("Error while getting %s from %s", property, getAsStringForException(element));
throw new ResourceRegistryException(error, ex);
}
}
}