Merged three functions in only one

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/resource-registry@134607 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2016-11-23 16:25:47 +00:00
parent e1d3c9d65d
commit a10c66578a
1 changed files with 38 additions and 252 deletions

View File

@ -33,11 +33,8 @@ import com.orientechnologies.orient.core.metadata.schema.OProperty;
import com.orientechnologies.orient.core.metadata.schema.OSchema;
import com.orientechnologies.orient.core.metadata.schema.OType;
import com.orientechnologies.orient.core.record.impl.ODocument;
import com.tinkerpop.blueprints.impls.orient.OrientEdgeType;
import com.tinkerpop.blueprints.impls.orient.OrientGraphFactory;
import com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx;
import com.tinkerpop.blueprints.impls.orient.OrientVertexType;
import com.tinkerpop.blueprints.impls.orient.OrientVertexType.OrientVertexProperty;
/**
* @author Luca Frosini (ISTI - CNR)
@ -58,20 +55,13 @@ public class SchemaManagementImpl implements SchemaManagement {
return oSchema.getClass(entityType);
}
protected OClass getTypeSchema(String type, String baseType)
protected OClass getTypeSchema(OrientGraphNoTx orientGraphNoTx, String type, String baseType)
throws SchemaNotFoundException {
OrientGraphFactory orientGraphFactory = SecurityContextMapper
.getSecurityContextFactory(
SecurityContextMapper.ADMIN_SECURITY_CONTEXT_UUID,
PermissionMode.READER);
OrientGraphNoTx orientGraphNoTx = null;
try {
logger.debug("Getting {} Type {} schema",
baseType != null ? baseType : "", type);
orientGraphNoTx = orientGraphFactory.getNoTx();
OClass oClass = getEntityOClass(orientGraphNoTx, type);
if (baseType != null) {
@ -139,7 +129,7 @@ public class SchemaManagementImpl implements SchemaManagement {
OrientGraphNoTx orientGraphNoTx = null;
try {
orientGraphNoTx = orientGraphFactory.getNoTx();
OClass oClass = getTypeSchema(type, baseType);
OClass oClass = getTypeSchema(orientGraphNoTx, type, baseType);
return serializeOClass(oClass);
} catch (Exception e) {
throw new SchemaNotFoundException(e);
@ -150,7 +140,7 @@ public class SchemaManagementImpl implements SchemaManagement {
}
}
protected String registerVertexTypeSchema(String jsonSchema,
protected String registerTypeSchema(String jsonSchema,
Class<?> baseType) throws SchemaException {
OrientGraphFactory orientGraphFactory = SecurityContextMapper
@ -168,20 +158,34 @@ public class SchemaManagementImpl implements SchemaManagement {
orientGraphNoTx = orientGraphFactory.getNoTx();
OrientVertexType ovt = orientGraphNoTx
.createVertexType(typeDefinition.getName());
ovt.setDescription(typeDefinition.getDescription());
OClass oClass = null;
if(Entity.class.isAssignableFrom(baseType)){
oClass = orientGraphNoTx.createVertexType(typeDefinition.getName());
}else if(Relation.class.isAssignableFrom(baseType)){
oClass = orientGraphNoTx.createEdgeType(typeDefinition.getName());
} else if(Embedded.class.isAssignableFrom(baseType)){
ODatabaseDocumentTx oDatabaseDocumentTx = orientGraphNoTx
.getRawGraph();
OMetadata oMetadata = oDatabaseDocumentTx.getMetadata();
OSchema oSchema = oMetadata.getSchema();
oClass = oSchema.createClass(typeDefinition.getName());
}
oClass.setDescription(typeDefinition.getDescription());
try {
ovt.setAbstract(typeDefinition.isAbstractType());
oClass.setAbstract(typeDefinition.isAbstractType());
} 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 abstarct.",
typeDefinition.getName());
}
List<OClass> oSuperclasses = getSuperclassesAndCheckCompliancy(
orientGraphNoTx, typeDefinition, baseType.getSimpleName());
ovt.setSuperClasses(oSuperclasses);
orientGraphNoTx, typeDefinition,
baseType==Embedded.class ? null : baseType.getSimpleName());
oClass.setSuperClasses(oSuperclasses);
if (Resource.class.isAssignableFrom(baseType)) {
Set<Property> properties = typeDefinition.getProperties();
@ -191,10 +195,8 @@ public class SchemaManagementImpl implements SchemaManagement {
}
} else {
for (Property property : typeDefinition.getProperties()) {
OrientVertexProperty ovp = ovt.createProperty(
property.getName(),
OType.getById(property.getType().byteValue()));
ovp.setDescription(property.getDescription());
OProperty op = oClass.createProperty(property.getName(), OType.getById(property.getType().byteValue()));
op.setDescription(property.getDescription());
/*
* Mandatory and notNull does not work in distributed mode:
@ -202,11 +204,11 @@ public class SchemaManagementImpl implements SchemaManagement {
* ovp.setMandatory(property.isMandatory());
* ovp.setNotNull(property.isNotnull());
*/
ovp.setMandatory(false);
ovp.setNotNull(false);
op.setMandatory(false);
op.setNotNull(false);
ovp.setReadonly(property.isReadonly());
ovp.setRegexp(property.getRegexpr());
op.setReadonly(property.isReadonly());
op.setRegexp(property.getRegexpr());
if (property.getLinkedClass() != null) {
OClass linkedClass = getEntityOClass(orientGraphNoTx,
property.getLinkedClass());
@ -224,97 +226,14 @@ public class SchemaManagementImpl implements SchemaManagement {
"An Embedded Field cannot be an Entity or a Relation");
}
ovp.setLinkedClass(linkedClass);
op.setLinkedClass(linkedClass);
} else if (property.getLinkedType() != null) {
ovp.setLinkedType(OType.getById(property
op.setLinkedType(OType.getById(property
.getLinkedType().byteValue()));
}
}
}
OClass oClass = getTypeSchema(typeDefinition.getName(), null);
String ret = serializeOClass(oClass);
logger.info("{} type registered successfully: {}", baseType.getSimpleName(), jsonSchema);
return ret;
} catch (Exception e) {
throw new SchemaException(e);
} finally {
if (orientGraphNoTx != null) {
orientGraphNoTx.shutdown();
}
}
}
protected String registerEdgeTypeSchema(String jsonSchema, String baseType)
throws SchemaException {
OrientGraphFactory orientGraphFactory = SecurityContextMapper
.getSecurityContextFactory(
SecurityContextMapper.ADMIN_SECURITY_CONTEXT_UUID,
PermissionMode.WRITER);
OrientGraphNoTx orientGraphNoTx = null;
try {
logger.info("Trying to register {} type : {}", baseType, jsonSchema);
ObjectMapper mapper = new ObjectMapper();
TypeDefinition typeDefinition = mapper.readValue(jsonSchema,
TypeDefinition.class);
orientGraphNoTx = orientGraphFactory.getNoTx();
OrientEdgeType oet = orientGraphNoTx.createEdgeType(typeDefinition
.getName());
oet.setDescription(typeDefinition.getDescription());
try {
oet.setAbstract(typeDefinition.isAbstractType());
} catch (Exception e) {
logger.error(
"Unable to set the Edge Type {} as abstract. This is an OrientDB <= 2.2.12 bug. The Type will be created as it is not abstarct.",
typeDefinition.getName());
}
List<OClass> oSuperclasses = getSuperclassesAndCheckCompliancy(
orientGraphNoTx, typeDefinition, baseType);
oet.setSuperClasses(oSuperclasses);
for (Property property : typeDefinition.getProperties()) {
OProperty op = oet.createProperty(property.getName(),
OType.getById(property.getType().byteValue()));
op.setDescription(property.getDescription());
/*
* Mandatory and notNull does not work in distributed mode: so
* that on Type declaration they are forced to false
* op.setMandatory(property.isMandatory());
* op.setNotNull(property.isNotnull());
*/
op.setMandatory(false);
op.setNotNull(false);
op.setReadonly(property.isReadonly());
op.setRegexp(property.getRegexpr());
if (property.getLinkedClass() != null) {
OClass linkedClass = getEntityOClass(orientGraphNoTx,
property.getLinkedClass());
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);
}
}
OClass oClass = getTypeSchema(typeDefinition.getName(), null);
String ret = serializeOClass(oClass);
logger.info("{} type registered successfully: {}", baseType, jsonSchema);
return ret;
@ -328,133 +247,10 @@ public class SchemaManagementImpl implements SchemaManagement {
}
}
public String registerDocumentSchema(String jsonSchema)
throws SchemaException {
OrientGraphFactory orientGraphFactory = SecurityContextMapper
.getSecurityContextFactory(
SecurityContextMapper.ADMIN_SECURITY_CONTEXT_UUID,
PermissionMode.WRITER);
OrientGraphNoTx orientGraphNoTx = null;
try {
logger.info("Trying to register {} type : {}", Embedded.NAME, jsonSchema);
ObjectMapper mapper = new ObjectMapper();
TypeDefinition typeDefinition = mapper.readValue(jsonSchema,
TypeDefinition.class);
orientGraphNoTx = orientGraphFactory.getNoTx();
ODatabaseDocumentTx oDatabaseDocumentTx = orientGraphNoTx
.getRawGraph();
OMetadata oMetadata = oDatabaseDocumentTx.getMetadata();
OSchema oSchema = oMetadata.getSchema();
OClass oClass = oSchema.createClass(typeDefinition.getName());
oClass.setDescription(typeDefinition.getDescription());
oClass.setAbstract(typeDefinition.isAbstractType());
List<OClass> oSuperclasses = getSuperclassesAndCheckCompliancy(
orientGraphNoTx, typeDefinition, null);
oClass.setSuperClasses(oSuperclasses);
oDatabaseDocumentTx.commit();
for (Property property : typeDefinition.getProperties()) {
OProperty ovp = oClass.createProperty(property.getName(),
OType.getById(property.getType().byteValue()));
ovp.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());
*/
ovp.setMandatory(false);
ovp.setNotNull(false);
ovp.setReadonly(property.isReadonly());
ovp.setRegexp(property.getRegexpr());
if (property.getLinkedClass() != null) {
OClass linkedClass = getEntityOClass(orientGraphNoTx,
property.getLinkedClass());
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");
}
ovp.setLinkedClass(linkedClass);
}
}
oDatabaseDocumentTx.commit();
String ret = serializeOClass(oClass);
logger.info("{} type registered successfully: {}", Embedded.NAME, jsonSchema);
return ret;
} catch (Exception e) {
throw new SchemaException(e);
} finally {
if (orientGraphNoTx != null) {
orientGraphNoTx.shutdown();
}
}
}
/*
* public void addDocumentProperties(TypeDefinition typeDefinition) throws
* SchemaException { OrientGraphFactory orientGraphFactory =
* SecurityContextMapper .getSecurityContextFactory(null,
* PermissionMode.WRITER);
*
* OrientGraphNoTx orientGraphNoTx = null; try {
* logger.trace("Adding properties to {}", typeDefinition);
*
* OClass oClass = getEntityOClass(orientGraphNoTx,
* typeDefinition.getName());
*
* orientGraphNoTx = orientGraphFactory.getNoTx();
*
* for (Property property : typeDefinition.getProperties()) { OProperty ovp
* = oClass.createProperty(property.getName(),
* OType.getById(property.getType().byteValue()));
* ovp.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()); / ovp.setMandatory(false);
* ovp.setNotNull(false);
*
* ovp.setReadonly(property.isReadonly());
* ovp.setRegexp(property.getRegexpr()); if (property.getLinkedClass() !=
* null) { OClass linkedClass = getEntityOClass(orientGraphNoTx,
* property.getLinkedClass()); 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"); }
*
* ovp.setLinkedClass(linkedClass); } } } catch (Exception e) { throw new
* SchemaException(e); } finally { if (orientGraphNoTx != null) {
* orientGraphNoTx.shutdown(); } } }
*/
@Override
public String registerEntitySchema(String jsonSchema)
throws SchemaException {
return registerVertexTypeSchema(jsonSchema, Entity.class);
return registerTypeSchema(jsonSchema, Entity.class);
}
@Override
@ -477,7 +273,7 @@ public class SchemaManagementImpl implements SchemaManagement {
@Override
public String registerFacetSchema(String jsonSchema) throws SchemaException {
return registerVertexTypeSchema(jsonSchema, Facet.class);
return registerTypeSchema(jsonSchema, Facet.class);
}
@Override
@ -501,7 +297,7 @@ public class SchemaManagementImpl implements SchemaManagement {
@Override
public String registerResourceSchema(String jsonSchema)
throws SchemaException {
return registerVertexTypeSchema(jsonSchema, Resource.class);
return registerTypeSchema(jsonSchema, Resource.class);
}
@Override
@ -525,17 +321,7 @@ public class SchemaManagementImpl implements SchemaManagement {
@Override
public String registerEmbeddedTypeSchema(String jsonSchema)
throws SchemaException {
return registerDocumentSchema(jsonSchema);
/*
* ObjectMapper mapper = new ObjectMapper(); TypeDefinition
* typeDefinition; try { typeDefinition = mapper.readValue(jsonSchema,
* TypeDefinition.class); registerDocumentSchema(typeDefinition);
* addDocumentProperties(typeDefinition);
*
* return jsonSchema; } catch (SchemaException e) { throw e; } catch
* (Exception e) { throw new SchemaException(e); }
*/
return registerTypeSchema(jsonSchema, Embedded.class);
}
@Override
@ -559,7 +345,7 @@ public class SchemaManagementImpl implements SchemaManagement {
@Override
public String registerRelationSchema(String jsonSchema)
throws SchemaException {
return registerEdgeTypeSchema(jsonSchema, Relation.NAME);
return registerTypeSchema(jsonSchema, Relation.class);
}
@Override
@ -583,7 +369,7 @@ public class SchemaManagementImpl implements SchemaManagement {
@Override
public String registerConsistOfSchema(String jsonSchema)
throws SchemaException {
return registerEdgeTypeSchema(jsonSchema, ConsistsOf.NAME);
return registerTypeSchema(jsonSchema, ConsistsOf.class);
}
@Override
@ -607,7 +393,7 @@ public class SchemaManagementImpl implements SchemaManagement {
@Override
public String registerRelatedToSchema(String jsonSchema)
throws SchemaException {
return registerEdgeTypeSchema(jsonSchema, IsRelatedTo.NAME);
return registerTypeSchema(jsonSchema, IsRelatedTo.class);
}
@Override