From bd0a897a39757b993f5719690f7576bdf596c010 Mon Sep 17 00:00:00 2001 From: "Zhenfei.Li" Date: Tue, 21 Jul 2020 22:12:05 +0800 Subject: [PATCH] =?UTF-8?q?BI-68899=20feat:=20=E6=89=B9=E9=87=8F=E5=8D=87?= =?UTF-8?q?=E7=BA=A7=E6=97=A7Excel?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 31 ++- .../decision/request/HWHandlerProvider.java | 8 +- .../decision/request/HWURLAliasProvider.java | 14 +- .../request/handler/UpgradeHandler.java | 205 ++++++++++++++++++ .../fr/plugin/decision/utils/JdbcUtils.java | 2 - src/main/main.iml | 15 ++ .../com/fr/plugin/hw/decision/js/hw.option.js | 6 +- .../fr/plugin/hw/decision/js/pane/panes.js | 4 + .../hw/decision/js/pane/upgrade/upgrade.js | 59 +++++ .../decision/js/pane/upgrade/upgrade.model.js | 25 +++ .../com/fr/plugin/hw/decision/js/utils.js | 3 + 11 files changed, 348 insertions(+), 24 deletions(-) create mode 100644 src/main/java/com/fr/plugin/decision/request/handler/UpgradeHandler.java create mode 100644 src/main/main.iml create mode 100644 src/main/resources/com/fr/plugin/hw/decision/js/pane/upgrade/upgrade.js create mode 100644 src/main/resources/com/fr/plugin/hw/decision/js/pane/upgrade/upgrade.model.js diff --git a/pom.xml b/pom.xml index f9b78ed..2bd336a 100644 --- a/pom.xml +++ b/pom.xml @@ -2,27 +2,44 @@ + - starter - com.fr.plugin - 10.0 + com.finebi.project + nuclear-maven + 5.0-RELEASE-SNAPSHOT + ../nuclear-maven/pom.xml + 4.0.0 plugin-exceldatamanagement + + + + + + + + com.finebi + direct-adapter-sdk + ${foundation-version} + + + - org.apache.maven.plugins maven-compiler-plugin - 2.0.2 + 3.3 - -Xlint:unchecked + 1.8 + 1.8 - ${project.basedir}/../webroot/WEB-INF/plugins/plugin-com.fr.plugin.exceldatamanagement-1.0/classes + /Users/leezerofly/env/direct-bi/WEB-INF/plugins/plugin-com.fr.plugin.hw.import.excel-2.0.0/classes + \ No newline at end of file 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 6fb20e3..6c4e7a9 100644 --- a/src/main/java/com/fr/plugin/decision/request/HWHandlerProvider.java +++ b/src/main/java/com/fr/plugin/decision/request/HWHandlerProvider.java @@ -2,10 +2,7 @@ package com.fr.plugin.decision.request; import com.fr.decision.fun.HttpHandler; import com.fr.decision.fun.impl.AbstractHttpHandlerProvider; -import com.fr.plugin.decision.request.handler.ExcelFiledHandler; -import com.fr.plugin.decision.request.handler.ExcelSheetCountHandler; -import com.fr.plugin.decision.request.handler.HWFileUploadHander; -import com.fr.plugin.decision.request.handler.TableDataImport; +import com.fr.plugin.decision.request.handler.*; /** * created by ezreal 2020/1/12 @@ -17,7 +14,8 @@ public class HWHandlerProvider extends AbstractHttpHandlerProvider { new HWFileUploadHander(), new ExcelSheetCountHandler(), new ExcelFiledHandler(), - new TableDataImport() + new TableDataImport(), + new UpgradeHandler() }; } } 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 7bf64d5..a250f12 100644 --- a/src/main/java/com/fr/plugin/decision/request/HWURLAliasProvider.java +++ b/src/main/java/com/fr/plugin/decision/request/HWURLAliasProvider.java @@ -3,10 +3,7 @@ package com.fr.plugin.decision.request; import com.fr.decision.fun.impl.AbstractURLAliasProvider; import com.fr.decision.webservice.url.alias.URLAlias; import com.fr.decision.webservice.url.alias.URLAliasFactory; -import com.fr.plugin.decision.request.handler.ExcelFiledHandler; -import com.fr.plugin.decision.request.handler.ExcelSheetCountHandler; -import com.fr.plugin.decision.request.handler.HWFileUploadHander; -import com.fr.plugin.decision.request.handler.TableDataImport; +import com.fr.plugin.decision.request.handler.*; /** * created by ezreal 2020/1/12 @@ -16,11 +13,10 @@ public class HWURLAliasProvider extends AbstractURLAliasProvider { public URLAlias[] registerAlias() { return new URLAlias[]{ URLAliasFactory.createPluginAlias(HWFileUploadHander.PATH, HWFileUploadHander.PATH,true), - URLAliasFactory.createPluginAlias(ExcelSheetCountHandler.PATH,ExcelSheetCountHandler.PATH,false), - URLAliasFactory.createPluginAlias(ExcelFiledHandler.PATH,ExcelFiledHandler.PATH,false), - URLAliasFactory.createPluginAlias(TableDataImport.PATH,TableDataImport.PATH,false), - - + URLAliasFactory.createPluginAlias(ExcelSheetCountHandler.PATH, ExcelSheetCountHandler.PATH,false), + URLAliasFactory.createPluginAlias(ExcelFiledHandler.PATH, ExcelFiledHandler.PATH,false), + URLAliasFactory.createPluginAlias(TableDataImport.PATH, TableDataImport.PATH,false), + URLAliasFactory.createPluginAlias(UpgradeHandler.PATH, UpgradeHandler.PATH,false), }; } } 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 new file mode 100644 index 0000000..679a76d --- /dev/null +++ b/src/main/java/com/fr/plugin/decision/request/handler/UpgradeHandler.java @@ -0,0 +1,205 @@ +package com.fr.plugin.decision.request.handler; + +import com.finebi.activate.foundation.StableManager; +import com.finebi.burger.api.bean.configuration.table.attachment.FineAttachment; +import com.finebi.burger.api.bean.configuration.table.field.FineBusinessField; +import com.finebi.burger.api.request.configuration.table.DataBaseAddTableRequestBean; +import com.finebi.burger.api.response.configuration.table.TableAddResponseBean; +import com.finebi.direct.burger.api.service.FineTableService; +import com.finebi.direct.burger.impl.service.helper.table.FineTableUtils; +import com.finebi.direct.common.authority.api.FineAuthorityUtils; +import com.finebi.foundation.api.reponse.FineRespond; +import com.finebi.foundation.api.service.FineService; +import com.finebi.foundation.api.service.context.BaseContext; +import com.finebi.utils.CompareUtils; +import com.fr.decision.webservice.v10.user.UserService; +import com.fr.json.JSONArray; +import com.fr.json.JSONObject; +import com.fr.log.FineLoggerFactory; +import com.fr.plugin.decision.HWUtils; +import com.fr.plugin.decision.core.ActionFactory; +import com.fr.plugin.decision.core.ExcelDmlBean; +import com.fr.plugin.decision.core.action.ExcelDmlAction; +import com.fr.plugin.decision.dao.ExcelLinkService; +import com.fr.plugin.decision.utils.JdbcUtils; +import com.fr.stable.StringUtils; +import com.fr.third.fasterxml.jackson.core.JsonGenerationException; +import com.fr.third.fasterxml.jackson.databind.JsonMappingException; +import com.fr.third.fasterxml.jackson.databind.ObjectMapper; +import com.fr.third.org.apache.poi.hssf.usermodel.HSSFSheet; +import com.fr.third.org.apache.poi.hssf.usermodel.HSSFWorkbook; +import com.fr.third.v2.org.apache.poi.xssf.usermodel.XSSFSheet; +import com.fr.third.v2.org.apache.poi.xssf.usermodel.XSSFWorkbook; +import com.finebi.direct.common.api.cell.context.CellCreator; +import com.finebi.common.impl.vo.table.FineBusinessTable; +import com.finebi.common.impl.vo.table.FineExcelBusinessTable; +import com.finebi.direct.common.authority.decision.role.FineAuthorityUser; +import com.fr.web.utils.WebUtils; + +import javax.servlet.http.HttpServletRequest; + +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.sql.Connection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class UpgradeHandler extends HWAbstractHandler { + public static final String PATH="/excelmng/upgrade"; + @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; + } + + final String createUser = UserService.getInstance().getCurrentUserIdFromCookie(req); + + JSONArray items = JSONArray.create(); + + JSONArray importDbResult = JSONArray.create(); + JSONArray addDbResult = JSONArray.create(); + JSONObject finalResult = JSONObject.create(); + + List allTables = CellCreator.getCellProvider().tableCell().getAllTableWithoutAuth(); + allTables.forEach(fineBusinessTable -> { + Map values = new HashMap(); + if (fineBusinessTable instanceof FineExcelBusinessTable) { + + FineAttachment sheetInfo = ((FineExcelBusinessTable) fineBusinessTable).getBaseAttach(); + String attachId = sheetInfo.getId(); + String fileName = sheetInfo.getFileName(); + int sheetNo = 0; + + List fields = ((FineExcelBusinessTable) fineBusinessTable).getExcelFields(); + + if (fileName.endsWith(".xlsx")) { + XSSFWorkbook excelWork = HWUtils.getExcelWork(attachId); + if(excelWork != null){ + XSSFSheet sheet = excelWork.getSheetAt(sheetNo); + JSONObject tableCol = JSONObject.create(); + for(int i = 0; i < fields.size(); i++){ + FineBusinessField field = fields.get(i); + tableCol.put(field.getName(), i); + + JSONObject item = new JSONObject(); + item.put("colLength", "255"); + item.put("colName", field.getName()); + item.put("colType", this.getFieldSqlType(field.getType())); + item.put("excelCol", i); + item.put("id", field.getId()); + + items.put(item); + } + values = HWUtils.getSheetContent(sheet, 0, tableCol); + } + } else if (fileName.endsWith(".xls")) { + HSSFWorkbook excel2003Work = HWUtils.getExcel2003Work(attachId); + if(excel2003Work != null){ + HSSFSheet sheetAt = excel2003Work.getSheetAt(sheetNo); + JSONObject tableCol = JSONObject.create(); + for(int i = 0; i < fields.size(); i++){ + FineBusinessField field = fields.get(i); + tableCol.put(field.getName(), i); + + JSONObject item = new JSONObject(); + item.put("colLength", "255"); + item.put("colName", field.getName()); + item.put("colType", this.getFieldSqlType(field.getType())); + item.put("excelCol", i); + item.put("id", field.getId()); + + items.put(item); + } + values = HWUtils.get2003SheetContent(sheetAt,0, tableCol); + } + } + + + String name = fineBusinessTable.getName(); + String schema = ""; + String tableName = fineBusinessTable.getName(); + + ExcelDmlAction excelDmlAction = ActionFactory.getInstance().create("add"); + + excelDmlAction.setExcelDmlBean( new ExcelDmlBean(connection, schema, tableName, items, values) ); + + try { + JSONObject dmlResult = excelDmlAction.execute(); + 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); + } + } catch(Exception e){ + e.printStackTrace(); + } + + 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() + "1"; + try { + String tableString = tablesBean.toString(); + FineBusinessTable[] businessTables = FineTableUtils.createBusinessTablesFromDBNewAddTableInfo(mapper.readValue(tableString, DataBaseAddTableRequestBean.class), authorityUser.getUserId()); + TableAddResponseBean responed = getTableService().addNewTable(packid, authorityUser, businessTables); + addDbResult.put(FineRespond.success(responed)); + } catch (JsonMappingException e) { + e.printStackTrace(); + } catch (JsonGenerationException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } + catch(Exception e){ + e.printStackTrace(); + } + + } + }); + + WebUtils.printAsJSON(res, finalResult.put("importDbResult", importDbResult).put("addDbResult", addDbResult)); + } + + @Override + public String getPath() { + return PATH; + } + + protected static FineTableService getTableService() { + return getBean(FineTableService.class); + } + + private static T getBean(Class annotatedClass) { + return getContext().getServiceBean(annotatedClass); + } + /** + * service获取集合 + */ + private static BaseContext getContext() { + return StableManager.getContext(); + } + + private int getFieldSqlType(int fieldType) { + if (CompareUtils.isEqual(fieldType, 16)) { + return 12; + } else if (CompareUtils.isEqual(fieldType, 32)) { + return 8; + } else if (CompareUtils.isEqual(fieldType, 48)) { + return 91; + } + return 12; + } +} diff --git a/src/main/java/com/fr/plugin/decision/utils/JdbcUtils.java b/src/main/java/com/fr/plugin/decision/utils/JdbcUtils.java index 40d3836..6559f7c 100644 --- a/src/main/java/com/fr/plugin/decision/utils/JdbcUtils.java +++ b/src/main/java/com/fr/plugin/decision/utils/JdbcUtils.java @@ -6,8 +6,6 @@ import com.fr.data.impl.NameDatabaseConnection; import com.fr.json.JSONArray; import com.fr.json.JSONObject; import com.fr.log.FineLoggerFactory; -import com.fr.plugin.decision.dialect.hive.HiveDialectConsummate; -import com.fr.report.core.A.S; import com.fr.stable.StringUtils; import com.fr.web.utils.WebUtils; diff --git a/src/main/main.iml b/src/main/main.iml new file mode 100644 index 0000000..f2fa783 --- /dev/null +++ b/src/main/main.iml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/resources/com/fr/plugin/hw/decision/js/hw.option.js b/src/main/resources/com/fr/plugin/hw/decision/js/hw.option.js index d4b542c..6d2ab6c 100644 --- a/src/main/resources/com/fr/plugin/hw/decision/js/hw.option.js +++ b/src/main/resources/com/fr/plugin/hw/decision/js/hw.option.js @@ -35,6 +35,9 @@ BI.$import(commPath + "/pane/authPane/auth.pane.right.js"); BI.$import(commPath + "/pane/authPane/auth.pane.right.model.js"); + BI.$import(commPath + "/pane/upgrade/upgrade.js"); + BI.$import(commPath + "/pane/upgrade/upgrade.model.js"); + BI.config("dec.constant.management.navigation", function (items) { items.push({ @@ -95,6 +98,7 @@ */ BI.constant("dec.hw.constant.tabs", [ {value: "export", text: "Excel数据管理"}, - {value: "auth", text: "Excel权限管理"} + {value: "auth", text: "Excel权限管理"}, + {value: "upgrade", text: "一键升级旧Excel"}, ]); })(); \ No newline at end of file diff --git a/src/main/resources/com/fr/plugin/hw/decision/js/pane/panes.js b/src/main/resources/com/fr/plugin/hw/decision/js/pane/panes.js index cdadafc..8b19d1a 100644 --- a/src/main/resources/com/fr/plugin/hw/decision/js/pane/panes.js +++ b/src/main/resources/com/fr/plugin/hw/decision/js/pane/panes.js @@ -60,6 +60,10 @@ return { type: "dec.mng.authPane" }; + case "upgrade": + return { + type: "dec.mng.upgrade" + }; } } }] 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 new file mode 100644 index 0000000..14d008e --- /dev/null +++ b/src/main/resources/com/fr/plugin/hw/decision/js/pane/upgrade/upgrade.js @@ -0,0 +1,59 @@ +!(function () { + var UpgradePane = BI.inherit(BI.Widget, { + props: { + baseCls: "dec-mng-upgrade" + }, + + _store: function () { + return BI.Models.getModel("dec.model.mng.upgrade"); + }, + + beforeInit: function (callback) { + this.store.initData(callback); + }, + + render: function () { + var self = this; + return { + type: "bi.vertical", + width: 640, + vgap: 15, + items: [{ + type: "bi.vertical_adapt", + items: [{ + type: "bi.label", + textAlign: "right", + cls: "dec-font-weight-bold", + text: "数据源:", + title: "数据源", + width: 100 + }, { + el: { + type: "bi.text_value_combo", + lgap: 4, + cls: "bi-border", + textAlign: "left", + items: this.model.connectionItems, + width: 480, + height: 22, + ref: function (_ref) { + self.connection = _ref; + } + }, + lgap: 10 + }] + }, { + type: "bi.button", + width: 200, + text: "升级旧Excel", + handler: function () { + Dec.HW.upgrade({ + connection: self.connection.getValue()[0] + }); + } + }] + }; + } + }); + BI.shortcut("dec.mng.upgrade", UpgradePane); +})(); \ No newline at end of file diff --git a/src/main/resources/com/fr/plugin/hw/decision/js/pane/upgrade/upgrade.model.js b/src/main/resources/com/fr/plugin/hw/decision/js/pane/upgrade/upgrade.model.js new file mode 100644 index 0000000..0a9d2b1 --- /dev/null +++ b/src/main/resources/com/fr/plugin/hw/decision/js/pane/upgrade/upgrade.model.js @@ -0,0 +1,25 @@ +!(function () { + var Model = BI.inherit(Fix.Model, { + state: function () { + return { + connectionItems: [], + }; + }, + + actions: { + initData: function (callback) { + var self = this; + Dec.Utils.getConnections(function (res) { + self.model.connectionItems = BI.map(res, function (i, val) { + return { + text: val.connectionName || "", + value: val.connectionName || "" + }; + }); + callback(); + }); + } + } + }); + BI.model("dec.model.mng.upgrade", Model); +})(); 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 72f4209..5c0a20f 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 @@ -21,6 +21,9 @@ }, queryData: function (config, callback) { Dec.reqPost("/url/excelmng/querydata", config, callback) + }, + upgrade: function (config, callback) { + Dec.reqPost("/url/excelmng/upgrade", config, callback) } }) })(); \ No newline at end of file