Search in sources :

Example 1 with PropertyContext

use of org.talend.sdk.component.form.internal.converter.PropertyContext in project component-runtime by Talend.

the class GridLayoutWidgetConverter method convert.

@Override
public CompletionStage<PropertyContext> convert(final CompletionStage<PropertyContext> cs) {
    return cs.thenCompose(context -> {
        // if we have a single tab we don't wrap the forms in tabs otherwise we do
        if (layouts.size() == 1) {
            final Map.Entry<String, String> first = layouts.entrySet().iterator().next();
            return createLayout(context, first.getValue(), first.getKey()).thenApply(uiSchema -> {
                synchronized (schemas) {
                    schemas.add(uiSchema);
                }
                return context;
            });
        } else {
            // if we have multiple tabs, priority is MAIN/ADVANCED pair first
            // but if they are not present then we use all layouts
            final Collection<String> tabs = (layouts.containsKey("Main") ? Stream.of("Main", "Advanced") : layouts.keySet().stream()).collect(toList());
            final UiSchema schema = newUiSchema(context);
            schema.setTitle(null);
            schema.setWidget("tabs");
            final Collection<UiSchema> resolvedLayouts = new ArrayList<>();
            return CompletableFuture.allOf(tabs.stream().sorted((o1, o2) -> {
                if (o1.equals(o2)) {
                    return 0;
                }
                if ("Main".equalsIgnoreCase(o1)) {
                    return -1;
                }
                if ("Main".equalsIgnoreCase(o2)) {
                    return 1;
                }
                final int compareToIgnoreCase = o1.compareToIgnoreCase(o2);
                if (compareToIgnoreCase == 0) {
                    return o1.compareTo(o2);
                }
                return compareToIgnoreCase;
            }).map(tab -> ofNullable(layouts.get(tab)).map(layoutStr -> createLayout(context, layoutStr, tab).thenApply(layout -> {
                layout.setTitle(tab);
                synchronized (resolvedLayouts) {
                    resolvedLayouts.add(layout);
                }
                return layout;
            })).orElse(null)).filter(Objects::nonNull).toArray(CompletableFuture[]::new)).thenApply(done -> {
                schema.setItems(resolvedLayouts);
                return context;
            });
        }
    });
}
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) ArrayList(java.util.ArrayList) Collectors.toMap(java.util.stream.Collectors.toMap) Map(java.util.Map) UiSchema(org.talend.sdk.component.form.model.uischema.UiSchema)

Example 2 with PropertyContext

use of org.talend.sdk.component.form.internal.converter.PropertyContext in project component-runtime by Talend.

the class UiSpecService method convert.

private CompletionStage<Ui> convert(final Supplier<String> displayName, final Supplier<String> family, final Supplier<Collection<SimplePropertyDefinition>> properties, final Supplier<Collection<ActionReference>> actions, final Predicate<SimplePropertyDefinition> isRootProperty) {
    final Collection<SimplePropertyDefinition> props = properties.get();
    final Ui ui = new Ui();
    ui.setUiSchema(new ArrayList<>());
    ui.setProperties(new HashMap<>());
    ui.setJsonSchema(new JsonSchema());
    ui.getJsonSchema().setTitle(displayName.get());
    ui.getJsonSchema().setType("object");
    ui.getJsonSchema().setRequired(props.stream().filter(isRootProperty).filter(p -> new PropertyContext(p).isRequired()).map(SimplePropertyDefinition::getName).collect(toSet()));
    final JsonSchemaConverter jsonSchemaConverter = new JsonSchemaConverter(jsonb, ui.getJsonSchema(), props);
    final UiSchemaConverter uiSchemaConverter = new UiSchemaConverter(null, family.get(), ui.getUiSchema(), new ArrayList<>(), client, props, actions.get());
    final PropertiesConverter propertiesConverter = new PropertiesConverter(jsonb, Map.class.cast(ui.getProperties()), props);
    return CompletableFuture.allOf(props.stream().filter(Objects::nonNull).filter(isRootProperty).map(PropertyContext::new).map(CompletableFuture::completedFuture).map(jsonSchemaConverter::convert).map(uiSchemaConverter::convert).map(propertiesConverter::convert).toArray(CompletableFuture[]::new)).thenApply(r -> ui);
}
Also used : PropertiesConverter(org.talend.sdk.component.form.internal.converter.impl.PropertiesConverter) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) PropertyContext(org.talend.sdk.component.form.internal.converter.PropertyContext) Supplier(java.util.function.Supplier) JsonbConfig(javax.json.bind.JsonbConfig) ArrayList(java.util.ArrayList) ComponentDetail(org.talend.sdk.component.server.front.model.ComponentDetail) ActionReference(org.talend.sdk.component.server.front.model.ActionReference) Map(java.util.Map) JsonSchemaConverter(org.talend.sdk.component.form.internal.converter.impl.JsonSchemaConverter) Collectors.toSet(java.util.stream.Collectors.toSet) JsonbBuilder(javax.json.bind.JsonbBuilder) Iterator(java.util.Iterator) Predicate(java.util.function.Predicate) Collection(java.util.Collection) JsonSchema(org.talend.sdk.component.form.model.jsonschema.JsonSchema) Objects(java.util.Objects) Slf4j(lombok.extern.slf4j.Slf4j) CompletionStage(java.util.concurrent.CompletionStage) SimplePropertyDefinition(org.talend.sdk.component.server.front.model.SimplePropertyDefinition) Ui(org.talend.sdk.component.form.model.Ui) UiSchemaConverter(org.talend.sdk.component.form.internal.converter.impl.UiSchemaConverter) Jsonb(javax.json.bind.Jsonb) ConfigTypeNode(org.talend.sdk.component.server.front.model.ConfigTypeNode) SimplePropertyDefinition(org.talend.sdk.component.server.front.model.SimplePropertyDefinition) JsonSchema(org.talend.sdk.component.form.model.jsonschema.JsonSchema) UiSchemaConverter(org.talend.sdk.component.form.internal.converter.impl.UiSchemaConverter) CompletableFuture(java.util.concurrent.CompletableFuture) JsonSchemaConverter(org.talend.sdk.component.form.internal.converter.impl.JsonSchemaConverter) Ui(org.talend.sdk.component.form.model.Ui) PropertyContext(org.talend.sdk.component.form.internal.converter.PropertyContext) PropertiesConverter(org.talend.sdk.component.form.internal.converter.impl.PropertiesConverter) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with PropertyContext

use of org.talend.sdk.component.form.internal.converter.PropertyContext in project component-runtime by Talend.

the class JsonSchemaConverter method convert.

@Override
public CompletionStage<PropertyContext> convert(final CompletionStage<PropertyContext> cs) {
    return cs.thenCompose(context -> {
        final JsonSchema jsonSchema = new JsonSchema();
        jsonSchema.setTitle(context.getProperty().getDisplayName());
        final String type = context.getProperty().getType();
        switch(type.toLowerCase(ROOT)) {
            case "enum":
                return new EnumPropertyConverter(jsonSchema).convert(CompletableFuture.completedFuture(context)).thenCompose(c -> postHandling(context, jsonSchema, type));
            case "array":
                return new ArrayPropertyConverter(jsonb, jsonSchema, properties).convert(CompletableFuture.completedFuture(context)).thenCompose(c -> postHandling(context, jsonSchema, type));
            default:
                if (context.getProperty().getPath().endsWith("[]")) {
                    return CompletableFuture.completedFuture(context);
                }
                jsonSchema.setType(type.toLowerCase(ROOT));
                jsonSchema.setRequired(properties.stream().filter(context::isDirectChild).filter(nested -> new PropertyContext(nested).isRequired()).map(SimplePropertyDefinition::getName).collect(toSet()));
                return CompletableFuture.completedFuture(context).thenCompose(c -> postHandling(context, jsonSchema, type));
        }
    });
}
Also used : EnumPropertyConverter(org.talend.sdk.component.form.internal.converter.impl.schema.EnumPropertyConverter) Optional.ofNullable(java.util.Optional.ofNullable) Collection(java.util.Collection) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) PropertyContext(org.talend.sdk.component.form.internal.converter.PropertyContext) JsonSchema(org.talend.sdk.component.form.model.jsonschema.JsonSchema) ArrayPropertyConverter(org.talend.sdk.component.form.internal.converter.impl.schema.ArrayPropertyConverter) PropertyConverter(org.talend.sdk.component.form.internal.converter.PropertyConverter) ArrayList(java.util.ArrayList) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) TreeMap(java.util.TreeMap) SimplePropertyDefinition(org.talend.sdk.component.server.front.model.SimplePropertyDefinition) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) Optional(java.util.Optional) PropertyValidation(org.talend.sdk.component.server.front.model.PropertyValidation) Jsonb(javax.json.bind.Jsonb) ROOT(java.util.Locale.ROOT) Comparator(java.util.Comparator) AllArgsConstructor(lombok.AllArgsConstructor) Collectors.toSet(java.util.stream.Collectors.toSet) SimplePropertyDefinition(org.talend.sdk.component.server.front.model.SimplePropertyDefinition) PropertyContext(org.talend.sdk.component.form.internal.converter.PropertyContext) JsonSchema(org.talend.sdk.component.form.model.jsonschema.JsonSchema) ArrayPropertyConverter(org.talend.sdk.component.form.internal.converter.impl.schema.ArrayPropertyConverter) EnumPropertyConverter(org.talend.sdk.component.form.internal.converter.impl.schema.EnumPropertyConverter)

Example 4 with PropertyContext

use of org.talend.sdk.component.form.internal.converter.PropertyContext in project component-runtime by Talend.

the class UiSchemaConverter method convert.

@Override
public CompletionStage<PropertyContext> convert(final CompletionStage<PropertyContext> cs) {
    return cs.thenCompose(context -> {
        final String type = context.getProperty().getType().toLowerCase(Locale.ROOT);
        switch(type) {
            case "object":
                final Map<String, String> gridLayouts = context.getProperty().getMetadata().entrySet().stream().filter(e -> e.getKey().startsWith("ui::gridlayout::") && e.getKey().endsWith("::value")).collect(toMap(e -> e.getKey().substring("ui::gridlayout::".length(), e.getKey().length() - "::value".length()), Map.Entry::getValue, (a, b) -> {
                    throw new IllegalArgumentException("Can't merge " + a + " and " + b);
                }, () -> new TreeMap<>(String.CASE_INSENSITIVE_ORDER)));
                if (!gridLayouts.isEmpty()) {
                    return new GridLayoutWidgetConverter(schemas, properties, actions, client, family, gridLayoutFilter != null && gridLayouts.containsKey(gridLayoutFilter) ? singletonMap(gridLayoutFilter, gridLayouts.get(gridLayoutFilter)) : gridLayouts).convert(CompletableFuture.completedFuture(context));
                }
                return new FieldSetWidgetConverter(schemas, properties, actions, client, family).convert(CompletableFuture.completedFuture(context));
            case "boolean":
                includedProperties.add(context.getProperty());
                return new ToggleWidgetConverter(schemas, properties, actions).convert(CompletableFuture.completedFuture(context));
            case "enum":
                includedProperties.add(context.getProperty());
                return new DataListWidgetConverter(schemas, properties, actions).convert(CompletableFuture.completedFuture(context));
            case "number":
                includedProperties.add(context.getProperty());
                return new NumberWidgetConverter(schemas, properties, actions).convert(CompletableFuture.completedFuture(context));
            case "array":
                includedProperties.add(context.getProperty());
                final String nestedPrefix = context.getProperty().getPath() + "[].";
                final int from = nestedPrefix.length();
                final Collection<SimplePropertyDefinition> nested = properties.stream().filter(prop -> prop.getPath().startsWith(nestedPrefix) && prop.getPath().indexOf('.', from) < 0).collect(toList());
                if (!nested.isEmpty()) {
                    return new ObjectArrayWidgetConverter(schemas, properties, actions, nested, family, client, gridLayoutFilter).convert(CompletableFuture.completedFuture(context));
                }
                return new MultiSelectTagWidgetConverter(schemas, properties, actions, client, family).convert(CompletableFuture.completedFuture(context));
            case "string":
            default:
                if (context.getProperty().getPath().endsWith("[]")) {
                    return CompletableFuture.completedFuture(context);
                }
                includedProperties.add(context.getProperty());
                if ("true".equalsIgnoreCase(context.getProperty().getMetadata().get("ui::credential"))) {
                    return new CredentialWidgetConverter(schemas, properties, actions).convert(CompletableFuture.completedFuture(context));
                } else if (context.getProperty().getMetadata().containsKey("ui::code::value")) {
                    return new CodeWidgetConverter(schemas, properties, actions).convert(CompletableFuture.completedFuture(context));
                } else if (context.getProperty().getMetadata() != null && context.getProperty().getMetadata().containsKey("action::dynamic_values")) {
                    return new MultiSelectTagWidgetConverter(schemas, properties, actions, client, family).convert(CompletableFuture.completedFuture(context));
                } else if (context.getProperty().getMetadata().containsKey("ui::textarea") && Boolean.valueOf(context.getProperty().getMetadata().get("ui::textarea"))) {
                    return new TextAreaWidgetConverter(schemas, properties, actions).convert(CompletableFuture.completedFuture(context));
                }
                return new TextWidgetConverter(schemas, properties, actions).convert(CompletableFuture.completedFuture(context));
        }
    });
}
Also used : ObjectArrayWidgetConverter(org.talend.sdk.component.form.internal.converter.impl.widget.ObjectArrayWidgetConverter) CredentialWidgetConverter(org.talend.sdk.component.form.internal.converter.impl.widget.CredentialWidgetConverter) CompletableFuture(java.util.concurrent.CompletableFuture) PropertyContext(org.talend.sdk.component.form.internal.converter.PropertyContext) PropertyConverter(org.talend.sdk.component.form.internal.converter.PropertyConverter) TextWidgetConverter(org.talend.sdk.component.form.internal.converter.impl.widget.TextWidgetConverter) FieldSetWidgetConverter(org.talend.sdk.component.form.internal.converter.impl.widget.FieldSetWidgetConverter) MultiSelectTagWidgetConverter(org.talend.sdk.component.form.internal.converter.impl.widget.MultiSelectTagWidgetConverter) DataListWidgetConverter(org.talend.sdk.component.form.internal.converter.impl.widget.DataListWidgetConverter) ActionReference(org.talend.sdk.component.server.front.model.ActionReference) Collectors.toMap(java.util.stream.Collectors.toMap) Locale(java.util.Locale) Map(java.util.Map) Collections.singletonMap(java.util.Collections.singletonMap) NumberWidgetConverter(org.talend.sdk.component.form.internal.converter.impl.widget.NumberWidgetConverter) Collection(java.util.Collection) TextAreaWidgetConverter(org.talend.sdk.component.form.internal.converter.impl.widget.TextAreaWidgetConverter) GridLayoutWidgetConverter(org.talend.sdk.component.form.internal.converter.impl.widget.GridLayoutWidgetConverter) Client(org.talend.sdk.component.form.api.Client) ToggleWidgetConverter(org.talend.sdk.component.form.internal.converter.impl.widget.ToggleWidgetConverter) UiSchema(org.talend.sdk.component.form.model.uischema.UiSchema) Collectors.toList(java.util.stream.Collectors.toList) CompletionStage(java.util.concurrent.CompletionStage) TreeMap(java.util.TreeMap) SimplePropertyDefinition(org.talend.sdk.component.server.front.model.SimplePropertyDefinition) AllArgsConstructor(lombok.AllArgsConstructor) CodeWidgetConverter(org.talend.sdk.component.form.internal.converter.impl.widget.CodeWidgetConverter) CredentialWidgetConverter(org.talend.sdk.component.form.internal.converter.impl.widget.CredentialWidgetConverter) TextAreaWidgetConverter(org.talend.sdk.component.form.internal.converter.impl.widget.TextAreaWidgetConverter) SimplePropertyDefinition(org.talend.sdk.component.server.front.model.SimplePropertyDefinition) CodeWidgetConverter(org.talend.sdk.component.form.internal.converter.impl.widget.CodeWidgetConverter) NumberWidgetConverter(org.talend.sdk.component.form.internal.converter.impl.widget.NumberWidgetConverter) TreeMap(java.util.TreeMap) DataListWidgetConverter(org.talend.sdk.component.form.internal.converter.impl.widget.DataListWidgetConverter) MultiSelectTagWidgetConverter(org.talend.sdk.component.form.internal.converter.impl.widget.MultiSelectTagWidgetConverter) TextWidgetConverter(org.talend.sdk.component.form.internal.converter.impl.widget.TextWidgetConverter) ToggleWidgetConverter(org.talend.sdk.component.form.internal.converter.impl.widget.ToggleWidgetConverter) ObjectArrayWidgetConverter(org.talend.sdk.component.form.internal.converter.impl.widget.ObjectArrayWidgetConverter) FieldSetWidgetConverter(org.talend.sdk.component.form.internal.converter.impl.widget.FieldSetWidgetConverter) GridLayoutWidgetConverter(org.talend.sdk.component.form.internal.converter.impl.widget.GridLayoutWidgetConverter) Collectors.toMap(java.util.stream.Collectors.toMap) Map(java.util.Map) Collections.singletonMap(java.util.Collections.singletonMap) TreeMap(java.util.TreeMap)

Example 5 with PropertyContext

use of org.talend.sdk.component.form.internal.converter.PropertyContext in project component-runtime by Talend.

the class ArrayPropertyConverter method convert.

@Override
public CompletionStage<PropertyContext> convert(final CompletionStage<PropertyContext> cs) {
    return cs.thenCompose(context -> {
        jsonSchema.setType(context.getProperty().getType().toLowerCase(ROOT));
        final String prefix = context.getProperty().getPath() + "[]";
        final List<SimplePropertyDefinition> arrayElements = properties.stream().filter(child -> child.getPath().startsWith(prefix)).collect(toList());
        if (arrayElements.stream().anyMatch(e -> e.getPath().startsWith(prefix + '.'))) {
            // complex object
            final JsonSchema items = new JsonSchema();
            items.setType("object");
            items.setProperties(new HashMap<>());
            jsonSchema.setItems(items);
            return CompletableFuture.allOf(arrayElements.stream().map(PropertyContext::new).map(CompletableFuture::completedFuture).map(e -> new JsonSchemaConverter(jsonb, items, emptyList()).convert(e)).toArray(CompletableFuture[]::new)).thenApply(done -> context);
        } else if (!arrayElements.isEmpty()) {
            // primitive
            final String type = arrayElements.get(0).getType();
            final JsonSchema item = new JsonSchema();
            item.setTitle(jsonSchema.getTitle());
            item.setType("enum".equalsIgnoreCase(type) ? "string" : type.toLowerCase(ROOT));
            jsonSchema.setItems(item);
        }
        return CompletableFuture.completedFuture(context);
    });
}
Also used : Collections.emptyList(java.util.Collections.emptyList) Collection(java.util.Collection) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) PropertyContext(org.talend.sdk.component.form.internal.converter.PropertyContext) JsonSchema(org.talend.sdk.component.form.model.jsonschema.JsonSchema) PropertyConverter(org.talend.sdk.component.form.internal.converter.PropertyConverter) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) SimplePropertyDefinition(org.talend.sdk.component.server.front.model.SimplePropertyDefinition) JsonSchemaConverter(org.talend.sdk.component.form.internal.converter.impl.JsonSchemaConverter) Jsonb(javax.json.bind.Jsonb) ROOT(java.util.Locale.ROOT) AllArgsConstructor(lombok.AllArgsConstructor) CompletableFuture(java.util.concurrent.CompletableFuture) SimplePropertyDefinition(org.talend.sdk.component.server.front.model.SimplePropertyDefinition) JsonSchemaConverter(org.talend.sdk.component.form.internal.converter.impl.JsonSchemaConverter) JsonSchema(org.talend.sdk.component.form.model.jsonschema.JsonSchema)

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