'多表支持修改,完成1、列表配置,2、查询配置,3、导出配置'
This commit is contained in:
@@ -51,79 +51,110 @@ public class Kv<K,V> extends LinkedHashMap<K,V> {
|
||||
}
|
||||
|
||||
public <T> T toBean(Class<T> type) {
|
||||
return (T) toBean(this, type);
|
||||
return toBean(this, type);
|
||||
}
|
||||
|
||||
// 首字母大写
|
||||
private static Function<String, String> upFirst = (s) -> {
|
||||
return s.substring(0, 1).toUpperCase() + s.substring(1);
|
||||
};
|
||||
private static Map<Class, Class[]> clazzMap = new HashMap<>();
|
||||
/*private static Map<Class, Class[]> clazzMap = new HashMap<>();
|
||||
static {
|
||||
clazzMap.put(ArrayList.class, new Class[]{List.class, ArrayList.class, String.class});
|
||||
clazzMap.put(HashMap.class, new Class[]{Map.class, HashMap.class, String.class});
|
||||
clazzMap.put(Long.class, new Class[]{Long.class, Integer.class, long.class, int.class, short.class, String.class});
|
||||
clazzMap.put(String.class, new Class[]{String.class, Integer.class, Date.class});
|
||||
clazzMap.put(Double.class, new Class[]{Double.class, Long.class, Integer.class, long.class, int.class, short.class, String.class});
|
||||
}
|
||||
}*/
|
||||
private static Predicate<Class> isNumber = (t) -> {
|
||||
return t == Integer.class || t == int.class
|
||||
|| t == Long.class || t == long.class
|
||||
|| t == Double.class || t == double.class
|
||||
;
|
||||
};
|
||||
public static <T> T toBean(Map m, Class<T> type) {
|
||||
/*public static <T> T toBean2(Map m, Class<T> type) {
|
||||
try {
|
||||
Object obj = type.newInstance();
|
||||
m.forEach((k, v) -> {
|
||||
String methodName = "set" + upFirst.apply(k+"");
|
||||
Class[] clazzs = clazzMap.get(v == null ? null : v.getClass());
|
||||
if (clazzs == null) {
|
||||
//doc.set(k, v);
|
||||
} else {
|
||||
for (Class clazz : clazzs) {
|
||||
Method method = null;
|
||||
try {
|
||||
method = type.getDeclaredMethod(methodName, clazz);
|
||||
} catch (NoSuchMethodException e) {
|
||||
}
|
||||
if (method != null) {
|
||||
try {
|
||||
Object _v = toAs(v, clazz);
|
||||
//如发现 映射异常,打开下面的注释查看 进行映射的值,并对上面的值转换过程升级
|
||||
//System.out.printf("%s:%s %s%n", k,v, v.getClass());
|
||||
method.invoke(obj, _v);
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
} catch (InvocationTargetException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
for (String k : (Set<String>)m.keySet()) {
|
||||
Object v = m.get(k);
|
||||
if (v == null) {
|
||||
continue;
|
||||
}
|
||||
});
|
||||
|
||||
//得到目标方法
|
||||
String methodName = "set" + upFirst.apply(k + "");
|
||||
Method method = null;
|
||||
Class tClazz = null;
|
||||
try {
|
||||
method = type.getDeclaredMethod(methodName, tClazz = v.getClass());
|
||||
if (method == null) {
|
||||
Class[] clazzs = clazzMap.get(v == null ? null : v.getClass());
|
||||
if (clazzs == null) {
|
||||
//doc.set(k, v);
|
||||
} else {
|
||||
|
||||
for (Class clazz : clazzs) {
|
||||
try {
|
||||
|
||||
type.getMethods();
|
||||
|
||||
method = type.getDeclaredMethod(methodName, tClazz = clazz);
|
||||
} catch (NoSuchMethodException e) {
|
||||
}
|
||||
}
|
||||
|
||||
if (method != null) {
|
||||
try {
|
||||
Object _v = toAs(v, tClazz);
|
||||
//如发现 映射异常,打开下面的注释查看 进行映射的值,并对上面的值转换过程升级
|
||||
//System.out.printf("%s:%s %s%n", k,v, v.getClass());
|
||||
method.invoke(obj, _v);
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
} catch (InvocationTargetException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
} catch (NoSuchMethodException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
|
||||
if (method != null) {
|
||||
|
||||
}
|
||||
//已知:key,value, 一个实例类型对象
|
||||
//找到目标方法
|
||||
//转换数据类型
|
||||
|
||||
}
|
||||
|
||||
return (T) obj;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}*/
|
||||
|
||||
public static <T> T toAs(Object v, Class<T> clazz) {
|
||||
if (v == null) {
|
||||
return null;
|
||||
}
|
||||
if (v.getClass() == clazz) {
|
||||
} else if (v.getClass() == clazz) {
|
||||
return (T) v;
|
||||
} else if (clazz == String.class) {
|
||||
return (T) String.valueOf(v);
|
||||
}
|
||||
|
||||
Object _v = null;
|
||||
try {
|
||||
if (v == null || "".equals(v)) {
|
||||
|
||||
} else if (v.getClass() == Long.class && clazz != Long.class) {//多种数值类型的处理: Long => x
|
||||
} else if (v.getClass() == Long.class) {//多种数值类型的处理: Long => x
|
||||
switch (clazz.getSimpleName()) {
|
||||
case "int":
|
||||
case "Integer": _v = (int)(long) v; break;
|
||||
@@ -176,4 +207,103 @@ public class Kv<K,V> extends LinkedHashMap<K,V> {
|
||||
}
|
||||
return (T) _v;
|
||||
}
|
||||
|
||||
public static <T> T toBean(Map map, Class<T> clazz) {
|
||||
//按照方法名 + 类型寻找,
|
||||
//按照方法名 寻找
|
||||
//+
|
||||
Object obj = null;
|
||||
try {
|
||||
obj = clazz.newInstance();
|
||||
} catch (InstantiationException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
for (String k : (Set<String>)map.keySet()) {
|
||||
Object v = map.get(k);
|
||||
if (v == null) continue;
|
||||
//寻找method
|
||||
try {
|
||||
String methodName = "set" + upFirst.apply(k);
|
||||
Class tClazz = null;
|
||||
Method method = null;
|
||||
try {
|
||||
method = clazz.getMethod(methodName, tClazz = v.getClass());
|
||||
} catch (NoSuchMethodException e) {
|
||||
//e.printStackTrace();
|
||||
}
|
||||
if (method == null) {
|
||||
for (Method _method : clazz.getMethods()) {
|
||||
if (methodName.equals(_method.getName()) && _method.getParameterCount() == 1) {
|
||||
method = _method;
|
||||
tClazz = _method.getParameterTypes()[0];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (method == null) {
|
||||
for (Method _method : clazz.getMethods()) {
|
||||
if (methodName.equalsIgnoreCase(_method.getName()) && _method.getParameterCount() == 1) {
|
||||
method = _method;
|
||||
tClazz = _method.getParameterTypes()[0];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (method != null) {
|
||||
method.invoke(obj, toAs(v, tClazz));
|
||||
}
|
||||
|
||||
//没有方法,找属性注解
|
||||
if (method == null) {
|
||||
Field field = null;
|
||||
Field[] fields = clazz.getDeclaredFields();
|
||||
for (Field _field : fields) {
|
||||
To to = _field.getAnnotation(To.class);
|
||||
if (to != null && k.equals(to.value())) {
|
||||
field = _field;
|
||||
tClazz = _field.getType();
|
||||
break;
|
||||
}
|
||||
}
|
||||
/*if (field == null) {
|
||||
fields = clazz.getDeclaredFields();
|
||||
continue;
|
||||
}
|
||||
while (true){
|
||||
|
||||
|
||||
break;
|
||||
}*/
|
||||
|
||||
if (field != null) {
|
||||
field.setAccessible(true);
|
||||
field.set(obj, toAs(v, tClazz));
|
||||
}
|
||||
}
|
||||
} catch (IllegalAccessException | InvocationTargetException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
return (T) obj;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Map map = new HashMap<>();
|
||||
|
||||
map.put("name", "xxxx");
|
||||
map.put("age", 12);
|
||||
map.put("abx", 123);
|
||||
|
||||
User user = toBean(map, User.class);
|
||||
|
||||
System.out.println(user);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
12
src/main/java/net/tccn/base/To.java
Normal file
12
src/main/java/net/tccn/base/To.java
Normal file
@@ -0,0 +1,12 @@
|
||||
package net.tccn.base;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
|
||||
/**
|
||||
* Created by liangxianyou at 2019/3/26 20:35.
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface To {
|
||||
String value();
|
||||
}
|
||||
29
src/main/java/net/tccn/base/User.java
Normal file
29
src/main/java/net/tccn/base/User.java
Normal file
@@ -0,0 +1,29 @@
|
||||
package net.tccn.base;
|
||||
|
||||
/**
|
||||
* Created by liangxianyou at 2019/3/26 19:36.
|
||||
*/
|
||||
public class User {
|
||||
|
||||
private String name;
|
||||
private int age;
|
||||
@To("")
|
||||
private float attr;
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public void setAge(int age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "User{" +
|
||||
"name='" + name + '\'' +
|
||||
", age=" + age +
|
||||
", attr='" + attr + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
23
src/main/java/net/tccn/base/UtilityExt.java
Normal file
23
src/main/java/net/tccn/base/UtilityExt.java
Normal file
@@ -0,0 +1,23 @@
|
||||
package net.tccn.base;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* Created by liangxianyou at 2019/3/19 18:01.
|
||||
*/
|
||||
public class UtilityExt {
|
||||
|
||||
public static <T> Set<T> streamConcat(Stream<T> ... streams) {
|
||||
Stream<T> stream = Stream.empty();
|
||||
for (int i = 0; i < streams.length-1; i++) {
|
||||
stream = Stream.concat(
|
||||
stream, streams[i]
|
||||
);
|
||||
}
|
||||
return stream.collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -45,7 +45,7 @@ public class ParseMysql implements Parser {
|
||||
|
||||
@Deprecated
|
||||
Function<FBean, Kv<String, MetaTable>> tablesFun = fbean -> {
|
||||
MetaService metaService = MetaKit.metaService(fbean.getName());
|
||||
MetaService metaService = MetaKit.getMetaService(fbean.getName());
|
||||
List<Filter> filters = fbean.getFilters();
|
||||
List<String> shows = metaService.getShows();
|
||||
|
||||
@@ -91,10 +91,10 @@ public class ParseMysql implements Parser {
|
||||
|
||||
Predicate<Kv<String, MetaTable>> sameDbFun = (kv) -> {
|
||||
String dbPlatId = null;
|
||||
for (MetaTable table : kv.values()) {
|
||||
for (MetaTable metaTable : kv.values()) {
|
||||
if (dbPlatId == null) {
|
||||
dbPlatId = table.getDbPlatId();
|
||||
} else if (!dbPlatId.equals(table.getDbPlatId())) {
|
||||
dbPlatId = metaTable.getDbPlatId();
|
||||
} else if (!dbPlatId.equals(metaTable.getDbPlatId())) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -103,20 +103,18 @@ public class ParseMysql implements Parser {
|
||||
|
||||
@Override
|
||||
public String[] parse(FBean fBean) {
|
||||
MetaService metaService = MetaKit.metaService(fBean.getName());
|
||||
MetaService metaService = MetaKit.getMetaService(fBean.getName());
|
||||
|
||||
Kv<String, MetaTable> tables = tablesFun.apply(fBean);//所有的关联表信息
|
||||
Kv<String, MetaTable> tables = MetaKit.getMetaTables(metaService);//所有的关联表信息
|
||||
MetaTable metaTable = tables.get(metaService.getTable());//基础元数据
|
||||
List<String> shows = metaService.getShows();//查询的属性
|
||||
|
||||
List<String> _filters = fBean.getFilters().stream().map(Filter::getCol).collect(Collectors.toList());
|
||||
List<MetaLink> links = MetaKit.getMetaLinks(metaService.getTable(), shows, _filters);
|
||||
|
||||
//查询条件
|
||||
List<Filter> filters = fBean.getFilters();
|
||||
Limit limit = fBean.getLimit();
|
||||
List<Order> orders = fBean.getOrders();
|
||||
|
||||
//Map<String, List<Filter>> filterMap = filters.stream().collect(Collectors.groupingBy(x -> x.getCol().split("[.]")[0]));
|
||||
//Map<String, List<String>> showMap = shows.stream().collect(Collectors.groupingBy(x -> x.split("[.]")[0]));
|
||||
|
||||
|
||||
@@ -1,16 +1,12 @@
|
||||
package net.tccn.meta;
|
||||
|
||||
import net.tccn.base.Kv;
|
||||
import net.tccn.dbq.jdbc.api.DbAccount;
|
||||
import net.tccn.base.UtilityExt;
|
||||
import net.tccn.dbq.Field;
|
||||
import net.tccn.dbq.fbean.FilterType;
|
||||
import net.tccn.dbq.jdbc.api.DbAccount;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.*;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
@@ -111,7 +107,7 @@ public class MetaKit {
|
||||
* @param name
|
||||
* @return
|
||||
*/
|
||||
public static MetaService metaService(String name) {
|
||||
public static MetaService getMetaService(String name) {
|
||||
Optional<MetaService> service = metaServices.stream().filter(x -> x.getName().equals(name)).findAny();
|
||||
//处理业务逻辑
|
||||
service.ifPresent(x -> {
|
||||
@@ -168,7 +164,7 @@ public class MetaKit {
|
||||
Field field = getField.apply(x);
|
||||
if (field != null) {
|
||||
Kv col = Kv.of();
|
||||
col.set("col", x.split("[.]]")[0] + field.showField());//a.createtime=dt
|
||||
col.set("col", x.split("$]")[0] + field.showField());//a.createtime=dt
|
||||
col.set("label", field.getLabel());
|
||||
col.set("order", 1);//dev 是否支持排序
|
||||
_cols.add(col);
|
||||
@@ -233,7 +229,7 @@ public class MetaKit {
|
||||
//showUpdate
|
||||
public static BiFunction<String, List<String>, MetaService> showUpdate = (serviceKey, shows) -> {
|
||||
|
||||
MetaService metaService = metaService(serviceKey);
|
||||
MetaService metaService = getMetaService(serviceKey);
|
||||
|
||||
MetaService _metaService = MetaService.dao.findByKey(metaService.getKey());
|
||||
_metaService.setShows(shows);
|
||||
@@ -277,4 +273,51 @@ public class MetaKit {
|
||||
.collect(Collectors.toList());
|
||||
return links;
|
||||
}
|
||||
|
||||
public static Kv buildeDetail(MetaService metaService) {
|
||||
//tables
|
||||
Kv<String, MetaTable> tables = getMetaTables(metaService);
|
||||
return Kv.of("tables", tables)
|
||||
.set("links", Kv.of());
|
||||
}
|
||||
|
||||
public static Kv<String, MetaTable> getMetaTables(MetaService metaService) {
|
||||
Kv<String, MetaTable> tables = Kv.of();
|
||||
|
||||
String table = metaService.getTable();//
|
||||
tables.set(table, getMetaTableByAlias(table));
|
||||
|
||||
//收集所有的col
|
||||
Set<String> allAlias = UtilityExt.streamConcat(
|
||||
metaService.getFilters().stream().map(f -> {
|
||||
String col = (String) f.get("name");
|
||||
String alias = col.split("[.]")[0];
|
||||
return alias;
|
||||
}),
|
||||
metaService.getShows().stream().map(x -> x.split("[.]")[0]),
|
||||
metaService.getEdits().stream().map(x -> x.split("[.]")[0])
|
||||
);
|
||||
|
||||
allAlias.forEach(x -> tables.set(x, getMetaTableByAlias(x)));
|
||||
return tables;
|
||||
}
|
||||
|
||||
public String nextAlias(String x) {
|
||||
return next(x, "");
|
||||
}
|
||||
|
||||
//使用['a',...,'z'] 创建Table 别名
|
||||
private String next(String x, String end) {
|
||||
if (x == null || "".equals(x)) {
|
||||
return "a" + end;
|
||||
}
|
||||
char c = x.charAt(x.length() - 1);
|
||||
String sub = x.substring(0, x.length() - 1);
|
||||
if (c < 'z') {
|
||||
return sub + (++c) + end;
|
||||
} else if (c == 'z') {
|
||||
return next(sub, "a" + end);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package net.tccn.meta;
|
||||
|
||||
import net.tccn.base.Kv;
|
||||
import net.tccn.base.arango.Doc;
|
||||
|
||||
import javax.persistence.Table;
|
||||
@@ -23,6 +24,7 @@ public class MetaService extends Doc<MetaService> {
|
||||
private List<String> shows = new ArrayList<>();
|
||||
private List<String> edits = new ArrayList<>();
|
||||
private List<Map> filters = new ArrayList<>();
|
||||
private List<String> exports = new ArrayList<>();
|
||||
|
||||
//待组装数据
|
||||
//private MetaTable metaTable;
|
||||
@@ -86,6 +88,14 @@ public class MetaService extends Doc<MetaService> {
|
||||
this.filters = filters;
|
||||
}
|
||||
|
||||
public List<String> getExports() {
|
||||
return exports;
|
||||
}
|
||||
|
||||
public void setExports(List<String> exports) {
|
||||
this.exports = exports;
|
||||
}
|
||||
|
||||
/*public MetaTable getMetaTable() {
|
||||
return metaTable;
|
||||
}
|
||||
|
||||
@@ -51,7 +51,7 @@ public class MetadataService extends BaseService { //arango
|
||||
public JBean listCfg(String key, @RestParam(name = "platToken") String token) {
|
||||
JBean jBean = JBean.by(0, "");
|
||||
|
||||
MetaService metaService = MetaKit.metaService(key);
|
||||
MetaService metaService = MetaKit.getMetaService(key);
|
||||
fixme: jBean.set(0, "", MetaKit.builderCfg.apply(metaService));
|
||||
return jBean;
|
||||
}
|
||||
@@ -67,10 +67,7 @@ public class MetadataService extends BaseService { //arango
|
||||
|
||||
//----------- 元数据管理 ---------------
|
||||
@RestMapping(name = "tablelist", comment = "table列表")
|
||||
public JBean tableList(@RestParam(name = "platToken") String token,
|
||||
String catalog,
|
||||
String dbPlatId,
|
||||
String name) {
|
||||
public JBean tableList(@RestParam(name = "platToken") String token, String catalog, String dbPlatId, String name) {
|
||||
JBean jBean = JBean.by(0, "");
|
||||
SysPlat sysPlat = qtaskService.getSysPlat(token);
|
||||
if (sysPlat == null) {
|
||||
@@ -114,6 +111,20 @@ public class MetadataService extends BaseService { //arango
|
||||
return JBean.by(0, "", findMetaTable(name, token));
|
||||
}
|
||||
|
||||
@RestMapping(name = "service_info", comment = "service基本信息")
|
||||
public JBean serviceInfo(@RestParam(name = "platToken") String token, String name) {
|
||||
MetaService metaService = MetaKit.getMetaService(name);
|
||||
return JBean.by(0, "", metaService);
|
||||
}
|
||||
|
||||
@RestMapping(name = "service_detail", comment = "service详情")
|
||||
public JBean serviceDetail(@RestParam(name = "platToken") String token, String name) {
|
||||
MetaService metaService = MetaKit.getMetaService(name);
|
||||
Kv detail = MetaKit.buildeDetail(metaService);
|
||||
|
||||
return JBean.by(0, "", detail);
|
||||
}
|
||||
|
||||
//修改item的排序
|
||||
@RestMapping(name = "itemsort", comment = "字段排序")
|
||||
public String[] itemSortSave(String serviceKey , String[] items, @RestParam(name = "platToken") String token) {
|
||||
@@ -146,9 +157,10 @@ public class MetadataService extends BaseService { //arango
|
||||
public List<String> exportSort(String serviceKey , List<String> items, @RestParam(name = "platToken") String token) {
|
||||
if (items == null || items.size() == 0) return null;
|
||||
|
||||
MetaTable metaTable = findMetaTable(serviceKey, token);
|
||||
//fixme: metaTable.setExports(items);
|
||||
metaTable.update();
|
||||
MetaService metaService = MetaKit.getMetaService(serviceKey);
|
||||
metaService.setExports(items);
|
||||
|
||||
metaService.update();
|
||||
return items;
|
||||
}
|
||||
|
||||
@@ -180,11 +192,10 @@ public class MetadataService extends BaseService { //arango
|
||||
@RestParam(name = "filters") String filters,
|
||||
@RestParam(name = "platToken") String token) {
|
||||
List _filters = gson.fromJson(filters, List.class);
|
||||
MetaTable metaTable = findMetaTable(serviceKey, token);
|
||||
|
||||
//fixme: metaTable.setFilters(_filters);
|
||||
metaTable.update();
|
||||
MetaService metaService = MetaKit.getMetaService(serviceKey);
|
||||
metaService.setFilters(_filters);
|
||||
|
||||
metaService.update();
|
||||
return JBean.by(0, "");
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user