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.AfterVariables;
029import org.talend.sdk.component.api.component.AfterVariables.AfterVariable;
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.spi.component.ComponentMetadataEnricher;
034
035/**
036 * Search annotation {@link AfterVariable} and add a new meta information about description of after variables.
037 * NOTE. This functionality is used only in Studio.
038 */
039@Deprecated
040public class AfterVariableMetadataEnricher implements ComponentMetadataEnricher {
041
042    private static final Set<Class<? extends Annotation>> SUPPORTED_ANNOTATIONS =
043            new HashSet<>(Arrays.asList(PartitionMapper.class, Emitter.class, Processor.class));
044
045    public static final String META_KEY_AFTER_VARIABLE = "variables::after::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 afterVariableMetaValue = Stream
060                .concat(Stream.of(annotations),
061                        Stream
062                                .of(annotations)
063                                .filter(a -> a.annotationType().equals(AfterVariables.class))
064                                .map(AfterVariables.class::cast)
065                                .map(AfterVariables::value)
066                                .flatMap(Stream::of))
067                .filter(a -> a.annotationType().equals(AfterVariable.class))
068                .map(AfterVariable.class::cast)
069                .map(AfterVariableMetadataEnricher::makeAfterVariableString)
070                .collect(Collectors.joining(LINE_DELIMITER));
071
072        if (afterVariableMetaValue.isEmpty()) {
073            return Collections.emptyMap();
074        }
075
076        return Collections.singletonMap(META_KEY_AFTER_VARIABLE, afterVariableMetaValue);
077    }
078
079    private static String makeAfterVariableString(final AfterVariable a) {
080        return a.value() + VALUE_DELIMITER + a.type().getCanonicalName() + VALUE_DELIMITER + a.description();
081    }
082}