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@158615 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2017-11-17 15:04:15 +00:00
parent 6edb78bdfd
commit 99d0e8e04f
4 changed files with 154 additions and 207 deletions

View File

@ -35,32 +35,22 @@ public class ERManagementUtility {
private static Logger logger = LoggerFactory.getLogger(EntityManagement.class); private static Logger logger = LoggerFactory.getLogger(EntityManagement.class);
/* /*
@SuppressWarnings("rawtypes") * @SuppressWarnings("rawtypes") public static ERManagement
public static ERManagement getERManagement(AccessType querableType) * getERManagement(AccessType querableType) throws ResourceRegistryException {
throws ResourceRegistryException { * switch (querableType) { case FACET: return new FacetManagement();
switch (querableType) { *
case FACET: * case RESOURCE: return new ResourceManagement();
return new FacetManagement(); *
* case IS_RELATED_TO: return new IsRelatedToManagement();
case RESOURCE: *
return new ResourceManagement(); * case CONSISTS_OF: return new ConsistsOfManagement();
*
case IS_RELATED_TO: * default: throw new ResourceRegistryException(String.format(
return new IsRelatedToManagement(); * "%s is not querable", querableType.toString())); } }
*/
case CONSISTS_OF:
return new ConsistsOfManagement();
default:
throw new ResourceRegistryException(String.format(
"%s is not querable", querableType.toString()));
}
}
*/
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
public static ERManagement getERManagement(String type) public static ERManagement getERManagement(String type) throws ResourceRegistryException {
throws ResourceRegistryException {
OClass oClass = SchemaManagementImpl.getTypeSchema(type, null); OClass oClass = SchemaManagementImpl.getTypeSchema(type, null);
ERManagement erManagement = null; ERManagement erManagement = null;
@ -76,8 +66,7 @@ public class ERManagementUtility {
} }
if (erManagement == null) { if (erManagement == null) {
throw new ResourceRegistryException(String.format( throw new ResourceRegistryException(String.format("%s is not querable", type.toString()));
"%s is not querable", type.toString()));
} }
erManagement.setElementType(type); erManagement.setElementType(type);
@ -85,24 +74,21 @@ public class ERManagementUtility {
} }
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
private static ERManagement getERManagement(OrientGraph orientGraph, private static ERManagement getERManagement(OrientGraph orientGraph, Element element)
Element element) throws ResourceRegistryException { throws ResourceRegistryException {
if (element instanceof Vertex) { if (element instanceof Vertex) {
return getEntityManagement(orientGraph, return getEntityManagement(orientGraph, (Vertex) element);
(Vertex) element);
} else if (element instanceof Edge) { } else if (element instanceof Edge) {
return getRelationManagement(orientGraph, return getRelationManagement(orientGraph, (Edge) element);
(Edge) element);
} }
throw new ResourceRegistryException(String.format( throw new ResourceRegistryException(String.format("%s is not a %s nor a %s", element.getClass().getSimpleName(),
"%s is not a %s nor a %s", element.getClass().getSimpleName(),
Entity.NAME, Relation.NAME)); Entity.NAME, Relation.NAME));
} }
public static Element getAnyElementByUUID(UUID uuid) throws ERNotFoundException, ResourceRegistryException { public static Element getAnyElementByUUID(UUID uuid) throws ERNotFoundException, ResourceRegistryException {
try{ try {
return Utility.getElementByUUIDAsAdmin(null, uuid, Vertex.class); return Utility.getElementByUUIDAsAdmin(null, uuid, Vertex.class);
}catch (ERNotFoundException e) { } catch (ERNotFoundException e) {
return Utility.getElementByUUIDAsAdmin(null, uuid, Edge.class); return Utility.getElementByUUIDAsAdmin(null, uuid, Edge.class);
} catch (ResourceRegistryException e) { } catch (ResourceRegistryException e) {
throw e; throw e;
@ -111,11 +97,11 @@ public class ERManagementUtility {
} }
} }
private static Element getAnyElementByUUID(OrientGraph orientGraph, UUID uuid)
private static Element getAnyElementByUUID(OrientGraph orientGraph, UUID uuid) throws ERNotFoundException, ResourceRegistryException { throws ERNotFoundException, ResourceRegistryException {
try{ try {
return Utility.getElementByUUID(orientGraph, null, uuid, Vertex.class); return Utility.getElementByUUID(orientGraph, null, uuid, Vertex.class);
}catch (ERNotFoundException e) { } catch (ERNotFoundException e) {
return Utility.getElementByUUID(orientGraph, null, uuid, Edge.class); return Utility.getElementByUUID(orientGraph, null, uuid, Edge.class);
} catch (ResourceRegistryException e) { } catch (ResourceRegistryException e) {
throw e; throw e;
@ -125,37 +111,39 @@ public class ERManagementUtility {
} }
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
public static ERManagement getERManagementFromUUID(OrientGraph orientGraph, public static ERManagement getERManagementFromUUID(OrientGraph orientGraph, UUID uuid)
UUID uuid) throws ResourceRegistryException { throws ResourceRegistryException {
Element element; Element element;
try { try {
element = getAnyElementByUUID(orientGraph, uuid); element = getAnyElementByUUID(orientGraph, uuid);
return getERManagement(orientGraph, element); return getERManagement(orientGraph, element);
} catch (Exception e) { } catch (Exception e) {
throw new ResourceRegistryException(String.format( throw new ResourceRegistryException(String.format("%s does not belong to an %s nor to a %s",
"%s does not belong to an %s nor to a %s", uuid.toString(), Entity.NAME, Relation.NAME));
uuid.toString(), Entity.NAME, Relation.NAME));
} }
} }
@SuppressWarnings({ "rawtypes", "unchecked" }) @SuppressWarnings({ "rawtypes", "unchecked" })
public static EntityManagement getEntityManagement(OrientGraph orientGraph, public static EntityManagement getEntityManagement(OrientGraph orientGraph, Vertex vertex)
Vertex vertex) throws ResourceRegistryException { throws ResourceRegistryException {
if(orientGraph==null){ if (orientGraph == null) {
throw new ResourceRegistryException(OrientGraph.class.getSimpleName() + "instance is null. This is really strage please contact the administrator."); throw new ResourceRegistryException(OrientGraph.class.getSimpleName()
+ "instance is null. This is really strage please contact the administrator.");
} }
if(vertex==null){ if (vertex == null) {
throw new ResourceRegistryException(Vertex.class.getSimpleName() + "instance is null. This is really strage please contact the administrator."); throw new ResourceRegistryException(Vertex.class.getSimpleName()
+ "instance is null. This is really strage please contact the administrator.");
} }
OrientVertexType orientVertexType = null; OrientVertexType orientVertexType = null;
try { try {
orientVertexType = ((OrientVertex) vertex).getType(); orientVertexType = ((OrientVertex) vertex).getType();
}catch (Exception e) { } catch (Exception e) {
String error = String.format("Unable to detect type of %s. This is really strage please contact the administrator.", vertex.toString()); String error = String.format(
"Unable to detect type of %s. This is really strage please contact the administrator.",
vertex.toString());
logger.error(error, e); logger.error(error, e);
throw new ResourceRegistryException(error); throw new ResourceRegistryException(error);
} }
@ -166,10 +154,8 @@ public class ERManagementUtility {
} else if (orientVertexType.isSubClassOf(Facet.NAME)) { } else if (orientVertexType.isSubClassOf(Facet.NAME)) {
entityManagement = new FacetManagement(orientGraph); entityManagement = new FacetManagement(orientGraph);
} else { } else {
String error = String.format("{%s is not a %s nor a %s. " String error = String.format("{%s is not a %s nor a %s. " + "This is really strange and should not occur. "
+ "This is really strange and should not occur. " + "Please Investigate it.", vertex, Resource.NAME, Facet.NAME);
+ "Please Investigate it.", vertex, Resource.NAME,
Facet.NAME);
throw new ResourceRegistryException(error); throw new ResourceRegistryException(error);
} }
entityManagement.setElement(vertex); entityManagement.setElement(vertex);

View File

@ -66,7 +66,7 @@ public abstract class EntityManagement<E extends Entity> extends
String id = edge.getId().toString(); String id = edge.getId().toString();
RelationManagement relationManagement = relationManagements.get(id); RelationManagement relationManagement = relationManagements.get(id);
if(relationManagement==null) { if(relationManagement==null) {
relationManagement = RelationManagement.getRelationManagement(orientGraph, edge); relationManagement = ERManagementUtility.getRelationManagement(orientGraph, edge);
} }
return relationManagement; return relationManagement;
} }

View File

@ -88,7 +88,7 @@ public class ResourceManagement extends EntityManagement<Resource> {
Iterable<Edge> edges = getElement().getEdges(Direction.OUT); Iterable<Edge> edges = getElement().getEdges(Direction.OUT);
for (Edge edge : edges) { for (Edge edge : edges) {
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
RelationManagement relationManagement = RelationManagement.getRelationManagement(orientGraph, edge); RelationManagement relationManagement = ERManagementUtility.getRelationManagement(orientGraph, edge);
relationManagement.setSourceEntityManagement(this); relationManagement.setSourceEntityManagement(this);
if (relationManagement instanceof ConsistsOfManagement) { if (relationManagement instanceof ConsistsOfManagement) {

View File

@ -21,8 +21,6 @@ import org.gcube.informationsystem.model.embedded.PropagationConstraint.RemoveCo
import org.gcube.informationsystem.model.entity.Entity; import org.gcube.informationsystem.model.entity.Entity;
import org.gcube.informationsystem.model.entity.Facet; import org.gcube.informationsystem.model.entity.Facet;
import org.gcube.informationsystem.model.entity.Resource; 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.model.relation.Relation;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextException; import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextException;
@ -44,7 +42,6 @@ import com.tinkerpop.blueprints.Edge;
import com.tinkerpop.blueprints.Vertex; import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.blueprints.impls.orient.OrientBaseGraph; import com.tinkerpop.blueprints.impls.orient.OrientBaseGraph;
import com.tinkerpop.blueprints.impls.orient.OrientEdge; 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.OrientGraph;
/** /**
@ -162,6 +159,7 @@ public abstract class RelationManagement<R extends Relation, S extends EntityMan
String id = source.getId().toString(); String id = source.getId().toString();
JSONObject sourceResource = visitedSourceResources.get(id); JSONObject sourceResource = visitedSourceResources.get(id);
if (sourceResource == null) { if (sourceResource == null) {
ResourceManagement resourceManagement = (ResourceManagement) ERManagementUtility ResourceManagement resourceManagement = (ResourceManagement) ERManagementUtility
.getEntityManagement(orientGraph, source); .getEntityManagement(orientGraph, source);
@ -307,17 +305,16 @@ public abstract class RelationManagement<R extends Relation, S extends EntityMan
protected abstract T newTargetEntityManagement() throws ResourceRegistryException; protected abstract T newTargetEntityManagement() throws ResourceRegistryException;
/* /*
public void setSourceUUID(UUID sourceUUID) throws ResourceRegistryException { * public void setSourceUUID(UUID sourceUUID) throws ResourceRegistryException {
this.sourceEntityManagemen = newSourceEntityManagement(); * this.sourceEntityManagemen = newSourceEntityManagement();
this.sourceEntityManagemen.setUUID(sourceUUID); * this.sourceEntityManagemen.setUUID(sourceUUID); }
} *
* public void setTargetUUID(UUID targetUUID) throws ResourceRegistryException {
public void setTargetUUID(UUID targetUUID) throws ResourceRegistryException { * this.targetEntityManagemen = newTargetEntityManagement();
this.targetEntityManagemen = newTargetEntityManagement(); * this.targetEntityManagemen.setUUID(targetUUID);
this.targetEntityManagemen.setUUID(targetUUID); *
* }
} */
*/
@Override @Override
protected Edge reallyUpdate() throws ResourceRegistryException { protected Edge reallyUpdate() throws ResourceRegistryException {
@ -372,22 +369,22 @@ public abstract class RelationManagement<R extends Relation, S extends EntityMan
} }
switch (addConstraint) { switch (addConstraint) {
case propagate: case propagate:
/* /*
* The relation must be added only in the case the target vertex must be added. * 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. * Otherwise we have a relation which point to an entity outside of the context.
*/ */
getTargetEntityManagemen().internalAddToContext(); getTargetEntityManagemen().internalAddToContext();
ContextUtility.addToActualContext(orientGraph, getElement()); ContextUtility.addToActualContext(orientGraph, getElement());
break; break;
case unpropagate: case unpropagate:
break; break;
default: default:
break; break;
} }
return true; return true;
@ -409,17 +406,12 @@ public abstract class RelationManagement<R extends Relation, S extends EntityMan
} }
/* /*
protected boolean removeFromContextTargetVertex(Vertex target) throws ResourceRegistryException { * protected boolean removeFromContextTargetVertex(Vertex target) throws
EntityManagement entityManagement = EntityManagement.getEntityManagement(orientGraph, target); * ResourceRegistryException { EntityManagement entityManagement =
if (entityManagement != null) { * EntityManagement.getEntityManagement(orientGraph, target); if
entityManagement.internalRemoveFromContext(); * (entityManagement != null) { entityManagement.internalRemoveFromContext();
return true; * return true; } else { return false; } }
} else { */
return false;
}
}
*/
@Override @Override
protected boolean reallyRemoveFromContext() throws ContextException, ResourceRegistryException { protected boolean reallyRemoveFromContext() throws ContextException, ResourceRegistryException {
@ -458,81 +450,50 @@ public abstract class RelationManagement<R extends Relation, S extends EntityMan
ContextUtility.removeFromActualContext(orientGraph, element); ContextUtility.removeFromActualContext(orientGraph, element);
switch (removeConstraint) { switch (removeConstraint) {
case cascade: case cascade:
getTargetEntityManagemen().internalRemoveFromContext(); getTargetEntityManagemen().internalRemoveFromContext();
break; break;
case cascadeWhenOrphan: case cascadeWhenOrphan:
Vertex target = (Vertex) getTargetEntityManagemen().getElement(); Vertex target = (Vertex) getTargetEntityManagemen().getElement();
Iterable<Edge> iterable = target.getEdges(Direction.IN); Iterable<Edge> iterable = target.getEdges(Direction.IN);
Iterator<Edge> iterator = iterable.iterator(); Iterator<Edge> iterator = iterable.iterator();
int count = 0; int count = 0;
OrientEdge edge = null; OrientEdge edge = null;
while (iterator.hasNext()) { while (iterator.hasNext()) {
edge = (OrientEdge) iterator.next(); edge = (OrientEdge) iterator.next();
OrientEdge thisOrientEdge = (OrientEdge) getElement(); OrientEdge thisOrientEdge = (OrientEdge) getElement();
if (edge.compareTo(thisOrientEdge) != 0) { if (edge.compareTo(thisOrientEdge) != 0) {
if (thisOrientEdge.getOutVertex().compareTo(edge.getOutVertex()) != 0) { if (thisOrientEdge.getOutVertex().compareTo(edge.getOutVertex()) != 0) {
count++; count++;
break; break;
}
/*
* else{ ContextUtility.removeFromActualContext(orientGraph, edge); }
*/
} }
/*
* else{ ContextUtility.removeFromActualContext(orientGraph, edge); }
*/
} }
}
if (count > 0) { if (count > 0) {
logger.trace( logger.trace(
"{} point to {} which is not orphan ({} exists). Giving {} directive, it will be not remove from current context.", "{} point to {} which is not orphan ({} exists). Giving {} directive, it will be not remove from current context.",
element, target, edge, removeConstraint); element, target, edge, removeConstraint);
} else { } else {
getTargetEntityManagemen().internalRemoveFromContext(); getTargetEntityManagemen().internalRemoveFromContext();
} }
break; break;
case keep: case keep:
break; break;
default: default:
break; break;
} }
return true; return true;
} }
@SuppressWarnings("unchecked")
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;
}
@Override @Override
protected boolean reallyDelete() throws RelationNotFoundException, ResourceRegistryException { protected boolean reallyDelete() throws RelationNotFoundException, ResourceRegistryException {
logger.debug("Going to remove {} with UUID {}. Related {}s will be detached.", accessType.getName(), uuid, logger.debug("Going to remove {} with UUID {}. Related {}s will be detached.", accessType.getName(), uuid,
@ -567,26 +528,26 @@ public abstract class RelationManagement<R extends Relation, S extends EntityMan
element.remove(); element.remove();
switch (removeConstraint) { switch (removeConstraint) {
case cascade: case cascade:
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);
} else {
getTargetEntityManagemen().internalDelete(); getTargetEntityManagemen().internalDelete();
break; }
break;
case cascadeWhenOrphan: case keep:
Iterable<Edge> iterable = target.getEdges(Direction.IN); break;
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 {
getTargetEntityManagemen().internalDelete();
}
break;
case keep: default:
break; break;
default:
break;
} }
return true; return true;
@ -631,7 +592,7 @@ public abstract class RelationManagement<R extends Relation, S extends EntityMan
if (postFilterPolymorphic && edge.getLabel().compareTo(erType) != 0) { if (postFilterPolymorphic && edge.getLabel().compareTo(erType) != 0) {
continue; continue;
} }
RelationManagement relationManagement = getRelationManagement(orientGraph, edge); RelationManagement relationManagement = ERManagementUtility.getRelationManagement(orientGraph, edge);
visitedSourceResources = relationManagement.fullSerialize(visitedSourceResources); visitedSourceResources = relationManagement.fullSerialize(visitedSourceResources);
} }
return visitedSourceResources.values(); return visitedSourceResources.values();