'包结构调整'
This commit is contained in:
@@ -1,87 +0,0 @@
|
||||
package net.tccn.service;
|
||||
|
||||
import com.arangodb.ArangoDBException;
|
||||
import com.google.gson.Gson;
|
||||
import net.tccn.base.JBean;
|
||||
import net.tccn.base.Kv;
|
||||
import net.tccn.base.arango.ArangoSource;
|
||||
import net.tccn.user.User;
|
||||
import net.tccn.user.UserService;
|
||||
import org.redkale.net.http.HttpRequest;
|
||||
import org.redkale.net.http.HttpResponse;
|
||||
import org.redkale.net.http.HttpScope;
|
||||
import org.redkale.net.http.HttpServlet;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
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 {
|
||||
|
||||
@Resource
|
||||
private UserService userService;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
75
src/main/java/net/tccn/service/PlatService.java
Normal file
75
src/main/java/net/tccn/service/PlatService.java
Normal file
@@ -0,0 +1,75 @@
|
||||
package net.tccn.service;
|
||||
|
||||
import net.tccn.base.JBean;
|
||||
import net.tccn.base.MetaKit;
|
||||
import net.tccn.base.PageBean;
|
||||
import net.tccn.dbq.jdbc.api.DbAccount;
|
||||
import net.tccn.plat.DbPlat;
|
||||
import net.tccn.plat.SysPlat;
|
||||
import org.redkale.net.http.RestMapping;
|
||||
import org.redkale.net.http.RestService;
|
||||
import org.redkale.source.Flipper;
|
||||
import org.redkale.util.Comment;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestService(name = "plat", automapping = true, comment = "业务/数据平台")
|
||||
public class PlatService extends BaseService {
|
||||
|
||||
@RestMapping(name = "list", comment = "平台列表")
|
||||
public JBean list(SysPlat plat, Flipper flipper) {
|
||||
JBean jBean = new JBean();
|
||||
|
||||
//PageBean<SysPlat> page = SysPlat.dao.findPage(plat, flipper);
|
||||
List<SysPlat> list = MetaKit.getSysPlats();
|
||||
PageBean page = PageBean.by(list, list.size());
|
||||
|
||||
return jBean.setBody(page);
|
||||
}
|
||||
|
||||
@Comment("平台信息保存")
|
||||
public JBean save(SysPlat plat) {
|
||||
if (plat.getKey() == null) {
|
||||
plat.save();
|
||||
} else {
|
||||
plat.update();
|
||||
}
|
||||
|
||||
return new JBean();
|
||||
}
|
||||
|
||||
@RestMapping(name = "info", comment = "平台详情")
|
||||
public void info(int key) {
|
||||
|
||||
}
|
||||
|
||||
//------------------------
|
||||
|
||||
@RestMapping(name = "db_list", comment = "数据源列表")
|
||||
public JBean dbList(DbPlat plat, Flipper flipper) {
|
||||
JBean jBean = new JBean();
|
||||
|
||||
//PageBean<DbPlat> page = DbPlat.dao.findPage(plat, flipper);
|
||||
|
||||
List<DbAccount> list = MetaKit.getDbPlats();
|
||||
PageBean page = PageBean.by(list, list.size());
|
||||
|
||||
return jBean.setBody(page);
|
||||
}
|
||||
|
||||
@RestMapping(name = "db_save", comment = "数据源信息保存")
|
||||
public JBean dbSave(DbPlat plat) {
|
||||
//DbAccount dbPlat = MetaKit.getDbPlat(plat.getKey());
|
||||
|
||||
if (plat.getKey() == null) {
|
||||
plat.save();
|
||||
} else {
|
||||
plat.update();
|
||||
}
|
||||
MetaKit.reload(DbPlat.class);
|
||||
|
||||
return new JBean();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
56
src/main/java/net/tccn/service/UserService.java
Normal file
56
src/main/java/net/tccn/service/UserService.java
Normal file
@@ -0,0 +1,56 @@
|
||||
package net.tccn.service;
|
||||
|
||||
import net.tccn.base.JBean;
|
||||
import net.tccn.user.User;
|
||||
import org.redkale.net.http.RestMapping;
|
||||
import org.redkale.net.http.RestService;
|
||||
import org.redkale.net.http.RestSessionid;
|
||||
|
||||
/**
|
||||
* @author: liangxianyou at 2018/11/22 17:16.
|
||||
*/
|
||||
@RestService(name = "user", automapping = true, comment = "用户服务")
|
||||
public class UserService extends BaseService {
|
||||
|
||||
@RestMapping(name = "login", auth = false, comment = "登陆验证")
|
||||
public JBean login(@RestSessionid String sessionid,
|
||||
String username,
|
||||
String pwd) {
|
||||
User bean = new User();
|
||||
bean.setUsername(username);
|
||||
|
||||
User user = User.dao.findFirst(bean);
|
||||
if (user == null) {
|
||||
return JBean.by(-1, "登陆失败:账号无效");
|
||||
}
|
||||
|
||||
JBean jBean = user.checkLogin(pwd);
|
||||
if (jBean.getCode() == 0) {
|
||||
cacheSource.set(30 * 60 * 2,sessionid, User.class, user);
|
||||
|
||||
user.setSessionid(sessionid);
|
||||
user.setLoginTime(System.currentTimeMillis());
|
||||
user.update();
|
||||
}
|
||||
|
||||
return jBean;
|
||||
}
|
||||
|
||||
@RestMapping(name = "current")
|
||||
public User current(@RestSessionid String sessionid) {
|
||||
return getT("user_" + sessionid, User.class, () -> User.dao.findFirst(new User(sessionid)));
|
||||
}
|
||||
|
||||
@RestMapping(name = "logout", comment = "退出登陆")
|
||||
public JBean logout(@RestSessionid String sessionid) {
|
||||
User user = User.dao.findFirst(new User(sessionid));
|
||||
if (user != null) {
|
||||
user.setSessionid("");
|
||||
user.update();
|
||||
}
|
||||
cacheSource.removeAsync("user_" + sessionid);
|
||||
|
||||
return new JBean();
|
||||
}
|
||||
|
||||
}
|
||||
101
src/main/java/net/tccn/service/_DbService.java
Normal file
101
src/main/java/net/tccn/service/_DbService.java
Normal file
@@ -0,0 +1,101 @@
|
||||
package net.tccn.service;
|
||||
|
||||
import net.tccn.base.JBean;
|
||||
import net.tccn.base.MetaKit;
|
||||
import net.tccn.dbq.jdbc.api.DbAccount;
|
||||
import net.tccn.dbq.jdbc.api.DbKit;
|
||||
import net.tccn.dbq.table.Column;
|
||||
import net.tccn.dbq.table.Table;
|
||||
import net.tccn.meta.MetaTable;
|
||||
import net.tccn.service.BaseService;
|
||||
import org.redkale.net.http.RestMapping;
|
||||
import org.redkale.net.http.RestService;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@RestService(automapping = true, comment = "数据库操作类")
|
||||
public class _DbService extends BaseService {
|
||||
|
||||
@RestMapping(name = "catalog_list", comment = "获取数据源的database")
|
||||
public JBean catalogList(DbAccount dbAccount, String dbPlatId) {
|
||||
JBean jBean = new JBean();
|
||||
DbKit dbKit = null;
|
||||
if (dbAccount != null) {
|
||||
dbKit = new DbKit(dbAccount);
|
||||
} else {
|
||||
dbKit = MetaKit.getDbKit(dbPlatId);
|
||||
}
|
||||
|
||||
List<Map> list = dbKit.findList("SHOW DATABASES;", Map.class);
|
||||
|
||||
Stream<String> database = list.stream().map(x -> String.valueOf(x.get("Database")));
|
||||
|
||||
return jBean.setBody(database.toArray());
|
||||
}
|
||||
|
||||
@RestMapping(name = "table_list", comment = "数据库表列表")
|
||||
public List<Table> tableList(String dbPlatId, String[] catalogs) {
|
||||
|
||||
DbKit dbKit = MetaKit.getDbKit(dbPlatId);
|
||||
|
||||
StringBuffer sqlBuf = new StringBuffer("SELECT TABLE_NAME 'name',TABLE_COMMENT 'comment',table_schema 'catalog' FROM INFORMATION_SCHEMA.TABLES");
|
||||
if (catalogs != null && catalogs.length > 0) {
|
||||
sqlBuf.append(" WHERE TABLE_SCHEMA in (");
|
||||
for (String catalog : catalogs) {
|
||||
sqlBuf.append("'").append(catalog).append("',");
|
||||
}
|
||||
sqlBuf.deleteCharAt(sqlBuf.length() - 1);
|
||||
sqlBuf.append(")");
|
||||
}
|
||||
List<Table> list = dbKit.findList(sqlBuf.toString(), Table.class);
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
@RestMapping(name = "table_info", comment = "数据库表详情")
|
||||
public JBean tableInfo(String dbPlatId, String catalog, String tableName) {
|
||||
JBean jBean = new JBean();
|
||||
DbKit dbKit = MetaKit.getDbKit(dbPlatId);
|
||||
|
||||
String sql = String.format("SELECT TABLE_NAME 'name',TABLE_COMMENT 'comment',table_schema 'catalog' FROM INFORMATION_SCHEMA.TABLES where TABLE_NAME='%s'", tableName);
|
||||
String columnSql = String.format("SHOW FULL COLUMNS FROM %s.`%s`", catalog, tableName);
|
||||
|
||||
CompletableFuture<Table> tableFuture = CompletableFuture.supplyAsync(() -> dbKit.findfirst(sql, Table.class));
|
||||
CompletableFuture<List<Column>> columnFuture = CompletableFuture.supplyAsync(() -> dbKit.findList(columnSql, Column.class));
|
||||
|
||||
try {
|
||||
Table table = tableFuture.get();
|
||||
table.setColumns(columnFuture.get());
|
||||
|
||||
//jBean.setBody(table);
|
||||
jBean.setBody(MetaTable.toAs(table)); //todo: 将此转换提取到 单独方法中<依据六边形架构原则>
|
||||
} catch (InterruptedException | ExecutionException e) {
|
||||
jBean.set(-1, "查询表信息失败");
|
||||
new IllegalArgumentException("查询表信息失败", e);
|
||||
}
|
||||
|
||||
return jBean;
|
||||
}
|
||||
|
||||
@RestMapping(name = "table_create", comment = "新建表[mysql]")
|
||||
public JBean tableCreate(String dbPlatId, String catalog, String sql) {
|
||||
JBean jBean = new JBean();
|
||||
|
||||
DbKit dbKit = MetaKit.getDbKit(dbPlatId);
|
||||
dbKit.createTable(sql);
|
||||
|
||||
return jBean;
|
||||
}
|
||||
|
||||
@RestMapping(ignore = true, comment = "查询表信息")
|
||||
public List<Table> tableList(String dbPlatId, String catalog, String ... tables) {
|
||||
//todo:
|
||||
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
131
src/main/java/net/tccn/service/_TableService.java
Normal file
131
src/main/java/net/tccn/service/_TableService.java
Normal file
@@ -0,0 +1,131 @@
|
||||
package net.tccn.service;
|
||||
|
||||
import net.tccn.base.JBean;
|
||||
import net.tccn.base.Kv;
|
||||
import net.tccn.base.MetaKit;
|
||||
import net.tccn.dbq.table.Table;
|
||||
import net.tccn.meta.MetaTable;
|
||||
import org.redkale.net.http.RestMapping;
|
||||
import org.redkale.net.http.RestParam;
|
||||
import org.redkale.net.http.RestService;
|
||||
import org.redkale.util.Comment;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
/**
|
||||
* 元数据实体管理
|
||||
* 1、实体导入,
|
||||
* 2、实体信息维护
|
||||
* 3、数据库表管理
|
||||
*/
|
||||
@RestService(automapping = true, comment = "元数据实体管理")
|
||||
public class _TableService extends BaseService {
|
||||
|
||||
@Resource
|
||||
_FileService fileService;
|
||||
@Resource
|
||||
_DbService dbService;
|
||||
|
||||
|
||||
@RestMapping(name = "sheets", comment = "导入选择列表数据准备")
|
||||
public JBean sheets(String cate, //类型
|
||||
//excel {文件地址}
|
||||
String filePath,
|
||||
|
||||
//mysql {数据库连接账号、数据源id、数据库database数组}
|
||||
String dbPlatId, String[] catalogs,
|
||||
@RestParam(name = "platToken") String token) {
|
||||
|
||||
JBean jBean = new JBean();
|
||||
|
||||
if ("excel".equals(cate)) {
|
||||
jBean.setBody(fileService.data(filePath, token));
|
||||
|
||||
} else if ("mysql".equals(cate)){
|
||||
List<Table> list = dbService.tableList(dbPlatId, catalogs);
|
||||
|
||||
String[] tableArr = list.stream().map(Table::getName).toArray(String[]::new);
|
||||
CompletableFuture<List<String>> hvFuture = tableExist(tableArr, token);
|
||||
|
||||
try {
|
||||
List<String> _hv = hvFuture.get();
|
||||
|
||||
List<MetaTable> sheets = new ArrayList<>();
|
||||
list.forEach(x -> {
|
||||
MetaTable bean = MetaTable.toAs(x);
|
||||
bean.setHv(_hv.contains(x.getName()) ? 1 : 0);
|
||||
sheets.add(bean);
|
||||
});
|
||||
|
||||
//对数据分组后返回
|
||||
Kv<String, MetaTable> data = Kv.of();
|
||||
sheets.forEach(x -> data.set(x.getName(), x));
|
||||
|
||||
jBean.setBody(data);
|
||||
} catch (InterruptedException | ExecutionException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
return jBean;
|
||||
}
|
||||
|
||||
|
||||
/*public JBean tableInfo(DbAccount dbAccount,
|
||||
String dbPlatId, String catalog, String tableName) {
|
||||
|
||||
dbService.tableInfo(dbAccount, dbPlatId, catalog, tableName)
|
||||
|
||||
return null;
|
||||
}*/
|
||||
|
||||
@RestMapping(name = "table_save", comment = "保存数据源实体到元数据实体表")
|
||||
public void tableSave(String dbPlatId,
|
||||
String catalog,
|
||||
String[] tableArr,
|
||||
@RestParam(name = "platToken") String token) {
|
||||
|
||||
CompletableFuture<List<String>> hvfuture = tableExist(tableArr, token);
|
||||
List<Table> tables = dbService.tableList(dbPlatId, catalog, tableArr);
|
||||
|
||||
try {
|
||||
List<String> hvs = hvfuture.get();
|
||||
tables.forEach(t -> {
|
||||
if (!hvs.contains(t.getName())) {
|
||||
MetaTable metaTable = MetaTable.toAs(t);
|
||||
metaTable.setCatalog(catalog);
|
||||
metaTable.setDbPlatId(dbPlatId);
|
||||
metaTable.setAlias(MetaKit.nextAlias());//todo: 表别名
|
||||
metaTable.setSysPlatId(platId(token));
|
||||
|
||||
//保存数据到元数据表
|
||||
metaTable.save();
|
||||
}
|
||||
});
|
||||
} catch (InterruptedException | ExecutionException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Comment("查询元数据中存在的表")
|
||||
private CompletableFuture<List<String>> tableExist(String[] tableArr, String token) {
|
||||
return CompletableFuture.supplyAsync(() -> {
|
||||
StringBuffer buf = new StringBuffer();
|
||||
buf.append("for d in MetaTable\n" +
|
||||
" filter d.name in [");
|
||||
for (String x : tableArr) {
|
||||
buf.append("'").append(x).append("',");
|
||||
}
|
||||
buf.deleteCharAt(buf.length() - 1);
|
||||
buf.append("] and d.sysPlatId=='" + platId(token) + "'\n" +
|
||||
" return d.name");
|
||||
List<String> hv = MetaTable.dao.find(buf.toString(), String.class); //在元数据中已经存在的sheet
|
||||
return hv;
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user