diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/contexts/entities/ContextManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/contexts/entities/ContextManagement.java index c510bed..2c1e6fa 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/contexts/entities/ContextManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/contexts/entities/ContextManagement.java @@ -4,7 +4,6 @@ import java.io.IOException; import java.util.HashMap; import java.util.Iterator; import java.util.List; -import java.util.Map; import java.util.UUID; import org.gcube.com.fasterxml.jackson.core.JsonProcessingException; @@ -49,19 +48,19 @@ import com.orientechnologies.orient.core.sql.executor.OResultSet; * @author Luca Frosini (ISTI - CNR) */ public class ContextManagement extends EntityElementManagement { - + private static Logger logger = LoggerFactory.getLogger(ContextManagement.class); - + protected String name; - + private void init() { this.ignoreStartWithKeys.add(Context.PARENT_PROPERTY); this.ignoreStartWithKeys.add(Context.CHILDREN_PROPERTY); this.typeName = Context.NAME; } - + protected ContextCacheRenewal contextCacheRenewal = new ContextCacheRenewal() { - + @Override public List renew() throws ResourceRegistryException { String contextsJsonString = allFromServer(false); @@ -73,25 +72,25 @@ public class ContextManagement extends EntityElementManagement { } return contexts; } - }; - + }; + public ContextManagement() { super(AccessType.CONTEXT); init(); ContextCache contextCache = ContextCache.getInstance(); contextCache.setContextCacheRenewal(contextCacheRenewal); } - + public ContextManagement(ODatabaseDocument oDatabaseDocument) throws ResourceRegistryException { this(); this.oDatabaseDocument = oDatabaseDocument; getWorkingContext(); } - + public String getName() { - if(name == null) { - if(element == null) { - if(jsonNode != null) { + if (name == null) { + if (element == null) { + if (jsonNode != null) { name = jsonNode.get(Context.NAME_PROPERTY).asText(); } } else { @@ -100,160 +99,158 @@ public class ContextManagement extends EntityElementManagement { } return name; } - + @Override protected SecurityContext getWorkingContext() throws ResourceRegistryException { - if(workingContext == null) { + if (workingContext == null) { workingContext = ContextUtility.getInstance() .getSecurityContextByUUID(DatabaseEnvironment.CONTEXT_SECURITY_CONTEXT_UUID); } return workingContext; } - + @Override protected ContextNotFoundException getSpecificElementNotFoundException(NotFoundException e) { return new ContextNotFoundException(e.getMessage(), e.getCause()); } - + @Override protected ContextAlreadyPresentException getSpecificERAlreadyPresentException(String message) { return new ContextAlreadyPresentException(message); } - + protected void checkContext(ContextManagement parentContext) throws ContextNotFoundException, ContextAlreadyPresentException, ResourceRegistryException { - - if(parentContext != null) { + + if (parentContext != null) { String parentId = parentContext.getElement().getIdentity().toString(); - + String select = "SELECT FROM (TRAVERSE out(" + IsParentOf.NAME + ") FROM " + parentId + " MAXDEPTH 1) WHERE " + Context.NAME_PROPERTY + "=\"" + getName() + "\" AND " + Context.HEADER_PROPERTY + "." + Header.UUID_PROPERTY + "<>\"" + parentContext.uuid + "\""; - + logger.trace(select); - + StringBuilder message = new StringBuilder(); message.append("A context with name ("); message.append(getName()); message.append(") has been already created as child of "); message.append(parentContext.getElement().toString()); - + logger.trace("Checking if {} -> {}", message, select); - + OResultSet resultSet = oDatabaseDocument.command(select, new HashMap<>()); - - if(resultSet != null && resultSet.hasNext()) { + + if (resultSet != null && resultSet.hasNext()) { throw new ContextAlreadyPresentException(message.toString()); } - + } else { - String select = "SELECT FROM " + Context.NAME + " WHERE " - + Context.NAME_PROPERTY + " = \"" + getName() + "\"" + " AND in(\"" + IsParentOf.NAME - + "\").size() = 0"; - + String select = "SELECT FROM " + Context.NAME + " WHERE " + Context.NAME_PROPERTY + " = \"" + getName() + + "\"" + " AND in(\"" + IsParentOf.NAME + "\").size() = 0"; + OResultSet resultSet = oDatabaseDocument.command(select, new HashMap<>()); - - if(resultSet != null && resultSet.hasNext()) { + + if (resultSet != null && resultSet.hasNext()) { throw new ContextAlreadyPresentException( "A root context with the same name (" + this.getName() + ") already exist"); } - + } - + } - + @Override protected JsonNode createCompleteJsonNode() throws ResourceRegistryException { - + JsonNode context = serializeSelfAsJsonNode(); - + int count = 0; Iterable parents = getElement().getEdges(ODirection.IN); - for(OEdge edge : parents) { - if(++count > 1) { + for (OEdge edge : parents) { + if (++count > 1) { throw new ContextException("A " + Context.NAME + " can not have more than one parent"); } try { IsParentOfManagement isParentOfManagement = new IsParentOfManagement(oDatabaseDocument); isParentOfManagement.setElement(edge); JsonNode isParentOf = isParentOfManagement.createCompleteJsonNode(true, false); - if(isParentOf!=null) { + if (isParentOf != null) { ((ObjectNode) context).replace(Context.PARENT_PROPERTY, isParentOf); } - } catch(Exception e) { + } catch (Exception e) { logger.error("Unable to correctly serialize {}. {}", edge, Utility.SHOULD_NOT_OCCUR_ERROR_MESSAGE); throw new ContextException(""); } } - + Iterable childrenEdges = getElement().getEdges(ODirection.OUT); - for(OEdge edge : childrenEdges) { - + for (OEdge edge : childrenEdges) { + IsParentOfManagement isParentOfManagement = new IsParentOfManagement(oDatabaseDocument); isParentOfManagement.setElement(edge); try { JsonNode isParentOf = isParentOfManagement.serializeAsJsonNode(); context = addRelation(context, isParentOf, Context.CHILDREN_PROPERTY); - } catch(ResourceRegistryException e) { + } catch (ResourceRegistryException e) { logger.error("Unable to correctly serialize {}. {}", edge, Utility.SHOULD_NOT_OCCUR_ERROR_MESSAGE); throw e; - } catch(Exception e) { + } catch (Exception e) { logger.error("Unable to correctly serialize {}. {}", edge, Utility.SHOULD_NOT_OCCUR_ERROR_MESSAGE); throw new ResourceRegistryException(e); } } - + return context; } - + @Override protected OVertex reallyCreate() throws AlreadyPresentException, ResourceRegistryException { SecurityContext securityContext = null; SecurityContext parentSecurityContext = null; - + try { JsonNode isParentOfJsonNode = jsonNode.get(Context.PARENT_PROPERTY); - - if(isParentOfJsonNode != null && !(isParentOfJsonNode instanceof NullNode)) { - + + if (isParentOfJsonNode != null && !(isParentOfJsonNode instanceof NullNode)) { + JsonNode parentJsonNode = isParentOfJsonNode.get(Relation.SOURCE_PROPERTY); ContextManagement parentContextManagement = new ContextManagement(oDatabaseDocument); parentContextManagement.setJsonNode(parentJsonNode); UUID parentUUID = parentContextManagement.uuid; parentSecurityContext = ContextUtility.getInstance().getSecurityContextByUUID(parentUUID); - - + checkContext(parentContextManagement); - if(uuid == null) { + if (uuid == null) { uuid = UUID.randomUUID(); } - + createVertex(); - + IsParentOfManagement isParentOfManagement = new IsParentOfManagement(oDatabaseDocument); isParentOfManagement.setJsonNode(isParentOfJsonNode); isParentOfManagement.setSourceEntityManagement(parentContextManagement); isParentOfManagement.setTargetEntityManagement(this); - + isParentOfManagement.internalCreate(); - + } else { checkContext(null); createVertex(); } - + securityContext = new SecurityContext(uuid); securityContext.setParentSecurityContext(parentSecurityContext); securityContext.create(oDatabaseDocument); - + ContextUtility.getInstance().addSecurityContext(securityContext); - + return getElement(); - } catch(Exception e) { + } catch (Exception e) { oDatabaseDocument.rollback(); - if(securityContext != null) { + if (securityContext != null) { securityContext.delete(oDatabaseDocument); - if(parentSecurityContext!=null && securityContext!=null) { + if (parentSecurityContext != null && securityContext != null) { parentSecurityContext.getChildren().remove(securityContext); } ContextCache.getInstance().cleanCache(); @@ -261,19 +258,19 @@ public class ContextManagement extends EntityElementManagement { throw e; } } - + @Override protected OVertex reallyUpdate() throws NotFoundException, ResourceRegistryException { - + boolean parentChanged = false; boolean nameChanged = false; - + OVertex parent = null; boolean found = false; - + Iterable iterable = getElement().getVertices(ODirection.IN, IsParentOf.NAME); - for(OVertex p : iterable) { - if(found) { + for (OVertex p : iterable) { + if (found) { String message = String.format("{} has more than one parent. {}", Context.NAME, Utility.SHOULD_NOT_OCCUR_ERROR_MESSAGE); throw new ResourceRegistryException(message.toString()); @@ -281,142 +278,142 @@ public class ContextManagement extends EntityElementManagement { parent = p; found = true; } - + ContextManagement actualParentContextManagement = null; - if(parent != null) { + if (parent != null) { actualParentContextManagement = new ContextManagement(oDatabaseDocument); actualParentContextManagement.setElement(parent); } - + ContextManagement newParentContextManagement = actualParentContextManagement; - + JsonNode isParentOfJsonNode = jsonNode.get(Context.PARENT_PROPERTY); JsonNode parentContextJsonNode = null; - if(isParentOfJsonNode != null && !(isParentOfJsonNode instanceof NullNode)) { + if (isParentOfJsonNode != null && !(isParentOfJsonNode instanceof NullNode)) { parentContextJsonNode = isParentOfJsonNode.get(Relation.SOURCE_PROPERTY); } - - if(parentContextJsonNode != null && !(parentContextJsonNode instanceof NullNode)) { + + if (parentContextJsonNode != null && !(parentContextJsonNode instanceof NullNode)) { UUID parentUUID = org.gcube.informationsystem.utils.Utility.getUUIDFromJsonNode(parentContextJsonNode); - if(actualParentContextManagement != null) { - if(parentUUID.compareTo(actualParentContextManagement.uuid) != 0) { + if (actualParentContextManagement != null) { + if (parentUUID.compareTo(actualParentContextManagement.uuid) != 0) { parentChanged = true; } } else { parentChanged = true; } - - if(parentChanged) { + + if (parentChanged) { newParentContextManagement = new ContextManagement(oDatabaseDocument); newParentContextManagement.setJsonNode(parentContextJsonNode); } } else { - if(actualParentContextManagement != null) { + if (actualParentContextManagement != null) { parentChanged = true; newParentContextManagement = null; } - + } - + String oldName = getElement().getProperty(Context.NAME_PROPERTY); String newName = jsonNode.get(Context.NAME_PROPERTY).asText(); - if(oldName.compareTo(newName) != 0) { + if (oldName.compareTo(newName) != 0) { nameChanged = true; name = newName; } - - if(parentChanged || nameChanged) { + + if (parentChanged || nameChanged) { checkContext(newParentContextManagement); } - - if(parentChanged) { + + if (parentChanged) { move(newParentContextManagement, false); } - - element = (OVertex) updateProperties(oClass, getElement(), jsonNode, ignoreKeys, - ignoreStartWithKeys); - + + element = (OVertex) updateProperties(oClass, getElement(), jsonNode, ignoreKeys, ignoreStartWithKeys); + ContextCache.getInstance().cleanCache(); - + return element; } - + private void move(ContextManagement newParentContextManagement, boolean check) throws ContextNotFoundException, ContextAlreadyPresentException, ResourceRegistryException { - if(check) { + if (check) { checkContext(newParentContextManagement); } - + SecurityContext newParentSecurityContext = null; - + // Removing the old parent relationship if any Iterable edges = getElement().getEdges(ODirection.IN, IsParentOf.NAME); - if(edges != null && edges.iterator().hasNext()) { + if (edges != null && edges.iterator().hasNext()) { Iterator edgeIterator = edges.iterator(); OEdge edge = edgeIterator.next(); IsParentOfManagement isParentOfManagement = new IsParentOfManagement(); isParentOfManagement.setElement(edge); isParentOfManagement.internalDelete(); - - if(edgeIterator.hasNext()) { + + if (edgeIterator.hasNext()) { throw new ContextException( "Seems that the Context has more than one Parent. " + Utility.SHOULD_NOT_OCCUR_ERROR_MESSAGE); } } - - if(newParentContextManagement != null) { + + if (newParentContextManagement != null) { JsonNode isParentOfJsonNode = jsonNode.get(Context.PARENT_PROPERTY); IsParentOfManagement isParentOfManagement = new IsParentOfManagement(oDatabaseDocument); isParentOfManagement.setJsonNode(isParentOfJsonNode); isParentOfManagement.setSourceEntityManagement(newParentContextManagement); isParentOfManagement.setTargetEntityManagement(this); isParentOfManagement.internalCreate(); - newParentSecurityContext = ContextUtility.getInstance().getSecurityContextByUUID(newParentContextManagement.uuid); + newParentSecurityContext = ContextUtility.getInstance() + .getSecurityContextByUUID(newParentContextManagement.uuid); } - + SecurityContext thisSecurityContext = ContextUtility.getInstance().getSecurityContextByUUID(uuid); thisSecurityContext.changeParentSecurityContext(newParentSecurityContext, oDatabaseDocument); } - + @Override protected boolean reallyDelete() throws NotFoundException, ResourceRegistryException { Iterable iterable = getElement().getEdges(ODirection.OUT); Iterator iterator = iterable.iterator(); - while(iterator.hasNext()) { + while (iterator.hasNext()) { throw new ContextException("Cannot remove a " + Context.NAME + " having children"); } - + element.delete(); - + ContextUtility contextUtility = ContextUtility.getInstance(); SecurityContext securityContext = contextUtility.getSecurityContextByUUID(uuid); securityContext.delete(oDatabaseDocument); - + ContextCache.getInstance().cleanCache(); - + return true; - + } - + @Override public String reallyGetAll(boolean polymorphic) throws ResourceRegistryException { ObjectMapper objectMapper = new ObjectMapper(); ArrayNode arrayNode = objectMapper.createArrayNode(); Iterable iterable = oDatabaseDocument.browseClass(typeName, polymorphic); - for(ODocument vertex : iterable) { + for (ODocument vertex : iterable) { ContextManagement contextManagement = new ContextManagement(); contextManagement.setElement((OVertex) vertex); try { JsonNode jsonObject = contextManagement.serializeAsJsonNode(); arrayNode.add(jsonObject); - } catch(ResourceRegistryException e) { + } catch (ResourceRegistryException e) { logger.error("Unable to correctly serialize {}. It will be excluded from results. {}", vertex.toString(), Utility.SHOULD_NOT_OCCUR_ERROR_MESSAGE); } } try { return objectMapper.writeValueAsString(arrayNode); - } catch(JsonProcessingException e) { + } catch (JsonProcessingException e) { throw new ResourceRegistryException(e); } } @@ -424,7 +421,7 @@ public class ContextManagement extends EntityElementManagement { public String allFromServer(boolean polymorphic) throws ResourceRegistryException { return super.all(polymorphic); } - + @Override public String all(boolean polymorphic) throws ResourceRegistryException { try { @@ -434,12 +431,14 @@ public class ContextManagement extends EntityElementManagement { return allFromServer(polymorphic); } } - - public String readFromServer() throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException { + + public String readFromServer() + throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException { return super.read().toString(); } - - public String readAsString() throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException { + + public String readAsString() + throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException { try { ContextCache contextCache = ContextCache.getInstance(); return ElementMapper.marshal(contextCache.getContextByUUID(uuid)); @@ -447,10 +446,9 @@ public class ContextManagement extends EntityElementManagement { return readFromServer(); } } - + @Override - public void sanityCheck(Map expectedInstances) - throws SchemaViolationException, ResourceRegistryException { + public void sanityCheck() throws SchemaViolationException, ResourceRegistryException { // Nothing to do } } diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/contexts/relations/IsParentOfManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/contexts/relations/IsParentOfManagement.java index c5e193c..985798e 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/contexts/relations/IsParentOfManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/contexts/relations/IsParentOfManagement.java @@ -1,8 +1,5 @@ package org.gcube.informationsystem.resourceregistry.contexts.relations; -import java.util.Map; -import java.util.UUID; - import org.gcube.com.fasterxml.jackson.databind.JsonNode; import org.gcube.com.fasterxml.jackson.databind.node.ObjectNode; import org.gcube.informationsystem.base.reference.AccessType; @@ -27,77 +24,80 @@ import com.orientechnologies.orient.core.record.OVertex; /** * @author Luca Frosini (ISTI - CNR) */ -public class IsParentOfManagement extends RelationElementManagement { - +public class IsParentOfManagement extends RelationElementManagement { + public IsParentOfManagement() { super(AccessType.IS_PARENT_OF, Context.class, Context.class); } - + public IsParentOfManagement(ODatabaseDocument oDatabaseDocument) throws ResourceRegistryException { this(); this.oDatabaseDocument = oDatabaseDocument; getWorkingContext(); } - + @Override protected SecurityContext getWorkingContext() throws ResourceRegistryException { - if(workingContext == null) { + if (workingContext == null) { workingContext = ContextUtility.getInstance() .getSecurityContextByUUID(DatabaseEnvironment.CONTEXT_SECURITY_CONTEXT_UUID); } return workingContext; } - + @Override protected IsParentOfNotFoundException getSpecificElementNotFoundException(NotFoundException e) { return new IsParentOfNotFoundException(e.getMessage(), e.getCause()); } - + @Override protected IsParentOfAlreadyPresentException getSpecificERAlreadyPresentException(String message) { return new IsParentOfAlreadyPresentException(message); } - + @Override protected JsonNode createCompleteJsonNode() throws ResourceRegistryException { return createCompleteJsonNode(false, true); } - + @Override - public JsonNode createCompleteJsonNode(boolean includeSource, boolean includeTarget) throws ResourceRegistryException { + public JsonNode createCompleteJsonNode(boolean includeSource, boolean includeTarget) + throws ResourceRegistryException { JsonNode relation = serializeSelfAsJsonNode(); - + try { OVertex source = element.getVertex(ODirection.OUT); ContextManagement sourceContextManagement = new ContextManagement(oDatabaseDocument); sourceContextManagement.setElement(source); - if(includeSource) { - ((ObjectNode)relation).replace(Relation.SOURCE_PROPERTY, sourceContextManagement.serializeSelfAsJsonNode()); + if (includeSource) { + ((ObjectNode) relation).replace(Relation.SOURCE_PROPERTY, + sourceContextManagement.serializeSelfAsJsonNode()); } - + OVertex target = element.getVertex(ODirection.IN); ContextManagement targetContextManagement = new ContextManagement(oDatabaseDocument); targetContextManagement.setElement(target); - if(includeTarget) { - ((ObjectNode)relation).replace(Relation.TARGET_PROPERTY, targetContextManagement.serializeSelfAsJsonNode()); + if (includeTarget) { + ((ObjectNode) relation).replace(Relation.TARGET_PROPERTY, + targetContextManagement.serializeSelfAsJsonNode()); } - - } catch(ResourceRegistryException e) { + + } catch (ResourceRegistryException e) { logger.error("Unable to correctly serialize {}. {}", element, Utility.SHOULD_NOT_OCCUR_ERROR_MESSAGE, e); throw e; - } catch(Exception e) { + } catch (Exception e) { logger.error("Unable to correctly serialize {}. {}", element, Utility.SHOULD_NOT_OCCUR_ERROR_MESSAGE, e); throw new ResourceRegistryException(e); } - + return relation; } - + @Override protected ContextManagement newSourceEntityManagement() throws ResourceRegistryException { return new ContextManagement(oDatabaseDocument); } - + @Override protected ContextManagement newTargetEntityManagement() throws ResourceRegistryException { return new ContextManagement(oDatabaseDocument); @@ -109,9 +109,8 @@ public class IsParentOfManagement extends RelationElementManagement expectedInstances) - throws SchemaViolationException, ResourceRegistryException { + public void sanityCheck() throws SchemaViolationException, ResourceRegistryException { // Nothing to do } - + } diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/base/ElementManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/base/ElementManagement.java index 5ed2ff4..f5cda87 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/base/ElementManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/base/ElementManagement.java @@ -448,7 +448,9 @@ public abstract class ElementManagement { public abstract String reallyGetAll(boolean polymorphic) throws ResourceRegistryException; - public abstract void sanityCheck(Map expectedInstances) throws SchemaViolationException, ResourceRegistryException; + public abstract void sanityCheck() throws SchemaViolationException, ResourceRegistryException; + + //public abstract void sanityCheck(Map expectedInstances) throws SchemaViolationException, ResourceRegistryException; public String all(boolean polymorphic) throws ResourceRegistryException { ODatabaseDocument current = ContextUtility.getCurrentODatabaseDocumentFromThreadLocal(); @@ -513,7 +515,7 @@ public abstract class ElementManagement { element = internalCreate(); } - sanityCheck(null); + sanityCheck(); oDatabaseDocument.commit(); @@ -556,7 +558,7 @@ public abstract class ElementManagement { element = internalCreate(); - sanityCheck(null); + sanityCheck(); oDatabaseDocument.commit(); @@ -621,7 +623,7 @@ public abstract class ElementManagement { element = internalUpdate(); - sanityCheck(null); + sanityCheck(); oDatabaseDocument.commit(); diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/ERManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/ERManagement.java index d2c12c1..6677abb 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/ERManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/ERManagement.java @@ -33,7 +33,8 @@ public interface ERManagement { public Map removeFromContext(UUID contextUUID) throws SchemaViolationException, NotFoundException, ContextException, ResourceRegistryException; - public void sanityCheck(Map expectedInstances) throws SchemaViolationException, ResourceRegistryException; + public void sanityCheck() throws SchemaViolationException, ResourceRegistryException; + //public void sanityCheck(Map expectedInstances) throws SchemaViolationException, ResourceRegistryException; public AvailableInAnotherContextException getSpecificERAvailableInAnotherContextException(String message); diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/ERManagementUtility.java b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/ERManagementUtility.java index 007fafb..a6a2110 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/ERManagementUtility.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/ERManagementUtility.java @@ -10,7 +10,6 @@ import org.gcube.informationsystem.base.reference.Element; import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextException; -import org.gcube.informationsystem.resourceregistry.api.rest.SharingPath.SharingOperation; import org.gcube.informationsystem.resourceregistry.contexts.ContextUtility; import org.gcube.informationsystem.resourceregistry.contexts.security.AdminSecurityContext; import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityContext; @@ -39,6 +38,7 @@ public class ERManagementUtility { SecurityContext targetSecurityContext = ContextUtility.getInstance().getSecurityContextByUUID(contextUUID); + Map> instancesManagement = new HashMap<>(); for(UUID uuid : expectedInstances.keySet()) { String type = expectedInstances.get(uuid).get(Element.CLASS_PROPERTY).asText(); ElementManagement elementManagement = ElementManagementUtility.getERManagement(type); @@ -49,7 +49,13 @@ public class ERManagementUtility { ((ERManagement) elementManagement).setHonourPropagationConstraintsInContextSharing(false); ((ERManagement) elementManagement).setDryRunContextSharing(dryRun); ((ERManagement) elementManagement).internalAddToContext(targetSecurityContext); - ((ERManagement) elementManagement).sanityCheck(expectedInstances); + instancesManagement.put(uuid, elementManagement); + } + + for(UUID uuid : expectedInstances.keySet()) { + ElementManagement elementManagement = instancesManagement.get(uuid); + // TODO + elementManagement.sanityCheck(); } /* @@ -120,14 +126,16 @@ public class ERManagementUtility { affectedInstances.putAll(((ERManagement) elementManagement).internalRemoveFromContext(targetSecurityContext)); - ((ERManagement) elementManagement).sanityCheck(expectedInstances); + ((ERManagement) elementManagement).sanityCheck(); } + /* SharingOperationValidator operationValidator = new SharingOperationValidator(expectedInstances, SharingOperation.REMOVE); if(operationValidator.isValidOperation(affectedInstances)) { oDatabaseDocument.commit(); } + */ staticLogger.info("{} successfully removed from Context with UUID {} not following Propagation Constraints", instances, contextUUID); diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/entities/FacetManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/entities/FacetManagement.java index b27756a..a650ba4 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/entities/FacetManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/entities/FacetManagement.java @@ -1,8 +1,5 @@ package org.gcube.informationsystem.resourceregistry.instances.model.entities; -import java.util.Map; -import java.util.UUID; - import org.gcube.com.fasterxml.jackson.databind.JsonNode; import org.gcube.informationsystem.base.reference.AccessType; import org.gcube.informationsystem.model.reference.entities.Facet; @@ -63,7 +60,7 @@ public class FacetManagement extends EntityManagement { } @Override - public void sanityCheck(Map expectedInstances) throws ResourceRegistryException { + public void sanityCheck() throws ResourceRegistryException { // TODO Auto-generated method stub } diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/entities/ResourceManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/entities/ResourceManagement.java index 8fec800..85e6bd2 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/entities/ResourceManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/entities/ResourceManagement.java @@ -12,11 +12,9 @@ import org.gcube.com.fasterxml.jackson.databind.JsonNode; import org.gcube.com.fasterxml.jackson.databind.node.ArrayNode; import org.gcube.informationsystem.base.reference.AccessType; import org.gcube.informationsystem.base.reference.Element; -import org.gcube.informationsystem.base.reference.IdentifiableElement; import org.gcube.informationsystem.context.reference.entities.Context; import org.gcube.informationsystem.model.reference.entities.Facet; import org.gcube.informationsystem.model.reference.entities.Resource; -import org.gcube.informationsystem.model.reference.properties.Header; import org.gcube.informationsystem.model.reference.relations.ConsistsOf; import org.gcube.informationsystem.model.reference.relations.IsRelatedTo; import org.gcube.informationsystem.model.reference.relations.Relation; @@ -311,25 +309,6 @@ public class ResourceManagement extends EntityManagement { } } - private String facetMustBePresentErrorMessage(String consistsOfType, UUID consistsOfUUID, String facetType, UUID facetUUID) { - StringBuffer stringBuffer = new StringBuffer(); - stringBuffer.append("To avoid to have an incosistent graph, add to context no follows cannot add a "); - stringBuffer.append(ConsistsOf.NAME); - stringBuffer.append(" relation (i.e. "); - stringBuffer.append(consistsOfType); - stringBuffer.append(" with UUID "); - stringBuffer.append(consistsOfUUID.toString()); - stringBuffer.append(") without indicating the target "); - stringBuffer.append(Facet.NAME); - stringBuffer.append(" (i.e. "); - stringBuffer.append(facetType); - stringBuffer.append(" with UUID "); - stringBuffer.append(facetUUID.toString()); - stringBuffer.append(")."); - return stringBuffer.toString(); - } - - protected Set getResourceTypeConstraint() throws SchemaException, ResourceRegistryException{ Set constraints = new HashSet<>(); @@ -420,6 +399,76 @@ public class ResourceManagement extends EntityManagement { return true; } + @Override + public void sanityCheck() throws SchemaViolationException, ResourceRegistryException { + JsonNode resourceInstance = createCompleteJsonNode(); + + TypesCache typesCache = TypesCache.getInstance(); + + Set consistsOfFacetConstraints = getResourceTypeConstraint(); + Map satisfiedConsistsOfFacet = new HashMap<>(); + + ArrayNode consistsOfArrayNode = (ArrayNode) resourceInstance.get(Resource.CONSISTS_OF_PROPERTY); + for(JsonNode consistsOfJsonNode : consistsOfArrayNode) { + String consistsOfType = consistsOfJsonNode.get(Element.CLASS_PROPERTY).asText(); + JsonNode facetJsonNode = consistsOfJsonNode.get(Relation.TARGET_PROPERTY); + String facetType = facetJsonNode.get(Element.CLASS_PROPERTY).asText(); + + for(LinkedEntity constraint : consistsOfFacetConstraints) { + if(constraintSatisfied(typesCache, constraint, consistsOfType, facetType)) { + Integer integer = satisfiedConsistsOfFacet.get(constraint); + if(integer==null) { + satisfiedConsistsOfFacet.put(constraint, 1); + }else { + satisfiedConsistsOfFacet.put(constraint, ++integer); + } + } + + } + } + + consistsOfFacetConstraints.removeAll(satisfiedConsistsOfFacet.keySet()); + + if(!consistsOfFacetConstraints.isEmpty()) { + String message = constraintNotSatisfiedErrorMessage(consistsOfFacetConstraints.iterator().next(), 0); + throw new SchemaViolationException(message); + } + + for(LinkedEntity linkedEntity : satisfiedConsistsOfFacet.keySet()) { + Integer satisfiedTimes = satisfiedConsistsOfFacet.get(linkedEntity); + if(satisfiedTimes0) && satisfiedTimes>max) { + String message = constraintNotSatisfiedErrorMessage(linkedEntity, satisfiedTimes); + throw new SchemaViolationException(message); + } + } + + } + + /* + + private String facetMustBePresentErrorMessage(String consistsOfType, UUID consistsOfUUID, String facetType, UUID facetUUID) { + StringBuffer stringBuffer = new StringBuffer(); + stringBuffer.append("To avoid to have an incosistent graph, add to context no follows cannot add a "); + stringBuffer.append(ConsistsOf.NAME); + stringBuffer.append(" relation (i.e. "); + stringBuffer.append(consistsOfType); + stringBuffer.append(" with UUID "); + stringBuffer.append(consistsOfUUID.toString()); + stringBuffer.append(") without indicating the target "); + stringBuffer.append(Facet.NAME); + stringBuffer.append(" (i.e. "); + stringBuffer.append(facetType); + stringBuffer.append(" with UUID "); + stringBuffer.append(facetUUID.toString()); + stringBuffer.append(")."); + return stringBuffer.toString(); + } + @Override public void sanityCheck(Map expectedInstances) throws SchemaViolationException, ResourceRegistryException { JsonNode resourceInstance = createCompleteJsonNode(); @@ -519,7 +568,6 @@ public class ResourceManagement extends EntityManagement { throw new SchemaViolationException(stringBuffer.toString()); } } - - + */ } diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/relations/ConsistsOfManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/relations/ConsistsOfManagement.java index fdc0ed3..52061eb 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/relations/ConsistsOfManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/relations/ConsistsOfManagement.java @@ -1,9 +1,5 @@ package org.gcube.informationsystem.resourceregistry.instances.model.relations; -import java.util.Map; -import java.util.UUID; - -import org.gcube.com.fasterxml.jackson.databind.JsonNode; import org.gcube.informationsystem.base.reference.AccessType; import org.gcube.informationsystem.model.impl.properties.PropagationConstraintImpl; import org.gcube.informationsystem.model.reference.entities.Facet; @@ -60,7 +56,7 @@ public class ConsistsOfManagement extends RelationManagement { } @Override - public void sanityCheck(Map expectedInstances) throws SchemaViolationException, ResourceRegistryException { + public void sanityCheck() throws SchemaViolationException, ResourceRegistryException { // TODO Auto-generated method stub } diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/relations/IsRelatedToManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/relations/IsRelatedToManagement.java index 16ebe72..769c457 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/relations/IsRelatedToManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/relations/IsRelatedToManagement.java @@ -1,9 +1,5 @@ package org.gcube.informationsystem.resourceregistry.instances.model.relations; -import java.util.Map; -import java.util.UUID; - -import org.gcube.com.fasterxml.jackson.databind.JsonNode; import org.gcube.informationsystem.base.reference.AccessType; import org.gcube.informationsystem.model.impl.properties.PropagationConstraintImpl; import org.gcube.informationsystem.model.reference.entities.Resource; @@ -60,7 +56,7 @@ public class IsRelatedToManagement extends RelationManagement expectedInstances) throws SchemaViolationException, ResourceRegistryException { + public void sanityCheck() throws SchemaViolationException, ResourceRegistryException { // TODO Auto-generated method stub } diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/types/entities/EntityTypeDefinitionManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/types/entities/EntityTypeDefinitionManagement.java index 98ae3c7..cd9e162 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/types/entities/EntityTypeDefinitionManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/types/entities/EntityTypeDefinitionManagement.java @@ -1,8 +1,6 @@ package org.gcube.informationsystem.resourceregistry.types.entities; import java.util.HashMap; -import java.util.Map; -import java.util.UUID; import org.gcube.com.fasterxml.jackson.databind.JsonNode; import org.gcube.informationsystem.base.reference.AccessType; @@ -32,33 +30,33 @@ import com.orientechnologies.orient.core.sql.executor.OResultSet; * @author Luca Frosini (ISTI - CNR) */ public abstract class EntityTypeDefinitionManagement extends EntityElementManagement { - + private static Logger logger = LoggerFactory.getLogger(EntityTypeDefinitionManagement.class); - + protected String name; - + protected EntityTypeDefinitionManagement(Class clz) { super(AccessType.ENTITY_TYPE); this.typeName = TypeMapper.getType(clz); } - + @Override protected SecurityContext getWorkingContext() throws ResourceRegistryException { - if(workingContext == null) { + if (workingContext == null) { workingContext = ContextUtility.getInstance() .getSecurityContextByUUID(DatabaseEnvironment.SCHEMA_SECURITY_CONTEXT_UUID); } return workingContext; } - + public void setName(String name) { this.name = name; } - + public String getName() { - if(name == null) { - if(element == null) { - if(jsonNode != null) { + if (name == null) { + if (element == null) { + if (jsonNode != null) { name = jsonNode.get(EntityType.NAME_PROPERTY).asText(); } } else { @@ -67,136 +65,135 @@ public abstract class EntityTypeDefinitionManagement exten } return name; } - + @Override protected JsonNode createCompleteJsonNode() throws ResourceRegistryException { return serializeSelfAsJsonNode(); } - + @Override protected OVertex reallyCreate() throws AlreadyPresentException, ResourceRegistryException { logger.debug("Going to create {} for {}", this.typeName, getName()); return createVertex(); } - + @Override protected OVertex reallyUpdate() throws NotFoundException, ResourceRegistryException { logger.debug("Going to update {} for {}", this.typeName, getName()); OVertex entityTypeDefinition = getElement(); - entityTypeDefinition = (OVertex) updateProperties(oClass, entityTypeDefinition, jsonNode, - ignoreKeys, ignoreStartWithKeys); + entityTypeDefinition = (OVertex) updateProperties(oClass, entityTypeDefinition, jsonNode, ignoreKeys, + ignoreStartWithKeys); return entityTypeDefinition; } - + @Override protected boolean reallyDelete() throws NotFoundException, ResourceRegistryException { logger.debug("Going to remove {} for {}", this.typeName, getName()); getElement().delete(); return true; } - + @Override public OVertex getElement() throws NotFoundException, ResourceRegistryException { - if(element == null) { + if (element == null) { try { element = retrieveElement(); - } catch(NotFoundException e) { + } catch (NotFoundException e) { throw e; - } catch(ResourceRegistryException e) { + } catch (ResourceRegistryException e) { throw e; - } catch(Exception e) { + } catch (Exception e) { throw new ResourceRegistryException(e); } - + } else { - if(reload) { + if (reload) { element.reload(); } } return element; } - + @Override public OVertex retrieveElement() throws NotFoundException, ResourceRegistryException { try { - if(getName() == null) { + if (getName() == null) { throw new NotFoundException("null name does not allow to retrieve the Element"); } - - String select = "SELECT FROM " + typeName + " WHERE " + EntityType.NAME_PROPERTY + " = \"" - + getName() + "\""; - + + String select = "SELECT FROM " + typeName + " WHERE " + EntityType.NAME_PROPERTY + " = \"" + getName() + + "\""; + OResultSet resultSet = oDatabaseDocument.query(select, new HashMap<>()); - - if(resultSet == null || !resultSet.hasNext()) { + + if (resultSet == null || !resultSet.hasNext()) { String error = String.format("No %s with name %s was found", typeName, getName()); logger.info(error); throw new NotFoundException(error); } - + OResult oResult = resultSet.next(); OVertex element = (OVertex) ElementManagementUtility.getElementFromOptional(oResult.getElement()); - + logger.trace("{} with id {} is : {}", typeName, getName(), Utility.toJsonString(element, true)); - - if(resultSet.hasNext()) { + + if (resultSet.hasNext()) { throw new ResourceRegistryException("Found more than one " + typeName + " with name " + getName() + ". This is a fatal error please contact Admnistrator"); } - + return element; - } catch(NotFoundException e) { + } catch (NotFoundException e) { throw getSpecificElementNotFoundException(e); - } catch(ResourceRegistryException e) { + } catch (ResourceRegistryException e) { throw e; - } catch(Exception e) { + } catch (Exception e) { throw new ResourceRegistryException(e); } } - + @Override protected OVertex createVertex() throws EntityAlreadyPresentException, ResourceRegistryException { - + logger.trace("Going to create {} for {} ({}) using {}", OVertex.class.getSimpleName(), accessType.getName(), typeName, jsonNode); - + try { - + this.element = oDatabaseDocument.newVertex(typeName); - + updateProperties(oClass, element, jsonNode, ignoreKeys, ignoreStartWithKeys); - + logger.info("Created {} is {}", OVertex.class.getSimpleName(), Utility.toJsonString(element, true)); - + return element; - } catch(ResourceRegistryException e) { + } catch (ResourceRegistryException e) { throw e; - } catch(Exception e) { + } catch (Exception e) { logger.trace("Error while creating {} for {} ({}) using {}", OVertex.class.getSimpleName(), accessType.getName(), typeName, jsonNode, e); throw new ResourceRegistryException("Error Creating " + typeName + " with " + jsonNode, e.getCause()); } } - + @Override public String reallyGetAll(boolean polymorphic) throws ResourceRegistryException { throw new UnsupportedOperationException(); } - + @Override protected NotFoundException getSpecificElementNotFoundException(NotFoundException e) { return new SchemaNotFoundException(e.getMessage(), e.getCause()); } - + @Override protected AlreadyPresentException getSpecificERAlreadyPresentException(String message) { return new SchemaAlreadyPresentException(message); } - + @Override - public void sanityCheck(Map expectedInstances) - throws SchemaViolationException, ResourceRegistryException { + public void sanityCheck() throws SchemaViolationException, ResourceRegistryException { // Nothing to do } - + } diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/types/entities/ResourceTypeDefinitionManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/types/entities/ResourceTypeDefinitionManagement.java index 36589d4..46c202a 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/types/entities/ResourceTypeDefinitionManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/types/entities/ResourceTypeDefinitionManagement.java @@ -1,9 +1,5 @@ package org.gcube.informationsystem.resourceregistry.types.entities; -import java.util.Map; -import java.util.UUID; - -import org.gcube.com.fasterxml.jackson.databind.JsonNode; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaViolationException; import org.gcube.informationsystem.types.reference.entities.ResourceType; @@ -18,8 +14,7 @@ public class ResourceTypeDefinitionManagement extends EntityTypeDefinitionManage } @Override - public void sanityCheck(Map expectedInstances) - throws SchemaViolationException, ResourceRegistryException { + public void sanityCheck() throws SchemaViolationException, ResourceRegistryException { // TODO // We should check that a constraint defined here is not in contrast with the constraint of supertypes } diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/types/properties/PropertyTypeDefinitionManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/types/properties/PropertyTypeDefinitionManagement.java index 0455bb1..709cbac 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/types/properties/PropertyTypeDefinitionManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/types/properties/PropertyTypeDefinitionManagement.java @@ -1,8 +1,6 @@ package org.gcube.informationsystem.resourceregistry.types.properties; import java.util.HashMap; -import java.util.Map; -import java.util.UUID; import org.gcube.com.fasterxml.jackson.databind.JsonNode; import org.gcube.informationsystem.base.reference.AccessType; @@ -195,8 +193,8 @@ public class PropertyTypeDefinitionManagement extends ElementManagement expectedInstances) - throws SchemaViolationException, ResourceRegistryException { + public void sanityCheck() throws SchemaViolationException, ResourceRegistryException { // Nothing to do } + } diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/types/relations/ConsistsOfTypeDefinitionManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/types/relations/ConsistsOfTypeDefinitionManagement.java index 26ab0e4..d305adc 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/types/relations/ConsistsOfTypeDefinitionManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/types/relations/ConsistsOfTypeDefinitionManagement.java @@ -1,6 +1,7 @@ package org.gcube.informationsystem.resourceregistry.types.relations; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; +import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaViolationException; import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityContext; import org.gcube.informationsystem.resourceregistry.types.entities.FacetTypeDefinitionManagement; import org.gcube.informationsystem.types.reference.entities.FacetType; @@ -33,4 +34,9 @@ public class ConsistsOfTypeDefinitionManagement return ftdm; } + @Override + public void sanityCheck() throws SchemaViolationException, ResourceRegistryException { + // Nothing to do + } + } \ No newline at end of file diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/types/relations/IsRelatedToTypeDefinitionManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/types/relations/IsRelatedToTypeDefinitionManagement.java index 90e9cb7..aedb761 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/types/relations/IsRelatedToTypeDefinitionManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/types/relations/IsRelatedToTypeDefinitionManagement.java @@ -1,6 +1,7 @@ package org.gcube.informationsystem.resourceregistry.types.relations; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; +import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaViolationException; import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityContext; import org.gcube.informationsystem.resourceregistry.types.entities.ResourceTypeDefinitionManagement; import org.gcube.informationsystem.types.reference.entities.ResourceType; @@ -33,4 +34,8 @@ public class IsRelatedToTypeDefinitionManagement return rtdm; } + @Override + public void sanityCheck() throws SchemaViolationException, ResourceRegistryException { + // Nothing to do + } } diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/types/relations/RelationTypeDefinitionManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/types/relations/RelationTypeDefinitionManagement.java index 51d9175..6468c3f 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/types/relations/RelationTypeDefinitionManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/types/relations/RelationTypeDefinitionManagement.java @@ -1,10 +1,7 @@ package org.gcube.informationsystem.resourceregistry.types.relations; import java.util.HashMap; -import java.util.Map; -import java.util.UUID; -import org.gcube.com.fasterxml.jackson.databind.JsonNode; import org.gcube.informationsystem.base.reference.AccessType; import org.gcube.informationsystem.model.reference.relations.Relation; import org.gcube.informationsystem.resourceregistry.api.exceptions.AlreadyPresentException; @@ -13,7 +10,6 @@ import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegis import org.gcube.informationsystem.resourceregistry.api.exceptions.relation.RelationNotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaAlreadyPresentException; import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaNotFoundException; -import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaViolationException; import org.gcube.informationsystem.resourceregistry.contexts.ContextUtility; import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityContext; import org.gcube.informationsystem.resourceregistry.dbinitialization.DatabaseEnvironment; @@ -37,37 +33,38 @@ import com.orientechnologies.orient.core.sql.executor.OResultSet; */ public abstract class RelationTypeDefinitionManagement, TT extends EntityType> extends RelationElementManagement { - + protected String name; - + public RelationTypeDefinitionManagement(Class clz) { super(AccessType.RELATION_TYPE, ResourceType.class, clz); this.typeName = RelationType.NAME; } - - public RelationTypeDefinitionManagement(SecurityContext securityContext, ODatabaseDocument oDatabaseDocument, Class clz) throws ResourceRegistryException { + + public RelationTypeDefinitionManagement(SecurityContext securityContext, ODatabaseDocument oDatabaseDocument, + Class clz) throws ResourceRegistryException { this(clz); this.oDatabaseDocument = oDatabaseDocument; setWorkingContext(securityContext); } - + @Override protected SecurityContext getWorkingContext() throws ResourceRegistryException { - if(workingContext == null) { + if (workingContext == null) { this.workingContext = ContextUtility.getInstance() .getSecurityContextByUUID(DatabaseEnvironment.SCHEMA_SECURITY_CONTEXT_UUID); } return workingContext; } - + public void setName(String name) { this.name = name; } - + public String getName() { - if(name == null) { - if(element == null) { - if(jsonNode != null) { + if (name == null) { + if (element == null) { + if (jsonNode != null) { name = jsonNode.get(RelationType.NAME_PROPERTY).asText(); } } else { @@ -76,135 +73,134 @@ public abstract class RelationTypeDefinitionManagement {}", typeName, source.toString(), - target.toString()); - + + logger.trace("Creating {} beetween {} -> {}", typeName, source.toString(), target.toString()); + element = oDatabaseDocument.newEdge(source, target, typeName); - + updateProperties(oClass, element, jsonNode, ignoreKeys, ignoreStartWithKeys); - + return element; } - + @Override protected OEdge reallyUpdate() throws NotFoundException, ResourceRegistryException { logger.debug("Going to update {} for {}", RelationType.NAME, getName()); OEdge relationTypeDefinition = getElement(); - relationTypeDefinition = (OEdge) updateProperties(oClass, relationTypeDefinition, jsonNode, - ignoreKeys, ignoreStartWithKeys); + relationTypeDefinition = (OEdge) updateProperties(oClass, relationTypeDefinition, jsonNode, ignoreKeys, + ignoreStartWithKeys); return relationTypeDefinition; - + } - + @Override protected boolean reallyDelete() throws RelationNotFoundException, ResourceRegistryException { logger.debug("Going to remove {} for {}", RelationType.NAME, getName()); getElement().delete(); return true; } - + @Override public OEdge getElement() throws NotFoundException, ResourceRegistryException { - if(element == null) { + if (element == null) { try { element = retrieveElement(); - } catch(NotFoundException e) { + } catch (NotFoundException e) { throw e; - } catch(ResourceRegistryException e) { + } catch (ResourceRegistryException e) { throw e; - } catch(Exception e) { + } catch (Exception e) { throw new ResourceRegistryException(e); } - + } else { - if(reload) { + if (reload) { element.reload(); } } return element; } - + @Override public OEdge retrieveElement() throws NotFoundException, ResourceRegistryException { try { - if(getName() == null) { + if (getName() == null) { throw new NotFoundException("null name does not allow to retrieve the Element"); } - - String select = "SELECT FROM " + typeName + " WHERE " + RelationType.NAME_PROPERTY + " = \"" - + getName() + "\""; - + + String select = "SELECT FROM " + typeName + " WHERE " + RelationType.NAME_PROPERTY + " = \"" + getName() + + "\""; + OResultSet resultSet = oDatabaseDocument.query(select, new HashMap<>()); - - if(resultSet == null || !resultSet.hasNext()) { + + if (resultSet == null || !resultSet.hasNext()) { String error = String.format("No %s with name %s was found", typeName, getName()); logger.info(error); throw new NotFoundException(error); } - + OResult oResult = resultSet.next(); OEdge element = (OEdge) ElementManagementUtility.getElementFromOptional(oResult.getElement()); - + logger.trace("{} with id {} is : {}", typeName, getName(), Utility.toJsonString(element, true)); - - if(resultSet.hasNext()) { + + if (resultSet.hasNext()) { throw new ResourceRegistryException("Found more than one " + typeName + " with name " + getName() + ". This is a fatal error please contact Admnistrator"); } - + return element; - } catch(NotFoundException e) { + } catch (NotFoundException e) { throw getSpecificElementNotFoundException(e); - } catch(ResourceRegistryException e) { + } catch (ResourceRegistryException e) { throw e; - } catch(Exception e) { + } catch (Exception e) { throw new ResourceRegistryException(e); } } - + @Override public String reallyGetAll(boolean polymorphic) throws ResourceRegistryException { throw new UnsupportedOperationException(); } - + @Override protected NotFoundException getSpecificElementNotFoundException(NotFoundException e) { return new SchemaNotFoundException(e.getMessage(), e.getCause()); } - + @Override protected AlreadyPresentException getSpecificERAlreadyPresentException(String message) { return new SchemaAlreadyPresentException(message); } - + @Override protected ResourceTypeDefinitionManagement newSourceEntityManagement() throws ResourceRegistryException { ResourceTypeDefinitionManagement rtdm = new ResourceTypeDefinitionManagement(); @@ -212,11 +208,5 @@ public abstract class RelationTypeDefinitionManagement expectedInstances) - throws SchemaViolationException, ResourceRegistryException { - // Nothing to do - } - + }