2016-12-22 17:27:26 +01:00
|
|
|
package org.gcube.informationsystem.resourceregistry.utils;
|
2016-07-18 11:57:12 +02:00
|
|
|
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.Set;
|
2016-10-27 17:46:09 +02:00
|
|
|
import java.util.UUID;
|
2016-07-18 11:57:12 +02:00
|
|
|
|
|
|
|
import org.codehaus.jettison.json.JSONException;
|
2016-09-16 15:35:44 +02:00
|
|
|
import org.codehaus.jettison.json.JSONObject;
|
2018-10-02 09:17:58 +02:00
|
|
|
import org.gcube.informationsystem.model.impl.utils.ISMapper;
|
|
|
|
import org.gcube.informationsystem.model.reference.embedded.Embedded;
|
|
|
|
import org.gcube.informationsystem.model.reference.embedded.Header;
|
|
|
|
import org.gcube.informationsystem.model.reference.entity.Entity;
|
|
|
|
import org.gcube.informationsystem.model.reference.relation.Relation;
|
2018-06-04 15:31:13 +02:00
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFoundException;
|
2016-07-18 11:57:12 +02:00
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
|
2017-11-15 19:31:49 +01:00
|
|
|
import org.gcube.informationsystem.resourceregistry.context.ContextUtility;
|
2017-11-30 18:06:08 +01:00
|
|
|
import org.gcube.informationsystem.resourceregistry.context.security.AdminSecurityContext;
|
|
|
|
import org.gcube.informationsystem.resourceregistry.context.security.SecurityContext.PermissionMode;
|
2016-07-18 11:57:12 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
import com.orientechnologies.orient.core.record.ORecord;
|
2016-12-19 14:59:27 +01:00
|
|
|
import com.orientechnologies.orient.core.record.impl.ODocument;
|
2016-07-18 11:57:12 +02:00
|
|
|
import com.orientechnologies.orient.core.sql.query.OSQLSynchQuery;
|
|
|
|
import com.tinkerpop.blueprints.Edge;
|
2016-09-16 15:35:44 +02:00
|
|
|
import com.tinkerpop.blueprints.Element;
|
2017-11-28 17:34:43 +01:00
|
|
|
import com.tinkerpop.blueprints.Graph;
|
2016-07-18 11:57:12 +02:00
|
|
|
import com.tinkerpop.blueprints.Vertex;
|
2017-03-29 16:14:03 +02:00
|
|
|
import com.tinkerpop.blueprints.impls.orient.OrientBaseGraph;
|
2016-09-16 18:37:57 +02:00
|
|
|
import com.tinkerpop.blueprints.impls.orient.OrientElement;
|
2017-03-29 16:14:03 +02:00
|
|
|
import com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx;
|
2016-07-18 11:57:12 +02:00
|
|
|
import com.tinkerpop.blueprints.util.io.graphson.GraphSONMode;
|
|
|
|
import com.tinkerpop.blueprints.util.io.graphson.GraphSONUtility;
|
|
|
|
|
|
|
|
/**
|
2016-10-12 14:33:23 +02:00
|
|
|
* @author Luca Frosini (ISTI - CNR)
|
2016-07-18 11:57:12 +02:00
|
|
|
*/
|
2016-11-11 12:01:12 +01:00
|
|
|
public class Utility {
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2016-07-18 11:57:12 +02:00
|
|
|
private static final Logger logger = LoggerFactory.getLogger(Utility.class);
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2017-11-23 09:39:21 +01:00
|
|
|
public static final String SHOULD_NOT_OCCUR_ERROR_MESSAGE = "This is really strange and should not occur. Please contact the system administrator.";
|
2017-11-17 18:10:35 +01:00
|
|
|
|
2017-11-30 18:06:08 +01:00
|
|
|
public static JSONObject toJsonObject(OrientElement element, boolean raw) throws ResourceRegistryException {
|
2016-12-19 14:59:27 +01:00
|
|
|
try {
|
|
|
|
return new JSONObject(toJsonString(element, raw));
|
2017-11-30 18:06:08 +01:00
|
|
|
} catch(Exception e) {
|
2016-12-19 14:59:27 +01:00
|
|
|
throw new ResourceRegistryException(e);
|
|
|
|
}
|
2016-09-12 14:45:40 +02:00
|
|
|
}
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2016-09-16 18:37:57 +02:00
|
|
|
public static String toJsonString(OrientElement element, boolean raw) {
|
|
|
|
ORecord oRecord = element.getRecord();
|
2016-10-14 15:35:20 +02:00
|
|
|
return toJsonString(oRecord, raw);
|
|
|
|
}
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2016-10-14 15:35:20 +02:00
|
|
|
public static String toJsonString(ORecord oRecord, boolean raw) {
|
2017-11-30 18:06:08 +01:00
|
|
|
if(raw) {
|
2016-09-08 11:57:10 +02:00
|
|
|
return oRecord.toJSON();
|
|
|
|
}
|
|
|
|
return oRecord.toJSON("class");
|
2016-07-18 11:57:12 +02:00
|
|
|
}
|
2017-11-30 18:06:08 +01:00
|
|
|
|
|
|
|
public static JSONObject toJsonObject(Element element, boolean raw) throws JSONException {
|
|
|
|
if(raw) {
|
|
|
|
return GraphSONUtility.jsonFromElement(element, element.getPropertyKeys(), GraphSONMode.EXTENDED);
|
2016-12-19 14:59:27 +01:00
|
|
|
} else {
|
2016-09-16 15:35:44 +02:00
|
|
|
Set<String> keys = new HashSet<>(element.getPropertyKeys());
|
2017-11-30 18:06:08 +01:00
|
|
|
for(String key : element.getPropertyKeys()) {
|
|
|
|
if(key.startsWith("_")) {
|
2016-07-18 11:57:12 +02:00
|
|
|
keys.remove(key);
|
|
|
|
}
|
|
|
|
}
|
2017-11-30 18:06:08 +01:00
|
|
|
return GraphSONUtility.jsonFromElement(element, keys, GraphSONMode.EXTENDED);
|
2016-07-18 11:57:12 +02:00
|
|
|
}
|
|
|
|
}
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2016-10-14 15:35:20 +02:00
|
|
|
public static String toJsonString(Element element, boolean raw) {
|
2016-07-18 11:57:12 +02:00
|
|
|
try {
|
2016-09-16 15:35:44 +02:00
|
|
|
return toJsonObject(element, true).toString();
|
2017-11-30 18:06:08 +01:00
|
|
|
} catch(Exception e) {
|
2016-09-16 15:35:44 +02:00
|
|
|
return String.valueOf(element);
|
2016-07-18 11:57:12 +02:00
|
|
|
}
|
|
|
|
}
|
2017-03-29 16:14:03 +02:00
|
|
|
|
|
|
|
public static <El extends Element> El getElementByUUIDAsAdmin(String elementType, UUID uuid,
|
2018-06-04 15:31:13 +02:00
|
|
|
Class<? extends El> clz) throws NotFoundException, ResourceRegistryException {
|
2017-03-29 16:14:03 +02:00
|
|
|
OrientGraphNoTx orientGraphNoTx = null;
|
|
|
|
try {
|
2017-11-30 18:06:08 +01:00
|
|
|
AdminSecurityContext adminSecurityContext = ContextUtility.getAdminSecurityContext();
|
2017-11-28 17:34:43 +01:00
|
|
|
orientGraphNoTx = adminSecurityContext.getGraphNoTx(PermissionMode.READER);
|
2017-03-29 16:14:03 +02:00
|
|
|
return Utility.getElementByUUID(orientGraphNoTx, elementType, uuid, clz);
|
|
|
|
} finally {
|
2017-11-30 18:06:08 +01:00
|
|
|
if(orientGraphNoTx != null) {
|
2017-03-29 16:14:03 +02:00
|
|
|
orientGraphNoTx.shutdown();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-30 18:06:08 +01:00
|
|
|
|
|
|
|
public static <El extends Element> El getElementByUUID(Graph graph, String elementType, UUID uuid,
|
2018-06-04 15:31:13 +02:00
|
|
|
Class<? extends El> clz) throws NotFoundException, ResourceRegistryException {
|
2017-11-30 18:06:08 +01:00
|
|
|
|
|
|
|
if(elementType == null || elementType.compareTo("") == 0) {
|
|
|
|
if(Vertex.class.isAssignableFrom(clz)) {
|
2016-11-23 17:52:04 +01:00
|
|
|
elementType = Entity.NAME;
|
|
|
|
}
|
2017-11-30 18:06:08 +01:00
|
|
|
if(Edge.class.isAssignableFrom(clz)) {
|
2016-11-23 17:52:04 +01:00
|
|
|
elementType = Relation.NAME;
|
|
|
|
}
|
2016-07-18 11:57:12 +02:00
|
|
|
}
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2016-07-18 11:57:12 +02:00
|
|
|
// TODO Rewrite using Gremlin
|
2017-11-30 18:06:08 +01:00
|
|
|
String select = "SELECT FROM " + elementType + " WHERE " + Relation.HEADER_PROPERTY + "." + Header.UUID_PROPERTY
|
2016-10-27 17:46:09 +02:00
|
|
|
+ " = \"" + uuid.toString() + "\"";
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2016-11-23 17:52:04 +01:00
|
|
|
OSQLSynchQuery<El> osqlSynchQuery = new OSQLSynchQuery<>(select);
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2017-11-28 17:34:43 +01:00
|
|
|
Iterable<El> elements = ((OrientBaseGraph) graph).command(osqlSynchQuery).execute();
|
2017-11-30 18:06:08 +01:00
|
|
|
if(elements == null || !elements.iterator().hasNext()) {
|
|
|
|
String error = String.format("No %s with UUID %s was found", elementType, uuid.toString());
|
2016-07-18 11:57:12 +02:00
|
|
|
logger.info(error);
|
2018-06-04 15:31:13 +02:00
|
|
|
throw new NotFoundException(error);
|
2016-07-18 11:57:12 +02:00
|
|
|
}
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2016-11-23 17:52:04 +01:00
|
|
|
Iterator<El> iterator = elements.iterator();
|
|
|
|
El element = iterator.next();
|
2017-11-30 18:06:08 +01:00
|
|
|
|
|
|
|
logger.trace("{} with {} is : {}", elementType, uuid.toString(), Utility.toJsonString(element, true));
|
|
|
|
|
|
|
|
if(iterator.hasNext()) {
|
|
|
|
throw new ResourceRegistryException("Found more than one " + elementType + " with uuid " + uuid.toString()
|
2016-07-18 11:57:12 +02:00
|
|
|
+ ". This is a fatal error please contact Admnistrator");
|
|
|
|
}
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2016-11-23 17:52:04 +01:00
|
|
|
return element;
|
2016-07-18 11:57:12 +02:00
|
|
|
}
|
2016-11-23 17:52:04 +01:00
|
|
|
|
2017-01-25 15:33:38 +01:00
|
|
|
public static <E extends Embedded> E getEmbedded(Class<E> clz, Element element, String property)
|
2016-12-19 14:59:27 +01:00
|
|
|
throws ResourceRegistryException {
|
|
|
|
try {
|
|
|
|
ODocument oDocument = element.getProperty(property);
|
2017-01-30 11:43:18 +01:00
|
|
|
E e = ISMapper.unmarshal(clz, oDocument.toJSON());
|
2017-01-25 15:33:38 +01:00
|
|
|
return e;
|
2017-11-30 18:06:08 +01:00
|
|
|
} catch(Exception ex) {
|
|
|
|
String error = String.format("Error while getting %s from %s", property, toJsonString(element, true));
|
2017-01-25 15:33:38 +01:00
|
|
|
throw new ResourceRegistryException(error, ex);
|
2016-12-19 14:59:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-30 18:06:08 +01:00
|
|
|
public static UUID getUUID(Element element) throws ResourceRegistryException {
|
2016-12-19 14:59:27 +01:00
|
|
|
/*
|
2017-11-30 18:06:08 +01:00
|
|
|
* ODocument header = element.getProperty(Entity.HEADER_PROPERTY); String
|
|
|
|
* contextID = header.field(Header.UUID_PROPERTY); return
|
2016-12-19 14:59:27 +01:00
|
|
|
* UUID.fromString(contextID);
|
|
|
|
*/
|
|
|
|
Header header = HeaderUtility.getHeader(element);
|
|
|
|
return header.getUUID();
|
|
|
|
}
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2016-07-18 11:57:12 +02:00
|
|
|
}
|