use of com.qlangtech.tis.extension.impl.SuFormProperties in project tis by qlangtech.
the class DataxReader method createDataXReaderKey.
private static TIS.DataXReaderAppKey createDataXReaderKey(IPluginContext pluginContext, boolean db, String appname) {
return new TIS.DataXReaderAppKey(pluginContext, db, appname, new PluginStore.IPluginProcessCallback<DataxReader>() {
@Override
public void afterDeserialize(final DataxReader reader) {
List<PluginFormProperties> subFieldFormPropertyTypes = reader.getDescriptor().getSubPluginFormPropertyTypes();
if (subFieldFormPropertyTypes.size() > 0) {
// 加载子字段
subFieldFormPropertyTypes.forEach((pt) -> {
pt.accept(new PluginFormProperties.IVisitor() {
@Override
public Void visit(final SuFormProperties props) {
SubFieldFormAppKey<DataxReader> subFieldKey = new SubFieldFormAppKey<>(pluginContext, db, appname, props, DataxReader.class);
KeyedPluginStore<DataxReader> subFieldStore = KeyedPluginStore.getPluginStore(subFieldKey);
// 子表单中的内容更新了之后,要同步父表单中的状态
subFieldStore.addPluginsUpdateListener(new PluginStore.PluginsUpdateListener<DataxReader>(subFieldKey.getSerializeFileName(), reader) {
@Override
public void accept(PluginStore<DataxReader> pluginStore) {
setReaderSubFormProp(props, pluginStore.getPlugin());
}
});
DataxReader subFieldReader = subFieldStore.getPlugin();
if (subFieldReader == null) {
return null;
}
setReaderSubFormProp(props, subFieldReader);
return null;
}
private void setReaderSubFormProp(SuFormProperties props, DataxReader subFieldReader) {
setReaderSubFormProp(props, reader, subFieldReader);
}
private void setReaderSubFormProp(SuFormProperties props, DataxReader reader, DataxReader subFieldReader) {
if (reader == null) {
return;
}
try {
props.subFormField.set(reader, props.subFormField.get(subFieldReader));
} catch (IllegalAccessException e) {
throw new RuntimeException("get subField:" + props.getSubFormFieldName(), e);
}
}
});
});
}
}
});
}
use of com.qlangtech.tis.extension.impl.SuFormProperties in project plugins by qlangtech.
the class TestDataxMySQLReader method testUpdateDataxReader.
/**
* 这个单元测试很重要
*
* @throws Exception
*/
public void testUpdateDataxReader() throws Exception {
KeyedPluginStore<DataxReader> readerStore = DataxReader.getPluginStore(null, dataXName);
final SuFormProperties props = EasyMock.createMock("subformProp", SuFormProperties.class);
EasyMock.expect(props.getSubFormFieldName()).andReturn("selectedTabs").anyTimes();
EasyMock.replay(props);
DataxReader.SubFieldFormAppKey<DataxReader> subFieldKey = new DataxReader.SubFieldFormAppKey<>(null, false, dataXName, props, DataxReader.class);
KeyedPluginStore<DataxReader> subFieldStore = KeyedPluginStore.getPluginStore(subFieldKey);
File targetFile = readerStore.getTargetFile();
FileUtils.deleteQuietly(targetFile);
FileUtils.deleteQuietly(subFieldStore.getTargetFile());
DataxMySQLReader dataxReader = (DataxMySQLReader) readerStore.getPlugin();
assertNull("targetFile:" + targetFile.getAbsolutePath(), dataxReader);
List<Descriptor.ParseDescribable<DataxReader>> dlist = Lists.newArrayList();
dataxReader = new DataxMySQLReader();
dataxReader.dbName = dbName;
dataxReader.fetchSize = 1999;
dataxReader.template = DataxMySQLReader.getDftTemplate();
dataxReader.dataXName = dataXName;
dlist.add(new Descriptor.ParseDescribable<>(dataxReader));
readerStore.setPlugins(null, Optional.empty(), dlist);
dataxReader = (DataxMySQLReader) readerStore.getPlugin();
assertNotNull(dataxReader);
assertTrue("dataxReader.getSelectedTabs() must be empty", CollectionUtils.isEmpty(dataxReader.getSelectedTabs()));
dlist = Lists.newArrayList();
DataxMySQLReader subformReader = new DataxMySQLReader();
SelectedTab tabOrder = new SelectedTab();
tabOrder.name = TestSelectedTabs.tabNameOrderDetail;
tabOrder.setCols(TestSelectedTabs.tabColsMetaOrderDetail.stream().map((c) -> c.getName()).collect(Collectors.toList()));
subformReader.setSelectedTabs(Collections.singletonList(tabOrder));
dlist.add(new Descriptor.ParseDescribable(subformReader));
subFieldStore.setPlugins(null, Optional.empty(), dlist);
assertFalse("must not dirty", dataxReader.isDirty());
// 这里有点像变魔术,这是通过 DataXReader中addPluginsUpdateListener添加的回调函数实现的
assertTrue(dataxReader.getSelectedTabs().size() > 0);
dataxReader.getSelectedTabs().forEach((t) -> {
t.getCols().forEach((c) -> {
assertNotNull("col:" + c.getName(), c.isPk());
assertNotNull("col:" + c.getName(), c.getType());
});
});
// 子form中再加一个tab
SelectedTab tabTotalpayinfo = new SelectedTab();
tabTotalpayinfo.name = TestSelectedTabs.tabNameTotalpayinfo;
tabTotalpayinfo.setCols(TestSelectedTabs.tabColsMetaTotalpayinfo.stream().map((c) -> c.getName()).collect(Collectors.toList()));
subformReader.setSelectedTabs(Lists.newArrayList(tabOrder, tabTotalpayinfo));
dlist = Lists.newArrayList();
dlist.add(new Descriptor.ParseDescribable(subformReader));
subFieldStore.setPlugins(null, Optional.empty(), dlist);
assertEquals(2, dataxReader.getSelectedTabs().size());
dataxReader.getSelectedTabs().forEach((t) -> {
t.getCols().forEach((c) -> {
assertNotNull("tab:" + t.getName() + ",col:" + c.getName() + " relevant PK can not be null", c.isPk());
assertNotNull("tab:" + t.getName() + ",col:" + c.getName() + " relevant Type can not be null", c.getType());
});
});
DataxMySQLReader oldReader = dataxReader;
dataxReader = new DataxMySQLReader();
dataxReader.dbName = dbName;
dataxReader.fetchSize = 2999;
dataxReader.template = DataxMySQLReader.getDftTemplate();
dataxReader.dataXName = dataXName;
readerStore.setPlugins(null, Optional.empty(), Collections.singletonList(new Descriptor.ParseDescribable(dataxReader)));
assertTrue("oldReader must be dirty", oldReader.isDirty());
assertTrue(dataxReader.getSelectedTabs().size() > 0);
// 清空测试
dlist = Lists.newArrayList();
subformReader = new DataxMySQLReader();
dlist.add(new Descriptor.ParseDescribable(subformReader));
subFieldStore.setPlugins(null, Optional.empty(), dlist);
assertTrue("getSelectedTabs shall be empty", dataxReader.getSelectedTabs().size() < 1);
EasyMock.verify(props);
}
use of com.qlangtech.tis.extension.impl.SuFormProperties in project tis by qlangtech.
the class OfflineDatasourceAction method doGetDsTabsVals.
/**
* 批量设置用,初始化数据库表
*
* @param context
* @throws IOException
*/
public void doGetDsTabsVals(Context context) throws IOException {
com.alibaba.fastjson.JSONObject body = this.parseJsonPost();
com.alibaba.fastjson.JSONArray tabs = body.getJSONArray("tabs");
if (tabs == null) {
throw new IllegalArgumentException("initialize Tabs can not be null");
}
List<String> selectedTabs = tabs.stream().map((tab) -> (String) tab).collect(Collectors.toList());
String pluginName = body.getString("name");
boolean require = body.getBooleanValue("require");
String extraParam = body.getString("extraParam");
UploadPluginMeta pluginMeta = null;
List<UploadPluginMeta> pluginMetas = UploadPluginMeta.parse(new String[] { pluginName + ":" + (require ? "require" : StringUtils.EMPTY) + "," + extraParam });
for (UploadPluginMeta m : pluginMetas) {
pluginMeta = m;
}
Objects.requireNonNull(pluginMeta, "pluginMeta can not be null");
HeteroList<DataxReader> heteroList = pluginMeta.getHeteroList(this);
List<DataxReader> readers = heteroList.getItems();
Map<String, List<ColumnMetaData>> mapCols = null;
List<ISelectedTab> allNewTabs = Lists.newArrayList();
PluginFormProperties pluginFormPropertyTypes = null;
Map<String, Object> bizResult = Maps.newHashMap();
for (DataxReader reader : readers) {
mapCols = selectedTabs.stream().collect(Collectors.toMap((tab) -> tab, (tab) -> {
return reader.getTableMetadata(tab);
}));
if (MapUtils.isEmpty(mapCols)) {
throw new IllegalStateException("mapCols can not be empty");
}
pluginFormPropertyTypes = reader.getDescriptor().getPluginFormPropertyTypes(pluginMeta.getSubFormFilter());
for (Map.Entry<String, List<ColumnMetaData>> tab2cols : mapCols.entrySet()) {
SuFormProperties.setSuFormGetterContext(reader, pluginMeta, tab2cols.getKey());
allNewTabs.add(createNewSelectedTab(pluginFormPropertyTypes, tab2cols));
}
DescriptorsJSON desc2Json = new DescriptorsJSON(reader.getDescriptor());
bizResult.put("subformDescriptor", desc2Json.getDescriptorsJSON(pluginMeta.getSubFormFilter()));
break;
}
Objects.requireNonNull(pluginFormPropertyTypes, "pluginFormPropertyTypes can not be null");
if (allNewTabs.size() < 1) {
throw new IllegalStateException("allNewTabs size can not small than 1");
}
bizResult.put("tabVals", pluginFormPropertyTypes.accept(new PluginFormProperties.IVisitor() {
@Override
public com.alibaba.fastjson.JSONObject visit(SuFormProperties props) {
return props.createSubFormVals(allNewTabs.stream().map((t) -> (IdentityName) t).collect(Collectors.toList()));
}
}));
this.setBizResult(context, bizResult);
}
use of com.qlangtech.tis.extension.impl.SuFormProperties in project tis by qlangtech.
the class OfflineDatasourceAction method createNewSelectedTab.
/**
* 通过表名和列创建新tab实例,如果SelectedTab对象中有其他字段但是没有设置默认值,创建过程中就会出错
*
* @param pluginFormPropertyTypes
* @param tab2cols
* @return
*/
private ISelectedTab createNewSelectedTab(PluginFormProperties pluginFormPropertyTypes, Map.Entry<String, List<ColumnMetaData>> tab2cols) {
return pluginFormPropertyTypes.accept(new PluginFormProperties.IVisitor() {
@Override
public ISelectedTab visit(SuFormProperties props) {
// try {
// } catch (Exception e) {
// throw new RuntimeException("create subform,table:" + tab2cols.getKey()
// + ",cols size:" + tab2cols.getValue().size(), e);
// }
ISelectedTab subForm = props.newSubDetailed();
PropertyType pp = null;
ppDftValGetter: for (Map.Entry<String, PropertyType> pentry : props.getKVTuples()) {
pp = pentry.getValue();
if (pp.isIdentity()) {
pp.setVal(subForm, tab2cols.getKey());
continue;
}
if (pp.formField.type() == FormFieldType.MULTI_SELECTABLE) {
pp.setVal(subForm, tab2cols.getValue().stream().map((c) -> c.getName()).collect(Collectors.toList()));
continue ppDftValGetter;
}
if (pp.isInputRequired()) {
if (StringUtils.isNotEmpty(pp.dftVal())) {
if (pp.isDescribable()) {
List<? extends Descriptor> descriptors = pp.getApplicableDescriptors();
for (Descriptor desc : descriptors) {
if (StringUtils.equals(pp.dftVal(), desc.getDisplayName())) {
// desc.getPluginFormPropertyTypes();
pp.setVal(subForm, desc.newInstance(null, Collections.emptyMap(), Optional.empty()).instance);
continue ppDftValGetter;
}
}
} else {
pp.setVal(subForm, pp.dftVal());
continue ppDftValGetter;
}
} else {
// pp.getEnumConstants()
FormFieldType fieldType = pp.formField.type();
if (fieldType == FormFieldType.SELECTABLE || fieldType == FormFieldType.ENUM) {
Object enumPp = pp.getExtraProps().get(Descriptor.KEY_ENUM_PROP);
com.alibaba.fastjson.JSONArray enums = null;
if (enumPp instanceof com.alibaba.fastjson.JSONArray) {
enums = (com.alibaba.fastjson.JSONArray) enumPp;
} else if (enumPp instanceof JsonUtil.UnCacheString) {
enums = ((JsonUtil.UnCacheString<com.alibaba.fastjson.JSONArray>) enumPp).getValue();
} else {
throw new IllegalStateException("unsupport type:" + pp.getClass().getName());
}
for (int i = 0; i < enums.size(); i++) {
com.alibaba.fastjson.JSONObject opt = enums.getJSONObject(i);
pp.setVal(subForm, opt.get(Option.KEY_VALUE));
continue ppDftValGetter;
}
}
}
throw new IllegalStateException("have not prepare for table:" + tab2cols.getKey() + " creating,prop name:'" + pentry.getKey() + "',subform class:" + subForm.getClass().getName());
}
}
return subForm;
}
});
}
use of com.qlangtech.tis.extension.impl.SuFormProperties in project tis by qlangtech.
the class DescriptorsJSON method getDescriptorsJSON.
public JSONObject getDescriptorsJSON(Optional<IPropertyType.SubFormFilter> subFormFilter) {
JSONArray attrs;
String key;
PropertyType val;
JSONObject extraProps = null;
// FormField fieldAnnot;
JSONObject attrVal;
JSONObject descriptors = new JSONObject();
Map<String, Object> extractProps;
// IPropertyType.SubFormFilter subFilter = null;
PluginFormProperties pluginFormPropertyTypes;
for (Descriptor<T> d : this.descriptors) {
pluginFormPropertyTypes = d.getPluginFormPropertyTypes(subFormFilter);
JSONObject des = new JSONObject();
pluginFormPropertyTypes.accept(new SubFormFieldVisitor(subFormFilter) {
@Override
public Void visit(SuFormProperties props) {
JSONObject subForm = new JSONObject();
subForm.put("fieldName", props.getSubFormFieldName());
if (subFormFilter.isPresent()) {
IPropertyType.SubFormFilter filter = subFormFilter.get();
if (!filter.subformDetailView) {
des.put("subForm", true);
subForm.put("idList", props.getSubFormIdListGetter().build(filter));
}
}
des.put("subFormMeta", subForm);
return null;
}
});
des.put(KEY_EXTEND_POINT, d.getT().getName());
this.setDescInfo(d, des);
des.put("veriflable", d.overWriteValidateMethod);
if (IdentityName.class.isAssignableFrom(d.clazz)) {
des.put("pkField", d.getIdentityField().displayName);
}
extractProps = d.getExtractProps();
if (!extractProps.isEmpty()) {
des.put("extractProps", extractProps);
}
attrs = new JSONArray();
ArrayList<Map.Entry<String, PropertyType>> entries = Lists.newArrayList(pluginFormPropertyTypes.getKVTuples());
entries.sort(((o1, o2) -> o1.getValue().ordinal() - o2.getValue().ordinal()));
for (Map.Entry<String, PropertyType> pp : entries) {
key = pp.getKey();
val = pp.getValue();
// fieldAnnot = val.getFormField();
attrVal = new JSONObject();
attrVal.put("key", key);
// 是否是主键
attrVal.put("pk", val.isIdentity());
attrVal.put("describable", val.isDescribable());
attrVal.put("type", val.typeIdentity());
attrVal.put("required", val.isInputRequired());
attrVal.put("ord", val.ordinal());
extraProps = val.getExtraProps();
if (extraProps != null) {
// 额外属性
attrVal.put("eprops", extraProps);
}
if (val.typeIdentity() == FormFieldType.SELECTABLE.getIdentity()) {
attrVal.put("options", getSelectOptions(d, val, key));
}
if (val.isDescribable()) {
DescriptorsJSON des2Json = new DescriptorsJSON(val.getApplicableDescriptors());
attrVal.put("descriptors", des2Json.getDescriptorsJSON());
}
// attrs.put(attrVal);
attrs.add(attrVal);
}
// 对象拥有的属性
des.put("attrs", attrs);
// processor.process(attrs.keySet(), d);
descriptors.put(d.getId(), des);
}
return descriptors;
}
Aggregations