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,17 +74,14 @@ 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));
} }
@ -111,8 +97,8 @@ 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) {
@ -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,16 +305,15 @@ 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
@ -409,18 +406,13 @@ 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 {
getElement(); getElement();
@ -502,37 +494,6 @@ public abstract class RelationManagement<R extends Relation, S extends EntityMan
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,
@ -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();