Creating update method ofr type

This commit is contained in:
Luca Frosini 2020-12-14 12:34:21 +01:00
parent 7327f61370
commit 679c0a7910
2 changed files with 199 additions and 4 deletions

View File

@ -17,8 +17,7 @@ public interface SchemaManagement {
public String read(String type, boolean includeSubtypes) throws SchemaNotFoundException, SchemaException;
public String update(String type, AccessType accessType, String json)
throws SchemaNotFoundException, SchemaException;
public String update(String json, AccessType accessType) throws SchemaNotFoundException, SchemaException;
public String delete(String type, AccessType accessType) throws SchemaNotFoundException;

View File

@ -374,6 +374,151 @@ public class SchemaManagementImpl implements SchemaManagement {
}
}
// TODO
protected void updateTypeSchema(Type type, AccessType baseElementAccessType)
throws SchemaAlreadyPresentException, SchemaException {
ODatabaseDocument oDatabaseDocument = null;
try {
if(typeName.compareTo(type.getName()) != 0) {
String error = String.format(
"Provided type name path argument %s does not match with the type name in the definition %S. Please be coherent.",
typeName, type.getName());
throw new SchemaCreationException(error);
}
AdminSecurityContext adminSecurityContext = ContextUtility.getAdminSecurityContext();
oDatabaseDocument = adminSecurityContext.getDatabaseDocument(PermissionMode.WRITER);
OMetadata oMetadata = oDatabaseDocument.getMetadata();
OSchema oSchema = oMetadata.getSchema();
OClass oClass = oSchema.getClass(typeName);
if(oClass == null) {
throw new SchemaNotFoundException(typeName + " does not Exists");
}
try {
String description = type.getDescription();
if(description != null && description.compareTo("") != 0) {
try {
oClass.setDescription(description);
} catch(Exception e) {
logger.warn(
"Unable to set description. This is an orient bug. See https://github.com/orientechnologies/orientdb/issues/7065");
}
}
try {
// oClass.setAbstract(false); // Used to allow to persist Schema in Context
// Management
oClass.setAbstract(type.isAbstract());
} catch(Exception e) {
logger.error(
"Unable to set the Vertex Type {} as abstract. This is an OrientDB <= 2.2.12 bug. The Type will be created as it is not abstract.",
type.getName());
}
if(!baseElementTypes.contains(type.getName())) {
List<OClass> oSuperclasses = getSuperclassesAndCheckCompliancy(oDatabaseDocument, type,
baseElementAccessType.getName());
oClass.setSuperClasses(oSuperclasses);
}
// TODO check removed/added properties
if(!(type instanceof ResourceType)) {
// A Resource cannot contains any properties.
Set<PropertyDefinition> propertyDefinitions = type.getProperties();
if(propertyDefinitions!=null) {
for(PropertyDefinition propertyDefinition : propertyDefinitions) {
OType oType = OType.getById(propertyDefinition.getType().byteValue());
/*
* Types update is not allowed,
* hence bug https://github.com/orientechnologies/orientdb/issues/7354 cannot occur
* Excluding the check from types used for type definition
*
*/
if(!typeList.contains(type.getName())) {
switch(oType) {
case EMBEDDEDLIST:
throw new UnsupportedDataTypeException(OrientDBType.OType.PROPERTYLIST
+ " support is currently disabled due to OrientDB bug see https://github.com/orientechnologies/orientdb/issues/7354");
case EMBEDDEDSET:
throw new UnsupportedDataTypeException(OrientDBType.OType.PROPERTYSET
+ " support is currently disabled due to OrientDB bug see https://github.com/orientechnologies/orientdb/issues/7354");
default:
break;
}
}
OProperty op = oClass.createProperty(propertyDefinition.getName(), oType);
op.setDescription(propertyDefinition.getDescription());
/*
* Mandatory and notNull does not work in distributed mode: so that on Type
* declaration they are forced to false
* ovp.setMandatory(property.isMandatory());
* ovp.setNotNull(property.isNotnull()); This information are persisted in
* Management Context
*/
op.setMandatory(false);
op.setNotNull(false);
op.setReadonly(propertyDefinition.isReadonly());
op.setRegexp(propertyDefinition.getRegexp());
if(propertyDefinition.getLinkedClass() != null) {
OClass linkedClass = getOClass(oSchema, propertyDefinition.getLinkedClass());
if(linkedClass == null) {
logger.trace("class {} not found in schema", propertyDefinition.getLinkedClass());
throw new Exception(
"class " + propertyDefinition.getLinkedClass() + " not found in schema");
}
if(linkedClass.isEdgeType() || linkedClass.isVertexType()) {
throw new Exception("A Property Field cannot be an Entity or a Relation");
}
op.setLinkedClass(linkedClass);
} else if(propertyDefinition.getLinkedType() != null) {
op.setLinkedType(OType.getById(propertyDefinition.getLinkedType().byteValue()));
}
}
}
}
oDatabaseDocument.commit();
logger.info("{} {} registered successfully", baseElementAccessType.getName(), type.getName());
} catch(Exception e) {
oSchema.dropClass(type.getName());
throw e;
}
} catch(OSchemaException ex) {
if(ex.getMessage().contains("already exists")) {
throw new SchemaAlreadyPresentException(ex);
}
throw new SchemaException(ex);
} catch(SchemaException e) {
throw e;
} catch(Exception ex) {
throw new SchemaCreationException(ex);
} finally {
oDatabaseDocument.close();
}
}
protected String getSchema(String typeName, boolean includeSubtypes) throws SchemaNotFoundException, SchemaException {
ODatabaseDocument oDatabaseDocument = null;
try {
@ -470,10 +615,61 @@ public class SchemaManagementImpl implements SchemaManagement {
return getSchema(typeName, includeSubtypes);
}
// TODO
@Override
public String update(String typeName, AccessType accessType, String jsonSchema)
public String update(String jsonSchema, AccessType accessType)
throws SchemaNotFoundException, SchemaException {
throw new UnsupportedOperationException();
Type typeDefinition = null;
try {
try {
typeDefinition = TypeMapper.deserializeTypeDefinition(jsonSchema);
logger.info("Trying to register {} {} : {}", accessType.getName(), typeDefinition.getName(),
jsonSchema);
} catch(Exception e) {
logger.error("Error while trying to register {} {}", accessType.getName(), jsonSchema);
throw new SchemaCreationException(e);
}
// TODO check if the version is greater
updateTypeSchema(typeDefinition, accessType);
ElementManagement<? extends OElement> erManagement = null;
switch(accessType) {
case PROPERTY:
erManagement = new PropertyTypeDefinitionManagement();
break;
case RESOURCE:
erManagement = new ResourceTypeDefinitionManagement();
break;
case FACET:
erManagement = new FacetTypeDefinitionManagement();
break;
case IS_RELATED_TO:
erManagement = new IsRelatedToTypeDefinitionManagement();
break;
case CONSISTS_OF:
erManagement = new ConsistsOfTypeDefinitionManagement();
break;
default:
break;
}
String ret = null;
if(erManagement!=null) {
erManagement.setJson(jsonSchema);
ret = erManagement.update();
}else {
ret = TypeMapper.serializeTypeDefinition(typeDefinition);
}
return ret;
} catch(SchemaException e) {
throw e;
} catch(Exception ex) {
throw new SchemaCreationException(ex);
}
}
@Override