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 5108f28..df7dd28 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 @@ -177,6 +177,10 @@ public abstract class ElementManagement { } } + public void setoDatabaseDocument(ODatabaseDocument oDatabaseDocument) { + this.oDatabaseDocument = oDatabaseDocument; + } + public void setOClass(OClass oClass) { this.oClass = oClass; } @@ -186,8 +190,12 @@ public abstract class ElementManagement { if(element != null) { oClass = getOClass(element); } else { - oClass = TypesCache.getTypeSchema(elementType); - TypesCache.checkAccessType(oClass, elementType, accessType); + TypesCache typesCache = TypesCache.getInstance(); + oClass = TypesCache.getInstance().getTypeOClass(elementType); + AccessType gotAccessType = typesCache.getBaseAccessType(elementType); + if(accessType!=gotAccessType) { + throw new SchemaException(elementType + " is not a " + accessType.getName()); + } } } return oClass; @@ -454,7 +462,6 @@ public abstract class ElementManagement { try { oDatabaseDocument = getWorkingContext().getDatabaseDocument(PermissionMode.WRITER); oDatabaseDocument.begin(); - boolean update = false; try { getElement(); 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 e4ab076..03f5afc 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 @@ -6,7 +6,6 @@ import org.gcube.informationsystem.base.reference.AccessType; import org.gcube.informationsystem.model.reference.entities.Entity; 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.model.reference.relations.Relation; @@ -37,71 +36,34 @@ public class ElementManagementUtility { private static Logger logger = LoggerFactory.getLogger(ElementManagementUtility.class); - public static AccessType getBaseAccessType(String type) throws ResourceRegistryException { - - OClass oClass = TypesCache.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"); - - } - - @SuppressWarnings("rawtypes") - public static ElementManagement getERManagement(SecurityContext workingContext, ODatabaseDocument oDatabaseDocument, String type) throws ResourceRegistryException { - - OClass oClass = TypesCache.getTypeSchema(oDatabaseDocument, type); - ElementManagement erManagement = null; - - if(oClass.isSubClassOf(Resource.NAME)) { - erManagement = new ResourceManagement(workingContext, oDatabaseDocument); - } else if(oClass.isSubClassOf(Facet.NAME)) { - erManagement = new FacetManagement(workingContext, oDatabaseDocument); - } else if(oClass.isSubClassOf(ConsistsOf.NAME)) { - erManagement = new ConsistsOfManagement(workingContext, oDatabaseDocument); - } else if(oClass.isSubClassOf(IsRelatedTo.NAME)) { - erManagement = new IsRelatedToManagement(workingContext, oDatabaseDocument); - } - - if(erManagement == null) { - throw new ResourceRegistryException(String.format("%s is not querable", type.toString())); - } - - erManagement.setElementType(type); - return erManagement; - } - @SuppressWarnings("rawtypes") public static ElementManagement getERManagement(String type) throws ResourceRegistryException { - OClass oClass = TypesCache.getTypeSchema(type); + AccessType accessType = TypesCache.getInstance().getBaseAccessType(type); + ElementManagement erManagement = null; - if(oClass.isSubClassOf(Resource.NAME)) { - erManagement = new ResourceManagement(); - } else if(oClass.isSubClassOf(Facet.NAME)) { - erManagement = new FacetManagement(); - } else if(oClass.isSubClassOf(ConsistsOf.NAME)) { - erManagement = new ConsistsOfManagement(); - } else if(oClass.isSubClassOf(IsRelatedTo.NAME)) { - erManagement = new IsRelatedToManagement(); + switch (accessType) { + case RESOURCE: + erManagement = new ResourceManagement(); + break; + + case FACET: + erManagement = new FacetManagement(); + break; + + case IS_RELATED_TO: + erManagement = new IsRelatedToManagement(); + break; + + case CONSISTS_OF: + erManagement = new ConsistsOfManagement(); + + default: + throw new ResourceRegistryException(String.format("%s is not querable", type.toString())); } - - if(erManagement == null) { - throw new ResourceRegistryException(String.format("%s is not querable", type.toString())); - } - erManagement.setElementType(type); + return erManagement; } @@ -180,14 +142,16 @@ public class ElementManagementUtility { EntityManagement entityManagement = null; if(oClass.isSubClassOf(Resource.NAME)) { - entityManagement = new ResourceManagement(workingContext, oDatabaseDocument); + entityManagement = new ResourceManagement(); } else if(oClass.isSubClassOf(Facet.NAME)) { - entityManagement = new FacetManagement(workingContext, oDatabaseDocument); + entityManagement = new FacetManagement(); } else { String error = String.format("{%s is not a %s nor a %s. %s", vertex, Resource.NAME, Facet.NAME, Utility.SHOULD_NOT_OCCUR_ERROR_MESSAGE); throw new ResourceRegistryException(error); } + entityManagement.setoDatabaseDocument(oDatabaseDocument); + entityManagement.setWorkingContext(workingContext); entityManagement.setElement(vertex); return entityManagement; } @@ -210,14 +174,19 @@ public class ElementManagementUtility { RelationManagement relationManagement = null; if(oClass.isSubClassOf(ConsistsOf.NAME)) { - relationManagement = new ConsistsOfManagement(workingContext, oDatabaseDocument); + relationManagement = new ConsistsOfManagement(); } else if(oClass.isSubClassOf(IsRelatedTo.NAME)) { - relationManagement = new IsRelatedToManagement(workingContext, oDatabaseDocument); + relationManagement = new IsRelatedToManagement(); } else { String error = String.format("{%s is not a %s nor a %s. %s", edge, ConsistsOf.NAME, IsRelatedTo.NAME, Utility.SHOULD_NOT_OCCUR_ERROR_MESSAGE); throw new ResourceRegistryException(error); } + + + relationManagement.setoDatabaseDocument(oDatabaseDocument); + relationManagement.setWorkingContext(workingContext); + relationManagement.setElement(edge); return relationManagement; } 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 4328a1e..b9b79ec 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 @@ -14,6 +14,7 @@ import org.gcube.informationsystem.model.reference.properties.Encrypted; import org.gcube.informationsystem.model.reference.properties.Header; import org.gcube.informationsystem.model.reference.properties.Property; 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.dbinitialization.DatabaseEnvironment; import org.gcube.informationsystem.resourceregistry.instances.base.ElementManagement; @@ -53,8 +54,12 @@ public class PropertyElementManagement { OClass oClass = null; try { - oClass = TypesCache.getTypeSchema(type); - TypesCache.checkAccessType(oClass, type, AccessType.PROPERTY_ELEMENT); + TypesCache typesCache = TypesCache.getInstance(); + oClass = typesCache.getTypeOClass(type); + AccessType gotAccessType = typesCache.getBaseAccessType(type); + if(AccessType.PROPERTY_ELEMENT!=gotAccessType) { + throw new SchemaException(type + " is not a " + AccessType.PROPERTY_ELEMENT.getName()); + } } catch(SchemaNotFoundException e) { throw e; } @@ -122,9 +127,12 @@ public class PropertyElementManagement { if(type==null) { return jsonNode; } - - OClass oClass = TypesCache.getTypeSchema(type); - TypesCache.checkAccessType(oClass, type, AccessType.PROPERTY_ELEMENT); + TypesCache typesCache = TypesCache.getInstance(); + OClass oClass = typesCache.getTypeOClass(type); + AccessType gotAccessType = typesCache.getBaseAccessType(type); + if(AccessType.PROPERTY_ELEMENT!=gotAccessType) { + throw new SchemaException(type + " is not a " + AccessType.PROPERTY_ELEMENT.getName()); + } /* * 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 7145dec..7ed6cd2 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 @@ -187,7 +187,9 @@ public abstract class RelationElementManagement elementManagement = ElementManagementUtility.getERManagement(adminSecurityContext, oDatabaseDocument, type); + ElementManagement elementManagement = ElementManagementUtility.getERManagement(type); + elementManagement.setWorkingContext(adminSecurityContext); + elementManagement.setoDatabaseDocument(oDatabaseDocument); elementManagement.setUUID(uuid); elementManagement.setElementType(type); ((ERManagement) elementManagement).setHonourPropagationConstraintsInContextSharing(false); @@ -109,7 +111,9 @@ public class ERManagementUtility { for(UUID uuid : expectedInstances.keySet()) { String type = expectedInstances.get(uuid).get(Element.CLASS_PROPERTY).asText(); - ElementManagement elementManagement = ElementManagementUtility.getERManagement(adminSecurityContext, oDatabaseDocument, type); + ElementManagement elementManagement = ElementManagementUtility.getERManagement(type); + elementManagement.setWorkingContext(adminSecurityContext); + elementManagement.setoDatabaseDocument(oDatabaseDocument); elementManagement.setUUID(uuid); ((ERManagement) elementManagement).setHonourPropagationConstraintsInContextSharing(false); ((ERManagement) elementManagement).setDryRunContextSharing(dryRun); diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/entities/EntityManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/entities/EntityManagement.java index c85cfab..5a9474b 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/entities/EntityManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/entities/EntityManagement.java @@ -36,6 +36,7 @@ import org.gcube.informationsystem.resourceregistry.instances.base.ElementManage import org.gcube.informationsystem.resourceregistry.instances.base.entities.EntityElementManagement; import org.gcube.informationsystem.resourceregistry.instances.model.ERManagement; import org.gcube.informationsystem.resourceregistry.instances.model.relations.RelationManagement; +import org.gcube.informationsystem.resourceregistry.types.TypesCache; import org.gcube.informationsystem.resourceregistry.utils.HeaderUtility; import org.gcube.informationsystem.resourceregistry.utils.Utility; @@ -153,12 +154,6 @@ public abstract class EntityManagement extends EntityElementMa this.honourPropagationConstraintsInContextSharing = true; } - protected EntityManagement(AccessType accessType, SecurityContext workingContext, ODatabaseDocument orientGraph) { - this(accessType); - this.oDatabaseDocument = orientGraph; - setWorkingContext(workingContext); - } - @Override public OVertex getElement() throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException { try { @@ -784,13 +779,13 @@ public abstract class EntityManagement extends EntityElementMa try { oDatabaseDocument = getWorkingContext().getDatabaseDocument(PermissionMode.READER); - AccessType relationAccessType = ElementManagementUtility.getBaseAccessType(relationType); + AccessType relationAccessType = TypesCache.getInstance().getBaseAccessType(relationType); if(relationAccessType != AccessType.IS_RELATED_TO && relationAccessType != AccessType.CONSISTS_OF) { String error = String.format("%s must be a relation type", relationType); throw new ResourceRegistryException(error); } - AccessType referenceAccessType = ElementManagementUtility.getBaseAccessType(referenceType); + AccessType referenceAccessType = TypesCache.getInstance().getBaseAccessType(referenceType); if(referenceAccessType != AccessType.RESOURCE && referenceAccessType != AccessType.FACET) { String error = String.format("%s must be a en entity type", referenceType); throw new ResourceRegistryException(error); 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 ee0144e..e3799e7 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 @@ -11,9 +11,7 @@ import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegis import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.facet.FacetAlreadyPresentException; 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 com.orientechnologies.orient.core.db.document.ODatabaseDocument; import com.orientechnologies.orient.core.record.OVertex; /** @@ -25,10 +23,6 @@ public class FacetManagement extends EntityManagement { super(AccessType.FACET); } - public FacetManagement(SecurityContext workingContext, ODatabaseDocument orientGraph) { - super(AccessType.FACET, workingContext, orientGraph); - } - @Override protected FacetNotFoundException getSpecificElementNotFoundException(NotFoundException e) { return new FacetNotFoundException(e.getMessage(), e.getCause()); 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 47022e4..5234c40 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 @@ -36,7 +36,6 @@ 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; import com.orientechnologies.orient.core.record.ODirection; import com.orientechnologies.orient.core.record.OEdge; @@ -51,10 +50,6 @@ public class ResourceManagement extends EntityManagement { super(AccessType.RESOURCE); } - public ResourceManagement(SecurityContext workingContext, ODatabaseDocument oDatabaseDocument) { - super(AccessType.RESOURCE, workingContext, oDatabaseDocument); - } - @Override protected ResourceNotFoundException getSpecificElementNotFoundException(NotFoundException e) { return new ResourceNotFoundException(e.getMessage(), e.getCause()); @@ -147,7 +142,9 @@ public class ResourceManagement extends EntityManagement { if(jsonNode.has(property)) { JsonNode jsonNodeArray = jsonNode.get(property); for(JsonNode consistOfJsonNode : jsonNodeArray) { - ConsistsOfManagement com = new ConsistsOfManagement(getWorkingContext(), oDatabaseDocument); + ConsistsOfManagement com = new ConsistsOfManagement(); + com.setWorkingContext(getWorkingContext()); + com.setoDatabaseDocument(oDatabaseDocument); com.setJsonNode(consistOfJsonNode); com.setSourceEntityManagement(this); com.internalCreate(); @@ -159,7 +156,9 @@ public class ResourceManagement extends EntityManagement { if(jsonNode.has(property)) { JsonNode jsonNodeArray = jsonNode.get(property); for(JsonNode relationJsonNode : jsonNodeArray) { - IsRelatedToManagement irtm = new IsRelatedToManagement(getWorkingContext(), oDatabaseDocument); + IsRelatedToManagement irtm = new IsRelatedToManagement(); + irtm.setWorkingContext(getWorkingContext()); + irtm.setoDatabaseDocument(oDatabaseDocument); irtm.setJsonNode(relationJsonNode); irtm.setSourceEntityManagement(this); irtm.internalCreate(); @@ -179,7 +178,9 @@ public class ResourceManagement extends EntityManagement { if(jsonNode.has(property)) { JsonNode jsonNodeArray = jsonNode.get(property); for(JsonNode relationJsonNode : jsonNodeArray) { - ConsistsOfManagement com = new ConsistsOfManagement(getWorkingContext(), oDatabaseDocument); + ConsistsOfManagement com = new ConsistsOfManagement(); + com.setWorkingContext(getWorkingContext()); + com.setoDatabaseDocument(oDatabaseDocument); com.setJsonNode(relationJsonNode); com.internalCreateOrUdate(); addToRelationManagement(com); @@ -190,7 +191,9 @@ public class ResourceManagement extends EntityManagement { if(jsonNode.has(property)) { JsonNode jsonNodeArray = jsonNode.get(property); for(JsonNode relationJsonNode : jsonNodeArray) { - IsRelatedToManagement irtm = new IsRelatedToManagement(getWorkingContext(), oDatabaseDocument); + IsRelatedToManagement irtm = new IsRelatedToManagement(); + irtm.setWorkingContext(getWorkingContext()); + irtm.setoDatabaseDocument(oDatabaseDocument); irtm.setJsonNode(relationJsonNode); irtm.internalUpdate(); addToRelationManagement(irtm); @@ -217,14 +220,16 @@ public class ResourceManagement extends EntityManagement { @SuppressWarnings("rawtypes") RelationManagement relationManagement = null; if(oClass.isSubClassOf(IsRelatedTo.NAME)) { - relationManagement = new IsRelatedToManagement(getWorkingContext(), oDatabaseDocument); + relationManagement = new IsRelatedToManagement(); } else if(oClass.isSubClassOf(ConsistsOf.NAME)) { - relationManagement = new ConsistsOfManagement(getWorkingContext(), oDatabaseDocument); + relationManagement = new ConsistsOfManagement(); } else { logger.warn("{} is not a {} nor a {}. {}", Utility.toJsonString(edge, true), IsRelatedTo.NAME, ConsistsOf.NAME, Utility.SHOULD_NOT_OCCUR_ERROR_MESSAGE); } if(relationManagement != null) { + relationManagement.setWorkingContext(getWorkingContext()); + relationManagement.setoDatabaseDocument(oDatabaseDocument); relationManagement.setElement(edge); relationManagement.internalDelete(); } 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 21cae0e..5f65f7d 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 @@ -15,11 +15,8 @@ import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegis import org.gcube.informationsystem.resourceregistry.api.exceptions.relation.consistsOf.ConsistsOfAlreadyPresentException; import org.gcube.informationsystem.resourceregistry.api.exceptions.relation.consistsOf.ConsistsOfAvailableInAnotherContextException; import org.gcube.informationsystem.resourceregistry.api.exceptions.relation.consistsOf.ConsistsOfNotFoundException; -import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityContext; import org.gcube.informationsystem.resourceregistry.instances.model.entities.FacetManagement; -import com.orientechnologies.orient.core.db.document.ODatabaseDocument; - /** * @author Luca Frosini (ISTI - CNR) */ @@ -37,10 +34,6 @@ public class ConsistsOfManagement extends RelationManagement { super(AccessType.CONSISTS_OF, Facet.class, DEFAULT_CONSISTS_OF_PC); } - public ConsistsOfManagement(SecurityContext workingContext, ODatabaseDocument orientGraph) { - super(AccessType.CONSISTS_OF, Facet.class, workingContext, orientGraph, DEFAULT_CONSISTS_OF_PC); - } - @Override protected ConsistsOfNotFoundException getSpecificElementNotFoundException(NotFoundException e) { return new ConsistsOfNotFoundException(e.getMessage(), e.getCause()); @@ -59,7 +52,10 @@ public class ConsistsOfManagement extends RelationManagement { @Override protected FacetManagement newTargetEntityManagement() throws ResourceRegistryException { - return new FacetManagement(getWorkingContext(), oDatabaseDocument); + FacetManagement facetManagement = new FacetManagement(); + facetManagement.setoDatabaseDocument(oDatabaseDocument); + facetManagement.setWorkingContext(getWorkingContext()); + return facetManagement; } @Override 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 8cd9bcc..119fe89 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 @@ -15,11 +15,8 @@ import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegis import org.gcube.informationsystem.resourceregistry.api.exceptions.relation.isrelatedto.IsRelatedToAlreadyPresentException; import org.gcube.informationsystem.resourceregistry.api.exceptions.relation.isrelatedto.IsRelatedToAvailableInAnotherContextException; import org.gcube.informationsystem.resourceregistry.api.exceptions.relation.isrelatedto.IsRelatedToNotFoundException; -import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityContext; import org.gcube.informationsystem.resourceregistry.instances.model.entities.ResourceManagement; -import com.orientechnologies.orient.core.db.document.ODatabaseDocument; - /** * @author Luca Frosini (ISTI - CNR) */ @@ -37,10 +34,6 @@ public class IsRelatedToManagement extends RelationManagement targetEntityClass, SecurityContext workingContext, ODatabaseDocument orientGraph, - PropagationConstraint defaultPropagationConstraint) { - this(accessType, targetEntityClass, defaultPropagationConstraint); - this.oDatabaseDocument = orientGraph; - setWorkingContext(workingContext); - } - protected PropagationConstraint propagationConstraint; @Override @@ -335,7 +328,10 @@ public abstract class RelationManagement oClasses; - protected static final Map types; - protected static final Map> superTypes; - protected static final Map> specilisationTypes; + private static TypesCache typesCache; + + public synchronized static TypesCache getInstance() { + if(typesCache == null) { + typesCache = new TypesCache(); + } + return typesCache; + } + + protected final Map oClasses; + protected final Map accessTypes; + protected final Map types; + protected final Map> superTypes; + protected final Map> specilisationTypes; - static { + private TypesCache() { oClasses = new HashMap<>(); + accessTypes = new HashMap<>(); types = new HashMap<>(); superTypes = new HashMap<>(); specilisationTypes = new HashMap<>(); } - - - public static AccessType getBaseAccessType(String type) throws ResourceRegistryException { + public synchronized AccessType getBaseAccessType(String type) throws ResourceRegistryException { + AccessType accessType = accessTypes.get(type); + if(accessType==null) { - 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; + OClass oClass = getTypeOClass(type); + + if(oClass.isSubClassOf(Resource.NAME)) { + accessType = AccessType.RESOURCE; + } else if(oClass.isSubClassOf(Facet.NAME)) { + accessType = AccessType.FACET; + } else if(oClass.isSubClassOf(ConsistsOf.NAME)) { + accessType = AccessType.CONSISTS_OF; + } else if(oClass.isSubClassOf(IsRelatedTo.NAME)) { + accessType = AccessType.IS_RELATED_TO; + } else if(oClass.isSubClassOf(Property.NAME)) { + accessType = AccessType.PROPERTY; + }else { + throw new ResourceRegistryException(type + "is not a base type"); + } + + accessTypes.put(type, accessType); } - - throw new ResourceRegistryException(type + "is not a base type"); - + return accessType; } - public static void checkAccessType(OClass oClass, String type, AccessType accessType) throws SchemaException { + /* + public 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) + private OClass getTypeOClass(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); + OClass 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) { @@ -94,7 +107,7 @@ public class TypesCache { } } - public static OClass getTypeSchema(ODatabaseDocument oDatabaseDocument, String type) + public synchronized OClass getTypeOClass(ODatabaseDocument oDatabaseDocument, String type) throws SchemaException, SchemaNotFoundException { synchronized (oClasses) { @@ -106,17 +119,15 @@ public class TypesCache { OMetadata oMetadata = oDatabaseDocument.getMetadata(); OSchema oSchema = oMetadata.getSchema(); - return getTypeSchema(oSchema, type); + return getTypeOClass(oSchema, type); } - public static OClass getTypeSchema(String type) + public synchronized OClass getTypeOClass(String type) throws SchemaException, ResourceRegistryException { - synchronized (oClasses) { - OClass oClass = oClasses.get(type); - if(oClass!=null) { - return oClass; - } + OClass oClass = oClasses.get(type); + if(oClass!=null) { + return oClass; } ODatabaseDocument current = ContextUtility.getCurrentODatabaseDocumentFromThreadLocal(); @@ -125,7 +136,7 @@ public class TypesCache { logger.debug("GettingType {} schema", type); AdminSecurityContext adminSecurityContext = ContextUtility.getAdminSecurityContext(); oDatabaseDocument = adminSecurityContext.getDatabaseDocument(PermissionMode.READER); - return getTypeSchema(oDatabaseDocument, type); + return getTypeOClass(oDatabaseDocument, type); } catch(ResourceRegistryException e) { throw e; } catch(Exception e) {