1 package fr.ifremer.dali.ui.swing.content.extraction.list;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 import fr.ifremer.dali.service.DaliBusinessException;
27 import fr.ifremer.quadrige3.ui.swing.ApplicationUIUtil;
28 import org.apache.commons.io.FileUtils;
29 import org.apache.commons.io.IOUtils;
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import org.nuiton.util.DateUtil;
33
34 import javax.swing.JEditorPane;
35 import javax.swing.JOptionPane;
36 import javax.swing.event.HyperlinkEvent;
37 import java.io.IOException;
38 import java.io.OutputStream;
39 import java.net.URL;
40 import java.util.Date;
41 import java.util.Objects;
42
43 import static org.nuiton.i18n.I18n.t;
44
45
46
47
48
49
50 public abstract class AbstractInternalExtractAction extends AbstractExtractAction {
51
52 private static final Log LOG = LogFactory.getLog(AbstractInternalExtractAction.class);
53
54
55
56
57
58
59 protected AbstractInternalExtractAction(ExtractionsTableUIHandler handler) {
60 super(handler);
61 }
62
63
64
65
66 @Override
67 public boolean prepareAction() throws Exception {
68 if (!super.prepareAction()) {
69 return false;
70 }
71
72 if (getOutputType().isExternal()) {
73 LOG.error("wrong ExtractionOutputType (should be not external");
74 return false;
75 }
76
77
78 String date = DateUtil.formatDate(new Date(), "yyyy-MM-dd-HHmmss");
79 String fileName = String.format("%s-%s-%s-%s",
80 t("dali.service.extraction.file.prefix"),
81 getOutputType().getLabel(),
82 getSelectedExtraction().getName(),
83 date);
84 String fileExtension = getConfig().getExtractionResultFileExtension();
85
86
87 setOutputFile(saveFile(
88 fileName,
89 fileExtension,
90 t("dali.extraction.choose.file.title"),
91 t("dali.extraction.choose.file.buttonLabel"),
92 "^.*\\." + fileExtension,
93 t("dali.common.file." + fileExtension)));
94
95 return getOutputFile() != null;
96 }
97
98
99
100
101 @Override
102 public void doAction() throws Exception {
103
104
105 super.doAction();
106
107
108 try {
109 OutputStream out = FileUtils.openOutputStream(getOutputFile());
110 IOUtils.closeQuietly(out);
111 } catch (IOException e) {
112 throw new DaliBusinessException(e.getLocalizedMessage());
113 }
114
115
116 performExtraction();
117 }
118
119
120
121
122 @Override
123 public void postSuccessAction() {
124
125 String text = t("dali.action.extract.done",
126 decorate(Objects.requireNonNull(getSelectedExtraction())),
127 getOutputType().getLabel(),
128 getOutputFile().getAbsoluteFile().toURI(),
129 getOutputFile().getAbsolutePath());
130
131 JEditorPane editorPane = new JEditorPane("text/html", text);
132 editorPane.setEditable(false);
133 editorPane.addHyperlinkListener(e -> {
134 if (HyperlinkEvent.EventType.ACTIVATED == e.getEventType()) {
135 URL url = e.getURL();
136 if (LOG.isInfoEnabled()) {
137 LOG.info("open url: " + url);
138 }
139 ApplicationUIUtil.openLink(url);
140 }
141 });
142
143 getContext().getDialogHelper().showOptionDialog(
144 getUI(),
145 editorPane,
146 t("dali.action.extract.title", getOutputType().getLabel()),
147 JOptionPane.INFORMATION_MESSAGE, JOptionPane.DEFAULT_OPTION);
148
149 super.postSuccessAction();
150 }
151
152 @Override
153 public void postFailedAction(Throwable error) {
154
155
156 if (getOutputFile() != null && getOutputFile().exists() && getOutputFile().length() == 0) {
157 getOutputFile().delete();
158 }
159
160 super.postFailedAction(error);
161 }
162
163 }