diff --git a/src/main/java/com/fr/plugin/decision/http/DeleteLinkHttpHandle.java b/src/main/java/com/fr/plugin/decision/http/DeleteLinkHttpHandle.java index 6d6a52d..786deeb 100644 --- a/src/main/java/com/fr/plugin/decision/http/DeleteLinkHttpHandle.java +++ b/src/main/java/com/fr/plugin/decision/http/DeleteLinkHttpHandle.java @@ -17,7 +17,6 @@ import com.fr.web.utils.WebUtils; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.sql.Connection; -import java.sql.Statement; public class DeleteLinkHttpHandle extends BaseHttpHandler { @@ -56,7 +55,7 @@ DeleteLinkHttpHandle extends BaseHttpHandler { conn = JdbcUtils.getConnection(connection); Dialect dialect = DialectFactory.generateDialect(conn); Table table = new Table(schema, tableName); - DML.deleteTable(dialect, conn, table); + DML.dropTable(dialect, conn, table); FineLoggerFactory.getLogger().info("表删除成功"); ExcelLinkService.getInstance().deleteLinkById(id); diff --git a/src/main/java/com/fr/plugin/decision/request/HWHandlerProvider.java b/src/main/java/com/fr/plugin/decision/request/HWHandlerProvider.java index 6c4e7a9..93ede2d 100644 --- a/src/main/java/com/fr/plugin/decision/request/HWHandlerProvider.java +++ b/src/main/java/com/fr/plugin/decision/request/HWHandlerProvider.java @@ -15,7 +15,8 @@ public class HWHandlerProvider extends AbstractHttpHandlerProvider { new ExcelSheetCountHandler(), new ExcelFiledHandler(), new TableDataImport(), - new UpgradeHandler() + new UpgradeHandler(), + new CheckHandler() }; } } diff --git a/src/main/java/com/fr/plugin/decision/request/HWURLAliasProvider.java b/src/main/java/com/fr/plugin/decision/request/HWURLAliasProvider.java index a250f12..5a36f96 100644 --- a/src/main/java/com/fr/plugin/decision/request/HWURLAliasProvider.java +++ b/src/main/java/com/fr/plugin/decision/request/HWURLAliasProvider.java @@ -17,6 +17,7 @@ public class HWURLAliasProvider extends AbstractURLAliasProvider { URLAliasFactory.createPluginAlias(ExcelFiledHandler.PATH, ExcelFiledHandler.PATH,false), URLAliasFactory.createPluginAlias(TableDataImport.PATH, TableDataImport.PATH,false), URLAliasFactory.createPluginAlias(UpgradeHandler.PATH, UpgradeHandler.PATH,false), + URLAliasFactory.createPluginAlias(CheckHandler.PATH, CheckHandler.PATH,false), }; } } diff --git a/src/main/java/com/fr/plugin/decision/request/handler/CheckHandler.java b/src/main/java/com/fr/plugin/decision/request/handler/CheckHandler.java new file mode 100644 index 0000000..e76cdda --- /dev/null +++ b/src/main/java/com/fr/plugin/decision/request/handler/CheckHandler.java @@ -0,0 +1,80 @@ +package com.fr.plugin.decision.request.handler; + +import com.finebi.activate.foudation.StableManager; +import com.finebi.common.impl.vo.table.FineBusinessTable; +import com.finebi.common.impl.vo.table.FineExcelBusinessTable; +import com.finebi.common.api.cell.context.CellCreator; +import com.finebi.foundation.api.service.context.BaseContext; +import com.fr.json.JSONObject; +import com.fr.log.FineLoggerFactory; +import com.fr.plugin.decision.utils.JdbcUtils; +import com.fr.web.utils.WebUtils; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.sql.Connection; +import java.util.List; +import java.sql.SQLException; +import java.sql.DatabaseMetaData; +import java.util.ArrayList; +import java.sql.ResultSet; + +public class CheckHandler extends HWAbstractHandler { + public static final String PATH="/excelmng/check"; + @Override + protected void deal(HttpServletRequest req, HttpServletResponse res, JSONObject para, JSONObject result) throws Exception { + + String connection = para.getString("connection"); + + Connection conn = JdbcUtils.getConnection(connection); + + if (conn == null){ + FineLoggerFactory.getLogger().error("HW_excel校验重名:数据链接 " + connection + " 获取异常" ); + WebUtils.printAsJSON(res, new JSONObject().put("status","fail").put("errorText","获取数据连接异常")); + return; + } + List tables = getTables(conn); + + JSONObject finalResult = JSONObject.create(); + + ArrayList excelList = new ArrayList(); + + List allTables = CellCreator.getCellProvider().tableCell().getAllTableWithoutAuth(); + allTables.forEach(fineBusinessTable -> { + if (fineBusinessTable instanceof FineExcelBusinessTable) { + excelList.add(fineBusinessTable.getName()); + } + }); + + tables.retainAll(excelList); + + WebUtils.printAsJSON(res, finalResult.put("checkResult", tables)); + } + + @Override + public String getPath() { + return PATH; + } + + /** + * service获取集合 + */ + private static BaseContext getContext() { + return StableManager.getContext(); + } + + /**获取数据库中所有表名称 + * @param conn + * @return + * @throws SQLException + */ + private static List getTables(Connection conn) throws SQLException { + DatabaseMetaData databaseMetaData = conn.getMetaData(); + ResultSet tables = databaseMetaData.getTables(null, null, "%", null); + ArrayList tablesList = new ArrayList(); + while (tables.next()) { + tablesList.add(tables.getString("TABLE_NAME")); + } + return tablesList; + } +} diff --git a/src/main/java/com/fr/plugin/decision/request/handler/UpgradeHandler.java b/src/main/java/com/fr/plugin/decision/request/handler/UpgradeHandler.java index bdf75fc..2a21677 100644 --- a/src/main/java/com/fr/plugin/decision/request/handler/UpgradeHandler.java +++ b/src/main/java/com/fr/plugin/decision/request/handler/UpgradeHandler.java @@ -10,6 +10,7 @@ import com.finebi.burger.api.service.FineTableService; import com.finebi.common.api.cell.context.CellCreator; import com.finebi.common.authority.api.FineAuthorityUtils; import com.finebi.common.authority.decision.role.FineAuthorityUser; +import com.finebi.common.impl.vo.pack.FineBusinessPackage; import com.finebi.common.impl.vo.relation.FineBusinessRelation; import com.finebi.common.impl.vo.table.FineDBBusinessTable; import com.finebi.foundation.api.reponse.FineRespond; @@ -46,6 +47,7 @@ import java.sql.Connection; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Optional; public class UpgradeHandler extends HWAbstractHandler { public static final String PATH="/excelmng/upgrade"; @@ -122,8 +124,6 @@ public class UpgradeHandler extends HWAbstractHandler { } } - - String name = fineBusinessTable.getName(); String schema = ""; String tableName = fineBusinessTable.getName(); @@ -136,22 +136,30 @@ public class UpgradeHandler extends HWAbstractHandler { importDbResult.put(dmlResult); if (StringUtils.equals("success", dmlResult.getString("status")) && dmlResult.getBoolean("addLink")) { - ExcelLinkService.getInstance().addLink(name, connection, schema, tableName, createUser); - FineLoggerFactory.getLogger().info("HW_excel导入:FineDB 新增数据成功" + name); - List fineBusinessRelations = CellCreator.getCellProvider().relationPathCell().getRelationsByTableNameWithoutAuth(fineBusinessTable.getName()); - CellCreator.getCellProvider().tableCell().removeTable(fineBusinessTable.getName(), FineAuthorityUser.ROOT_USER_ID); + ExcelLinkService.getInstance().addLink(tableName, connection, schema, tableName, createUser); + FineLoggerFactory.getLogger().info("HW_excel导入:FineDB 新增数据成功" + tableName); + } + if (StringUtils.equals("success", dmlResult.getString("status")) || StringUtils.contains(dmlResult.getString("errorText"), "exist")) { ObjectMapper mapper = new ObjectMapper(); JSONObject table = JSONObject.create().put("tableName", fineBusinessTable.getName()).put("connectionName", connection); JSONArray tables = JSONArray.create().put(table); JSONObject tablesBean = JSONObject.create().put("tables", tables); try{ FineAuthorityUser authorityUser = FineAuthorityUtils.getAuth(req); - String packid = "__my_analysis__" + authorityUser.getUserId(); + + Optional packageIdWithoutAuth = CellCreator.getCellProvider().packageCell().getPackageByTableNameWithoutAuth(tableName); + FineBusinessPackage businessPackage = packageIdWithoutAuth.get(); + String packId = businessPackage.getId(); + + List fineBusinessRelations = CellCreator.getCellProvider().relationPathCell().getRelationsByTableNameWithoutAuth(fineBusinessTable.getName()); + + CellCreator.getCellProvider().tableCell().removeTable(fineBusinessTable.getName(), FineAuthorityUser.ROOT_USER_ID); +// String packId = "__my_analysis__" + fineBusinessTable.getCreateByWithId() + "1"; try { String tableString = tablesBean.toString(); FineBusinessTable[] businessTables = createBusinessTablesFromDBNewAddTableInfo(mapper.readValue(tableString, DataBaseAddTableRequestBean.class), authorityUser.getUserId()); - TableAddResponseBean responed = getTableService().addNewTable(packid, authorityUser, businessTables); + TableAddResponseBean responed = getTableService().addNewTable(packId, authorityUser, businessTables); CellCreator.getCellProvider().relationPathCell().addRelations(fineBusinessRelations, authorityUser); addDbResult.put(FineRespond.success(responed)); } catch (JsonMappingException e) { diff --git a/src/main/resources/com/fr/plugin/hw/decision/js/pane/upgrade/upgrade.js b/src/main/resources/com/fr/plugin/hw/decision/js/pane/upgrade/upgrade.js index 6647ee7..fa64a85 100644 --- a/src/main/resources/com/fr/plugin/hw/decision/js/pane/upgrade/upgrade.js +++ b/src/main/resources/com/fr/plugin/hw/decision/js/pane/upgrade/upgrade.js @@ -54,6 +54,24 @@ }, { type: "bi.center_adapt", items: [{ + type: "bi.button", + width: 200, + text: "重名检测", + handler: function () { + Dec.HW.check({ + connection: self.connection.getValue()[0] + }, function (res) { + var tip = "以下excel与数据库中已存在的表重名:" + BI.each(res.checkResult, function (i, tableName) { + tip += "\n" + tableName; + }) + BI.Msg.toast(tip, { + autoClose: false, + level: "warning" + }); + }); + } + }, { type: "bi.button", width: 200, text: "升级旧Excel", @@ -62,7 +80,7 @@ connection: self.connection.getValue()[0] }, function (res) { self.store.setAddDbResult(res.addDbResult); - self.store.setImportDbResult(res.importDbResult) + self.store.setImportDbResult(res.importDbResult); }); } }] @@ -117,14 +135,11 @@ }, _createImportItems: function (array) { - var items = BI.filter(array, function (idx, item) { - return item.status === "failed"; - }) - return BI.map(items, function (idx, item) { + return BI.map(array, function (idx, item) { return { type: "bi.label", textAlign: "left", - text: item.errorText + text: item.status === "failed" ? item.errorText : item.text } }) } diff --git a/src/main/resources/com/fr/plugin/hw/decision/js/utils.js b/src/main/resources/com/fr/plugin/hw/decision/js/utils.js index 5c0a20f..977a5d6 100644 --- a/src/main/resources/com/fr/plugin/hw/decision/js/utils.js +++ b/src/main/resources/com/fr/plugin/hw/decision/js/utils.js @@ -24,6 +24,9 @@ }, upgrade: function (config, callback) { Dec.reqPost("/url/excelmng/upgrade", config, callback) + }, + check: function (config, callback) { + Dec.reqPost("/url/excelmng/check", config, callback) } }) })(); \ No newline at end of file