.
This commit is contained in:
88
src/main/java/net/tccn/service/BaseService.java
Normal file
88
src/main/java/net/tccn/service/BaseService.java
Normal file
@@ -0,0 +1,88 @@
|
||||
package net.tccn.service;
|
||||
|
||||
import com.arangodb.Predicate;
|
||||
import com.google.gson.Gson;
|
||||
import org.redkale.net.http.RestMapping;
|
||||
import org.redkale.service.Service;
|
||||
import org.redkale.source.CacheSource;
|
||||
import org.redkale.util.AnyValue;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* @author: liangxianyou at 2018/10/22 11:49.
|
||||
*/
|
||||
public class BaseService implements Service {
|
||||
|
||||
public static Gson gson = new Gson();
|
||||
public Logger logger = Logger.getLogger(this.getClass().getSimpleName());
|
||||
|
||||
public static Predicate isEmpty = (x) -> {
|
||||
if (x == null)
|
||||
return true;
|
||||
if (x instanceof List)
|
||||
return ((List) x).isEmpty();
|
||||
if (x instanceof String)
|
||||
return ((String) x).isEmpty();
|
||||
if (x instanceof Map)
|
||||
return ((Map) x).isEmpty();
|
||||
if (x instanceof Collection)
|
||||
return ((Collection) x).isEmpty();
|
||||
return false;
|
||||
};
|
||||
|
||||
public static boolean isWinos = System.getProperty("os.name").contains("Window");
|
||||
|
||||
@Resource(name = "cacheSource")
|
||||
protected CacheSource cacheSource;
|
||||
|
||||
@Resource(name = "APP_HOME")
|
||||
protected File APP_HOME;
|
||||
|
||||
public static Properties prop = new Properties();
|
||||
|
||||
@Override
|
||||
public void init(AnyValue config) {
|
||||
try {
|
||||
File file = new File(APP_HOME.toPath() + "/conf/config.txt");
|
||||
if (file.exists()) {
|
||||
prop.load(new FileInputStream(file));
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@RestMapping(ignore = true)
|
||||
public <T> T getT(String key, Class<T> clazz, Supplier<T> supplier) {
|
||||
Object obj = cacheSource.getAndRefresh(key, 1000 * 60 * 3, clazz);
|
||||
if (obj != null) {
|
||||
return (T) obj;
|
||||
}
|
||||
|
||||
T t = supplier.get();
|
||||
if (t != null) {
|
||||
cacheSource.set(1000 * 60 * 3, key, clazz, t);
|
||||
}
|
||||
return t;
|
||||
}
|
||||
|
||||
@RestMapping(ignore = true)
|
||||
public String getProperty(String k, String defaultValue){
|
||||
return prop.getProperty(k, defaultValue).replace("${APP_HOME}", APP_HOME.getPath());
|
||||
}
|
||||
@RestMapping(ignore = true)
|
||||
public String getProperty(String k){
|
||||
return prop.getProperty(k);
|
||||
}
|
||||
|
||||
}
|
||||
79
src/main/java/net/tccn/service/BaseServlet.java
Normal file
79
src/main/java/net/tccn/service/BaseServlet.java
Normal file
@@ -0,0 +1,79 @@
|
||||
package net.tccn.service;
|
||||
|
||||
import com.arangodb.ArangoDBException;
|
||||
import com.google.gson.Gson;
|
||||
import net.tccn.base.Kv;
|
||||
import net.tccn.base.arango.ArangoSource;
|
||||
import org.redkale.net.http.HttpRequest;
|
||||
import org.redkale.net.http.HttpResponse;
|
||||
import org.redkale.net.http.HttpServlet;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* @author: liangxianyou at 2018/11/8 17:05.
|
||||
*/
|
||||
public class BaseServlet extends HttpServlet {
|
||||
|
||||
public Logger logger = Logger.getLogger(this.getClass().getSimpleName());
|
||||
protected static Gson gson = new Gson();
|
||||
|
||||
@Override
|
||||
protected void preExecute(HttpRequest request, HttpResponse response) throws IOException {
|
||||
String sessionid = request.getParameter("token");
|
||||
if (sessionid == null) {
|
||||
sessionid = request.getHeader("token");
|
||||
}
|
||||
if (sessionid == null) {
|
||||
sessionid = request.getSessionid(true);
|
||||
}
|
||||
|
||||
if (sessionid != null) {
|
||||
//User user = userService.current(sessionid);
|
||||
//request.setCurrentUser(user);
|
||||
}
|
||||
|
||||
super.preExecute(request, response);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void authenticate(HttpRequest request, HttpResponse response) throws IOException {
|
||||
/* fixme: 权限拦截
|
||||
if (request.currentUser() == null) {
|
||||
if ("XMLHttpRequest".equals(request.getHeader("X-Requested-With"))){
|
||||
response.finish(JBean.by(-2, "未登陆"));
|
||||
}else {
|
||||
response.finish(HttpScope.refer("/user/login.html"));
|
||||
}
|
||||
return;
|
||||
}*/
|
||||
super.authenticate(request, response);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(HttpRequest request, HttpResponse response) throws IOException {
|
||||
try {
|
||||
super.execute(request, response);
|
||||
} catch (ArangoDBException e) {
|
||||
logger.log(Level.INFO, "arangodb init!", e);
|
||||
ArangoSource.init();
|
||||
}
|
||||
}
|
||||
|
||||
public Kv getParams(HttpRequest request, String... key) {
|
||||
Kv kv = Kv.of();
|
||||
for (String k : key) {
|
||||
if (k.contains("=")) { //如果没有值使用默认值
|
||||
kv.put(k.split("=")[0], request.getParameter(k.split("=")[0], k.split("=")[1]));
|
||||
continue;
|
||||
} else if (k.contains("<")) { //强制使用"<"右侧的值
|
||||
kv.put(k.split("<")[0], k.split("<")[1]);
|
||||
continue;
|
||||
}
|
||||
kv.put(k, request.getParameter(k));
|
||||
}
|
||||
return kv;
|
||||
}
|
||||
}
|
||||
181
src/main/java/net/tccn/service/MetadataService.java
Normal file
181
src/main/java/net/tccn/service/MetadataService.java
Normal file
@@ -0,0 +1,181 @@
|
||||
package net.tccn.service;
|
||||
|
||||
import net.tccn.base.JBean;
|
||||
import net.tccn.base.Kv;
|
||||
import net.tccn.dbq.DbPlat;
|
||||
import net.tccn.dbq.Field;
|
||||
import net.tccn.dbq.qtask.SysPlat;
|
||||
import net.tccn.meta.MetaKit;
|
||||
import net.tccn.meta.MetaService;
|
||||
import net.tccn.meta.MetaTable;
|
||||
import org.redkale.net.http.RestMapping;
|
||||
import org.redkale.net.http.RestParam;
|
||||
import org.redkale.net.http.RestService;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.logging.Level;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* @author: liangxianyou at 2018/10/17 16:59.
|
||||
*/
|
||||
@RestService(name = "meta", automapping = true, comment = "元数据服务")
|
||||
public class MetadataService extends BaseService { //arango
|
||||
|
||||
@Resource
|
||||
private QtaskService qtaskService;
|
||||
|
||||
|
||||
public MetaTable findMetaTable(String name, String token) {
|
||||
return getT("meta_cols_" + name + token, MetaTable.class, () -> {
|
||||
SysPlat sysPlat = qtaskService.getSysPlat(token);
|
||||
if (sysPlat == null) {
|
||||
logger.log(Level.INFO, "平台信息未知");
|
||||
throw new IllegalArgumentException("平台信息未知");
|
||||
}
|
||||
|
||||
return MetaKit.getMetaTables()
|
||||
.stream()
|
||||
.filter(x -> {
|
||||
//fixme: 平台过滤
|
||||
return x.getName().equals(name);
|
||||
})
|
||||
.findAny()
|
||||
.get();
|
||||
});
|
||||
}
|
||||
|
||||
@RestMapping(name = "list_cfg", auth = false, comment = "内容列表配置")
|
||||
public JBean listCfg(String key, @RestParam(name = "platToken") String token) {
|
||||
JBean jBean = JBean.by(0, "");
|
||||
|
||||
MetaService metaService = MetaKit.metaService(key);
|
||||
fixme: jBean.set(0, "", MetaKit.builderCfg.apply(metaService));
|
||||
return jBean;
|
||||
}
|
||||
|
||||
//----------- 数据平台 ---------------
|
||||
@RestMapping(name = "db_plat_list", comment = "数据平台")
|
||||
public List<DbPlat> dbPlatList() {
|
||||
return MetaKit.getDbPlats();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
//----------- 元数据管理 ---------------
|
||||
@RestMapping(name = "tablelist", comment = "table列表")
|
||||
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) {
|
||||
return jBean.set(-1, "平台信息未知");
|
||||
}
|
||||
|
||||
List<Kv> list = MetaKit.getMetaTables()
|
||||
.stream()
|
||||
.filter(x -> {
|
||||
return (isEmpty.test(catalog) || catalog.equals(x.getCatalog())) &&
|
||||
(isEmpty.test(dbPlatId) || dbPlatId.equals(x.getDbPlatId())) &&
|
||||
(isEmpty.test(name) || x.getName().contains(name));
|
||||
})
|
||||
.sorted(Comparator.comparing(MetaTable::getName))
|
||||
.map(x -> {
|
||||
//组装返回的数据
|
||||
Kv kv = Kv.of("name", x.getName())
|
||||
.set("comment", x.getComment())
|
||||
.set("catalog", x.getCatalog());
|
||||
MetaKit.getDbPlats().stream().filter(d -> d.getKey().equals(x.getDbPlatId())).findAny().ifPresent(d -> kv.set("dbPlatName", d.getName()));
|
||||
return kv;
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
|
||||
return jBean.setBody(list);
|
||||
}
|
||||
|
||||
@RestMapping(name = "tableinfo", comment = "table详情")
|
||||
public JBean tableInfo(@RestParam(name = "platToken") String token, String name) {
|
||||
return JBean.by(0, "", findMetaTable(name, token));
|
||||
}
|
||||
|
||||
//修改item的排序
|
||||
@RestMapping(name = "itemsort", comment = "字段排序")
|
||||
public String[] itemSortSave(String serviceKey , String[] items, @RestParam(name = "platToken") String token) {
|
||||
|
||||
MetaTable metaTable = findMetaTable(serviceKey, token);
|
||||
MetaKit.sortItem.apply(metaTable, items);
|
||||
metaTable.update();
|
||||
return items;
|
||||
}
|
||||
|
||||
@RestMapping(name = "itemupdate", comment = "字段修改")
|
||||
public List<Field> itemUpdate(String serviceKey , List<Field> items, @RestParam(name = "platToken") String token) {
|
||||
MetaTable metaTable = findMetaTable(serviceKey, token);
|
||||
|
||||
MetaKit.itemUpdate.apply(metaTable, items);
|
||||
metaTable.update();
|
||||
return items;
|
||||
}
|
||||
|
||||
@RestMapping(name = "showsort", comment = "展示字段修改")
|
||||
public List<String> showSort(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.setShows(items);
|
||||
metaTable.update();
|
||||
return items;
|
||||
}
|
||||
|
||||
@RestMapping(name = "exportsort", comment = "导出字段排序保存")
|
||||
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();
|
||||
return items;
|
||||
}
|
||||
|
||||
@RestMapping(name = "importsort", comment = "导入字段保存")
|
||||
public List<String> importSort(String serviceKey , List<String> items, @RestParam(name = "platToken") String token) {
|
||||
if (isEmpty.test(items)) return null;
|
||||
|
||||
MetaTable metaTable = findMetaTable(serviceKey, token);
|
||||
//fixme: metaTable.setImports(items);
|
||||
metaTable.update();
|
||||
return items;
|
||||
}
|
||||
|
||||
@RestMapping(name = "dbplatupdate", comment = "数据平台修改")
|
||||
public JBean dbPlatUpdate(MetaTable metaTable, @RestParam(name = "platToken") String token) {
|
||||
|
||||
MetaTable _metaTable = MetaKit.getMetaTableByKey(metaTable.getKey());
|
||||
_metaTable.setComment(metaTable.getComment());
|
||||
_metaTable.setCatalog(metaTable.getCatalog());
|
||||
_metaTable.setDbPlatId(metaTable.getDbPlatId());
|
||||
_metaTable.setCatalog(metaTable.getCatalog());
|
||||
|
||||
_metaTable.update();
|
||||
return JBean.by(0, "");
|
||||
}
|
||||
|
||||
@RestMapping(name = "filter_update", comment = "查询配置修改")
|
||||
public JBean filterUpdate(@RestParam(name = "serviceKey") String serviceKey,
|
||||
@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();
|
||||
|
||||
return JBean.by(0, "");
|
||||
}
|
||||
|
||||
}
|
||||
122
src/main/java/net/tccn/service/QtaskService.java
Normal file
122
src/main/java/net/tccn/service/QtaskService.java
Normal file
@@ -0,0 +1,122 @@
|
||||
package net.tccn.service;
|
||||
|
||||
import net.tccn.base.JBean;
|
||||
import net.tccn.base.Kv;
|
||||
import net.tccn.base.arango.ArangoSource;
|
||||
import net.tccn.dbq.DbPlat;
|
||||
import net.tccn.dbq.qtask.Qtask;
|
||||
import net.tccn.dbq.qtask.SysPlat;
|
||||
import org.redkale.net.http.RestMapping;
|
||||
import org.redkale.net.http.RestParam;
|
||||
import org.redkale.net.http.RestService;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.sql.SQLException;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
/**
|
||||
* @author: liangxianyou at 2018/11/13 18:14.
|
||||
*/
|
||||
@RestService(name = "qtask", automapping = true, comment = "qtask查询服务")
|
||||
public class QtaskService extends BaseService {
|
||||
|
||||
private Predicate<String> checkObj = (s) -> s != null && s.startsWith("{") && s.endsWith("}");
|
||||
|
||||
public SysPlat getSysPlat(String token) {
|
||||
return getT(token, SysPlat.class, () -> SysPlat.dao.findFirst(new SysPlat(token)));
|
||||
}
|
||||
|
||||
///qtask/query?queryId=platform.list¶={id:211}
|
||||
@RestMapping(name = "call", comment = "qtask查询")
|
||||
public JBean call(String queryId, String para, @RestParam(name = "platToken") String token) {
|
||||
JBean jBean = JBean.by(0, "");
|
||||
do {
|
||||
SysPlat sysPlat = getSysPlat(token);
|
||||
if (sysPlat == null) {
|
||||
jBean.set(-1, "平台信息未知"); break;
|
||||
}
|
||||
|
||||
Qtask qtask = getQTask(queryId, sysPlat.getKey());
|
||||
if (qtask == null) {
|
||||
jBean.set(-1, "未知任务"); break;
|
||||
}
|
||||
|
||||
if (!checkObj.test(para)) {
|
||||
para = qtask.getPara();
|
||||
}
|
||||
Kv kv = null;
|
||||
if (checkObj.test(para)) {
|
||||
kv = BaseService.gson.fromJson(para, Kv.class);
|
||||
}
|
||||
try {
|
||||
jBean.setBody(run(qtask, kv));
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
return jBean.set(-1, "数据查询失败", e.getMessage());
|
||||
}
|
||||
} while (false);
|
||||
return jBean;
|
||||
}
|
||||
|
||||
|
||||
@RestMapping(name = "test", comment = "qtask调试")
|
||||
public JBean test(Qtask qtask) {
|
||||
JBean jBean = JBean.by(0, "");
|
||||
do {
|
||||
//check data
|
||||
if (isEmpty.test(qtask.getSql())) {
|
||||
jBean.set(-1, "查询失败,请设置正确的【SQL】"); break;
|
||||
}
|
||||
if (isEmpty.test(qtask.getPlatId()) || isEmpty.test(qtask.getCatalog())) {
|
||||
jBean.set(-1, "查询失败,请设置正确的【数据平台】"); break;
|
||||
}
|
||||
|
||||
DbPlat dbPlat = DbPlat.dao.findByKey(qtask.getPlatId());//arangoSource.findById("db_plat/" + qTask.getPlatId(), DbPlat.class);
|
||||
if (dbPlat == null) {
|
||||
jBean.set(-1, "查询失败,请设置正确的【数据平台】"); break;
|
||||
}
|
||||
Kv kv = null;
|
||||
if (checkObj.test(qtask.getPara())) {
|
||||
try {
|
||||
kv = BaseService.gson.fromJson(qtask.getPara(), Kv.class);
|
||||
} catch (Exception e) {
|
||||
jBean.set(-1, "查询失败,请设置正确的【查询参数】"); break;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
jBean.setBody(run(qtask, kv));
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
jBean.set(-1, e.getMessage());
|
||||
}
|
||||
} while (false);
|
||||
return jBean;
|
||||
}
|
||||
|
||||
// /qtask/query?queryId=platform.list¶={id:211}
|
||||
@RestMapping(ignore = true, comment = "qtask查询数据")
|
||||
private Object run(Qtask qTask, Kv kv) throws SQLException {
|
||||
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
//----------- Qtask ---------------
|
||||
|
||||
@RestMapping(name = "qsave", comment = "qtask保存")
|
||||
public JBean qtaskSave(Qtask qtask) {
|
||||
JBean jBean = JBean.by(0, "");
|
||||
|
||||
return jBean;
|
||||
}
|
||||
|
||||
@RestMapping(ignore = true, comment = "qtask获取")
|
||||
public Qtask getQTask(String queryId, String sysPlatId) {
|
||||
Qtask qtask = null;
|
||||
|
||||
return qtask;
|
||||
}
|
||||
|
||||
//-------------------------------------------------------
|
||||
}
|
||||
Reference in New Issue
Block a user