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.exception.QuadrigeTechnicalException;
28 import fr.ifremer.quadrige3.core.security.AuthenticationInfo;
29 import fr.ifremer.quadrige3.core.service.ClientServiceLocator;
30 import fr.ifremer.quadrige3.synchro.service.client.SynchroRestClientService;
31 import fr.ifremer.quadrige3.ui.swing.action.AbstractMainUIAction;
32 import fr.ifremer.quadrige3.ui.swing.content.AbstractMainUIHandler;
33 import fr.ifremer.quadrige3.ui.swing.model.ProgressionUIModel;
34 import fr.ifremer.quadrige3.ui.swing.synchro.SynchroDirection;
35 import fr.ifremer.quadrige3.ui.swing.synchro.SynchroUIContext;
36 import fr.ifremer.quadrige3.ui.swing.synchro.SynchroUIHandler;
37 import org.nuiton.i18n.I18n;
38
39
40
41
42
43
44
45 public class ImportReferentialSynchroAtOnceAction extends AbstractMainUIAction {
46
47 private boolean forceClearUpdateDateCache = false;
48
49
50
51
52
53
54 public ImportReferentialSynchroAtOnceAction(AbstractMainUIHandler handler) {
55 super(handler, true);
56 }
57
58 public boolean isForceClearUpdateDateCache() {
59 return forceClearUpdateDateCache;
60 }
61
62 public void setForceClearUpdateDateCache(boolean forceClearUpdateDateCache) {
63 this.forceClearUpdateDateCache = forceClearUpdateDateCache;
64 }
65
66
67 @Override
68 public void doAction() throws Exception {
69
70
71 if (forceClearUpdateDateCache && getConfig().isSynchronizationUsingServer()) {
72 clearReferentialUpdateDateCacheOnServer();
73 }
74
75
76 ImportSynchroCheckAction checkAction = getContext().getActionFactory().createNonBlockingUIAction(getSynchroHandler(), ImportSynchroCheckAction.class);
77 checkAction.setCheckReferentialOnly(true);
78 checkAction.executeAndWait();
79 boolean needReferentialImport = getSynchroUIContext().getSynchroImportContext().isWithReferential();
80
81
82
83 if (!needReferentialImport) {
84 return;
85 }
86
87
88 SynchroDirection previousDirection = getSynchroUIContext().getDirection();
89 getSynchroUIContext().setDirection(SynchroDirection.IMPORT);
90
91
92 if (getConfig().isSynchronizationUsingServer()) {
93
94 ProgressionUIModel previousProgressionModel = getProgressionUIModel();
95 setProgressionUIModel(getSynchroUIContext().getProgressionModel());
96 getProgressionUIModel().setTotal(50);
97
98
99 AbstractSynchroAction action = getContext().getActionFactory().createNonBlockingUIAction(getSynchroHandler(), ImportSynchroStartAction.class);
100 action.executeAndWait();
101
102
103 action = getContext().getActionFactory().createNonBlockingUIAction(getSynchroHandler(), ImportSynchroDownloadAction.class);
104 action.executeAndWait();
105
106
107 ImportSynchroApplyAction importAction = getActionFactory().createLogicAction(handler, ImportSynchroApplyAction.class);
108 importAction.setSilent(true);
109 importAction.setSkipScreenReload(true);
110 if (importAction.prepareAction()) {
111 getActionEngine().runInternalAction(importAction);
112 }
113
114 setProgressionUIModel(previousProgressionModel);
115 } else {
116
117 ImportSynchroDirectAction importAction = getActionFactory().createLogicAction(getSynchroHandler(), ImportSynchroDirectAction.class);
118 if (importAction.prepareAction()) {
119 getActionEngine().runInternalAction(importAction);
120 }
121 }
122
123
124 getSynchroUIContext().setDirection(previousDirection);
125 }
126
127 private SynchroUIContext getSynchroUIContext() {
128 return getContext().getSynchroContext();
129 }
130
131 private SynchroUIHandler getSynchroHandler() {
132 return getContext().getSynchroHandler();
133 }
134
135 private void clearReferentialUpdateDateCacheOnServer() {
136
137 SynchroRestClientService service = ClientServiceLocator.instance().getSynchroRestClientService();
138 try {
139
140
141 AuthenticationInfo authenticationInfo = !getContext().isAuthenticated() || getContext().isAuthenticatedAsLocalUser()
142 ? null
143 : getContext().getAuthenticationInfo();
144
145
146 service.clearReferentialUpdateDateCache(authenticationInfo);
147
148 } catch (QuadrigeTechnicalException e) {
149 throw new SynchroException(I18n.t("quadrige3.error.synchro.connect", e.getMessage()), e);
150 }
151 }
152 }