Search in sources :

Example 1 with Schema

use of org.talend.sdk.component.api.service.schema.Schema in project component-runtime by Talend.

the class TaCoKitGuessSchema method guessSchemaThroughAction.

public boolean guessSchemaThroughAction() {
    if (action == null || action.isEmpty()) {
        return false;
    }
    final ServiceMeta.ActionMeta actionRef = componentManager.findPlugin(plugin).orElseThrow(() -> new IllegalArgumentException("No component " + plugin)).get(ContainerComponentRegistry.class).getServices().stream().flatMap(s -> s.getActions().stream()).filter(a -> a.getFamily().equals(family) && a.getAction().equals(action) && a.getType().equals(type)).findFirst().orElseThrow(() -> new IllegalArgumentException("No action " + family + "#" + type + "#" + action));
    final Object schemaResult = actionRef.getInvoker().apply(buildActionConfig(actionRef, configuration));
    if (schemaResult instanceof Schema) {
        Collection<Schema.Entry> entries = Schema.class.cast(schemaResult).getEntries();
        if (entries == null || entries.isEmpty()) {
            log.info("No column found by guess schema action");
            return false;
        }
        for (Schema.Entry entry : entries) {
            String name = entry.getName();
            Type entryType = entry.getType();
            if (entryType == null) {
                entryType = Type.STRING;
            }
            String typeName;
            switch(entryType) {
                case BOOLEAN:
                    typeName = javaTypesManager.BOOLEAN.getId();
                    break;
                case DOUBLE:
                    typeName = javaTypesManager.DOUBLE.getId();
                    break;
                case INT:
                    typeName = javaTypesManager.INTEGER.getId();
                    break;
                default:
                    typeName = javaTypesManager.STRING.getId();
                    break;
            }
            append(str, name, typeName);
        }
        return true;
    } else {
        log.error("Result of built-in guess schema action is not an instance of TaCoKit Schema");
        return false;
    }
}
Also used : IntStream(java.util.stream.IntStream) HashMap(java.util.HashMap) Modifier.isStatic(java.lang.reflect.Modifier.isStatic) Type(org.talend.sdk.component.api.service.schema.Type) Schema(org.talend.sdk.component.api.service.schema.Schema) HashSet(java.util.HashSet) JsonValue(javax.json.JsonValue) Collectors.toMap(java.util.stream.Collectors.toMap) ElementListener(org.talend.sdk.component.api.processor.ElementListener) Map(java.util.Map) Input(org.talend.sdk.component.runtime.input.Input) JobStateAware(org.talend.sdk.component.runtime.di.JobStateAware) Output(org.talend.sdk.component.api.processor.Output) ParameterMeta(org.talend.sdk.component.runtime.manager.ParameterMeta) PrintStream(java.io.PrintStream) JsonObject(javax.json.JsonObject) ContainerComponentRegistry(org.talend.sdk.component.runtime.manager.ContainerComponentRegistry) Collection(java.util.Collection) Set(java.util.Set) Delegated(org.talend.sdk.component.runtime.base.Delegated) Field(java.lang.reflect.Field) ChainedMapper(org.talend.sdk.component.runtime.manager.chain.ChainedMapper) Processor(org.talend.sdk.component.runtime.output.Processor) OutputEmitter(org.talend.sdk.component.api.processor.OutputEmitter) Mapper(org.talend.sdk.component.runtime.input.Mapper) Slf4j(lombok.extern.slf4j.Slf4j) Stream(java.util.stream.Stream) ParameterizedType(java.lang.reflect.ParameterizedType) Optional(java.util.Optional) ComponentManager(org.talend.sdk.component.runtime.manager.ComponentManager) ServiceMeta(org.talend.sdk.component.runtime.manager.ServiceMeta) Type(org.talend.sdk.component.api.service.schema.Type) ParameterizedType(java.lang.reflect.ParameterizedType) Schema(org.talend.sdk.component.api.service.schema.Schema) JsonObject(javax.json.JsonObject) ServiceMeta(org.talend.sdk.component.runtime.manager.ServiceMeta)

Example 2 with Schema

use of org.talend.sdk.component.api.service.schema.Schema in project component-runtime by Talend.

the class Generator method sample.

private static String sample(final Class<?> returnedType) {
    if (returnedType == Values.class) {
        final Values list = new Values();
        list.setItems(new ArrayList<>());
        final Values.Item item = new Values.Item();
        item.setId("value");
        item.setLabel("label");
        list.getItems().add(item);
        return new MapperBuilder().setPretty(false).build().writeObjectAsString(list);
    }
    if (returnedType == HealthCheckStatus.class) {
        final HealthCheckStatus status = new HealthCheckStatus();
        status.setStatus(HealthCheckStatus.Status.KO);
        status.setComment("Something went wrong");
        return new MapperBuilder().setPretty(false).build().writeObjectAsString(status);
    }
    if (returnedType == Schema.class) {
        final Schema.Entry entry = new Schema.Entry();
        entry.setName("column1");
        entry.setType(Type.STRING);
        final Schema schema = new Schema();
        schema.setEntries(new ArrayList<>());
        schema.getEntries().add(entry);
        return new MapperBuilder().setPretty(false).build().writeObjectAsString(schema);
    }
    if (returnedType == ValidationResult.class) {
        final ValidationResult status = new ValidationResult();
        status.setStatus(ValidationResult.Status.KO);
        status.setComment("Something went wrong");
        return new MapperBuilder().setPretty(false).build().writeObjectAsString(status);
    }
    return "{\n" + Stream.of(returnedType.getDeclaredFields()).map(f -> " \"" + f.getName() + "\": " + createSample(f.getType())).collect(joining("\n")) + "\n}";
}
Also used : MapperBuilder(org.apache.johnzon.mapper.MapperBuilder) Condition(org.talend.sdk.component.api.configuration.condition.meta.Condition) RequiredArgsConstructor(lombok.RequiredArgsConstructor) AnnotationFinder(org.apache.xbean.finder.AnnotationFinder) ConfigurationTypeParameterEnricher(org.talend.sdk.component.runtime.manager.reflect.parameterenricher.ConfigurationTypeParameterEnricher) Type(org.talend.sdk.component.api.service.schema.Type) Collectors.toMap(java.util.stream.Collectors.toMap) Pair(org.apache.commons.lang3.tuple.Pair) Map(java.util.Map) Defaults(org.talend.sdk.component.runtime.reflect.Defaults) ENGLISH(java.util.Locale.ENGLISH) UiParameterEnricher(org.talend.sdk.component.runtime.manager.reflect.parameterenricher.UiParameterEnricher) Collectors.toSet(java.util.stream.Collectors.toSet) Validations(org.talend.sdk.component.api.configuration.constraint.meta.Validations) JsonbBuilder(javax.json.bind.JsonbBuilder) ActiveIf(org.talend.sdk.component.api.configuration.condition.ActiveIf) Collection(java.util.Collection) Ui(org.talend.sdk.component.api.configuration.ui.meta.Ui) Mapper(org.apache.johnzon.mapper.Mapper) Set(java.util.Set) FilterOutputStream(java.io.FilterOutputStream) PropertyOrderStrategy(javax.json.bind.config.PropertyOrderStrategy) Collectors.joining(java.util.stream.Collectors.joining) StandardCharsets(java.nio.charset.StandardCharsets) InvocationTargetException(java.lang.reflect.InvocationTargetException) GenericType(javax.ws.rs.core.GenericType) Base64(java.util.Base64) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) Stream(java.util.stream.Stream) Documentation(org.talend.sdk.component.api.meta.Documentation) Modifier(java.lang.reflect.Modifier) Annotation(java.lang.annotation.Annotation) Configuration(org.apache.deltaspike.core.api.config.Configuration) PRIVATE(lombok.AccessLevel.PRIVATE) MapperBuilder(org.apache.johnzon.mapper.MapperBuilder) Proxy(java.lang.reflect.Proxy) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) Client(javax.ws.rs.client.Client) Collectors.collectingAndThen(java.util.stream.Collectors.collectingAndThen) Function(java.util.function.Function) Supplier(java.util.function.Supplier) JsonbConfig(javax.json.bind.JsonbConfig) AutoLayout(org.talend.sdk.component.api.configuration.ui.layout.AutoLayout) Schema(org.talend.sdk.component.api.service.schema.Schema) ValidationParameterEnricher(org.talend.sdk.component.runtime.manager.reflect.parameterenricher.ValidationParameterEnricher) ConfigProperty(org.apache.deltaspike.core.api.config.ConfigProperty) FileArchive(org.apache.xbean.finder.archive.FileArchive) ArrayList(java.util.ArrayList) ClientBuilder(javax.ws.rs.client.ClientBuilder) ComponentServerConfiguration(org.talend.sdk.component.server.configuration.ComponentServerConfiguration) Collectors.mapping(java.util.stream.Collectors.mapping) Validation(org.talend.sdk.component.api.configuration.constraint.meta.Validation) ConfigurationType(org.talend.sdk.component.api.configuration.type.meta.ConfigurationType) GridLayout(org.talend.sdk.component.api.configuration.ui.layout.GridLayout) Comparator.comparing(java.util.Comparator.comparing) JsonbJaxrsProvider(org.apache.johnzon.jaxrs.jsonb.jaxrs.JsonbJaxrsProvider) ValidationResult(org.talend.sdk.component.api.service.asyncvalidation.ValidationResult) ConditionParameterEnricher(org.talend.sdk.component.runtime.manager.reflect.parameterenricher.ConditionParameterEnricher) OutputStream(java.io.OutputStream) PrintStream(java.io.PrintStream) BaseEnvironmentProvider(org.talend.sdk.component.junit.environment.BaseEnvironmentProvider) ParameterExtensionEnricher(org.talend.sdk.component.spi.parameter.ParameterExtensionEnricher) MalformedURLException(java.net.MalformedURLException) Files(java.nio.file.Files) JarArchive(org.apache.xbean.finder.archive.JarArchive) Values(org.talend.sdk.component.api.service.completion.Values) Optional.ofNullable(java.util.Optional.ofNullable) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) Collectors.toList(java.util.stream.Collectors.toList) TreeMap(java.util.TreeMap) HealthCheckStatus(org.talend.sdk.component.api.service.healthcheck.HealthCheckStatus) JarLocation.jarLocation(org.apache.ziplock.JarLocation.jarLocation) Data(lombok.Data) APPLICATION_JSON_TYPE(javax.ws.rs.core.MediaType.APPLICATION_JSON_TYPE) Jsonb(javax.json.bind.Jsonb) WebTarget(javax.ws.rs.client.WebTarget) Comparator(java.util.Comparator) ActionType(org.talend.sdk.component.api.service.ActionType) NoArgsConstructor(lombok.NoArgsConstructor) Schema(org.talend.sdk.component.api.service.schema.Schema) Values(org.talend.sdk.component.api.service.completion.Values) ValidationResult(org.talend.sdk.component.api.service.asyncvalidation.ValidationResult) HealthCheckStatus(org.talend.sdk.component.api.service.healthcheck.HealthCheckStatus)

Aggregations

PrintStream (java.io.PrintStream)2 Collection (java.util.Collection)2 Map (java.util.Map)2 Set (java.util.Set)2 Collectors.toMap (java.util.stream.Collectors.toMap)2 Stream (java.util.stream.Stream)2 Slf4j (lombok.extern.slf4j.Slf4j)2 Schema (org.talend.sdk.component.api.service.schema.Schema)2 Type (org.talend.sdk.component.api.service.schema.Type)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 FilterOutputStream (java.io.FilterOutputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 Annotation (java.lang.annotation.Annotation)1 Field (java.lang.reflect.Field)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Modifier (java.lang.reflect.Modifier)1 Modifier.isStatic (java.lang.reflect.Modifier.isStatic)1