From 2f814b1ce6f4d753957d01eef5ded864cc8c6e19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yvan-=E6=AC=A7=E9=98=B3=E5=B8=86?= Date: Wed, 22 Mar 2023 12:07:59 +0800 Subject: [PATCH 1/3] =?UTF-8?q?REPORT-91503=20=E4=BF=9D=E5=AD=98=E6=A8=A1?= =?UTF-8?q?=E6=9D=BF=E9=A2=91=E7=B9=81=E6=8A=A5=E9=94=99=E9=94=81=E5=AE=9A?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=E4=B8=8D=E4=B8=80=E8=87=B4=EF=BC=8C=E5=9C=A8?= =?UTF-8?q?=E8=BF=99=E4=B8=AA=E5=BC=B9=E7=AA=97=E5=8F=A6=E5=AD=98=E4=B8=BA?= =?UTF-8?q?=E4=B8=80=E5=AE=9A=E4=BC=9A=E5=A4=B1=E8=B4=A5=E3=80=82=20?= =?UTF-8?q?=E3=80=90=E9=97=AE=E9=A2=98=E5=8E=9F=E5=9B=A0=E3=80=91REPORT-70?= =?UTF-8?q?481=E8=BF=AD=E4=BB=A3=E5=BC=95=E8=B5=B7=EF=BC=8C=E5=8E=9F?= =?UTF-8?q?=E5=9B=A0=E6=98=AF=E5=9C=A8=E6=A8=A1=E6=9D=BF=E9=94=81=E5=AE=9A?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=E4=B8=8D=E4=B8=80=E8=87=B4=E7=9A=84=E6=97=B6?= =?UTF-8?q?=E5=80=99=EF=BC=8C=E5=8E=9F=E6=9C=AC=E7=9A=84=E9=80=BB=E8=BE=91?= =?UTF-8?q?=E6=98=AF=E5=B0=86=E5=BD=93=E5=89=8D=E6=A8=A1=E6=9D=BF=E4=BF=9D?= =?UTF-8?q?=E5=AD=98=E4=B8=BA=E5=A4=87=E4=BB=BD=E6=96=87=E4=BB=B6=20+=20?= =?UTF-8?q?=E5=85=B3=E9=97=AD=E5=BD=93=E5=89=8D=E6=A8=A1=E6=9D=BF=EF=BC=8C?= =?UTF-8?q?=E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84done=E6=96=B9=E6=B3=95=E4=B8=AD=EF=BC=8C=E5=8D=B3?= =?UTF-8?q?=E5=85=88=E4=BF=9D=E5=AD=98=E6=88=90=E5=8A=9F=EF=BC=8C=E5=86=8D?= =?UTF-8?q?=E5=85=B3=E9=97=AD=E6=A8=A1=E6=9D=BF=EF=BC=9B=E5=8F=A6=E5=A4=96?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BA=86=E4=B8=80=E4=B8=8B=E4=BF=9D=E5=AD=98?= =?UTF-8?q?=E7=9A=84=E4=BB=A3=E7=A0=81=EF=BC=8C=E4=BD=BF=E5=85=B6=E6=9B=B4?= =?UTF-8?q?=E7=AC=A6=E5=90=88=E5=BC=80=E5=8F=91=E8=A7=84=E8=8C=83=20?= =?UTF-8?q?=E3=80=90review=E5=BB=BA=E8=AE=AE=E3=80=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/fr/design/utils/TemplateUtils.java | 74 ++++++++++++------- .../worker/save/SaveFailureHandler.java | 8 +- 2 files changed, 53 insertions(+), 29 deletions(-) diff --git a/designer-base/src/main/java/com/fr/design/utils/TemplateUtils.java b/designer-base/src/main/java/com/fr/design/utils/TemplateUtils.java index 9fb1e632af..e82251f4b8 100644 --- a/designer-base/src/main/java/com/fr/design/utils/TemplateUtils.java +++ b/designer-base/src/main/java/com/fr/design/utils/TemplateUtils.java @@ -16,6 +16,7 @@ import com.fr.log.FineLoggerFactory; import com.fr.stable.ArrayUtils; import com.fr.stable.CoreConstants; import com.fr.stable.ProductConstants; +import com.fr.stable.StringUtils; import com.fr.workspace.WorkContext; import com.fr.workspace.server.lock.TplOperator; @@ -33,6 +34,7 @@ public class TemplateUtils { /** * 创建新的模板文件并打开模板 + * * @param prefix 模板文件名称前缀 * @param file 模板文件 * @param createByEditingTemplate 是否根据 当前编辑模板 来创建新模板 @@ -41,6 +43,21 @@ public class TemplateUtils { * @param openNewTemplate 是否需要在创建后打开模板 */ public static void createAndOpenTemplate(String prefix, FILE file, boolean createByEditingTemplate, boolean openNewTemplate) { + createAndOpenTemplate(prefix, file, createByEditingTemplate, openNewTemplate, () -> {}); + } + + /** + * 创建新的模板文件并打开模板,并在创建备份模板成功后执行doAfterCreate + * + * @param prefix 模板文件名称前缀 + * @param file 模板文件 + * @param createByEditingTemplate 是否根据 当前编辑模板 来创建新模板 + * 为true时以CurrentEditingTemplate为准创建新模板 + * 为false时以传入的File文件为准创建新模板,此文件可以不是编辑状态 + * @param openNewTemplate 是否需要在创建后打开模板 + * @param doAfterCreateTemplate 创建备份模板成功后调用 + */ + public static void createAndOpenTemplate(String prefix, FILE file, boolean createByEditingTemplate, boolean openNewTemplate, Runnable doAfterCreateTemplate) { String fileName = file.getName(); String oldPath = file.getPath(); int indexOfLastDot = fileName.lastIndexOf(CoreConstants.DOT); @@ -62,43 +79,25 @@ public class TemplateUtils { if (isOk(result)) { file = fileChooserPane.getSelectedFILE(); - _createAndOpenTemplate(file, oldPath, createByEditingTemplate, openNewTemplate); + createAndOpenTemplate0(file, oldPath, createByEditingTemplate, openNewTemplate, doAfterCreateTemplate); } } - private static void _createAndOpenTemplate(FILE file, String oldPath, boolean createByEditingTemplate, boolean openNewTemplate){ + private static void createAndOpenTemplate0(FILE file, String oldPath, boolean createByEditingTemplate, boolean openNewTemplate, Runnable doAfterCreateTemplate) { new SwingWorker() { @Override protected Void doInBackground() throws Exception { - byte[] content = new byte[0]; - if (createByEditingTemplate) { - // 从当前编辑模板中生成备份文件 - JTemplate template = HistoryTemplateListCache.getInstance().getCurrentEditingTemplate(); - content = template.exportData(); - } else { - content = WorkContext.getWorkResource().readFully(oldPath); - } - if (ArrayUtils.isEmpty(content)) { - throw new Exception(oldPath + " content is empty" ); - } - OutputStream out = null; - try { + // 读取模板数据 + byte[] content = getTemplateData(createByEditingTemplate, oldPath); + try(OutputStream out = file.asOutputStream()) { // 加锁 WorkContext.getCurrent().get(TplOperator.class).saveAs(file.getPath()); - out = file.asOutputStream(); out.write(content); } finally { - try { - if (out != null) { - out.close(); - } - } finally { - // 解锁 - WorkContext.getCurrent().get(TplOperator.class).closeAndFreeFile(file.getPath()); - } - + // 解锁 + WorkContext.getCurrent().get(TplOperator.class).closeAndFreeFile(file.getPath()); } return null; } @@ -107,6 +106,8 @@ public class TemplateUtils { protected void done() { try { get(); + // 创建备份成功后 + doAfterCreateTemplate.run(); if (openNewTemplate) { DesignerContext.getDesignerFrame().openTemplate(file); } @@ -118,7 +119,30 @@ public class TemplateUtils { } } }.execute(); + } + /** + * 读取模板文件数据 + * + * @param readCurrentEditingTemplate 是否读取当前编辑模板 + * @param path 模板路径 + * @return + */ + private static byte[] getTemplateData(boolean readCurrentEditingTemplate, String path) throws Exception { + byte[] content = new byte[0]; + if (readCurrentEditingTemplate) { + // 从当前编辑模板中生成备份文件 + JTemplate template = HistoryTemplateListCache.getInstance().getCurrentEditingTemplate(); + if (JTemplate.isValid(template)) { + content = template.exportData(); + } + } else { + content = WorkContext.getWorkResource().readFully(path); + } + if (ArrayUtils.isEmpty(content)) { + throw new Exception(StringUtils.messageFormat("{} content is empty", path)); + } + return content; } private static boolean isCancel(int result) { diff --git a/designer-base/src/main/java/com/fr/design/worker/save/SaveFailureHandler.java b/designer-base/src/main/java/com/fr/design/worker/save/SaveFailureHandler.java index 080b9ddbcd..128064301e 100644 --- a/designer-base/src/main/java/com/fr/design/worker/save/SaveFailureHandler.java +++ b/designer-base/src/main/java/com/fr/design/worker/save/SaveFailureHandler.java @@ -133,14 +133,14 @@ public class SaveFailureHandler implements ThrowableHandler { new Object[] {Toolkit.i18nText("Fine_Design_Template_SaveAs_Backup"), Toolkit.i18nText("Fine-Design_Basic_Button_Cancel")}, null); if (option == JOptionPane.YES_OPTION) { JTemplate template = HistoryTemplateListCache.getInstance().getCurrentEditingTemplate(); - if (template != null) { + if (JTemplate.isValid(template)) { TemplateUtils.createAndOpenTemplate( Toolkit.i18nText("Fine_Design_Template_Backup"), new FileNodeFILE(new FileNode(template.getPath(), false)), true, - true); - // 创建并打开备份模板后,关闭原模板 - HistoryTemplateListCache.getInstance().closeSelectedReport(template); + true, + // 创建并打开备份模板后,关闭原模板 + () -> HistoryTemplateListCache.getInstance().closeSelectedReport(template)); } } } From be3c8395be08a09eaf08d75c6db2718fb0b211ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yvan-=E6=AC=A7=E9=98=B3=E5=B8=86?= Date: Thu, 23 Mar 2023 09:35:36 +0800 Subject: [PATCH 2/3] =?UTF-8?q?REPORT-91503=20=E4=BF=9D=E5=AD=98=E6=A8=A1?= =?UTF-8?q?=E6=9D=BF=E9=A2=91=E7=B9=81=E6=8A=A5=E9=94=99=E9=94=81=E5=AE=9A?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=E4=B8=8D=E4=B8=80=E8=87=B4=EF=BC=8C=E5=9C=A8?= =?UTF-8?q?=E8=BF=99=E4=B8=AA=E5=BC=B9=E7=AA=97=E5=8F=A6=E5=AD=98=E4=B8=BA?= =?UTF-8?q?=E4=B8=80=E5=AE=9A=E4=BC=9A=E5=A4=B1=E8=B4=A5=E3=80=82=20?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=A0=BC=E5=BC=8F=E5=8C=96=E3=80=81=E6=B3=A8?= =?UTF-8?q?=E9=87=8A=E7=AD=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/fr/design/utils/TemplateUtils.java | 33 ++++++++++--------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/designer-base/src/main/java/com/fr/design/utils/TemplateUtils.java b/designer-base/src/main/java/com/fr/design/utils/TemplateUtils.java index e82251f4b8..e7d3c3dca3 100644 --- a/designer-base/src/main/java/com/fr/design/utils/TemplateUtils.java +++ b/designer-base/src/main/java/com/fr/design/utils/TemplateUtils.java @@ -35,12 +35,12 @@ public class TemplateUtils { /** * 创建新的模板文件并打开模板 * - * @param prefix 模板文件名称前缀 - * @param file 模板文件 + * @param prefix 模板文件名称前缀 + * @param file 模板文件 * @param createByEditingTemplate 是否根据 当前编辑模板 来创建新模板 * 为true时以CurrentEditingTemplate为准创建新模板 * 为false时以传入的File文件为准创建新模板,此文件可以不是编辑状态 - * @param openNewTemplate 是否需要在创建后打开模板 + * @param openNewTemplate 是否需要在创建后打开模板 */ public static void createAndOpenTemplate(String prefix, FILE file, boolean createByEditingTemplate, boolean openNewTemplate) { createAndOpenTemplate(prefix, file, createByEditingTemplate, openNewTemplate, () -> {}); @@ -49,13 +49,13 @@ public class TemplateUtils { /** * 创建新的模板文件并打开模板,并在创建备份模板成功后执行doAfterCreate * - * @param prefix 模板文件名称前缀 - * @param file 模板文件 - * @param createByEditingTemplate 是否根据 当前编辑模板 来创建新模板 - * 为true时以CurrentEditingTemplate为准创建新模板 - * 为false时以传入的File文件为准创建新模板,此文件可以不是编辑状态 - * @param openNewTemplate 是否需要在创建后打开模板 - * @param doAfterCreateTemplate 创建备份模板成功后调用 + * @param prefix 模板文件名称前缀 + * @param file 模板文件 + * @param createByEditingTemplate 是否根据 当前编辑模板 来创建新模板 + * 为true时以CurrentEditingTemplate为准创建新模板 + * 为false时以传入的File文件为准创建新模板,此文件可以不是编辑状态 + * @param openNewTemplate 是否需要在创建后打开模板 + * @param doAfterCreateTemplate 创建备份模板成功后调用 */ public static void createAndOpenTemplate(String prefix, FILE file, boolean createByEditingTemplate, boolean openNewTemplate, Runnable doAfterCreateTemplate) { String fileName = file.getName(); @@ -65,7 +65,7 @@ public class TemplateUtils { return; } String suffix = fileName.substring(indexOfLastDot + 1); - FILEChooserPane fileChooserPane = FILEChooserPane.getInstance(true, true); + FILEChooserPane fileChooserPane = FILEChooserPane.getInstance(true, true); fileChooserPane.setFileNameTextField(prefix + fileName, suffix); FileExtension fileExtension = FileExtension.parse(suffix); fileChooserPane.addChooseFILEFilter(new ChooseFileFilter(fileExtension, ProductConstants.APP_NAME + Toolkit.i18nText("Fine-Design_Report_Template_File"))); @@ -91,7 +91,7 @@ public class TemplateUtils { protected Void doInBackground() throws Exception { // 读取模板数据 byte[] content = getTemplateData(createByEditingTemplate, oldPath); - try(OutputStream out = file.asOutputStream()) { + try (OutputStream out = file.asOutputStream()) { // 加锁 WorkContext.getCurrent().get(TplOperator.class).saveAs(file.getPath()); out.write(content); @@ -124,19 +124,20 @@ public class TemplateUtils { /** * 读取模板文件数据 * - * @param readCurrentEditingTemplate 是否读取当前编辑模板 - * @param path 模板路径 - * @return + * @param readCurrentEditingTemplate 是否读取当前编辑模板 + * @param path 模板路径 + * @return 模板文件数据 */ private static byte[] getTemplateData(boolean readCurrentEditingTemplate, String path) throws Exception { byte[] content = new byte[0]; if (readCurrentEditingTemplate) { - // 从当前编辑模板中生成备份文件 + // 从当前编辑模板中读取模板文件数据 JTemplate template = HistoryTemplateListCache.getInstance().getCurrentEditingTemplate(); if (JTemplate.isValid(template)) { content = template.exportData(); } } else { + // 从给定的模板路径中读取模板文件数据 content = WorkContext.getWorkResource().readFully(path); } if (ArrayUtils.isEmpty(content)) { From fe85bf97116dc844a808cbfcb1f439222f56131d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yvan-=E6=AC=A7=E9=98=B3=E5=B8=86?= Date: Fri, 24 Mar 2023 11:44:32 +0800 Subject: [PATCH 3/3] =?UTF-8?q?REPORT-91503=20=E4=BF=9D=E5=AD=98=E6=A8=A1?= =?UTF-8?q?=E6=9D=BF=E9=A2=91=E7=B9=81=E6=8A=A5=E9=94=99=E9=94=81=E5=AE=9A?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=E4=B8=8D=E4=B8=80=E8=87=B4=EF=BC=8C=E5=9C=A8?= =?UTF-8?q?=E8=BF=99=E4=B8=AA=E5=BC=B9=E7=AA=97=E5=8F=A6=E5=AD=98=E4=B8=BA?= =?UTF-8?q?=E4=B8=80=E5=AE=9A=E4=BC=9A=E5=A4=B1=E8=B4=A5=E3=80=82=20?= =?UTF-8?q?=E5=A4=84=E7=90=86=E5=88=9B=E5=BB=BA=E6=A8=A1=E6=9D=BF=E5=A4=87?= =?UTF-8?q?=E4=BB=BD=E6=96=87=E4=BB=B6=E6=97=B6=E7=9A=84=E9=94=81=E5=AE=9A?= =?UTF-8?q?=E9=97=AE=E9=A2=98=EF=BC=8C=E4=BB=A5=E5=8F=8A=E9=94=81=E5=AE=9A?= =?UTF-8?q?=E5=8F=AF=E8=83=BD=E5=A4=B1=E8=B4=A5=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/fr/design/utils/TemplateUtils.java | 34 +++++++++++++------ .../worker/save/SaveFailureHandler.java | 2 +- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/designer-base/src/main/java/com/fr/design/utils/TemplateUtils.java b/designer-base/src/main/java/com/fr/design/utils/TemplateUtils.java index e7d3c3dca3..ce01d9992f 100644 --- a/designer-base/src/main/java/com/fr/design/utils/TemplateUtils.java +++ b/designer-base/src/main/java/com/fr/design/utils/TemplateUtils.java @@ -85,28 +85,42 @@ public class TemplateUtils { private static void createAndOpenTemplate0(FILE file, String oldPath, boolean createByEditingTemplate, boolean openNewTemplate, Runnable doAfterCreateTemplate) { - new SwingWorker() { + new SwingWorker() { @Override - protected Void doInBackground() throws Exception { + protected Boolean doInBackground() throws Exception { // 读取模板数据 byte[] content = getTemplateData(createByEditingTemplate, oldPath); - try (OutputStream out = file.asOutputStream()) { + OutputStream out = null; + try { // 加锁 - WorkContext.getCurrent().get(TplOperator.class).saveAs(file.getPath()); + boolean saveAsLock = WorkContext.getCurrent().get(TplOperator.class).saveAs(file.getPath()); + if (!saveAsLock) { + // 加锁失败时,直接返回 + return false; + } + out = file.asOutputStream(); out.write(content); } finally { - // 解锁 - WorkContext.getCurrent().get(TplOperator.class).closeAndFreeFile(file.getPath()); + try { + if (out != null) { + out.close(); + } + } finally { + // 解锁 + WorkContext.getCurrent().get(TplOperator.class).closeAndFreeFile(file.getPath()); + } } - return null; + return true; } @Override protected void done() { try { - get(); - // 创建备份成功后 + if (!get()) { + throw new Exception("[RemoteDesign] back up template file failed"); + } + // 创建备份成功后,关闭原模板 doAfterCreateTemplate.run(); if (openNewTemplate) { DesignerContext.getDesignerFrame().openTemplate(file); @@ -126,7 +140,7 @@ public class TemplateUtils { * * @param readCurrentEditingTemplate 是否读取当前编辑模板 * @param path 模板路径 - * @return 模板文件数据 + * @return 模板文件数据 */ private static byte[] getTemplateData(boolean readCurrentEditingTemplate, String path) throws Exception { byte[] content = new byte[0]; diff --git a/designer-base/src/main/java/com/fr/design/worker/save/SaveFailureHandler.java b/designer-base/src/main/java/com/fr/design/worker/save/SaveFailureHandler.java index 128064301e..119668ec68 100644 --- a/designer-base/src/main/java/com/fr/design/worker/save/SaveFailureHandler.java +++ b/designer-base/src/main/java/com/fr/design/worker/save/SaveFailureHandler.java @@ -139,7 +139,7 @@ public class SaveFailureHandler implements ThrowableHandler { new FileNodeFILE(new FileNode(template.getPath(), false)), true, true, - // 创建并打开备份模板后,关闭原模板 + // 创建并打开备份模板后,关闭原模板,无需释放原模板锁(锁定信息不一致 = 此用户模板锁信息已被清除) () -> HistoryTemplateListCache.getInstance().closeSelectedReport(template)); } }