You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
125 lines
5.9 KiB
125 lines
5.9 KiB
package com.fr.plugin.decision.request.handler; |
|
|
|
import com.finebi.burger.api.request.configuration.table.DataBaseAddTableRequestBean; |
|
import com.finebi.common.api.cell.user.authority.UserAuthority; |
|
import com.finebi.common.api.vo.table.FineBusinessTable; |
|
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.*; |
|
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.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; |
|
|
|
/** |
|
* created by ezreal 2020/1/13 |
|
*/ |
|
public class TableDataImport extends HWAbstractHandler { |
|
public static final String PATH="/hw/table/import"; |
|
@Override |
|
protected void deal(HttpServletRequest req, HttpServletResponse res, JSONObject para, JSONObject result) throws Exception { |
|
JSONArray items=para.getJSONArray("items"); |
|
JSONObject sheetInfo=para.getJSONObject("sheetInfo"); |
|
String attachId=sheetInfo.getString("attachId"); |
|
String fileName=sheetInfo.getString("fileName"); |
|
int sheetNo=sheetInfo.getInt("sheetNo"); |
|
Map<String, List> values=new HashMap<String, List>(); |
|
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<items.length();i++){ |
|
JSONObject o=items.getJSONObject(i); |
|
tableCol.put(o.getString("colName"),o.getInt("excelCol")); |
|
} |
|
values=HWUtils.getSheetContent(sheet, sheetInfo.getInt("titleRow"), 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<items.length();i++){ |
|
JSONObject o=items.getJSONObject(i); |
|
tableCol.put(o.getString("colName"),o.getInt("excelCol")); |
|
} |
|
values=HWUtils.get2003SheetContent(sheetAt,sheetInfo.getInt("titleRow"),tableCol); |
|
} |
|
} |
|
|
|
final String createUser = UserService.getInstance().getCurrentUserIdFromCookie(req); |
|
String connection = para.getString("connection"); |
|
String name = para.getString("aliasName"); |
|
String schema = para.getString("schema"); |
|
String tableName = para.getString("tableName"); |
|
|
|
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; |
|
} |
|
|
|
ExcelDmlAction excelDmlAction = ActionFactory.getInstance().create( para.getString("infoId")); |
|
excelDmlAction.setExcelDmlBean( new ExcelDmlBean(connection, schema, tableName, items, values) ); |
|
JSONObject dmlResult = excelDmlAction.execute(); |
|
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); |
|
} |
|
|
|
if (StringUtils.equals("success", dmlResult.getString("status")) || StringUtils.contains(dmlResult.getString("errorText"), "existed")) { |
|
ObjectMapper mapper = new ObjectMapper(); |
|
JSONObject table = JSONObject.create().put("tableName", tableName).put("connectionName", connection); |
|
JSONArray tables = JSONArray.create().put(table); |
|
JSONObject tablesBean = JSONObject.create().put("tables", tables); |
|
try{ |
|
UserAuthority authorityUser = UpgradeHandler.getFineUserInfoService().getUserAuthority(req); |
|
String userId = authorityUser.getUser().getInfo().getUserId(); |
|
String packId = "__my_analysis__" + userId + 1; |
|
|
|
try { |
|
String tableString = tablesBean.toString(); |
|
FineBusinessTable[] businessTables = UpgradeHandler.createBusinessTablesFromDBNewAddTableInfo(mapper.readValue(tableString, DataBaseAddTableRequestBean.class), userId); |
|
UpgradeHandler.getTableService().addNewTable(packId, authorityUser, businessTables); |
|
} catch (JsonMappingException e) { |
|
e.printStackTrace(); |
|
} catch (JsonGenerationException e) { |
|
e.printStackTrace(); |
|
} catch (IOException e) { |
|
e.printStackTrace(); |
|
} |
|
} catch(Exception e){ |
|
e.printStackTrace(); |
|
} |
|
} |
|
|
|
WebUtils.printAsJSON(res, dmlResult); |
|
} |
|
|
|
|
|
@Override |
|
public String getPath() { |
|
return PATH; |
|
} |
|
}
|
|
|