Search in sources :

Example 11 with UiSchema

use of org.talend.sdk.component.form.model.uischema.UiSchema in project component-runtime by Talend.

the class FieldSetWidgetConverter method convert.

@Override
public CompletionStage<PropertyContext> convert(final CompletionStage<PropertyContext> cs) {
    return cs.thenCompose(context -> {
        final UiSchema uiSchema = newUiSchema(context);
        uiSchema.setWidget("fieldset");
        uiSchema.setItems(new ArrayList<>());
        final List<SimplePropertyDefinition> properties = new ArrayList<>();
        final UiSchemaConverter uiSchemaConverter = new UiSchemaConverter(null, family, uiSchema.getItems(), properties, client, this.properties, actions);
        // Create Nested UI Items
        return CompletableFuture.allOf(this.properties.stream().filter(context::isDirectChild).map(PropertyContext::new).map(CompletableFuture::completedFuture).map(uiSchemaConverter::convert).toArray(CompletableFuture[]::new)).thenApply(done -> {
            addActions(context, uiSchema, properties);
            return context;
        });
    });
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) SimplePropertyDefinition(org.talend.sdk.component.server.front.model.SimplePropertyDefinition) ArrayList(java.util.ArrayList) UiSchemaConverter(org.talend.sdk.component.form.internal.converter.impl.UiSchemaConverter) UiSchema(org.talend.sdk.component.form.model.uischema.UiSchema)

Example 12 with UiSchema

use of org.talend.sdk.component.form.model.uischema.UiSchema in project component-runtime by Talend.

the class GridLayoutWidgetConverter method createLayout.

private CompletionStage<UiSchema> createLayout(final PropertyContext root, final String layout, final String layoutFilter) {
    final UiSchema uiSchema = newOrphanSchema(root);
    uiSchema.setItems(new ArrayList<>());
    final Collection<SimplePropertyDefinition> visitedProperties = new ArrayList<>();
    final Map<String, SimplePropertyDefinition> childProperties = properties.stream().filter(root::isDirectChild).collect(toMap(SimplePropertyDefinition::getName, identity()));
    return CompletableFuture.allOf(Stream.of(layout.split("\\|")).map(line -> line.split(",")).map(line -> {
        if (line.length == 1 && childProperties.containsKey(line[0])) {
            return new UiSchemaConverter(layoutFilter, family, uiSchema.getItems(), visitedProperties, client, properties, actions).convert(completedFuture(new PropertyContext(childProperties.get(line[0])))).thenApply(r -> uiSchema);
        } else if (line.length > 1) {
            final UiSchema schema = new UiSchema();
            schema.setWidget("columns");
            schema.setItems(new ArrayList<>());
            final UiSchemaConverter columnConverter = new UiSchemaConverter(layoutFilter, family, schema.getItems(), visitedProperties, client, properties, actions);
            return CompletableFuture.allOf(Stream.of(line).map(String::trim).map(childProperties::get).filter(Objects::nonNull).map(PropertyContext::new).map(CompletableFuture::completedFuture).map(columnConverter::convert).toArray(CompletableFuture[]::new)).thenApply(r -> {
                final Collection<UiSchema> items = uiSchema.getItems();
                synchronized (items) {
                    items.add(schema);
                }
                return uiSchema;
            });
        }
        return completedFuture(null);
    }).toArray(CompletableFuture[]::new)).thenApply(done -> {
        addActions(root, uiSchema, visitedProperties);
        return uiSchema;
    });
}
Also used : Optional.ofNullable(java.util.Optional.ofNullable) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) Collection(java.util.Collection) CompletableFuture(java.util.concurrent.CompletableFuture) PropertyContext(org.talend.sdk.component.form.internal.converter.PropertyContext) Client(org.talend.sdk.component.form.api.Client) ArrayList(java.util.ArrayList) Objects(java.util.Objects) UiSchema(org.talend.sdk.component.form.model.uischema.UiSchema) Collectors.toList(java.util.stream.Collectors.toList) CompletionStage(java.util.concurrent.CompletionStage) Stream(java.util.stream.Stream) ActionReference(org.talend.sdk.component.server.front.model.ActionReference) Collectors.toMap(java.util.stream.Collectors.toMap) SimplePropertyDefinition(org.talend.sdk.component.server.front.model.SimplePropertyDefinition) Map(java.util.Map) UiSchemaConverter(org.talend.sdk.component.form.internal.converter.impl.UiSchemaConverter) Function.identity(java.util.function.Function.identity) SimplePropertyDefinition(org.talend.sdk.component.server.front.model.SimplePropertyDefinition) ArrayList(java.util.ArrayList) UiSchemaConverter(org.talend.sdk.component.form.internal.converter.impl.UiSchemaConverter) UiSchema(org.talend.sdk.component.form.model.uischema.UiSchema) CompletableFuture(java.util.concurrent.CompletableFuture) PropertyContext(org.talend.sdk.component.form.internal.converter.PropertyContext) Objects(java.util.Objects)

Example 13 with UiSchema

use of org.talend.sdk.component.form.model.uischema.UiSchema in project component-runtime by Talend.

the class MultiSelectTagWidgetConverter method convert.

@Override
public CompletionStage<PropertyContext> convert(final CompletionStage<PropertyContext> cs) {
    return cs.thenCompose(context -> {
        final UiSchema schema = newUiSchema(context);
        schema.setWidget("multiSelectTag");
        schema.setRestricted(false);
        final String actionName = context.getProperty().getMetadata().get("action::dynamic_values");
        if (client != null && actionName != null) {
            return client.action(family, "dynamic_values", actionName, emptyMap()).exceptionally(e -> {
                log.warn(e.getMessage(), e);
                return emptyMap();
            }).thenApply(values -> {
                final List<UiSchema.NameValue> namedValues = ofNullable(values).map(v -> v.get("items")).filter(Collection.class::isInstance).map(c -> {
                    final Collection<?> dynamicValues = Collection.class.cast(c);
                    return dynamicValues.stream().filter(Map.class::isInstance).filter(m -> Map.class.cast(m).get("id") != null && Map.class.cast(m).get("id") instanceof String).map(Map.class::cast).map(entry -> {
                        final UiSchema.NameValue val = new UiSchema.NameValue();
                        val.setName((String) entry.get("id"));
                        val.setValue(entry.get("label") == null ? (String) entry.get("id") : (String) entry.get("label"));
                        return val;
                    }).collect(toList());
                }).orElse(emptyList());
                schema.setTitleMap(namedValues);
                return context;
            });
        } else {
            schema.setTitleMap(emptyList());
        }
        return CompletableFuture.completedFuture(context);
    });
}
Also used : Collections.emptyMap(java.util.Collections.emptyMap) Collections.emptyList(java.util.Collections.emptyList) Optional.ofNullable(java.util.Optional.ofNullable) Collection(java.util.Collection) CompletableFuture(java.util.concurrent.CompletableFuture) PropertyContext(org.talend.sdk.component.form.internal.converter.PropertyContext) Client(org.talend.sdk.component.form.api.Client) UiSchema(org.talend.sdk.component.form.model.uischema.UiSchema) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) CompletionStage(java.util.concurrent.CompletionStage) ActionReference(org.talend.sdk.component.server.front.model.ActionReference) SimplePropertyDefinition(org.talend.sdk.component.server.front.model.SimplePropertyDefinition) Map(java.util.Map) Collection(java.util.Collection) UiSchema(org.talend.sdk.component.form.model.uischema.UiSchema)

Example 14 with UiSchema

use of org.talend.sdk.component.form.model.uischema.UiSchema in project component-runtime by Talend.

the class ObjectArrayWidgetConverter method convert.

@Override
public CompletionStage<PropertyContext> convert(final CompletionStage<PropertyContext> cs) {
    return cs.thenCompose(context -> {
        final UiSchema arraySchema = newUiSchema(context);
        arraySchema.setTitle(context.getProperty().getDisplayName());
        arraySchema.setItems(new ArrayList<>());
        arraySchema.setItemWidget("collapsibleFieldset");
        final UiSchemaConverter converter = new UiSchemaConverter(gridLayoutFilter, family, arraySchema.getItems(), new ArrayList<>(), client, properties, actions);
        return CompletableFuture.allOf(nestedProperties.stream().map(p -> converter.convert(CompletableFuture.completedFuture(new PropertyContext(p)))).toArray(CompletableFuture[]::new)).thenApply(r -> context);
    });
}
Also used : UiSchema(org.talend.sdk.component.form.model.uischema.UiSchema) CompletionStage(java.util.concurrent.CompletionStage) ActionReference(org.talend.sdk.component.server.front.model.ActionReference) SimplePropertyDefinition(org.talend.sdk.component.server.front.model.SimplePropertyDefinition) Collection(java.util.Collection) UiSchemaConverter(org.talend.sdk.component.form.internal.converter.impl.UiSchemaConverter) CompletableFuture(java.util.concurrent.CompletableFuture) PropertyContext(org.talend.sdk.component.form.internal.converter.PropertyContext) Client(org.talend.sdk.component.form.api.Client) ArrayList(java.util.ArrayList) PropertyContext(org.talend.sdk.component.form.internal.converter.PropertyContext) UiSchemaConverter(org.talend.sdk.component.form.internal.converter.impl.UiSchemaConverter) UiSchema(org.talend.sdk.component.form.model.uischema.UiSchema)

Example 15 with UiSchema

use of org.talend.sdk.component.form.model.uischema.UiSchema in project component-runtime by Talend.

the class ObjectWidgetConverter method addActions.

protected void addActions(final PropertyContext root, final UiSchema uiSchema, final Collection<SimplePropertyDefinition> includedProperties) {
    final Optional<SimplePropertyDefinition> schemaBinding = includedProperties.stream().filter(p -> "OUT".equals(p.getMetadata().get("ui::structure::type"))).findFirst();
    final Collection<UiSchema> items = uiSchema.getItems();
    if (schemaBinding.isPresent()) {
        SimplePropertyDefinition bindingProp = schemaBinding.get();
        final String schemaActionName = ofNullable(bindingProp.getMetadata().get("action::schema")).filter(n -> !n.isEmpty()).orElse("default");
        actions.stream().filter(a -> a.getName().equals(schemaActionName) && "schema".equals(a.getType())).findFirst().ifPresent(ref -> {
            final UiSchema.Trigger trigger = toTrigger(properties, root.getProperty(), ref);
            trigger.setOptions(singletonList(new UiSchema.Option.Builder().withPath(bindingProp.getPath().replace("[]", "")).withType("array".equalsIgnoreCase(bindingProp.getType()) ? "array" : "object").build()));
            if (trigger.getParameters() == null || trigger.getParameters().isEmpty()) {
                // find the matching dataset
                Optional<SimplePropertyDefinition> findParameters = properties.stream().filter(nested -> ref.getName().equals(nested.getMetadata().get("configurationtype::name")) && "dataset".equals(nested.getMetadata().get("configurationtype::type"))).findFirst();
                if (!findParameters.isPresent()) {
                    // if not ambiguous grab the unique dataset
                    final Collection<SimplePropertyDefinition> datasets = properties.stream().filter(nested -> "dataset".equals(nested.getMetadata().get("configurationtype::type"))).collect(toSet());
                    if (datasets.size() == 1) {
                        findParameters = of(datasets.iterator().next());
                    }
                }
                findParameters.ifPresent(dataset -> {
                    final UiSchema.Parameter parameter = new UiSchema.Parameter();
                    parameter.setKey(ofNullable(ref.getProperties()).orElse(Collections.emptyList()).stream().filter(p -> !p.getPath().contains(".")).findFirst().map(SimplePropertyDefinition::getName).orElse("dataset"));
                    parameter.setPath(dataset.getPath());
                    trigger.setParameters(toParams(properties, dataset, ref, dataset.getPath()));
                });
            }
            final UiSchema button = new UiSchema();
            button.setKey("button_schema_" + root.getProperty().getPath());
            button.setTitle("Guess Schema");
            button.setWidget("button");
            button.setTriggers(singletonList(trigger));
            synchronized (items) {
                items.add(button);
            }
        });
    }
    ofNullable(root.getProperty().getMetadata().get("action::healthcheck")).flatMap(v -> (actions == null ? Stream.<ActionReference>empty() : actions.stream()).filter(a -> a.getName().equals(v) && "healthcheck".equals(a.getType())).findFirst()).ifPresent(ref -> {
        final UiSchema.Trigger trigger = toTrigger(properties, root.getProperty(), ref);
        if (trigger.getParameters() == null || trigger.getParameters().isEmpty()) {
            // find the matching dataset
            properties.stream().filter(nested -> "datastore".equals(nested.getMetadata().get("configurationtype::type")) && ref.getName().equals(nested.getMetadata().get("configurationtype::name"))).findFirst().ifPresent(datastore -> trigger.setParameters(toParams(properties, datastore, ref, datastore.getPath())));
        }
        final UiSchema button = new UiSchema();
        button.setKey("button_healthcheck_" + root.getProperty().getPath());
        button.setTitle("Validate Datastore");
        button.setWidget("button");
        button.setTriggers(singletonList(trigger));
        synchronized (items) {
            items.add(button);
        }
    });
}
Also used : UiSchema(org.talend.sdk.component.form.model.uischema.UiSchema) Stream(java.util.stream.Stream) ActionReference(org.talend.sdk.component.server.front.model.ActionReference) SimplePropertyDefinition(org.talend.sdk.component.server.front.model.SimplePropertyDefinition) Optional.ofNullable(java.util.Optional.ofNullable) Optional.of(java.util.Optional.of) Collection(java.util.Collection) Optional(java.util.Optional) PropertyContext(org.talend.sdk.component.form.internal.converter.PropertyContext) Collections(java.util.Collections) Collectors.toSet(java.util.stream.Collectors.toSet) Collections.singletonList(java.util.Collections.singletonList) SimplePropertyDefinition(org.talend.sdk.component.server.front.model.SimplePropertyDefinition) ActionReference(org.talend.sdk.component.server.front.model.ActionReference) UiSchema(org.talend.sdk.component.form.model.uischema.UiSchema)

Aggregations

UiSchema (org.talend.sdk.component.form.model.uischema.UiSchema)18 Collection (java.util.Collection)9 ArrayList (java.util.ArrayList)7 CompletableFuture (java.util.concurrent.CompletableFuture)7 CompletionStage (java.util.concurrent.CompletionStage)7 Collectors.toList (java.util.stream.Collectors.toList)7 SimplePropertyDefinition (org.talend.sdk.component.server.front.model.SimplePropertyDefinition)7 Stream (java.util.stream.Stream)6 PropertyContext (org.talend.sdk.component.form.internal.converter.PropertyContext)6 ActionReference (org.talend.sdk.component.server.front.model.ActionReference)6 Map (java.util.Map)5 Optional.ofNullable (java.util.Optional.ofNullable)5 Test (org.junit.jupiter.api.Test)5 Ui (org.talend.sdk.component.form.model.Ui)5 Collections.singletonList (java.util.Collections.singletonList)4 List (java.util.List)4 Collectors.toSet (java.util.stream.Collectors.toSet)4 Client (org.talend.sdk.component.form.api.Client)4 Iterator (java.util.Iterator)3 UiSchemaConverter (org.talend.sdk.component.form.internal.converter.impl.UiSchemaConverter)3