Search in sources :

Example 1 with AttributeValidator

use of ddf.catalog.validation.AttributeValidator in project ddf by codice.

the class ReportingMetacardValidatorImpl method validateMetacard.

@Override
public Optional<MetacardValidationReport> validateMetacard(final Metacard metacard) {
    Preconditions.checkArgument(metacard != null, "The metacard cannot be null.");
    final Set<ValidationViolation> violations = new HashSet<>();
    for (final AttributeDescriptor descriptor : metacard.getMetacardType().getAttributeDescriptors()) {
        final String attributeName = descriptor.getName();
        final Attribute attribute = metacard.getAttribute(attributeName);
        if (attribute != null) {
            for (final AttributeValidator validator : validatorRegistry.getValidators(attributeName)) {
                validator.validate(attribute).ifPresent(report -> violations.addAll(report.getAttributeValidationViolations()));
            }
        }
    }
    if (violations.size() > 0) {
        return getReport(violations);
    }
    return Optional.empty();
}
Also used : ValidationViolation(ddf.catalog.validation.violation.ValidationViolation) Attribute(ddf.catalog.data.Attribute) AttributeDescriptor(ddf.catalog.data.AttributeDescriptor) AttributeValidator(ddf.catalog.validation.AttributeValidator) HashSet(java.util.HashSet)

Example 2 with AttributeValidator

use of ddf.catalog.validation.AttributeValidator in project ddf by codice.

the class Validator method validateAttribute.

public AttributeValidationResponse validateAttribute(String attribute, String value) {
    Set<AttributeValidator> validators = attributeValidatorRegistry.stream().map(avr -> avr.getValidators(attribute)).reduce((left, right) -> {
        left.addAll(right);
        return left;
    }).orElse(new HashSet<>());
    Set<String> suggestedValues = new HashSet<>();
    Set<ValidationViolation> violations = new HashSet<>();
    for (AttributeValidator validator : validators) {
        Optional<AttributeValidationReport> validationReport = validator.validate(new AttributeImpl(attribute, value));
        if (validationReport.isPresent()) {
            AttributeValidationReport report = validationReport.get();
            if (!report.getSuggestedValues().isEmpty()) {
                suggestedValues = report.getSuggestedValues();
            }
            violations.addAll(report.getAttributeValidationViolations());
        }
    }
    return new AttributeValidationResponse(violations, suggestedValues);
}
Also used : MetacardValidationReport(ddf.catalog.validation.report.MetacardValidationReport) SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) AttributeImpl(ddf.catalog.data.impl.AttributeImpl) Set(java.util.Set) HashMap(java.util.HashMap) AttributeValidatorRegistry(ddf.catalog.validation.AttributeValidatorRegistry) FederationException(ddf.catalog.federation.FederationException) Collectors(java.util.stream.Collectors) AttributeValidationReport(ddf.catalog.validation.report.AttributeValidationReport) ArrayList(java.util.ArrayList) ReportingMetacardValidator(ddf.catalog.validation.ReportingMetacardValidator) ValidationViolation(ddf.catalog.validation.violation.ValidationViolation) HashSet(java.util.HashSet) AttributeValidator(ddf.catalog.validation.AttributeValidator) List(java.util.List) Metacard(ddf.catalog.data.Metacard) Map(java.util.Map) Optional(java.util.Optional) AttributeImpl(ddf.catalog.data.impl.AttributeImpl) AttributeValidationReport(ddf.catalog.validation.report.AttributeValidationReport) ValidationViolation(ddf.catalog.validation.violation.ValidationViolation) AttributeValidator(ddf.catalog.validation.AttributeValidator) HashSet(java.util.HashSet)

Aggregations

AttributeValidator (ddf.catalog.validation.AttributeValidator)2 ValidationViolation (ddf.catalog.validation.violation.ValidationViolation)2 HashSet (java.util.HashSet)2 Attribute (ddf.catalog.data.Attribute)1 AttributeDescriptor (ddf.catalog.data.AttributeDescriptor)1 Metacard (ddf.catalog.data.Metacard)1 AttributeImpl (ddf.catalog.data.impl.AttributeImpl)1 FederationException (ddf.catalog.federation.FederationException)1 SourceUnavailableException (ddf.catalog.source.SourceUnavailableException)1 UnsupportedQueryException (ddf.catalog.source.UnsupportedQueryException)1 AttributeValidatorRegistry (ddf.catalog.validation.AttributeValidatorRegistry)1 ReportingMetacardValidator (ddf.catalog.validation.ReportingMetacardValidator)1 AttributeValidationReport (ddf.catalog.validation.report.AttributeValidationReport)1 MetacardValidationReport (ddf.catalog.validation.report.MetacardValidationReport)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Set (java.util.Set)1