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
27 import fr.ifremer.quadrige3.core.security.SecurityContextHelper;
28 import fr.ifremer.quadrige3.core.service.ClientServiceLocator;
29 import fr.ifremer.quadrige3.synchro.service.client.SynchroClientService;
30 import fr.ifremer.quadrige3.synchro.service.client.vo.SynchroClientExportToFileResult;
31 import fr.ifremer.quadrige3.synchro.vo.SynchroProgressionStatus;
32 import fr.ifremer.quadrige3.ui.swing.action.AbstractReloadCurrentScreenAction;
33 import fr.ifremer.quadrige3.ui.swing.content.AbstractMainUIHandler;
34 import fr.ifremer.quadrige3.ui.swing.model.ProgressionUIModel;
35 import fr.ifremer.quadrige3.ui.swing.synchro.SynchroUIContext;
36 import fr.ifremer.quadrige3.ui.swing.synchro.SynchroUIHandler;
37 import org.apache.commons.logging.Log;
38 import org.apache.commons.logging.LogFactory;
39 import org.nuiton.util.DateUtil;
40
41 import java.io.File;
42 import java.util.Date;
43
44 import static org.nuiton.i18n.I18n.t;
45
46
47
48
49
50
51
52 public class ExportReferentialToFileAction extends AbstractReloadCurrentScreenAction {
53
54 private static final Log log = LogFactory.getLog(ExportReferentialToFileAction.class);
55
56 private boolean hasReferential = false;
57
58 protected File file;
59
60
61
62
63
64
65 public ExportReferentialToFileAction(AbstractMainUIHandler handler) {
66 super(handler, true);
67 setActionDescription(t("quadrige3.action.synchro.exportReferentialToFile.title"));
68 }
69
70
71
72
73
74
75 public void setFile(File file) {
76 this.file = file;
77 }
78
79
80 @Override
81 public boolean prepareAction() throws Exception {
82 file = null;
83 boolean doAction = super.prepareAction();
84 if (!doAction) {
85 return false;
86 }
87
88 String date = DateUtil.formatDate(new Date(), "yyyy-MM-dd");
89
90
91 file = saveFile(
92 getConfig().getSynchroZipFilePrefix() + date,
93 "zip",
94 t("quadrige3.synchro.export.choose.exportReferentialToFile.title"),
95 t("quadrige3.synchro.export.choose.exportReferentialToFile.buttonLabel"),
96 "^.*\\.zip", t("quadrige3.common.file.zip")
97 );
98 doAction = file != null;
99
100 return doAction;
101 }
102
103
104 private SynchroUIContext getSynchroUIContext() {
105 return getContext().getSynchroContext();
106 }
107
108 private SynchroUIHandler getSynchroHandler() {
109 return getContext().getSynchroHandler();
110 }
111
112
113 @Override
114 public void doActionBeforeReload() {
115 hasReferential = false;
116
117 SynchroClientService synchroService = ClientServiceLocator.instance().getSynchroClientService();
118
119 getSynchroUIContext().saveExportContext();
120
121 ProgressionUIModel mainProgressionModel = createProgressionUIModel(100);
122 getSynchroUIContext().setStatus(SynchroProgressionStatus.RUNNING);
123 getSynchroUIContext().saveExportContext();
124
125
126 getProgressionUIModel().setMessage(t("quadrige3.synchro.progress.export"));
127
128
129 SynchroClientExportToFileResult exportResult = synchroService.exportAllReferentialToFile(
130 SecurityContextHelper.getQuadrigeUserId(),
131 file,
132 getProgressionUIModel(),
133 100);
134
135
136 hasReferential = exportResult.getReferentialResult() != null && exportResult.getReferentialResult().getTotalTreated() > 0;
137 if (!hasReferential) {
138 showNoReferentialMessage();
139 return;
140 }
141
142 getSynchroUIContext().setExportFileName(file.getName());
143 getSynchroUIContext().saveExportContext();
144
145
146 getProgressionUIModel().setTotal(100);
147 setProgressionUIModel(getSynchroUIContext().getProgressionModel());
148
149
150 setProgressionUIModel(mainProgressionModel);
151
152
153 setSkipScreenReload(!hasReferential);
154 }
155
156
157 @Override
158 public void postSuccessAction() {
159
160 super.postSuccessAction();
161 if (log.isInfoEnabled()) {
162 log.info("Synchronization export success");
163 }
164
165 getSynchroUIContext().resetExportContext();
166 getSynchroUIContext().saveExportContext();
167
168
169 if (!hasReferential) {
170 getContext().getSynchroHandler().report(t("quadrige3.action.synchro.export.noReferential"), false);
171 }
172 else {
173 getContext().getSynchroHandler().report(t("quadrige3.action.synchro.export.success"));
174 }
175 }
176
177
178 @Override
179 public void postFailedAction(Throwable error) {
180 super.postFailedAction(error);
181
182 if (error != null) {
183 log.error(t("quadrige3.action.synchro.export.failed.log", error.getMessage()));
184 }
185 else if(getSynchroUIContext().getProgressionModel() != null
186 && getSynchroUIContext().getProgressionModel().getMessage() != null) {
187
188 String serverMessage = getSynchroUIContext().getProgressionModel().getMessage();
189 log.error(t("quadrige3.action.synchro.export.failed.server.log", serverMessage));
190 }
191 else {
192 log.error(t("quadrige3.action.synchro.export.failed"));
193 }
194
195 getSynchroUIContext().resetExportContext();
196 getSynchroUIContext().saveExportContext();
197
198 getContext().getSynchroHandler().report(t("quadrige3.action.synchro.export.failed"));
199 }
200
201
202
203 private void showNoReferentialMessage() {
204 getContext().getDialogHelper().showMessageDialog(t("quadrige3.action.synchro.export.noReferential"),
205 t("quadrige3.action.synchro.export.result.title"));
206 }
207
208
209 @Override
210 protected void releaseAction() {
211 super.releaseAction();
212 file = null;
213 hasReferential = false;
214 }
215 }