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;
|
2016-12-19 14:59:27 +01:00
|
|
|
import org.gcube.informationsystem.model.embedded.Header;
|
|
|
|
import org.gcube.informationsystem.model.entity.Entity;
|
|
|
|
import org.gcube.informationsystem.model.entity.Facet;
|
|
|
|
import org.gcube.informationsystem.model.entity.Resource;
|
|
|
|
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;
|
2016-12-22 17:33:10 +01:00
|
|
|
import org.gcube.informationsystem.resourceregistry.context.ContextUtility;
|
2016-12-19 14:59:27 +01:00
|
|
|
import org.gcube.informationsystem.resourceregistry.context.SecurityContextMapper.PermissionMode;
|
2016-12-27 11:27:01 +01:00
|
|
|
import org.gcube.informationsystem.resourceregistry.er.ERManagement;
|
|
|
|
import org.gcube.informationsystem.resourceregistry.er.relation.RelationManagement;
|
2016-12-22 17:27:26 +01:00
|
|
|
import org.gcube.informationsystem.resourceregistry.utils.HeaderUtility;
|
|
|
|
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;
|
|
|
|
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
|
|
|
|
|
|
|
private static Logger logger = LoggerFactory
|
|
|
|
.getLogger(EntityManagement.class);
|
|
|
|
|
|
|
|
protected EntityManagement(Class<E> entityClass) {
|
2016-12-30 17:31:12 +01:00
|
|
|
super(entityClass);
|
|
|
|
|
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());
|
|
|
|
|
|
|
|
if (Facet.class.isAssignableFrom(entityClass)) {
|
|
|
|
this.baseType = Facet.NAME;
|
|
|
|
} else if (Resource.class.isAssignableFrom(entityClass)) {
|
|
|
|
this.baseType = Resource.NAME;
|
|
|
|
} else {
|
|
|
|
this.baseType = Entity.NAME;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected EntityManagement(Class<E> entityClass, OrientGraph orientGraph) {
|
|
|
|
this(entityClass);
|
|
|
|
this.orientGraph = orientGraph;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected Vertex createVertex() throws EntityAlreadyPresentException,
|
|
|
|
ResourceRegistryException {
|
|
|
|
|
|
|
|
logger.trace("Going to create {} for {} ({}) using {}",
|
2016-12-30 17:31:12 +01:00
|
|
|
Vertex.class.getSimpleName(), baseType, erType, jsonNode);
|
2016-12-19 14:59:27 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
|
2016-12-30 17:31:12 +01:00
|
|
|
Vertex vertexEntity = orientGraph.addVertex("class:" + erType);
|
2016-12-19 14:59:27 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
|
2016-12-30 17:31:12 +01:00
|
|
|
Vertex v = getElement();
|
2016-12-19 14:59:27 +01:00
|
|
|
if (v != null) {
|
|
|
|
String error = String.format(
|
2016-12-30 17:31:12 +01:00
|
|
|
"A %s with UUID %s already exist", erType,
|
2016-12-19 14:59:27 +01:00
|
|
|
uuid.toString());
|
|
|
|
throw new EntityAlreadyPresentException(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (EntityAlreadyPresentException e) {
|
|
|
|
throw e;
|
|
|
|
} catch (Exception e) {
|
|
|
|
// no header or no header with uuid is provided and it is fine
|
|
|
|
}
|
|
|
|
|
2016-12-30 17:31:12 +01:00
|
|
|
this.element = vertexEntity;
|
2016-12-19 16:25:29 +01:00
|
|
|
|
2016-12-19 14:59:27 +01:00
|
|
|
Header entityHeader = HeaderUtility.getHeader(jsonNode, true);
|
|
|
|
if (entityHeader != null) {
|
2016-12-30 17:31:12 +01:00
|
|
|
element.setProperty(Entity.HEADER_PROPERTY, entityHeader);
|
2016-12-19 14:59:27 +01:00
|
|
|
} else {
|
2016-12-30 17:31:12 +01:00
|
|
|
entityHeader = HeaderUtility.addHeader(element, null);
|
2016-12-19 14:59:27 +01:00
|
|
|
}
|
|
|
|
|
2017-01-11 18:00:21 +01:00
|
|
|
if (Resource.class.isAssignableFrom(erTypeClass)) {
|
2016-12-19 14:59:27 +01:00
|
|
|
// Facet and relation are created in calling method
|
|
|
|
} else {
|
2016-12-30 17:31:12 +01:00
|
|
|
ERManagement.updateProperties(element, jsonNode, ignoreKeys,
|
2016-12-19 14:59:27 +01:00
|
|
|
ignoreStartWithKeys);
|
|
|
|
}
|
|
|
|
|
2016-12-30 17:31:12 +01:00
|
|
|
ContextUtility.addToActualContext(orientGraph, element);
|
2016-12-19 14:59:27 +01:00
|
|
|
|
2016-12-30 17:31:12 +01:00
|
|
|
((OrientVertex) element).save();
|
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 {}",
|
2016-12-30 17:31:12 +01:00
|
|
|
Vertex.class.getSimpleName(), baseType, erType, jsonNode, e);
|
|
|
|
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
|
2016-12-19 16:25:29 +01:00
|
|
|
public boolean reallyAddToContext() throws ContextException,
|
|
|
|
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);
|
2016-12-19 14:59:27 +01:00
|
|
|
relationManagement.reallyAddToContext();
|
|
|
|
}
|
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
|
2016-12-19 16:25:29 +01:00
|
|
|
public boolean reallyRemoveFromContext() throws ContextException,
|
|
|
|
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);
|
|
|
|
relationManagement.reallyRemoveFromContext();
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2016-12-19 14:59:27 +01:00
|
|
|
OrientVertexType orientVertexType = ((OrientVertex) vertex).getType();
|
|
|
|
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
|
|
|
|
2016-12-30 17:31:12 +01:00
|
|
|
public abstract Vertex reallyCreate() throws EntityAlreadyPresentException,
|
|
|
|
ResourceRegistryException;
|
2016-12-19 14:59:27 +01:00
|
|
|
|
2016-12-30 17:31:12 +01:00
|
|
|
public String create() throws EntityAlreadyPresentException,
|
2016-12-19 14:59:27 +01:00
|
|
|
ResourceRegistryException {
|
|
|
|
|
|
|
|
try {
|
2016-12-19 16:25:29 +01:00
|
|
|
orientGraph = ContextUtility
|
|
|
|
.getActualSecurityContextGraph(PermissionMode.WRITER);
|
2016-12-19 14:59:27 +01:00
|
|
|
|
2016-12-30 17:31:12 +01:00
|
|
|
element = reallyCreate();
|
2016-12-19 14:59:27 +01:00
|
|
|
|
|
|
|
orientGraph.commit();
|
|
|
|
|
|
|
|
return serialize();
|
|
|
|
|
|
|
|
} catch (ResourceRegistryException e) {
|
|
|
|
if (orientGraph != null) {
|
|
|
|
orientGraph.rollback();
|
|
|
|
}
|
|
|
|
throw e;
|
|
|
|
} catch (Exception e) {
|
|
|
|
if (orientGraph != null) {
|
|
|
|
orientGraph.rollback();
|
|
|
|
}
|
2016-12-19 16:25:29 +01:00
|
|
|
throw new ResourceRegistryException(e);
|
2016-12-19 14:59:27 +01:00
|
|
|
} finally {
|
|
|
|
if (orientGraph != null) {
|
|
|
|
orientGraph.shutdown();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
JSONObject jsonObject = entityManagement.serializeAsJson();
|
|
|
|
jsonArray.put(jsonObject);
|
|
|
|
}
|
|
|
|
return jsonArray.toString();
|
|
|
|
}
|
2016-12-19 16:25:29 +01:00
|
|
|
|
2016-12-19 14:59:27 +01:00
|
|
|
}
|