Refs #10207: Improve management of SchemaException
Task-Url: https://support.d4science.org/issues/10207 git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/resource-registry@158163 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
394f39918d
commit
3fd72527ed
|
@ -16,6 +16,7 @@ import org.gcube.informationsystem.model.entity.Entity;
|
|||
import org.gcube.informationsystem.model.entity.Resource;
|
||||
import org.gcube.informationsystem.model.relation.Relation;
|
||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
|
||||
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.resourceregistry.context.SecurityContextMapper;
|
||||
|
@ -27,6 +28,7 @@ import org.slf4j.Logger;
|
|||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.orientechnologies.orient.core.exception.OSchemaException;
|
||||
import com.orientechnologies.orient.core.metadata.OMetadata;
|
||||
import com.orientechnologies.orient.core.metadata.schema.OClass;
|
||||
import com.orientechnologies.orient.core.metadata.schema.OClassImpl;
|
||||
|
@ -149,6 +151,9 @@ public class SchemaManagementImpl implements SchemaManagement {
|
|||
List<OClass> oSuperclasses = new ArrayList<>();
|
||||
for (String superClass : superClasses) {
|
||||
OClass oSuperClass = getOClass(oSchema, superClass);
|
||||
if(oSuperClass==null){
|
||||
throw new SchemaNotFoundException("Superclass "+ superClass + " does not exists");
|
||||
}
|
||||
if (baseType != null) {
|
||||
if (typeDefinition.getName().compareTo(baseType) != 0) {
|
||||
if (!oSuperClass.isSubClassOf(baseType)) {
|
||||
|
@ -187,7 +192,7 @@ public class SchemaManagementImpl implements SchemaManagement {
|
|||
OSchema oSchema = oMetadata.getSchema();
|
||||
|
||||
OClass oClass = null;
|
||||
|
||||
|
||||
if (Entity.class.isAssignableFrom(baseType.getTypeClass())) {
|
||||
oClass = orientGraphNoTx.createVertexType(typeDefinition
|
||||
.getName());
|
||||
|
@ -211,111 +216,123 @@ public class SchemaManagementImpl implements SchemaManagement {
|
|||
Entity.NAME, Relation.NAME, Embedded.NAME);
|
||||
throw new ResourceRegistryException(error);
|
||||
}
|
||||
|
||||
if (typeDefinition.getDescription() != null) {
|
||||
try {
|
||||
oClass.setDescription(typeDefinition.getDescription());
|
||||
}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(typeDefinition.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.",
|
||||
typeDefinition.getName());
|
||||
}
|
||||
|
||||
if (typeDefinition.getName().compareTo(Embedded.NAME) != 0) {
|
||||
List<OClass> oSuperclasses = getSuperclassesAndCheckCompliancy(
|
||||
orientGraphNoTx, typeDefinition, baseType.getName());
|
||||
oClass.setSuperClasses(oSuperclasses);
|
||||
}
|
||||
|
||||
if (Resource.class.isAssignableFrom(baseType.getTypeClass())) {
|
||||
Set<Property> properties = typeDefinition.getProperties();
|
||||
if (properties != null && properties.size() > 0) {
|
||||
throw new Exception(
|
||||
"A Resource cannot contains any properties.");
|
||||
}
|
||||
} else {
|
||||
for (Property property : typeDefinition.getProperties()) {
|
||||
|
||||
OType oType = OType.getById(property.getType().byteValue());
|
||||
switch (oType) {
|
||||
case EMBEDDEDLIST:
|
||||
throw new UnsupportedDataTypeException(oType.name() + " support is currently disabled due to OrientDB bug see https://github.com/orientechnologies/orientdb/issues/7354");
|
||||
case EMBEDDEDSET:
|
||||
throw new UnsupportedDataTypeException(oType.name() + " support is currently disabled due to OrientDB bug see https://github.com/orientechnologies/orientdb/issues/7354");
|
||||
default:
|
||||
break;
|
||||
|
||||
if (typeDefinition.getDescription() != null) {
|
||||
try {
|
||||
oClass.setDescription(typeDefinition.getDescription());
|
||||
}catch (Exception e) {
|
||||
logger.warn("Unable to set description. This is an orient bug. See https://github.com/orientechnologies/orientdb/issues/7065");
|
||||
}
|
||||
|
||||
OProperty op = oClass.createProperty(property.getName(), oType);
|
||||
op.setDescription(property.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(property.isReadonly());
|
||||
op.setRegexp(property.getRegexp());
|
||||
|
||||
if (property.getLinkedClass() != null) {
|
||||
OClass linkedClass = getOClass(oSchema,
|
||||
property.getLinkedClass());
|
||||
if (linkedClass == null) {
|
||||
logger.trace("class {} not found in schema",
|
||||
}
|
||||
|
||||
try {
|
||||
// oClass.setAbstract(false); // Used to allow to persist Schema in Context Management
|
||||
oClass.setAbstract(typeDefinition.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.",
|
||||
typeDefinition.getName());
|
||||
}
|
||||
|
||||
if (typeDefinition.getName().compareTo(Embedded.NAME) != 0) {
|
||||
List<OClass> oSuperclasses = getSuperclassesAndCheckCompliancy(
|
||||
orientGraphNoTx, typeDefinition, baseType.getName());
|
||||
oClass.setSuperClasses(oSuperclasses);
|
||||
}
|
||||
|
||||
if (Resource.class.isAssignableFrom(baseType.getTypeClass())) {
|
||||
Set<Property> properties = typeDefinition.getProperties();
|
||||
if (properties != null && properties.size() > 0) {
|
||||
throw new Exception(
|
||||
"A Resource cannot contains any properties.");
|
||||
}
|
||||
} else {
|
||||
for (Property property : typeDefinition.getProperties()) {
|
||||
|
||||
OType oType = OType.getById(property.getType().byteValue());
|
||||
switch (oType) {
|
||||
case EMBEDDEDLIST:
|
||||
throw new UnsupportedDataTypeException(oType.name() + " support is currently disabled due to OrientDB bug see https://github.com/orientechnologies/orientdb/issues/7354");
|
||||
case EMBEDDEDSET:
|
||||
throw new UnsupportedDataTypeException(oType.name() + " support is currently disabled due to OrientDB bug see https://github.com/orientechnologies/orientdb/issues/7354");
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
OProperty op = oClass.createProperty(property.getName(), oType);
|
||||
op.setDescription(property.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(property.isReadonly());
|
||||
op.setRegexp(property.getRegexp());
|
||||
|
||||
if (property.getLinkedClass() != null) {
|
||||
OClass linkedClass = getOClass(oSchema,
|
||||
property.getLinkedClass());
|
||||
throw new Exception("class "
|
||||
+ property.getLinkedClass()
|
||||
+ " not found in schema");
|
||||
if (linkedClass == null) {
|
||||
logger.trace("class {} not found in schema",
|
||||
property.getLinkedClass());
|
||||
throw new Exception("class "
|
||||
+ property.getLinkedClass()
|
||||
+ " not found in schema");
|
||||
}
|
||||
|
||||
if (linkedClass.isEdgeType()
|
||||
|| linkedClass.isVertexType()) {
|
||||
throw new Exception(
|
||||
"An Embedded Field cannot be an Entity or a Relation");
|
||||
}
|
||||
|
||||
op.setLinkedClass(linkedClass);
|
||||
} else if (property.getLinkedType() != null) {
|
||||
op.setLinkedType(OType.getById(property.getLinkedType()
|
||||
.byteValue()));
|
||||
}
|
||||
|
||||
if (linkedClass.isEdgeType()
|
||||
|| linkedClass.isVertexType()) {
|
||||
throw new Exception(
|
||||
"An Embedded Field cannot be an Entity or a Relation");
|
||||
}
|
||||
|
||||
op.setLinkedClass(linkedClass);
|
||||
} else if (property.getLinkedType() != null) {
|
||||
op.setLinkedType(OType.getById(property.getLinkedType()
|
||||
.byteValue()));
|
||||
}
|
||||
}
|
||||
|
||||
OClass toBeSerializedOClass = oClass;
|
||||
if (oClass instanceof OrientElementType) {
|
||||
toBeSerializedOClass = getOClass(oSchema,
|
||||
typeDefinition.getName());
|
||||
}
|
||||
|
||||
/*
|
||||
SchemaContextManagement managementUtility = new SchemaContextManagement();
|
||||
String ret = managementUtility.create(jsonSchema, baseType);
|
||||
*/
|
||||
|
||||
// TODO Remove when the previous has been implemented
|
||||
String ret = getTypeDefinitionAsString(toBeSerializedOClass);
|
||||
|
||||
logger.info("{} type registered successfully: {}",
|
||||
baseType.getName(), jsonSchema);
|
||||
return ret;
|
||||
}catch (Exception e) {
|
||||
oSchema. dropClass(typeDefinition.getName());
|
||||
throw e;
|
||||
}
|
||||
|
||||
OClass toBeSerializedOClass = oClass;
|
||||
if (oClass instanceof OrientElementType) {
|
||||
toBeSerializedOClass = getOClass(oSchema,
|
||||
typeDefinition.getName());
|
||||
} catch (OSchemaException ex) {
|
||||
if(ex.getMessage().contains("already exists")){
|
||||
throw new SchemaAlreadyPresentException(ex);
|
||||
}
|
||||
|
||||
/*
|
||||
SchemaContextManagement managementUtility = new SchemaContextManagement();
|
||||
String ret = managementUtility.create(jsonSchema, baseType);
|
||||
*/
|
||||
|
||||
// TODO Remove when the previous has been implemented
|
||||
String ret = getTypeDefinitionAsString(toBeSerializedOClass);
|
||||
|
||||
logger.info("{} type registered successfully: {}",
|
||||
baseType.getName(), jsonSchema);
|
||||
return ret;
|
||||
|
||||
} catch (Exception e) {
|
||||
throw new SchemaException(e);
|
||||
throw new SchemaException(ex);
|
||||
} catch (SchemaException e) {
|
||||
throw e;
|
||||
} catch (Exception ex) {
|
||||
throw new SchemaException(ex);
|
||||
} finally {
|
||||
if (orientGraphNoTx != null) {
|
||||
orientGraphNoTx.shutdown();
|
||||
|
|
Loading…
Reference in New Issue