From 5fc31b13f4a7d84dc48ffe48540db74ae426f257 Mon Sep 17 00:00:00 2001 From: datagear Date: Sat, 11 Jan 2020 16:23:13 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=9C=8B=E6=9D=BF=E5=AF=BC?= =?UTF-8?q?=E5=85=A5=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../analysis/TemplateDashboardWidget.java | 11 +- .../support/DashboardWidgetResManager.java | 14 ++ .../html/HtmlTplDashboardWidgetRenderer.java | 4 +- .../org/datagear/management/ddl/datagear.sql | 1 + .../HtmlTplDashboardWidgetEntityMapper.xml | 8 +- .../web/controller/ChartPluginController.java | 1 + .../web/controller/DashboardController.java | 159 +++++++++++++++++- .../resources/locales/datagear.properties | 6 + .../analysis/dashboard/dashboard_form.ftl | 2 + .../analysis/dashboard/dashboard_grid.ftl | 15 ++ .../analysis/dashboard/dashboard_import.ftl | 137 +++++++++++++++ 11 files changed, 341 insertions(+), 17 deletions(-) create mode 100644 datagear-web/src/main/webapp/WEB-INF/view/analysis/dashboard/dashboard_import.ftl diff --git a/datagear-analysis/src/main/java/org/datagear/analysis/TemplateDashboardWidget.java b/datagear-analysis/src/main/java/org/datagear/analysis/TemplateDashboardWidget.java index fefc5b4b..77cc657d 100644 --- a/datagear-analysis/src/main/java/org/datagear/analysis/TemplateDashboardWidget.java +++ b/datagear-analysis/src/main/java/org/datagear/analysis/TemplateDashboardWidget.java @@ -3,8 +3,6 @@ */ package org.datagear.analysis; -import org.datagear.util.StringUtil; - /** * 模板看板部件。 *

@@ -17,9 +15,11 @@ import org.datagear.util.StringUtil; public abstract class TemplateDashboardWidget extends AbstractIdentifiable implements DashboardWidget { + public static final String DEFAULT_TEMPLATE_ENCODING = "UTF-8"; + private String template; - private String templateEncoding = "UTF-8"; + private String templateEncoding = DEFAULT_TEMPLATE_ENCODING; public TemplateDashboardWidget() { @@ -42,11 +42,6 @@ public abstract class TemplateDashboardWidget extends A this.template = template; } - public boolean hasTemplateEncoding() - { - return !StringUtil.isEmpty(this.templateEncoding); - } - public String getTemplateEncoding() { return templateEncoding; diff --git a/datagear-analysis/src/main/java/org/datagear/analysis/support/DashboardWidgetResManager.java b/datagear-analysis/src/main/java/org/datagear/analysis/support/DashboardWidgetResManager.java index 8176cacc..f3686943 100644 --- a/datagear-analysis/src/main/java/org/datagear/analysis/support/DashboardWidgetResManager.java +++ b/datagear-analysis/src/main/java/org/datagear/analysis/support/DashboardWidgetResManager.java @@ -112,6 +112,20 @@ public class DashboardWidgetResManager } } + /** + * 获取指定看板部件的主目录。 + *

+ * 如果目录不存在,则会自动创建。 + *

+ * + * @param id + * @return + */ + public File getDirectory(String id) + { + return FileUtil.getDirectory(this.rootDirectory, id, true); + } + /** * 获取目录。 *

diff --git a/datagear-analysis/src/main/java/org/datagear/analysis/support/html/HtmlTplDashboardWidgetRenderer.java b/datagear-analysis/src/main/java/org/datagear/analysis/support/html/HtmlTplDashboardWidgetRenderer.java index 1a6af2b2..f4d69c28 100644 --- a/datagear-analysis/src/main/java/org/datagear/analysis/support/html/HtmlTplDashboardWidgetRenderer.java +++ b/datagear-analysis/src/main/java/org/datagear/analysis/support/html/HtmlTplDashboardWidgetRenderer.java @@ -391,7 +391,7 @@ public abstract class HtmlTplDashboardWidgetRenderer INSERT INTO DATAGEAR_HTML_DASHBOARD ( - HD_ID, HD_NAME, HD_TEMPLATE, HD_CREATE_USER_ID, + HD_ID, HD_NAME, HD_TEMPLATE, HD_TEMPLATE_ENCODING, HD_CREATE_USER_ID, HD_CREATE_TIME ) VALUES ( - #{entity.id}, #{entity.name}, #{entity.template}, #{entity.createUser.id}, + #{entity.id}, #{entity.name}, #{entity.template}, #{entity.templateEncoding}, #{entity.createUser.id}, #{entity.createTime} ) @@ -19,7 +19,8 @@ UPDATE DATAGEAR_HTML_DASHBOARD SET HD_NAME = #{entity.name}, - HD_TEMPLATE = #{entity.template} + HD_TEMPLATE = #{entity.template}, + HD_TEMPLATE_ENCODING = #{entity.templateEncoding} WHERE HD_ID = #{entity.id} @@ -136,6 +137,7 @@ A.HD_ID AS ${_iq_}id${_iq_}, A.HD_NAME AS ${_iq_}name${_iq_}, A.HD_TEMPLATE AS ${_iq_}template${_iq_}, + A.HD_TEMPLATE_ENCODING AS ${_iq_}templateEncoding${_iq_}, , A.HD_CREATE_TIME AS ${_iq_}createTime${_iq_} FROM diff --git a/datagear-web/src/main/java/org/datagear/web/controller/ChartPluginController.java b/datagear-web/src/main/java/org/datagear/web/controller/ChartPluginController.java index aefe7cfb..dfa35a48 100644 --- a/datagear-web/src/main/java/org/datagear/web/controller/ChartPluginController.java +++ b/datagear-web/src/main/java/org/datagear/web/controller/ChartPluginController.java @@ -67,6 +67,7 @@ public class ChartPluginController extends AbstractChartPluginAwareController DirectoryHtmlChartPluginManager directoryHtmlChartPluginManager, File tempDirectory) { super(messageSource, classDataConverter, directoryHtmlChartPluginManager); + this.tempDirectory = tempDirectory; } public File getTempDirectory() diff --git a/datagear-web/src/main/java/org/datagear/web/controller/DashboardController.java b/datagear-web/src/main/java/org/datagear/web/controller/DashboardController.java index 1a02a325..913830e6 100644 --- a/datagear-web/src/main/java/org/datagear/web/controller/DashboardController.java +++ b/datagear-web/src/main/java/org/datagear/web/controller/DashboardController.java @@ -6,12 +6,15 @@ package org.datagear.web.controller; import java.io.File; import java.io.IOException; +import java.io.InputStream; import java.io.OutputStream; import java.io.Serializable; import java.io.Writer; +import java.nio.charset.Charset; import java.util.Arrays; import java.util.HashMap; import java.util.Map; +import java.util.zip.ZipInputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -31,6 +34,7 @@ import org.datagear.management.domain.User; import org.datagear.management.service.HtmlTplDashboardWidgetEntityService; import org.datagear.persistence.PagingData; import org.datagear.persistence.PagingQuery; +import org.datagear.util.FileUtil; import org.datagear.util.IDUtil; import org.datagear.util.IOUtil; import org.datagear.util.StringUtil; @@ -44,6 +48,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.context.request.WebRequest; +import org.springframework.web.multipart.MultipartFile; /** * 看板控制器。 @@ -58,15 +63,20 @@ public class DashboardController extends AbstractDataAnalysisController @Autowired private HtmlTplDashboardWidgetEntityService htmlTplDashboardWidgetEntityService; + @Autowired + private File tempDirectory; + public DashboardController() { super(); } - public DashboardController(HtmlTplDashboardWidgetEntityService htmlTplDashboardWidgetEntityService) + public DashboardController(HtmlTplDashboardWidgetEntityService htmlTplDashboardWidgetEntityService, + File tempDirectory) { super(); this.htmlTplDashboardWidgetEntityService = htmlTplDashboardWidgetEntityService; + this.tempDirectory = tempDirectory; } public HtmlTplDashboardWidgetEntityService getHtmlTplDashboardWidgetEntityService() @@ -80,12 +90,29 @@ public class DashboardController extends AbstractDataAnalysisController this.htmlTplDashboardWidgetEntityService = htmlTplDashboardWidgetEntityService; } + public File getTempDirectory() + { + return tempDirectory; + } + + public void setTempDirectory(File tempDirectory) + { + this.tempDirectory = tempDirectory; + } + @RequestMapping("/add") public String add(HttpServletRequest request, org.springframework.ui.Model model) { HtmlTplDashboardWidgetEntity dashboard = new HtmlTplDashboardWidgetEntity(); + dashboard.setTemplate(HtmlTplDashboardWidgetEntity.DEFAULT_TEMPLATE); + dashboard.setTemplateEncoding(HtmlTplDashboardWidget.DEFAULT_TEMPLATE_ENCODING); + + String templateContent = "\n\n\n\n\n\n\n"; + model.addAttribute("dashboard", dashboard); + model.addAttribute("templateContent", templateContent); model.addAttribute(KEY_TITLE_MESSAGE_KEY, "dashboard.addDashboard"); model.addAttribute(KEY_FORM_ACTION, "saveAdd"); @@ -100,8 +127,6 @@ public class DashboardController extends AbstractDataAnalysisController { User user = WebUtils.getUser(request, response); - dashboard.setTemplate(HtmlTplDashboardWidgetEntity.DEFAULT_TEMPLATE); - checkSaveEntity(dashboard); dashboard.setId(IDUtil.uuid()); @@ -142,7 +167,8 @@ public class DashboardController extends AbstractDataAnalysisController { User user = WebUtils.getUser(request, response); - dashboard.setTemplate(HtmlTplDashboardWidgetEntity.DEFAULT_TEMPLATE); + if (isEmpty(dashboard.getTemplate())) + dashboard.setTemplate(HtmlTplDashboardWidgetEntity.DEFAULT_TEMPLATE); checkSaveEntity(dashboard); @@ -154,6 +180,124 @@ public class DashboardController extends AbstractDataAnalysisController return buildOperationMessageSaveSuccessResponseEntity(request); } + @RequestMapping("/import") + public String impt(HttpServletRequest request, org.springframework.ui.Model model) + { + return "/analysis/dashboard/dashboard_import"; + } + + @RequestMapping(value = "/uploadFile", produces = CONTENT_TYPE_JSON) + @ResponseBody + public Map uploadFile(HttpServletRequest request, HttpServletResponse response, + @RequestParam("file") MultipartFile multipartFile) throws Exception + { + String dashboardFileName = ""; + String template = ""; + String templateEncoding = ""; + + File tmpDirectory = FileUtil.generateUniqueDirectory(this.tempDirectory); + + dashboardFileName = tmpDirectory.getName(); + + String fileName = multipartFile.getOriginalFilename(); + + if (FileUtil.isExtension(fileName, "zip")) + { + ZipInputStream in = IOUtil.getZipInputStream(multipartFile.getInputStream()); + try + { + IOUtil.unzip(in, tmpDirectory); + } + finally + { + IOUtil.close(in); + } + + File[] files = tmpDirectory.listFiles(); + if (files != null) + { + for (File file : files) + { + if (file.isDirectory()) + continue; + + String name = file.getName(); + if (FileUtil.isExtension(name, "html") || FileUtil.isExtension(name, "htm")) + { + template = name; + + if (template.equalsIgnoreCase("index.html") || template.equalsIgnoreCase("index.htm")) + break; + } + } + } + } + else + { + File file = FileUtil.getFile(tmpDirectory, fileName); + + InputStream in = null; + OutputStream out = null; + try + { + in = multipartFile.getInputStream(); + out = IOUtil.getOutputStream(file); + IOUtil.write(in, out); + } + finally + { + IOUtil.close(in); + IOUtil.close(out); + } + + template = fileName; + } + + Map results = new HashMap(); + results.put("dashboardFileName", dashboardFileName); + results.put("template", template); + results.put("templateEncoding", templateEncoding); + + return results; + } + + @RequestMapping(value = "/saveImport", produces = CONTENT_TYPE_JSON) + @ResponseBody + public ResponseEntity saveImport(HttpServletRequest request, HttpServletResponse response, + @RequestParam("name") String name, + @RequestParam("template") String template, @RequestParam("templateEncoding") String templateEncoding, + @RequestParam("dashboardFileName") String dashboardFileName) + throws Exception + { + File uploadDirectory = FileUtil.getDirectory(this.tempDirectory, dashboardFileName, false); + + if (!uploadDirectory.exists()) + throw new IllegalInputException(); + + User user = WebUtils.getUser(request, response); + + HtmlTplDashboardWidgetEntity dashboard = new HtmlTplDashboardWidgetEntity(); + dashboard.setTemplate(template); + dashboard.setTemplateEncoding(templateEncoding); + dashboard.setName(name); + + checkSaveEntity(dashboard); + + dashboard.setId(IDUtil.uuid()); + dashboard.setCreateUser(user); + + this.htmlTplDashboardWidgetEntityService.add(user, dashboard); + + DashboardWidgetResManager dashboardWidgetResManager = this.htmlTplDashboardWidgetEntityService + .getHtmlTplDashboardWidgetRenderer().getDashboardWidgetResManager(); + + File dashboardResDirectory = dashboardWidgetResManager.getDirectory(dashboard.getId()); + + IOUtil.copy(uploadDirectory, dashboardResDirectory, false); + + return buildOperationMessageSaveSuccessResponseEntity(request); + } + @RequestMapping("/view") public String view(HttpServletRequest request, HttpServletResponse response, org.springframework.ui.Model model, @RequestParam("id") String id) throws Exception @@ -242,6 +386,13 @@ public class DashboardController extends AbstractDataAnalysisController if (dashboardWidget == null) throw new RecordNotFoundException(); + String responseEncoding = dashboardWidget.getTemplateEncoding(); + + if (StringUtil.isEmpty(responseEncoding)) + responseEncoding = Charset.defaultCharset().name(); + + response.setCharacterEncoding(responseEncoding); + Writer out = response.getWriter(); DefaultHtmlRenderContext renderContext = new DefaultHtmlRenderContext(createWebContext(request), out); diff --git a/datagear-web/src/main/resources/locales/datagear.properties b/datagear-web/src/main/resources/locales/datagear.properties index 16567b51..f9830784 100644 --- a/datagear-web/src/main/resources/locales/datagear.properties +++ b/datagear-web/src/main/resources/locales/datagear.properties @@ -649,11 +649,17 @@ dashboard.addDashboard=\u6DFB\u52A0\u770B\u677F dashboard.editDashboard=\u7F16\u8F91\u770B\u677F dashboard.viewDashboard=\u67E5\u770B\u770B\u677F dashboard.selectDashboard=\u9009\u62E9\u770B\u677F +dashboard.importDashboard=\u5BFC\u5165\u770B\u677F dashboard.name=\u540D\u79F0 dashboard.template=HTML\u6A21\u677F +dashboard.templateEncoding=HTML\u6A21\u677F\u7F16\u7801 +dashboard.templateName=HTML\u6A21\u677F\u6587\u4EF6\u540D dashboard.createUser=\u521B\u5EFA\u7528\u6237 dashboard.createTime=\u521B\u5EFA\u65F6\u95F4 dashboard.show=\u5C55\u793A +dashboard.import.selectFile=\u9009\u62E9\u6587\u4EF6 +dashboard.import.desc=\u6587\u4EF6\u683C\u5F0F\u5E94\u4E3A\uFF1A*.html\u3001*.htm\u3001*.zip +dashboard.import.validation.importDashboardFileRequired=\u8BF7\u9009\u62E9\u770B\u677F\u6587\u4EF6 #Chart plugin chartPlugin.manageChartPlugin=\u7BA1\u7406\u56FE\u8868\u63D2\u4EF6 diff --git a/datagear-web/src/main/webapp/WEB-INF/view/analysis/dashboard/dashboard_form.ftl b/datagear-web/src/main/webapp/WEB-INF/view/analysis/dashboard/dashboard_form.ftl index f14db506..7b56377b 100644 --- a/datagear-web/src/main/webapp/WEB-INF/view/analysis/dashboard/dashboard_form.ftl +++ b/datagear-web/src/main/webapp/WEB-INF/view/analysis/dashboard/dashboard_form.ftl @@ -19,6 +19,8 @@ readonly 是否只读操作,允许为null

+ +
diff --git a/datagear-web/src/main/webapp/WEB-INF/view/analysis/dashboard/dashboard_grid.ftl b/datagear-web/src/main/webapp/WEB-INF/view/analysis/dashboard/dashboard_grid.ftl index bb24c106..d6ce7162 100644 --- a/datagear-web/src/main/webapp/WEB-INF/view/analysis/dashboard/dashboard_grid.ftl +++ b/datagear-web/src/main/webapp/WEB-INF/view/analysis/dashboard/dashboard_grid.ftl @@ -25,6 +25,7 @@ selectonly 是否选择操作,允许为null <#else> + @@ -74,6 +75,20 @@ selectonly 是否选择操作,允许为null }); }); + po.element("input[name=importButton]").click(function() + { + po.open(po.url("import"), + { + pageParam : + { + afterSave : function() + { + po.refresh(); + } + } + }); + }); + po.element("input[name=editButton]").click(function() { po.executeOnSelect(function(row) diff --git a/datagear-web/src/main/webapp/WEB-INF/view/analysis/dashboard/dashboard_import.ftl b/datagear-web/src/main/webapp/WEB-INF/view/analysis/dashboard/dashboard_import.ftl new file mode 100644 index 00000000..606f1efc --- /dev/null +++ b/datagear-web/src/main/webapp/WEB-INF/view/analysis/dashboard/dashboard_import.ftl @@ -0,0 +1,137 @@ +<#include "../../include/import_global.ftl"> +<#include "../../include/html_doctype.ftl"> + + +<#include "../../include/html_head.ftl"> +<#include "../../include/html_title_app_name.ftl"><@spring.message code='dashboard.importDashboard' /> + + +
+
+
+
+ +
+
+ +
+
+ +
+ <@spring.message code='select' /> +
+
+
+
+
+
+ +
+
+ +
+
+
+
+ +
+
+ +
+
+
+
+ +
+
+ +
+
+
+
+ +
+
+
+<#include "../../include/page_js_obj.ftl" > +<#include "../../include/page_obj_form.ftl"> + + + \ No newline at end of file