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