11 changed files with 348 additions and 24 deletions
@ -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<FineBusinessTable> allTables = CellCreator.getCellProvider().tableCell().getAllTableWithoutAuth(); |
||||||
|
allTables.forEach(fineBusinessTable -> { |
||||||
|
Map<String, List> values = new HashMap<String, List>(); |
||||||
|
if (fineBusinessTable instanceof FineExcelBusinessTable) { |
||||||
|
|
||||||
|
FineAttachment sheetInfo = ((FineExcelBusinessTable) fineBusinessTable).getBaseAttach(); |
||||||
|
String attachId = sheetInfo.getId(); |
||||||
|
String fileName = sheetInfo.getFileName(); |
||||||
|
int sheetNo = 0; |
||||||
|
|
||||||
|
List<FineBusinessField> 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 extends FineService> T getBean(Class<T> 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; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,15 @@ |
|||||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||||
|
<module type="JAVA_MODULE" version="4"> |
||||||
|
<component name="NewModuleRootManager"> |
||||||
|
<output url="file://$USER_HOME$/env/direct-bi/WEB-INF/plugins/plugin-com.fr.plugin.hw.import.excel-2.0.0/classes" /> |
||||||
|
<output-test url="file://$MODULE_DIR$/../../../../direct-bi/nuclear-maven/classes/test/main" /> |
||||||
|
<exclude-output /> |
||||||
|
<content url="file://$MODULE_DIR$"> |
||||||
|
<sourceFolder url="file://$MODULE_DIR$/java" isTestSource="false" /> |
||||||
|
</content> |
||||||
|
<orderEntry type="inheritedJdk" /> |
||||||
|
<orderEntry type="sourceFolder" forTests="false" /> |
||||||
|
<orderEntry type="library" name="lib" level="project" /> |
||||||
|
<orderEntry type="module" module-name="direct-common-adapter" /> |
||||||
|
</component> |
||||||
|
</module> |
@ -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); |
||||||
|
})(); |
@ -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); |
||||||
|
})(); |
Loading…
Reference in new issue