2016-07-18 11:57:12 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
package org.gcube.informationsystem.resourceregistry.resources.utils;
|
|
|
|
|
|
|
|
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;
|
2016-07-18 11:57:12 +02:00
|
|
|
import org.gcube.informationsystem.model.embedded.Header;
|
|
|
|
import org.gcube.informationsystem.model.entity.Entity;
|
|
|
|
import org.gcube.informationsystem.model.relation.Relation;
|
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
import com.orientechnologies.orient.core.record.ORecord;
|
|
|
|
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;
|
2016-07-18 11:57:12 +02:00
|
|
|
import com.tinkerpop.blueprints.Vertex;
|
2016-09-16 18:37:57 +02:00
|
|
|
import com.tinkerpop.blueprints.impls.orient.OrientElement;
|
2016-07-18 11:57:12 +02:00
|
|
|
import com.tinkerpop.blueprints.impls.orient.OrientGraph;
|
|
|
|
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 {
|
2016-07-18 11:57:12 +02:00
|
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(Utility.class);
|
|
|
|
|
2016-09-16 18:37:57 +02:00
|
|
|
public static JSONObject toJsonObject(OrientElement element, boolean raw) throws JSONException {
|
|
|
|
return new JSONObject(toJsonString(element, raw));
|
2016-09-12 14:45:40 +02: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);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String toJsonString(ORecord oRecord, boolean raw) {
|
2016-09-12 14:45:40 +02: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
|
|
|
}
|
2016-10-14 15:35:20 +02:00
|
|
|
|
2016-09-16 15:35:44 +02:00
|
|
|
public static JSONObject toJsonObject(Element element, boolean raw) throws JSONException {
|
|
|
|
if(raw){
|
|
|
|
return GraphSONUtility.jsonFromElement(element,
|
|
|
|
element.getPropertyKeys(), GraphSONMode.EXTENDED);
|
|
|
|
}else{
|
|
|
|
Set<String> keys = new HashSet<>(element.getPropertyKeys());
|
|
|
|
for (String key : element.getPropertyKeys()) {
|
2016-07-18 11:57:12 +02:00
|
|
|
if (key.startsWith("_")) {
|
|
|
|
keys.remove(key);
|
|
|
|
}
|
|
|
|
}
|
2016-09-16 15:35:44 +02:00
|
|
|
return GraphSONUtility.jsonFromElement(element, keys,
|
2016-09-16 15:46:02 +02:00
|
|
|
GraphSONMode.EXTENDED);
|
2016-07-18 11:57:12 +02: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();
|
2016-07-18 11:57:12 +02:00
|
|
|
} catch (Exception e) {
|
2016-09-16 15:35:44 +02:00
|
|
|
return String.valueOf(element);
|
2016-07-18 11:57:12 +02:00
|
|
|
}
|
|
|
|
}
|
2016-11-23 17:52:04 +01:00
|
|
|
|
|
|
|
public static <El extends Element> El getElementByUUID(OrientGraph orientGraph,
|
|
|
|
String elementType, UUID uuid, Class<? extends El> clz) throws ResourceRegistryException {
|
2016-07-18 11:57:12 +02:00
|
|
|
|
2016-11-23 17:52:04 +01:00
|
|
|
if (elementType == null || elementType.compareTo("")==0) {
|
|
|
|
if(Vertex.class.isAssignableFrom(clz)){
|
|
|
|
elementType = Entity.NAME;
|
|
|
|
}
|
|
|
|
if(Edge.class.isAssignableFrom(clz)){
|
|
|
|
elementType = Relation.NAME;
|
|
|
|
}
|
2016-07-18 11:57:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO Rewrite using Gremlin
|
2016-11-23 17:52:04 +01:00
|
|
|
String select = "SELECT FROM " + elementType + " WHERE "
|
2016-07-18 11:57:12 +02:00
|
|
|
+ Relation.HEADER_PROPERTY + "." + Header.UUID_PROPERTY
|
2016-10-27 17:46:09 +02:00
|
|
|
+ " = \"" + uuid.toString() + "\"";
|
2016-07-18 11:57:12 +02:00
|
|
|
|
2016-11-23 17:52:04 +01:00
|
|
|
OSQLSynchQuery<El> osqlSynchQuery = new OSQLSynchQuery<>(select);
|
2016-07-18 11:57:12 +02:00
|
|
|
|
2016-11-23 17:52:04 +01:00
|
|
|
Iterable<El> elements = orientGraph.command(osqlSynchQuery).execute();
|
|
|
|
if (elements == null || !elements.iterator().hasNext()) {
|
2016-07-18 11:57:12 +02:00
|
|
|
String error = String.format("No %s with UUID %s was found",
|
2016-11-23 17:52:04 +01:00
|
|
|
elementType, uuid.toString());
|
2016-07-18 11:57:12 +02:00
|
|
|
logger.info(error);
|
2016-11-23 17:52:04 +01:00
|
|
|
throw new ResourceRegistryException(error);
|
2016-07-18 11:57:12 +02:00
|
|
|
}
|
|
|
|
|
2016-11-23 17:52:04 +01:00
|
|
|
Iterator<El> iterator = elements.iterator();
|
|
|
|
El element = iterator.next();
|
2016-07-18 11:57:12 +02:00
|
|
|
|
2016-11-23 17:52:04 +01:00
|
|
|
logger.trace("{} with {} is : {}", elementType, uuid.toString(),
|
|
|
|
Utility.toJsonString(element, true));
|
2016-07-18 11:57:12 +02:00
|
|
|
|
|
|
|
if (iterator.hasNext()) {
|
|
|
|
throw new ResourceRegistryException("Found more than one "
|
2016-11-23 17:52:04 +01:00
|
|
|
+ elementType + " with uuid " + uuid.toString()
|
2016-07-18 11:57:12 +02:00
|
|
|
+ ". This is a fatal error please contact Admnistrator");
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2016-07-18 11:57:12 +02:00
|
|
|
}
|