1 package fr.ifremer.quadrige2.ui.swing.common.content.db;
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.quadrige2.core.service.ClientServiceLocator;
28 import fr.ifremer.quadrige2.core.service.persistence.PersistenceService;
29 import fr.ifremer.quadrige2.ui.swing.common.DialogHelper;
30 import fr.ifremer.quadrige2.ui.swing.common.Screen;
31 import fr.ifremer.quadrige2.ui.swing.common.action.AbstractChangeScreenAction;
32 import fr.ifremer.quadrige2.ui.swing.common.content.AbstractMainUIHandler;
33 import fr.ifremer.quadrige2.ui.swing.common.content.login.AuthenticationAction;
34 import fr.ifremer.quadrige2.ui.swing.common.model.ProgressionModel;
35 import fr.ifremer.quadrige2.ui.swing.common.synchro.action.ImportReferentialSynchroAtOnceAction;
36 import org.apache.commons.logging.Log;
37 import org.apache.commons.logging.LogFactory;
38 import org.nuiton.jaxx.application.ApplicationBusinessException;
39 import org.nuiton.jaxx.application.swing.action.ApplicationUIAction;
40 import org.nuiton.updater.ApplicationInfo;
41 import org.nuiton.util.DateUtil;
42 import org.nuiton.version.Version;
43
44 import javax.swing.JOptionPane;
45 import java.io.File;
46 import java.util.Date;
47
48 import static org.nuiton.i18n.I18n.n;
49 import static org.nuiton.i18n.I18n.t;
50
51
52
53
54
55
56 public class OpenDbAction extends AbstractChangeScreenAction {
57
58
59
60
61 private static final Log LOG = LogFactory.getLog(OpenDbAction.class);
62
63 static {
64 n("quadrige2.dbManager.action.importDb.step.open");
65 n("quadrige2.dbManager.action.openDb.step.open");
66 n("quadrige2.dbManager.action.importDb.couldNotOpen");
67 n("quadrige2.dbManager.action.openDb.couldNotOpen");
68 n("quadrige2.dbManager.action.importDb.step.checkSchemaVersion");
69 n("quadrige2.dbManager.action.openDb.step.checkSchemaVersion");
70 n("quadrige2.dbManager.action.importDb.step.close");
71 n("quadrige2.dbManager.action.openDb.step.close");
72 n("quadrige2.dbManager.action.importDb.step.will.migrateSchema");
73 n("quadrige2.dbManager.action.openDb.step.will.migrateSchema");
74 }
75
76 protected Version dbVersion;
77
78 protected Version applicationVersion;
79
80 protected File file;
81
82 protected boolean updateSchema;
83
84 protected boolean closeDb;
85
86 protected String jdbcUrl;
87
88 protected ApplicationInfo updateDbVersion;
89
90
91
92
93 protected boolean isAfterImportDb;
94
95
96
97
98
99
100 public OpenDbAction(AbstractMainUIHandler handler) {
101 super(handler, true, Screen.HOME);
102
103 setActionDescription(t("quadrige2.dbManager.action.openDb.tip"));
104 }
105
106
107
108
109
110
111 public void setAfterImportDb(boolean isAfterImportDb) {
112 this.isAfterImportDb = isAfterImportDb;
113 }
114
115
116 @Override
117 public boolean prepareAction() throws Exception {
118
119 jdbcUrl = null;
120 dbVersion = applicationVersion = null;
121 file = null;
122 closeDb = updateSchema = false;
123 updateDbVersion = null;
124
125 return super.prepareAction();
126 }
127
128
129 @Override
130 public void doAction() throws Exception {
131
132 jdbcUrl = getConfig().getJdbcUrl();
133
134 if (LOG.isDebugEnabled()) {
135 LOG.debug("Will open db: " + jdbcUrl);
136 }
137
138 String actionName = isAfterImportDb ? "importDb" : "openDb";
139
140
141 ProgressionModel progressionModel = new ProgressionModel();
142 setProgressionModel(progressionModel);
143 progressionModel.setTotal(3);
144
145
146
147
148 PersistenceService persistenceService;
149
150 progressionModel.increments(t("quadrige2.dbManager.action." + actionName + ".step.open", jdbcUrl));
151 try {
152 getContext().setDbExist(true);
153 getContext().openPersistenceService(isAfterImportDb);
154 persistenceService = ClientServiceLocator.instance().getPersistenceService();
155
156 } catch (Exception e) {
157
158 if (LOG.isErrorEnabled()) {
159 LOG.error("Could not open db", e);
160 }
161
162 getContext().closePersistenceService();
163
164
165 throw new ApplicationBusinessException(t("quadrige2.dbManager.action." + actionName + ".couldNotOpen"), e);
166 }
167
168
169
170
171
172 progressionModel.increments(t("quadrige2.dbManager.action." + actionName + ".step.checkSchemaVersion"));
173
174 dbVersion = persistenceService.getDbVersion();
175 if (LOG.isDebugEnabled()) {
176 LOG.debug("Detected database version: " + (dbVersion == null ? "no version" : dbVersion));
177 }
178 applicationVersion = persistenceService.getApplicationVersion();
179
180 if (LOG.isDebugEnabled()) {
181 LOG.debug("Detected schema version need for application: " + (applicationVersion == null ? "no version" : applicationVersion));
182 }
183
184 if (dbVersion == null) {
185
186
187 displayWarningMessage(t("quadrige2.dbManager.title.schema.toUpdate"), t("quadrige2.dbManager.action.upgradeDb.schema.version.not.found"));
188 closeDb = true;
189
190 } else if (dbVersion.equals(applicationVersion)) {
191
192
193 if (LOG.isInfoEnabled()) {
194 LOG.info("Database schema is up-to-date at version: " + dbVersion);
195 }
196 } else if (dbVersion.compareTo(applicationVersion) < 0) {
197
198
199
200
201 int i = getContext().getDialogHelper().showConfirmDialog(
202 t("quadrige2.dbManager.action.upgradeDb.schema.to.update.message", dbVersion, applicationVersion),
203 t("quadrige2.dbManager.title.schema.toUpdate"),
204 DialogHelper.UPDATE_DB_CANCEL_OPTION);
205 boolean continueAction = i == JOptionPane.OK_OPTION;
206
207 if (continueAction) {
208 if (isAfterImportDb || getContext().isDbJustInstalled()) {
209
210
211 updateSchema = true;
212 } else {
213
214 String date = DateUtil.formatDate(new Date(), "yyy-MM-dd");
215
216
217 file = saveFile(
218 getConfig().getDbBackupDirectory(),
219 String.format("%s-db-%s", getConfig().getApplicationName(), date),
220 "zip",
221 t("quadrige2.dbManager.title.choose.dbBackupFile"),
222 t("quadrige2.dbManager.action.chooseDbBackupFile"),
223 "^.*\\.zip", t("quadrige2.common.file.zip")
224 );
225
226 if (file == null) {
227
228
229 closeDb = true;
230
231 displayWarningMessage(
232 t("quadrige2.dbManager.title.choose.dbBackupFile"),
233 t("quadrige2.dbManager.action.upgradeDb.no.backup.db.choosen"));
234 } else {
235
236 updateSchema = true;
237 }
238 }
239 } else {
240
241
242 closeDb = true;
243 }
244 } else {
245
246
247 if (isAfterImportDb) {
248
249 int i = getContext().getDialogHelper().showConfirmDialog(
250 t("quadrige2.dbManager.action.upgradeDb.schema.too.high", dbVersion, applicationVersion),
251 t("quadrige2.dbManager.title.schema.toUpdate"),
252 DialogHelper.LOAD_DB_CANCEL_OPTION);
253 boolean continueAction = i == JOptionPane.OK_OPTION;
254
255 if (!continueAction) {
256
257 closeDb = true;
258 }
259 } else {
260 displayWarningMessage(
261 t("quadrige2.dbManager.title.schema.toUpdate"),
262 t("quadrige2.dbManager.action.upgradeDb.schema.not.update.message", dbVersion, applicationVersion)
263 );
264 }
265 }
266 if (closeDb) {
267
268
269
270
271 progressionModel.increments(t("quadrige2.dbManager.action." + actionName + ".step.close"));
272 getActionEngine().runInternalAction(getHandler(), CloseDbAction.class);
273
274 if (!isAfterImportDb) {
275 setScreen(Screen.MANAGE_DB);
276 super.doAction();
277 }
278 return;
279 }
280
281 if (updateSchema) {
282
283
284 progressionModel.adaptTotal(progressionModel.getTotal() + ExportDbAction.TOTAL_STEP + 1);
285
286 if (!isAfterImportDb && file != null) {
287
288
289
290 ApplicationUIAction<ExportDbAction> backupAction
291 = getActionFactory().createUIAction(getHandler(), ExportDbAction.class);
292 backupAction.getLogicAction().setProgressionModel(getProgressionModel());
293 backupAction.getLogicAction().setFile(file);
294 getActionEngine().runInternalAction(backupAction.getLogicAction());
295 }
296
297
298
299
300 String message = t("quadrige2.dbManager.action." + actionName + ".step.will.migrateSchema", dbVersion, applicationVersion);
301 progressionModel.increments(message);
302 sendMessage(message);
303
304 ClientServiceLocator.instance().getPersistenceService().updateSchema();
305
306 sendMessage(t("quadrige2.flash.info.db.schema.updated", dbVersion, applicationVersion));
307 }
308
309
310
311
312 if (getContext().isPersistenceLoaded() && !getContext().isAuthenticated()) {
313
314 progressionModel.increments(t("quadrige2.dbManager.action.openDb.step.authenticate"));
315
316 AuthenticationAction action = new AuthenticationAction(getHandler());
317 getActionEngine().runInternalAction(action);
318
319 }
320
321
322
323
324 if (getContext().isSynchroEnabled() && getContext().isDbJustInstalled()) {
325 ImportReferentialSynchroAtOnceAction importAction = getContext().getActionFactory().createLogicAction(getHandler(), ImportReferentialSynchroAtOnceAction.class);
326 getContext().getActionEngine().runInternalAction(importAction);
327 }
328
329
330
331
332 progressionModel.increments(t("quadrige2.dbManager.action.openDb.step.check.dbContext", dbVersion, applicationVersion));
333
334 if (LOG.isDebugEnabled()) {
335 LOG.debug("Check db context");
336 }
337
338 getContext().setDbJustImportedFromFile(isAfterImportDb);
339
340
341 getContext().checkDbContext(progressionModel);
342
343
344
345
346
347
348
349
350
351
352 super.doAction();
353 }
354
355
356 @Override
357 public void postSuccessAction() {
358
359 getHandler().reloadDbManagerText();
360
361
362 getHandler().changeTitle();
363
364 if (!isAfterImportDb) {
365 if (closeDb) {
366 sendMessage(t("quadrige2.flash.info.db.not.opened", jdbcUrl));
367 } else {
368 sendMessage(t("quadrige2.flash.info.db.opened", jdbcUrl));
369 }
370 }
371 }
372
373
374 @Override
375 public void postFailedAction(Throwable error) {
376
377 getHandler().reloadDbManagerText();
378
379 super.postFailedAction(error);
380 }
381 }