Search in sources :

Example 1 with Unique

use of eu.esdihumboldt.hale.common.schema.model.constraint.property.Unique in project hale by halestudio.

the class XLinkReferenceValidator method validatePropertyConstraint.

@Override
public void validatePropertyConstraint(Object[] values, PropertyConstraint constraint, PropertyDefinition property, InstanceValidationContext context, ValidationLocation location) throws ValidationException {
    if (values == null) {
        return;
    }
    Object contextObj = context.getContext(XLinkReferenceValidator.class);
    XLinkReferenceContext ctx;
    if (contextObj instanceof XLinkReferenceContext) {
        ctx = (XLinkReferenceContext) contextObj;
    } else {
        ctx = new XLinkReferenceContext();
        context.putContext(XLinkReferenceValidator.class, ctx);
    }
    // collect local references
    Reference ref = property.getConstraint(Reference.class);
    if (ref instanceof XLinkReference && ref.isReference()) {
        for (Object value : values) {
            if (value != null) {
                String id = value.toString();
                if (id != null && id.startsWith("#")) {
                    ctx.addLocalReference(id.substring(1), location);
                }
            }
        }
    }
    // collect XML IDs
    Unique unique = property.getConstraint(Unique.class);
    if (unique instanceof XmlIdUnique && unique.isEnabled()) {
        for (Object value : values) {
            addIdentifier(value, ctx);
        }
    }
}
Also used : XmlIdUnique(eu.esdihumboldt.hale.io.xsd.constraint.XmlIdUnique) XLinkReference(eu.esdihumboldt.hale.io.xsd.reader.internal.constraint.XLinkReference) Reference(eu.esdihumboldt.hale.common.schema.model.constraint.property.Reference) XmlIdUnique(eu.esdihumboldt.hale.io.xsd.constraint.XmlIdUnique) Unique(eu.esdihumboldt.hale.common.schema.model.constraint.property.Unique) XLinkReference(eu.esdihumboldt.hale.io.xsd.reader.internal.constraint.XLinkReference)

Example 2 with Unique

use of eu.esdihumboldt.hale.common.schema.model.constraint.property.Unique in project hale by halestudio.

the class UniqueValidator method validatePropertyConstraint.

@Override
public void validatePropertyConstraint(Object[] values, PropertyConstraint constraint, PropertyDefinition property, InstanceValidationContext context, ValidationLocation location) throws ValidationException {
    Unique unique = (Unique) constraint;
    if (unique.isEnabled() && values != null) {
        for (Object value : values) {
            // only check it if it isn't null
            if (value != null) {
                @SuppressWarnings("unchecked") Map<String, Set<Object>> map = (Map<String, Set<Object>>) context.getContext(UniqueValidator.class);
                if (map == null) {
                    map = new HashMap<String, Set<Object>>();
                    context.putContext(UniqueValidator.class, map);
                }
                Set<Object> valueSet = map.get(unique.getIdentifier());
                if (valueSet == null) {
                    valueSet = new HashSet<Object>();
                    map.put(unique.getIdentifier(), valueSet);
                }
                if (valueSet.contains(value))
                    throw new ValidationException("The property " + property.getDisplayName() + " is marked as unique but the value (" + value + ") occurs multiple times.");
                else
                    valueSet.add(value);
            }
        }
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) ValidationException(eu.esdihumboldt.hale.common.instance.extension.validation.ValidationException) Unique(eu.esdihumboldt.hale.common.schema.model.constraint.property.Unique) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

Unique (eu.esdihumboldt.hale.common.schema.model.constraint.property.Unique)2 ValidationException (eu.esdihumboldt.hale.common.instance.extension.validation.ValidationException)1 Reference (eu.esdihumboldt.hale.common.schema.model.constraint.property.Reference)1 XmlIdUnique (eu.esdihumboldt.hale.io.xsd.constraint.XmlIdUnique)1 XLinkReference (eu.esdihumboldt.hale.io.xsd.reader.internal.constraint.XLinkReference)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Set (java.util.Set)1