Search in sources :

Example 6 with ValidationException

use of eu.esdihumboldt.hale.common.instance.extension.validation.ValidationException 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

ValidationException (eu.esdihumboldt.hale.common.instance.extension.validation.ValidationException)6 QName (javax.xml.namespace.QName)4 DefaultInstanceValidationMessage (eu.esdihumboldt.hale.common.instance.extension.validation.report.impl.DefaultInstanceValidationMessage)3 GroupPropertyConstraintValidator (eu.esdihumboldt.hale.common.instance.extension.validation.GroupPropertyConstraintValidator)2 TypeEntityDefinition (eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition)1 CodeList (eu.esdihumboldt.hale.common.codelist.CodeList)1 CodeEntry (eu.esdihumboldt.hale.common.codelist.CodeList.CodeEntry)1 CodeListAssociations (eu.esdihumboldt.hale.common.codelist.config.CodeListAssociations)1 CodeListReference (eu.esdihumboldt.hale.common.codelist.config.CodeListReference)1 InstanceValidationContext (eu.esdihumboldt.hale.common.instance.extension.validation.InstanceValidationContext)1 PropertyConstraintValidator (eu.esdihumboldt.hale.common.instance.extension.validation.PropertyConstraintValidator)1 TypeConstraintValidator (eu.esdihumboldt.hale.common.instance.extension.validation.TypeConstraintValidator)1 ValidationLocation (eu.esdihumboldt.hale.common.instance.extension.validation.ValidationLocation)1 InstanceValidationReporter (eu.esdihumboldt.hale.common.instance.extension.validation.report.InstanceValidationReporter)1 DefaultInstanceValidationReporter (eu.esdihumboldt.hale.common.instance.extension.validation.report.impl.DefaultInstanceValidationReporter)1 Group (eu.esdihumboldt.hale.common.instance.model.Group)1 Instance (eu.esdihumboldt.hale.common.instance.model.Instance)1 MutableInstance (eu.esdihumboldt.hale.common.instance.model.MutableInstance)1 DefaultInstance (eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstance)1 GroupPropertyConstraint (eu.esdihumboldt.hale.common.schema.model.GroupPropertyConstraint)1