2016-12-19 14:59:27 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2016-12-27 11:27:01 +01:00
|
|
|
package org.gcube.informationsystem.resourceregistry.er.relation;
|
2016-12-19 14:59:27 +01:00
|
|
|
|
2017-01-12 11:13:09 +01:00
|
|
|
import java.util.ArrayList;
|
2017-01-17 16:53:24 +01:00
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.HashMap;
|
2016-12-19 14:59:27 +01:00
|
|
|
import java.util.Iterator;
|
2017-01-12 11:13:09 +01:00
|
|
|
import java.util.List;
|
2017-01-17 16:53:24 +01:00
|
|
|
import java.util.Map;
|
2016-12-19 14:59:27 +01:00
|
|
|
import java.util.UUID;
|
|
|
|
|
2017-01-11 12:06:50 +01:00
|
|
|
import org.codehaus.jettison.json.JSONArray;
|
2016-12-19 16:25:29 +01:00
|
|
|
import org.codehaus.jettison.json.JSONException;
|
2016-12-19 14:59:27 +01:00
|
|
|
import org.codehaus.jettison.json.JSONObject;
|
2016-12-20 16:51:57 +01:00
|
|
|
import org.gcube.informationsystem.model.embedded.PropagationConstraint;
|
2016-12-20 18:36:17 +01:00
|
|
|
import org.gcube.informationsystem.model.embedded.PropagationConstraint.AddConstraint;
|
2016-12-20 16:51:57 +01:00
|
|
|
import org.gcube.informationsystem.model.embedded.PropagationConstraint.RemoveConstraint;
|
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;
|
|
|
|
import org.gcube.informationsystem.model.relation.ConsistsOf;
|
|
|
|
import org.gcube.informationsystem.model.relation.IsRelatedTo;
|
|
|
|
import org.gcube.informationsystem.model.relation.Relation;
|
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
|
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextException;
|
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.relation.RelationNotFoundException;
|
2016-12-22 17:33:10 +01:00
|
|
|
import org.gcube.informationsystem.resourceregistry.context.ContextUtility;
|
2017-03-23 13:37:15 +01:00
|
|
|
import org.gcube.informationsystem.resourceregistry.context.SecurityContextMapper;
|
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.entity.EntityManagement;
|
|
|
|
import org.gcube.informationsystem.resourceregistry.er.entity.FacetManagement;
|
|
|
|
import org.gcube.informationsystem.resourceregistry.er.entity.ResourceManagement;
|
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.fasterxml.jackson.databind.JsonNode;
|
|
|
|
import com.tinkerpop.blueprints.Direction;
|
|
|
|
import com.tinkerpop.blueprints.Edge;
|
|
|
|
import com.tinkerpop.blueprints.Vertex;
|
|
|
|
import com.tinkerpop.blueprints.impls.orient.OrientBaseGraph;
|
|
|
|
import com.tinkerpop.blueprints.impls.orient.OrientEdge;
|
|
|
|
import com.tinkerpop.blueprints.impls.orient.OrientEdgeType;
|
|
|
|
import com.tinkerpop.blueprints.impls.orient.OrientGraph;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Luca Frosini (ISTI - CNR)
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
@SuppressWarnings("rawtypes")
|
2017-01-13 12:21:35 +01:00
|
|
|
public abstract class RelationManagement<R extends Relation> extends
|
|
|
|
ERManagement<R, Edge> {
|
2016-12-19 14:59:27 +01:00
|
|
|
|
|
|
|
private static Logger logger = LoggerFactory
|
|
|
|
.getLogger(RelationManagement.class);
|
|
|
|
|
|
|
|
protected final Class<? extends Entity> targetEntityClass;
|
|
|
|
|
|
|
|
protected RelationManagement(Class<R> relationClass) {
|
2016-12-30 17:31:12 +01:00
|
|
|
super(relationClass);
|
|
|
|
|
2016-12-19 14:59:27 +01:00
|
|
|
this.ignoreKeys.add(Relation.HEADER_PROPERTY);
|
|
|
|
this.ignoreKeys.add(Relation.TARGET_PROPERTY);
|
|
|
|
this.ignoreKeys.add(Relation.SOURCE_PROPERTY);
|
|
|
|
this.ignoreKeys.add(OrientBaseGraph.CONNECTION_IN.toLowerCase());
|
|
|
|
this.ignoreKeys.add(OrientBaseGraph.CONNECTION_OUT.toLowerCase());
|
|
|
|
this.ignoreKeys.add(OrientBaseGraph.CONNECTION_IN.toUpperCase());
|
|
|
|
this.ignoreKeys.add(OrientBaseGraph.CONNECTION_OUT.toUpperCase());
|
|
|
|
|
|
|
|
if (ConsistsOf.class.isAssignableFrom(relationClass)) {
|
|
|
|
this.baseType = ConsistsOf.NAME;
|
|
|
|
this.targetEntityClass = Facet.class;
|
|
|
|
} else if (IsRelatedTo.class.isAssignableFrom(relationClass)) {
|
|
|
|
this.baseType = IsRelatedTo.NAME;
|
|
|
|
this.targetEntityClass = Resource.class;
|
|
|
|
} else {
|
|
|
|
this.baseType = Relation.NAME;
|
|
|
|
this.targetEntityClass = Resource.class;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected RelationManagement(Class<R> relationClass, OrientGraph orientGraph) {
|
|
|
|
this(relationClass);
|
|
|
|
this.orientGraph = orientGraph;
|
|
|
|
}
|
|
|
|
|
2017-01-12 11:13:09 +01:00
|
|
|
@Override
|
2016-12-19 14:59:27 +01:00
|
|
|
public String serialize() throws ResourceRegistryException {
|
2016-12-19 16:25:29 +01:00
|
|
|
return serializeAsJson().toString();
|
2016-12-19 14:59:27 +01:00
|
|
|
}
|
|
|
|
|
2017-01-12 11:13:09 +01:00
|
|
|
@Override
|
2016-12-19 14:59:27 +01:00
|
|
|
public JSONObject serializeAsJson() throws ResourceRegistryException {
|
2017-01-17 16:53:24 +01:00
|
|
|
JSONObject relation = serializeSelfOnly();
|
2016-12-19 16:25:29 +01:00
|
|
|
|
2017-01-13 12:21:35 +01:00
|
|
|
Vertex target = element.getVertex(Direction.IN);
|
2016-12-19 16:25:29 +01:00
|
|
|
EntityManagement entityManagement = EntityManagement
|
2017-01-13 12:21:35 +01:00
|
|
|
.getEntityManagement(orientGraph, target);
|
2016-12-19 16:25:29 +01:00
|
|
|
|
|
|
|
try {
|
2017-01-17 16:53:24 +01:00
|
|
|
relation.put(Relation.TARGET_PROPERTY,
|
2016-12-19 16:25:29 +01:00
|
|
|
entityManagement.serializeAsJson());
|
|
|
|
} catch (JSONException e) {
|
2017-01-13 12:21:35 +01:00
|
|
|
throw new ResourceRegistryException(e);
|
2016-12-19 16:25:29 +01:00
|
|
|
}
|
|
|
|
|
2017-01-17 16:53:24 +01:00
|
|
|
return relation;
|
2016-12-19 14:59:27 +01:00
|
|
|
}
|
2017-01-13 12:21:35 +01:00
|
|
|
|
2017-01-17 16:53:24 +01:00
|
|
|
protected Map<String, JSONObject> fullSerialize(
|
|
|
|
Map<String, JSONObject> visitedSourceResources)
|
|
|
|
throws ResourceRegistryException {
|
2017-01-13 12:21:35 +01:00
|
|
|
Vertex source = element.getVertex(Direction.OUT);
|
|
|
|
|
2017-01-17 16:53:24 +01:00
|
|
|
String id = source.getId().toString();
|
|
|
|
|
|
|
|
JSONObject sourceResource = visitedSourceResources.get(id);
|
|
|
|
if (sourceResource == null) {
|
|
|
|
ResourceManagement resourceManagement = (ResourceManagement) EntityManagement
|
|
|
|
.getEntityManagement(orientGraph, source);
|
|
|
|
if (this instanceof IsRelatedToManagement) {
|
|
|
|
sourceResource = resourceManagement.serializeAsJson();
|
|
|
|
} else if (this instanceof ConsistsOfManagement) {
|
|
|
|
sourceResource = resourceManagement.serializeSelfOnly();
|
|
|
|
} else {
|
|
|
|
String error = String.format("{%s is not a %s nor a %s. "
|
|
|
|
+ "This is really strange and should not occur. "
|
|
|
|
+ "Please Investigate it.", this,
|
|
|
|
IsRelatedToManagement.class.getSimpleName(),
|
|
|
|
ConsistsOfManagement.class.getSimpleName());
|
|
|
|
throw new ResourceRegistryException(error);
|
|
|
|
}
|
|
|
|
}
|
2017-01-13 12:21:35 +01:00
|
|
|
|
|
|
|
if (this instanceof IsRelatedToManagement) {
|
2017-01-17 16:53:24 +01:00
|
|
|
sourceResource = ResourceManagement.addIsRelatedTo(sourceResource,
|
|
|
|
serializeAsJson());
|
|
|
|
} else if (this instanceof ConsistsOfManagement) {
|
|
|
|
sourceResource = ResourceManagement.addConsistsOf(sourceResource,
|
|
|
|
serializeAsJson());
|
|
|
|
} else {
|
|
|
|
String error = String.format("{%s is not a %s nor a %s. "
|
|
|
|
+ "This is really strange and should not occur. "
|
|
|
|
+ "Please Investigate it.", this,
|
|
|
|
IsRelatedToManagement.class.getSimpleName(),
|
|
|
|
ConsistsOfManagement.class.getSimpleName());
|
|
|
|
throw new ResourceRegistryException(error);
|
2017-01-13 12:21:35 +01:00
|
|
|
}
|
|
|
|
|
2017-01-17 16:53:24 +01:00
|
|
|
visitedSourceResources.put(id, sourceResource);
|
|
|
|
|
|
|
|
return visitedSourceResources;
|
2017-01-13 12:21:35 +01:00
|
|
|
}
|
|
|
|
|
2016-12-19 14:59:27 +01:00
|
|
|
public Edge reallyCreate(UUID sourceUUID, UUID targetUUID)
|
|
|
|
throws ResourceRegistryException {
|
|
|
|
ResourceManagement srmSource = new ResourceManagement(orientGraph);
|
|
|
|
srmSource.setUUID(sourceUUID);
|
2016-12-30 17:31:12 +01:00
|
|
|
Vertex source = srmSource.getElement();
|
2016-12-19 14:59:27 +01:00
|
|
|
|
2016-12-19 16:25:29 +01:00
|
|
|
EntityManagement entityManagement = getEntityManagement();
|
|
|
|
entityManagement.setUUID(targetUUID);
|
2016-12-30 17:31:12 +01:00
|
|
|
Vertex target = (Vertex) entityManagement.getElement();
|
2016-12-19 14:59:27 +01:00
|
|
|
|
|
|
|
return reallyCreate(source, target);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
protected Edge reallyCreate(Vertex source, Vertex target)
|
|
|
|
throws ResourceRegistryException {
|
2016-12-20 18:36:17 +01:00
|
|
|
|
|
|
|
EntityManagement sourceEntityManagement = EntityManagement
|
|
|
|
.getEntityManagement(orientGraph, source);
|
|
|
|
EntityManagement targetEntityManagement = EntityManagement
|
|
|
|
.getEntityManagement(orientGraph, target);
|
|
|
|
if (!(sourceEntityManagement instanceof ResourceManagement)) {
|
|
|
|
String error = String.format(
|
|
|
|
"Any type of %s can have only a %s as %s. "
|
|
|
|
+ "Cannot instatiate %s beetween %s -> %s ",
|
|
|
|
Relation.NAME, Resource.NAME, Relation.SOURCE_PROPERTY,
|
2016-12-30 17:31:12 +01:00
|
|
|
erType, sourceEntityManagement.serialize(),
|
2016-12-20 18:36:17 +01:00
|
|
|
targetEntityManagement.serialize());
|
|
|
|
throw new ResourceRegistryException(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this instanceof IsRelatedToManagement) {
|
|
|
|
if (!(targetEntityManagement instanceof ResourceManagement)) {
|
|
|
|
String error = String.format("A %s can have only a %s as %s. "
|
|
|
|
+ "Cannot instatiate %s beetween %s -> %s ", baseType,
|
2016-12-30 17:31:12 +01:00
|
|
|
Resource.NAME, Relation.TARGET_PROPERTY, erType,
|
2016-12-20 18:36:17 +01:00
|
|
|
sourceEntityManagement.serialize(),
|
|
|
|
targetEntityManagement.serialize());
|
|
|
|
throw new ResourceRegistryException(error);
|
|
|
|
}
|
|
|
|
} else if (this instanceof ConsistsOfManagement) {
|
|
|
|
if (!(targetEntityManagement instanceof FacetManagement)) {
|
|
|
|
String error = String.format("A %s can have only a %s as %s. "
|
|
|
|
+ "Cannot instatiate %s beetween %s -> %s ", baseType,
|
2016-12-30 17:31:12 +01:00
|
|
|
Facet.NAME, Relation.TARGET_PROPERTY, erType,
|
2016-12-20 18:36:17 +01:00
|
|
|
sourceEntityManagement.serialize(),
|
|
|
|
targetEntityManagement.serialize());
|
|
|
|
throw new ResourceRegistryException(error);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
String error = String.format("{%s is not a %s nor a %s. "
|
|
|
|
+ "This is really strange and should not occur. "
|
|
|
|
+ "Please Investigate it.", this,
|
|
|
|
IsRelatedToManagement.class.getSimpleName(),
|
|
|
|
ConsistsOfManagement.class.getSimpleName());
|
|
|
|
throw new ResourceRegistryException(error);
|
|
|
|
}
|
|
|
|
|
2016-12-30 17:31:12 +01:00
|
|
|
logger.trace("Creating {} beetween {} -> {}", erType,
|
2016-12-20 18:36:17 +01:00
|
|
|
sourceEntityManagement.serialize(),
|
|
|
|
targetEntityManagement.serialize());
|
2016-12-19 14:59:27 +01:00
|
|
|
|
2016-12-30 17:31:12 +01:00
|
|
|
element = orientGraph.addEdge(null, source, target, erType);
|
2016-12-19 14:59:27 +01:00
|
|
|
|
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
|
|
|
HeaderUtility.addHeader(element, null);
|
|
|
|
ContextUtility.addToActualContext(orientGraph, element);
|
2016-12-19 14:59:27 +01:00
|
|
|
|
2016-12-30 17:31:12 +01:00
|
|
|
((OrientEdge) element).save();
|
2016-12-19 14:59:27 +01:00
|
|
|
|
2016-12-30 17:31:12 +01:00
|
|
|
logger.info("{} successfully created", erType);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
public Edge reallyCreate(Vertex source) throws ResourceRegistryException {
|
|
|
|
Vertex target = null;
|
|
|
|
EntityManagement entityManagement = getEntityManagement();
|
|
|
|
|
|
|
|
if (!jsonNode.has(Relation.TARGET_PROPERTY)) {
|
|
|
|
throw new ResourceRegistryException(
|
|
|
|
"Error while creating relation. No target definition found");
|
|
|
|
}
|
|
|
|
entityManagement.setJSON(jsonNode.get(Relation.TARGET_PROPERTY));
|
|
|
|
try {
|
2016-12-30 17:31:12 +01:00
|
|
|
target = (Vertex) entityManagement.getElement();
|
2016-12-19 14:59:27 +01:00
|
|
|
} catch (Exception e) {
|
|
|
|
target = entityManagement.reallyCreate();
|
|
|
|
}
|
|
|
|
return reallyCreate(source, target);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Edge reallyCreate(UUID sourceUUID) throws ResourceRegistryException {
|
|
|
|
ResourceManagement srmSource = new ResourceManagement(orientGraph);
|
|
|
|
srmSource.setUUID(sourceUUID);
|
2016-12-30 17:31:12 +01:00
|
|
|
Vertex source = srmSource.getElement();
|
2016-12-19 14:59:27 +01:00
|
|
|
return reallyCreate(source);
|
|
|
|
}
|
|
|
|
|
2017-01-13 12:21:35 +01:00
|
|
|
@Override
|
2016-12-19 14:59:27 +01:00
|
|
|
public Edge reallyUpdate() throws ResourceRegistryException {
|
|
|
|
|
2016-12-30 17:31:12 +01:00
|
|
|
logger.debug("Trying to update {} : {}", erType, jsonNode);
|
2016-12-19 14:59:27 +01:00
|
|
|
|
2016-12-30 17:31:12 +01:00
|
|
|
Edge edge = getElement();
|
2017-01-13 12:21:35 +01:00
|
|
|
ERManagement.updateProperties(edge, jsonNode, ignoreKeys,
|
|
|
|
ignoreStartWithKeys);
|
2016-12-19 14:59:27 +01:00
|
|
|
|
2017-01-11 18:00:21 +01:00
|
|
|
if (ConsistsOf.class.isAssignableFrom(erTypeClass)) {
|
2016-12-19 14:59:27 +01:00
|
|
|
JsonNode target = jsonNode.get(Relation.TARGET_PROPERTY);
|
|
|
|
if (target != null) {
|
|
|
|
FacetManagement fm = new FacetManagement(orientGraph);
|
|
|
|
fm.setJSON(target);
|
|
|
|
fm.reallyUpdate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-30 17:31:12 +01:00
|
|
|
logger.info("{} {} successfully updated", erType, jsonNode);
|
2016-12-19 14:59:27 +01:00
|
|
|
|
|
|
|
return edge;
|
|
|
|
|
|
|
|
}
|
2017-01-17 16:53:24 +01:00
|
|
|
|
2017-01-13 12:21:35 +01:00
|
|
|
@Override
|
2016-12-19 14:59:27 +01:00
|
|
|
public boolean reallyAddToContext() throws ContextException,
|
|
|
|
ResourceRegistryException {
|
2016-12-30 17:31:12 +01:00
|
|
|
getElement();
|
2017-01-13 12:21:35 +01:00
|
|
|
|
2016-12-20 18:36:17 +01:00
|
|
|
AddConstraint addConstraint = AddConstraint.unpropagate;
|
2016-12-19 14:59:27 +01:00
|
|
|
|
2016-12-20 18:36:17 +01:00
|
|
|
try {
|
|
|
|
PropagationConstraint propagationConstraint = Utility.getEmbedded(
|
2016-12-30 17:31:12 +01:00
|
|
|
PropagationConstraint.class, element,
|
2016-12-20 18:36:17 +01:00
|
|
|
Relation.PROPAGATION_CONSTRAINT);
|
|
|
|
if (propagationConstraint.getAddConstraint() != null) {
|
|
|
|
addConstraint = propagationConstraint.getAddConstraint();
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
logger.warn("Error while getting {} from {}. Assuming {}. "
|
|
|
|
+ "This is really strange and should not occur. "
|
|
|
|
+ "Please Investigate it.",
|
|
|
|
Relation.PROPAGATION_CONSTRAINT,
|
2016-12-30 17:31:12 +01:00
|
|
|
Utility.toJsonString(element, true), addConstraint);
|
2016-12-20 18:36:17 +01:00
|
|
|
}
|
|
|
|
|
2016-12-30 17:31:12 +01:00
|
|
|
Vertex target = element.getVertex(Direction.IN);
|
2016-12-20 18:36:17 +01:00
|
|
|
|
|
|
|
switch (addConstraint) {
|
2017-03-23 13:37:15 +01:00
|
|
|
case propagate:
|
|
|
|
/*
|
|
|
|
* The relation must be added only in the case the target vertex
|
|
|
|
* must be added. Otherwise we have a relation which point to an
|
|
|
|
* entity outside of the context.
|
|
|
|
*/
|
|
|
|
ContextUtility.addToActualContext(orientGraph, getElement());
|
|
|
|
EntityManagement entityManagement = EntityManagement
|
|
|
|
.getEntityManagement(orientGraph, target);
|
|
|
|
entityManagement.reallyAddToContext();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case unpropagate:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2016-12-20 18:36:17 +01:00
|
|
|
}
|
2016-12-19 14:59:27 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-03-23 13:37:15 +01:00
|
|
|
public boolean forcedAddToContext() throws ContextException,
|
|
|
|
ResourceRegistryException {
|
|
|
|
|
|
|
|
getElement();
|
|
|
|
|
|
|
|
/* Adding source to Context */
|
|
|
|
Vertex source = element.getVertex(Direction.OUT);
|
|
|
|
EntityManagement entityManagement = EntityManagement
|
|
|
|
.getEntityManagement(orientGraph, source);
|
|
|
|
entityManagement.reallyAddToContext();
|
|
|
|
|
|
|
|
/* Adding target to Context */
|
|
|
|
Vertex target = element.getVertex(Direction.IN);
|
|
|
|
entityManagement = EntityManagement
|
|
|
|
.getEntityManagement(orientGraph, target);
|
|
|
|
entityManagement.reallyAddToContext();
|
|
|
|
|
|
|
|
ContextUtility.addToActualContext(orientGraph, getElement());
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-12-20 18:36:17 +01:00
|
|
|
protected boolean removeFromContextTargetVertex(Vertex target)
|
|
|
|
throws ResourceRegistryException {
|
|
|
|
EntityManagement entityManagement = EntityManagement
|
|
|
|
.getEntityManagement(orientGraph, target);
|
|
|
|
if (entityManagement != null) {
|
|
|
|
entityManagement.reallyRemoveFromContext();
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2017-03-23 13:37:15 +01:00
|
|
|
|
2017-01-13 12:21:35 +01:00
|
|
|
@Override
|
2016-12-19 14:59:27 +01:00
|
|
|
public boolean reallyRemoveFromContext() throws ContextException,
|
|
|
|
ResourceRegistryException {
|
2016-12-30 17:31:12 +01:00
|
|
|
getElement();
|
2016-12-20 18:36:17 +01:00
|
|
|
|
|
|
|
RemoveConstraint removeConstraint = RemoveConstraint.keep;
|
|
|
|
|
|
|
|
try {
|
|
|
|
PropagationConstraint propagationConstraint = Utility.getEmbedded(
|
2016-12-30 17:31:12 +01:00
|
|
|
PropagationConstraint.class, element,
|
2016-12-20 18:36:17 +01:00
|
|
|
Relation.PROPAGATION_CONSTRAINT);
|
|
|
|
if (propagationConstraint.getRemoveConstraint() != null) {
|
|
|
|
removeConstraint = propagationConstraint.getRemoveConstraint();
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
logger.warn("Error while getting {} from {}. Assuming {}. "
|
|
|
|
+ "This is really strange and should not occur. "
|
|
|
|
+ "Please Investigate it.",
|
|
|
|
Relation.PROPAGATION_CONSTRAINT,
|
2016-12-30 17:31:12 +01:00
|
|
|
Utility.toJsonString(element, true), removeConstraint);
|
2016-12-20 18:36:17 +01:00
|
|
|
}
|
|
|
|
|
2016-12-30 17:31:12 +01:00
|
|
|
Vertex target = element.getVertex(Direction.IN);
|
2017-01-13 12:21:35 +01:00
|
|
|
|
2016-12-27 15:31:39 +01:00
|
|
|
/*
|
2017-03-23 13:37:15 +01:00
|
|
|
* In any removeConstraint value the relation MUST be removed from
|
2016-12-27 15:31:39 +01:00
|
|
|
* context to avoid to have edge having a source outside of the context.
|
|
|
|
*/
|
2016-12-30 17:31:12 +01:00
|
|
|
ContextUtility.removeFromActualContext(orientGraph, element);
|
2016-12-20 18:36:17 +01:00
|
|
|
|
|
|
|
switch (removeConstraint) {
|
2017-03-23 13:37:15 +01:00
|
|
|
case cascade:
|
2016-12-20 18:36:17 +01:00
|
|
|
removeFromContextTargetVertex(target);
|
2017-03-23 13:37:15 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case cascadeWhenOrphan:
|
|
|
|
Iterable<Edge> iterable = target.getEdges(Direction.IN);
|
|
|
|
Iterator<Edge> iterator = iterable.iterator();
|
|
|
|
int count = 0;
|
|
|
|
OrientEdge edge = null;
|
|
|
|
while (iterator.hasNext()) {
|
|
|
|
edge = (OrientEdge) iterator.next();
|
|
|
|
OrientEdge thisOrientEdge = (OrientEdge) element;
|
|
|
|
if(edge.compareTo(thisOrientEdge)!=0){
|
|
|
|
if(thisOrientEdge.getOutVertex().compareTo(edge.getOutVertex())!=0){
|
|
|
|
count++;
|
|
|
|
break;
|
|
|
|
}else{
|
|
|
|
ContextUtility.removeFromActualContext(orientGraph, edge);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count>0) {
|
|
|
|
logger.trace(
|
|
|
|
"{} point to {} which is not orphan ({} exists). Giving {} directive, it will be not remove from current context.",
|
|
|
|
element, target, edge, removeConstraint);
|
|
|
|
} else {
|
|
|
|
removeFromContextTargetVertex(target);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case keep:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2016-12-20 18:36:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2016-12-19 14:59:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
protected EntityManagement getEntityManagement()
|
|
|
|
throws ResourceRegistryException {
|
|
|
|
EntityManagement entityManagement;
|
2017-01-11 18:00:21 +01:00
|
|
|
if (ConsistsOf.class.isAssignableFrom(erTypeClass)) {
|
2016-12-19 14:59:27 +01:00
|
|
|
entityManagement = new FacetManagement(orientGraph);
|
2017-01-11 18:00:21 +01:00
|
|
|
} else if (IsRelatedTo.class.isAssignableFrom(erTypeClass)) {
|
2016-12-19 14:59:27 +01:00
|
|
|
entityManagement = new ResourceManagement(orientGraph);
|
|
|
|
} else {
|
|
|
|
String error = String.format("{%s is not a %s nor a %s. "
|
|
|
|
+ "This is really strange ad should not occur. "
|
2017-01-11 18:00:21 +01:00
|
|
|
+ "Please Investigate it.", erTypeClass, ConsistsOf.NAME,
|
2016-12-19 14:59:27 +01:00
|
|
|
IsRelatedTo.NAME);
|
|
|
|
throw new ResourceRegistryException(error);
|
|
|
|
}
|
|
|
|
return entityManagement;
|
|
|
|
}
|
|
|
|
|
2016-12-30 17:31:12 +01:00
|
|
|
@SuppressWarnings("unchecked")
|
2016-12-19 14:59:27 +01:00
|
|
|
public static RelationManagement getRelationManagement(
|
|
|
|
OrientGraph orientGraph, Edge edge)
|
|
|
|
throws ResourceRegistryException {
|
|
|
|
OrientEdgeType orientEdgeType = ((OrientEdge) edge).getType();
|
|
|
|
RelationManagement relationManagement = null;
|
|
|
|
if (orientEdgeType.isSubClassOf(ConsistsOf.NAME)) {
|
|
|
|
relationManagement = new ConsistsOfManagement(orientGraph);
|
|
|
|
} else if (orientEdgeType.isSubClassOf(IsRelatedTo.NAME)) {
|
|
|
|
relationManagement = new IsRelatedToManagement(orientGraph);
|
|
|
|
} else {
|
|
|
|
String error = String.format("{%s is not a %s nor a %s. "
|
|
|
|
+ "This is really strange ad should not occur. "
|
|
|
|
+ "Please Investigate it.", edge, ConsistsOf.NAME,
|
|
|
|
IsRelatedTo.NAME);
|
|
|
|
throw new ResourceRegistryException(error);
|
|
|
|
}
|
2016-12-30 17:31:12 +01:00
|
|
|
relationManagement.setElement(edge);
|
2016-12-19 14:59:27 +01:00
|
|
|
return relationManagement;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected boolean deleteTargetVertex(Vertex target)
|
|
|
|
throws ResourceRegistryException {
|
|
|
|
EntityManagement entityManagement = EntityManagement
|
|
|
|
.getEntityManagement(orientGraph, target);
|
|
|
|
if (entityManagement != null) {
|
|
|
|
entityManagement.reallyDelete();
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2017-01-17 16:53:24 +01:00
|
|
|
|
2017-01-13 12:21:35 +01:00
|
|
|
@Override
|
2016-12-19 14:59:27 +01:00
|
|
|
public boolean reallyDelete() throws RelationNotFoundException,
|
|
|
|
ResourceRegistryException {
|
2016-12-30 17:31:12 +01:00
|
|
|
logger.debug(
|
|
|
|
"Going to remove {} with UUID {}. Related {}s will be detached.",
|
|
|
|
baseType, uuid, targetEntityClass.getSimpleName());
|
2017-01-13 12:21:35 +01:00
|
|
|
|
2016-12-30 17:31:12 +01:00
|
|
|
getElement();
|
2016-12-19 14:59:27 +01:00
|
|
|
|
2016-12-20 16:51:57 +01:00
|
|
|
RemoveConstraint removeConstraint = RemoveConstraint.keep;
|
2016-12-19 14:59:27 +01:00
|
|
|
|
|
|
|
try {
|
2016-12-20 16:51:57 +01:00
|
|
|
PropagationConstraint propagationConstraint = Utility.getEmbedded(
|
2016-12-30 17:31:12 +01:00
|
|
|
PropagationConstraint.class, element,
|
2016-12-20 18:36:17 +01:00
|
|
|
Relation.PROPAGATION_CONSTRAINT);
|
|
|
|
if (propagationConstraint.getRemoveConstraint() != null) {
|
2016-12-20 16:51:57 +01:00
|
|
|
removeConstraint = propagationConstraint.getRemoveConstraint();
|
|
|
|
}
|
2016-12-19 14:59:27 +01:00
|
|
|
} catch (Exception e) {
|
|
|
|
logger.warn("Error while getting {} from {}. Assuming {}. "
|
2016-12-20 16:51:57 +01:00
|
|
|
+ "This is really strange and should not occur. "
|
2016-12-19 14:59:27 +01:00
|
|
|
+ "Please Investigate it.",
|
2016-12-20 16:51:57 +01:00
|
|
|
Relation.PROPAGATION_CONSTRAINT,
|
2016-12-30 17:31:12 +01:00
|
|
|
Utility.toJsonString(element, true), removeConstraint);
|
2016-12-19 14:59:27 +01:00
|
|
|
}
|
|
|
|
|
2016-12-30 17:31:12 +01:00
|
|
|
Vertex target = element.getVertex(Direction.IN);
|
|
|
|
element.remove();
|
2016-12-19 14:59:27 +01:00
|
|
|
|
2016-12-20 16:51:57 +01:00
|
|
|
switch (removeConstraint) {
|
2017-02-08 17:31:04 +01:00
|
|
|
case cascade:
|
2016-12-19 14:59:27 +01:00
|
|
|
deleteTargetVertex(target);
|
2017-02-08 17:31:04 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case cascadeWhenOrphan:
|
|
|
|
Iterable<Edge> iterable = target.getEdges(Direction.IN);
|
|
|
|
Iterator<Edge> iterator = iterable.iterator();
|
|
|
|
if (iterator.hasNext()) {
|
|
|
|
logger.trace(
|
|
|
|
"{} point to {} which is not orphan. Giving {} directive, it will be keep.",
|
|
|
|
element, target, removeConstraint);
|
|
|
|
} else {
|
|
|
|
deleteTargetVertex(target);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case keep:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2016-12-19 14:59:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String create(UUID sourceUUID, UUID targetUUID)
|
|
|
|
throws 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(sourceUUID, targetUUID);
|
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();
|
|
|
|
}
|
|
|
|
throw new ResourceRegistryException(e);
|
|
|
|
} finally {
|
|
|
|
if (orientGraph != null) {
|
|
|
|
orientGraph.shutdown();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-01-13 12:21:35 +01:00
|
|
|
|
2017-01-17 16:53:24 +01:00
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
protected Collection<JSONObject> serializeEdges(Iterable<Edge> edges,
|
2017-01-13 12:21:35 +01:00
|
|
|
boolean postFilterPolymorphic) throws ResourceRegistryException {
|
2017-01-17 16:53:24 +01:00
|
|
|
Map<String, JSONObject> visitedSourceResources = new HashMap<>();
|
2017-01-13 12:21:35 +01:00
|
|
|
for (Edge edge : edges) {
|
|
|
|
if (postFilterPolymorphic && edge.getLabel().compareTo(erType) != 0) {
|
2017-01-12 11:13:09 +01:00
|
|
|
continue;
|
|
|
|
}
|
2017-01-13 12:21:35 +01:00
|
|
|
RelationManagement relationManagement = getRelationManagement(
|
|
|
|
orientGraph, edge);
|
2017-01-17 16:53:24 +01:00
|
|
|
visitedSourceResources = relationManagement
|
|
|
|
.fullSerialize(visitedSourceResources);
|
2017-01-11 12:06:50 +01:00
|
|
|
}
|
2017-01-17 16:53:24 +01:00
|
|
|
return visitedSourceResources.values();
|
2017-01-12 11:13:09 +01:00
|
|
|
}
|
2017-01-13 12:21:35 +01:00
|
|
|
|
2017-01-17 16:53:24 +01:00
|
|
|
protected String serializeJSONObjectList(Collection<JSONObject> list) {
|
2017-01-12 11:13:09 +01:00
|
|
|
JSONArray jsonArray = new JSONArray(list);
|
2017-01-11 12:06:50 +01:00
|
|
|
return jsonArray.toString();
|
|
|
|
}
|
2017-01-13 12:21:35 +01:00
|
|
|
|
2017-01-11 12:06:50 +01:00
|
|
|
@Override
|
2017-01-13 12:21:35 +01:00
|
|
|
public String reallyGetAll(boolean polymorphic)
|
|
|
|
throws ResourceRegistryException {
|
2017-01-11 17:11:28 +01:00
|
|
|
Iterable<Edge> edges = orientGraph.getEdgesOfClass(erType, polymorphic);
|
2017-01-17 16:53:24 +01:00
|
|
|
Collection<JSONObject> collection = serializeEdges(edges, false);
|
|
|
|
return serializeJSONObjectList(collection);
|
2017-01-11 12:06:50 +01:00
|
|
|
}
|
2017-01-13 12:21:35 +01:00
|
|
|
|
|
|
|
public String reallyGetAllFrom(UUID uuid, Direction direction,
|
|
|
|
boolean polymorphic) throws ResourceRegistryException {
|
2017-01-11 12:06:50 +01:00
|
|
|
EntityManagement entityManagement = null;
|
|
|
|
try {
|
2017-01-13 12:21:35 +01:00
|
|
|
entityManagement = (EntityManagement) ERManagement
|
|
|
|
.getERManagementFromUUID(orientGraph, uuid);
|
|
|
|
} catch (ResourceRegistryException e) {
|
2017-01-11 12:06:50 +01:00
|
|
|
throw e;
|
2017-01-13 12:21:35 +01:00
|
|
|
} catch (Exception e) {
|
|
|
|
throw new ResourceRegistryException(String.format(
|
|
|
|
"Provided UUID %s does not belogn to any %s",
|
|
|
|
uuid.toString(), Entity.NAME));
|
2017-01-11 12:06:50 +01:00
|
|
|
}
|
2017-01-13 12:21:35 +01:00
|
|
|
|
2017-01-11 12:06:50 +01:00
|
|
|
Vertex vertex = (Vertex) entityManagement.getElement();
|
2017-01-13 12:21:35 +01:00
|
|
|
|
2017-01-12 11:13:09 +01:00
|
|
|
List<JSONObject> list = new ArrayList<>();
|
2017-01-13 17:06:57 +01:00
|
|
|
Iterable<Edge> edges = vertex.getEdges(direction, erType);
|
2017-01-13 12:21:35 +01:00
|
|
|
list.addAll(serializeEdges(edges, !polymorphic));
|
2017-01-12 11:13:09 +01:00
|
|
|
|
|
|
|
return serializeJSONObjectList(list);
|
2017-01-13 12:21:35 +01:00
|
|
|
|
2017-01-11 12:06:50 +01:00
|
|
|
}
|
2017-01-13 12:21:35 +01:00
|
|
|
|
|
|
|
public String allFrom(UUID uuid, Direction direction, boolean polymorphic)
|
|
|
|
throws ResourceRegistryException {
|
2017-01-11 12:06:50 +01:00
|
|
|
try {
|
|
|
|
orientGraph = ContextUtility
|
|
|
|
.getActualSecurityContextGraph(PermissionMode.READER);
|
|
|
|
|
|
|
|
return reallyGetAllFrom(uuid, direction, polymorphic);
|
|
|
|
} catch (ResourceRegistryException e) {
|
|
|
|
throw e;
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new ResourceRegistryException(e);
|
|
|
|
} finally {
|
|
|
|
if (orientGraph != null) {
|
|
|
|
orientGraph.shutdown();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-12-19 14:59:27 +01:00
|
|
|
|
2017-03-23 13:37:15 +01:00
|
|
|
@Override
|
|
|
|
public boolean addToContext() throws ContextException {
|
|
|
|
logger.debug("Going to add {} with UUID {} to actual Context",
|
|
|
|
baseType, uuid);
|
|
|
|
|
|
|
|
try {
|
|
|
|
orientGraph = SecurityContextMapper.getSecurityContextFactory(
|
|
|
|
SecurityContextMapper.ADMIN_SECURITY_CONTEXT_UUID,
|
|
|
|
PermissionMode.WRITER).getTx();
|
|
|
|
|
|
|
|
boolean added = forcedAddToContext();
|
|
|
|
|
|
|
|
orientGraph.commit();
|
|
|
|
logger.info("{} with UUID {} successfully added to actual Context",
|
|
|
|
baseType, uuid);
|
|
|
|
|
|
|
|
return added;
|
|
|
|
} catch (Exception e) {
|
|
|
|
logger.error("Unable to add {} with UUID {} to actual Context",
|
|
|
|
baseType, uuid, e);
|
|
|
|
if (orientGraph != null) {
|
|
|
|
orientGraph.rollback();
|
|
|
|
}
|
|
|
|
throw new ContextException(e);
|
|
|
|
} finally {
|
|
|
|
if (orientGraph != null) {
|
|
|
|
orientGraph.shutdown();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-19 14:59:27 +01:00
|
|
|
}
|