2016-12-19 14:59:27 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2016-12-27 11:27:01 +01:00
|
|
|
package org.gcube.informationsystem.resourceregistry.er.entity;
|
2016-12-19 14:59:27 +01:00
|
|
|
|
2017-01-11 12:06:50 +01:00
|
|
|
import org.codehaus.jettison.json.JSONArray;
|
|
|
|
import org.codehaus.jettison.json.JSONObject;
|
2017-03-29 16:53:37 +02:00
|
|
|
import org.gcube.informationsystem.model.AccessType;
|
2016-12-19 14:59:27 +01:00
|
|
|
import org.gcube.informationsystem.model.entity.Entity;
|
|
|
|
import org.gcube.informationsystem.model.entity.Facet;
|
|
|
|
import org.gcube.informationsystem.model.entity.Resource;
|
2017-04-04 15:03:25 +02:00
|
|
|
import org.gcube.informationsystem.model.relation.Relation;
|
2016-12-19 14:59:27 +01:00
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
|
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextException;
|
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.EntityAlreadyPresentException;
|
2017-04-04 15:03:25 +02:00
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.er.ERAvailableInAnotherContextException;
|
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.er.ERNotFoundException;
|
2016-12-22 17:33:10 +01:00
|
|
|
import org.gcube.informationsystem.resourceregistry.context.ContextUtility;
|
2016-12-27 11:27:01 +01:00
|
|
|
import org.gcube.informationsystem.resourceregistry.er.ERManagement;
|
2017-11-17 12:31:22 +01:00
|
|
|
import org.gcube.informationsystem.resourceregistry.er.ERManagementUtility;
|
2016-12-27 11:27:01 +01:00
|
|
|
import org.gcube.informationsystem.resourceregistry.er.relation.RelationManagement;
|
2016-12-22 17:27:26 +01:00
|
|
|
import org.gcube.informationsystem.resourceregistry.utils.Utility;
|
2016-12-19 14:59:27 +01:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
import com.tinkerpop.blueprints.Direction;
|
|
|
|
import com.tinkerpop.blueprints.Edge;
|
2017-04-04 15:03:25 +02:00
|
|
|
import com.tinkerpop.blueprints.Element;
|
2016-12-19 14:59:27 +01:00
|
|
|
import com.tinkerpop.blueprints.Vertex;
|
|
|
|
import com.tinkerpop.blueprints.impls.orient.OrientGraph;
|
|
|
|
import com.tinkerpop.blueprints.impls.orient.OrientVertex;
|
|
|
|
import com.tinkerpop.blueprints.impls.orient.OrientVertexType;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Luca Frosini (ISTI - CNR)
|
|
|
|
*/
|
2016-12-30 17:31:12 +01:00
|
|
|
public abstract class EntityManagement<E extends Entity> extends
|
|
|
|
ERManagement<E, Vertex> {
|
2016-12-19 14:59:27 +01:00
|
|
|
|
2017-11-15 19:31:49 +01:00
|
|
|
private static Logger staticLogger = LoggerFactory
|
2016-12-19 14:59:27 +01:00
|
|
|
.getLogger(EntityManagement.class);
|
|
|
|
|
2017-03-29 16:53:37 +02:00
|
|
|
protected EntityManagement(AccessType accessType) {
|
|
|
|
super(accessType);
|
2016-12-30 17:31:12 +01:00
|
|
|
|
2016-12-19 14:59:27 +01:00
|
|
|
this.ignoreKeys.add(Entity.HEADER_PROPERTY);
|
|
|
|
|
|
|
|
this.ignoreStartWithKeys.add(OrientVertex.CONNECTION_IN_PREFIX
|
|
|
|
.toLowerCase());
|
|
|
|
this.ignoreStartWithKeys.add(OrientVertex.CONNECTION_OUT_PREFIX
|
|
|
|
.toLowerCase());
|
|
|
|
this.ignoreStartWithKeys.add(OrientVertex.CONNECTION_IN_PREFIX
|
|
|
|
.toUpperCase());
|
|
|
|
this.ignoreStartWithKeys.add(OrientVertex.CONNECTION_OUT_PREFIX
|
|
|
|
.toUpperCase());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-03-29 16:53:37 +02:00
|
|
|
protected EntityManagement(AccessType accessType, OrientGraph orientGraph) {
|
|
|
|
this(accessType);
|
2016-12-19 14:59:27 +01:00
|
|
|
this.orientGraph = orientGraph;
|
|
|
|
}
|
2017-05-03 11:21:45 +02:00
|
|
|
|
2017-11-15 19:31:49 +01:00
|
|
|
protected static JSONObject addRelation(JSONObject sourceResource,
|
|
|
|
JSONObject relation, String arrayKey)
|
|
|
|
throws ResourceRegistryException {
|
|
|
|
JSONArray relationArray = null;
|
|
|
|
try {
|
|
|
|
if (sourceResource.has(arrayKey)) {
|
|
|
|
relationArray = sourceResource.getJSONArray(arrayKey);
|
|
|
|
} else {
|
|
|
|
relationArray = new JSONArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
relationArray.put(relation);
|
|
|
|
sourceResource.putOpt(arrayKey, relationArray);
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new ResourceRegistryException(e);
|
|
|
|
}
|
|
|
|
return sourceResource;
|
|
|
|
}
|
|
|
|
|
2016-12-19 14:59:27 +01:00
|
|
|
protected Vertex createVertex() throws EntityAlreadyPresentException,
|
|
|
|
ResourceRegistryException {
|
|
|
|
|
|
|
|
logger.trace("Going to create {} for {} ({}) using {}",
|
2017-03-29 16:53:37 +02:00
|
|
|
Vertex.class.getSimpleName(), accessType.getName(), erType, jsonNode);
|
2016-12-19 14:59:27 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
|
2017-03-28 16:02:23 +02:00
|
|
|
if(oClass.isAbstract()){
|
|
|
|
String error = String.format("Trying to create an instance of %s of type %s which is abstract. The operation will be aborted.",
|
2017-03-29 16:53:37 +02:00
|
|
|
accessType.getName(), erType);
|
2017-03-28 16:02:23 +02:00
|
|
|
throw new ResourceRegistryException(error);
|
|
|
|
}
|
|
|
|
|
2016-12-30 17:31:12 +01:00
|
|
|
Vertex vertexEntity = orientGraph.addVertex("class:" + erType);
|
2016-12-19 14:59:27 +01:00
|
|
|
|
|
|
|
try {
|
2017-04-04 15:03:25 +02:00
|
|
|
if(uuid!=null){
|
|
|
|
Vertex v = getElement();
|
|
|
|
if (v != null) {
|
|
|
|
String error = String.format(
|
|
|
|
"A %s with UUID %s already exist", erType,
|
|
|
|
uuid.toString());
|
2017-05-02 11:56:24 +02:00
|
|
|
throw getSpecificERAlreadyPresentException(error);
|
2017-04-04 15:03:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (ERNotFoundException e) {
|
|
|
|
try {
|
2017-11-17 12:31:22 +01:00
|
|
|
Element el = ERManagementUtility.getAnyElementByUUID(uuid);
|
2016-12-19 14:59:27 +01:00
|
|
|
String error = String.format(
|
2017-04-04 15:03:25 +02:00
|
|
|
"UUID %s is already used by another %s. This is not allowed.",
|
|
|
|
uuid.toString(), (el instanceof Vertex) ? Entity.NAME : Relation.NAME);
|
2017-05-02 14:25:06 +02:00
|
|
|
throw getSpecificERAvailableInAnotherContextException(error);
|
2017-04-04 15:03:25 +02:00
|
|
|
|
|
|
|
}catch (ERNotFoundException e1) {
|
|
|
|
// OK the UUID is not already used.
|
2016-12-19 14:59:27 +01:00
|
|
|
}
|
2017-04-04 15:03:25 +02:00
|
|
|
} catch (ERAvailableInAnotherContextException e) {
|
2016-12-19 14:59:27 +01:00
|
|
|
throw e;
|
2017-04-04 15:03:25 +02:00
|
|
|
}
|
2017-05-02 14:25:06 +02:00
|
|
|
|
2016-12-30 17:31:12 +01:00
|
|
|
this.element = vertexEntity;
|
2016-12-19 16:25:29 +01:00
|
|
|
|
2017-10-26 17:09:07 +02:00
|
|
|
if (accessType==AccessType.RESOURCE) {
|
2016-12-19 14:59:27 +01:00
|
|
|
// Facet and relation are created in calling method
|
|
|
|
} else {
|
2017-03-27 15:04:44 +02:00
|
|
|
ERManagement.updateProperties(oClass, element, jsonNode, ignoreKeys,
|
2016-12-19 14:59:27 +01:00
|
|
|
ignoreStartWithKeys);
|
|
|
|
}
|
|
|
|
|
2017-10-26 17:09:07 +02:00
|
|
|
/* This code has been moved in ERManagement.internalCreate()
|
|
|
|
Header entityHeader = HeaderUtility.getHeader(jsonNode, true);
|
|
|
|
if (entityHeader != null) {
|
|
|
|
element.setProperty(Entity.HEADER_PROPERTY, entityHeader);
|
|
|
|
} else {
|
|
|
|
entityHeader = HeaderUtility.addHeader(element, null);
|
|
|
|
}
|
2016-12-30 17:31:12 +01:00
|
|
|
ContextUtility.addToActualContext(orientGraph, element);
|
2017-10-26 17:09:07 +02:00
|
|
|
|
2016-12-30 17:31:12 +01:00
|
|
|
((OrientVertex) element).save();
|
2017-10-26 17:09:07 +02:00
|
|
|
*/
|
|
|
|
|
2016-12-19 14:59:27 +01:00
|
|
|
logger.info("Created {} is {}", Vertex.class.getSimpleName(),
|
2016-12-30 17:31:12 +01:00
|
|
|
Utility.toJsonString((OrientVertex) element, true));
|
2016-12-19 14:59:27 +01:00
|
|
|
|
2016-12-30 17:31:12 +01:00
|
|
|
return element;
|
2016-12-19 14:59:27 +01:00
|
|
|
} catch (ResourceRegistryException e) {
|
|
|
|
throw e;
|
|
|
|
} catch (Exception e) {
|
|
|
|
logger.trace("Error while creating {} for {} ({}) using {}",
|
2017-03-29 16:53:37 +02:00
|
|
|
Vertex.class.getSimpleName(), accessType.getName(), erType, jsonNode, e);
|
2016-12-30 17:31:12 +01:00
|
|
|
throw new ResourceRegistryException("Error Creating " + erType
|
2016-12-19 14:59:27 +01:00
|
|
|
+ " with " + jsonNode, e.getCause());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-30 17:31:12 +01:00
|
|
|
@Override
|
2017-10-26 17:09:07 +02:00
|
|
|
protected boolean reallyAddToContext() throws ContextException,
|
2016-12-19 16:25:29 +01:00
|
|
|
ResourceRegistryException {
|
2016-12-19 14:59:27 +01:00
|
|
|
|
2016-12-30 17:31:12 +01:00
|
|
|
ContextUtility.addToActualContext(orientGraph, getElement());
|
2016-12-19 14:59:27 +01:00
|
|
|
|
2016-12-30 17:31:12 +01:00
|
|
|
Iterable<Edge> edges = element.getEdges(Direction.OUT);
|
2016-12-19 16:25:29 +01:00
|
|
|
|
2016-12-19 14:59:27 +01:00
|
|
|
for (Edge edge : edges) {
|
|
|
|
@SuppressWarnings("rawtypes")
|
2016-12-19 16:25:29 +01:00
|
|
|
RelationManagement relationManagement = RelationManagement
|
|
|
|
.getRelationManagement(orientGraph, edge);
|
2017-10-26 17:09:07 +02:00
|
|
|
relationManagement.internalAddToContext();
|
2016-12-19 14:59:27 +01:00
|
|
|
}
|
2016-12-19 16:25:29 +01:00
|
|
|
|
2016-12-19 14:59:27 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-12-30 17:31:12 +01:00
|
|
|
@Override
|
2017-10-26 17:09:07 +02:00
|
|
|
protected boolean reallyRemoveFromContext() throws ContextException,
|
2016-12-19 16:25:29 +01:00
|
|
|
ResourceRegistryException {
|
2016-12-20 18:36:17 +01:00
|
|
|
|
2016-12-30 17:31:12 +01:00
|
|
|
ContextUtility.removeFromActualContext(orientGraph, getElement());
|
2017-03-23 13:37:15 +01:00
|
|
|
|
2016-12-30 17:31:12 +01:00
|
|
|
Iterable<Edge> edges = element.getEdges(Direction.OUT);
|
2016-12-20 18:36:17 +01:00
|
|
|
|
|
|
|
for (Edge edge : edges) {
|
|
|
|
@SuppressWarnings("rawtypes")
|
|
|
|
RelationManagement relationManagement = RelationManagement
|
|
|
|
.getRelationManagement(orientGraph, edge);
|
2017-10-26 17:09:07 +02:00
|
|
|
relationManagement.internalRemoveFromContext();
|
2016-12-20 18:36:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2016-12-19 14:59:27 +01:00
|
|
|
}
|
2016-12-19 16:25:29 +01:00
|
|
|
|
2016-12-30 17:31:12 +01:00
|
|
|
@SuppressWarnings({ "rawtypes", "unchecked" })
|
2016-12-19 16:25:29 +01:00
|
|
|
public static EntityManagement getEntityManagement(OrientGraph orientGraph,
|
|
|
|
Vertex vertex) throws ResourceRegistryException {
|
2017-03-28 16:02:23 +02:00
|
|
|
|
2017-04-14 14:37:57 +02:00
|
|
|
if(orientGraph==null){
|
|
|
|
throw new ResourceRegistryException(OrientGraph.class.getSimpleName() + "instance is null. This is really strage please contact the administrator.");
|
|
|
|
}
|
|
|
|
|
|
|
|
if(vertex==null){
|
|
|
|
throw new ResourceRegistryException(Vertex.class.getSimpleName() + "instance is null. This is really strage please contact the administrator.");
|
|
|
|
}
|
|
|
|
|
2017-03-28 16:02:23 +02:00
|
|
|
OrientVertexType orientVertexType = null;
|
|
|
|
try {
|
|
|
|
orientVertexType = ((OrientVertex) vertex).getType();
|
|
|
|
}catch (Exception e) {
|
2017-04-14 14:37:57 +02:00
|
|
|
String error = String.format("Unable to detect type of %s. This is really strage please contact the administrator.", vertex.toString());
|
2017-11-15 19:31:49 +01:00
|
|
|
staticLogger.error(error, e);
|
2017-03-28 16:02:23 +02:00
|
|
|
throw new ResourceRegistryException(error);
|
|
|
|
}
|
|
|
|
|
2016-12-19 14:59:27 +01:00
|
|
|
EntityManagement entityManagement = null;
|
|
|
|
if (orientVertexType.isSubClassOf(Resource.NAME)) {
|
|
|
|
entityManagement = new ResourceManagement(orientGraph);
|
|
|
|
} else if (orientVertexType.isSubClassOf(Facet.NAME)) {
|
|
|
|
entityManagement = new FacetManagement(orientGraph);
|
|
|
|
} else {
|
|
|
|
String error = String.format("{%s is not a %s nor a %s. "
|
2016-12-20 18:36:17 +01:00
|
|
|
+ "This is really strange and should not occur. "
|
2016-12-19 16:25:29 +01:00
|
|
|
+ "Please Investigate it.", vertex, Resource.NAME,
|
|
|
|
Facet.NAME);
|
2016-12-19 14:59:27 +01:00
|
|
|
throw new ResourceRegistryException(error);
|
|
|
|
}
|
2016-12-30 17:31:12 +01:00
|
|
|
entityManagement.setElement(vertex);
|
2016-12-19 14:59:27 +01:00
|
|
|
return entityManagement;
|
|
|
|
}
|
2016-12-19 16:25:29 +01:00
|
|
|
|
2017-01-11 12:06:50 +01:00
|
|
|
@Override
|
|
|
|
public String reallyGetAll(boolean polymorphic) throws ResourceRegistryException {
|
|
|
|
JSONArray jsonArray = new JSONArray();
|
2017-01-11 17:11:28 +01:00
|
|
|
Iterable<Vertex> iterable = orientGraph.getVerticesOfClass(erType, polymorphic);
|
2017-01-11 12:06:50 +01:00
|
|
|
for(Vertex vertex : iterable){
|
|
|
|
@SuppressWarnings("rawtypes")
|
|
|
|
EntityManagement entityManagement = getEntityManagement(orientGraph, vertex);
|
2017-03-28 16:02:23 +02:00
|
|
|
try {
|
|
|
|
JSONObject jsonObject = entityManagement.serializeAsJson();
|
|
|
|
jsonArray.put(jsonObject);
|
|
|
|
}catch (ResourceRegistryException e) {
|
|
|
|
logger.error("Unable to correctly serialize {}. It will be excluded from results. This is really strange and should not occur.", vertex.toString());
|
|
|
|
}
|
2017-01-11 12:06:50 +01:00
|
|
|
}
|
|
|
|
return jsonArray.toString();
|
|
|
|
}
|
2016-12-19 16:25:29 +01:00
|
|
|
|
2016-12-19 14:59:27 +01:00
|
|
|
}
|