<commit_before>/**
 * Copyright (C) 2006-2018 Talend Inc. - www.talend.com
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.talend.sdk.component.form.api;

import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toSet;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.function.Predicate;
import java.util.function.Supplier;

import javax.json.bind.Jsonb;
import javax.json.bind.JsonbBuilder;
import javax.json.bind.JsonbConfig;

import org.talend.sdk.component.form.internal.converter.PropertyContext;
import org.talend.sdk.component.form.internal.converter.impl.JsonSchemaConverter;
import org.talend.sdk.component.form.internal.converter.impl.PropertiesConverter;
import org.talend.sdk.component.form.internal.converter.impl.UiSchemaConverter;
import org.talend.sdk.component.form.model.Ui;
import org.talend.sdk.component.form.model.jsonschema.JsonSchema;
import org.talend.sdk.component.server.front.model.ActionReference;
import org.talend.sdk.component.server.front.model.ComponentDetail;
import org.talend.sdk.component.server.front.model.ConfigTypeNode;
import org.talend.sdk.component.server.front.model.SimplePropertyDefinition;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class UiSpecService implements AutoCloseable {

    private final Client client;

    private final Jsonb jsonb;

    private final boolean closeJsonb;

    public UiSpecService(final Client client) {
        this.client = client;
        this.jsonb = JsonbBuilder.create(new JsonbConfig().setProperty("johnzon.cdi.activated", false));
        this.closeJsonb = true;
    }

    public UiSpecService(final Client client, final Jsonb jsonb) {
        this.client = client;
        this.jsonb = jsonb;
        this.closeJsonb = false;
    }

    /**
     * Converts a configuration model to a uiSpec.
     *
     * @param family the family (you must browse the configuration tree to find the root parent id).
     * @param node the configuration model to convert to a uiSpec.
     * @return a Ui promise.
     */
    public CompletionStage<Ui> convert(final String family, final ConfigTypeNode node) {
        // extract root property
        final Collection<String> rootProperties =
                node.getProperties().stream().map(SimplePropertyDefinition::getPath).collect(toSet());
        final Iterator<String> it = rootProperties.iterator();
        while (it.hasNext()) {
            final String path = it.next() + '.';
            if (node.getProperties().stream().noneMatch(p -> p.getPath().startsWith(path))) {
                it.remove();
            }
        }
        if (rootProperties.isEmpty()) {
            log.warn("No root properties for configuration node {} (family={})", node.getId(), family);
        }

        // [TCOMP-767] 0.0.7 -> 0.0.8 compat
        final Collection<SimplePropertyDefinition> props;
        if (rootProperties.size() == 1 && "configuration".equals(rootProperties.iterator().next())) {
            final SimplePropertyDefinition def = node
                    .getProperties()
                    .stream()
                    .filter(prop -> prop.getPath().equals("configuration"))
                    .findFirst()
                    .get();
            if ("configuration".equals(def.getPath())) {
                props = node.getProperties();
            } else {
                props = node
                        .getProperties()
                        .stream()
                        .map(prop -> new SimplePropertyDefinition(
                                def.getName() + prop.getPath().substring("configuration".length()), prop.getName(),
                                prop.getDisplayName(), prop.getType(), prop.getDefaultValue(), prop.getValidation(),
                                prop.getMetadata(), prop.getPlaceholder(), prop.getProposalDisplayNames()))
                        .collect(toList());
            }
        } else {
            props = node.getProperties();
        }

        return convert(node::getDisplayName, () -> family, () -> props, node::getActions,
                p -> rootProperties.contains(p.getPath()));
    }

    /**<commit_msg>[TCOMP-767] rebasing root properties with the right name to ensure the rest API consistency for configuration types, s/path/name/ in compat test<commit_after>/**
 * Copyright (C) 2006-2018 Talend Inc. - www.talend.com
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.talend.sdk.component.form.api;

import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toSet;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.function.Predicate;
import java.util.function.Supplier;

import javax.json.bind.Jsonb;
import javax.json.bind.JsonbBuilder;
import javax.json.bind.JsonbConfig;

import org.talend.sdk.component.form.internal.converter.PropertyContext;
import org.talend.sdk.component.form.internal.converter.impl.JsonSchemaConverter;
import org.talend.sdk.component.form.internal.converter.impl.PropertiesConverter;
import org.talend.sdk.component.form.internal.converter.impl.UiSchemaConverter;
import org.talend.sdk.component.form.model.Ui;
import org.talend.sdk.component.form.model.jsonschema.JsonSchema;
import org.talend.sdk.component.server.front.model.ActionReference;
import org.talend.sdk.component.server.front.model.ComponentDetail;
import org.talend.sdk.component.server.front.model.ConfigTypeNode;
import org.talend.sdk.component.server.front.model.SimplePropertyDefinition;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class UiSpecService implements AutoCloseable {

    private final Client client;

    private final Jsonb jsonb;

    private final boolean closeJsonb;

    public UiSpecService(final Client client) {
        this.client = client;
        this.jsonb = JsonbBuilder.create(new JsonbConfig().setProperty("johnzon.cdi.activated", false));
        this.closeJsonb = true;
    }

    public UiSpecService(final Client client, final Jsonb jsonb) {
        this.client = client;
        this.jsonb = jsonb;
        this.closeJsonb = false;
    }

    /**
     * Converts a configuration model to a uiSpec.
     *
     * @param family the family (you must browse the configuration tree to find the root parent id).
     * @param node the configuration model to convert to a uiSpec.
     * @return a Ui promise.
     */
    public CompletionStage<Ui> convert(final String family, final ConfigTypeNode node) {
        // extract root property
        final Collection<String> rootProperties =
                node.getProperties().stream().map(SimplePropertyDefinition::getPath).collect(toSet());
        final Iterator<String> it = rootProperties.iterator();
        while (it.hasNext()) {
            final String path = it.next() + '.';
            if (node.getProperties().stream().noneMatch(p -> p.getPath().startsWith(path))) {
                it.remove();
            }
        }
        if (rootProperties.isEmpty()) {
            log.warn("No root properties for configuration node {} (family={})", node.getId(), family);
        }

        // [TCOMP-767] 0.0.7 -> 0.0.8 compat
        final Collection<SimplePropertyDefinition> props;
        if (rootProperties.size() == 1 && "configuration".equals(rootProperties.iterator().next())) {
            final SimplePropertyDefinition def = node
                    .getProperties()
                    .stream()
                    .filter(prop -> prop.getPath().equals("configuration"))
                    .findFirst()
                    .get();
            if ("configuration".equals(def.getName())) {
                props = node.getProperties();
            } else {
                props = node
                        .getProperties()
                        .stream()
                        .map(prop -> new SimplePropertyDefinition(
                                def.getName() + prop.getPath().substring("configuration".length()), prop.getName(),
                                prop.getDisplayName(), prop.getType(), prop.getDefaultValue(), prop.getValidation(),
                                prop.getMetadata(), prop.getPlaceholder(), prop.getProposalDisplayNames()))
                        .collect(toList());
            }
        } else {
            props = node.getProperties();
        }

        return convert(node::getDisplayName, () -> family, () -> props, node::getActions,
                p -> rootProperties.contains(p.getPath()));
    }

    /**
{'fraction_non_alphanumeric': 0.1128303024213805, 'fraction_numerical': 0.005325027629860344, 'mean_word_length': 2.6555269922879177, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1924616', 'n_tokens_mistral': 2688, 'n_tokens_neox': 2615, 'n_words': 656}