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.server.service;
017
018import static java.util.Collections.singleton;
019import static java.util.stream.Collectors.toList;
020import static java.util.stream.Collectors.toSet;
021
022import java.util.Collection;
023import java.util.Locale;
024import java.util.Objects;
025import java.util.Set;
026import java.util.stream.Stream;
027
028import javax.enterprise.context.ApplicationScoped;
029import javax.inject.Inject;
030
031import org.talend.sdk.component.container.Container;
032import org.talend.sdk.component.design.extension.repository.Config;
033import org.talend.sdk.component.runtime.base.Lifecycle;
034import org.talend.sdk.component.runtime.internationalization.FamilyBundle;
035import org.talend.sdk.component.runtime.manager.ComponentFamilyMeta;
036import org.talend.sdk.component.runtime.manager.ContainerComponentRegistry;
037import org.talend.sdk.component.runtime.manager.ParameterMeta;
038import org.talend.sdk.component.runtime.manager.reflect.parameterenricher.ActionParameterEnricher;
039import org.talend.sdk.component.server.front.model.ActionReference;
040
041import lombok.extern.slf4j.Slf4j;
042
043@Slf4j
044@ApplicationScoped
045public class ActionsService {
046
047    @Inject
048    private PropertiesService propertiesService;
049
050    public Collection<ActionReference> findActions(final String family, final Container container, final Locale locale,
051            final ComponentFamilyMeta.BaseMeta<Lifecycle> meta, final FamilyBundle familyBundle) {
052        final Set<ActionReference> actions = getActionReference(meta, familyBundle);
053        return findActions(family, actions, container, locale, familyBundle);
054    }
055
056    public Collection<ActionReference> findActions(final String family, final Container container, final Locale locale,
057            final Config config, final FamilyBundle familyBundle) {
058        final Set<ActionReference> actions =
059                getActionReference(toStream(singleton(config.getMeta())), family, familyBundle);
060        return findActions(family, actions, container, locale, familyBundle);
061    }
062
063    public Set<ActionReference> getActionReference(final ComponentFamilyMeta.BaseMeta<Lifecycle> meta,
064            final FamilyBundle familyBundle) {
065        return getActionReference(toStream(meta.getParameterMetas().get()), meta.getParent().getName(), familyBundle);
066    }
067
068    public Set<ActionReference> getActionReference(final Stream<ParameterMeta> parameters, final String familyName,
069            final FamilyBundle familyBundle) {
070        return parameters
071                .flatMap(p -> p.getMetadata().entrySet().stream())
072                .filter(e -> e.getKey().startsWith(ActionParameterEnricher.META_PREFIX))
073                .map(e -> {
074                    final String type = e.getKey().substring(ActionParameterEnricher.META_PREFIX.length());
075                    return new ActionReference(familyName, e.getValue(), type,
076                            familyBundle.actionDisplayName(type, e.getValue()).orElse(e.getValue()), null);
077                })
078                .collect(toSet());
079    }
080
081    private Collection<ActionReference> findActions(final String family, final Set<ActionReference> actions,
082            final Container container, final Locale locale, final FamilyBundle familyBundle) {
083        final ContainerComponentRegistry registry = container.get(ContainerComponentRegistry.class);
084        return registry
085                .getServices()
086                .stream()
087                .flatMap(s -> s.getActions().stream())
088                .filter(s -> s.getFamily().equals(family))
089                .filter(s -> actions
090                        .stream()
091                        .anyMatch(e -> s.getFamily().equals(e.getFamily()) && s.getType().equals(e.getType())
092                                && s.getAction().equals(e.getName())))
093                .map(s -> new ActionReference(s.getFamily(), s.getAction(), s.getType(),
094                        familyBundle.actionDisplayName(s.getType(), s.getAction()).orElse(s.getAction()),
095                        propertiesService
096                                .buildProperties(s.getParameters().get(), container.getLoader(), locale, null)
097                                .collect(toList())))
098                .collect(toList());
099    }
100
101    private Stream<ParameterMeta> toStream(final Collection<ParameterMeta> parameterMetas) {
102        return Stream
103                .concat(parameterMetas.stream(),
104                        parameterMetas
105                                .stream()
106                                .map(ParameterMeta::getNestedParameters)
107                                .filter(Objects::nonNull)
108                                .flatMap(this::toStream));
109    }
110}