1 package fr.ifremer.dali.ui.swing.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 com.google.common.collect.Sets;
27 import fr.ifremer.dali.dto.DaliBeans;
28 import fr.ifremer.dali.dto.SearchDateDTO;
29 import fr.ifremer.dali.dto.configuration.programStrategy.ProgramDTO;
30 import fr.ifremer.dali.service.DaliServiceLocator;
31 import fr.ifremer.dali.service.StatusFilter;
32 import fr.ifremer.dali.ui.swing.content.DaliMainUIHandler;
33 import fr.ifremer.dali.ui.swing.content.synchro.program.ProgramSelectUI;
34 import fr.ifremer.quadrige3.core.security.SecurityContextHelper;
35 import fr.ifremer.quadrige3.synchro.service.client.SynchroClientService;
36 import fr.ifremer.quadrige3.synchro.service.client.vo.SynchroClientExportToFileResult;
37 import fr.ifremer.quadrige3.ui.swing.ApplicationUI;
38 import fr.ifremer.quadrige3.ui.swing.action.AbstractReloadCurrentScreenAction;
39 import fr.ifremer.quadrige3.ui.swing.synchro.SynchroUIContext;
40 import fr.ifremer.quadrige3.ui.swing.synchro.SynchroUIHandler;
41 import org.apache.commons.collections4.CollectionUtils;
42 import org.apache.commons.logging.Log;
43 import org.apache.commons.logging.LogFactory;
44 import org.nuiton.util.DateUtil;
45
46 import java.awt.Dimension;
47 import java.io.File;
48 import java.util.Date;
49 import java.util.List;
50 import java.util.Set;
51
52 import static org.nuiton.i18n.I18n.t;
53
54
55
56
57
58
59
60 public class ExportToFileSynchroAction extends AbstractReloadCurrentScreenAction {
61
62 private static final Log log = LogFactory.getLog(ExportToFileSynchroAction.class);
63 protected File file;
64 private Set<String> programCodes;
65 private boolean dirtyOnly;
66 private SearchDateDTO searchDate;
67 private Date startDate;
68 private Date endDate;
69 private boolean hasData = false;
70
71
72
73
74
75
76 public ExportToFileSynchroAction(DaliMainUIHandler handler) {
77 super(handler, true);
78 setActionDescription(t("dali.action.synchro.exportToFile.title"));
79 }
80
81
82
83
84
85
86 public void setFile(File file) {
87 this.file = file;
88 }
89
90
91 @Override
92 public boolean prepareAction() throws Exception {
93 file = null;
94 boolean doAction = super.prepareAction();
95 if (!doAction) {
96 return false;
97 }
98
99
100 programCodes = getSynchroUIContext().getExportDataProgramCodes();
101 {
102 ProgramSelectUI programSelectUI = new ProgramSelectUI((ApplicationUI) getUI(), StatusFilter.ALL, programCodes, true, false);
103 handler.openDialog(programSelectUI, t("dali.action.synchro.export.dataProgramCodes.title"), new Dimension(800, 400));
104
105 List<ProgramDTO> programs = programSelectUI.getModel().getSelectedPrograms();
106
107
108 if (CollectionUtils.isEmpty(programs)) {
109 return false;
110 }
111
112
113 programCodes = Sets.newHashSet(DaliBeans.collectProperties(programs, ProgramDTO.PROPERTY_CODE));
114
115
116 dirtyOnly = programSelectUI.getModel().isDirtyOnly();
117 searchDate = programSelectUI.getModel().getSearchDate();
118 startDate = programSelectUI.getModel().getStartDate();
119 endDate = programSelectUI.getModel().getEndDate();
120 }
121
122
123 String date = DateUtil.formatDate(new Date(), "yyy-MM-dd");
124 file = saveFile(
125 getConfig().getSynchroZipFilePrefix() + date,
126 "zip",
127 t("dali.synchro.export.choose.exportDataToFile.title"),
128 t("dali.synchro.export.choose.exportDataToFile.buttonLabel"),
129 "^.*\\.zip", t("dali.common.file.zip")
130 );
131 doAction = file != null;
132
133 return doAction;
134 }
135
136 private SynchroUIContext getSynchroUIContext() {
137 return getContext().getSynchroContext();
138 }
139
140 private SynchroUIHandler getSynchroHandler() {
141 return getContext().getSynchroHandler();
142 }
143
144
145 @Override
146 public void doActionBeforeReload() {
147 hasData = false;
148
149 SynchroClientService synchroService = DaliServiceLocator.instance().getSynchroClientService();
150 int userId = SecurityContextHelper.getQuadrigeUserId();
151
152 createProgressionUIModel(100);
153
154
155 getSynchroUIContext().setExportDataProgramCodes(programCodes);
156 getSynchroUIContext().saveExportContext();
157
158
159 getProgressionUIModel().setMessage(t("quadrige3.synchro.progress.export"));
160
161
162 SynchroClientExportToFileResult exportResult = synchroService.exportToFile(userId,
163 file,
164 programCodes,
165 dirtyOnly,
166 false,
167 DaliBeans.getDateOperator(searchDate),
168 startDate,
169 endDate,
170 getProgressionUIModel(), 100);
171
172
173 hasData = exportResult.getDataResult().getTotalTreated() > 0;
174 if (!hasData) {
175 showNoDataMessage();
176 return;
177 }
178
179
180 getProgressionUIModel().setTotal(100);
181
182
183 setProgressionUIModel(getProgressionUIModel());
184
185
186 setSkipScreenReload(!hasData);
187 }
188
189
190 @Override
191 public void postSuccessAction() {
192
193 super.postSuccessAction();
194 if (log.isInfoEnabled()) {
195 log.info(t("dali.action.synchro.export.success"));
196 }
197
198
199 if (!hasData) {
200 getContext().getSynchroHandler().report(t("dali.action.synchro.export.noData"), false);
201 } else {
202 getContext().getSynchroHandler().report(t("dali.action.synchro.export.success"));
203 }
204 }
205
206
207 @Override
208 public void postFailedAction(Throwable error) {
209 super.postFailedAction(error);
210
211 if (error != null) {
212 log.error(t("dali.action.synchro.export.failed.log", error.getMessage()));
213 }
214
215 else if (getSynchroUIContext().getProgressionModel() != null
216 && getSynchroUIContext().getProgressionModel().getMessage() != null) {
217
218 String serverMessage = getSynchroUIContext().getProgressionModel().getMessage();
219 log.error(t("dali.action.synchro.export.failed.server.log", serverMessage));
220
221 }
222
223 else {
224 log.error(t("dali.action.synchro.export.failed"));
225 }
226
227 getContext().getSynchroHandler().report(t("dali.action.synchro.export.failed"));
228 }
229
230
231
232 private void showNoDataMessage() {
233 getContext().getDialogHelper().showMessageDialog(t("dali.action.synchro.export.noData"), t("dali.action.synchro.export.result.title"));
234 }
235
236
237 @Override
238 protected void releaseAction() {
239 super.releaseAction();
240 file = null;
241 hasData = false;
242 }
243 }