Fixed type schema serialization

This commit is contained in:
Luca Frosini 2019-11-11 16:09:47 +01:00
parent da8082d55f
commit 7e602b72bd
4 changed files with 57 additions and 46 deletions

View File

@ -439,10 +439,9 @@ public abstract class ERManagement<El extends OElement> {
public abstract String reallyGetAll(boolean polymorphic) throws ResourceRegistryException; public abstract String reallyGetAll(boolean polymorphic) throws ResourceRegistryException;
public String all(boolean polymorphic) throws ResourceRegistryException { public String all(boolean polymorphic) throws ResourceRegistryException {
ODatabaseDocument current = ContextUtility.getCurrentODatabaseDocumentFromThreadLocal();
try { try {
oDatabaseDocument = getWorkingContext().getDatabaseDocument(PermissionMode.READER); oDatabaseDocument = getWorkingContext().getDatabaseDocument(PermissionMode.READER);
return reallyGetAll(polymorphic); return reallyGetAll(polymorphic);
} catch(ResourceRegistryException e) { } catch(ResourceRegistryException e) {
throw e; throw e;
@ -452,10 +451,15 @@ public abstract class ERManagement<El extends OElement> {
if(oDatabaseDocument != null) { if(oDatabaseDocument != null) {
oDatabaseDocument.close(); oDatabaseDocument.close();
} }
if(current!=null) {
current.activateOnCurrentThread();
}
} }
} }
public boolean exists() throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException { public boolean exists() throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException {
ODatabaseDocument current = ContextUtility.getCurrentODatabaseDocumentFromThreadLocal();
try { try {
oDatabaseDocument = getWorkingContext().getDatabaseDocument(PermissionMode.READER); oDatabaseDocument = getWorkingContext().getDatabaseDocument(PermissionMode.READER);
@ -472,11 +476,16 @@ public abstract class ERManagement<El extends OElement> {
if(oDatabaseDocument != null) { if(oDatabaseDocument != null) {
oDatabaseDocument.close(); oDatabaseDocument.close();
} }
if(current!=null) {
current.activateOnCurrentThread();
}
} }
} }
public String createOrUpdate() public String createOrUpdate()
throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException { throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException {
ODatabaseDocument current = ContextUtility.getCurrentODatabaseDocumentFromThreadLocal();
try { try {
oDatabaseDocument = getWorkingContext().getDatabaseDocument(PermissionMode.WRITER); oDatabaseDocument = getWorkingContext().getDatabaseDocument(PermissionMode.WRITER);
oDatabaseDocument.begin(); oDatabaseDocument.begin();
@ -516,11 +525,15 @@ public abstract class ERManagement<El extends OElement> {
if(oDatabaseDocument != null) { if(oDatabaseDocument != null) {
oDatabaseDocument.close(); oDatabaseDocument.close();
} }
if(current!=null) {
current.activateOnCurrentThread();
}
} }
} }
public String create() throws AlreadyPresentException, ResourceRegistryException { public String create() throws AlreadyPresentException, ResourceRegistryException {
ODatabaseDocument current = ContextUtility.getCurrentODatabaseDocumentFromThreadLocal();
try { try {
oDatabaseDocument = getWorkingContext().getDatabaseDocument(PermissionMode.WRITER); oDatabaseDocument = getWorkingContext().getDatabaseDocument(PermissionMode.WRITER);
oDatabaseDocument.begin(); oDatabaseDocument.begin();
@ -549,11 +562,17 @@ public abstract class ERManagement<El extends OElement> {
if(oDatabaseDocument != null) { if(oDatabaseDocument != null) {
oDatabaseDocument.close(); oDatabaseDocument.close();
} }
if(current!=null) {
current.activateOnCurrentThread();
}
} }
} }
public String read() throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException { public String read() throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException {
ODatabaseDocument current = ContextUtility.getCurrentODatabaseDocumentFromThreadLocal();
try { try {
oDatabaseDocument = getWorkingContext().getDatabaseDocument(PermissionMode.READER); oDatabaseDocument = getWorkingContext().getDatabaseDocument(PermissionMode.READER);
getElement(); getElement();
@ -569,10 +588,15 @@ public abstract class ERManagement<El extends OElement> {
if(oDatabaseDocument != null) { if(oDatabaseDocument != null) {
oDatabaseDocument.close(); oDatabaseDocument.close();
} }
if(current!=null) {
current.activateOnCurrentThread();
}
} }
} }
public String update() throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException { public String update() throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException {
ODatabaseDocument current = ContextUtility.getCurrentODatabaseDocumentFromThreadLocal();
try { try {
oDatabaseDocument = getWorkingContext().getDatabaseDocument(PermissionMode.WRITER); oDatabaseDocument = getWorkingContext().getDatabaseDocument(PermissionMode.WRITER);
oDatabaseDocument.begin(); oDatabaseDocument.begin();
@ -603,12 +627,16 @@ public abstract class ERManagement<El extends OElement> {
if(oDatabaseDocument != null) { if(oDatabaseDocument != null) {
oDatabaseDocument.close(); oDatabaseDocument.close();
} }
if(current!=null) {
current.activateOnCurrentThread();
}
} }
} }
public boolean delete() throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException { public boolean delete() throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException {
logger.debug("Going to delete {} with UUID {}", accessType.getName(), uuid); logger.debug("Going to delete {} with UUID {}", accessType.getName(), uuid);
ODatabaseDocument current = ContextUtility.getCurrentODatabaseDocumentFromThreadLocal();
try { try {
oDatabaseDocument = ContextUtility.getAdminSecurityContext().getDatabaseDocument(PermissionMode.WRITER); oDatabaseDocument = ContextUtility.getAdminSecurityContext().getDatabaseDocument(PermissionMode.WRITER);
oDatabaseDocument.begin(); oDatabaseDocument.begin();
@ -641,13 +669,17 @@ public abstract class ERManagement<El extends OElement> {
if(oDatabaseDocument != null) { if(oDatabaseDocument != null) {
oDatabaseDocument.close(); oDatabaseDocument.close();
} }
if(current!=null) {
current.activateOnCurrentThread();
}
} }
} }
public boolean addToContext(UUID contextUUID) public boolean addToContext(UUID contextUUID)
throws NotFoundException, ContextException, ResourceRegistryException { throws NotFoundException, ContextException, ResourceRegistryException {
logger.info("Going to add {} with UUID {} to Context with UUID {}", accessType.getName(), uuid, contextUUID); logger.info("Going to add {} with UUID {} to Context with UUID {}", accessType.getName(), uuid, contextUUID);
ODatabaseDocument current = ContextUtility.getCurrentODatabaseDocumentFromThreadLocal();
try { try {
oDatabaseDocument = ContextUtility.getAdminSecurityContext().getDatabaseDocument(PermissionMode.WRITER); oDatabaseDocument = ContextUtility.getAdminSecurityContext().getDatabaseDocument(PermissionMode.WRITER);
oDatabaseDocument.begin(); oDatabaseDocument.begin();
@ -676,13 +708,17 @@ public abstract class ERManagement<El extends OElement> {
if(oDatabaseDocument != null) { if(oDatabaseDocument != null) {
oDatabaseDocument.close(); oDatabaseDocument.close();
} }
if(current!=null) {
current.activateOnCurrentThread();
}
} }
} }
public boolean removeFromContext(UUID contextUUID) public boolean removeFromContext(UUID contextUUID)
throws NotFoundException, ContextException, ResourceRegistryException { throws NotFoundException, ContextException, ResourceRegistryException {
logger.debug("Going to remove {} with UUID {} from Context with UUID {}", elementType, uuid, contextUUID); logger.debug("Going to remove {} with UUID {} from Context with UUID {}", elementType, uuid, contextUUID);
ODatabaseDocument current = ContextUtility.getCurrentODatabaseDocumentFromThreadLocal();
try { try {
oDatabaseDocument = ContextUtility.getAdminSecurityContext().getDatabaseDocument(PermissionMode.WRITER); oDatabaseDocument = ContextUtility.getAdminSecurityContext().getDatabaseDocument(PermissionMode.WRITER);
oDatabaseDocument.begin(); oDatabaseDocument.begin();
@ -713,6 +749,10 @@ public abstract class ERManagement<El extends OElement> {
if(oDatabaseDocument != null) { if(oDatabaseDocument != null) {
oDatabaseDocument.close(); oDatabaseDocument.close();
} }
if(current!=null) {
current.activateOnCurrentThread();
}
} }
} }

View File

@ -254,36 +254,6 @@ public class ERManagementUtility {
current.activateOnCurrentThread(); current.activateOnCurrentThread();
} }
} }
/*
try {
ExecutorService es = Executors.newSingleThreadExecutor();
Future<OClass> result = es.submit(new Callable<OClass>() {
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);
}
*/
} }
} }

View File

@ -122,7 +122,7 @@ public class SchemaManagementImpl implements SchemaManagement {
((RelationTypeDefinitionManagement) erManagement).setName(oClass.getName()); ((RelationTypeDefinitionManagement) erManagement).setName(oClass.getName());
} }
String ret = erManagement.serialize(); String ret = erManagement.read();
return TypeBinder.deserializeTypeDefinition(ret); return TypeBinder.deserializeTypeDefinition(ret);
} catch(Exception e) { } catch(Exception e) {
throw new SchemaException(e); throw new SchemaException(e);
@ -344,7 +344,7 @@ public class SchemaManagementImpl implements SchemaManagement {
ODatabaseDocument oDatabaseDocument = null; ODatabaseDocument oDatabaseDocument = null;
try { try {
AdminSecurityContext adminSecurityContext = ContextUtility.getAdminSecurityContext(); AdminSecurityContext adminSecurityContext = ContextUtility.getAdminSecurityContext();
oDatabaseDocument = adminSecurityContext.getDatabaseDocument(PermissionMode.WRITER); oDatabaseDocument = adminSecurityContext.getDatabaseDocument(PermissionMode.READER);
OMetadata oMetadata = oDatabaseDocument.getMetadata(); OMetadata oMetadata = oDatabaseDocument.getMetadata();
OSchema oSchema = oMetadata.getSchema(); OSchema oSchema = oMetadata.getSchema();

View File

@ -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.ConsistsOf;
import org.gcube.informationsystem.model.reference.relations.IsRelatedTo; import org.gcube.informationsystem.model.reference.relations.IsRelatedTo;
import org.gcube.informationsystem.model.reference.relations.Relation; 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.SchemaManagement;
import org.gcube.informationsystem.resourceregistry.instances.type.SchemaManagementImpl; import org.gcube.informationsystem.resourceregistry.instances.type.SchemaManagementImpl;
import org.gcube.informationsystem.types.TypeBinder; import org.gcube.informationsystem.types.TypeBinder;
@ -144,7 +145,7 @@ public class SchemaManagementImplTest {
} }
@Test @Test(expected=SchemaAlreadyPresentException.class)
public void createPropertyType() throws Exception { public void createPropertyType() throws Exception {
PropertyTypeDefinition<ValueSchema> propertyTypeDefinition = new PropertyTypeDefinitionImpl<>(ValueSchema.class); PropertyTypeDefinition<ValueSchema> propertyTypeDefinition = new PropertyTypeDefinitionImpl<>(ValueSchema.class);
@ -156,7 +157,7 @@ public class SchemaManagementImplTest {
} }
@Test @Test(expected=SchemaAlreadyPresentException.class)
public void createEncryptedType() throws Exception { public void createEncryptedType() throws Exception {
PropertyTypeDefinition<Encrypted> propertyTypeDefinition = new PropertyTypeDefinitionImpl<>(Encrypted.class); PropertyTypeDefinition<Encrypted> propertyTypeDefinition = new PropertyTypeDefinitionImpl<>(Encrypted.class);
@ -168,7 +169,7 @@ public class SchemaManagementImplTest {
} }
@Test @Test(expected=SchemaAlreadyPresentException.class)
public void createContextType() throws Exception { public void createContextType() throws Exception {
EntityTypeDefinition entityTypeDefinition = new EntityTypeDefinitionImpl(Context.class); EntityTypeDefinition entityTypeDefinition = new EntityTypeDefinitionImpl(Context.class);
@ -180,7 +181,7 @@ public class SchemaManagementImplTest {
} }
@Test @Test(expected=SchemaAlreadyPresentException.class)
public void createFacetType() throws Exception { public void createFacetType() throws Exception {
EntityTypeDefinition entityTypeDefinition = new EntityTypeDefinitionImpl(AccessPointFacet.class); EntityTypeDefinition entityTypeDefinition = new EntityTypeDefinitionImpl(AccessPointFacet.class);
@ -192,7 +193,7 @@ public class SchemaManagementImplTest {
} }
@Test @Test(expected=SchemaAlreadyPresentException.class)
public void createResourceType() throws Exception { public void createResourceType() throws Exception {
EntityTypeDefinition entityTypeDefinition = new EntityTypeDefinitionImpl(EService.class); EntityTypeDefinition entityTypeDefinition = new EntityTypeDefinitionImpl(EService.class);
@ -212,7 +213,7 @@ public class SchemaManagementImplTest {
} }
@Test @Test(expected=SchemaAlreadyPresentException.class)
public void createRelationTypeDefinitionType() throws Exception { public void createRelationTypeDefinitionType() throws Exception {
RelationTypeDefinition relationTypeDefinition = new RelationTypeDefinitionImpl((Class<? extends BaseRelation<?,?>>) RelationTypeDefinition.class); RelationTypeDefinition relationTypeDefinition = new RelationTypeDefinitionImpl((Class<? extends BaseRelation<?,?>>) RelationTypeDefinition.class);
@ -224,7 +225,7 @@ public class SchemaManagementImplTest {
} }
@Test @Test(expected=SchemaAlreadyPresentException.class)
public void createIsRelatedToType() throws Exception { public void createIsRelatedToType() throws Exception {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
RelationTypeDefinition relationTypeDefinition = new RelationTypeDefinitionImpl((Class<? extends BaseRelation<?,?>>) Uses.class); RelationTypeDefinition relationTypeDefinition = new RelationTypeDefinitionImpl((Class<? extends BaseRelation<?,?>>) Uses.class);
@ -237,7 +238,7 @@ public class SchemaManagementImplTest {
} }
@Test @Test(expected=SchemaAlreadyPresentException.class)
public void createConsistsOfType() throws Exception { public void createConsistsOfType() throws Exception {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
RelationTypeDefinition relationTypeDefinition = new RelationTypeDefinitionImpl((Class<? extends BaseRelation<?,?>>) HasVolatileMemory.class); RelationTypeDefinition relationTypeDefinition = new RelationTypeDefinitionImpl((Class<? extends BaseRelation<?,?>>) HasVolatileMemory.class);