View Javadoc
1   package fr.ifremer.dali.ui.swing.content.extraction.list;
2   
3   /*
4    * #%L
5    * Dali :: UI
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2014 - 2015 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
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   * Abstract extract action
47   * <p/>
48   * Created by Ludovic on 03/12/2015.
49   */
50  public abstract class AbstractInternalExtractAction extends AbstractExtractAction {
51  
52      private static final Log LOG = LogFactory.getLog(AbstractInternalExtractAction.class);
53  
54      /**
55       * Constructor.
56       *
57       * @param handler Handler
58       */
59      protected AbstractInternalExtractAction(ExtractionsTableUIHandler handler) {
60          super(handler);
61      }
62  
63      /**
64       * {@inheritDoc}
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          // Compute default output file
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          // ask user file where to export db
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       * {@inheritDoc}
100      */
101     @Override
102     public void doAction() throws Exception {
103 
104         // Will check data updates
105         super.doAction();
106 
107         // Try open a stream to verify file availability
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         // Launch extraction
116         performExtraction();
117     }
118 
119     /**
120      * {@inheritDoc}
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         // Delete empty file (Mantis #40285)
156         if (getOutputFile() != null && getOutputFile().exists() && getOutputFile().length() == 0) {
157             getOutputFile().delete();
158         }
159 
160         super.postFailedAction(error);
161     }
162 
163 }