From bfbaaa38e881c61250cbf4fbf7f2c79f8610db1a Mon Sep 17 00:00:00 2001 From: datagear Date: Sun, 28 Jul 2019 20:41:38 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=AF=BC=E5=85=A5excel?= =?UTF-8?q?=E5=9F=BA=E6=9C=AC=E7=95=8C=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/DataExchangeController.java | 139 +++++++++++++ .../resources/datagear-applicationContext.xml | 3 + .../resources/locales/datagear.properties | 5 + .../WEB-INF/view/dataexchange/import.ftl | 10 + .../view/dataexchange/import_excel.ftl | 182 ++++++++++++++++++ 5 files changed, 339 insertions(+) create mode 100644 datagear-web/src/main/webapp/WEB-INF/view/dataexchange/import_excel.ftl diff --git a/datagear-web/src/main/java/org/datagear/web/controller/DataExchangeController.java b/datagear-web/src/main/java/org/datagear/web/controller/DataExchangeController.java index 5d17a8ff..82818c40 100644 --- a/datagear-web/src/main/java/org/datagear/web/controller/DataExchangeController.java +++ b/datagear-web/src/main/java/org/datagear/web/controller/DataExchangeController.java @@ -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 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 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 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 subDataExchangeSet = new HashSet(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; diff --git a/datagear-web/src/main/resources/datagear-applicationContext.xml b/datagear-web/src/main/resources/datagear-applicationContext.xml index a33720a3..29086c30 100644 --- a/datagear-web/src/main/resources/datagear-applicationContext.xml +++ b/datagear-web/src/main/resources/datagear-applicationContext.xml @@ -411,6 +411,9 @@ + + + diff --git a/datagear-web/src/main/resources/locales/datagear.properties b/datagear-web/src/main/resources/locales/datagear.properties index 02da3490..751a95fd 100644 --- a/datagear-web/src/main/resources/locales/datagear.properties +++ b/datagear-web/src/main/resources/locales/datagear.properties @@ -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 diff --git a/datagear-web/src/main/webapp/WEB-INF/view/dataexchange/import.ftl b/datagear-web/src/main/webapp/WEB-INF/view/dataexchange/import.ftl index dd8e7fbc..db4f683f 100644 --- a/datagear-web/src/main/webapp/WEB-INF/view/dataexchange/import.ftl +++ b/datagear-web/src/main/webapp/WEB-INF/view/dataexchange/import.ftl @@ -47,6 +47,16 @@ Schema schema 数据库,不允许为null +
+
 
+
+ + +
+ <@spring.message code='dataImport.dataType.excel.desc' /> +
+
+
+ + +<#include "../include/html_head.ftl"> + + <#include "../include/html_title_app_name.ftl"> + <@spring.message code='dataImport.dataImport' /> + <@spring.message code='bracketLeft' /> + ${schema.title?html} + <@spring.message code='bracketRight' /> + + + +<#if !isAjaxRequest> +
+ +
+
+ <@spring.message code='dataImport.importExcelData' /> +
+
+
+ + +
+

<@spring.message code='dataImport.setDataFormat' />

+
+ <#include "include/dataExchange_form_dataFormat_html.ftl"> +
+
<@spring.message code='dataImport.ignoreInexistentColumn' />
+
+
+ + + + +
+
+
+
+
<@spring.message code='dataImport.nullForIllegalColumnValue' />
+
+
+ + + + +
+
+
+
+
<@spring.message code='dataExchange.exceptionResolve' />
+
+
+ + + + + + +
+
+
+
+

<@spring.message code='dataImport.uploadAndImportData' />

+
+
+
+ +
<@spring.message code='add' />
+
+
+
+
+
+ +
+
+
+
+
+
+ + +
+ <#-- +
+ + <@spring.message code='dataImport.importFileEncoding' /> + + +
+ --> +
+
+
+
+
+
+
+
+ +
+
+ +
+
+
+
+
+
+<#if !isAjaxRequest> +
+ + +<#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" > + + +