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.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
45
46
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
58
59
60
61 protected AbstractExternalExtractAction(ExtractionsTableUIHandler handler) {
62 super(handler);
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 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
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
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
112 @Override
113 public void doAction() throws Exception {
114
115
116 super.doAction();
117
118
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
125 performExtraction();
126
127
128 File zipFile = new File(tempDir, fileName + ".zip");
129 ZipUtils.compressFilesInPath(extractionDir.toPath(), zipFile.toPath(), true);
130
131
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
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
157 @Override
158 protected void releaseAction() {
159 fileName = null;
160 email = null;
161 super.releaseAction();
162 }
163 }