diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/resources/utils/Utility.java b/src/main/java/org/gcube/informationsystem/resourceregistry/resources/utils/Utility.java index f6c3eb9..86c58aa 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/resources/utils/Utility.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/resources/utils/Utility.java @@ -14,8 +14,6 @@ import org.gcube.informationsystem.model.embedded.Header; import org.gcube.informationsystem.model.entity.Entity; import org.gcube.informationsystem.model.relation.Relation; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; -import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.EntityException; -import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.FacetNotFoundException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -76,79 +74,47 @@ public class Utility { return String.valueOf(element); } } + + public static El getElementByUUID(OrientGraph orientGraph, + String elementType, UUID uuid, Class clz) throws ResourceRegistryException { - public static Vertex getEntityByUUID(OrientGraph orientGraph, - String entityType, UUID uuid) throws ResourceRegistryException { - - if (entityType == null) { - entityType = Entity.NAME; + if (elementType == null || elementType.compareTo("")==0) { + if(Vertex.class.isAssignableFrom(clz)){ + elementType = Entity.NAME; + } + if(Edge.class.isAssignableFrom(clz)){ + elementType = Relation.NAME; + } } // TODO Rewrite using Gremlin - String select = "SELECT FROM " + entityType + " WHERE " - + Entity.HEADER_PROPERTY + "." + Header.UUID_PROPERTY + " = \"" - + uuid.toString() + "\""; - - OSQLSynchQuery osqlSynchQuery = new OSQLSynchQuery( - select); - - Iterable vertexes = orientGraph.command(osqlSynchQuery) - .execute(); - if (vertexes == null || !vertexes.iterator().hasNext()) { - String error = String.format("No %s with UUID %s was found", - entityType, uuid.toString()); - logger.info(error); - throw new EntityException(error); - } - - Iterator iterator = vertexes.iterator(); - Vertex entity = iterator.next(); - - logger.trace("{} with {} is : {}", entityType, uuid.toString(), Utility.toJsonString(entity, true)); - - if (iterator.hasNext()) { - throw new ResourceRegistryException("Found more than one " - + entityType + " with uuid " + uuid.toString() - + ". This is a fatal error please contact Admnistrator"); - } - - return entity; - } - - public static Edge getRelationByUUID(OrientGraph orientGraph, - String relationType, UUID uuid) throws ResourceRegistryException { - - if (relationType == null) { - relationType = Relation.class.getSimpleName(); - } - - // TODO Rewrite using Gremlin - String select = "SELECT FROM " + relationType + " WHERE " + String select = "SELECT FROM " + elementType + " WHERE " + Relation.HEADER_PROPERTY + "." + Header.UUID_PROPERTY + " = \"" + uuid.toString() + "\""; - OSQLSynchQuery osqlSynchQuery = new OSQLSynchQuery(select); + OSQLSynchQuery osqlSynchQuery = new OSQLSynchQuery<>(select); - Iterable edges = orientGraph.command(osqlSynchQuery).execute(); - if (edges == null || !edges.iterator().hasNext()) { + Iterable elements = orientGraph.command(osqlSynchQuery).execute(); + if (elements == null || !elements.iterator().hasNext()) { String error = String.format("No %s with UUID %s was found", - relationType, uuid.toString()); + elementType, uuid.toString()); logger.info(error); - throw new FacetNotFoundException(error); + throw new ResourceRegistryException(error); } - Iterator iterator = edges.iterator(); - Edge relation = iterator.next(); + Iterator iterator = elements.iterator(); + El element = iterator.next(); - logger.trace("{} with {} is : {}", relationType, uuid.toString(), - Utility.toJsonString(relation, true)); + logger.trace("{} with {} is : {}", elementType, uuid.toString(), + Utility.toJsonString(element, true)); if (iterator.hasNext()) { throw new ResourceRegistryException("Found more than one " - + relationType + " with uuid " + uuid.toString() + + elementType + " with uuid " + uuid.toString() + ". This is a fatal error please contact Admnistrator"); } - return relation; + return element; } + } \ No newline at end of file