001/** 002 * Copyright (C) 2006-2024 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.builtinparams; 017 018import static java.util.Collections.emptyList; 019import static java.util.Optional.ofNullable; 020import static java.util.stream.Stream.concat; 021 022import java.util.HashMap; 023import java.util.stream.Stream; 024 025import org.talend.sdk.component.api.service.configuration.LocalConfiguration; 026import org.talend.sdk.component.runtime.manager.ParameterMeta; 027 028public class StreamingLongParamBuilder { 029 030 private final ParameterMeta root; 031 032 private final String componentClassName; 033 034 private final String name; 035 036 private final String layoutType; 037 038 private final Long defaultValue; 039 040 private final LocalConfiguration configuration; 041 042 public StreamingLongParamBuilder(final ParameterMeta root, final String componentClassName, 043 final String parameterName, final LocalConfiguration configuration) { 044 this.root = root; 045 this.componentClassName = componentClassName; 046 this.name = parameterName; 047 this.configuration = configuration; 048 this.layoutType = findLayoutType(); 049 this.defaultValue = Long.parseLong(ofNullable(configuration.get(componentClassName + name)) 050 .orElseGet(() -> ofNullable(configuration.get(name)).orElse("-1")) 051 .trim()); 052 } 053 054 public ParameterMeta newBulkParameter() { 055 return new ParameterMeta(new ParameterMeta.Source() { 056 057 @Override 058 public String name() { 059 return name; 060 } 061 062 @Override 063 public Class<?> declaringClass() { 064 return StreamingLongParamBuilder.class; 065 } 066 }, Long.class, ParameterMeta.Type.NUMBER, root.getPath() + "." + name, name, 067 concat(Stream.of(StreamingLongParamBuilder.class.getPackage().getName()), 068 Stream.of(ofNullable(root.getI18nPackages()).orElse(new String[0]))).toArray(String[]::new), 069 emptyList(), emptyList(), new HashMap<String, String>() { 070 071 { 072 put("tcomp::ui::defaultvalue::value", String.valueOf(defaultValue)); 073 put("tcomp::validation::min", "-1"); 074 } 075 }, true); 076 } 077 078 public String getLayoutType() { 079 return layoutType; 080 } 081 082 private String findLayoutType() { 083 if (root == null) { 084 return "tcomp::ui::gridlayout::Advanced::value"; 085 } 086 087 final String rootLayoutType = root 088 .getMetadata() 089 .keySet() 090 .stream() 091 .filter(k -> k.contains("tcomp::ui") && (k.contains("layout") || k.contains("optionsorder"))) 092 .map(k -> k.split("::")) 093 .filter(s -> s.length > 2) 094 .map(s -> s[2]) 095 .findFirst() 096 .orElse("default"); 097 switch (rootLayoutType.toLowerCase(java.util.Locale.ROOT)) { 098 case "verticallayout": 099 return "tcomp::ui::verticallayout"; 100 case "horizontallayout": 101 return "tcomp::ui::horizontallayout"; 102 case "autolayout": 103 return "tcomp::ui::autolayout"; 104 case "optionsorder": 105 return "tcomp::ui::optionsorder"; 106 case "default": 107 return null; 108 case "gridlayout": 109 default: 110 return "tcomp::ui::gridlayout::Advanced::value"; 111 } 112 } 113 114 public static class StreamingMaxRecordsParamBuilder extends StreamingLongParamBuilder { 115 116 public StreamingMaxRecordsParamBuilder(final ParameterMeta root, final String componentClassName, 117 final LocalConfiguration configuration) { 118 super(root, componentClassName, "$maxRecords", configuration); 119 } 120 } 121 122 public static class StreamingMaxDurationMsParamBuilder extends StreamingLongParamBuilder { 123 124 public StreamingMaxDurationMsParamBuilder(final ParameterMeta root, final String componentClassName, 125 final LocalConfiguration configuration) { 126 super(root, componentClassName, "$maxDurationMs", configuration); 127 } 128 } 129 130}