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.configuration.programStrategy.ProgramDTO;
29 import fr.ifremer.dali.service.StatusFilter;
30 import fr.ifremer.dali.ui.swing.DaliUIContext;
31 import fr.ifremer.dali.ui.swing.content.DaliMainUIHandler;
32 import fr.ifremer.dali.ui.swing.content.synchro.program.ProgramSelectUI;
33 import fr.ifremer.quadrige3.synchro.vo.SynchroProgressionStatus;
34 import fr.ifremer.quadrige3.ui.swing.ApplicationUI;
35 import fr.ifremer.quadrige3.ui.swing.action.AbstractMainUIAction;
36 import fr.ifremer.quadrige3.ui.swing.synchro.SynchroDirection;
37 import fr.ifremer.quadrige3.ui.swing.synchro.SynchroUIContext;
38 import fr.ifremer.quadrige3.ui.swing.synchro.SynchroUIHandler;
39 import fr.ifremer.quadrige3.ui.swing.synchro.action.AbstractSynchroAction;
40 import fr.ifremer.quadrige3.ui.swing.synchro.action.ImportSynchroCheckAction;
41 import fr.ifremer.quadrige3.ui.swing.synchro.action.ImportSynchroDirectAction;
42 import fr.ifremer.quadrige3.ui.swing.synchro.action.ImportSynchroStartAction;
43 import org.apache.commons.collections4.CollectionUtils;
44
45 import java.awt.Dimension;
46 import java.util.List;
47 import java.util.Set;
48
49 import static org.nuiton.i18n.I18n.t;
50
51
52
53
54
55
56 public class ImportSynchroAction extends AbstractMainUIAction {
57
58 private AbstractSynchroAction nextSynchroAction;
59 private boolean referentialOnly = false;
60 private boolean silentIfNoUpdate = false;
61 private boolean silentIfUpdate = false;
62
63
64
65
66
67
68 public ImportSynchroAction(DaliMainUIHandler handler) {
69 super(handler, false);
70 }
71
72 @Override
73 public DaliUIContext getContext() {
74 return (DaliUIContext) super.getContext();
75 }
76
77 private SynchroUIContext getSynchroUIContext() {
78 return getContext().getSynchroContext();
79 }
80
81 private SynchroUIHandler getSynchroHandler() {
82 return getContext().getSynchroHandler();
83 }
84
85
86
87
88
89
90 public void setReferentialOnly(boolean referentialOnly) {
91 this.referentialOnly = referentialOnly;
92 }
93
94
95
96
97
98
99 public void setSilentIfNoUpdate(boolean silentIfNoUpdate) {
100 this.silentIfNoUpdate = silentIfNoUpdate;
101 }
102
103 public void setSilentIfUpdate(boolean silentIfUpdate) {
104 this.silentIfUpdate = silentIfUpdate;
105 }
106
107
108
109
110 @Override
111 public boolean prepareAction() throws Exception {
112
113 if (!getContext().isSynchroEnabled() && getConfig().isSynchronizationUsingServer()) {
114 getContext().getErrorHelper().showWarningDialog(t("dali.synchro.unavailable"));
115 return false;
116 }
117
118
119 if (!referentialOnly && getContext().isAuthenticatedAsLocalUser()) {
120 getContext().getErrorHelper().showWarningDialog(t("dali.error.authenticate.accessDenied"));
121 return false;
122 }
123
124 return super.prepareAction();
125 }
126
127
128
129
130 @Override
131 public void doAction() throws Exception {
132
133 getSynchroUIContext().getProgressionModel().clear();
134 getSynchroUIContext().setDirection(SynchroDirection.IMPORT);
135 getSynchroUIContext().loadImportContext();
136 boolean isSynchronizationUsingServer = getConfig().isSynchronizationUsingServer();
137
138
139 if (getSynchroUIContext().getStatus() == SynchroProgressionStatus.FAILED) {
140 getSynchroHandler().report(t("quadrige3.synchro.report.failed"));
141 return;
142 }
143
144
145 if (isSynchronizationUsingServer && getSynchroUIContext().getStatus() == SynchroProgressionStatus.SUCCESS && getSynchroUIContext().getWorkingDirectory() != null) {
146
147 getSynchroHandler().showValidApplyPopup();
148 return;
149 }
150
151
152
153 if (isSynchronizationUsingServer
154 && getSynchroUIContext().isRunningStatus()) {
155 if (getSynchroUIContext().getImportJobId() != null) {
156 nextSynchroAction = getContext().getActionFactory().createNonBlockingUIAction(getSynchroHandler(), ImportSynchroStartAction.class);
157 return;
158 } else {
159
160 getSynchroUIContext().setImportJobId(null);
161 getSynchroUIContext().setStatus(SynchroProgressionStatus.NOT_STARTED);
162 }
163 }
164
165
166 getSynchroUIContext().setImportReferential(true);
167 getSynchroUIContext().setImportData(!referentialOnly);
168
169
170 if (!referentialOnly) {
171
172
173 if (isSynchronizationUsingServer
174 && !getContext().getProgramStrategyService().hasRemoteAccessRightOnProgram(getContext().getAuthenticationInfo())) {
175 referentialOnly = true;
176 } else {
177
178
179 Set<String> programCodes = getSynchroUIContext().getImportDataProgramCodes();
180 ProgramSelectUI programSelectUI = new ProgramSelectUI((ApplicationUI) getUI(), StatusFilter.ACTIVE, programCodes, false, true);
181
182 handler.openDialog(programSelectUI, t("dali.action.synchro.import.programCodes.title"), new Dimension(800, 400));
183
184 List<ProgramDTO> programs = programSelectUI.getModel().getSelectedPrograms();
185
186
187 if (CollectionUtils.isEmpty(programs)) {
188 getSynchroUIContext().setImportData(false);
189 referentialOnly = true;
190 }
191
192
193 else {
194 programCodes = Sets.newHashSet(DaliBeans.<String, ProgramDTO>collectProperties(programs, ProgramDTO.PROPERTY_CODE));
195 getSynchroUIContext().setImportDataProgramCodes(programCodes);
196 }
197
198
199 getSynchroUIContext().setImportPhoto(programSelectUI.getModel().isEnablePhoto());
200
201 }
202 }
203
204 getSynchroUIContext().saveImportContext();
205
206
207 if (!referentialOnly && getConfig().isSynchronizationPeriodSelectionEnable()) {
208
209
210 getSynchroHandler().showDateChoicePopup();
211 return;
212 }
213
214
215 if (isSynchronizationUsingServer) {
216
217 ImportSynchroCheckAction checkAction = getContext().getActionFactory().createNonBlockingUIAction(getSynchroHandler(), ImportSynchroCheckAction.class);
218 checkAction.setCheckReferentialOnly(referentialOnly);
219 checkAction.setSilentIfNoUpdate(silentIfNoUpdate);
220 checkAction.setSilentIfUpdate(silentIfUpdate);
221 nextSynchroAction = checkAction;
222 }
223
224
225 else {
226
227 ImportSynchroDirectAction importAction = getActionFactory().createLogicAction(getSynchroHandler(), ImportSynchroDirectAction.class);
228 if (importAction.prepareAction()) {
229 getActionEngine().runInternalAction(importAction);
230 }
231 }
232 }
233
234
235
236
237 @Override
238 public void postSuccessAction() {
239 super.postSuccessAction();
240
241 getSynchroUIContext().saveImportContext();
242
243 if (nextSynchroAction != null && getConfig().isSynchronizationUsingServer()) {
244 nextSynchroAction.execute();
245 }
246
247 }
248
249
250
251
252 @Override
253 protected void releaseAction() {
254 super.releaseAction();
255 referentialOnly = false;
256 silentIfNoUpdate = false;
257 silentIfUpdate = false;
258 nextSynchroAction = null;
259 }
260 }