Search in sources :

Example 1 with ElementListener

use of org.talend.sdk.component.api.processor.ElementListener in project component-runtime by Talend.

the class FormatterProcessor method format.

@ElementListener
public void format(final JsonObject data, @Output("formatted") final OutputEmitter<JsonObject> formatted) {
    if (data == null) {
        return;
    }
    final JsonObjectBuilder builder = factory.createObjectBuilder();
    if (lowerCase) {
        data.keySet().forEach(k -> builder.add(k, data.getString(k).toLowerCase(Locale.ROOT)));
    } else {
        data.keySet().forEach(k -> builder.add(k, data.getString(k).toUpperCase(Locale.ROOT)));
    }
    formatted.emit(builder.build());
}
Also used : JsonObjectBuilder(javax.json.JsonObjectBuilder) ElementListener(org.talend.sdk.component.api.processor.ElementListener)

Example 2 with ElementListener

use of org.talend.sdk.component.api.processor.ElementListener in project component-runtime by Talend.

the class ModelVisitor method validateProcessor.

private void validateProcessor(final Class<?> input) {
    final List<Method> producers = Stream.of(input.getMethods()).filter(m -> m.isAnnotationPresent(ElementListener.class)).collect(toList());
    if (producers.size() != 1) {
        throw new IllegalArgumentException(input + " must have a single @ElementListener method");
    }
    if (Stream.of(producers.get(0).getParameters()).filter(p -> {
        if (p.isAnnotationPresent(Output.class)) {
            if (!ParameterizedType.class.isInstance(p.getParameterizedType())) {
                throw new IllegalArgumentException("@Output parameter must be of type OutputEmitter");
            }
            final ParameterizedType pt = ParameterizedType.class.cast(p.getParameterizedType());
            if (OutputEmitter.class != pt.getRawType()) {
                throw new IllegalArgumentException("@Output parameter must be of type OutputEmitter");
            }
            return false;
        }
        return true;
    }).count() < 1) {
        throw new IllegalArgumentException(input + " doesn't have the input parameter on its producer method");
    }
    Stream.of(input.getMethods()).filter(m -> m.isAnnotationPresent(BeforeGroup.class) || m.isAnnotationPresent(AfterGroup.class)).forEach(m -> {
        if (m.getParameterCount() > 0) {
            throw new IllegalArgumentException(m + " must not have any parameter");
        }
    });
}
Also used : Emitter(org.talend.sdk.component.api.input.Emitter) PartitionMapper(org.talend.sdk.component.api.input.PartitionMapper) Producer(org.talend.sdk.component.api.input.Producer) AfterGroup(org.talend.sdk.component.api.processor.AfterGroup) BeforeGroup(org.talend.sdk.component.api.processor.BeforeGroup) Collection(java.util.Collection) Assessor(org.talend.sdk.component.api.input.Assessor) Split(org.talend.sdk.component.api.input.Split) OutputEmitter(org.talend.sdk.component.api.processor.OutputEmitter) Processor(org.talend.sdk.component.api.processor.Processor) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) Stream(java.util.stream.Stream) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) ElementListener(org.talend.sdk.component.api.processor.ElementListener) Annotation(java.lang.annotation.Annotation) PartitionSize(org.talend.sdk.component.api.input.PartitionSize) Method(java.lang.reflect.Method) Output(org.talend.sdk.component.api.processor.Output) ParameterizedType(java.lang.reflect.ParameterizedType) BeforeGroup(org.talend.sdk.component.api.processor.BeforeGroup) Method(java.lang.reflect.Method) AfterGroup(org.talend.sdk.component.api.processor.AfterGroup)

Example 3 with ElementListener

use of org.talend.sdk.component.api.processor.ElementListener in project component-runtime by Talend.

the class FormatterProcessor method length.

@ElementListener
public void length(@Input("firstName") final JsonObject firstName, @Input("lastName") final JsonObject lastName, @Output("formatted-firstName") final OutputEmitter<JsonObject> lowerCase, @Output("formatted-lastName") final OutputEmitter<JsonObject> upperCase) {
    final JsonObjectBuilder internal = factory.createObjectBuilder().add("key", (firstName == null ? lastName : firstName).getJsonObject("$$internal").getString("key"));
    lowerCase.emit(firstName == null ? null : factory.createObjectBuilder().add("data", firstName.getString("data").toLowerCase(ROOT)).add("$$internal", internal).build());
    upperCase.emit(lastName == null ? null : factory.createObjectBuilder().add("data", lastName.getString("data").toUpperCase(ROOT)).add("$$internal", internal).build());
}
Also used : JsonObjectBuilder(javax.json.JsonObjectBuilder) ElementListener(org.talend.sdk.component.api.processor.ElementListener)

Example 4 with ElementListener

use of org.talend.sdk.component.api.processor.ElementListener in project component-runtime by Talend.

the class ConcatProcessor method cat.

@ElementListener
public void cat(@Input("str1") final JsonObject str1, @Input("str2") final JsonObject str2, @Output final OutputEmitter<JsonObject> concat) {
    if (str1 == null && str2 == null)
        return;
    final JsonObjectBuilder builder = factory.createObjectBuilder();
    if (str1 != null) {
        str1.keySet().forEach(k -> builder.add(k, str1.get(k)));
    }
    if (str2 != null) {
        str2.keySet().forEach(k -> builder.add(k, str2.get(k)));
    }
    concat.emit(builder.build());
}
Also used : JsonObjectBuilder(javax.json.JsonObjectBuilder) ElementListener(org.talend.sdk.component.api.processor.ElementListener)

Aggregations

ElementListener (org.talend.sdk.component.api.processor.ElementListener)4 JsonObjectBuilder (javax.json.JsonObjectBuilder)3 Annotation (java.lang.annotation.Annotation)1 Method (java.lang.reflect.Method)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 Collection (java.util.Collection)1 List (java.util.List)1 Collectors.toList (java.util.stream.Collectors.toList)1 Stream (java.util.stream.Stream)1 Assessor (org.talend.sdk.component.api.input.Assessor)1 Emitter (org.talend.sdk.component.api.input.Emitter)1 PartitionMapper (org.talend.sdk.component.api.input.PartitionMapper)1 PartitionSize (org.talend.sdk.component.api.input.PartitionSize)1 Producer (org.talend.sdk.component.api.input.Producer)1 Split (org.talend.sdk.component.api.input.Split)1 AfterGroup (org.talend.sdk.component.api.processor.AfterGroup)1 BeforeGroup (org.talend.sdk.component.api.processor.BeforeGroup)1 Output (org.talend.sdk.component.api.processor.Output)1 OutputEmitter (org.talend.sdk.component.api.processor.OutputEmitter)1