添加导入excel基本界面

This commit is contained in:
datagear 2019-07-28 20:41:38 +08:00
parent 9cd34594f0
commit bfbaaa38e8
5 changed files with 339 additions and 0 deletions

View File

@ -44,6 +44,7 @@ import org.datagear.dataexchange.DataExchangeService;
import org.datagear.dataexchange.DataFormat;
import org.datagear.dataexchange.DataImportOption;
import org.datagear.dataexchange.DataSourceConnectionFactory;
import org.datagear.dataexchange.FileInputStreamResourceFactory;
import org.datagear.dataexchange.FileOutputStreamResourceFactory;
import org.datagear.dataexchange.FileReaderResourceFactory;
import org.datagear.dataexchange.FileWriterResourceFactory;
@ -58,6 +59,7 @@ import org.datagear.dataexchange.ValueDataImportOption;
import org.datagear.dataexchange.support.CsvDataExport;
import org.datagear.dataexchange.support.CsvDataImport;
import org.datagear.dataexchange.support.ExcelDataExport;
import org.datagear.dataexchange.support.ExcelDataImport;
import org.datagear.dataexchange.support.SqlDataExport;
import org.datagear.dataexchange.support.SqlDataImport;
import org.datagear.dbinfo.DatabaseInfoResolver;
@ -418,6 +420,125 @@ public class DataExchangeController extends AbstractSchemaConnController
return buildOperationMessageSuccessEmptyResponseEntity();
}
@RequestMapping("/{schemaId}/import/excel")
public String imptExcel(HttpServletRequest request, HttpServletResponse response,
org.springframework.ui.Model springModel, @PathVariable("schemaId") String schemaId) throws Throwable
{
new VoidSchemaConnExecutor(request, response, springModel, schemaId, true)
{
@Override
protected void execute(HttpServletRequest request, HttpServletResponse response, Model springModel,
Schema schema) throws Throwable
{
}
}.execute();
DataFormat defaultDataFormat = new DataFormat();
String dataExchangeId = IDUtil.uuid();
springModel.addAttribute("defaultDataFormat", defaultDataFormat);
springModel.addAttribute("dataExchangeId", dataExchangeId);
springModel.addAttribute("dataExchangeChannelId", getDataExchangeChannelId(dataExchangeId));
return "/dataexchange/import_excel";
}
@RequestMapping(value = "/{schemaId}/import/excel/uploadImportFile", produces = CONTENT_TYPE_JSON)
@ResponseBody
public List<DataImportFileInfo> imptExcelUploadFile(HttpServletRequest request, HttpServletResponse response,
@PathVariable("schemaId") String schemaId, @RequestParam("dataExchangeId") String dataExchangeId,
@RequestParam("file") MultipartFile multipartFile) throws Exception
{
return uploadImportFile(request, response, schemaId, dataExchangeId, multipartFile, new ExcelFileFilger());
}
@RequestMapping(value = "/{schemaId}/import/excel/doImport", produces = CONTENT_TYPE_JSON)
@ResponseBody
public ResponseEntity<OperationMessage> imptExcelDoImport(HttpServletRequest request, HttpServletResponse response,
org.springframework.ui.Model springModel, @PathVariable("schemaId") String schemaId,
@RequestParam("dataExchangeId") String dataExchangeId, TextValueFileBatchDataImportForm dataImportForm,
@RequestParam("dependentNumberAuto") final String dependentNumberAuto) throws Throwable
{
if (dataImportForm == null || isEmpty(dataImportForm.getSubDataExchangeIds())
|| isEmpty(dataImportForm.getFileNames()) || isEmpty(dataImportForm.getNumbers())
|| isEmpty(dataImportForm.getDependentNumbers()) || isEmpty(dataImportForm.getImportOption())
|| isEmpty(dataImportForm.getDataFormat()) || isEmpty(dataImportForm.getTableNames())
|| dataImportForm.getSubDataExchangeIds().length != dataImportForm.getFileNames().length
|| dataImportForm.getSubDataExchangeIds().length != dataImportForm.getNumbers().length
|| dataImportForm.getSubDataExchangeIds().length != dataImportForm.getDependentNumbers().length
|| dataImportForm.getSubDataExchangeIds().length != dataImportForm.getTableNames().length)
throw new IllegalInputException();
String[] subDataExchangeIds = dataImportForm.getSubDataExchangeIds();
final String[] numbers = dataImportForm.getNumbers();
final String[] dependentNumbers = dataImportForm.getDependentNumbers();
String[] fileNames = dataImportForm.getFileNames();
final String[] tableNames = dataImportForm.getTableNames();
File directory = getTempDataExchangeDirectory(dataExchangeId, true);
File logDirectory = getTempDataExchangeLogDirectory(dataExchangeId, true);
Schema schema = getSchemaNotNull(request, response, schemaId);
ConnectionFactory connectionFactory = new DataSourceConnectionFactory(new SchemaDataSource(schema));
String importChannelId = getDataExchangeChannelId(dataExchangeId);
ServerChannel importServerChannel = this.dataExchangeCometdService.getChannelWithCreation(importChannelId);
Locale locale = getLocale(request);
SubDataExchange[] subDataExchanges = new SubDataExchange[subDataExchangeIds.length];
for (int i = 0; i < subDataExchangeIds.length; i++)
{
File file = FileUtil.getFile(directory, fileNames[i]);
ResourceFactory<InputStream> readerFactory = FileInputStreamResourceFactory.valueOf(file);
ExcelDataImport excelDataImport = new ExcelDataImport(connectionFactory, dataImportForm.getDataFormat(),
dataImportForm.getImportOption(), readerFactory);
excelDataImport.setUnifiedTable(tableNames[i]);
CometdSubTextValueDataImportListener listener = new CometdSubTextValueDataImportListener(
this.dataExchangeCometdService, importServerChannel, getMessageSource(), locale,
subDataExchangeIds[i], excelDataImport.getImportOption().getExceptionResolve());
listener.setLogFile(getTempSubDataExchangeLogFile(logDirectory, subDataExchangeIds[i]));
listener.setSendExchangingMessageInterval(
evalSendDataExchangingMessageInterval(subDataExchangeIds.length, excelDataImport));
excelDataImport.setListener(listener);
SubDataExchange subDataExchange = new SubDataExchange(subDataExchangeIds[i], numbers[i], excelDataImport);
subDataExchanges[i] = subDataExchange;
}
new VoidSchemaConnExecutor(request, response, springModel, schemaId, true)
{
@Override
protected void execute(HttpServletRequest request, HttpServletResponse response, Model springModel,
Schema schema) throws Throwable
{
Connection cn = getConnection();
inflateDependentNumbers(cn, numbers, tableNames, dependentNumbers, dependentNumberAuto);
}
}.execute();
resolveSubDataExchangeDependencies(subDataExchanges, numbers, dependentNumbers, "");
Set<SubDataExchange> subDataExchangeSet = new HashSet<SubDataExchange>(subDataExchangeIds.length);
Collections.addAll(subDataExchangeSet, subDataExchanges);
BatchDataExchange batchDataExchange = buildBatchDataExchange(connectionFactory, subDataExchangeSet,
importServerChannel, locale);
this.dataExchangeService.exchange(batchDataExchange);
BatchDataExchangeInfo batchDataExchangeInfo = new BatchDataExchangeInfo(dataExchangeId, batchDataExchange);
storeBatchDataExchangeInfo(request, batchDataExchangeInfo);
return buildOperationMessageSuccessEmptyResponseEntity();
}
@RequestMapping("/{schemaId}/import/db")
public String imptDb(HttpServletRequest request, HttpServletResponse response,
org.springframework.ui.Model springModel, @PathVariable("schemaId") String schemaId) throws Throwable
@ -1387,6 +1508,24 @@ public class DataExchangeController extends AbstractSchemaConnController
}
}
protected static class ExcelFileFilger implements FileFilter
{
public ExcelFileFilger()
{
super();
}
@Override
public boolean accept(File file)
{
if (file.isDirectory())
return true;
String fileName = file.getName().toLowerCase();
return (fileName.endsWith(".xlsx") || fileName.endsWith(".xls"));
}
}
public static class AbstractFileBatchDataExchangeForm implements Serializable
{
private static final long serialVersionUID = 1L;

View File

@ -411,6 +411,9 @@
<bean class="org.datagear.dataexchange.support.SqlDataExportService">
<property name="databaseInfoResolver" ref="databaseInfoResolver" />
</bean>
<bean class="org.datagear.dataexchange.support.ExcelDataImportService">
<property name="databaseInfoResolver" ref="databaseInfoResolver" />
</bean>
<bean class="org.datagear.dataexchange.support.ExcelDataExportService">
<property name="databaseInfoResolver" ref="databaseInfoResolver" />
</bean>

View File

@ -454,10 +454,13 @@ dataImport.dataType.csv=CSV
dataImport.dataType.csv.desc=\u4ECE\u9017\u53F7\u5206\u9694\u503C\u6587\u4EF6\u4E2D\u5BFC\u5165\u6570\u636E
dataImport.dataType.sql=SQL
dataImport.dataType.sql.desc=\u4ECESQL\u8BED\u53E5\u5BFC\u5165\u6570\u636E
dataImport.dataType.excel=Excel
dataImport.dataType.excel.desc=\u4ECEExcel\u5BFC\u5165\u6570\u636E
dataImport.dataType.db=\u6570\u636E\u5E93
dataImport.dataType.db.desc=\u4ECE\u5176\u4ED6\u6570\u636E\u5E93\u4E2D\u5BFC\u5165\u6570\u636E
dataImport.importCsvData=\u5BFC\u5165CSV\u6570\u636E
dataImport.importSqlData=\u5BFC\u5165SQL\u6570\u636E
dataImport.importExcelData=\u5BFC\u5165Excel\u6570\u636E
dataImport.setDataFormat=\u8BBE\u7F6E
dataImport.ignoreInexistentColumn=\u5FFD\u7565\u4E0D\u5B58\u5728\u5B57\u6BB5
dataImport.nullForIllegalColumnValue=\u5217\u503C\u975E\u6CD5\u65F6\u8BBE\u7F6E\u4E3ANULL
@ -466,6 +469,8 @@ dataImport.uploadCsvDataFile=\u6DFB\u52A0CSV\u6570\u636E\u6587\u4EF6
dataImport.uploadCsvDataFile.desc=*.csv\u3001*.zip
dataImport.uploadSqlDataFile=\u6DFB\u52A0SQL\u6570\u636E\u6587\u4EF6
dataImport.uploadSqlDataFile.desc=*.sql\u3001*.zip
dataImport.uploadExcelDataFile=\u6DFB\u52A0Excel\u6570\u636E\u6587\u4EF6
dataImport.uploadExcelDataFile.desc=*.xlsx\u3001*.xls\u3001*.zip
dataImport.importFileEncoding=\u5BFC\u5165\u6587\u4EF6\u7F16\u7801
dataImport.importFileName=\u6587\u4EF6\u540D\u79F0
dataImport.importFileSize=\u6587\u4EF6\u5927\u5C0F

View File

@ -47,6 +47,16 @@ Schema schema 数据库不允许为null
</div>
</div>
</div>
<div class="form-item">
<div class="form-item-label">&nbsp;</div>
<div class="form-item-value">
<label for="${pageId}-dataType-2"><@spring.message code='dataImport.dataType.excel' /></label>
<input id="${pageId}-dataType-2" type="radio" name="dataType" value="excel" />
<div class="input-desc minor">
<@spring.message code='dataImport.dataType.excel.desc' />
</div>
</div>
</div>
<!--
<div class="form-item">
<div class="form-item-label">&nbsp;</div>

View File

@ -0,0 +1,182 @@
<#include "../include/import_global.ftl">
<#include "../include/html_doctype.ftl">
<#--
Schema schema 数据库不允许为null
-->
<html>
<head>
<#include "../include/html_head.ftl">
<title>
<#include "../include/html_title_app_name.ftl">
<@spring.message code='dataImport.dataImport' />
<@spring.message code='bracketLeft' />
${schema.title?html}
<@spring.message code='bracketRight' />
</title>
</head>
<body class="fill-parent">
<#if !isAjaxRequest>
<div class="fill-parent">
</#if>
<div id="${pageId}" class="page-dataexchange page-dataimport-text page-dataimport-excel">
<div class="head">
<@spring.message code='dataImport.importExcelData' />
</div>
<div class="content">
<form id="${pageId}-form" action="${contextPath}/dataexchange/${schema.id}/import/excel/doImport" method="POST">
<input type="hidden" name="dataExchangeId" value="${dataExchangeId}" />
<input type="hidden" name="dependentNumberAuto" value="<@spring.message code='dataImport.dependentNumber.auto' />" />
<div class="form-content form-content-wizard">
<h3><@spring.message code='dataImport.setDataFormat' /></h3>
<div>
<#include "include/dataExchange_form_dataFormat_html.ftl">
<div class="form-item">
<div class="form-item-label"><@spring.message code='dataImport.ignoreInexistentColumn' /></div>
<div class="form-item-value">
<div id="${pageId}-ignoreInexistentColumn">
<label for="${pageId}-ignoreInexistentColumn-0"><@spring.message code='yes' /></label>
<input id="${pageId}-ignoreInexistentColumn-0" type="radio" name="importOption.ignoreInexistentColumn" value="true" />
<label for="${pageId}-ignoreInexistentColumn-1"><@spring.message code='no' /></label>
<input id="${pageId}-ignoreInexistentColumn-1" type="radio" name="importOption.ignoreInexistentColumn" value="false" />
</div>
</div>
</div>
<div class="form-item">
<div class="form-item-label"><@spring.message code='dataImport.nullForIllegalColumnValue' /></div>
<div class="form-item-value">
<div id="${pageId}-nullForIllegalColumnValue">
<label for="${pageId}-nullForIllegalColumnValue-0"><@spring.message code='yes' /></label>
<input id="${pageId}-nullForIllegalColumnValue-0" type="radio" name="importOption.nullForIllegalColumnValue" value="true" />
<label for="${pageId}-nullForIllegalColumnValue-1"><@spring.message code='no' /></label>
<input id="${pageId}-nullForIllegalColumnValue-1" type="radio" name="importOption.nullForIllegalColumnValue" value="false" />
</div>
</div>
</div>
<div class="form-item">
<div class="form-item-label"><@spring.message code='dataExchange.exceptionResolve' /></div>
<div class="form-item-value">
<div id="${pageId}-exceptionResolve">
<label for="${pageId}-exceptionResolve-0"><@spring.message code='dataExchange.exceptionResolve.ROLLBACK' /></label>
<input id="${pageId}-exceptionResolve-0" type="radio" name="importOption.exceptionResolve" value="ROLLBACK" />
<label for="${pageId}-exceptionResolve-1"><@spring.message code='dataExchange.exceptionResolve.ABORT' /></label>
<input id="${pageId}-exceptionResolve-1" type="radio" name="importOption.exceptionResolve" value="ABORT" />
<label for="${pageId}-exceptionResolve-2"><@spring.message code='dataExchange.exceptionResolve.IGNORE' /></label>
<input id="${pageId}-exceptionResolve-2" type="radio" name="importOption.exceptionResolve" value="IGNORE" />
</div>
</div>
</div>
</div>
<h3><@spring.message code='dataImport.uploadAndImportData' /></h3>
<div>
<div class="form-item form-item-table-head form-item-upload page-status-aware-show edit-status-show">
<div class="form-item-value">
<label><@spring.message code='dataImport.uploadExcelDataFile' /></label>
<div class="fileinput-button ui-widget ui-button ui-corner-all" upload-action="excel/uploadImportFile" title="<@spring.message code='dataImport.uploadExcelDataFile.desc' />"><@spring.message code='add' /><input type="file"></div>
<div class="file-info"></div>
</div>
</div>
<div class="form-item form-item-table-head form-item-progress page-status-aware-show exchange-status-show finish-status-show">
<div class="form-item-value">
<label><@spring.message code='dataImport.importProgress' /></label>
<div id="${pageId}-progress"></div>
<div id="${pageId}-progress-percent" class="progress-percent"></div>
</div>
</div>
<div class="form-item form-item-table">
<div class="table-operation-wrapper">
<button type="button" class="table-delete-item-button page-status-aware-show edit-status-show"><@spring.message code='delete' /></button>
<button type="button" class="table-cancel-import-button page-status-aware-show exchange-status-show"><@spring.message code='cancel' /></button>
</div>
<#--
<div class="file-encoding-wrapper">
<span class="file-encoding-label page-status-aware-enable edit-status-enable">
<@spring.message code='dataImport.importFileEncoding' />
</span>
<select name="fileEncoding">
<#list availableCharsetNames as item>
<option value="${item}" <#if item == defaultCharsetName>selected="selected"</#if>>${item}</option>
</#list>
</select>
</div>
-->
<div class="table-wrapper minor-dataTable">
<table id="${pageId}-table" width="100%" class="hover stripe"></table>
</div>
</div>
</div>
</div>
</form>
<div class="return-wrapper page-status-aware-show edit-status-show finish-status-show">
<button type="button" class="return-button" return-url="${contextPath}/dataexchange/${schema.id}/import">
<@spring.message code='return' />
</button>
</div>
<div class="restart-wrapper page-status-aware-show finish-status-show">
<button type="button" class="restart-button"><@spring.message code='restart' /></button>
</div>
<div id="${pageId}-exchange-exception-tooltip" title="import tooltip" style="width:0; height:0;"></div>
</div>
<div class="foot">
</div>
</div>
<#if !isAjaxRequest>
</div>
</#if>
<#include "../include/page_js_obj.ftl">
<#include "../include/page_obj_grid.ftl">
<#include "../include/page_obj_cometd.ftl">
<#include "../include/page_obj_format_time.ftl" >
<#include "include/dataExchange_js.ftl" >
<#include "include/dataImport_js.ftl" >
<script type="text/javascript">
(function(po)
{
po.postBuildSubDataExchange = function(subDataExchange)
{
subDataExchange.dependentNumber = "<@spring.message code='dataImport.dependentNumber.auto' />";
};
po.initDataImportExcelUIs = function()
{
po.element("#${pageId}-ignoreInexistentColumn").buttonset();
po.element("#${pageId}-nullForIllegalColumnValue").buttonset();
po.element("#${pageId}-ignoreInexistentColumn-1").click();
po.element("#${pageId}-nullForIllegalColumnValue-1").click();
};
po.dataImportTableColumns.splice(3, 0,
{
title : "<@spring.message code='dataImport.importTableName' />",
data : "tableName",
render : function(data, type, row, meta)
{
return "<input type='text' name='tableNames' value='"+$.escapeHtml(data)+"' class='table-name-input ui-widget ui-widget-content' style='width:90%' />";
},
defaultContent: "",
width : "20%"
});
po.dataImportTableColumns[1].width = "25%";
po.cometdInitIfNot();
po.initDataImportSteps();
po.initDataExchangeUIs();
po.initDataImportUIs();
po.initDataImportExcelUIs();
po.initDataImportDataTable();
po.initDataExchangeActions();
po.initDataImportActions();
po.table().on("click", ".table-name-input", function(event)
{
//阻止行选中
event.stopPropagation();
});
po.updateDataExchangePageStatus("edit");
})
(${pageId});
</script>
</body>
</html>