Search in sources :

Example 1 with RequiredAttributesMetacardValidator

use of ddf.catalog.validation.impl.validator.RequiredAttributesMetacardValidator in project ddf by codice.

the class ValidationParser method parseMetacardTypes.

private List<Callable<Boolean>> parseMetacardTypes(Changeset changeset, List<Outer.MetacardType> metacardTypes) {
    List<Callable<Boolean>> staged = new ArrayList<>();
    BundleContext context = getBundleContext();
    for (Outer.MetacardType metacardType : metacardTypes) {
        Set<AttributeDescriptor> attributeDescriptors = new HashSet<>(BasicTypes.BASIC_METACARD.getAttributeDescriptors());
        Set<String> requiredAttributes = new HashSet<>();
        metacardType.attributes.forEach((attributeName, attribute) -> {
            AttributeDescriptor descriptor = attributeRegistry.lookup(attributeName).orElseThrow(() -> new IllegalStateException(String.format("Metacard type [%s] includes the attribute [%s], but that attribute is not in the attribute registry.", metacardType.type, attributeName)));
            attributeDescriptors.add(descriptor);
            if (attribute.required) {
                requiredAttributes.add(attributeName);
            }
        });
        if (!requiredAttributes.isEmpty()) {
            final MetacardValidator validator = new RequiredAttributesMetacardValidator(metacardType.type, requiredAttributes);
            staged.add(() -> {
                ServiceRegistration<MetacardValidator> registration = context.registerService(MetacardValidator.class, validator, null);
                changeset.metacardValidatorServices.add(registration);
                return registration != null;
            });
        }
        Dictionary<String, Object> properties = new Hashtable<>();
        properties.put(NAME_PROPERTY, metacardType.type);
        MetacardType type = new MetacardTypeImpl(metacardType.type, attributeDescriptors);
        staged.add(() -> {
            ServiceRegistration<MetacardType> registration = context.registerService(MetacardType.class, type, properties);
            changeset.metacardTypeServices.add(registration);
            return registration != null;
        });
    }
    return staged;
}
Also used : Hashtable(java.util.Hashtable) ArrayList(java.util.ArrayList) AttributeDescriptor(ddf.catalog.data.AttributeDescriptor) MetacardTypeImpl(ddf.catalog.data.impl.MetacardTypeImpl) Callable(java.util.concurrent.Callable) MetacardType(ddf.catalog.data.MetacardType) MetacardValidator(ddf.catalog.validation.MetacardValidator) RequiredAttributesMetacardValidator(ddf.catalog.validation.impl.validator.RequiredAttributesMetacardValidator) RequiredAttributesMetacardValidator(ddf.catalog.validation.impl.validator.RequiredAttributesMetacardValidator) BundleContext(org.osgi.framework.BundleContext) HashSet(java.util.HashSet)

Example 2 with RequiredAttributesMetacardValidator

use of ddf.catalog.validation.impl.validator.RequiredAttributesMetacardValidator in project ddf by codice.

the class ValidationParser method getMetacardValidators.

@SuppressWarnings("unchecked")
private List<MetacardValidator> getMetacardValidators(MetacardValidatorDefinition validatorDefinition) {
    List<MetacardValidator> metacardValidators = new ArrayList<>();
    MetacardValidator metacardValidator = null;
    String validatorName = null;
    Object metacardValidatorObj = validatorDefinition.arguments.get(VALIDATOR_PROPERTY);
    if (metacardValidatorObj instanceof String) {
        validatorName = (String) metacardValidatorObj;
    }
    if (validatorName != null) {
        String metacardType = validatorDefinition.metacardType;
        switch(validatorName) {
            case REQUIRED_ATTRIBUTE_VALIDATOR_PROPERTY:
                {
                    if (metacardType == null) {
                        throw new IllegalStateException("Required Attributes Validator received invalid configuration");
                    }
                    Set<String> requiredAttributes = new HashSet<>();
                    Object requiredAttributesObj = validatorDefinition.arguments.get(REQUIRED_ATTRIBUTES_PROPERTY);
                    if (requiredAttributesObj instanceof List) {
                        List<Object> requiredAttrObjList = (List) requiredAttributesObj;
                        requiredAttributes = requiredAttrObjList.stream().filter(String.class::isInstance).map(String.class::cast).collect(Collectors.toSet());
                    }
                    if (CollectionUtils.isNotEmpty(requiredAttributes)) {
                        metacardValidator = new RequiredAttributesMetacardValidator(metacardType, requiredAttributes);
                    } else {
                        throw new IllegalStateException("Required Attributes Validator received invalid configuration");
                    }
                    break;
                }
            default:
                throw new IllegalStateException("Validator does not exist. (" + validatorName + ")");
        }
    }
    if (metacardValidator != null) {
        metacardValidators.add(metacardValidator);
    }
    return metacardValidators;
}
Also used : Collectors.toSet(java.util.stream.Collectors.toSet) Set(java.util.Set) HashSet(java.util.HashSet) MetacardValidator(ddf.catalog.validation.MetacardValidator) RequiredAttributesMetacardValidator(ddf.catalog.validation.impl.validator.RequiredAttributesMetacardValidator) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) Collectors.toList(java.util.stream.Collectors.toList) RequiredAttributesMetacardValidator(ddf.catalog.validation.impl.validator.RequiredAttributesMetacardValidator)

Aggregations

MetacardValidator (ddf.catalog.validation.MetacardValidator)2 RequiredAttributesMetacardValidator (ddf.catalog.validation.impl.validator.RequiredAttributesMetacardValidator)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 AttributeDescriptor (ddf.catalog.data.AttributeDescriptor)1 MetacardType (ddf.catalog.data.MetacardType)1 MetacardTypeImpl (ddf.catalog.data.impl.MetacardTypeImpl)1 Hashtable (java.util.Hashtable)1 List (java.util.List)1 Set (java.util.Set)1 Callable (java.util.concurrent.Callable)1 Collectors.toList (java.util.stream.Collectors.toList)1 Collectors.toSet (java.util.stream.Collectors.toSet)1 BundleContext (org.osgi.framework.BundleContext)1