001/**
002 * Copyright (C) 2006-2025 Talend Inc. - www.talend.com
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.talend.sdk.component.runtime.manager.extension;
017
018import java.lang.annotation.Annotation;
019import java.lang.reflect.Type;
020import java.util.Arrays;
021import java.util.Collections;
022import java.util.HashSet;
023import java.util.Map;
024import java.util.Optional;
025import java.util.Set;
026import java.util.stream.Stream;
027
028import org.talend.sdk.component.api.meta.ConditionalOutput;
029import org.talend.sdk.component.api.processor.Processor;
030import org.talend.sdk.component.spi.component.ComponentMetadataEnricher;
031
032/**
033 * Search annotation {@link ConditionalOutput} and add a new meta information about name of related
034 * AvailableOutputsFlow.
035 * NOTE. This functionality is used only in Studio.
036 */
037public class ConditionalOutputMetadataEnricher implements ComponentMetadataEnricher {
038
039    private static final Set<Class<? extends Annotation>> SUPPORTED_ANNOTATIONS =
040            new HashSet<>(Collections.singletonList(Processor.class));
041
042    public static final String META_KEY_RETURN_VARIABLE = "conditional_output::value";
043
044    @Override
045    public Map<String, String> onComponent(final Type type, final Annotation[] annotations) {
046        final boolean noneMatch =
047                Stream.of(annotations).map(Annotation::annotationType).noneMatch(SUPPORTED_ANNOTATIONS::contains);
048        if (noneMatch) {
049            return Collections.emptyMap();
050        }
051
052        final Optional<ConditionalOutput> metaValue = Arrays.stream(annotations)
053                .filter(a -> a.annotationType().equals(ConditionalOutput.class))
054                .findFirst()
055                .map(ConditionalOutput.class::cast);
056
057        return metaValue
058                .map(conditionalOutput -> Collections.singletonMap(META_KEY_RETURN_VARIABLE, conditionalOutput.value()))
059                .orElse(Collections.emptyMap());
060
061    }
062
063}