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.entity.Resource;
|
||||||
import org.gcube.informationsystem.model.relation.Relation;
|
import org.gcube.informationsystem.model.relation.Relation;
|
||||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
|
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.SchemaException;
|
||||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaNotFoundException;
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaNotFoundException;
|
||||||
import org.gcube.informationsystem.resourceregistry.context.SecurityContextMapper;
|
import org.gcube.informationsystem.resourceregistry.context.SecurityContextMapper;
|
||||||
|
@ -27,6 +28,7 @@ import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
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.OMetadata;
|
||||||
import com.orientechnologies.orient.core.metadata.schema.OClass;
|
import com.orientechnologies.orient.core.metadata.schema.OClass;
|
||||||
import com.orientechnologies.orient.core.metadata.schema.OClassImpl;
|
import com.orientechnologies.orient.core.metadata.schema.OClassImpl;
|
||||||
|
@ -149,6 +151,9 @@ public class SchemaManagementImpl implements SchemaManagement {
|
||||||
List<OClass> oSuperclasses = new ArrayList<>();
|
List<OClass> oSuperclasses = new ArrayList<>();
|
||||||
for (String superClass : superClasses) {
|
for (String superClass : superClasses) {
|
||||||
OClass oSuperClass = getOClass(oSchema, superClass);
|
OClass oSuperClass = getOClass(oSchema, superClass);
|
||||||
|
if(oSuperClass==null){
|
||||||
|
throw new SchemaNotFoundException("Superclass "+ superClass + " does not exists");
|
||||||
|
}
|
||||||
if (baseType != null) {
|
if (baseType != null) {
|
||||||
if (typeDefinition.getName().compareTo(baseType) != 0) {
|
if (typeDefinition.getName().compareTo(baseType) != 0) {
|
||||||
if (!oSuperClass.isSubClassOf(baseType)) {
|
if (!oSuperClass.isSubClassOf(baseType)) {
|
||||||
|
@ -212,110 +217,122 @@ public class SchemaManagementImpl implements SchemaManagement {
|
||||||
throw new ResourceRegistryException(error);
|
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 {
|
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) {
|
if (typeDefinition.getDescription() != null) {
|
||||||
List<OClass> oSuperclasses = getSuperclassesAndCheckCompliancy(
|
try {
|
||||||
orientGraphNoTx, typeDefinition, baseType.getName());
|
oClass.setDescription(typeDefinition.getDescription());
|
||||||
oClass.setSuperClasses(oSuperclasses);
|
}catch (Exception e) {
|
||||||
}
|
logger.warn("Unable to set description. This is an orient bug. See https://github.com/orientechnologies/orientdb/issues/7065");
|
||||||
|
|
||||||
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);
|
try {
|
||||||
op.setDescription(property.getDescription());
|
// 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) {
|
||||||
* Mandatory and notNull does not work in distributed mode:
|
List<OClass> oSuperclasses = getSuperclassesAndCheckCompliancy(
|
||||||
* so that on Type declaration they are forced to false
|
orientGraphNoTx, typeDefinition, baseType.getName());
|
||||||
* ovp.setMandatory(property.isMandatory());
|
oClass.setSuperClasses(oSuperclasses);
|
||||||
* ovp.setNotNull(property.isNotnull());
|
}
|
||||||
*
|
|
||||||
* This information are persisted in Management Context
|
|
||||||
*/
|
|
||||||
op.setMandatory(false);
|
|
||||||
op.setNotNull(false);
|
|
||||||
|
|
||||||
op.setReadonly(property.isReadonly());
|
if (Resource.class.isAssignableFrom(baseType.getTypeClass())) {
|
||||||
op.setRegexp(property.getRegexp());
|
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()) {
|
||||||
|
|
||||||
if (property.getLinkedClass() != null) {
|
OType oType = OType.getById(property.getType().byteValue());
|
||||||
OClass linkedClass = getOClass(oSchema,
|
switch (oType) {
|
||||||
property.getLinkedClass());
|
case EMBEDDEDLIST:
|
||||||
if (linkedClass == null) {
|
throw new UnsupportedDataTypeException(oType.name() + " support is currently disabled due to OrientDB bug see https://github.com/orientechnologies/orientdb/issues/7354");
|
||||||
logger.trace("class {} not found in schema",
|
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());
|
property.getLinkedClass());
|
||||||
throw new Exception("class "
|
if (linkedClass == null) {
|
||||||
+ property.getLinkedClass()
|
logger.trace("class {} not found in schema",
|
||||||
+ " not found in schema");
|
property.getLinkedClass());
|
||||||
}
|
throw new Exception("class "
|
||||||
|
+ property.getLinkedClass()
|
||||||
|
+ " not found in schema");
|
||||||
|
}
|
||||||
|
|
||||||
if (linkedClass.isEdgeType()
|
if (linkedClass.isEdgeType()
|
||||||
|| linkedClass.isVertexType()) {
|
|| linkedClass.isVertexType()) {
|
||||||
throw new Exception(
|
throw new Exception(
|
||||||
"An Embedded Field cannot be an Entity or a Relation");
|
"An Embedded Field cannot be an Entity or a Relation");
|
||||||
}
|
}
|
||||||
|
|
||||||
op.setLinkedClass(linkedClass);
|
op.setLinkedClass(linkedClass);
|
||||||
} else if (property.getLinkedType() != null) {
|
} else if (property.getLinkedType() != null) {
|
||||||
op.setLinkedType(OType.getById(property.getLinkedType()
|
op.setLinkedType(OType.getById(property.getLinkedType()
|
||||||
.byteValue()));
|
.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;
|
||||||
}
|
}
|
||||||
|
} catch (OSchemaException ex) {
|
||||||
OClass toBeSerializedOClass = oClass;
|
if(ex.getMessage().contains("already exists")){
|
||||||
if (oClass instanceof OrientElementType) {
|
throw new SchemaAlreadyPresentException(ex);
|
||||||
toBeSerializedOClass = getOClass(oSchema,
|
|
||||||
typeDefinition.getName());
|
|
||||||
}
|
}
|
||||||
|
throw new SchemaException(ex);
|
||||||
/*
|
} catch (SchemaException e) {
|
||||||
SchemaContextManagement managementUtility = new SchemaContextManagement();
|
throw e;
|
||||||
String ret = managementUtility.create(jsonSchema, baseType);
|
} catch (Exception ex) {
|
||||||
*/
|
throw new SchemaException(ex);
|
||||||
|
|
||||||
// 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);
|
|
||||||
} finally {
|
} finally {
|
||||||
if (orientGraphNoTx != null) {
|
if (orientGraphNoTx != null) {
|
||||||
orientGraphNoTx.shutdown();
|
orientGraphNoTx.shutdown();
|
||||||
|
|
Loading…
Reference in New Issue