From c21fa2aa1a4a65990cc313f6eff245b101f8e5c6 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Mon, 8 Feb 2021 16:26:58 +0100 Subject: [PATCH] Refactoring code --- .../contexts/entities/ContextManagement.java | 3 +- .../instances/base/ElementManagement.java | 19 ++- .../base/ElementManagementUtility.java | 65 +------- .../entities/EntityElementManagement.java | 2 +- .../properties/PropertyElementManagement.java | 9 +- .../relations/RelationElementManagement.java | 4 +- .../model/entities/EntityManagement.java | 2 +- .../model/entities/FacetManagement.java | 3 +- .../model/entities/ResourceManagement.java | 42 ++++- .../model/relations/RelationManagement.java | 2 +- .../types/SchemaManagement.java | 2 +- .../resourceregistry/types/TypesCache.java | 143 ++++++++++++++++++ .../EntityTypeDefinitionManagement.java | 4 +- .../PropertyTypeDefinitionManagement.java | 4 +- .../RelationTypeDefinitionManagement.java | 4 +- .../instances/multicontext/BasicTest.java | 4 +- 16 files changed, 217 insertions(+), 95 deletions(-) create mode 100644 src/main/java/org/gcube/informationsystem/resourceregistry/types/TypesCache.java 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 2f75568..a5056fa 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 @@ -30,7 +30,6 @@ import org.gcube.informationsystem.resourceregistry.contexts.ContextUtility; import org.gcube.informationsystem.resourceregistry.contexts.relations.IsParentOfManagement; import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityContext; import org.gcube.informationsystem.resourceregistry.dbinitialization.DatabaseEnvironment; -import org.gcube.informationsystem.resourceregistry.instances.base.ElementManagement; import org.gcube.informationsystem.resourceregistry.instances.base.entities.EntityElementManagement; import org.gcube.informationsystem.resourceregistry.utils.Utility; import org.gcube.informationsystem.utils.ElementMapper; @@ -337,7 +336,7 @@ public class ContextManagement extends EntityElementManagement { move(newParentContextManagement, false); } - element = (OVertex) ElementManagement.updateProperties(oClass, getElement(), jsonNode, ignoreKeys, + element = (OVertex) updateProperties(oClass, getElement(), jsonNode, ignoreKeys, ignoreStartWithKeys); ContextCache.getInstance().cleanCache(); 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 c8cc926..5108f28 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 @@ -41,6 +41,7 @@ import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityCo import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityContext.PermissionMode; import org.gcube.informationsystem.resourceregistry.dbinitialization.DatabaseEnvironment; import org.gcube.informationsystem.resourceregistry.instances.base.properties.PropertyElementManagement; +import org.gcube.informationsystem.resourceregistry.types.TypesCache; import org.gcube.informationsystem.resourceregistry.utils.HeaderOrient; import org.gcube.informationsystem.resourceregistry.utils.HeaderUtility; import org.gcube.informationsystem.resourceregistry.utils.Utility; @@ -63,7 +64,7 @@ public abstract class ElementManagement { protected Logger logger = LoggerFactory.getLogger(this.getClass()); - private static Logger staticLogger = LoggerFactory.getLogger(ElementManagement.class); + // private static Logger staticLogger = LoggerFactory.getLogger(ElementManagement.class); public final static String DELETED = "deleted"; @@ -185,7 +186,8 @@ public abstract class ElementManagement { if(element != null) { oClass = getOClass(element); } else { - oClass = ElementManagementUtility.getTypeSchema(elementType, accessType); + oClass = TypesCache.getTypeSchema(elementType); + TypesCache.checkAccessType(oClass, elementType, accessType); } } return oClass; @@ -535,7 +537,7 @@ public abstract class ElementManagement { } } - public JsonNode read() throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException { + public String read() throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException { ODatabaseDocument current = ContextUtility.getCurrentODatabaseDocumentFromThreadLocal(); try { @@ -543,7 +545,8 @@ public abstract class ElementManagement { getElement(); - return serializeAsJson(); + jsonNode = serializeAsJson(); + return jsonNode.toString(); } catch(ResourceRegistryException e) { logger.error("Unable to read {} with UUID {}", accessType.getName(), uuid); throw e; @@ -767,7 +770,7 @@ public abstract class ElementManagement { return null; } - public static Map getPropertyMap(JsonNode jsonNode, Set ignoreKeys, + public Map getPropertyMap(JsonNode jsonNode, Set ignoreKeys, Set ignoreStartWith) throws JsonProcessingException, IOException { Map map = new HashMap<>(); @@ -805,7 +808,7 @@ public abstract class ElementManagement { map.put(key, object); } } catch(ResourceRegistryException e) { - staticLogger.warn("An invalidy property has been provided. It will be ignored."); + logger.warn("An invalidy property has been provided. It will be ignored."); } } @@ -813,7 +816,7 @@ public abstract class ElementManagement { return map; } - public static OElement updateProperties(OClass oClass, OElement element, JsonNode jsonNode, Set ignoreKeys, + public OElement updateProperties(OClass oClass, OElement element, JsonNode jsonNode, Set ignoreKeys, Set ignoreStartWithKeys) throws ResourceRegistryException { Set oldKeys = element.getPropertyNames(); @@ -859,7 +862,7 @@ public abstract class ElementManagement { } catch(Exception e) { String error = String.format("Error while setting property %s : %s (%s)", key, properties.get(key).toString(), e.getMessage()); - staticLogger.error(error); + logger.error(error); throw new ResourceRegistryException(error, e); } } diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/base/ElementManagementUtility.java b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/base/ElementManagementUtility.java index 15b61f4..e4ab076 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/base/ElementManagementUtility.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/base/ElementManagementUtility.java @@ -12,26 +12,20 @@ import org.gcube.informationsystem.model.reference.relations.IsRelatedTo; import org.gcube.informationsystem.model.reference.relations.Relation; import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; -import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaException; -import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaNotFoundException; -import org.gcube.informationsystem.resourceregistry.contexts.ContextUtility; -import org.gcube.informationsystem.resourceregistry.contexts.security.AdminSecurityContext; import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityContext; -import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityContext.PermissionMode; import org.gcube.informationsystem.resourceregistry.instances.model.entities.EntityManagement; import org.gcube.informationsystem.resourceregistry.instances.model.entities.FacetManagement; import org.gcube.informationsystem.resourceregistry.instances.model.entities.ResourceManagement; import org.gcube.informationsystem.resourceregistry.instances.model.relations.ConsistsOfManagement; import org.gcube.informationsystem.resourceregistry.instances.model.relations.IsRelatedToManagement; import org.gcube.informationsystem.resourceregistry.instances.model.relations.RelationManagement; +import org.gcube.informationsystem.resourceregistry.types.TypesCache; import org.gcube.informationsystem.resourceregistry.utils.Utility; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.orientechnologies.orient.core.db.document.ODatabaseDocument; -import com.orientechnologies.orient.core.metadata.OMetadata; import com.orientechnologies.orient.core.metadata.schema.OClass; -import com.orientechnologies.orient.core.metadata.schema.OSchema; import com.orientechnologies.orient.core.record.OEdge; import com.orientechnologies.orient.core.record.OElement; import com.orientechnologies.orient.core.record.OVertex; @@ -45,7 +39,7 @@ public class ElementManagementUtility { public static AccessType getBaseAccessType(String type) throws ResourceRegistryException { - OClass oClass = ElementManagementUtility.getTypeSchema(type, null); + OClass oClass = TypesCache.getTypeSchema(type); if(oClass.isSubClassOf(Resource.NAME)) { return AccessType.RESOURCE; @@ -66,7 +60,7 @@ public class ElementManagementUtility { @SuppressWarnings("rawtypes") public static ElementManagement getERManagement(SecurityContext workingContext, ODatabaseDocument oDatabaseDocument, String type) throws ResourceRegistryException { - OClass oClass = ElementManagementUtility.getTypeSchema(oDatabaseDocument, type, null); + OClass oClass = TypesCache.getTypeSchema(oDatabaseDocument, type); ElementManagement erManagement = null; if(oClass.isSubClassOf(Resource.NAME)) { @@ -90,7 +84,7 @@ public class ElementManagementUtility { @SuppressWarnings("rawtypes") public static ElementManagement getERManagement(String type) throws ResourceRegistryException { - OClass oClass = ElementManagementUtility.getTypeSchema(type, null); + OClass oClass = TypesCache.getTypeSchema(type); ElementManagement erManagement = null; if(oClass.isSubClassOf(Resource.NAME)) { @@ -228,55 +222,4 @@ public class ElementManagementUtility { return relationManagement; } - public static OClass getTypeSchema(ODatabaseDocument oDatabaseDocument, String type, AccessType accessType) - throws SchemaException, SchemaNotFoundException { - OMetadata oMetadata = oDatabaseDocument.getMetadata(); - OSchema oSchema = oMetadata.getSchema(); - return getTypeSchema(oSchema, type, accessType); - } - - public static OClass getTypeSchema(OSchema oSchema, String type, AccessType accessType) - throws SchemaException, SchemaNotFoundException { - try { - OClass oClass = oSchema.getClass(type); - if(oClass == null) { - throw new SchemaNotFoundException(type + " was not registered"); - } - if(accessType != null && type.compareTo(accessType.getName()) != 0) { - if(!oClass.isSubClassOf(accessType.getName())) { - throw new SchemaException(type + " is not a " + accessType.getName()); - } - } - return oClass; - } catch(SchemaNotFoundException snfe) { - throw snfe; - } catch(Exception e) { - throw new SchemaException(e.getMessage()); - } - } - - public static OClass getTypeSchema(String type, AccessType accessType) - throws SchemaException, ResourceRegistryException { - ODatabaseDocument current = ContextUtility.getCurrentODatabaseDocumentFromThreadLocal(); - ODatabaseDocument oDatabaseDocument = null; - try { - logger.debug("Getting {} Type {} schema", accessType != null ? accessType.getName() : "", type); - AdminSecurityContext adminSecurityContext = ContextUtility.getAdminSecurityContext(); - oDatabaseDocument = adminSecurityContext.getDatabaseDocument(PermissionMode.READER); - return getTypeSchema(oDatabaseDocument, type, accessType); - } catch(ResourceRegistryException e) { - throw e; - } catch(Exception e) { - throw new ResourceRegistryException(e); - } finally { - if(oDatabaseDocument != null) { - oDatabaseDocument.close(); - } - - if(current!=null) { - current.activateOnCurrentThread(); - } - } - } - } diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/base/entities/EntityElementManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/base/entities/EntityElementManagement.java index 8eac536..2cce5c9 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/base/entities/EntityElementManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/base/entities/EntityElementManagement.java @@ -153,7 +153,7 @@ public abstract class EntityElementManagement extends E if(accessType == AccessType.RESOURCE) { // Facet and relation are created in calling method } else { - ElementManagement.updateProperties(oClass, element, jsonNode, ignoreKeys, ignoreStartWithKeys); + updateProperties(oClass, element, jsonNode, ignoreKeys, ignoreStartWithKeys); } logger.info("Created {} is {}", OVertex.class.getSimpleName(), diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/base/properties/PropertyElementManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/base/properties/PropertyElementManagement.java index 5cf7b1d..4328a1e 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/base/properties/PropertyElementManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/base/properties/PropertyElementManagement.java @@ -17,7 +17,7 @@ import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegis import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaNotFoundException; import org.gcube.informationsystem.resourceregistry.dbinitialization.DatabaseEnvironment; import org.gcube.informationsystem.resourceregistry.instances.base.ElementManagement; -import org.gcube.informationsystem.resourceregistry.instances.base.ElementManagementUtility; +import org.gcube.informationsystem.resourceregistry.types.TypesCache; import org.gcube.informationsystem.resourceregistry.utils.EncryptedOrient; import org.gcube.informationsystem.resourceregistry.utils.HeaderUtility; import org.slf4j.Logger; @@ -53,7 +53,8 @@ public class PropertyElementManagement { OClass oClass = null; try { - oClass = ElementManagementUtility.getTypeSchema(type, AccessType.PROPERTY_ELEMENT); + oClass = TypesCache.getTypeSchema(type); + TypesCache.checkAccessType(oClass, type, AccessType.PROPERTY_ELEMENT); } catch(SchemaNotFoundException e) { throw e; } @@ -122,8 +123,8 @@ public class PropertyElementManagement { return jsonNode; } - OClass oClass = ElementManagementUtility.getTypeSchema(type, AccessType.PROPERTY_ELEMENT); - + OClass oClass = TypesCache.getTypeSchema(type); + TypesCache.checkAccessType(oClass, type, AccessType.PROPERTY_ELEMENT); /* * In case it is an Encrypted type the value is encrypted with the DB Key * Resource Registry must decrypt the value with the DB Key and Encrypt it with Context key. diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/base/relations/RelationElementManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/base/relations/RelationElementManagement.java index 36f5905..7145dec 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/base/relations/RelationElementManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/base/relations/RelationElementManagement.java @@ -167,7 +167,7 @@ public abstract class RelationElementManagement extends EntityElementMa if(accessType == AccessType.RESOURCE) { // Facet and relation are created in calling method } else { - ElementManagement.updateProperties(oClass, element, jsonNode, ignoreKeys, ignoreStartWithKeys); + updateProperties(oClass, element, jsonNode, ignoreKeys, ignoreStartWithKeys); } logger.info("Created {} is {}", OVertex.class.getSimpleName(), 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 66e6917..ee0144e 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 @@ -12,7 +12,6 @@ import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.facet. import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.facet.FacetAvailableInAnotherContextException; import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.facet.FacetNotFoundException; import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityContext; -import org.gcube.informationsystem.resourceregistry.instances.base.ElementManagement; import com.orientechnologies.orient.core.db.document.ODatabaseDocument; import com.orientechnologies.orient.core.record.OVertex; @@ -63,7 +62,7 @@ public class FacetManagement extends EntityManagement { @Override protected OVertex reallyUpdate() throws FacetNotFoundException, ResourceRegistryException { OVertex facet = getElement(); - facet = (OVertex) ElementManagement.updateProperties(oClass, facet, jsonNode, ignoreKeys, ignoreStartWithKeys); + facet = (OVertex) updateProperties(oClass, facet, jsonNode, ignoreKeys, ignoreStartWithKeys); return facet; } 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 edb5922..47022e4 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 @@ -1,7 +1,9 @@ package org.gcube.informationsystem.resourceregistry.instances.model.entities; +import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; +import java.util.List; import java.util.Map; import java.util.UUID; @@ -28,7 +30,11 @@ import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityCo import org.gcube.informationsystem.resourceregistry.instances.model.relations.ConsistsOfManagement; import org.gcube.informationsystem.resourceregistry.instances.model.relations.IsRelatedToManagement; import org.gcube.informationsystem.resourceregistry.instances.model.relations.RelationManagement; +import org.gcube.informationsystem.resourceregistry.types.entities.ResourceTypeDefinitionManagement; import org.gcube.informationsystem.resourceregistry.utils.Utility; +import org.gcube.informationsystem.types.TypeMapper; +import org.gcube.informationsystem.types.reference.entities.ResourceType; +import org.gcube.informationsystem.types.reference.properties.LinkedEntity; import com.orientechnologies.orient.core.db.document.ODatabaseDocument; import com.orientechnologies.orient.core.metadata.schema.OClass; @@ -296,10 +302,38 @@ public class ResourceManagement extends EntityManagement { } } - // TODO Create something like SchemaDefinitionNotRespected o ConstaintNotRespected Exception + @Override public void sanityCheck(Map expectedInstances) throws ResourceRegistryException { - // TODO check that at least a facet and its consistOf will be added to the context - JsonNode jsonNode = read(); + /* + * Actually this function only check the Resource constraint, i.e., resource has at least a Facet + * in any context. + * + * We need to load the Resource definition and validate it. + * This task is not very easy because we cannot check just the type but + * me must take in account that a specialisation instance is valid. + * + */ + read(); + + ResourceTypeDefinitionManagement resourceTypeDefinitionManagement = new ResourceTypeDefinitionManagement(); + resourceTypeDefinitionManagement.setName(elementType); + String stringType = resourceTypeDefinitionManagement.read().toString(); + ResourceType resourceType = null; + try { + resourceType = (ResourceType) TypeMapper.deserializeTypeDefinition(stringType); + }catch (Exception e) { + throw new ResourceRegistryException(e); + } + + // TODO Create something like SchemaDefinitionNotRespected o ConstaintNotRespected Exception + + List constraint = new ArrayList<>(); + List linkedEntities = resourceType.getFacets(); + for(LinkedEntity linkedEntity : linkedEntities) { + if(linkedEntity.getMin()>0 || linkedEntity.getMax()>0) { + constraint.add(linkedEntity); + } + } boolean found = false; ArrayNode consistsOfArrayNode = (ArrayNode) jsonNode.get(Resource.CONSISTS_OF_PROPERTY); @@ -307,7 +341,7 @@ public class ResourceManagement extends EntityManagement { String consistsOfUUIDString = consistsOfJsonNode.get(IdentifiableElement.HEADER_PROPERTY).get(Header.UUID_PROPERTY).asText(); UUID consistsOfUUID = UUID.fromString(consistsOfUUIDString); if(expectedInstances.containsKey(consistsOfUUID)) { - // we need to check that also the facets are + // we need to check that also the facets is present JsonNode facetJsonNode = consistsOfJsonNode.get(Relation.TARGET_PROPERTY); String facetUUIDString = facetJsonNode.get(IdentifiableElement.HEADER_PROPERTY).get(Header.UUID_PROPERTY).asText(); UUID facetUUID = UUID.fromString(facetUUIDString); diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/relations/RelationManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/relations/RelationManagement.java index 4eb11ac..ff7e2b8 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/relations/RelationManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/relations/RelationManagement.java @@ -346,7 +346,7 @@ public abstract class RelationManagement getTypeManagement(OClass oClass) { + public static ElementManagement getTypeManagement(OClass oClass) { ElementManagement erManagement = null; if(oClass.isSubClassOf(Property.NAME)) { erManagement = new PropertyTypeDefinitionManagement(); diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/types/TypesCache.java b/src/main/java/org/gcube/informationsystem/resourceregistry/types/TypesCache.java new file mode 100644 index 0000000..0daaa43 --- /dev/null +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/types/TypesCache.java @@ -0,0 +1,143 @@ +package org.gcube.informationsystem.resourceregistry.types; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.gcube.informationsystem.base.reference.AccessType; +import org.gcube.informationsystem.model.reference.entities.Facet; +import org.gcube.informationsystem.model.reference.entities.Resource; +import org.gcube.informationsystem.model.reference.properties.Property; +import org.gcube.informationsystem.model.reference.relations.ConsistsOf; +import org.gcube.informationsystem.model.reference.relations.IsRelatedTo; +import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; +import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaException; +import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaNotFoundException; +import org.gcube.informationsystem.resourceregistry.contexts.ContextUtility; +import org.gcube.informationsystem.resourceregistry.contexts.security.AdminSecurityContext; +import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityContext.PermissionMode; +import org.gcube.informationsystem.types.reference.Type; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.orientechnologies.orient.core.db.document.ODatabaseDocument; +import com.orientechnologies.orient.core.metadata.OMetadata; +import com.orientechnologies.orient.core.metadata.schema.OClass; +import com.orientechnologies.orient.core.metadata.schema.OSchema; + +public class TypesCache { + + private static Logger logger = LoggerFactory.getLogger(TypesCache.class); + + protected static final Map oClasses; + protected static final Map types; + protected static final Map> superTypes; + protected static final Map> specilisationTypes; + + + static { + oClasses = new HashMap<>(); + types = new HashMap<>(); + superTypes = new HashMap<>(); + specilisationTypes = new HashMap<>(); + } + + + + public static AccessType getBaseAccessType(String type) throws ResourceRegistryException { + + OClass oClass = getTypeSchema(type); + + if(oClass.isSubClassOf(Resource.NAME)) { + return AccessType.RESOURCE; + } else if(oClass.isSubClassOf(Facet.NAME)) { + return AccessType.FACET; + } else if(oClass.isSubClassOf(ConsistsOf.NAME)) { + return AccessType.CONSISTS_OF; + } else if(oClass.isSubClassOf(IsRelatedTo.NAME)) { + return AccessType.IS_RELATED_TO; + } else if(oClass.isSubClassOf(Property.NAME)) { + return AccessType.PROPERTY; + } + + throw new ResourceRegistryException(type + "is not a base type"); + + } + + public static void checkAccessType(OClass oClass, String type, AccessType accessType) throws SchemaException { + if(accessType != null && type.compareTo(accessType.getName()) != 0) { + if(!oClass.isSubClassOf(accessType.getName())) { + throw new SchemaException(type + " is not a " + accessType.getName()); + } + } + } + + private static OClass getTypeSchema(OSchema oSchema, String type) + throws SchemaException, SchemaNotFoundException { + try { + OClass oClass; + synchronized (oClasses) { + oClass = oClasses.get(type); + if(oClass==null) { + oClass = oSchema.getClass(type); + if(oClass == null) { + throw new SchemaNotFoundException(type + " was not registered"); + } + oClasses.put(type, oClass); + } + } + return oClass; + } catch(SchemaNotFoundException snfe) { + throw snfe; + } catch(Exception e) { + throw new SchemaException(e.getMessage()); + } + } + + public static OClass getTypeSchema(ODatabaseDocument oDatabaseDocument, String type) + throws SchemaException, SchemaNotFoundException { + + synchronized (oClasses) { + OClass oClass = oClasses.get(type); + if(oClass!=null) { + return oClass; + } + } + + OMetadata oMetadata = oDatabaseDocument.getMetadata(); + OSchema oSchema = oMetadata.getSchema(); + return getTypeSchema(oSchema, type); + } + + public static OClass getTypeSchema(String type) + throws SchemaException, ResourceRegistryException { + + synchronized (oClasses) { + OClass oClass = oClasses.get(type); + if(oClass!=null) { + return oClass; + } + } + + ODatabaseDocument current = ContextUtility.getCurrentODatabaseDocumentFromThreadLocal(); + ODatabaseDocument oDatabaseDocument = null; + try { + logger.debug("GettingType {} schema", type); + AdminSecurityContext adminSecurityContext = ContextUtility.getAdminSecurityContext(); + oDatabaseDocument = adminSecurityContext.getDatabaseDocument(PermissionMode.READER); + return getTypeSchema(oDatabaseDocument, type); + } catch(ResourceRegistryException e) { + throw e; + } catch(Exception e) { + throw new ResourceRegistryException(e); + } finally { + if(oDatabaseDocument != null) { + oDatabaseDocument.close(); + } + + if(current!=null) { + current.activateOnCurrentThread(); + } + } + } +} 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 b3d9228..25a5532 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 @@ -93,7 +93,7 @@ public abstract class EntityTypeDefinitionManagement exten protected OVertex reallyUpdate() throws NotFoundException, ResourceRegistryException { logger.debug("Going to update {} for {}", this.elementType, getName()); OVertex entityTypeDefinition = getElement(); - entityTypeDefinition = (OVertex) ElementManagement.updateProperties(oClass, entityTypeDefinition, jsonNode, + entityTypeDefinition = (OVertex) updateProperties(oClass, entityTypeDefinition, jsonNode, ignoreKeys, ignoreStartWithKeys); return entityTypeDefinition; } @@ -174,7 +174,7 @@ public abstract class EntityTypeDefinitionManagement exten this.element = oDatabaseDocument.newVertex(elementType); - ElementManagement.updateProperties(oClass, element, jsonNode, ignoreKeys, ignoreStartWithKeys); + updateProperties(oClass, element, jsonNode, ignoreKeys, ignoreStartWithKeys); logger.info("Created {} is {}", OVertex.class.getSimpleName(), Utility.toJsonString(element, true)); 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 928f48a..dfbf4a5 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 @@ -91,7 +91,7 @@ public class PropertyTypeDefinitionManagement extends ElementManagement