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);
}
}
}
}
Aggregations