View Javadoc
1   package fr.ifremer.quadrige3.ui.swing.synchro.action;
2   
3   /*-
4    * #%L
5    * Quadrige3 Core :: Quadrige3 UI Common
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2017 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.quadrige3.core.dao.technical.ZipUtils;
27  import fr.ifremer.quadrige3.core.service.ClientServiceLocator;
28  import fr.ifremer.quadrige3.core.service.http.HttpService;
29  import fr.ifremer.quadrige3.ui.swing.synchro.SynchroUIHandler;
30  import org.apache.commons.io.FileUtils;
31  import org.apache.commons.io.FilenameUtils;
32  import org.apache.commons.lang3.StringUtils;
33  import org.apache.http.client.methods.HttpGet;
34  
35  import java.io.*;
36  
37  import static org.nuiton.i18n.I18n.t;
38  
39  /**
40   * <p>Abstract AbstractDownloadAction class.</p>
41   *
42   * @author Ludovic Pecquot <ludovic.pecquot@e-is.pro>
43   */
44  public abstract class AbstractDownloadAction extends AbstractSynchroAction {
45  
46      private boolean downloaded;
47      private boolean toUncompress;
48      private File resultDir = null;
49  
50      /**
51       * <p>getTargetDirectory.</p>
52       *
53       * @return a {@link File} object.
54       */
55      public abstract File getTargetDirectory();
56  
57      /**
58       * <p>getUrlPath.</p>
59       *
60       * @return a {@link String} object.
61       */
62      public abstract String getUrlPath();
63  
64      /**
65       * <p>Constructor for AbstractDownloadAction.</p>
66       *
67       * @param handler a {@link SynchroUIHandler} object.
68       */
69      public AbstractDownloadAction(SynchroUIHandler handler) {
70          super(handler);
71  
72          toUncompress = true;
73      }
74  
75      /**
76       * <p>Setter for the field <code>toUncompress</code>.</p>
77       *
78       * @param toUncompress a boolean.
79       */
80      public void setToUncompress(boolean toUncompress) {
81          this.toUncompress = toUncompress;
82      }
83  
84      /**
85       * {@inheritDoc}
86       */
87      @Override
88      public boolean initAction() {
89          super.initAction();
90  
91          getHandler().showProgressCard();
92  
93          downloaded = false;
94          getModel().setWorkingDirectory(null);
95  
96          return true;
97      }
98  
99      /**
100      * {@inheritDoc}
101      */
102     @Override
103     public void doAction() throws Exception {
104 
105         HttpService httpService = ClientServiceLocator.instance().getHttpService();
106         String fileName = getModel().getTransferFilename();
107 
108         if (StringUtils.isBlank(fileName)) {
109             throw new IOException("can not get the file name to download");
110         }
111 
112         File downloadedFile = new File(getConfig().getTempDirectory(), fileName);
113 
114         getModel().getProgressionModel().setMessage(t("quadrige3.synchro.progress.download"));
115 
116         // download the zip file
117         HttpGet downloadHttpGet = new HttpGet(httpService.getPathUri(getUrlPath(), fileName));
118         httpService.executeDownloadFileRequest(getContext().getAuthenticationInfo(), downloadHttpGet, getModel().getProgressionModel(), downloadedFile);
119 
120         // Compute the target directory for user
121         File userDir = getTargetDirectory();
122         FileUtils.forceMkdir(userDir);
123 
124         if (toUncompress && ZipUtils.isZipFile(downloadedFile)) {
125 
126             // Unzip it
127             resultDir = new File(userDir, FilenameUtils.getBaseName(fileName));
128             getModel().getProgressionModel().setCurrent(0);
129             getModel().getProgressionModel().setMessage(t("quadrige3.synchro.progress.uncompress"));
130             ZipUtils.uncompressFileToPath(downloadedFile.toPath(), resultDir.toPath(), getModel().getProgressionModel(), true);
131 
132         } else {
133 
134             // move it
135             resultDir = userDir;
136             FileUtils.moveFileToDirectory(downloadedFile, resultDir, true);
137         }
138 
139         downloaded = true;
140 
141     }
142 
143     /**
144      * {@inheritDoc}
145      */
146     @Override
147     public void doneAction() {
148         updateModel();
149     }
150 
151     /**
152      * {@inheritDoc}
153      */
154     @Override
155     public void failedAction(Throwable ex) {
156         updateModel();
157         super.failedAction(ex);
158     }
159 
160     private void updateModel() {
161         getModel().setWorkingDirectory((downloaded && resultDir != null && resultDir.isDirectory()) ? resultDir : null);
162     }
163 
164 }