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.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 extends Element> El getElementByUUID(OrientGraph orientGraph,
String elementType, UUID uuid, Class<? extends El> 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<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 "
String select = "SELECT FROM " + elementType + " WHERE "
+ Relation.HEADER_PROPERTY + "." + Header.UUID_PROPERTY
+ " = \"" + uuid.toString() + "\"";
OSQLSynchQuery<Edge> osqlSynchQuery = new OSQLSynchQuery<Edge>(select);
OSQLSynchQuery<El> osqlSynchQuery = new OSQLSynchQuery<>(select);
Iterable<Edge> edges = orientGraph.command(osqlSynchQuery).execute();
if (edges == null || !edges.iterator().hasNext()) {
Iterable<El> 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<Edge> iterator = edges.iterator();
Edge relation = iterator.next();
Iterator<El> 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;
}
}