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;
|
|
|
|
|
|
|
|
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.gcube.informationsystem.resourceregistry.api.exceptions.entity.EntityException;
|
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.FacetNotFoundException;
|
|
|
|
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;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public class Utility {
|
|
|
|
|
|
|
|
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-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-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-09-16 15:35:44 +02:00
|
|
|
public static String toJsonString(Element element) {
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Vertex getEntityByUUID(OrientGraph orientGraph,
|
|
|
|
String entityType, String uuid) throws ResourceRegistryException {
|
|
|
|
|
|
|
|
if (entityType == null) {
|
|
|
|
entityType = Entity.NAME;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO Rewrite using Gremlin
|
|
|
|
String select = "SELECT FROM " + entityType + " WHERE "
|
|
|
|
+ Entity.HEADER_PROPERTY + "." + Header.UUID_PROPERTY + " = \""
|
|
|
|
+ uuid + "\"";
|
|
|
|
|
|
|
|
OSQLSynchQuery<Vertex> osqlSynchQuery = new OSQLSynchQuery<Vertex>(
|
|
|
|
select);
|
|
|
|
|
|
|
|
Iterable<Vertex> vertexes = orientGraph.command(osqlSynchQuery)
|
|
|
|
.execute();
|
|
|
|
if (vertexes == null || !vertexes.iterator().hasNext()) {
|
|
|
|
String error = String.format("No %s with UUID %s was found",
|
|
|
|
entityType, uuid);
|
|
|
|
logger.info(error);
|
|
|
|
throw new EntityException(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
Iterator<Vertex> iterator = vertexes.iterator();
|
|
|
|
Vertex entity = iterator.next();
|
2016-09-12 14:45:40 +02:00
|
|
|
|
2016-09-16 15:35:44 +02:00
|
|
|
logger.trace("{} with {} is : {}", entityType, uuid, Utility.toJsonString(entity));
|
2016-09-12 14:45:40 +02:00
|
|
|
|
2016-07-18 11:57:12 +02:00
|
|
|
if (iterator.hasNext()) {
|
|
|
|
throw new ResourceRegistryException("Found more than one "
|
|
|
|
+ entityType + " with uuid " + uuid
|
|
|
|
+ ". This is a fatal error please contact Admnistrator");
|
|
|
|
}
|
|
|
|
|
|
|
|
return entity;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Edge getRelationByUUID(OrientGraph orientGraph,
|
|
|
|
String relationType, String uuid) throws ResourceRegistryException {
|
|
|
|
|
|
|
|
if (relationType == null) {
|
|
|
|
relationType = Relation.class.getSimpleName();
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO Rewrite using Gremlin
|
|
|
|
String select = "SELECT FROM " + relationType + " WHERE "
|
|
|
|
+ Relation.HEADER_PROPERTY + "." + Header.UUID_PROPERTY
|
|
|
|
+ " = \"" + uuid + "\"";
|
|
|
|
|
|
|
|
OSQLSynchQuery<Edge> osqlSynchQuery = new OSQLSynchQuery<Edge>(select);
|
|
|
|
|
|
|
|
Iterable<Edge> edges = orientGraph.command(osqlSynchQuery).execute();
|
|
|
|
if (edges == null || !edges.iterator().hasNext()) {
|
|
|
|
String error = String.format("No %s with UUID %s was found",
|
|
|
|
relationType, uuid);
|
|
|
|
logger.info(error);
|
|
|
|
throw new FacetNotFoundException(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
Iterator<Edge> iterator = edges.iterator();
|
|
|
|
Edge relation = iterator.next();
|
|
|
|
|
2016-09-12 14:45:40 +02:00
|
|
|
logger.trace("{} with {} is : {}", relationType, uuid,
|
2016-09-16 15:35:44 +02:00
|
|
|
Utility.toJsonString(relation));
|
2016-07-18 11:57:12 +02:00
|
|
|
|
|
|
|
if (iterator.hasNext()) {
|
|
|
|
throw new ResourceRegistryException("Found more than one "
|
|
|
|
+ relationType + " with uuid " + uuid
|
|
|
|
+ ". This is a fatal error please contact Admnistrator");
|
|
|
|
}
|
|
|
|
|
|
|
|
return relation;
|
|
|
|
}
|
|
|
|
}
|