1 package fr.ifremer.quadrige3.ui.swing.synchro.action;
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.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
41
42
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
52
53
54
55 public abstract File getTargetDirectory();
56
57
58
59
60
61
62 public abstract String getUrlPath();
63
64
65
66
67
68
69 public AbstractDownloadAction(SynchroUIHandler handler) {
70 super(handler);
71
72 toUncompress = true;
73 }
74
75
76
77
78
79
80 public void setToUncompress(boolean toUncompress) {
81 this.toUncompress = toUncompress;
82 }
83
84
85
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
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
117 HttpGet downloadHttpGet = new HttpGet(httpService.getPathUri(getUrlPath(), fileName));
118 httpService.executeDownloadFileRequest(getContext().getAuthenticationInfo(), downloadHttpGet, getModel().getProgressionModel(), downloadedFile);
119
120
121 File userDir = getTargetDirectory();
122 FileUtils.forceMkdir(userDir);
123
124 if (toUncompress && ZipUtils.isZipFile(downloadedFile)) {
125
126
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
135 resultDir = userDir;
136 FileUtils.moveFileToDirectory(downloadedFile, resultDir, true);
137 }
138
139 downloaded = true;
140
141 }
142
143
144
145
146 @Override
147 public void doneAction() {
148 updateModel();
149 }
150
151
152
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 }