From 679c0a79104303baaa1999342aeb2b4f8f9a55f1 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Mon, 14 Dec 2020 12:34:21 +0100 Subject: [PATCH] Creating update method ofr type --- .../types/SchemaManagement.java | 3 +- .../types/SchemaManagementImpl.java | 200 +++++++++++++++++- 2 files changed, 199 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/types/SchemaManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/types/SchemaManagement.java index b560c21..f1b0263 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/types/SchemaManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/types/SchemaManagement.java @@ -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; diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/types/SchemaManagementImpl.java b/src/main/java/org/gcube/informationsystem/resourceregistry/types/SchemaManagementImpl.java index cb8edc5..46960c3 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/types/SchemaManagementImpl.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/types/SchemaManagementImpl.java @@ -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 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 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 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