From 78213468eb817a087888b96b8be959f2cd546832 Mon Sep 17 00:00:00 2001 From: "luca.frosini" Date: Tue, 29 Nov 2016 17:49:43 +0000 Subject: [PATCH] Implementing Referential integrity git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/resource-registry@135078 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../resources/impl/EntityManagementImpl.java | 137 +++++++++++------- .../impl/ContextManagementImplTest.java | 2 +- .../impl/SmartgearResourcesTest.java | 32 ++-- 3 files changed, 100 insertions(+), 71 deletions(-) diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/resources/impl/EntityManagementImpl.java b/src/main/java/org/gcube/informationsystem/resourceregistry/resources/impl/EntityManagementImpl.java index fa80cee..9dec774 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/resources/impl/EntityManagementImpl.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/resources/impl/EntityManagementImpl.java @@ -61,7 +61,6 @@ import com.tinkerpop.blueprints.Element; import com.tinkerpop.blueprints.Vertex; import com.tinkerpop.blueprints.impls.orient.OrientBaseGraph; import com.tinkerpop.blueprints.impls.orient.OrientEdge; -import com.tinkerpop.blueprints.impls.orient.OrientElement; import com.tinkerpop.blueprints.impls.orient.OrientGraph; import com.tinkerpop.blueprints.impls.orient.OrientVertex; @@ -956,7 +955,7 @@ public class EntityManagementImpl implements EntityManagement { } } } - + @Override public boolean deleteFacet(UUID uuid) throws FacetNotFoundException, ResourceRegistryException { @@ -1364,6 +1363,86 @@ public class EntityManagementImpl implements EntityManagement { } } + + private String getTraverseQueryToSelectEntityToDelete(String relationName, UUID uuid){ + /* + * SELECT FROM ( + * TRAVERSE outE('Relation') FROM + * //( SELECT FROM Resource WHERE header.uuid = 'c2001636-9227-425e-a3f5-67902c6fe301' ) + * #: + * ) + * + * WHERE ( + * relationProperty.referentialIntegrity = 'onDeleteCascade' OR + * ( + * relationProperty.referentialIntegrity = 'onDeleteCascadeWhenOrphan' AND + * inV().inE().size()=1 + * ) + *) + * + */ + + + String query = + "SELECT FROM (" + + "TRAVERSE outE('" + relationName +"') FROM " + + "(SELECT FROM " + Resource.NAME + " WHERE " + Resource.HEADER_PROPERTY + "." + Header.UUID_PROPERTY + " = '" + uuid.toString() + "'" + ")" + //+ rid + + ") " + + "WHERE (" + + Relation.RELATION_PROPERTY + "." + RelationProperty.REFERENTIAL_INTEGRITY + " = '" + ReferentiaIntegrity.onDeleteCascade.toString() + "' OR (" + + Relation.RELATION_PROPERTY + "." + RelationProperty.REFERENTIAL_INTEGRITY + " = '" + ReferentiaIntegrity.onDeleteCascadeWhenOrphan.toString() + "' AND " + + "inV().inE().size()=1" + + ")" + + ")"; + + return query; + } + + + private void internalDeleteResource(OrientGraph orientGraph, UUID uuid, Set excludes) throws ResourceNotFoundException, ResourceRegistryException{ + if(excludes == null){ + excludes = new HashSet<>(); + } + excludes.add(uuid); + + Vertex resource = getEntity(orientGraph, uuid, null, Resource.class); + + String query = getTraverseQueryToSelectEntityToDelete(IsRelatedTo.NAME, uuid); + + OSQLSynchQuery osqlSynchQuery = new OSQLSynchQuery<>(query); + Iterable iterable = orientGraph.command(osqlSynchQuery).execute(); + if (iterable != null && iterable.iterator() != null) { + Iterator iterator = iterable.iterator(); + while (iterator.hasNext()) { + OrientEdge relation = iterator.next(); + ODocument oDocument = relation.getInVertex().getRecord(); + ODocument header = oDocument.field(Entity.HEADER_PROPERTY); + String uuidString = header.field(Header.UUID_PROPERTY); + UUID resourceUUID = UUID.fromString(uuidString); + if (uuid.compareTo(resourceUUID)!=0 && !excludes.contains(resourceUUID)){ + internalDeleteResource(orientGraph, resourceUUID, excludes); + } + + } + } + + query = getTraverseQueryToSelectEntityToDelete(ConsistsOf.NAME, uuid); + osqlSynchQuery = new OSQLSynchQuery<>(query); + iterable = orientGraph.command(osqlSynchQuery).execute(); + if (iterable != null && iterable.iterator() !=null) { + Iterator iterator = iterable.iterator(); + while(iterator.hasNext()){ + OrientEdge relation = iterator.next(); + ODocument oDocument = relation.getInVertex().getRecord(); + oDocument.delete(); + } + } + + resource.remove(); + + } + @Override public boolean deleteResource(UUID uuid) throws ResourceNotFoundException, ResourceRegistryException { @@ -1376,62 +1455,10 @@ public class EntityManagementImpl implements EntityManagement { orientGraph = ContextUtility .getActualSecurityContextGraph(PermissionMode.WRITER); - Vertex resource = getEntity(orientGraph, uuid, null, Resource.class); - - /* - * SELECT FROM ( - * TRAVERSE outE('ConsistsOf') FROM - * #: - * // Use this instead of the previous if you want to skip getEntity - * // ( SELECT FROM Resource WHERE header.uuid = 'c2001636-9227-425e-a3f5-67902c6fe301' ) - * ) - * - * WHERE ( - * relationProperty IS null OR - * relationProperty.referentialIntegrity IS null OR - * relationProperty.referentialIntegrity = 'onDeleteCascade' OR ( - * relationProperty.referentialIntegrity = 'onDeleteCascadeWhenOrphan' AND - * inV().inE().size()=1 - * ) - *) - * - */ - String query = - "SELECT FROM (" - + "TRAVERSE outE('" + ConsistsOf.NAME +"') FROM " - + resource.getId() - //+ "SELECT FROM " + Resource.NAME + " WHERE " + Resource.HEADER_PROPERTY + "." + Header.UUID_PROPERTY + " = '" + uuid.toString() + "'" + ")" - + ") " - + "WHERE (" - + Relation.RELATION_PROPERTY + " IS null OR " - + Relation.RELATION_PROPERTY + "." + RelationProperty.REFERENTIAL_INTEGRITY + " IS null OR " - + Relation.RELATION_PROPERTY + "." + RelationProperty.REFERENTIAL_INTEGRITY + " = '" + ReferentiaIntegrity.onDeleteCascade.toString() + "' OR (" - + Relation.RELATION_PROPERTY + "." + RelationProperty.REFERENTIAL_INTEGRITY + " = '" + ReferentiaIntegrity.onDeleteCascadeWhenOrphan.toString() + "' AND " - + "inV().inE().size()=1" - + ")" - + ")"; + internalDeleteResource(orientGraph, uuid, null); - OSQLSynchQuery osqlSynchQuery = new OSQLSynchQuery<>(query); - Iterable iterable = orientGraph.command(osqlSynchQuery).execute(); - if (iterable != null && iterable.iterator() !=null) { - Iterator iterator = iterable.iterator(); - while(iterator.hasNext()){ - OrientElement element = iterator.next(); - if(element instanceof OrientVertex){ - continue; - }else{ - OrientEdge consistsOf = (OrientEdge) element; - consistsOf.getInVertex().getRecord().delete(); - } - - } - } - - resource.remove(); - orientGraph.commit(); - logger.info("{} with UUID {} was successfully deleted.", Resource.NAME, uuid); diff --git a/src/test/java/org/gcube/informationsystem/resourceregistry/resources/impl/ContextManagementImplTest.java b/src/test/java/org/gcube/informationsystem/resourceregistry/resources/impl/ContextManagementImplTest.java index d0f71ae..5c0c3bb 100644 --- a/src/test/java/org/gcube/informationsystem/resourceregistry/resources/impl/ContextManagementImplTest.java +++ b/src/test/java/org/gcube/informationsystem/resourceregistry/resources/impl/ContextManagementImplTest.java @@ -213,7 +213,7 @@ public class ContextManagementImplTest { logger.debug("The DB should be now clean"); } - //@Test + @Test public void createDevContext() throws ContextNotFoundException, ContextException, InternalException, JsonParseException, JsonMappingException, IOException { diff --git a/src/test/java/org/gcube/informationsystem/resourceregistry/resources/impl/SmartgearResourcesTest.java b/src/test/java/org/gcube/informationsystem/resourceregistry/resources/impl/SmartgearResourcesTest.java index 424beae..941ef42 100644 --- a/src/test/java/org/gcube/informationsystem/resourceregistry/resources/impl/SmartgearResourcesTest.java +++ b/src/test/java/org/gcube/informationsystem/resourceregistry/resources/impl/SmartgearResourcesTest.java @@ -24,6 +24,7 @@ import java.util.regex.Pattern; import org.gcube.common.scope.api.ScopeProvider; import org.gcube.informationsystem.impl.embedded.HeaderImpl; +import org.gcube.informationsystem.impl.embedded.RelationPropertyImpl; import org.gcube.informationsystem.impl.entity.facet.CPUFacetImpl; import org.gcube.informationsystem.impl.entity.facet.ContainerStateFacetImpl; import org.gcube.informationsystem.impl.entity.facet.MemoryFacetImpl; @@ -31,11 +32,13 @@ import org.gcube.informationsystem.impl.entity.facet.NetworkingFacetImpl; import org.gcube.informationsystem.impl.entity.facet.SimplePropertyFacetImpl; import org.gcube.informationsystem.impl.entity.facet.SoftwareFacetImpl; import org.gcube.informationsystem.impl.entity.resource.HostingNodeImpl; +import org.gcube.informationsystem.impl.relation.ConsistsOfImpl; import org.gcube.informationsystem.impl.relation.IsIdentifiedByImpl; import org.gcube.informationsystem.impl.relation.consistsof.HasPersistentMemoryImpl; import org.gcube.informationsystem.impl.relation.consistsof.HasVolatileMemoryImpl; import org.gcube.informationsystem.impl.utils.Entities; import org.gcube.informationsystem.model.embedded.Header; +import org.gcube.informationsystem.model.embedded.RelationProperty; import org.gcube.informationsystem.model.entity.Facet; import org.gcube.informationsystem.model.entity.Resource; import org.gcube.informationsystem.model.entity.facet.CPUFacet; @@ -52,7 +55,6 @@ import org.gcube.informationsystem.model.relation.IsIdentifiedBy; import org.gcube.informationsystem.model.relation.consistsof.HasPersistentMemory; import org.gcube.informationsystem.model.relation.consistsof.HasVolatileMemory; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; -import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resource.ResourceNotFoundException; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -127,15 +129,19 @@ public class SmartgearResourcesTest { } networkingFacet.setHostName("pc-frosini.isti.cnr.it"); networkingFacet.setDomainName(getDomain(networkingFacet.getHostName())); - networkingFacet.setAdditionalProperty("Port", 8080); + + RelationProperty relationProperty = new RelationPropertyImpl(); + relationProperty.setReferentialIntegrity(RelationProperty.ReferentiaIntegrity.onDeleteCascadeWhenOrphan); + IsIdentifiedBy isIdentifiedBy = - new IsIdentifiedByImpl<>(hostingNode, networkingFacet, null); + new IsIdentifiedByImpl<>(hostingNode, networkingFacet, relationProperty); hostingNode.addFacet(isIdentifiedBy); List cpuFacets = getCPUFacets(); for (CPUFacet cpuFacet: cpuFacets) { - hostingNode.addFacet(cpuFacet); + ConsistsOf consistsOf = new ConsistsOfImpl(hostingNode, cpuFacet, relationProperty); + hostingNode.addFacet(consistsOf); } @@ -144,11 +150,13 @@ public class SmartgearResourcesTest { softwareFacet.setGroup(mxbean.getName()); // softwareFacet.setGroup(System.getProperty("os.name")); softwareFacet.setName(mxbean.getArch()); // softwareFacet.setName(System.getProperty("os.arch")); softwareFacet.setVersion(mxbean.getVersion()); // softwareFacet.setName(System.getProperty("os.version")); - hostingNode.addFacet(softwareFacet); + ConsistsOf sfR = new ConsistsOfImpl(hostingNode, softwareFacet, relationProperty); + hostingNode.addFacet(sfR); SimplePropertyFacet simplePropertyFacet = addEnvironmentVariables(); - hostingNode.addFacet(simplePropertyFacet); + ConsistsOf spfR = new ConsistsOfImpl(hostingNode, simplePropertyFacet, relationProperty); + hostingNode.addFacet(spfR); ContainerStateFacet containerStateFacet = getContainerStateFacet(null); hostingNode.addFacet(containerStateFacet); @@ -157,20 +165,20 @@ public class SmartgearResourcesTest { MemoryFacet ramFacet = getRamInfo(null); HasVolatileMemory hasVolatileRAMMemory = new HasVolatileMemoryImpl( - hostingNode, ramFacet, null); + hostingNode, ramFacet, relationProperty); hasVolatileRAMMemory.setAdditionalProperty(MEMORY_TYPE, MEMORY_TYPE_RAM); hostingNode.addFacet(hasVolatileRAMMemory); MemoryFacet jvmMemoryFacet = getJVMMemoryInfo(null); HasVolatileMemory hasVolatileJVMMemory = new HasVolatileMemoryImpl( - hostingNode, jvmMemoryFacet, null); + hostingNode, jvmMemoryFacet, relationProperty); hasVolatileJVMMemory.setAdditionalProperty(MEMORY_TYPE, MEMORY_TYPE_JVM); hostingNode.addFacet(hasVolatileJVMMemory); MemoryFacet disk = getDiskSpace(null); HasPersistentMemory hasPersistentMemory = - new HasPersistentMemoryImpl(hostingNode, disk, null); + new HasPersistentMemoryImpl(hostingNode, disk, relationProperty); hostingNode.addFacet(hasPersistentMemory); String json = entityManagementImpl.createResource(HostingNode.NAME, Entities.marshal(hostingNode)); @@ -228,12 +236,6 @@ public class SmartgearResourcesTest { } - @Test - public void testDelete() throws ResourceNotFoundException, ResourceRegistryException{ - ScopeProvider.instance.set("/gcube/devNext"); - entityManagementImpl.deleteResource(UUID.fromString("9a2cedcb-2df2-49d7-93b9-f2b34d70fc06")); - } - private ContainerStateFacet getContainerStateFacet(ContainerStateFacet containerStateFacet){ if(containerStateFacet == null){ containerStateFacet = new ContainerStateFacetImpl();