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