diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/base/ERManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/base/ERManagement.java index f050b92..3a9d2f1 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/base/ERManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/base/ERManagement.java @@ -439,10 +439,9 @@ public abstract class ERManagement { public abstract String reallyGetAll(boolean polymorphic) throws ResourceRegistryException; public String all(boolean polymorphic) throws ResourceRegistryException { + ODatabaseDocument current = ContextUtility.getCurrentODatabaseDocumentFromThreadLocal(); try { - oDatabaseDocument = getWorkingContext().getDatabaseDocument(PermissionMode.READER); - return reallyGetAll(polymorphic); } catch(ResourceRegistryException e) { throw e; @@ -452,10 +451,15 @@ public abstract class ERManagement { if(oDatabaseDocument != null) { oDatabaseDocument.close(); } + + if(current!=null) { + current.activateOnCurrentThread(); + } } } public boolean exists() throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException { + ODatabaseDocument current = ContextUtility.getCurrentODatabaseDocumentFromThreadLocal(); try { oDatabaseDocument = getWorkingContext().getDatabaseDocument(PermissionMode.READER); @@ -472,11 +476,16 @@ public abstract class ERManagement { if(oDatabaseDocument != null) { oDatabaseDocument.close(); } + + if(current!=null) { + current.activateOnCurrentThread(); + } } } public String createOrUpdate() throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException { + ODatabaseDocument current = ContextUtility.getCurrentODatabaseDocumentFromThreadLocal(); try { oDatabaseDocument = getWorkingContext().getDatabaseDocument(PermissionMode.WRITER); oDatabaseDocument.begin(); @@ -516,11 +525,15 @@ public abstract class ERManagement { if(oDatabaseDocument != null) { oDatabaseDocument.close(); } + + if(current!=null) { + current.activateOnCurrentThread(); + } } } public String create() throws AlreadyPresentException, ResourceRegistryException { - + ODatabaseDocument current = ContextUtility.getCurrentODatabaseDocumentFromThreadLocal(); try { oDatabaseDocument = getWorkingContext().getDatabaseDocument(PermissionMode.WRITER); oDatabaseDocument.begin(); @@ -549,11 +562,17 @@ public abstract class ERManagement { if(oDatabaseDocument != null) { oDatabaseDocument.close(); } + + if(current!=null) { + current.activateOnCurrentThread(); + } } } public String read() throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException { + ODatabaseDocument current = ContextUtility.getCurrentODatabaseDocumentFromThreadLocal(); try { + oDatabaseDocument = getWorkingContext().getDatabaseDocument(PermissionMode.READER); getElement(); @@ -569,10 +588,15 @@ public abstract class ERManagement { if(oDatabaseDocument != null) { oDatabaseDocument.close(); } + + if(current!=null) { + current.activateOnCurrentThread(); + } } } public String update() throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException { + ODatabaseDocument current = ContextUtility.getCurrentODatabaseDocumentFromThreadLocal(); try { oDatabaseDocument = getWorkingContext().getDatabaseDocument(PermissionMode.WRITER); oDatabaseDocument.begin(); @@ -603,12 +627,16 @@ public abstract class ERManagement { if(oDatabaseDocument != null) { oDatabaseDocument.close(); } + + if(current!=null) { + current.activateOnCurrentThread(); + } } } public boolean delete() throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException { logger.debug("Going to delete {} with UUID {}", accessType.getName(), uuid); - + ODatabaseDocument current = ContextUtility.getCurrentODatabaseDocumentFromThreadLocal(); try { oDatabaseDocument = ContextUtility.getAdminSecurityContext().getDatabaseDocument(PermissionMode.WRITER); oDatabaseDocument.begin(); @@ -641,13 +669,17 @@ public abstract class ERManagement { if(oDatabaseDocument != null) { oDatabaseDocument.close(); } + + if(current!=null) { + current.activateOnCurrentThread(); + } } } public boolean addToContext(UUID contextUUID) throws NotFoundException, ContextException, ResourceRegistryException { logger.info("Going to add {} with UUID {} to Context with UUID {}", accessType.getName(), uuid, contextUUID); - + ODatabaseDocument current = ContextUtility.getCurrentODatabaseDocumentFromThreadLocal(); try { oDatabaseDocument = ContextUtility.getAdminSecurityContext().getDatabaseDocument(PermissionMode.WRITER); oDatabaseDocument.begin(); @@ -676,13 +708,17 @@ public abstract class ERManagement { if(oDatabaseDocument != null) { oDatabaseDocument.close(); } + + if(current!=null) { + current.activateOnCurrentThread(); + } } } public boolean removeFromContext(UUID contextUUID) throws NotFoundException, ContextException, ResourceRegistryException { logger.debug("Going to remove {} with UUID {} from Context with UUID {}", elementType, uuid, contextUUID); - + ODatabaseDocument current = ContextUtility.getCurrentODatabaseDocumentFromThreadLocal(); try { oDatabaseDocument = ContextUtility.getAdminSecurityContext().getDatabaseDocument(PermissionMode.WRITER); oDatabaseDocument.begin(); @@ -713,6 +749,10 @@ public abstract class ERManagement { if(oDatabaseDocument != null) { oDatabaseDocument.close(); } + + if(current!=null) { + current.activateOnCurrentThread(); + } } } diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/base/ERManagementUtility.java b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/base/ERManagementUtility.java index 6d9556d..2b86fb0 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/base/ERManagementUtility.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/base/ERManagementUtility.java @@ -254,36 +254,6 @@ public class ERManagementUtility { current.activateOnCurrentThread(); } } - - /* - try { - ExecutorService es = Executors.newSingleThreadExecutor(); - Future result = es.submit(new Callable() { - public OClass call() throws Exception { - ODatabaseDocument oDatabaseDocument = null; - try { - logger.debug("Getting {} Type {} schema", accessType != null ? accessType.getName() : "", type); - - AdminSecurityContext adminSecurityContext = ContextUtility.getAdminSecurityContext(); - oDatabaseDocument = adminSecurityContext.getDatabaseDocument(PermissionMode.READER); - oDatabaseDocument.activateOnCurrentThread(); - return getTypeSchema(oDatabaseDocument, type, accessType); - } catch(ResourceRegistryException e) { - throw e; - } catch(Exception e) { - throw new ResourceRegistryException(e); - } finally { - if(oDatabaseDocument != null) { - oDatabaseDocument.close(); - } - } - } - }); - return result.get(); - } catch(Exception e) { - throw new ResourceRegistryException(e); - } - */ } } diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/type/SchemaManagementImpl.java b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/type/SchemaManagementImpl.java index cb7b460..f66fbcd 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/type/SchemaManagementImpl.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/type/SchemaManagementImpl.java @@ -122,7 +122,7 @@ public class SchemaManagementImpl implements SchemaManagement { ((RelationTypeDefinitionManagement) erManagement).setName(oClass.getName()); } - String ret = erManagement.serialize(); + String ret = erManagement.read(); return TypeBinder.deserializeTypeDefinition(ret); } catch(Exception e) { throw new SchemaException(e); @@ -344,7 +344,7 @@ public class SchemaManagementImpl implements SchemaManagement { ODatabaseDocument oDatabaseDocument = null; try { AdminSecurityContext adminSecurityContext = ContextUtility.getAdminSecurityContext(); - oDatabaseDocument = adminSecurityContext.getDatabaseDocument(PermissionMode.WRITER); + oDatabaseDocument = adminSecurityContext.getDatabaseDocument(PermissionMode.READER); OMetadata oMetadata = oDatabaseDocument.getMetadata(); OSchema oSchema = oMetadata.getSchema(); diff --git a/src/test/java/org/gcube/informationsystem/resourceregistry/type/SchemaManagementImplTest.java b/src/test/java/org/gcube/informationsystem/resourceregistry/type/SchemaManagementImplTest.java index 85bb21a..e62f5e7 100644 --- a/src/test/java/org/gcube/informationsystem/resourceregistry/type/SchemaManagementImplTest.java +++ b/src/test/java/org/gcube/informationsystem/resourceregistry/type/SchemaManagementImplTest.java @@ -19,6 +19,7 @@ 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; +import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaAlreadyPresentException; import org.gcube.informationsystem.resourceregistry.instances.type.SchemaManagement; import org.gcube.informationsystem.resourceregistry.instances.type.SchemaManagementImpl; import org.gcube.informationsystem.types.TypeBinder; @@ -144,7 +145,7 @@ public class SchemaManagementImplTest { } - @Test + @Test(expected=SchemaAlreadyPresentException.class) public void createPropertyType() throws Exception { PropertyTypeDefinition propertyTypeDefinition = new PropertyTypeDefinitionImpl<>(ValueSchema.class); @@ -156,7 +157,7 @@ public class SchemaManagementImplTest { } - @Test + @Test(expected=SchemaAlreadyPresentException.class) public void createEncryptedType() throws Exception { PropertyTypeDefinition propertyTypeDefinition = new PropertyTypeDefinitionImpl<>(Encrypted.class); @@ -168,7 +169,7 @@ public class SchemaManagementImplTest { } - @Test + @Test(expected=SchemaAlreadyPresentException.class) public void createContextType() throws Exception { EntityTypeDefinition entityTypeDefinition = new EntityTypeDefinitionImpl(Context.class); @@ -180,7 +181,7 @@ public class SchemaManagementImplTest { } - @Test + @Test(expected=SchemaAlreadyPresentException.class) public void createFacetType() throws Exception { EntityTypeDefinition entityTypeDefinition = new EntityTypeDefinitionImpl(AccessPointFacet.class); @@ -192,7 +193,7 @@ public class SchemaManagementImplTest { } - @Test + @Test(expected=SchemaAlreadyPresentException.class) public void createResourceType() throws Exception { EntityTypeDefinition entityTypeDefinition = new EntityTypeDefinitionImpl(EService.class); @@ -212,7 +213,7 @@ public class SchemaManagementImplTest { } - @Test + @Test(expected=SchemaAlreadyPresentException.class) public void createRelationTypeDefinitionType() throws Exception { RelationTypeDefinition relationTypeDefinition = new RelationTypeDefinitionImpl((Class>) RelationTypeDefinition.class); @@ -224,7 +225,7 @@ public class SchemaManagementImplTest { } - @Test + @Test(expected=SchemaAlreadyPresentException.class) public void createIsRelatedToType() throws Exception { @SuppressWarnings("unchecked") RelationTypeDefinition relationTypeDefinition = new RelationTypeDefinitionImpl((Class>) Uses.class); @@ -237,7 +238,7 @@ public class SchemaManagementImplTest { } - @Test + @Test(expected=SchemaAlreadyPresentException.class) public void createConsistsOfType() throws Exception { @SuppressWarnings("unchecked") RelationTypeDefinition relationTypeDefinition = new RelationTypeDefinitionImpl((Class>) HasVolatileMemory.class);