-
Notifications
You must be signed in to change notification settings - Fork 210
/
PropertyType.java
615 lines (518 loc) · 25.3 KB
/
PropertyType.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p>
* http:https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.qlangtech.tis.extension.impl;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.google.common.collect.Sets;
import com.qlangtech.tis.TIS;
import com.qlangtech.tis.extension.Describable;
import com.qlangtech.tis.extension.Descriptor;
import com.qlangtech.tis.extension.ElementPluginDesc;
import com.qlangtech.tis.extension.IPropertyType;
import com.qlangtech.tis.extension.util.GroovyShellEvaluate;
import com.qlangtech.tis.extension.util.GroovyShellUtil;
import com.qlangtech.tis.extension.util.MultiItemsViewType;
import com.qlangtech.tis.extension.util.PluginExtraProps;
import com.qlangtech.tis.manage.common.Option;
import com.qlangtech.tis.plugin.annotation.FormField;
import com.qlangtech.tis.plugin.annotation.FormFieldType;
import com.qlangtech.tis.plugin.annotation.SubForm;
import com.qlangtech.tis.plugin.annotation.Validator;
import com.qlangtech.tis.plugin.ds.CMeta;
import com.qlangtech.tis.plugin.ds.DataTypeMeta;
import com.qlangtech.tis.runtime.module.misc.IMessageHandler;
import com.qlangtech.tis.trigger.util.JsonUtil;
import org.apache.commons.beanutils.ConvertUtilsBean;
import org.apache.commons.beanutils.Converter;
import org.apache.commons.lang.StringUtils;
import org.jvnet.tiger_types.Types;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* @author 百岁([email protected])
* @date 2021-04-11 12:07
*/
public class PropertyType implements IPropertyType {
private static final ConvertUtilsBean convertUtils = new ConvertUtilsBean();
static {
convertUtils.register(new Converter() {
@Override
public <T> T convert(Class<T> type, Object value) {
if (value instanceof JsonUtil.UnCacheString) {
return (T) ((JsonUtil.UnCacheString) value).getValue();
} else if (value instanceof JSONArray) {
JSONArray array = (JSONArray) value;
List<String> convert = array.toJavaList(String.class);
return (T) convert;
} else {
return (T) value;
}
}
}, List.class);
}
// private final Optional<Descriptor.ElementPluginDesc> parentPluginDesc;
public final Class clazz;
public final Type type;
private volatile Class itemType;
public final String displayName;
public final FormField formField;
public final Field f;
private Boolean inputRequired;
private MultiItemsViewType multiItemsViewType;
public PluginExtraProps.Props extraProp;
public PropertyType(Field f, FormField formField) {
this(f, f.getType(), f.getGenericType(), f.getName(), formField);
}
PropertyType(Field f, Class clazz, Type type, String displayName, FormField formField) {
this.f = f;
this.clazz = clazz;
this.type = type;
this.displayName = displayName;
if (formField == null) {
throw new IllegalStateException("param formField can not be null");
}
this.formField = formField;
}
private static String getStrProp(PluginExtraProps.Props props, String key) {
return props.getProps().getString(key);
}
public static MultiItemsViewType createMultiItemsViewType(PluginExtraProps.Props props) {
// if (this.multiItemsViewType == null) {
Optional<CMeta.ElementCreatorFactory> elementCreator = Optional.empty();
try {
String selectElementCreator = getStrProp(props, CMeta.KEY_ELEMENT_CREATOR_FACTORY);
if (StringUtils.isNotEmpty(selectElementCreator)) {
elementCreator = Optional.of((CMeta.ElementCreatorFactory) //
TIS.get().getPluginManager().uberClassLoader.loadClass(selectElementCreator).newInstance());
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return new MultiItemsViewType(MultiItemsViewType.ViewType.parse(getStrProp(props, PluginExtraProps.Props.KEY_VIEW_TYPE)),
elementCreator);
// }
// return this.multiItemsViewType;
}
public static Map<String, /*** fieldname*/PropertyType> filterFieldProp(Map<String,
/*** fieldname*/IPropertyType> props) {
return props.entrySet().stream().filter((e) -> e.getValue() instanceof PropertyType) //
.collect(Collectors.toMap((e) -> e.getKey(), (e) -> (PropertyType) e.getValue()));
}
/**
* 可能plugin form 表单需要几个步骤才能 填充完一个plugin form 表单就需要单独取出部分表单属性去渲染前端页面
*
* @param clazz
* @return
*/
public static Map<String, /*** fieldname */IPropertyType> buildPropertyTypes( //
Optional<ElementPluginDesc> descriptor,
Class<? extends Describable> clazz) {
try {
Map<String, IPropertyType> propMapper = new HashMap<>();
Optional<PluginExtraProps> extraProps = PluginExtraProps.load(descriptor, clazz);
// 支持使用继承的方式来实现复用,例如:DataXHiveWriter继承DataXHdfsWriter来实现
PluginExtraProps.visitAncestorsClass(clazz, new PluginExtraProps.IClassVisitor<Void>() {
@Override
public Void process(Class<?> targetClass, Void v) {
FormField formField = null;
SubForm subFormFields = null;
// ptype = null;
Class<? extends Describable> subFromDescClass = null;
try {
for (Field f : targetClass.getDeclaredFields()) {
if (!Modifier.isPublic(f.getModifiers()) || Modifier.isStatic(f.getModifiers())) {
continue;
}
if ((subFormFields = f.getAnnotation(SubForm.class)) != null) {
subFromDescClass = subFormFields.desClazz();
if (subFromDescClass == null) {
throw new IllegalStateException("field " + f.getName() + "'s SubForm annotation " + "descClass can not be null");
}
final Descriptor subFormDesc =
Objects.requireNonNull(TIS.get().getDescriptor(subFromDescClass),
"subFromDescClass:" + subFromDescClass + " relevant descriptor can not be null");
propMapper.put(f.getName(), new SuFormProperties(clazz, f, subFormFields, subFormDesc,
filterFieldProp(buildPropertyTypes(ElementPluginDesc.create(subFormDesc), subFromDescClass))));
} else if ((formField = f.getAnnotation(FormField.class)) != null) {
PluginExtraProps.Props fieldExtraProps = null;
final PropertyType ptype = new PropertyType(f, formField);
if (extraProps.isPresent() && (fieldExtraProps = extraProps.get().getProp(f.getName())) != null) {
ptype.setExtraProp(fieldExtraProps);
String placeholder = fieldExtraProps.getPlaceholder();
Object dftVal = fieldExtraProps.getDftVal();
String help = fieldExtraProps.getHelpContent();
JSONObject props = fieldExtraProps.getProps();
// Object disabled = props.get(PluginExtraProps.KEY_DISABLE);
// if (disabled != null) {
// if (StringUtils.startsWith(String.valueOf(disabled), IMessageHandler.TSEARCH_PACKAGE)) {
// props.put(PluginExtraProps.KEY_DISABLE, GroovyShellEvaluate.scriptEval(String.valueOf(disabled)));
// }
// }
// disabled = props.get(PluginExtraProps.KEY_DISABLE);
// if (disabled != null) {
// if ((disabled instanceof JsonUtil.UnCacheString && ((JsonUtil.UnCacheString<Boolean>) disabled).getValue())
// || (disabled instanceof Boolean && (Boolean) disabled)
// ) {
// propMapper.remove(f.getName());
// continue;
// }
// }
if (fieldExtraProps.getBoolean(PluginExtraProps.KEY_DISABLE)) {
propMapper.remove(f.getName());
continue;
//return null;
}
if (StringUtils.isNotEmpty(help) && StringUtils.startsWith(help,
IMessageHandler.TSEARCH_PACKAGE)) {
props.put(PluginExtraProps.Props.KEY_HELP, GroovyShellUtil.eval(help));
}
if (dftVal != null && StringUtils.startsWith(String.valueOf(dftVal),
IMessageHandler.TSEARCH_PACKAGE)) {
final PropertyType pt = ptype;
Function<Object, Object> process = pt.getEnumFieldMode() != null ?
pt.getEnumFieldMode().createDefaultValProcess(targetClass, f) :
Function.identity();
props.put(PluginExtraProps.KEY_DFTVAL_PROP,
GroovyShellEvaluate.scriptEval(String.valueOf(dftVal), process));
}
if (placeholder != null && StringUtils.startsWith(placeholder,
IMessageHandler.TSEARCH_PACKAGE)) {
props.put(PluginExtraProps.KEY_PLACEHOLDER_PROP,
GroovyShellEvaluate.scriptEval(placeholder));
}
if (descriptor.isPresent() //
&& (formField.type() == FormFieldType.ENUM)) {
resolveEnumProp(descriptor.get().getElementDesc(), fieldExtraProps, (opts) -> {
return Option.toJson((List<Option>) opts);
});
}
if (descriptor.isPresent() //
&& (formField.type() == FormFieldType.MULTI_SELECTABLE)) {
final PluginExtraProps.Props feProps = fieldExtraProps;
ElementPluginDesc paretPluginRef = descriptor.get();
resolveEnumProp(paretPluginRef.getElementDesc(), feProps, (cols) -> {
final List<CMeta> mcols = (List<CMeta>) cols;
// if (CollectionUtils.isEmpty(mcols)) {
// throw new IllegalStateException("mcols can not be empty");
// }
return ptype.multiSelectablePropProcess((viewType) -> {
// cols有两种显示模式
MultiItemsViewType multiItemsViewType = viewType;
switch (multiItemsViewType.viewType) {
case IdList:
return Option.toJson(mcols);
case TupleList:
return DataTypeMeta.createViewBiz(multiItemsViewType.getElementPropertyKeys(), mcols);
default:
throw new IllegalStateException("unhandle view type:" + multiItemsViewType);
}
}, true);
});
}
}
propMapper.put(f.getName(), ptype);
} else {
// throw new IllegalStateException("field:" + f.getName() + " is illegal");
}
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return null;
}
});
return propMapper;
} catch (Exception e) {
throw new RuntimeException("parse desc:" + clazz.getName(), e);
}
}
private static JSONArray resolveEnumProp(Descriptor descriptor, PluginExtraProps.Props fieldExtraProps,
Function<Object, Object> process) {
Object anEnum = fieldExtraProps.getProps().get(Descriptor.KEY_ENUM_PROP);
JSONArray enums = new JSONArray();
if (anEnum != null && anEnum instanceof String) {
try {
GroovyShellUtil.descriptorThreadLocal.set(descriptor);
fieldExtraProps.getProps().put(Descriptor.KEY_ENUM_PROP, GroovyShellEvaluate.scriptEval((String) anEnum, process));
} finally {
GroovyShellUtil.descriptorThreadLocal.remove();
}
}
return enums;
}
public static Map<String, /*** fieldname*/PropertyType> filterFieldProp(Descriptor descriptor) {
return filterFieldProp(descriptor.getPropertyTypes());
}
private MultiItemsViewType getMultiItemsViewType() {
if (this.multiItemsViewType == null) {
this.multiItemsViewType = createMultiItemsViewType(new PluginExtraProps.Props(new JSONObject()));
}
return this.multiItemsViewType;
}
public void setMultiItemsViewType(MultiItemsViewType multiItemsViewType) {
this.multiItemsViewType = multiItemsViewType;
}
public void setMultiItemsViewType(PropertyType oldPt) {
this.multiItemsViewType = oldPt.getMultiItemsViewType();
}
/**
* 是否是主键
*
* @return
*/
public boolean isIdentity() {
return this.formField.identity();
}
public JSONObject getExtraProps() {
if (this.extraProp == null) {
return null;
}
return this.extraProp.getProps();
}
@JSONField(serialize = false)
public EnumFieldMode getEnumFieldMode() {
if (this.formField.type() != FormFieldType.ENUM) {
return null;
}
return EnumFieldMode.parse(this.extraProp != null ? getExtraProps().getString("enumMode") : null);
}
public void setExtraProp(PluginExtraProps.Props extraProp) {
this.extraProp = extraProp;
}
public Object dftVal() {
if (this.extraProp == null) {
return null;
}
return this.extraProp.getDftVal();
}
public int ordinal() {
return formField.ordinal();
}
public boolean advance() {
return (this.extraProp != null && this.extraProp.isAdvance()) || formField.advance();
}
public int typeIdentity() {
return formField.type().getIdentity();
}
private Validator[] validators;
public Validator[] getValidator() {
if (this.validators == null) {
Set<Validator> result = Sets.newHashSet();
Map<Validator, PluginExtraProps.Props.ValidatorCfg> validators
= (extraProp == null ? Collections.emptyList() : (this.extraProp.getExtraValidators()))
.stream().collect(Collectors.toMap((v) -> ((PluginExtraProps.Props.ValidatorCfg) v).validator, (v) -> (PluginExtraProps.Props.ValidatorCfg) v));
PluginExtraProps.Props.ValidatorCfg validatorCfg = null;
for (Validator v : formField.validate()) {
if ((validatorCfg = validators.get(v)) != null) {
if (!validatorCfg.disable) {
result.add(v);
}
} else {
result.add(v);
}
}
for (PluginExtraProps.Props.ValidatorCfg cfg : validators.values()) {
if (!cfg.disable) {
result.add(cfg.validator);
}
}
this.validators = result.toArray(new Validator[result.size()]);
}
return this.validators;
}
public boolean isInputRequired() {
if (inputRequired == null) {
inputRequired = false;
for (Validator v : this.getValidator()) {
if (v == Validator.require) {
return inputRequired = true;
}
}
}
return inputRequired;
}
// PropertyType(Method getter) {
// this(getter.getReturnType(), getter.getGenericReturnType(), getter.toString());
// }
public Enum[] getEnumConstants() {
return (Enum[]) clazz.getEnumConstants();
}
/**
* If the property is a collection/array type, what is an item type?
*/
public Class getItemType() {
if (itemType == null)
itemType = computeItemType();
return itemType;
}
/**
* 取得实例的值
*
* @param instance
* @return
*/
public Object getVal(Object instance) {
try {
Object val = this.f.get(instance);
if (this.formField.type() == FormFieldType.MULTI_SELECTABLE) {
return this.getMultiItemsViewType().serialize2Frontend(val);
// return this.extraProp.multiItemsViewType(this.getParentHostClass()).serialize2Frontend(val);
}
return this.formField.type().valProcessor.serialize2Output(this, val);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public <T> T multiSelectablePropProcess(Function<MultiItemsViewType, T> consumer) {
return multiSelectablePropProcess(consumer, false);
}
/**
* 当类型为multiSelectable 属性的设置
*
* @param consumer
*/
public <T> T multiSelectablePropProcess(Function<MultiItemsViewType, T> consumer, boolean validate) {
if (this.formField.type() == FormFieldType.MULTI_SELECTABLE) {
return consumer.apply(this.getMultiItemsViewType());
}
if (validate) {
throw new IllegalStateException(" illegal form type:" + this.formField.type());
}
return null;
}
public void setVal(Object instance, Object val) {
PropVal fieldVal = new PropVal(val, this.clazz, this.extraProp);
try {
this.f.set(instance, this.formField.type().valProcessor.processInput(instance, fieldVal));
} catch (Throwable e) {
throw new RuntimeException("\ntarget instance:" + instance.getClass()
+ "\nfield:" + this.f.getName() + ("\nprop class:" + val.getClass()), e);
}
}
public static class PropVal {
private final Object val;
private final Class targetClazz;
public final PluginExtraProps.Props extraProp;
public PropVal(Object val, Class targetClazz, PluginExtraProps.Props extraProp) {
this.val = val;
this.targetClazz = targetClazz;
this.extraProp = extraProp;
}
public <T> T convertedVal() {
return (T) convertUtils.convert(val, this.targetClazz);
}
public Object rawVal() {
return this.val;
}
public Class getTargetClazz() {
return this.targetClazz;
}
}
private Class computeItemType() {
if (clazz.isArray()) {
return clazz.getComponentType();
}
if (Collection.class.isAssignableFrom(clazz)) {
Type col = Types.getBaseClass(type, Collection.class);
if (col instanceof ParameterizedType) {
return Types.erasure(Types.getTypeArgument(col, 0));
} else {
return Object.class;
}
}
return null;
}
/**
* Returns {@link Descriptor} whose 'clazz' is the same as {@link #getItemType() the item type}.
*/
public Descriptor getItemTypeDescriptor() {
return TIS.get().getDescriptor(getItemType());
}
public boolean isDescribable() {
// }
return Describable.class.isAssignableFrom(clazz);
}
public Descriptor getItemTypeDescriptorOrDie() {
Class it = getItemType();
if (it == null) {
throw new AssertionError(clazz + " is not an array/collection type in " + displayName + ". See " + "https"
+ ":https://wiki.jenkins-ci.org/display/JENKINS/My+class+is+missing+descriptor");
}
Descriptor d = TIS.get().getDescriptor(it);
if (d == null)
throw new AssertionError(it + " is missing its descriptor in " + displayName + ". See https://wiki" +
".jenkins-ci.org/display/JENKINS/My+class+is+missing+descriptor");
return d;
}
private Function<List<? extends Descriptor>, List<? extends Descriptor>> subDescFilter;
/**
* Returns all the descriptors that produce types assignable to the property type.
*/
public List<? extends Descriptor> getApplicableDescriptors() {
JSONObject eprops = null;
if (subDescFilter == null && (eprops = this.getExtraProps()) != null) {
String subDescEnumFilter = eprops.getString(PluginExtraProps.KEY_ENUM_FILTER);
if (StringUtils.isNotEmpty(subDescEnumFilter)) {
String className = this.clazz.getSimpleName() + "_" + this.f.getName() + "_SubFilter";
String pkg = this.clazz.getPackage().getName();
String script = " package " + pkg + " ;\n" //
+ "import java.util.function.Function;\n" //
+ "import java.util.List;\n" //
+ "import " + com.qlangtech.tis.extension.Descriptor.class.getName() + ";\n"
// + "import com.qlangtech.plugins.incr.flink.chunjun.sink.SinkTabPropsExtends;\n"
+ "class " + className + " implements Function<List<? extends Descriptor>,List<? extends " //
+ "Descriptor>> { \n" //
+ " @Override \n" //
+ " public List<? extends Descriptor> apply" //
+ "(List<?" + " extends Descriptor> desc) {" //
+ subDescEnumFilter + " }" + "}";
subDescFilter = GroovyShellEvaluate.createParamizerScript(this.clazz, className, script);
}
}
if (subDescFilter == null) {
subDescFilter = (descs) -> descs;
}
return subDescFilter.apply(TIS.get().getDescriptorList(clazz));
}
/**
* Returns all the descriptors that produce types assignable to the item type for a collection property.
*/
public List<? extends Descriptor> getApplicableItemDescriptors() {
Class itemType = getItemType();
if (itemType == null) {
return null;
}
return TIS.get().getDescriptorList(itemType);
}
public Optional<CMeta.ElementCreatorFactory> getCMetaCreator() {
return this.multiItemsViewType.tupleFactory;
}
}