From 052f9357e2b688099f530baf7026d2040e3df094 Mon Sep 17 00:00:00 2001 From: "yaoh.wu" Date: Fri, 22 Feb 2019 17:46:17 +0800 Subject: [PATCH] =?UTF-8?q?REPORT-14624=20Rename=20=E6=96=B9=E5=BC=8F?= =?UTF-8?q?=E5=AF=BC=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../file/export/AbstractExportAction.java | 15 ++- .../main/java/com/fr/file/AbstractFILE.java | 93 ++++++++++++++++ .../src/main/java/com/fr/file/FileFILE.java | 2 +- .../java/com/fr/file/RenameExportFILE.java | 102 ++++++++++++++++++ .../main/java/com/fr/file/StashedFILE.java | 34 +----- 5 files changed, 207 insertions(+), 39 deletions(-) create mode 100644 designer-base/src/main/java/com/fr/file/AbstractFILE.java create mode 100644 designer-base/src/main/java/com/fr/file/RenameExportFILE.java diff --git a/designer-base/src/main/java/com/fr/design/actions/file/export/AbstractExportAction.java b/designer-base/src/main/java/com/fr/design/actions/file/export/AbstractExportAction.java index 536fc87ee5..17d19aab71 100644 --- a/designer-base/src/main/java/com/fr/design/actions/file/export/AbstractExportAction.java +++ b/designer-base/src/main/java/com/fr/design/actions/file/export/AbstractExportAction.java @@ -1,7 +1,7 @@ package com.fr.design.actions.file.export; -import com.fr.base.vcs.DesignerMode; import com.fr.design.actions.JTemplateAction; +import com.fr.design.base.mode.DesignModeContext; import com.fr.design.gui.iprogressbar.FRProgressBar; import com.fr.design.i18n.Toolkit; import com.fr.design.mainframe.DesignerContext; @@ -9,6 +9,7 @@ import com.fr.design.mainframe.JTemplate; import com.fr.exception.RemoteDesignPermissionDeniedException; import com.fr.file.FILE; import com.fr.file.FILEChooserPane; +import com.fr.file.RenameExportFILE; import com.fr.file.filter.ChooseFileFilter; import com.fr.io.exporter.DesignExportType; import com.fr.io.exporter.ExporterKey; @@ -92,12 +93,17 @@ public abstract class AbstractExportAction> extends JT int saveValue = fileChooserPane.showSaveDialog(DesignerContext.getDesignerFrame(), "." + this.getDefaultExtension()); if (saveValue == FILEChooserPane.JOPTIONPANE_OK_OPTION || saveValue == FILEChooserPane.OK_OPTION) { FILE target = fileChooserPane.getSelectedFILE(); + //rename 方式导出 + target = RenameExportFILE.create(target); try { target.mkfile(); } catch (Exception exp) { FineLoggerFactory.getLogger().error("Error In Make New File", exp); } - FineLoggerFactory.getLogger().info("\"" + target.getName() + "\"" + Toolkit.i18nText("Fine-Design_Report_Prepare_Export") + "!"); + FineLoggerFactory.getLogger().info( + "\"" + RenameExportFILE.recoverFileName(target.getName()) + "\"" + + Toolkit.i18nText("Fine-Design_Report_Prepare_Export") + "!" + ); progressbar = new FRProgressBar( createExportWork(getSource(), target, para), @@ -124,7 +130,7 @@ public abstract class AbstractExportAction> extends JT private boolean processNotSaved() { //当前编辑的模板 E e = getEditingComponent(); - if (!e.isALLSaved() && !DesignerMode.isVcsMode()) { + if (!e.isALLSaved() && !DesignModeContext.isVcsMode()) { e.stopEditing(); int returnVal = JOptionPane.showConfirmDialog( DesignerContext.getDesignerFrame(), @@ -149,7 +155,7 @@ public abstract class AbstractExportAction> extends JT private SwingWorker createExportWork(final FILE source, final FILE target, final Map parameterMap) { final String path = source.getPath(); - final String name = target.getName(); + final String name = RenameExportFILE.recoverFileName(target.getName()); return new SwingWorker() { @@ -162,7 +168,6 @@ public abstract class AbstractExportAction> extends JT dealExporter(outputStream, path, parameterMap); this.setProgress(80); outputStream.flush(); - outputStream.close(); this.setProgress(100); FineLoggerFactory.getLogger().info("\"" + name + "\"" + Toolkit.i18nText("Fine-Design_Report_Finish_Export") + "!"); diff --git a/designer-base/src/main/java/com/fr/file/AbstractFILE.java b/designer-base/src/main/java/com/fr/file/AbstractFILE.java new file mode 100644 index 0000000000..866ea2c2b1 --- /dev/null +++ b/designer-base/src/main/java/com/fr/file/AbstractFILE.java @@ -0,0 +1,93 @@ +package com.fr.file; + +import javax.swing.Icon; +import java.io.InputStream; +import java.io.OutputStream; + +public abstract class AbstractFILE implements FILE { + + @Override + public String prefix() { + throw new UnsupportedOperationException(); + } + + @Override + public boolean isDirectory() { + throw new UnsupportedOperationException(); + } + + @Override + public String getName() { + throw new UnsupportedOperationException(); + } + + @Override + public Icon getIcon() { + throw new UnsupportedOperationException(); + } + + @Override + public String getPath() { + throw new UnsupportedOperationException(); + } + + @Override + public void setPath(String path) { + throw new UnsupportedOperationException(); + } + + @Override + public FILE getParent() { + throw new UnsupportedOperationException(); + } + + @Override + public FILE[] listFiles() { + throw new UnsupportedOperationException(); + } + + @Override + public boolean createFolder(String name) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean mkfile() throws Exception { + throw new UnsupportedOperationException(); + } + + @Override + public boolean exists() { + throw new UnsupportedOperationException(); + } + + @Override + public void closeTemplate() throws Exception { + throw new UnsupportedOperationException(); + } + + @Override + public InputStream asInputStream() throws Exception { + throw new UnsupportedOperationException(); + } + + @Override + public OutputStream asOutputStream() throws Exception { + throw new UnsupportedOperationException(); + } + + @Override + public String getEnvFullName() { + throw new UnsupportedOperationException(); + } + + @Override + public boolean isMemFile() { + throw new UnsupportedOperationException(); + } + + @Override + public boolean isEnvFile() { + throw new UnsupportedOperationException(); + } +} diff --git a/designer-base/src/main/java/com/fr/file/FileFILE.java b/designer-base/src/main/java/com/fr/file/FileFILE.java index 5331cd7785..c4f58fa1a3 100644 --- a/designer-base/src/main/java/com/fr/file/FileFILE.java +++ b/designer-base/src/main/java/com/fr/file/FileFILE.java @@ -201,7 +201,7 @@ public class FileFILE implements FILE { OutputStream out; try { StableUtils.makesureFileExist(file); - out = new FileOutputStream(file, true); + out = new FileOutputStream(file, false); } catch (Exception e) { throw SessionLocalManager.createLogPackedException(e); } diff --git a/designer-base/src/main/java/com/fr/file/RenameExportFILE.java b/designer-base/src/main/java/com/fr/file/RenameExportFILE.java new file mode 100644 index 0000000000..8b03e943f6 --- /dev/null +++ b/designer-base/src/main/java/com/fr/file/RenameExportFILE.java @@ -0,0 +1,102 @@ +package com.fr.file; + +import com.fr.stable.StableUtils; +import com.fr.stable.StringUtils; +import com.fr.third.org.apache.commons.io.FileUtils; +import com.fr.web.session.SessionLocalManager; + +import javax.swing.Icon; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; + +public class RenameExportFILE extends AbstractFILE { + + private static final String EXPORT_SUFFIX = ".FRExportTmp"; + + private FILE file; + + private RenameExportFILE(FILE file) { + this.file = new FileFILE(new File(file.getPath() + EXPORT_SUFFIX)); + } + + public static RenameExportFILE create(FILE file) { + return new RenameExportFILE(file); + } + + public static String recoverFileName(String fileName) { + if (StringUtils.isEmpty(fileName) || !fileName.endsWith(EXPORT_SUFFIX)) { + return fileName; + } + return fileName.substring(0, fileName.lastIndexOf(EXPORT_SUFFIX)); + } + + @Override + public String prefix() { + return file.prefix(); + } + + @Override + public boolean isDirectory() { + return false; + } + + @Override + public String getName() { + return file.getName(); + } + + @Override + public Icon getIcon() { + return null; + } + + @Override + public String getPath() { + return file.getPath(); + } + + + @Override + public FILE getParent() { + return file.getParent(); + } + + + @Override + public boolean mkfile() throws Exception { + return file.mkfile(); + } + + @Override + public boolean exists() { + return file.exists(); + } + + @Override + public OutputStream asOutputStream() throws Exception { + + final File localeFile = new File(file.getPath()); + OutputStream out; + try { + StableUtils.makesureFileExist(localeFile); + out = new FileOutputStream(localeFile, false) { + @Override + public void close() throws IOException { + super.close(); + String path = file.getPath(); + if (localeFile.exists()) { + FileUtils.copyFile(localeFile, new File(recoverFileName(path))); + if (localeFile.getPath().endsWith(EXPORT_SUFFIX)) { + FileUtils.forceDelete(localeFile); + } + } + } + }; + } catch (Exception e) { + throw SessionLocalManager.createLogPackedException(e); + } + return out; + } +} \ No newline at end of file diff --git a/designer-base/src/main/java/com/fr/file/StashedFILE.java b/designer-base/src/main/java/com/fr/file/StashedFILE.java index 4bc9b431ea..181af28601 100644 --- a/designer-base/src/main/java/com/fr/file/StashedFILE.java +++ b/designer-base/src/main/java/com/fr/file/StashedFILE.java @@ -1,15 +1,13 @@ package com.fr.file; import javax.swing.Icon; -import javax.transaction.NotSupportedException; import java.io.ByteArrayInputStream; import java.io.InputStream; -import java.io.OutputStream; /** * 切换环境用于暂存的文件类型 */ -public class StashedFILE implements FILE { +public class StashedFILE extends AbstractFILE { private FILE file; private byte[] content; @@ -44,31 +42,6 @@ public class StashedFILE implements FILE { return file.getPath(); } - @Override - public void setPath(String path) { - throw new UnsupportedOperationException(); - } - - @Override - public FILE getParent() { - throw new UnsupportedOperationException(); - } - - @Override - public FILE[] listFiles() { - throw new UnsupportedOperationException(); - } - - @Override - public boolean createFolder(String name) { - throw new UnsupportedOperationException(); - } - - @Override - public boolean mkfile() throws Exception { - throw new UnsupportedOperationException(); - } - @Override public boolean exists() { return false; @@ -84,11 +57,6 @@ public class StashedFILE implements FILE { return new ByteArrayInputStream(content); } - @Override - public OutputStream asOutputStream() throws Exception { - throw new NotSupportedException(); - } - @Override public String getEnvFullName() { return file.getEnvFullName();