Fixed type update

This commit is contained in:
Luca Frosini 2021-01-14 18:26:26 +01:00
parent 11bf7f0638
commit c1f694a9e2
4 changed files with 90 additions and 21 deletions

View File

@ -462,7 +462,7 @@ public class SchemaManagement {
private boolean superClassesMatch(Type actualTypeDefinition, Type newTypeDefinition) {
// Checking superclasses. Must be the same. If differs the operation will be aborted.
Set<String> actualSuperClasses = new HashSet<>(actualTypeDefinition.getSuperClasses());
Set<String> newSuperClasses = newTypeDefinition.getSuperClasses();
Set<String> newSuperClasses = new HashSet<>(newTypeDefinition.getSuperClasses());
if(actualSuperClasses.size()!=newSuperClasses.size()) {
return false;
@ -475,7 +475,7 @@ public class SchemaManagement {
actualSuperClasses = new HashSet<>(actualTypeDefinition.getSuperClasses());
newSuperClasses.removeAll(actualSuperClasses);
if(actualSuperClasses.size()>0) {
if(newSuperClasses.size()>0) {
return false;
}
@ -545,16 +545,25 @@ public class SchemaManagement {
}
Set<PropertyDefinition> newPropertyDefinitions = newTypeDefinition.getProperties();
Map<String, PropertyDefinition> newPropertyDefinitionMap = new HashMap<>(actualPropertyDefinitions.size());
for(PropertyDefinition newPropertyDefinition : newPropertyDefinitions) {
newPropertyDefinitionMap.put(newPropertyDefinition.getName(), newPropertyDefinition);
}
if(newPropertyDefinitions!=null) {
for(PropertyDefinition newPropertyDefinition : newPropertyDefinitions) {
if(newTypeDefinition.getName().compareTo(IdentifiableElement.HEADER_PROPERTY)==0 || newTypeDefinition.getName().compareTo(Relation.PROPAGATION_CONSTRAINT_PROPERTY)==0) {
String propertyName = newPropertyDefinition.getName();
if(propertyName.compareTo(IdentifiableElement.HEADER_PROPERTY)==0 || propertyName.compareTo(Relation.PROPAGATION_CONSTRAINT_PROPERTY)==0) {
continue;
}
if(newPropertyDefinition.equals(actualPropertyDefinitionMap.get(newPropertyDefinition.getName()))) {
// This property was not changed. Checking the next one.
PropertyDefinition actualPropertyDefinition = actualPropertyDefinitionMap.get(propertyName);
if(newPropertyDefinition.equals(actualPropertyDefinition)) {
// This property was not changed. Going to managing the next one.
continue;
}
@ -578,7 +587,14 @@ public class SchemaManagement {
}
}
OProperty op = oClass.createProperty(newPropertyDefinition.getName(), oType);
OProperty op;
if(actualPropertyDefinition!=null) {
// The property already exists and has changed (the check has been performed few lines above).
op = oClass.getProperty(propertyName);
}else {
op = oClass.createProperty(propertyName, oType);
}
op.setDescription(newPropertyDefinition.getDescription());
/*
@ -613,11 +629,13 @@ public class SchemaManagement {
}
}
actualPropertyDefinitions.removeAll(newPropertyDefinitions);
for(String propertyName : newPropertyDefinitionMap.keySet()) {
actualPropertyDefinitionMap.remove(propertyName);
}
// Removing old properties which are no more present in the new type definition
for(PropertyDefinition propertyDefinitionToRemove : actualPropertyDefinitions) {
oClass.dropProperty(propertyDefinitionToRemove.getName());
for(String propertyNameToRemove : actualPropertyDefinitionMap.keySet()) {
oClass.dropProperty(propertyNameToRemove);
}
}
@ -807,6 +825,9 @@ public class SchemaManagement {
oSchema.dropClass(typeName);
ElementManagement<? extends OElement> erManagement = getTypeManagement(accessType, typeName);
erManagement.delete();
oDatabaseDocument.commit();
return true;
@ -814,6 +835,12 @@ public class SchemaManagement {
throw e;
} catch(SchemaNotFoundException e) {
throw e;
} catch (OSchemaException e) {
if(e.getMessage().contains("was not found in current database")) {
throw new SchemaNotFoundException(e);
}else {
throw new SchemaException(e);
}
} catch(Exception e) {
throw new SchemaException(e);
} finally {

View File

@ -18,8 +18,10 @@ 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.ContextTest;
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaAlreadyPresentException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaNotFoundException;
import org.gcube.informationsystem.types.TypeMapper;
import org.gcube.informationsystem.types.impl.entities.EntityTypeImpl;
import org.gcube.informationsystem.types.impl.properties.PropertyTypeImpl;
@ -44,7 +46,7 @@ import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class SchemaManagementImplTest {
public class SchemaManagementImplTest extends ContextTest {
private static Logger logger = LoggerFactory
.getLogger(SchemaManagementImplTest.class);
@ -324,19 +326,56 @@ public class SchemaManagementImplTest {
@Test
public void createUpdateDeleteFacetType() throws Exception {
@SuppressWarnings("unchecked")
Class<? extends Facet>[] classes = new Class[]{TestFacet.class, TestFacet1_0_1.class, TestFacet1_0_2.class};
for(Class<? extends Facet> c : classes) {
SchemaManagement schemaManagement = new SchemaManagement();
Type type = TypeMapper.createTypeDefinition(c);
schemaManagement.setTypeName(type.getName());
schemaManagement.update(TypeMapper.serializeTypeDefinition(type), AccessType.FACET);
}
SchemaManagement schemaManagement = new SchemaManagement();
schemaManagement.setTypeName(TestFacet.NAME);
schemaManagement.delete(AccessType.FACET);
try {
@SuppressWarnings("unchecked")
Class<? extends Facet>[] classes = new Class[]{TestFacet.class, TestFacet1_0_1.class, TestFacet1_0_2.class};
for(Class<? extends Facet> c : classes) {
SchemaManagement schemaManagement = new SchemaManagement();
Type type = TypeMapper.createTypeDefinition(c);
schemaManagement.setTypeName(type.getName());
if(c == TestFacet.class) {
logger.info("Going to create {} : {}", type.getName(), TypeMapper.serializeTypeDefinition(type));
String ret = schemaManagement.create(TypeMapper.serializeTypeDefinition(type), AccessType.FACET);
logger.info("Created {} : {}", type.getName(), ret);
} else {
logger.info("Going to update {} : {}", type.getName(), TypeMapper.serializeTypeDefinition(type));
String ret = schemaManagement.update(TypeMapper.serializeTypeDefinition(type), AccessType.FACET);
logger.info("Updated {} : {}", type.getName(), ret);
}
}
SchemaManagement schemaManagement = new SchemaManagement();
Type type = TypeMapper.createTypeDefinition(TestFacet.class);
schemaManagement.setTypeName(type.getName());
schemaManagement.setSkipVersionCheckOnUpdate(true);
logger.info("Going to update {} : {}", type.getName(), TypeMapper.serializeTypeDefinition(type));
String ret = schemaManagement.update(TypeMapper.serializeTypeDefinition(type), AccessType.FACET);
logger.info("Updated {} : {}", type.getName(), ret);
} catch (Exception e) {
throw e;
} finally {
SchemaManagement schemaManagement = new SchemaManagement();
String typeName = TestFacet.NAME;
schemaManagement.setTypeName(typeName);
try {
logger.info("Going to delete {}", typeName);
schemaManagement.delete(AccessType.FACET);
logger.info("Deleted {}", typeName);
} catch (SchemaNotFoundException e) {
// The test has been failed before creating the type
} catch (Exception e) {
throw e;
}
}
}
@Test(expected=SchemaNotFoundException.class)
public void deleteFacetType() throws Exception {
SchemaManagement schemaManagement = new SchemaManagement();
schemaManagement.setTypeName(TestFacet.NAME);
schemaManagement.delete(AccessType.FACET);
}
}

View File

@ -30,6 +30,7 @@ import org.gcube.resourcemanagement.model.impl.entities.facets.SimpleFacetImpl;
version = "1.0.1"
)
@Change(version = TypeVersion.MINIMAL_VERSION_STRING, description = TypeVersion.MINIMAL_VERSION_DESCRIPTION)
@Change(version = "1.0.1", description = "Removed 'name' property, added 'title' property.")
public interface TestFacet1_0_1 extends Facet {
public static final String NAME = TestFacet.NAME; // SimpleFacet.class.getSimpleName();

View File

@ -30,6 +30,8 @@ import org.gcube.resourcemanagement.model.impl.entities.facets.SimpleFacetImpl;
version = "1.0.2"
)
@Change(version = TypeVersion.MINIMAL_VERSION_STRING, description = TypeVersion.MINIMAL_VERSION_DESCRIPTION)
@Change(version = "1.0.1", description = "Removed 'name' property, added 'title' property.")
@Change(version = "1.0.2", description = "Restored 'name' property. Set 'title' property as not mandatory and nullable.")
public interface TestFacet1_0_2 extends Facet {
public static final String NAME = TestFacet.NAME; // SimpleFacet.class.getSimpleName();