Search in sources :

Example 11 with PropertyContext

use of org.talend.sdk.component.form.internal.converter.PropertyContext 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

Collection (java.util.Collection)11 PropertyContext (org.talend.sdk.component.form.internal.converter.PropertyContext)11 SimplePropertyDefinition (org.talend.sdk.component.server.front.model.SimplePropertyDefinition)11 CompletionStage (java.util.concurrent.CompletionStage)9 ActionReference (org.talend.sdk.component.server.front.model.ActionReference)9 CompletableFuture (java.util.concurrent.CompletableFuture)8 UiSchema (org.talend.sdk.component.form.model.uischema.UiSchema)8 Collectors.toList (java.util.stream.Collectors.toList)7 Map (java.util.Map)6 Optional.ofNullable (java.util.Optional.ofNullable)6 ArrayList (java.util.ArrayList)5 Client (org.talend.sdk.component.form.api.Client)5 List (java.util.List)4 Collectors.toSet (java.util.stream.Collectors.toSet)4 Stream (java.util.stream.Stream)4 AllArgsConstructor (lombok.AllArgsConstructor)4 PropertyConverter (org.talend.sdk.component.form.internal.converter.PropertyConverter)4 UiSchemaConverter (org.talend.sdk.component.form.internal.converter.impl.UiSchemaConverter)4 JsonSchema (org.talend.sdk.component.form.model.jsonschema.JsonSchema)4 Collections.emptyList (java.util.Collections.emptyList)3