diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/context/ContextManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/context/ContextManagement.java index 811336a..697e92d 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/context/ContextManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/context/ContextManagement.java @@ -46,7 +46,7 @@ public class ContextManagement extends EntityManagement { private void init() { this.ignoreStartWithKeys.add(Context.PARENT_PROPERTY); this.ignoreStartWithKeys.add(Context.CHILDREN_PROPERTY); - this.erType = Context.NAME; + this.elementType = Context.NAME; } public ContextManagement() { @@ -384,7 +384,7 @@ public class ContextManagement extends EntityManagement { @Override public String reallyGetAll(boolean polymorphic) throws ResourceRegistryException { JSONArray jsonArray = new JSONArray(); - Iterable iterable = orientGraph.getVerticesOfClass(erType, polymorphic); + Iterable iterable = orientGraph.getVerticesOfClass(elementType, polymorphic); for(Vertex vertex : iterable) { ContextManagement contextManagement = new ContextManagement(); contextManagement.setElement(vertex); 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 7e70b78..2d6e705 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/er/ERManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/er/ERManagement.java @@ -80,7 +80,7 @@ public abstract class ERManagement { protected UUID uuid; protected JsonNode jsonNode; protected OClass oClass; - protected String erType; + protected String elementType; protected El element; protected boolean reload; @@ -159,14 +159,14 @@ public abstract class ERManagement { String type = orientElement.getRecord().getClassName(); oClass = oSchema.getClass(type); } else { - oClass = SchemaManagementImpl.getTypeSchema(erType, accessType); + oClass = SchemaManagementImpl.getTypeSchema(elementType, accessType); } } return oClass; } public void setElementType(String erType) throws ResourceRegistryException { - this.erType = erType; + this.elementType = erType; if(erType == null || erType.compareTo("") == 0) { erType = accessType.getName(); } @@ -175,6 +175,11 @@ public abstract class ERManagement { } } + public String getElementType() { + return elementType; + } + + protected void checkJSON() throws ResourceRegistryException { if(uuid == null) { try { @@ -185,8 +190,8 @@ public abstract class ERManagement { checkUUIDMatch(); } - if(this.erType == null) { - this.erType = getClassProperty(jsonNode); + if(this.elementType == null) { + this.elementType = getClassProperty(jsonNode); getOClass(); } else { checkERMatch(); @@ -196,9 +201,9 @@ public abstract class ERManagement { protected void checkERMatch() throws ResourceRegistryException { if(jsonNode != null) { String type = getClassProperty(jsonNode); - if(type != null && type.compareTo(erType) != 0) { + if(type != null && type.compareTo(elementType) != 0) { String error = String.format("Requested type does not match with json representation %s!=%s", - erType, type); + elementType, type); logger.trace(error); throw new ResourceRegistryException(error); } @@ -219,7 +224,7 @@ public abstract class ERManagement { if(resourceUUID.compareTo(uuid) != 0) { String error = String.format( "UUID provided in header (%s) differs from the one (%s) used to identify the %s instance", - resourceUUID.toString(), uuid.toString(), erType); + resourceUUID.toString(), uuid.toString(), elementType); throw new ResourceRegistryException(error); } @@ -259,7 +264,7 @@ public abstract class ERManagement { } catch(ResourceRegistryException e) { throw e; } catch(Exception e) { - throw new ResourceRegistryException("Error Creating " + erType + " with " + jsonNode, e); + throw new ResourceRegistryException("Error Creating " + elementType + " with " + jsonNode, e); } } @@ -277,7 +282,7 @@ public abstract class ERManagement { } catch(ResourceRegistryException e) { throw e; } catch(Exception e) { - throw new ResourceRegistryException("Error Updating " + erType + " with " + jsonNode, e); + throw new ResourceRegistryException("Error Updating " + elementType + " with " + jsonNode, e); } } @@ -307,7 +312,7 @@ public abstract class ERManagement { } catch(ResourceRegistryException e) { throw e; } catch(Exception e) { - throw new ResourceRegistryException("Error Adding " + erType + " to " + targetSecurityContext.toString(), e.getCause()); + throw new ResourceRegistryException("Error Adding " + elementType + " to " + targetSecurityContext.toString(), e.getCause()); } } @@ -322,7 +327,7 @@ public abstract class ERManagement { } catch(ResourceRegistryException e) { throw e; } catch(Exception e) { - throw new ResourceRegistryException("Error Removing " + erType + " from " + targetSecurityContext.toString(), e.getCause()); + throw new ResourceRegistryException("Error Removing " + elementType + " from " + targetSecurityContext.toString(), e.getCause()); } } @@ -332,6 +337,7 @@ public abstract class ERManagement { } this.element = element; this.uuid = HeaderUtility.getHeader(element).getUUID(); + this.elementType = ((OrientElement) element).getLabel(); } protected abstract NotFoundException getSpecificElementNotFoundException(NotFoundException e); @@ -348,8 +354,8 @@ public abstract class ERManagement { } catch(NotFoundException e) { try { retrieveElementFromAnyContext(); - throw getSpecificERAvailableInAnotherContextException(erType == null ? accessType.getName() - : erType + " with UUID " + uuid + " is available in another " + throw getSpecificERAvailableInAnotherContextException(elementType == null ? accessType.getName() + : elementType + " with UUID " + uuid + " is available in another " + Context.class.getSimpleName()); } catch(AvailableInAnotherContextException e1) { throw e1; @@ -375,7 +381,7 @@ public abstract class ERManagement { if(uuid == null) { throw new NotFoundException("null UUID does not allow to retrieve the Element"); } - return Utility.getElementByUUID(orientGraph, erType == null ? accessType.getName() : erType, uuid, + return Utility.getElementByUUID(orientGraph, elementType == null ? accessType.getName() : elementType, uuid, elementClass); } catch(NotFoundException e) { throw getSpecificElementNotFoundException(e); @@ -388,7 +394,7 @@ public abstract class ERManagement { public El retrieveElementFromAnyContext() throws NotFoundException, ResourceRegistryException { try { - return Utility.getElementByUUIDAsAdmin(erType == null ? accessType.getName() : erType, uuid, elementClass); + return Utility.getElementByUUIDAsAdmin(elementType == null ? accessType.getName() : elementType, uuid, elementClass); } catch(NotFoundException e) { throw getSpecificElementNotFoundException(e); } catch(ResourceRegistryException e) { @@ -581,17 +587,17 @@ public abstract class ERManagement { boolean added = internalAddToContext(targetSecurityContext); orientGraph.commit(); - logger.info("{} with UUID {} successfully added to Context with UUID {}", erType, uuid, contextUUID); + logger.info("{} with UUID {} successfully added to Context with UUID {}", elementType, uuid, contextUUID); return added; } catch(ResourceRegistryException e) { - logger.error("Unable to add {} with UUID {} to Context with UUID {}", erType, uuid, contextUUID); + logger.error("Unable to add {} with UUID {} to Context with UUID {}", elementType, uuid, contextUUID); if(orientGraph != null) { orientGraph.rollback(); } throw e; } catch(Exception e) { - logger.error("Unable to add {} with UUID {} to Context with UUID {}", erType, uuid, contextUUID, e); + logger.error("Unable to add {} with UUID {} to Context with UUID {}", elementType, uuid, contextUUID, e); if(orientGraph != null) { orientGraph.rollback(); } @@ -604,7 +610,7 @@ public abstract class ERManagement { } public boolean removeFromContext(UUID contextUUID) throws NotFoundException, ContextException, ResourceRegistryException { - logger.debug("Going to remove {} with UUID {} from Context with UUID {}", erType, uuid, contextUUID); + logger.debug("Going to remove {} with UUID {} from Context with UUID {}", elementType, uuid, contextUUID); try { @@ -617,17 +623,17 @@ public abstract class ERManagement { boolean removed = internalRemoveFromContext(targetSecurityContext); orientGraph.commit(); - logger.info("{} with UUID {} successfully removed from Context with UUID {}", erType, uuid, contextUUID); + logger.info("{} with UUID {} successfully removed from Context with UUID {}", elementType, uuid, contextUUID); return removed; } catch(ResourceRegistryException e) { - logger.error("Unable to remove {} with UUID {} from Context with UUID {}", erType, uuid, contextUUID); + logger.error("Unable to remove {} with UUID {} from Context with UUID {}", elementType, uuid, contextUUID); if(orientGraph != null) { orientGraph.rollback(); } throw e; } catch(Exception e) { - logger.error("Unable to remove {} with UUID {} from Context with UUID {}", erType, uuid, contextUUID, e); + logger.error("Unable to remove {} with UUID {} from Context with UUID {}", elementType, uuid, contextUUID, e); if(orientGraph != null) { orientGraph.rollback(); } 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 b1b1169..6d81215 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 @@ -1,6 +1,8 @@ package org.gcube.informationsystem.resourceregistry.er.entity; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.UUID; @@ -124,24 +126,24 @@ public abstract class EntityManagement extends ERManagement extends ERManagement extends ERManagement iterable = orientGraph.getVerticesOfClass(erType, polymorphic); + Iterable iterable = orientGraph.getVerticesOfClass(elementType, polymorphic); for(Vertex vertex : iterable) { @SuppressWarnings("rawtypes") EntityManagement entityManagement = ERManagementUtility.getEntityManagement(getWorkingContext(), @@ -234,10 +236,103 @@ public abstract class EntityManagement extends ERManagement constraint) throws ResourceRegistryException { + public String reallyQuery(String relationType, String referenceType, UUID referenceUUID, Direction direction, + boolean polymorphic, Map constraint) throws ResourceRegistryException { JSONArray jsonArray = new JSONArray(); + Iterable references = null; + + if(referenceUUID != null) { + Element element = ERManagementUtility.getAnyElementByUUID(referenceUUID); + if(element instanceof Vertex) { + @SuppressWarnings("unchecked") + EntityManagement entityManagement = ERManagementUtility.getEntityManagement(getWorkingContext(), + orientGraph, (Vertex) element); + + OrientVertexType orientVertexType = ((OrientVertex) element).getType(); + + String elementType = entityManagement.getElementType(); + if(elementType.compareTo(referenceType) != 0) { + if(polymorphic && orientVertexType.isSubClassOf(referenceType)) { + // OK + } else { + String error = String.format("Referenced instace with UUID %s is not a %s", referenceUUID, + referenceType); + throw new InvalidQueryException(error); + } + } + + List vertexes = new ArrayList<>(); + vertexes.add((Vertex) element); + references = vertexes; + + } else { + String error = String.format("Referenced instace with UUID %s is not an %s", referenceUUID, Entity.NAME); + throw new InvalidQueryException(error); + } + + } else { + references = orientGraph.getVerticesOfClass(referenceType, polymorphic); + } + + for(Vertex v : references) { + List directions = new ArrayList<>(); + if(direction==Direction.BOTH) { + directions.add(Direction.IN); + directions.add(Direction.OUT); + }else { + directions.add(direction); + } + + for(Direction d : directions) { + + Iterable edges = v.getEdges(d.opposite(), relationType); + for(Edge edge : edges) { + Vertex vertex = ((OrientEdge) edge).getVertex(d); + OrientVertex orientVertex = (OrientVertex) vertex; + + if(((OrientVertex) v).getIdentity().compareTo(orientVertex.getIdentity()) == 0) { + continue; + } + + if(elementType.compareTo(orientVertex.getLabel()) != 0) { + OrientVertexType orientVertexType = orientVertex.getType(); + + if(polymorphic && orientVertexType.isSubClassOf(elementType)) { + // OK + } else { + // excluding from results + continue; + } + } + + @SuppressWarnings("rawtypes") + EntityManagement entityManagement = ERManagementUtility.getEntityManagement(getWorkingContext(), + orientGraph, vertex); + try { + if(entityManagement.getUUID().compareTo(referenceUUID) == 0) { + continue; + } + JSONObject jsonObject = entityManagement.serializeAsJson(); + jsonArray.put(jsonObject); + } catch(ResourceRegistryException e) { + logger.error("Unable to correctly serialize {}. It will be excluded from results. {}", + vertex.toString(), Utility.SHOULD_NOT_OCCUR_ERROR_MESSAGE); + } + } + } + } + return jsonArray.toString(); + } + + public String reallyQueryTraversal(String relationType, String referenceType, UUID referenceUUID, + Direction direction, boolean polymorphic, Map constraint) throws ResourceRegistryException { + JSONArray jsonArray = new JSONArray(); + + if(referenceUUID != null) { + constraint.put(Entity.HEADER_PROPERTY + "." + Header.UUID_PROPERTY, referenceUUID.toString()); + } + // TODO check types /* @@ -254,7 +349,7 @@ public abstract class EntityManagement extends ERManagement extends ERManagement extends ERManagement extends ERManagement extends ERManagement extends ERManagement extends ERManagement extends ERManagement {}", erType, getSourceEntityManagement().serialize(), + logger.trace("Creating {} beetween {} -> {}", elementType, getSourceEntityManagement().serialize(), getTargetEntityManagement().serialize()); Vertex source = (Vertex) getSourceEntityManagement().getElement(); Vertex target = (Vertex) getTargetEntityManagement().getElement(); - element = orientGraph.addEdge(null, source, target, erType); + element = orientGraph.addEdge(null, source, target, elementType); ERManagement.updateProperties(oClass, element, jsonNode, ignoreKeys, ignoreStartWithKeys); - logger.info("{} successfully created", erType); + logger.info("{} successfully created", elementType); return element; } @@ -267,7 +267,7 @@ public abstract class RelationManagement visitedSourceResources = new HashMap<>(); for(Edge edge : edges) { - if(postFilterPolymorphic && edge.getLabel().compareTo(erType) != 0) { + if(postFilterPolymorphic && edge.getLabel().compareTo(elementType) != 0) { continue; } @@ -506,7 +506,7 @@ public abstract class RelationManagement edges = orientGraph.getEdgesOfClass(erType, polymorphic); + Iterable edges = orientGraph.getEdgesOfClass(elementType, polymorphic); Collection collection = serializeEdges(edges, false); return serializeJSONObjectList(collection); } diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/rest/SharingManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/rest/SharingManagement.java index e8ab05c..05706c0 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/rest/SharingManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/rest/SharingManagement.java @@ -33,6 +33,7 @@ public class SharingManagement { protected void setCalledMethod(HTTPMETHOD httpMethod, String type) { List list = new ArrayList<>(); list.add(SharingPath.SHARING_PATH_PART); + list.add(SharingPath.CONTEXTS_PATH_PART); list.add("{" + AccessPath.CONTEXT_UUID_PATH_PARAM + "}"); list.add(type); list.add("{" + AccessPath.UUID_PATH_PARAM + "}"); @@ -48,8 +49,8 @@ public class SharingManagement { * */ @PUT - @Path("/{" + AccessPath.CONTEXT_UUID_PATH_PARAM + "}" + "/" + AccessPath.TYPE_PATH_PARAM + "/{" - + AccessPath.UUID_PATH_PARAM + "}") + @Path("/" + SharingPath.CONTEXTS_PATH_PART + "/{" + AccessPath.CONTEXT_UUID_PATH_PARAM + "}" + "/" + + AccessPath.TYPE_PATH_PARAM + "/{" + AccessPath.UUID_PATH_PARAM + "}") @Consumes({MediaType.TEXT_PLAIN, ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8}) @Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8) public boolean add(@PathParam(AccessPath.CONTEXT_UUID_PATH_PARAM) String contextId, @@ -88,8 +89,8 @@ public class SharingManagement { * */ @DELETE - @Path("/{" + AccessPath.CONTEXT_UUID_PATH_PARAM + "}" + "/" + AccessPath.TYPE_PATH_PARAM + "/{" - + AccessPath.UUID_PATH_PARAM + "}") + @Path("/" + SharingPath.CONTEXTS_PATH_PART + "/{" + AccessPath.CONTEXT_UUID_PATH_PARAM + "}" + "/" + + AccessPath.TYPE_PATH_PARAM + "/{" + AccessPath.UUID_PATH_PARAM + "}") @Consumes({MediaType.TEXT_PLAIN, ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8}) @Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8) public boolean remove(@PathParam(AccessPath.CONTEXT_UUID_PATH_PARAM) String contextId, diff --git a/src/test/java/org/gcube/informationsystem/resourceregistry/er/ERManagementTest.java b/src/test/java/org/gcube/informationsystem/resourceregistry/er/ERManagementTest.java index 0f88f40..0e2a824 100644 --- a/src/test/java/org/gcube/informationsystem/resourceregistry/er/ERManagementTest.java +++ b/src/test/java/org/gcube/informationsystem/resourceregistry/er/ERManagementTest.java @@ -638,24 +638,21 @@ public class ERManagementTest extends ScopedTest { String json = resourceManagement.query(IsRelatedTo.NAME, EService.NAME, eServiceUUID, Direction.BOTH, true, null); List resourceList = ISMapper.unmarshalList(Resource.class, json); Assert.assertTrue(resourceList.size()==1); - Resource sourceResource = resourceList.get(0); - Resource targetResource = sourceResource.getIsRelatedTo().get(0).getTarget(); - Assert.assertTrue(sourceResource.getHeader().getUUID().compareTo(hostingNodeUUID)==0); - Assert.assertTrue(targetResource.getHeader().getUUID().compareTo(eServiceUUID)==0); + Resource resource = resourceList.get(0); + Assert.assertTrue(resource.getHeader().getUUID().compareTo(hostingNodeUUID)==0); json = resourceManagement.query(IsRelatedTo.NAME, EService.NAME, eServiceUUID, Direction.OUT, true, null); resourceList = ISMapper.unmarshalList(Resource.class, json); Assert.assertTrue(resourceList.size()==1); - sourceResource = resourceList.get(0); - targetResource = sourceResource.getIsRelatedTo().get(0).getTarget(); - Assert.assertTrue(sourceResource.getHeader().getUUID().compareTo(hostingNodeUUID)==0); - Assert.assertTrue(targetResource.getHeader().getUUID().compareTo(eServiceUUID)==0); + resource = resourceList.get(0); + Assert.assertTrue(resource.getHeader().getUUID().compareTo(hostingNodeUUID)==0); json = resourceManagement.query(IsRelatedTo.NAME, EService.NAME, eServiceUUID, Direction.IN, true, null); resourceList = ISMapper.unmarshalList(Resource.class, json); Assert.assertTrue(resourceList.size()==0); + json = resourceManagement.query(IsRelatedTo.NAME, EService.NAME, eServiceUUID, Direction.BOTH, false, null); resourceList = ISMapper.unmarshalList(Resource.class, json); Assert.assertTrue(resourceList.size()==0); @@ -677,24 +674,22 @@ public class ERManagementTest extends ScopedTest { json = resourceManagement.query(IsRelatedTo.NAME, HostingNode.NAME, hostingNodeUUID, Direction.OUT, true, null); resourceList = ISMapper.unmarshalList(Resource.class, json); + Assert.assertTrue(resourceList.size()==0); + + json = resourceManagement.query(IsRelatedTo.NAME, HostingNode.NAME, hostingNodeUUID, Direction.IN, true, null); + resourceList = ISMapper.unmarshalList(Resource.class, json); Assert.assertTrue(resourceList.size()==1); Assert.assertTrue(resourceList.get(0).getHeader().getUUID().compareTo(eServiceUUID)==0); - Assert.assertTrue(resourceList.size()==0); - json = resourceManagement.query(IsRelatedTo.NAME, HostingNode.NAME, hostingNodeUUID, Direction.IN, true, null); + json = resourceManagement.query(IsRelatedTo.NAME, HostingNode.NAME, hostingNodeUUID, Direction.BOTH, false, null); resourceList = ISMapper.unmarshalList(Resource.class, json); Assert.assertTrue(resourceList.size()==0); - - json = resourceManagement.query(IsRelatedTo.NAME, HostingNode.NAME, hostingNodeUUID, Direction.BOTH, true, null); + json = resourceManagement.query(IsRelatedTo.NAME, HostingNode.NAME, hostingNodeUUID, Direction.OUT, false, null); resourceList = ISMapper.unmarshalList(Resource.class, json); Assert.assertTrue(resourceList.size()==0); - json = resourceManagement.query(IsRelatedTo.NAME, HostingNode.NAME, hostingNodeUUID, Direction.OUT, true, null); - resourceList = ISMapper.unmarshalList(Resource.class, json); - Assert.assertTrue(resourceList.size()==0); - - json = resourceManagement.query(IsRelatedTo.NAME, HostingNode.NAME, hostingNodeUUID, Direction.IN, true, null); + json = resourceManagement.query(IsRelatedTo.NAME, HostingNode.NAME, hostingNodeUUID, Direction.IN, false, null); resourceList = ISMapper.unmarshalList(Resource.class, json); Assert.assertTrue(resourceList.size()==0); /* END Getting HostingNode */ @@ -713,9 +708,9 @@ public class ERManagementTest extends ScopedTest { json = resourceManagement.query(ConsistsOf.NAME, SoftwareFacet.NAME, identificationFacetUUID, Direction.OUT, true, null); resourceList = ISMapper.unmarshalList(Resource.class, json); Assert.assertTrue(resourceList.size()==1); - sourceResource = resourceList.get(0); - Facet targetIdentificationFacet = sourceResource.getIdentificationFacets().get(0); - Assert.assertTrue(sourceResource.getHeader().getUUID().compareTo(eServiceUUID)==0); + resource = resourceList.get(0); + Facet targetIdentificationFacet = resource.getIdentificationFacets().get(0); + Assert.assertTrue(resource.getHeader().getUUID().compareTo(eServiceUUID)==0); Assert.assertTrue(targetIdentificationFacet.getHeader().getUUID().compareTo(identificationFacetUUID)==0); try { @@ -726,17 +721,17 @@ public class ERManagementTest extends ScopedTest { try { - json = resourceManagement.query(ConsistsOf.NAME, SoftwareFacet.NAME, identificationFacetUUID, Direction.BOTH, true, null); + json = resourceManagement.query(ConsistsOf.NAME, SoftwareFacet.NAME, identificationFacetUUID, Direction.BOTH, false, null); }catch(InvalidQueryException e) { // Ok expected } - json = resourceManagement.query(ConsistsOf.NAME, SoftwareFacet.NAME, identificationFacetUUID, Direction.OUT, true, null); + json = resourceManagement.query(ConsistsOf.NAME, SoftwareFacet.NAME, identificationFacetUUID, Direction.OUT, false, null); resourceList = ISMapper.unmarshalList(Resource.class, json); Assert.assertTrue(resourceList.size()==0); try { - json = resourceManagement.query(ConsistsOf.NAME, SoftwareFacet.NAME, identificationFacetUUID, Direction.IN, true, null); + json = resourceManagement.query(ConsistsOf.NAME, SoftwareFacet.NAME, identificationFacetUUID, Direction.IN, false, null); }catch(InvalidQueryException e) { // Ok expected }