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