Merged two functions to reduce code

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/resource-registry@134616 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2016-11-23 16:52:04 +00:00
parent a10c66578a
commit 5e37f319d3
1 changed files with 23 additions and 57 deletions

View File

@ -14,8 +14,6 @@ import org.gcube.informationsystem.model.embedded.Header;
import org.gcube.informationsystem.model.entity.Entity; import org.gcube.informationsystem.model.entity.Entity;
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.entity.EntityException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.FacetNotFoundException;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -77,78 +75,46 @@ public class Utility {
} }
} }
public static Vertex getEntityByUUID(OrientGraph orientGraph, public static <El extends Element> El getElementByUUID(OrientGraph orientGraph,
String entityType, UUID uuid) throws ResourceRegistryException { String elementType, UUID uuid, Class<? extends El> clz) throws ResourceRegistryException {
if (entityType == null) { if (elementType == null || elementType.compareTo("")==0) {
entityType = Entity.NAME; if(Vertex.class.isAssignableFrom(clz)){
elementType = Entity.NAME;
}
if(Edge.class.isAssignableFrom(clz)){
elementType = Relation.NAME;
}
} }
// TODO Rewrite using Gremlin // TODO Rewrite using Gremlin
String select = "SELECT FROM " + entityType + " WHERE " String select = "SELECT FROM " + elementType + " WHERE "
+ Entity.HEADER_PROPERTY + "." + Header.UUID_PROPERTY + " = \""
+ uuid.toString() + "\"";
OSQLSynchQuery<Vertex> osqlSynchQuery = new OSQLSynchQuery<Vertex>(
select);
Iterable<Vertex> 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<Vertex> 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 "
+ Relation.HEADER_PROPERTY + "." + Header.UUID_PROPERTY + Relation.HEADER_PROPERTY + "." + Header.UUID_PROPERTY
+ " = \"" + uuid.toString() + "\""; + " = \"" + uuid.toString() + "\"";
OSQLSynchQuery<Edge> osqlSynchQuery = new OSQLSynchQuery<Edge>(select); OSQLSynchQuery<El> osqlSynchQuery = new OSQLSynchQuery<>(select);
Iterable<Edge> edges = orientGraph.command(osqlSynchQuery).execute(); Iterable<El> elements = orientGraph.command(osqlSynchQuery).execute();
if (edges == null || !edges.iterator().hasNext()) { if (elements == null || !elements.iterator().hasNext()) {
String error = String.format("No %s with UUID %s was found", String error = String.format("No %s with UUID %s was found",
relationType, uuid.toString()); elementType, uuid.toString());
logger.info(error); logger.info(error);
throw new FacetNotFoundException(error); throw new ResourceRegistryException(error);
} }
Iterator<Edge> iterator = edges.iterator(); Iterator<El> iterator = elements.iterator();
Edge relation = iterator.next(); El element = iterator.next();
logger.trace("{} with {} is : {}", relationType, uuid.toString(), logger.trace("{} with {} is : {}", elementType, uuid.toString(),
Utility.toJsonString(relation, true)); Utility.toJsonString(element, true));
if (iterator.hasNext()) { if (iterator.hasNext()) {
throw new ResourceRegistryException("Found more than one " 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"); + ". This is a fatal error please contact Admnistrator");
} }
return relation; return element;
} }
} }