diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/er/ERManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/er/ERManagement.java index adfb5a8..e6d0174 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/er/ERManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/er/ERManagement.java @@ -387,6 +387,29 @@ public abstract class ERManagement { } } } + + + public boolean exists() throws ERNotFoundException, ResourceRegistryException { + try { + orientGraph = ContextUtility.getActualSecurityContextGraph(PermissionMode.READER); + + getElement(); + + if(element==null){ + return false; + }else{ + return true; + } + } catch (ResourceRegistryException e) { + throw e; + } catch (Exception e) { + throw new ResourceRegistryException(e); + } finally { + if (orientGraph != null) { + orientGraph.shutdown(); + } + } + } public String read() throws ERNotFoundException, ResourceRegistryException { diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/er/entity/EntityManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/er/entity/EntityManagement.java index bc21398..b2151ed 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/er/entity/EntityManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/er/entity/EntityManagement.java @@ -73,6 +73,12 @@ public abstract class EntityManagement extends try { + if(oClass.isAbstract()){ + String error = String.format("Trying to create an instance of %s of type %s which is abstract. The operation will be aborted.", + baseType, erType); + throw new ResourceRegistryException(error); + } + Vertex vertexEntity = orientGraph.addVertex("class:" + erType); try { @@ -164,7 +170,16 @@ public abstract class EntityManagement extends @SuppressWarnings({ "rawtypes", "unchecked" }) public static EntityManagement getEntityManagement(OrientGraph orientGraph, Vertex vertex) throws ResourceRegistryException { - OrientVertexType orientVertexType = ((OrientVertex) vertex).getType(); + + OrientVertexType orientVertexType = null; + try { + orientVertexType = ((OrientVertex) vertex).getType(); + }catch (Exception e) { + String error = String.format("Unable to detect type of %s. This is really strage please contact the administrator", vertex.toString()); + logger.error(error, e); + throw new ResourceRegistryException(error); + } + EntityManagement entityManagement = null; if (orientVertexType.isSubClassOf(Resource.NAME)) { entityManagement = new ResourceManagement(orientGraph); @@ -221,8 +236,12 @@ public abstract class EntityManagement extends for(Vertex vertex : iterable){ @SuppressWarnings("rawtypes") EntityManagement entityManagement = getEntityManagement(orientGraph, vertex); - JSONObject jsonObject = entityManagement.serializeAsJson(); - jsonArray.put(jsonObject); + try { + JSONObject jsonObject = entityManagement.serializeAsJson(); + jsonArray.put(jsonObject); + }catch (ResourceRegistryException e) { + logger.error("Unable to correctly serialize {}. It will be excluded from results. This is really strange and should not occur.", vertex.toString()); + } } return jsonArray.toString(); } diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/er/entity/ResourceManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/er/entity/ResourceManagement.java index 6e64341..98e5036 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/er/entity/ResourceManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/er/entity/ResourceManagement.java @@ -68,9 +68,13 @@ public class ResourceManagement extends EntityManagement { RelationManagement relationManagement = RelationManagement .getRelationManagement(orientGraph, edge); if (relationManagement instanceof ConsistsOfManagement) { - JSONObject consistsOf = relationManagement - .serializeAsJson(); - sourceResource = addConsistsOf(sourceResource, consistsOf); + try { + JSONObject consistsOf = relationManagement + .serializeAsJson(); + sourceResource = addConsistsOf(sourceResource, consistsOf); + }catch (ResourceRegistryException e) { + logger.error("Unable to correctly serialize {}. It will be excluded from results. This is really strange and should not occur.", edge.toString()); + } } /* diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/er/relation/RelationManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/er/relation/RelationManagement.java index ffe5425..b8ea7a3 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/er/relation/RelationManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/er/relation/RelationManagement.java @@ -12,7 +12,6 @@ import java.util.Map; import java.util.UUID; import org.codehaus.jettison.json.JSONArray; -import org.codehaus.jettison.json.JSONException; import org.codehaus.jettison.json.JSONObject; import org.gcube.informationsystem.model.embedded.PropagationConstraint; import org.gcube.informationsystem.model.embedded.PropagationConstraint.AddConstraint; @@ -98,14 +97,13 @@ public abstract class RelationManagement extends JSONObject relation = serializeSelfOnly(); Vertex target = element.getVertex(Direction.IN); - EntityManagement entityManagement = EntityManagement - .getEntityManagement(orientGraph, target); - try { + EntityManagement entityManagement = EntityManagement + .getEntityManagement(orientGraph, target); relation.put(Relation.TARGET_PROPERTY, entityManagement.serializeAsJson()); - } catch (JSONException e) { - throw new ResourceRegistryException(e); + } catch (Exception e) { + logger.error("Unable to correctly serialize {}. It will be excluded from results. This is really strange and should not occur.", target.toString()); } return relation;