Fixed type update
This commit is contained in:
parent
11bf7f0638
commit
c1f694a9e2
|
@ -462,7 +462,7 @@ public class SchemaManagement {
|
||||||
private boolean superClassesMatch(Type actualTypeDefinition, Type newTypeDefinition) {
|
private boolean superClassesMatch(Type actualTypeDefinition, Type newTypeDefinition) {
|
||||||
// Checking superclasses. Must be the same. If differs the operation will be aborted.
|
// Checking superclasses. Must be the same. If differs the operation will be aborted.
|
||||||
Set<String> actualSuperClasses = new HashSet<>(actualTypeDefinition.getSuperClasses());
|
Set<String> actualSuperClasses = new HashSet<>(actualTypeDefinition.getSuperClasses());
|
||||||
Set<String> newSuperClasses = newTypeDefinition.getSuperClasses();
|
Set<String> newSuperClasses = new HashSet<>(newTypeDefinition.getSuperClasses());
|
||||||
|
|
||||||
if(actualSuperClasses.size()!=newSuperClasses.size()) {
|
if(actualSuperClasses.size()!=newSuperClasses.size()) {
|
||||||
return false;
|
return false;
|
||||||
|
@ -475,7 +475,7 @@ public class SchemaManagement {
|
||||||
|
|
||||||
actualSuperClasses = new HashSet<>(actualTypeDefinition.getSuperClasses());
|
actualSuperClasses = new HashSet<>(actualTypeDefinition.getSuperClasses());
|
||||||
newSuperClasses.removeAll(actualSuperClasses);
|
newSuperClasses.removeAll(actualSuperClasses);
|
||||||
if(actualSuperClasses.size()>0) {
|
if(newSuperClasses.size()>0) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -545,16 +545,25 @@ public class SchemaManagement {
|
||||||
}
|
}
|
||||||
|
|
||||||
Set<PropertyDefinition> newPropertyDefinitions = newTypeDefinition.getProperties();
|
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) {
|
if(newPropertyDefinitions!=null) {
|
||||||
for(PropertyDefinition newPropertyDefinition : newPropertyDefinitions) {
|
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;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(newPropertyDefinition.equals(actualPropertyDefinitionMap.get(newPropertyDefinition.getName()))) {
|
PropertyDefinition actualPropertyDefinition = actualPropertyDefinitionMap.get(propertyName);
|
||||||
// This property was not changed. Checking the next one.
|
|
||||||
|
if(newPropertyDefinition.equals(actualPropertyDefinition)) {
|
||||||
|
// This property was not changed. Going to managing the next one.
|
||||||
continue;
|
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());
|
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
|
// Removing old properties which are no more present in the new type definition
|
||||||
for(PropertyDefinition propertyDefinitionToRemove : actualPropertyDefinitions) {
|
for(String propertyNameToRemove : actualPropertyDefinitionMap.keySet()) {
|
||||||
oClass.dropProperty(propertyDefinitionToRemove.getName());
|
oClass.dropProperty(propertyNameToRemove);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -807,6 +825,9 @@ public class SchemaManagement {
|
||||||
|
|
||||||
oSchema.dropClass(typeName);
|
oSchema.dropClass(typeName);
|
||||||
|
|
||||||
|
ElementManagement<? extends OElement> erManagement = getTypeManagement(accessType, typeName);
|
||||||
|
erManagement.delete();
|
||||||
|
|
||||||
oDatabaseDocument.commit();
|
oDatabaseDocument.commit();
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
@ -814,6 +835,12 @@ public class SchemaManagement {
|
||||||
throw e;
|
throw e;
|
||||||
} catch(SchemaNotFoundException e) {
|
} catch(SchemaNotFoundException e) {
|
||||||
throw 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) {
|
} catch(Exception e) {
|
||||||
throw new SchemaException(e);
|
throw new SchemaException(e);
|
||||||
} finally {
|
} finally {
|
||||||
|
|
|
@ -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.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.ContextTest;
|
||||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaAlreadyPresentException;
|
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.SchemaException;
|
||||||
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaNotFoundException;
|
||||||
import org.gcube.informationsystem.types.TypeMapper;
|
import org.gcube.informationsystem.types.TypeMapper;
|
||||||
import org.gcube.informationsystem.types.impl.entities.EntityTypeImpl;
|
import org.gcube.informationsystem.types.impl.entities.EntityTypeImpl;
|
||||||
import org.gcube.informationsystem.types.impl.properties.PropertyTypeImpl;
|
import org.gcube.informationsystem.types.impl.properties.PropertyTypeImpl;
|
||||||
|
@ -44,7 +46,7 @@ import org.slf4j.LoggerFactory;
|
||||||
/**
|
/**
|
||||||
* @author Luca Frosini (ISTI - CNR)
|
* @author Luca Frosini (ISTI - CNR)
|
||||||
*/
|
*/
|
||||||
public class SchemaManagementImplTest {
|
public class SchemaManagementImplTest extends ContextTest {
|
||||||
|
|
||||||
private static Logger logger = LoggerFactory
|
private static Logger logger = LoggerFactory
|
||||||
.getLogger(SchemaManagementImplTest.class);
|
.getLogger(SchemaManagementImplTest.class);
|
||||||
|
@ -324,19 +326,56 @@ public class SchemaManagementImplTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void createUpdateDeleteFacetType() throws Exception {
|
public void createUpdateDeleteFacetType() throws Exception {
|
||||||
|
|
||||||
|
try {
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
Class<? extends Facet>[] classes = new Class[]{TestFacet.class, TestFacet1_0_1.class, TestFacet1_0_2.class};
|
Class<? extends Facet>[] classes = new Class[]{TestFacet.class, TestFacet1_0_1.class, TestFacet1_0_2.class};
|
||||||
for(Class<? extends Facet> c : classes) {
|
for(Class<? extends Facet> c : classes) {
|
||||||
SchemaManagement schemaManagement = new SchemaManagement();
|
SchemaManagement schemaManagement = new SchemaManagement();
|
||||||
Type type = TypeMapper.createTypeDefinition(c);
|
Type type = TypeMapper.createTypeDefinition(c);
|
||||||
schemaManagement.setTypeName(type.getName());
|
schemaManagement.setTypeName(type.getName());
|
||||||
schemaManagement.update(TypeMapper.serializeTypeDefinition(type), AccessType.FACET);
|
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 schemaManagement = new SchemaManagement();
|
||||||
schemaManagement.setTypeName(TestFacet.NAME);
|
schemaManagement.setTypeName(TestFacet.NAME);
|
||||||
schemaManagement.delete(AccessType.FACET);
|
schemaManagement.delete(AccessType.FACET);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,6 +30,7 @@ import org.gcube.resourcemanagement.model.impl.entities.facets.SimpleFacetImpl;
|
||||||
version = "1.0.1"
|
version = "1.0.1"
|
||||||
)
|
)
|
||||||
@Change(version = TypeVersion.MINIMAL_VERSION_STRING, description = TypeVersion.MINIMAL_VERSION_DESCRIPTION)
|
@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 interface TestFacet1_0_1 extends Facet {
|
||||||
|
|
||||||
public static final String NAME = TestFacet.NAME; // SimpleFacet.class.getSimpleName();
|
public static final String NAME = TestFacet.NAME; // SimpleFacet.class.getSimpleName();
|
||||||
|
|
|
@ -30,6 +30,8 @@ import org.gcube.resourcemanagement.model.impl.entities.facets.SimpleFacetImpl;
|
||||||
version = "1.0.2"
|
version = "1.0.2"
|
||||||
)
|
)
|
||||||
@Change(version = TypeVersion.MINIMAL_VERSION_STRING, description = TypeVersion.MINIMAL_VERSION_DESCRIPTION)
|
@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 interface TestFacet1_0_2 extends Facet {
|
||||||
|
|
||||||
public static final String NAME = TestFacet.NAME; // SimpleFacet.class.getSimpleName();
|
public static final String NAME = TestFacet.NAME; // SimpleFacet.class.getSimpleName();
|
||||||
|
|
Loading…
Reference in New Issue