Refs #10238: Refactor Context Port Type

Task-Url: https://support.d4science.org/issues/10238

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/resource-registry@158614 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2017-11-17 14:59:25 +00:00
parent bcf1621866
commit 6edb78bdfd
11 changed files with 450 additions and 424 deletions

View File

@ -1,11 +1,14 @@
package org.gcube.informationsystem.resourceregistry.context;
import java.io.StringWriter;
import java.util.Iterator;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.gcube.informationsystem.model.AccessType;
import org.gcube.informationsystem.model.embedded.Header;
import org.gcube.informationsystem.model.entity.Context;
import org.gcube.informationsystem.model.relation.IsParentOf;
import org.gcube.informationsystem.model.relation.Relation;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextAlreadyPresentException;
@ -20,6 +23,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.databind.JsonNode;
import com.orientechnologies.orient.core.sql.query.OSQLSynchQuery;
import com.tinkerpop.blueprints.Direction;
import com.tinkerpop.blueprints.Edge;
import com.tinkerpop.blueprints.Vertex;
@ -46,6 +50,10 @@ public class ContextManagement extends EntityManagement<Context> {
init();
}
public String getName() {
return jsonNode.get(Context.NAME_PROPERTY).asText();
}
@Override
protected ContextNotFoundException getSpecificElementNotFoundException(ERNotFoundException e) {
return new ContextNotFoundException(e.getMessage(), e.getCause());
@ -61,6 +69,61 @@ public class ContextManagement extends EntityManagement<Context> {
return new ContextAlreadyPresentException(message);
}
protected void checkContext(ContextManagement parentContext, ContextManagement childContext) throws ContextNotFoundException,
ContextAlreadyPresentException, ResourceRegistryException {
if (parentContext != null) {
String parentId = parentContext.getElement().getId().toString();
// TODO Rewrite using Gremlin
String select = "SELECT FROM (TRAVERSE out(" + IsParentOf.NAME
+ ") FROM " + parentId + " MAXDEPTH 1) WHERE "
+ Context.NAME_PROPERTY + "=\"" + childContext.getName() + "\" AND "
+ Context.HEADER_PROPERTY + "." + Header.UUID_PROPERTY
+ "<>\"" + parentContext.toString() + "\"";
logger.trace(select);
StringWriter message = new StringWriter();
message.append("A context with the same name (");
message.append(childContext.getName());
message.append(") has been already created as child of ");
message.append(parentContext.serializeSelfOnly().toString());
logger.trace("Checking if {} -> {}", message, select);
OSQLSynchQuery<Vertex> osqlSynchQuery = new OSQLSynchQuery<Vertex>(
select);
Iterable<Vertex> vertexes = orientGraph.command(osqlSynchQuery)
.execute();
if (vertexes != null && vertexes.iterator().hasNext()) {
throw new ContextAlreadyPresentException(message.toString());
}
} else {
// TODO Rewrite using Gremlin
String select = "SELECT FROM "
+ org.gcube.informationsystem.model.entity.Context.NAME
+ " WHERE " + Context.NAME_PROPERTY + " = \"" + childContext.getName()
+ "\"" + " AND in(\"" + IsParentOf.NAME + "\").size() = 0";
OSQLSynchQuery<Vertex> osqlSynchQuery = new OSQLSynchQuery<Vertex>(
select);
Iterable<Vertex> vertexes = orientGraph.command(osqlSynchQuery)
.execute();
if (vertexes != null && vertexes.iterator().hasNext()) {
throw new ContextAlreadyPresentException(
"A root context with the same name (" + childContext.getName()
+ ") already exist");
}
}
}
@Override
public String serialize() throws ResourceRegistryException {
return serializeAsJson().toString();
@ -111,6 +174,9 @@ public class ContextManagement extends EntityManagement<Context> {
@Override
protected Vertex reallyCreate() throws ERAlreadyPresentException, ResourceRegistryException {
createVertex();
String propertyName = Context.PARENT_PROPERTY;
@ -139,6 +205,7 @@ public class ContextManagement extends EntityManagement<Context> {
// TODO check if parent changed
// TODO check if name changes and check the feasibility
context = (Vertex) ERManagement.updateProperties(oClass, context, jsonNode, ignoreKeys, ignoreStartWithKeys);
return context;

View File

@ -35,7 +35,7 @@ public class ContextManagementImpl implements OLDContextManagement {
private static Logger logger = LoggerFactory
.getLogger(ContextManagementImpl.class);
protected Vertex checkContext(OrientGraph orientGraph, UUID parentContext,
protected Vertex checkContext(OrientGraph orientGraph, UUID parentContext,
String contextName) throws ContextNotFoundException,
ContextException {

View File

@ -199,7 +199,7 @@ public class ContextUtility {
ScopeBean scopeBean = new ScopeBean(fullName);
String name = scopeBean.name();
// TODO Rewrite the previous query using Gremlin
// TODO Rewrite the query using Gremlin
// Please note that this query works because all the scope parts has a
// different name
String select = "SELECT FROM " + Context.class.getSimpleName()

View File

@ -3,8 +3,6 @@
*/
package org.gcube.informationsystem.resourceregistry.context;
import java.util.UUID;
import org.codehaus.jettison.json.JSONObject;
import org.gcube.informationsystem.model.AccessType;
import org.gcube.informationsystem.model.relation.IsParentOf;
@ -96,17 +94,13 @@ public class IsParentOfManagement extends RelationManagement<IsParentOf, Context
}
@Override
protected ContextManagement getSourceEntityManagement(UUID sourceUUID) throws ResourceRegistryException {
ContextManagement contextManagement = new ContextManagement(orientGraph);
contextManagement.setUUID(sourceUUID);
return contextManagement;
protected ContextManagement newSourceEntityManagement() throws ResourceRegistryException {
return new ContextManagement(orientGraph);
}
@Override
protected ContextManagement getTargetEntityManagement(UUID targetUUID) throws ResourceRegistryException {
ContextManagement contextManagement = new ContextManagement(orientGraph);
contextManagement.setUUID(targetUUID);
return contextManagement;
protected ContextManagement newTargetEntityManagement() throws ResourceRegistryException {
return new ContextManagement(orientGraph);
}
}

View File

@ -270,7 +270,9 @@ public abstract class ERManagement<ERType extends ER, El extends Element> {
public El internalUpdate() throws ERNotFoundException, ResourceRegistryException {
try {
reallyUpdate();
HeaderUtility.updateModifiedByAndLastUpdate(element);
((OrientVertex) element).save();
@ -490,6 +492,7 @@ public abstract class ERManagement<ERType extends ER, El extends Element> {
try {
orientGraph = ContextUtility
.getActualSecurityContextGraph(PermissionMode.WRITER, forceAdmin);
element = internalUpdate();
orientGraph.commit();

View File

@ -18,15 +18,22 @@ import org.gcube.informationsystem.resourceregistry.er.relation.IsRelatedToManag
import org.gcube.informationsystem.resourceregistry.er.relation.RelationManagement;
import org.gcube.informationsystem.resourceregistry.schema.SchemaManagementImpl;
import org.gcube.informationsystem.resourceregistry.utils.Utility;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.orientechnologies.orient.core.metadata.schema.OClass;
import com.tinkerpop.blueprints.Edge;
import com.tinkerpop.blueprints.Element;
import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.blueprints.impls.orient.OrientEdge;
import com.tinkerpop.blueprints.impls.orient.OrientEdgeType;
import com.tinkerpop.blueprints.impls.orient.OrientGraph;
import com.tinkerpop.blueprints.impls.orient.OrientVertex;
import com.tinkerpop.blueprints.impls.orient.OrientVertexType;
public class ERManagementUtility {
private static Logger logger = LoggerFactory.getLogger(EntityManagement.class);
/*
@SuppressWarnings("rawtypes")
public static ERManagement getERManagement(AccessType querableType)
@ -81,10 +88,10 @@ public class ERManagementUtility {
private static ERManagement getERManagement(OrientGraph orientGraph,
Element element) throws ResourceRegistryException {
if (element instanceof Vertex) {
return EntityManagement.getEntityManagement(orientGraph,
return getEntityManagement(orientGraph,
(Vertex) element);
} else if (element instanceof Edge) {
return RelationManagement.getRelationManagement(orientGraph,
return getRelationManagement(orientGraph,
(Edge) element);
}
throw new ResourceRegistryException(String.format(
@ -131,4 +138,71 @@ public class ERManagementUtility {
}
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public static EntityManagement getEntityManagement(OrientGraph orientGraph,
Vertex vertex) throws ResourceRegistryException {
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.");
}
OrientVertexType orientVertexType = null;
try {
orientVertexType = ((OrientVertex) vertex).getType();
}catch (Exception e) {
String error = String.format("Unable to detect type of %s. This is really strage please contact the administrator.", vertex.toString());
logger.error(error, e);
throw new ResourceRegistryException(error);
}
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. "
+ "This is really strange and should not occur. "
+ "Please Investigate it.", vertex, Resource.NAME,
Facet.NAME);
throw new ResourceRegistryException(error);
}
entityManagement.setElement(vertex);
return entityManagement;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
public static RelationManagement getRelationManagement(OrientGraph orientGraph, Edge edge)
throws ResourceRegistryException {
if (orientGraph == null) {
throw new ResourceRegistryException(OrientGraph.class.getSimpleName()
+ "instance is null. This is really strage please contact the administrator.");
}
if (edge == null) {
throw new ResourceRegistryException(Edge.class.getSimpleName()
+ "instance is null. This is really strage please contact the administrator.");
}
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);
}
relationManagement.setElement(edge);
return relationManagement;
}
}

View File

@ -3,12 +3,13 @@
*/
package org.gcube.informationsystem.resourceregistry.er.entity;
import java.util.HashMap;
import java.util.Map;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONObject;
import org.gcube.informationsystem.model.AccessType;
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.Relation;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextException;
@ -20,8 +21,6 @@ import org.gcube.informationsystem.resourceregistry.er.ERManagement;
import org.gcube.informationsystem.resourceregistry.er.ERManagementUtility;
import org.gcube.informationsystem.resourceregistry.er.relation.RelationManagement;
import org.gcube.informationsystem.resourceregistry.utils.Utility;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.tinkerpop.blueprints.Direction;
import com.tinkerpop.blueprints.Edge;
@ -29,7 +28,6 @@ import com.tinkerpop.blueprints.Element;
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)
@ -37,9 +35,14 @@ import com.tinkerpop.blueprints.impls.orient.OrientVertexType;
public abstract class EntityManagement<E extends Entity> extends
ERManagement<E, Vertex> {
private static Logger staticLogger = LoggerFactory
.getLogger(EntityManagement.class);
/**
* Provide a cache edge-internal-id -> RelationManagement
* this avoid to recreate the relationManagement of already visited
* edges
*/
@SuppressWarnings("rawtypes")
protected Map<String, RelationManagement> relationManagements;
protected EntityManagement(AccessType accessType) {
super(accessType);
@ -53,8 +56,20 @@ public abstract class EntityManagement<E extends Entity> extends
.toUpperCase());
this.ignoreStartWithKeys.add(OrientVertex.CONNECTION_OUT_PREFIX
.toUpperCase());
this.relationManagements = new HashMap<>();
}
@SuppressWarnings("rawtypes")
protected RelationManagement getRelationManagement(Edge edge) throws ResourceRegistryException {
String id = edge.getId().toString();
RelationManagement relationManagement = relationManagements.get(id);
if(relationManagement==null) {
relationManagement = RelationManagement.getRelationManagement(orientGraph, edge);
}
return relationManagement;
}
protected EntityManagement(AccessType accessType, OrientGraph orientGraph) {
this(accessType);
@ -167,8 +182,7 @@ public abstract class EntityManagement<E extends Entity> extends
for (Edge edge : edges) {
@SuppressWarnings("rawtypes")
RelationManagement relationManagement = RelationManagement
.getRelationManagement(orientGraph, edge);
RelationManagement relationManagement = getRelationManagement(edge);
relationManagement.internalAddToContext();
}
@ -185,50 +199,14 @@ public abstract class EntityManagement<E extends Entity> extends
for (Edge edge : edges) {
@SuppressWarnings("rawtypes")
RelationManagement relationManagement = RelationManagement
.getRelationManagement(orientGraph, edge);
RelationManagement relationManagement = getRelationManagement(edge);
relationManagement.internalRemoveFromContext();
}
return true;
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public static EntityManagement getEntityManagement(OrientGraph orientGraph,
Vertex vertex) throws ResourceRegistryException {
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.");
}
OrientVertexType orientVertexType = null;
try {
orientVertexType = ((OrientVertex) vertex).getType();
}catch (Exception e) {
String error = String.format("Unable to detect type of %s. This is really strage please contact the administrator.", vertex.toString());
staticLogger.error(error, e);
throw new ResourceRegistryException(error);
}
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. "
+ "This is really strange and should not occur. "
+ "Please Investigate it.", vertex, Resource.NAME,
Facet.NAME);
throw new ResourceRegistryException(error);
}
entityManagement.setElement(vertex);
return entityManagement;
}
@Override
public String reallyGetAll(boolean polymorphic) throws ResourceRegistryException {
@ -236,7 +214,7 @@ public abstract class EntityManagement<E extends Entity> extends
Iterable<Vertex> iterable = orientGraph.getVerticesOfClass(erType, polymorphic);
for(Vertex vertex : iterable){
@SuppressWarnings("rawtypes")
EntityManagement entityManagement = getEntityManagement(orientGraph, vertex);
EntityManagement entityManagement = ERManagementUtility.getEntityManagement(orientGraph, vertex);
try {
JSONObject jsonObject = entityManagement.serializeAsJson();
jsonArray.put(jsonObject);

View File

@ -20,6 +20,7 @@ import org.gcube.informationsystem.resourceregistry.api.exceptions.er.ERNotFound
import org.gcube.informationsystem.resourceregistry.api.rest.AccessPath;
import org.gcube.informationsystem.resourceregistry.context.ContextUtility;
import org.gcube.informationsystem.resourceregistry.context.SecurityContextMapper.PermissionMode;
import org.gcube.informationsystem.resourceregistry.er.ERManagementUtility;
import org.gcube.informationsystem.resourceregistry.er.relation.ConsistsOfManagement;
import org.gcube.informationsystem.resourceregistry.er.relation.IsRelatedToManagement;
import org.gcube.informationsystem.resourceregistry.er.relation.RelationManagement;
@ -330,7 +331,7 @@ public class ResourceManagement extends EntityManagement<Resource> {
Vertex vertex = (Vertex) element;
@SuppressWarnings("rawtypes")
EntityManagement entityManagement = EntityManagement.getEntityManagement(orientGraph, vertex);
EntityManagement entityManagement = ERManagementUtility.getEntityManagement(orientGraph, vertex);
try {
JSONObject jsonObject = entityManagement.serializeAsJson();
jsonArray.put(jsonObject);

View File

@ -3,8 +3,6 @@
*/
package org.gcube.informationsystem.resourceregistry.er.relation;
import java.util.UUID;
import org.gcube.informationsystem.model.AccessType;
import org.gcube.informationsystem.model.relation.ConsistsOf;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
@ -48,17 +46,13 @@ public class ConsistsOfManagement extends RelationManagement<ConsistsOf, Resourc
}
@Override
protected ResourceManagement getSourceEntityManagement(UUID sourceUUID) throws ResourceRegistryException {
ResourceManagement resourceManagement = new ResourceManagement(orientGraph);
resourceManagement.setUUID(sourceUUID);
return resourceManagement;
protected ResourceManagement newSourceEntityManagement() throws ResourceRegistryException {
return new ResourceManagement(orientGraph);
}
@Override
protected FacetManagement getTargetEntityManagement(UUID targetUUID) throws ResourceRegistryException {
FacetManagement facetManagement = new FacetManagement(orientGraph);
facetManagement.setUUID(targetUUID);
return facetManagement;
protected FacetManagement newTargetEntityManagement() throws ResourceRegistryException {
return new FacetManagement(orientGraph);
}
}

View File

@ -3,8 +3,6 @@
*/
package org.gcube.informationsystem.resourceregistry.er.relation;
import java.util.UUID;
import org.gcube.informationsystem.model.AccessType;
import org.gcube.informationsystem.model.relation.IsRelatedTo;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
@ -47,17 +45,13 @@ public class IsRelatedToManagement extends RelationManagement<IsRelatedTo, Resou
}
@Override
protected ResourceManagement getSourceEntityManagement(UUID sourceUUID) throws ResourceRegistryException {
ResourceManagement resourceManagement = new ResourceManagement(orientGraph);
resourceManagement.setUUID(sourceUUID);
return resourceManagement;
protected ResourceManagement newSourceEntityManagement() throws ResourceRegistryException {
return new ResourceManagement(orientGraph);
}
@Override
protected ResourceManagement getTargetEntityManagement(UUID targetUUID) throws ResourceRegistryException {
ResourceManagement resourceManagement = new ResourceManagement(orientGraph);
resourceManagement.setUUID(targetUUID);
return resourceManagement;
protected ResourceManagement newTargetEntityManagement() throws ResourceRegistryException {
return new ResourceManagement(orientGraph);
}
}

View File

@ -51,13 +51,13 @@ import com.tinkerpop.blueprints.impls.orient.OrientGraph;
* @author Luca Frosini (ISTI - CNR)
*/
@SuppressWarnings("rawtypes")
public abstract class RelationManagement<R extends Relation, S extends EntityManagement, T extends EntityManagement> extends
ERManagement<R, Edge> {
public abstract class RelationManagement<R extends Relation, S extends EntityManagement, T extends EntityManagement>
extends ERManagement<R, Edge> {
protected final Class<? extends Entity> targetEntityClass;
protected S sourceEntityManagement;
protected T targetEntityManagement;
private S sourceEntityManagemen;
private T targetEntityManagemen;
protected RelationManagement(AccessType accessType) {
super(accessType);
@ -71,21 +71,21 @@ public abstract class RelationManagement<R extends Relation, S extends EntityMan
this.ignoreKeys.add(OrientBaseGraph.CONNECTION_OUT.toUpperCase());
switch (accessType) {
case CONSISTS_OF:
this.targetEntityClass = Facet.class;
break;
case IS_RELATED_TO:
this.targetEntityClass = Resource.class;
break;
default:
this.targetEntityClass = Resource.class;
break;
case CONSISTS_OF:
this.targetEntityClass = Facet.class;
break;
case IS_RELATED_TO:
this.targetEntityClass = Resource.class;
break;
default:
this.targetEntityClass = Resource.class;
break;
}
sourceEntityManagement = null;
targetEntityManagement = null;
sourceEntityManagemen = null;
targetEntityManagemen = null;
}
@ -93,6 +93,34 @@ public abstract class RelationManagement<R extends Relation, S extends EntityMan
this(accessType);
this.orientGraph = orientGraph;
}
@SuppressWarnings("unchecked")
protected S getSourceEntityManagemen() throws ResourceRegistryException {
if (sourceEntityManagemen == null) {
Vertex source = getElement().getVertex(Direction.OUT);
sourceEntityManagemen = newSourceEntityManagement();
sourceEntityManagemen.setElement(source);
}
return sourceEntityManagemen;
}
@SuppressWarnings("unchecked")
public T getTargetEntityManagemen() throws ResourceRegistryException {
if (targetEntityManagemen == null) {
Vertex target = getElement().getVertex(Direction.IN);
targetEntityManagemen = newTargetEntityManagement();
targetEntityManagemen.setElement(target);
}
return targetEntityManagemen;
}
public void setSourceEntityManagement(S sourceEntityManagement) {
this.sourceEntityManagemen = sourceEntityManagement;
}
public void setTargetEntityManagement(T targetEntityManagement) {
this.targetEntityManagemen = targetEntityManagement;
}
@Override
public String serialize() throws ResourceRegistryException {
@ -103,29 +131,19 @@ public abstract class RelationManagement<R extends Relation, S extends EntityMan
public JSONObject serializeAsJson() throws ResourceRegistryException {
return serializeAsJson(true, true);
}
@SuppressWarnings("unchecked")
public JSONObject serializeAsJson(boolean includeSource, boolean includeTarget) throws ResourceRegistryException {
JSONObject relation = serializeSelfOnly();
try {
if(includeSource) {
if(sourceEntityManagement == null) {
Vertex source = element.getVertex(Direction.OUT);
sourceEntityManagement = (S) EntityManagement.getEntityManagement(orientGraph, source);
}
relation.put(Relation.SOURCE_PROPERTY, sourceEntityManagement.serializeSelfOnly());
if (includeSource) {
relation.put(Relation.SOURCE_PROPERTY, getSourceEntityManagemen().serializeSelfOnly());
}
if(includeTarget) {
if(targetEntityManagement == null) {
Vertex target = element.getVertex(Direction.IN);
targetEntityManagement = (T) EntityManagement
.getEntityManagement(orientGraph, target);
}
relation.put(Relation.TARGET_PROPERTY, targetEntityManagement.serializeAsJson());
if (includeTarget) {
relation.put(Relation.TARGET_PROPERTY, getTargetEntityManagemen().serializeAsJson());
}
} catch (ResourceRegistryException e) {
logger.error("Unable to correctly serialize {}. This is really strange and should not occur.", element, e);
throw e;
@ -137,8 +155,7 @@ public abstract class RelationManagement<R extends Relation, S extends EntityMan
return relation;
}
protected Map<String, JSONObject> fullSerialize(
Map<String, JSONObject> visitedSourceResources)
protected Map<String, JSONObject> fullSerialize(Map<String, JSONObject> visitedSourceResources)
throws ResourceRegistryException {
Vertex source = element.getVertex(Direction.OUT);
@ -146,34 +163,30 @@ public abstract class RelationManagement<R extends Relation, S extends EntityMan
JSONObject sourceResource = visitedSourceResources.get(id);
if (sourceResource == null) {
ResourceManagement resourceManagement = (ResourceManagement) EntityManagement
ResourceManagement resourceManagement = (ResourceManagement) ERManagementUtility
.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());
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);
}
}
if (this instanceof IsRelatedToManagement) {
sourceResource = ResourceManagement.addIsRelatedTo(sourceResource,
serializeAsJson());
sourceResource = ResourceManagement.addIsRelatedTo(sourceResource, serializeAsJson());
} else if (this instanceof ConsistsOfManagement) {
sourceResource = ResourceManagement.addConsistsOf(sourceResource,
serializeAsJson());
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());
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);
}
@ -181,160 +194,140 @@ public abstract class RelationManagement<R extends Relation, S extends EntityMan
return visitedSourceResources;
}
@Override
protected Edge reallyCreate() throws ResourceRegistryException {
if(sourceEntityManagement==null) {
if(!jsonNode.has(Relation.SOURCE_PROPERTY)){
throw new ResourceRegistryException(
"Error while creating relation. No source definition found");
if (sourceEntityManagemen == null) {
if (!jsonNode.has(Relation.SOURCE_PROPERTY)) {
throw new ResourceRegistryException("Error while creating relation. No source definition found");
}
UUID sourceUUID = org.gcube.informationsystem.impl.utils.Utility
.getUUIDFromJsonNode(jsonNode.get(Relation.SOURCE_PROPERTY));
setSourceUUID(sourceUUID);
sourceEntityManagemen = newSourceEntityManagement();
sourceEntityManagemen.setUUID(sourceUUID);
}
if(targetEntityManagement == null) {
targetEntityManagement = getTargetEntityManagement();
if (targetEntityManagemen == null) {
targetEntityManagemen = newTargetEntityManagement();
if (!jsonNode.has(Relation.TARGET_PROPERTY)) {
throw new ResourceRegistryException(
"Error while creating " + erType + ". No target definition found");
throw new ResourceRegistryException("Error while creating " + erType + ". No target definition found");
}
try {
targetEntityManagement.setJSON(jsonNode.get(Relation.TARGET_PROPERTY));
}catch (SchemaException e) {
targetEntityManagemen.setJSON(jsonNode.get(Relation.TARGET_PROPERTY));
} catch (SchemaException e) {
StringWriter errorMessage = new StringWriter();
errorMessage.append("A ");
errorMessage.append(erType);
errorMessage.append(" can be only created beetween ");
errorMessage.append(sourceEntityManagement.getAccessType().getName());
errorMessage.append(sourceEntityManagemen.getAccessType().getName());
errorMessage.append(" and ");
errorMessage.append(targetEntityManagement.getAccessType().getName());
errorMessage.append(targetEntityManagemen.getAccessType().getName());
throw new ResourceRegistryException(errorMessage.toString(), e);
}
try {
targetEntityManagement.getElement();
targetEntityManagemen.getElement();
} catch (Exception e) {
targetEntityManagement.internalCreate();
targetEntityManagemen.internalCreate();
}
}
// Revisit this if-else because should be not needed anymore.
/*
if(this instanceof IsParentOfManagement) {
if (!(sourceEntityManagement instanceof ContextManagement && targetEntityManagement instanceof ContextManagement)) {
String error = String.format(
"A %s can be only created beetween two %s. "
+ "Cannot instatiate %s beetween %s -> %s ",
IsParentOf.NAME, Context.NAME, erType,
sourceEntityManagement.serialize(),
targetEntityManagement.serialize());
throw new ResourceRegistryException(error);
}
} else {
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,
erType, sourceEntityManagement.serialize(),
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 ", accessType.getName(),
Resource.NAME, Relation.TARGET_PROPERTY, erType,
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 ", accessType.getName(),
Facet.NAME, Relation.TARGET_PROPERTY, erType,
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);
}
}
*/
* if(this instanceof IsParentOfManagement) { if (!(sourceEntityManagement
* instanceof ContextManagement && targetEntityManagement instanceof
* ContextManagement)) { String error = String.format(
* "A %s can be only created beetween two %s. " +
* "Cannot instatiate %s beetween %s -> %s ", IsParentOf.NAME, Context.NAME,
* erType, sourceEntityManagement.serialize(),
* targetEntityManagement.serialize()); throw new
* ResourceRegistryException(error); } } else { 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, erType, sourceEntityManagement.serialize(),
* 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 ", accessType.getName(),
* Resource.NAME, Relation.TARGET_PROPERTY, erType,
* 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 ", accessType.getName(), Facet.NAME,
* Relation.TARGET_PROPERTY, erType, 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); }
*
* }
*/
// end of if-else to be revisited
logger.trace("Creating {} beetween {} -> {}", erType,
sourceEntityManagement.serialize(),
targetEntityManagement.serialize());
Vertex source = (Vertex) sourceEntityManagement.getElement();
Vertex target = (Vertex) targetEntityManagement.getElement();
logger.trace("Creating {} beetween {} -> {}", erType, getSourceEntityManagemen().serialize(),
getTargetEntityManagemen().serialize());
Vertex source = (Vertex) getSourceEntityManagemen().getElement();
Vertex target = (Vertex) getTargetEntityManagemen().getElement();
element = orientGraph.addEdge(null, source, target, erType);
ERManagement.updateProperties(oClass, element, jsonNode, ignoreKeys,
ignoreStartWithKeys);
ERManagement.updateProperties(oClass, element, jsonNode, ignoreKeys, ignoreStartWithKeys);
/* This code has been moved in ERManagement.internalCreate()
HeaderUtility.addHeader(element, null);
ContextUtility.addToActualContext(orientGraph, element);
/*
* This code has been moved in ERManagement.internalCreate()
* HeaderUtility.addHeader(element, null);
* ContextUtility.addToActualContext(orientGraph, element);
*
* ((OrientEdge) element).save();
*/
((OrientEdge) element).save();
*/
logger.info("{} successfully created", erType);
return element;
}
protected abstract S getSourceEntityManagement(UUID sourceUUID) throws ResourceRegistryException;
protected abstract S newSourceEntityManagement() throws ResourceRegistryException;
protected abstract T getTargetEntityManagement(UUID targetUUID) throws ResourceRegistryException;
protected abstract T newTargetEntityManagement() throws ResourceRegistryException;
public void setSourceEntityManagement(S sourceEntityManagement) {
this.sourceEntityManagement = sourceEntityManagement;
}
public void setTargetEntityManagement(T targetEntityManagement) {
this.targetEntityManagement = targetEntityManagement;
/*
public void setSourceUUID(UUID sourceUUID) throws ResourceRegistryException {
this.sourceEntityManagemen = newSourceEntityManagement();
this.sourceEntityManagemen.setUUID(sourceUUID);
}
public void setSourceUUID(UUID sourceUUID) throws ResourceRegistryException {
this.sourceEntityManagement = getSourceEntityManagement(sourceUUID);
}
public void setTargetUUID(UUID targetUUID) throws ResourceRegistryException {
this.targetEntityManagement = getTargetEntityManagement(targetUUID);
this.targetEntityManagemen = newTargetEntityManagement();
this.targetEntityManagemen.setUUID(targetUUID);
}
*/
@Override
protected Edge reallyUpdate() throws ResourceRegistryException {
logger.debug("Trying to update {} : {}", erType, jsonNode);
Edge edge = getElement();
ERManagement.updateProperties(oClass, edge, jsonNode, ignoreKeys,
ignoreStartWithKeys);
ERManagement.updateProperties(oClass, edge, jsonNode, ignoreKeys, ignoreStartWithKeys);
if (accessType.compareTo(AccessType.CONSISTS_OF)==0) {
if (accessType.compareTo(AccessType.CONSISTS_OF) == 0) {
JsonNode target = jsonNode.get(Relation.TARGET_PROPERTY);
if (target != null) {
FacetManagement fm = new FacetManagement(orientGraph);
@ -350,52 +343,44 @@ public abstract class RelationManagement<R extends Relation, S extends EntityMan
}
@Override
protected boolean reallyAddToContext() throws ContextException,
ResourceRegistryException {
protected boolean reallyAddToContext() throws ContextException, ResourceRegistryException {
getElement();
AddConstraint addConstraint = AddConstraint.unpropagate;
try {
PropagationConstraint propagationConstraint = Utility.getEmbedded(
PropagationConstraint.class, element,
PropagationConstraint propagationConstraint = Utility.getEmbedded(PropagationConstraint.class, element,
Relation.PROPAGATION_CONSTRAINT);
if (propagationConstraint.getAddConstraint() != null) {
addConstraint = propagationConstraint.getAddConstraint();
}else {
String error = String.format("%s.%s in %s is null"
+ "This is really strange and should not occur. "
+ "Please Investigate it.",
Relation.PROPAGATION_CONSTRAINT,
PropagationConstraint.ADD_PROPERTY,
} else {
String error = String.format(
"%s.%s in %s is null" + "This is really strange and should not occur. "
+ "Please Investigate it.",
Relation.PROPAGATION_CONSTRAINT, PropagationConstraint.ADD_PROPERTY,
Utility.toJsonString(element, true));
logger.error(error);
throw new ResourceRegistryException(error);
}
} catch (Exception e) {
String error = String.format("Error while getting %s from %s while performing AddToContext."
+ "This is really strange and should not occur. "
+ "Please Investigate it.",
Relation.PROPAGATION_CONSTRAINT,
Utility.toJsonString(element, true));
String error = String.format(
"Error while getting %s from %s while performing AddToContext."
+ "This is really strange and should not occur. " + "Please Investigate it.",
Relation.PROPAGATION_CONSTRAINT, Utility.toJsonString(element, true));
logger.warn(error);
throw new ResourceRegistryException(error, e);
}
Vertex target = element.getVertex(Direction.IN);
switch (addConstraint) {
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.
* 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.
*/
EntityManagement entityManagement = EntityManagement
.getEntityManagement(orientGraph, target);
entityManagement.internalAddToContext();
ContextUtility.addToActualContext(orientGraph, getElement());
getTargetEntityManagemen().internalAddToContext();
ContextUtility.addToActualContext(orientGraph, getElement());
break;
case unpropagate:
@ -408,32 +393,24 @@ public abstract class RelationManagement<R extends Relation, S extends EntityMan
return true;
}
public boolean forcedAddToContext() throws ContextException,
ResourceRegistryException {
public boolean forcedAddToContext() throws ContextException, ResourceRegistryException {
getElement();
/* Adding source to Context */
Vertex source = element.getVertex(Direction.OUT);
EntityManagement entityManagement = EntityManagement
.getEntityManagement(orientGraph, source);
entityManagement.internalAddToContext();
getSourceEntityManagemen().internalAddToContext();
/* Adding target to Context */
Vertex target = element.getVertex(Direction.IN);
entityManagement = EntityManagement
.getEntityManagement(orientGraph, target);
entityManagement.internalAddToContext();
getTargetEntityManagemen().internalAddToContext();
ContextUtility.addToActualContext(orientGraph, getElement());
return true;
}
protected boolean removeFromContextTargetVertex(Vertex target)
throws ResourceRegistryException {
EntityManagement entityManagement = EntityManagement
.getEntityManagement(orientGraph, target);
/*
protected boolean removeFromContextTargetVertex(Vertex target) throws ResourceRegistryException {
EntityManagement entityManagement = EntityManagement.getEntityManagement(orientGraph, target);
if (entityManagement != null) {
entityManagement.internalRemoveFromContext();
return true;
@ -441,81 +418,77 @@ public abstract class RelationManagement<R extends Relation, S extends EntityMan
return false;
}
}
*/
@Override
protected boolean reallyRemoveFromContext() throws ContextException,
ResourceRegistryException {
protected boolean reallyRemoveFromContext() throws ContextException, ResourceRegistryException {
getElement();
RemoveConstraint removeConstraint = RemoveConstraint.keep;
try {
PropagationConstraint propagationConstraint = Utility.getEmbedded(
PropagationConstraint.class, element,
PropagationConstraint propagationConstraint = Utility.getEmbedded(PropagationConstraint.class, element,
Relation.PROPAGATION_CONSTRAINT);
if (propagationConstraint.getRemoveConstraint() != null) {
removeConstraint = propagationConstraint.getRemoveConstraint();
}else{
String error = String.format("%s.%s in %s is null"
+ "This is really strange and should not occur. "
+ "Please Investigate it.",
Relation.PROPAGATION_CONSTRAINT,
PropagationConstraint.REMOVE_PROPERTY,
} else {
String error = String.format(
"%s.%s in %s is null" + "This is really strange and should not occur. "
+ "Please Investigate it.",
Relation.PROPAGATION_CONSTRAINT, PropagationConstraint.REMOVE_PROPERTY,
Utility.toJsonString(element, true));
logger.error(error);
throw new ResourceRegistryException(error);
}
} catch (Exception e) {
String error = String.format("Error while getting %s from %s while performing RemoveFromContext."
+ "This is really strange and should not occur. "
+ "Please Investigate it.",
Relation.PROPAGATION_CONSTRAINT,
Utility.toJsonString(element, true));
String error = String.format(
"Error while getting %s from %s while performing RemoveFromContext."
+ "This is really strange and should not occur. " + "Please Investigate it.",
Relation.PROPAGATION_CONSTRAINT, Utility.toJsonString(element, true));
logger.error(error);
throw new ResourceRegistryException(error, e);
}
Vertex target = element.getVertex(Direction.IN);
/*
* In any removeConstraint value the relation MUST be removed from
* context to avoid to have edge having a source outside of the context.
* In any removeConstraint value the relation MUST be removed from context to
* avoid to have edge having a source outside of the context.
*/
ContextUtility.removeFromActualContext(orientGraph, element);
switch (removeConstraint) {
case cascade:
removeFromContextTargetVertex(target);
getTargetEntityManagemen().internalRemoveFromContext();
break;
case cascadeWhenOrphan:
Vertex target = (Vertex) getTargetEntityManagemen().getElement();
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){
OrientEdge thisOrientEdge = (OrientEdge) getElement();
if (edge.compareTo(thisOrientEdge) != 0) {
if (thisOrientEdge.getOutVertex().compareTo(edge.getOutVertex()) != 0) {
count++;
break;
}
/*
else{
ContextUtility.removeFromActualContext(orientGraph, edge);
}
*/
* else{ ContextUtility.removeFromActualContext(orientGraph, edge); }
*/
}
}
if (count>0) {
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);
getTargetEntityManagemen().internalRemoveFromContext();
}
break;
@ -530,43 +503,21 @@ public abstract class RelationManagement<R extends Relation, S extends EntityMan
}
@SuppressWarnings("unchecked")
protected T getTargetEntityManagement()
throws ResourceRegistryException {
T entityManagement;
switch (accessType) {
case CONSISTS_OF:
entityManagement = (T) new FacetManagement(orientGraph);
break;
case IS_RELATED_TO:
entityManagement = (T) new ResourceManagement(orientGraph);
break;
default:
String error = String.format("{%s is not a %s nor a %s. "
+ "This is really strange ad should not occur. "
+ "Please Investigate it.", accessType.getName(), ConsistsOf.NAME,
IsRelatedTo.NAME);
throw new ResourceRegistryException(error);
}
return entityManagement;
}
@SuppressWarnings("unchecked")
public static RelationManagement getRelationManagement(
OrientGraph orientGraph, Edge edge)
public static RelationManagement getRelationManagement(OrientGraph orientGraph, Edge edge)
throws ResourceRegistryException {
if(orientGraph==null){
throw new ResourceRegistryException(OrientGraph.class.getSimpleName() + "instance is null. This is really strage please contact the administrator.");
if (orientGraph == null) {
throw new ResourceRegistryException(OrientGraph.class.getSimpleName()
+ "instance is null. This is really strage please contact the administrator.");
}
if(edge==null){
throw new ResourceRegistryException(Edge.class.getSimpleName() + "instance is null. This is really strage please contact the administrator.");
if (edge == null) {
throw new ResourceRegistryException(Edge.class.getSimpleName()
+ "instance is null. This is really strage please contact the administrator.");
}
OrientEdgeType orientEdgeType = ((OrientEdge) edge).getType();
RelationManagement relationManagement = null;
if (orientEdgeType.isSubClassOf(ConsistsOf.NAME)) {
@ -574,78 +525,60 @@ public abstract class RelationManagement<R extends Relation, S extends EntityMan
} 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);
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);
}
relationManagement.setElement(edge);
return relationManagement;
}
protected boolean deleteTargetVertex(Vertex target)
throws ResourceRegistryException {
EntityManagement entityManagement = EntityManagement.getEntityManagement(orientGraph, target);
if (entityManagement != null) {
return entityManagement.internalDelete();
} else {
return false;
}
}
@Override
protected boolean reallyDelete() throws RelationNotFoundException,
ResourceRegistryException {
logger.debug(
"Going to remove {} with UUID {}. Related {}s will be detached.",
accessType.getName(), uuid, targetEntityClass.getSimpleName());
protected boolean reallyDelete() throws RelationNotFoundException, ResourceRegistryException {
logger.debug("Going to remove {} with UUID {}. Related {}s will be detached.", accessType.getName(), uuid,
targetEntityClass.getSimpleName());
getElement();
RemoveConstraint removeConstraint = RemoveConstraint.keep;
try {
PropagationConstraint propagationConstraint = Utility.getEmbedded(
PropagationConstraint.class, element,
PropagationConstraint propagationConstraint = Utility.getEmbedded(PropagationConstraint.class, element,
Relation.PROPAGATION_CONSTRAINT);
if (propagationConstraint.getRemoveConstraint() != null) {
removeConstraint = propagationConstraint.getRemoveConstraint();
}else{
String error = String.format("%s.%s in %s is null"
+ "This is really strange and should not occur. "
+ "Please Investigate it.",
Relation.PROPAGATION_CONSTRAINT,
PropagationConstraint.REMOVE_PROPERTY,
} else {
String error = String.format(
"%s.%s in %s is null" + "This is really strange and should not occur. "
+ "Please Investigate it.",
Relation.PROPAGATION_CONSTRAINT, PropagationConstraint.REMOVE_PROPERTY,
Utility.toJsonString(element, true));
logger.error(error);
throw new ResourceRegistryException(error);
}
} catch (Exception e) {
logger.warn("Error while getting {} from {}. Assuming {}. "
+ "This is really strange and should not occur. "
+ "Please Investigate it.",
Relation.PROPAGATION_CONSTRAINT,
Utility.toJsonString(element, true), removeConstraint);
logger.warn(
"Error while getting {} from {}. Assuming {}. " + "This is really strange and should not occur. "
+ "Please Investigate it.",
Relation.PROPAGATION_CONSTRAINT, Utility.toJsonString(element, true), removeConstraint);
}
Vertex target = element.getVertex(Direction.IN);
Vertex target = (Vertex) getTargetEntityManagemen().getElement();
element.remove();
switch (removeConstraint) {
case cascade:
deleteTargetVertex(target);
getTargetEntityManagemen().internalDelete();
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);
logger.trace("{} point to {} which is not orphan. Giving {} directive, it will be keep.", element,
target, removeConstraint);
} else {
deleteTargetVertex(target);
getTargetEntityManagemen().internalDelete();
}
break;
@ -660,15 +593,13 @@ public abstract class RelationManagement<R extends Relation, S extends EntityMan
}
@SuppressWarnings("unused")
private String create(UUID sourceUUID, UUID targetUUID)
throws ResourceRegistryException {
private String create(UUID sourceUUID, UUID targetUUID) throws ResourceRegistryException {
try {
orientGraph = ContextUtility
.getActualSecurityContextGraph(PermissionMode.WRITER, forceAdmin);
setSourceUUID(sourceUUID);
setTargetUUID(targetUUID);
orientGraph = ContextUtility.getActualSecurityContextGraph(PermissionMode.WRITER, forceAdmin);
getSourceEntityManagemen().setUUID(sourceUUID);
getTargetEntityManagemen().setUUID(targetUUID);
element = reallyCreate();
orientGraph.commit();
@ -691,19 +622,17 @@ public abstract class RelationManagement<R extends Relation, S extends EntityMan
}
}
}
@SuppressWarnings("unchecked")
protected Collection<JSONObject> serializeEdges(Iterable<Edge> edges,
boolean postFilterPolymorphic) throws ResourceRegistryException {
protected Collection<JSONObject> serializeEdges(Iterable<Edge> edges, boolean postFilterPolymorphic)
throws ResourceRegistryException {
Map<String, JSONObject> visitedSourceResources = new HashMap<>();
for (Edge edge : edges) {
if (postFilterPolymorphic && edge.getLabel().compareTo(erType) != 0) {
continue;
}
RelationManagement relationManagement = getRelationManagement(
orientGraph, edge);
visitedSourceResources = relationManagement
.fullSerialize(visitedSourceResources);
RelationManagement relationManagement = getRelationManagement(orientGraph, edge);
visitedSourceResources = relationManagement.fullSerialize(visitedSourceResources);
}
return visitedSourceResources.values();
}
@ -714,24 +643,22 @@ public abstract class RelationManagement<R extends Relation, S extends EntityMan
}
@Override
public String reallyGetAll(boolean polymorphic)
throws ResourceRegistryException {
public String reallyGetAll(boolean polymorphic) throws ResourceRegistryException {
Iterable<Edge> edges = orientGraph.getEdgesOfClass(erType, polymorphic);
Collection<JSONObject> collection = serializeEdges(edges, false);
return serializeJSONObjectList(collection);
}
public String reallyGetAllFrom(UUID uuid, Direction direction,
boolean polymorphic) throws ResourceRegistryException {
public String reallyGetAllFrom(UUID uuid, Direction direction, boolean polymorphic)
throws ResourceRegistryException {
EntityManagement entityManagement = null;
try {
entityManagement = (EntityManagement) ERManagementUtility.getERManagementFromUUID(orientGraph, uuid);
} catch (ResourceRegistryException e) {
throw e;
} catch (Exception e) {
throw new ResourceRegistryException(String.format(
"Provided UUID %s does not belogn to any %s",
uuid.toString(), Entity.NAME));
throw new ResourceRegistryException(
String.format("Provided UUID %s does not belogn to any %s", uuid.toString(), Entity.NAME));
}
Vertex vertex = (Vertex) entityManagement.getElement();
@ -744,11 +671,9 @@ public abstract class RelationManagement<R extends Relation, S extends EntityMan
}
public String allFrom(UUID uuid, Direction direction, boolean polymorphic)
throws ResourceRegistryException {
public String allFrom(UUID uuid, Direction direction, boolean polymorphic) throws ResourceRegistryException {
try {
orientGraph = ContextUtility
.getActualSecurityContextGraph(PermissionMode.READER, forceAdmin);
orientGraph = ContextUtility.getActualSecurityContextGraph(PermissionMode.READER, forceAdmin);
return reallyGetAllFrom(uuid, direction, polymorphic);
} catch (ResourceRegistryException e) {
@ -764,23 +689,19 @@ public abstract class RelationManagement<R extends Relation, S extends EntityMan
@Override
public boolean addToContext() throws ERNotFoundException, ContextException {
logger.debug("Going to add {} with UUID {} to actual Context",
accessType.getName(), uuid);
logger.debug("Going to add {} with UUID {} to actual Context", accessType.getName(), uuid);
try {
orientGraph = ContextUtility.getActualSecurityContextGraph(
PermissionMode.WRITER, true);
orientGraph = ContextUtility.getActualSecurityContextGraph(PermissionMode.WRITER, true);
boolean added = forcedAddToContext();
orientGraph.commit();
logger.info("{} with UUID {} successfully added to actual Context",
accessType.getName(), uuid);
logger.info("{} with UUID {} successfully added to actual Context", accessType.getName(), uuid);
return added;
} catch (Exception e) {
logger.error("Unable to add {} with UUID {} to actual Context",
accessType.getName(), uuid, e);
logger.error("Unable to add {} with UUID {} to actual Context", accessType.getName(), uuid, e);
if (orientGraph != null) {
orientGraph.rollback();
}
@ -791,5 +712,5 @@ public abstract class RelationManagement<R extends Relation, S extends EntityMan
}
}
}
}