Deleting facet when deleting resource taking in account referential intergity attribute on consistsOf relation if any.

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/resource-registry@135032 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2016-11-29 12:10:49 +00:00
parent 35adebb0fa
commit bcad10fe05
2 changed files with 69 additions and 14 deletions

View File

@ -20,6 +20,8 @@ import org.codehaus.jettison.json.JSONObject;
import org.gcube.informationsystem.impl.utils.Entities;
import org.gcube.informationsystem.model.embedded.Embedded;
import org.gcube.informationsystem.model.embedded.Header;
import org.gcube.informationsystem.model.embedded.RelationProperty;
import org.gcube.informationsystem.model.embedded.RelationProperty.ReferentiaIntegrity;
import org.gcube.informationsystem.model.entity.Entity;
import org.gcube.informationsystem.model.entity.Facet;
import org.gcube.informationsystem.model.entity.Resource;
@ -52,12 +54,14 @@ import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.JsonNodeType;
import com.orientechnologies.orient.core.record.impl.ODocument;
import com.orientechnologies.orient.core.sql.query.OSQLSynchQuery;
import com.tinkerpop.blueprints.Direction;
import com.tinkerpop.blueprints.Edge;
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;
@ -581,20 +585,22 @@ public class EntityManagementImpl implements EntityManagement {
try {
UUID uuid = org.gcube.informationsystem.impl.utils.Utility
.getUUIDFromJsonNode(jsonNode);
Vertex v = getEntity(orientGraph, jsonNode, uuid, entity);
if(v!=null){
String error = String.format("An %s with UUID %s already exist", entity.getSimpleName(), uuid.toString());
if (v != null) {
String error = String.format(
"An %s with UUID %s already exist",
entity.getSimpleName(), uuid.toString());
if (Facet.class.isAssignableFrom(entity)) {
throw new FacetAlreadyPresentException(error);
}
if (Resource.class.isAssignableFrom(entity)) {
throw new ResourceAlreadyPresentException(error);
}
throw new EntityAlreadyPresentException(error);
}
} catch (EntityAlreadyPresentException e) {
throw e;
} catch (Exception e) {
@ -1372,14 +1378,56 @@ public class EntityManagementImpl implements EntityManagement {
Vertex resource = getEntity(orientGraph, uuid, null, Resource.class);
// TODO remove attached facets if not managed from hooks
// SELECT FROM (TRAVERSE outE("ConsistsOf") FROM #145:27) WHERE
// relationProperty IS null OR relationProperty.referentialIntegrity
// IS null OR relationProperty.referentialIntegrity =
// 'onDeleteCascade' OR (relationProperty.referentialIntegrity =
// 'onDeleteCascadeWhenOrphan' AND inV().inE().size()=1)
/*
* SELECT FROM (
* TRAVERSE outE('ConsistsOf') FROM
* #<cluster>:<position>
* // 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"
+ ")"
+ ")";
OSQLSynchQuery<OrientElement> osqlSynchQuery = new OSQLSynchQuery<>(query);
Iterable<OrientElement> iterable = orientGraph.command(osqlSynchQuery).execute();
if (iterable != null && iterable.iterator() !=null) {
Iterator<OrientElement> 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();

View File

@ -52,6 +52,7 @@ 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;
@ -222,10 +223,16 @@ public class SmartgearResourcesTest {
HostingNode hostingNodeToUpdated = Entities.unmarshal(HostingNode.class, updatedHN);
logger.debug("Upodate Hosting Node {}", hostingNodeToUpdated);
entityManagementImpl.deleteResource(uuid);
}
@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){