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.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.Locale; 024import java.util.stream.Stream; 025 026import org.talend.sdk.component.api.service.configuration.LocalConfiguration; 027import org.talend.sdk.component.runtime.manager.ParameterMeta; 028 029public class MaxBatchSizeParamBuilder { 030 031 private final String name = "$maxBatchSize"; 032 033 private final Integer defaultValue; 034 035 private final ParameterMeta root; 036 037 private final String layoutType; 038 039 public MaxBatchSizeParamBuilder(final ParameterMeta root, final String componentSimpleClassName, 040 final LocalConfiguration configuration) { 041 this.root = root; 042 this.layoutType = getLayoutType(root); 043 this.defaultValue = !isActive(componentSimpleClassName, configuration) ? -1 044 : Integer 045 .parseInt(ofNullable(configuration.get(componentSimpleClassName + "._maxBatchSize.value")) 046 .orElseGet(() -> ofNullable(configuration.get("_maxBatchSize.value")).orElse("1000")) 047 .trim()); 048 } 049 050 private boolean isActive(final String componentSimpleClassName, final LocalConfiguration configuration) { 051 return Boolean 052 .parseBoolean(ofNullable(configuration.get(componentSimpleClassName + "._maxBatchSize.active")) 053 .orElseGet(() -> ofNullable(configuration.get("_maxBatchSize.active")).orElse("true")) 054 .trim()); 055 } 056 057 public ParameterMeta newBulkParameter() { 058 return defaultValue <= 0 ? null : new ParameterMeta(new ParameterMeta.Source() { 059 060 @Override 061 public String name() { 062 return name; 063 } 064 065 @Override 066 public Class<?> declaringClass() { 067 return MaxBatchSizeParamBuilder.class; 068 } 069 }, Integer.class, ParameterMeta.Type.NUMBER, root.getPath() + "." + name, name, 070 concat(Stream.of(MaxBatchSizeParamBuilder.class.getPackage().getName()), 071 Stream.of(ofNullable(root.getI18nPackages()).orElse(new String[0]))).toArray(String[]::new), 072 emptyList(), emptyList(), new HashMap<String, String>() { 073 074 { 075 put("tcomp::ui::defaultvalue::value", String.valueOf(defaultValue)); 076 put("tcomp::validation::min", "1"); 077 } 078 }, true); 079 } 080 081 private String getLayoutType(final ParameterMeta root) { 082 if (root == null) { 083 return "tcomp::ui::gridlayout::Advanced::value"; 084 } 085 086 final String rootLayoutType = root 087 .getMetadata() 088 .keySet() 089 .stream() 090 .filter(k -> k.contains("tcomp::ui") && (k.contains("layout") || k.contains("optionsorder"))) 091 .map(k -> k.split("::")) 092 .filter(s -> s.length > 2) 093 .map(s -> s[2]) 094 .findFirst() 095 .orElse("default"); 096 switch (rootLayoutType.toLowerCase(Locale.ROOT)) { 097 case "verticallayout": 098 return "tcomp::ui::verticallayout"; 099 case "horizontallayout": 100 return "tcomp::ui::horizontallayout"; 101 case "autolayout": 102 return "tcomp::ui::autolayout"; 103 case "optionsorder": 104 return "tcomp::ui::optionsorder"; 105 case "default": 106 return null; 107 case "gridlayout": 108 default: 109 return "tcomp::ui::gridlayout::Advanced::value"; 110 } 111 } 112 113 public String getLayoutType() { 114 return layoutType; 115 } 116}