diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/resources/impl/SchemaManagementImpl.java b/src/main/java/org/gcube/informationsystem/resourceregistry/resources/impl/SchemaManagementImpl.java index 01df5b7..b916732 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/resources/impl/SchemaManagementImpl.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/resources/impl/SchemaManagementImpl.java @@ -33,6 +33,7 @@ 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.OrientBaseGraph; import com.tinkerpop.blueprints.impls.orient.OrientElementType; import com.tinkerpop.blueprints.impls.orient.OrientGraphFactory; import com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx; @@ -49,13 +50,32 @@ public class SchemaManagementImpl implements SchemaManagement { private static Logger logger = LoggerFactory .getLogger(SchemaManagementImpl.class); - protected static OClass getEntityOClass(OrientGraphNoTx orientGraphNoTx, + protected static OClass getEntityOClass(OrientBaseGraph orientBaseGraph, String entityType) throws SchemaException { - OMetadata oMetadata = orientGraphNoTx.getRawGraph().getMetadata(); + OMetadata oMetadata = orientBaseGraph.getRawGraph().getMetadata(); OSchema oSchema = oMetadata.getSchema(); return oSchema.getClass(entityType); } + protected static OClass getTypeSchema(OrientBaseGraph orientBaseGraph, + String type, String baseType) throws SchemaNotFoundException { + try { + OClass oClass = getEntityOClass(orientBaseGraph, type); + if (baseType != null) { + if (baseType.compareTo(Embedded.NAME) != 0 + && !oClass.isSubClassOf(baseType)) { + throw new SchemaException("The requested type is not a " + + baseType); + } + } + return oClass; + } catch (SchemaNotFoundException snfe) { + throw snfe; + } catch (Exception e) { + throw new SchemaNotFoundException(e.getMessage()); + } + } + protected static OClass getTypeSchema(String type, String baseType) throws SchemaNotFoundException { OrientGraphFactory orientGraphFactory = SecurityContextMapper @@ -70,20 +90,8 @@ public class SchemaManagementImpl implements SchemaManagement { orientGraphNoTx = orientGraphFactory.getNoTx(); - OClass oClass = getEntityOClass(orientGraphNoTx, type); + return getTypeSchema(orientGraphNoTx, type, baseType); - if (baseType != null) { - if(baseType.compareTo(Embedded.NAME)!=0 && !oClass.isSubClassOf(baseType)) { - throw new SchemaException("The requested type is not a " - + baseType); - } - } - return oClass; - - } catch (SchemaNotFoundException snfe) { - throw snfe; - } catch (Exception e) { - throw new SchemaNotFoundException(e.getMessage()); } finally { if (orientGraphNoTx != null) { orientGraphNoTx.shutdown(); @@ -91,7 +99,8 @@ public class SchemaManagementImpl implements SchemaManagement { } } - protected static String serializeOClass(OClass oClass) throws SchemaException { + protected static String serializeOClass(OClass oClass) + throws SchemaException { ODocument oDocument = ((OClassImpl) oClass).toStream(); return oDocument.toJSON(); } @@ -139,8 +148,8 @@ public class SchemaManagementImpl implements SchemaManagement { } } - protected String registerTypeSchema(String jsonSchema, - Class baseType) throws SchemaException { + protected String registerTypeSchema(String jsonSchema, Class baseType) + throws SchemaException { OrientGraphFactory orientGraphFactory = SecurityContextMapper .getSecurityContextFactory( @@ -149,7 +158,8 @@ public class SchemaManagementImpl implements SchemaManagement { OrientGraphNoTx orientGraphNoTx = null; try { - logger.info("Trying to register {} {}", baseType.getSimpleName(), jsonSchema); + logger.info("Trying to register {} {}", baseType.getSimpleName(), + jsonSchema); ObjectMapper mapper = new ObjectMapper(); TypeDefinition typeDefinition = mapper.readValue(jsonSchema, @@ -158,19 +168,21 @@ public class SchemaManagementImpl implements SchemaManagement { orientGraphNoTx = orientGraphFactory.getNoTx(); 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)){ + + 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 { oClass.setAbstract(typeDefinition.isAbstractType()); @@ -180,10 +192,11 @@ public class SchemaManagementImpl implements SchemaManagement { typeDefinition.getName()); } - List oSuperclasses = getSuperclassesAndCheckCompliancy( - orientGraphNoTx, typeDefinition, - baseType==Embedded.class ? null : baseType.getSimpleName()); + orientGraphNoTx, + typeDefinition, + baseType == Embedded.class ? null : baseType + .getSimpleName()); oClass.setSuperClasses(oSuperclasses); if (Resource.class.isAssignableFrom(baseType)) { @@ -194,7 +207,8 @@ public class SchemaManagementImpl implements SchemaManagement { } } else { for (Property property : typeDefinition.getProperties()) { - OProperty op = oClass.createProperty(property.getName(), OType.getById(property.getType().byteValue())); + OProperty op = oClass.createProperty(property.getName(), + OType.getById(property.getType().byteValue())); op.setDescription(property.getDescription()); /* @@ -227,21 +241,23 @@ public class SchemaManagementImpl implements SchemaManagement { op.setLinkedClass(linkedClass); } else if (property.getLinkedType() != null) { - op.setLinkedType(OType.getById(property - .getLinkedType().byteValue())); + op.setLinkedType(OType.getById(property.getLinkedType() + .byteValue())); } } } - - OClass toBeSerializedOClass = oClass; - if(oClass instanceof OrientElementType){ - toBeSerializedOClass = getEntityOClass(orientGraphNoTx, typeDefinition.getName()); + + OClass toBeSerializedOClass = oClass; + if (oClass instanceof OrientElementType) { + toBeSerializedOClass = getEntityOClass(orientGraphNoTx, + typeDefinition.getName()); } - + String ret = serializeOClass(toBeSerializedOClass); - logger.info("{} type registered successfully: {}", baseType.getSimpleName(), jsonSchema); + logger.info("{} type registered successfully: {}", + baseType.getSimpleName(), jsonSchema); return ret; - + } catch (Exception e) { throw new SchemaException(e); } finally {