diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/entities/FacetManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/entities/FacetManagement.java index a650ba4..b3881de 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/entities/FacetManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/entities/FacetManagement.java @@ -1,5 +1,7 @@ package org.gcube.informationsystem.resourceregistry.instances.model.entities; +import java.util.Set; + import org.gcube.com.fasterxml.jackson.databind.JsonNode; import org.gcube.informationsystem.base.reference.AccessType; import org.gcube.informationsystem.model.reference.entities.Facet; @@ -8,6 +10,11 @@ import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegis import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.facet.FacetAlreadyPresentException; import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.facet.FacetAvailableInAnotherContextException; import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.facet.FacetNotFoundException; +import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaViolationException; +import org.gcube.informationsystem.resourceregistry.types.CachedType; +import org.gcube.informationsystem.resourceregistry.types.TypesCache; +import org.gcube.informationsystem.types.reference.entities.FacetType; +import org.gcube.informationsystem.types.reference.properties.PropertyDefinition; import com.orientechnologies.orient.core.record.OVertex; @@ -58,10 +65,62 @@ public class FacetManagement extends EntityManagement { getElement().delete(); return true; } - + + private String getNotNullErrorMessage(String fieldName) { + StringBuffer stringBuffer = new StringBuffer(); + stringBuffer.append("The type "); + stringBuffer.append(typeName); + stringBuffer.append(" defines the fields "); + stringBuffer.append(fieldName); + stringBuffer.append(" as not nullable. Null or no value has been provided instead."); + return stringBuffer.toString(); + } + + private String getMandatoryErrorMessage(String fieldName) { + StringBuffer stringBuffer = new StringBuffer(); + stringBuffer.append("The type "); + stringBuffer.append(typeName); + stringBuffer.append(" defines the fields "); + stringBuffer.append(fieldName); + stringBuffer.append(" as mandatory but no value has been provided."); + return stringBuffer.toString(); + } + @Override - public void sanityCheck() throws ResourceRegistryException { - // TODO Auto-generated method stub + public void sanityCheck() throws SchemaViolationException, ResourceRegistryException { + // OrientDB distributed mode does not support + // mandatory and notnull constraints due to technical problem + // https://www.orientdb.com/docs/last/java/Graph-Schema-Property.html#using-constraints + // Going to validate them here + + JsonNode facetInstance = createCompleteJsonNode(); + + TypesCache typesCache = TypesCache.getInstance(); + CachedType cachedType = typesCache.getCachedType(typeName); + FacetType facetType = (FacetType) cachedType.getType(); + Set definedProperties = facetType.getProperties(); + for(PropertyDefinition propertyDefinition : definedProperties) { + String fieldName = propertyDefinition.getName(); + + if(propertyDefinition.isMandatory() && !facetInstance.has(fieldName)) { + if(propertyDefinition.isNotnull()) { + // If the field is mandatory but null value is accepted I add the + // field as null value + element.setProperty(fieldName, null); + } else { + throw new SchemaViolationException(getMandatoryErrorMessage(fieldName)); + } + } + + JsonNode jsonNode = facetInstance.get(fieldName); + + if(!propertyDefinition.isNotnull() && jsonNode==null) { + throw new SchemaViolationException(getNotNullErrorMessage(fieldName)); + } + + // leaving the rest of validation to OrientDB + + } }