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.Set;
025import java.util.stream.Collectors;
026import java.util.stream.Stream;
027
028import org.talend.sdk.component.api.component.ReturnVariables;
029import org.talend.sdk.component.api.component.ReturnVariables.ReturnVariable;
030import org.talend.sdk.component.api.input.Emitter;
031import org.talend.sdk.component.api.input.PartitionMapper;
032import org.talend.sdk.component.api.processor.Processor;
033import org.talend.sdk.component.api.standalone.DriverRunner;
034import org.talend.sdk.component.spi.component.ComponentMetadataEnricher;
035
036/**
037 * Search annotation {@link ReturnVariable} and add a new meta information about description of return variables.
038 * NOTE. This functionality is used only in Studio.
039 */
040public class ReturnVariableMetadataEnricher implements ComponentMetadataEnricher {
041
042    private static final Set<Class<? extends Annotation>> SUPPORTED_ANNOTATIONS =
043            new HashSet<>(Arrays.asList(PartitionMapper.class, Emitter.class, Processor.class, DriverRunner.class));
044
045    public static final String META_KEY_RETURN_VARIABLE = "variables::return::value";
046
047    private static final String VALUE_DELIMITER = "\\:";
048
049    private static final String LINE_DELIMITER = "\\;";
050
051    @Override
052    public Map<String, String> onComponent(final Type type, final Annotation[] annotations) {
053        boolean noneMatch =
054                Stream.of(annotations).map(Annotation::annotationType).noneMatch(SUPPORTED_ANNOTATIONS::contains);
055        if (noneMatch) {
056            return Collections.emptyMap();
057        }
058
059        String returnVariableMetaValue = Stream
060                .concat(Stream.of(annotations),
061                        Stream
062                                .of(annotations)
063                                .filter(a -> a.annotationType().equals(ReturnVariables.class))
064                                .map(ReturnVariables.class::cast)
065                                .map(ReturnVariables::value)
066                                .flatMap(Stream::of))
067                .filter(a -> a.annotationType().equals(ReturnVariable.class))
068                .map(ReturnVariable.class::cast)
069                .map(ReturnVariableMetadataEnricher::makeReturnVariableString)
070                .collect(Collectors.joining(LINE_DELIMITER));
071
072        if (returnVariableMetaValue.isEmpty()) {
073            return Collections.emptyMap();
074        }
075
076        return Collections.singletonMap(META_KEY_RETURN_VARIABLE, returnVariableMetaValue);
077    }
078
079    private static String makeReturnVariableString(final ReturnVariable var) {
080        return var.value() + VALUE_DELIMITER + var.type().getCanonicalName() + VALUE_DELIMITER
081                + var.availability().getKey() + VALUE_DELIMITER + var.description();
082    }
083}