Search in sources :

Example 1 with PropertyValidation

use of org.talend.sdk.component.server.front.model.PropertyValidation in project component-runtime by Talend.

the class JsonSchemaConverter method postHandling.

private CompletionStage<PropertyContext> postHandling(final PropertyContext context, final JsonSchema jsonSchema, final String type) {
    final String defaultValue = context.getProperty().getMetadata().getOrDefault("ui::defaultvalue::value", context.getProperty().getDefaultValue());
    convertDefaultValue(type, defaultValue).ifPresent(jsonSchema::setDefaultValue);
    final PropertyValidation validation = context.getProperty().getValidation();
    if (validation != null) {
        ofNullable(validation.getMin()).ifPresent(m -> jsonSchema.setMinimum(m.doubleValue()));
        ofNullable(validation.getMax()).ifPresent(m -> jsonSchema.setMaximum(m.doubleValue()));
        ofNullable(validation.getMinItems()).ifPresent(jsonSchema::setMinItems);
        ofNullable(validation.getMaxItems()).ifPresent(jsonSchema::setMaxItems);
        ofNullable(validation.getMinLength()).ifPresent(jsonSchema::setMinLength);
        ofNullable(validation.getMaxLength()).ifPresent(jsonSchema::setMaxLength);
        ofNullable(validation.getUniqueItems()).ifPresent(jsonSchema::setUniqueItems);
        ofNullable(validation.getPattern()).ifPresent(jsonSchema::setPattern);
    }
    synchronized (rootJsonSchema) {
        if (rootJsonSchema.getProperties() == null) {
            rootJsonSchema.setProperties(new HashMap<>());
        }
        rootJsonSchema.getProperties().put(context.getProperty().getName(), jsonSchema);
    }
    if (properties.stream().anyMatch(context::isDirectChild)) {
        // has child
        final String order = context.getProperty().getMetadata().get("ui::optionsorder::value");
        if (order != null) {
            jsonSchema.setProperties(new TreeMap<>(new Comparator<String>() {

                private final List<String> propertiesOrder = new ArrayList<>(asList(order.split(",")));

                @Override
                public int compare(final String o1, final String o2) {
                    final int i = propertiesOrder.indexOf(o1) - propertiesOrder.indexOf(o2);
                    return i == 0 ? o1.compareTo(o2) : i;
                }
            }));
        } else {
            jsonSchema.setProperties(new HashMap<>());
        }
        final JsonSchemaConverter jsonSchemaConverter = new JsonSchemaConverter(jsonb, jsonSchema, properties);
        return CompletableFuture.allOf(properties.stream().filter(context::isDirectChild).map(PropertyContext::new).map(CompletableFuture::completedFuture).map(jsonSchemaConverter::convert).toArray(CompletableFuture[]::new)).thenApply(r -> context);
    }
    return CompletableFuture.completedFuture(context);
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) ArrayList(java.util.ArrayList) List(java.util.List) Arrays.asList(java.util.Arrays.asList) PropertyValidation(org.talend.sdk.component.server.front.model.PropertyValidation) Comparator(java.util.Comparator)

Example 2 with PropertyValidation

use of org.talend.sdk.component.server.front.model.PropertyValidation in project component-runtime by Talend.

the class PropertyValidationService method initMapper.

@PostConstruct
private void initMapper() {
    // precompute the mapping of validations to centralize the convention - note: can be moved to impl for setters
    // part
    final Collection<BiFunction<Object, Map<String, String>, Boolean>> validationSetters = Stream.of(PropertyValidation.class.getDeclaredFields()).map(f -> {
        // we need boolean, int, string, collection<string>
        final Function<String, Object> valueConverter;
        if (Integer.class == f.getType()) {
            valueConverter = v -> Double.valueOf(v).intValue();
        } else if (Boolean.class == f.getType()) {
            valueConverter = Boolean::parseBoolean;
        } else if (Collection.class == f.getType()) {
            valueConverter = s -> Stream.of(s.split(",")).collect(toList());
        } else {
            valueConverter = s -> s;
        }
        if (!f.isAccessible()) {
            f.setAccessible(true);
        }
        return (BiFunction<Object, Map<String, String>, Boolean>) (instance, meta) -> ofNullable(meta.get(ValidationParameterEnricher.META_PREFIX + f.getName())).map(valueConverter).map(val -> {
            try {
                f.set(instance, val);
            } catch (IllegalAccessException e) {
                throw new IllegalStateException(e);
            }
            return true;
        }).orElse(false);
    }).collect(toList());
    propertyValidationCreator = config -> {
        final PropertyValidation validation = new PropertyValidation();
        if (validationSetters.stream().filter(s -> s.apply(validation, config)).count() == 0) {
            return null;
        }
        return validation;
    };
}
Also used : Collectors.toList(java.util.stream.Collectors.toList) Stream(java.util.stream.Stream) Optional.ofNullable(java.util.Optional.ofNullable) Collection(java.util.Collection) Map(java.util.Map) BiFunction(java.util.function.BiFunction) PostConstruct(javax.annotation.PostConstruct) PropertyValidation(org.talend.sdk.component.server.front.model.PropertyValidation) ApplicationScoped(javax.enterprise.context.ApplicationScoped) Function(java.util.function.Function) ValidationParameterEnricher(org.talend.sdk.component.runtime.manager.reflect.parameterenricher.ValidationParameterEnricher) PropertyValidation(org.talend.sdk.component.server.front.model.PropertyValidation) BiFunction(java.util.function.BiFunction) Function(java.util.function.Function) BiFunction(java.util.function.BiFunction) Collection(java.util.Collection) Map(java.util.Map) PostConstruct(javax.annotation.PostConstruct)

Aggregations

PropertyValidation (org.talend.sdk.component.server.front.model.PropertyValidation)2 ArrayList (java.util.ArrayList)1 Arrays.asList (java.util.Arrays.asList)1 Collection (java.util.Collection)1 Comparator (java.util.Comparator)1 List (java.util.List)1 Map (java.util.Map)1 Optional.ofNullable (java.util.Optional.ofNullable)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 BiFunction (java.util.function.BiFunction)1 Function (java.util.function.Function)1 Collectors.toList (java.util.stream.Collectors.toList)1 Stream (java.util.stream.Stream)1 PostConstruct (javax.annotation.PostConstruct)1 ApplicationScoped (javax.enterprise.context.ApplicationScoped)1 ValidationParameterEnricher (org.talend.sdk.component.runtime.manager.reflect.parameterenricher.ValidationParameterEnricher)1