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.dto.DaliBeans;
27  import fr.ifremer.dali.service.DaliServiceLocator;
28  import fr.ifremer.dali.ui.swing.content.extraction.list.external.ExternalChooseUI;
29  import fr.ifremer.quadrige3.core.dao.technical.Assert;
30  import fr.ifremer.quadrige3.core.dao.technical.ZipUtils;
31  import org.apache.commons.lang3.StringUtils;
32  import org.apache.commons.logging.Log;
33  import org.apache.commons.logging.LogFactory;
34  import org.nuiton.util.DateUtil;
35  
36  import java.awt.Dimension;
37  import java.io.File;
38  import java.util.Date;
39  import java.util.Objects;
40  
41  import static org.nuiton.i18n.I18n.t;
42  
43  /**
44   * Abstract extract action
45   * <p/>
46   * Created by Ludovic on 03/12/2015.
47   */
48  public abstract class AbstractExternalExtractAction extends AbstractExtractAction {
49  
50      private static final Log LOG = LogFactory.getLog(AbstractExternalExtractAction.class);
51      private static final int MAX_FILE_NAME_LENGTH = 255;
52  
53      private String fileName;
54      private String email;
55  
56      /**
57       * Constructor.
58       *
59       * @param handler Handler
60       */
61      protected AbstractExternalExtractAction(ExtractionsTableUIHandler handler) {
62          super(handler);
63      }
64  
65      /** {@inheritDoc} */
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 external");
74              return false;
75          }
76  
77          String tempFileName = String.format("%s-%s-%s-%s",
78                  t("dali.service.extraction.file.prefix"),
79                  getOutputType().getLabel(),
80                  getSelectedExtraction().getName(),
81                  DateUtil.formatDate(new Date(), "yyyy-MM-dd-HHmmss"));
82  
83          // ask for email& file name confirmation
84          ExternalChooseUI externalChooseUI = new ExternalChooseUI(getContext());
85          externalChooseUI.getModel().setEmail(StringUtils.isEmpty(getSelectedExtraction().getUser().getEmail()) ? "" : getSelectedExtraction().getUser().getEmail().toLowerCase());
86          externalChooseUI.getModel().setFileName(tempFileName);
87          getHandler().openDialog(externalChooseUI, new Dimension(500, 150));
88  
89          if (!externalChooseUI.getModel().isValid()) {
90              if (LOG.isDebugEnabled()) {
91                  LOG.debug("extraction aborted");
92              }
93              return false;
94          }
95  
96          email = externalChooseUI.getModel().getEmail().toLowerCase().trim();
97          String securedName = DaliBeans.toSecuredString(externalChooseUI.getModel().getFileName()).trim();
98          fileName = String.format("%s#%s", email, securedName);
99  
100         // trim to fit 255 characters maximum
101         if (fileName.length() > MAX_FILE_NAME_LENGTH) {
102             fileName = String.format("%s#%s", email, securedName.substring(0, securedName.length() - MAX_FILE_NAME_LENGTH - fileName.length()));
103         }
104         Assert.isTrue(fileName.length() <= 255);
105 
106         createProgressionUIModel();
107 
108         return true;
109     }
110 
111     /** {@inheritDoc} */
112     @Override
113     public void doAction() throws Exception {
114 
115         // Will check data updates
116         super.doAction();
117 
118         // create the extraction file in temp dir
119         File tempDir = getConfig().getNewTmpDirectory("extraction");
120         File extractionDir = new File(tempDir, fileName);
121         extractionDir.mkdirs();
122         setOutputFile(new File(extractionDir, fileName + "." + getConfig().getExtractionResultFileExtension()));
123 
124         // Perform
125         performExtraction();
126 
127         // zip files
128         File zipFile = new File(tempDir, fileName + ".zip");
129         ZipUtils.compressFilesInPath(extractionDir.toPath(), zipFile.toPath(), true);
130 
131         // export zip file
132         getProgressionUIModel().setMessage(t("dali.extraction.external.upload.message"));
133 
134         DaliServiceLocator.instance().getExtractionRestClientService().uploadExtractionFile(
135                 getContext().getAuthenticationInfo(),
136                 zipFile, getOutputType().toString(),
137                 getProgressionUIModel());
138 
139     }
140 
141     /** {@inheritDoc} */
142     @Override
143     public void postSuccessAction() {
144 
145         String text = t("dali.action.external.extract.done",
146                 decorate(Objects.requireNonNull(getSelectedExtraction())),
147                 getOutputType().getLabel(),
148                 email
149                 );
150 
151         getContext().getDialogHelper().showMessageDialog(text, t("dali.action.extract.title", getOutputType().getLabel()));
152 
153         super.postSuccessAction();
154     }
155 
156     /** {@inheritDoc} */
157     @Override
158     protected void releaseAction() {
159         fileName = null;
160         email = null;
161         super.releaseAction();
162     }
163 }