1 package fr.ifremer.tutti.ui.swing.content.db.actions;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 import com.google.common.base.Preconditions;
26 import fr.ifremer.tutti.persistence.ProgressionModel;
27 import fr.ifremer.tutti.persistence.TuttiPersistence;
28 import fr.ifremer.tutti.persistence.service.UpdateSchemaContextSupport;
29 import fr.ifremer.tutti.service.PersistenceService;
30 import fr.ifremer.tutti.ui.swing.content.actions.AbstractMainUITuttiAction;
31 import fr.ifremer.tutti.ui.swing.content.MainUIHandler;
32 import fr.ifremer.tutti.ui.swing.content.actions.OpenHomeScreenAction;
33 import fr.ifremer.tutti.ui.swing.util.AbstractTuttiUIHandler;
34 import org.apache.commons.logging.Log;
35 import org.apache.commons.logging.LogFactory;
36 import org.nuiton.jaxx.application.ApplicationBusinessException;
37 import org.nuiton.version.Version;
38
39 import javax.swing.JOptionPane;
40 import java.io.File;
41
42 import static org.nuiton.i18n.I18n.t;
43
44
45
46
47
48
49
50 public class ImportDbAction extends AbstractMainUITuttiAction {
51
52
53 private static final Log log = LogFactory.getLog(ImportDbAction.class);
54
55 protected File importFile;
56
57 protected String jdbcUrl;
58
59 protected PersistenceService.ImportStructureType importStructureType;
60
61 private UpdateSchemaContext updateSchemaContext;
62
63 public ImportDbAction(MainUIHandler handler) {
64 super(handler, true);
65 setActionDescription(t("tutti.dbManager.action.importDb.tip"));
66 }
67
68 public void setImportFile(File importFile) {
69 this.importFile = importFile;
70 }
71
72
73 public class UpdateSchemaContext extends UpdateSchemaContextSupport {
74
75 private boolean closeDb;
76
77 @Override
78 public void init(Version schemaVersion, Version schemaVersionIfUpdate) {
79
80 super.init(schemaVersion, schemaVersionIfUpdate);
81
82 if (isSchemaVersionTooHigh()) {
83
84
85 String message = t("tutti.dbManager.action.upgradeDb.schema.too.high", schemaVersion, schemaVersionIfUpdate);
86
87 String htmlMessage = String.format(
88 AbstractTuttiUIHandler.CONFIRMATION_FORMAT,
89 message,
90 t("tutti.dbManager.action.upgradeDb.schema.too.high.help"));
91 int i = JOptionPane.showConfirmDialog(
92 handler.getTopestUI(),
93 htmlMessage,
94 t("tutti.dbManager.title.schema.toupdate"),
95 JOptionPane.OK_CANCEL_OPTION,
96 JOptionPane.QUESTION_MESSAGE);
97 boolean continueAction = i == JOptionPane.OK_OPTION;
98
99 if (!continueAction) {
100
101
102 closeDb = true;
103 }
104 }
105 }
106
107 @Override
108 protected boolean askUserToMigrate(Version schemaVersion, Version schemaVersionIfUpdate) {
109
110
111
112
113 String message = t("tutti.dbManager.action.upgradeDb.schema.to.update.message", schemaVersion, schemaVersionIfUpdate);
114
115 String htmlMessage = String.format(
116 AbstractTuttiUIHandler.CONFIRMATION_FORMAT,
117 message,
118 t("tutti.dbManager.action.upgradeDb.schema.to.update.message.help"));
119 int i = JOptionPane.showConfirmDialog(
120 handler.getTopestUI(),
121 htmlMessage,
122 t("tutti.dbManager.title.schema.toupdate"),
123 JOptionPane.OK_CANCEL_OPTION,
124 JOptionPane.QUESTION_MESSAGE);
125 boolean continueAction = i == JOptionPane.OK_OPTION;
126
127 if (continueAction) {
128
129
130 continueAction = true;
131 } else {
132
133
134 closeDb = true;
135 }
136
137
138 return continueAction;
139
140 }
141
142 public boolean isCloseDb() {
143 return closeDb;
144 }
145
146 }
147
148 @Override
149 public boolean prepareAction() throws Exception {
150
151 importStructureType = null;
152 jdbcUrl = null;
153 updateSchemaContext = null;
154
155 boolean doAction = super.prepareAction();
156
157 if (doAction) {
158
159 jdbcUrl = getConfig().getJdbcUrl();
160
161 if (importFile == null) {
162
163
164 importFile = chooseFile(
165 t("tutti.dbManager.title.choose.dbImportFile"),
166 t("tutti.dbManager.action.chooseDbFile"),
167 "^.*\\.zip", t("tutti.common.file.zip")
168 );
169
170 if (importFile == null) {
171
172 displayWarningMessage(
173 t("tutti.dbManager.title.choose.dbImportFile"),
174 t("tutti.dbManager.action.importdb.no.import.file.choosen")
175 );
176
177 doAction = false;
178 }
179 }
180
181 if (doAction) {
182
183 ProgressionModel progressionModel = new ProgressionModel();
184 progressionModel.setTotal(3);
185 setProgressionModel(progressionModel);
186
187 importStructureType =
188 getContext().getPersistenceService().checkImportStructure(importFile);
189 }
190 }
191 return doAction;
192 }
193
194 @Override
195 public void releaseAction() {
196 importFile = null;
197 super.releaseAction();
198 }
199
200 @Override
201 public void doAction() throws Exception {
202 Preconditions.checkNotNull(importFile);
203
204 if (log.isInfoEnabled()) {
205 log.info("Will import db: " + importFile);
206 }
207
208 ProgressionModel progressionModel = getProgressionModel();
209
210
211
212
213
214 progressionModel.increments(t("tutti.importDb.step.unzipArchive"));
215
216 getContext().getPersistenceService().importDb(importStructureType, importFile);
217
218
219
220
221
222 progressionModel.increments(t("tutti.importDb.step.openDb", jdbcUrl));
223 try {
224 getContext().setDbExist(true);
225 getContext().openPersistenceService();
226 } catch (Exception e) {
227
228 if (log.isErrorEnabled()) {
229 log.error("Could not open db", e);
230 }
231
232 getContext().closePersistenceService();
233
234
235 throw new ApplicationBusinessException(t("tutti.dbManager.action.importdb.couldNotOpen"), e);
236 }
237
238
239
240
241
242 progressionModel.increments(t("tutti.importDb.step.checkSchemaVersion"));
243
244 TuttiPersistence persistenceService = getContext().getPersistenceService();
245
246 updateSchemaContext = new UpdateSchemaContext();
247 persistenceService.prepareUpdateSchemaContext(updateSchemaContext);
248
249 Version schemaVersion = updateSchemaContext.getSchemaVersion();
250
251 if (log.isInfoEnabled()) {
252 log.info("Detected database version: " + schemaVersion);
253 }
254 Version schemaVersionIfUpdate = updateSchemaContext.getSchemaVersionIfUpdate();
255
256 if (log.isInfoEnabled()) {
257 log.info("Detected schema application version:" + schemaVersionIfUpdate);
258 }
259
260
261 if (updateSchemaContext.isCloseDb()) {
262
263
264
265
266
267 progressionModel.increments(t("tutti.importDb.step.closeDb"));
268 getActionEngine().runInternalAction(handler, CloseDbAction.class);
269
270 return;
271 }
272
273 if (updateSchemaContext.isWillUpdate()) {
274
275
276 progressionModel.adaptTotal(progressionModel.getTotal() + 2);
277
278
279
280
281
282 String message = t("tutti.importDb.step.will.migrateSchema", schemaVersion, schemaVersionIfUpdate);
283
284 progressionModel.increments(message);
285 sendMessage(message);
286 getContext().getPersistenceService().updateSchema();
287
288 sendMessage(t("tutti.flash.info.db.schema.updated", schemaVersion, schemaVersionIfUpdate));
289 }
290
291
292
293
294
295 String message = t("tutti.importDb.step.check.dbContext", schemaVersion, schemaVersionIfUpdate);
296
297 progressionModel.increments(message);
298
299 if (log.isDebugEnabled()) {
300 log.debug("Check db context");
301 }
302 getContext().checkDbContext();
303
304
305
306
307
308 getActionEngine().runInternalAction(handler, OpenHomeScreenAction.class);
309 }
310
311 @Override
312 public void postSuccessAction() {
313 handler.reloadDbManagerText();
314
315 super.postSuccessAction();
316
317 if (updateSchemaContext.isCloseDb()) {
318 sendMessage(t("tutti.flash.info.db.imported.but.closed", jdbcUrl));
319 } else {
320 sendMessage(t("tutti.flash.info.db.imported", jdbcUrl));
321 }
322
323
324 getUI().getHandler().changeTitle();
325 }
326
327 @Override
328 public void postFailedAction(Throwable error) {
329 handler.reloadDbManagerText();
330 super.postFailedAction(error);
331 }
332
333 }