1 package fr.ifremer.quadrige3.ui.swing.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 import fr.ifremer.quadrige3.core.dao.technical.Assert;
27 import fr.ifremer.quadrige3.core.service.persistence.PersistenceServiceHelper;
28 import fr.ifremer.quadrige3.ui.swing.action.AbstractMainUIAction;
29 import fr.ifremer.quadrige3.ui.swing.content.AbstractMainUIHandler;
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32
33 import java.io.File;
34
35 import static org.nuiton.i18n.I18n.t;
36
37
38
39
40
41
42 public class RestoreDbAction extends AbstractMainUIAction {
43
44
45
46
47 private static final Log LOG = LogFactory.getLog(RestoreDbAction.class);
48
49 private boolean mustBackup;
50 private BackupDbAction backupDbAction;
51 private File backupFile;
52 private File restoreFile;
53 private String jdbcUrl;
54
55
56
57
58
59
60 public RestoreDbAction(AbstractMainUIHandler handler) {
61 super(handler, true);
62 setActionDescription(t("quadrige3.dbManager.action.restoreDb.tip"));
63 }
64
65
66
67
68
69
70 public void setRestoreFile(File restoreFile) {
71 this.restoreFile = restoreFile;
72 }
73
74
75
76
77 @Override
78 public boolean prepareAction() throws Exception {
79
80 boolean doAction = super.prepareAction();
81 mustBackup = getContext().isDbExist();
82
83 if (doAction) {
84
85 if (mustBackup) {
86
87 displayInfoMessage(
88 t("quadrige3.dbManager.title.backup.db"),
89 t("quadrige3.dbManager.action.restoreDb.backup.db")
90 );
91
92 backupDbAction = getActionFactory().createLogicAction(getHandler(), BackupDbAction.class);
93 backupDbAction.prepareAction();
94 backupFile = backupDbAction.getBackupFile();
95
96 if (backupFile == null) {
97
98 displayWarningMessage(
99 t("quadrige3.dbManager.title.backup.db"),
100 t("quadrige3.dbManager.action.restoreDb.no.backup.db.choosen")
101 );
102
103 doAction = false;
104 }
105
106 }
107
108 if (doAction && restoreFile == null) {
109
110
111 restoreFile = chooseFile(
112 t("quadrige3.dbManager.title.choose.dbImportFile"),
113 t("quadrige3.dbManager.action.chooseDbFile"),
114 "^.*\\.zip", t("quadrige3.common.file.zip")
115 );
116
117 if (restoreFile == null) {
118
119 displayWarningMessage(
120 t("quadrige3.dbManager.title.choose.dbImportFile"),
121 t("quadrige3.dbManager.action.restoreDb.no.import.file.choosen")
122 );
123
124 doAction = false;
125 }
126 }
127 }
128 return doAction;
129 }
130
131
132
133
134 @Override
135 public void doAction() throws Exception {
136 jdbcUrl = getConfig().getJdbcUrl();
137 Assert.notNull(restoreFile);
138 if (mustBackup) {
139 Assert.notNull(backupDbAction);
140 Assert.notNull(backupFile);
141 }
142
143 if (LOG.isInfoEnabled()) {
144 LOG.info("Will import db: " + restoreFile);
145 }
146
147 createProgressionUIModel();
148
149
150 if (mustBackup) {
151
152 getActionEngine().runInternalAction(backupDbAction);
153
154 getContext().closePersistenceService();
155 getContext().clearDbContext();
156
157 PersistenceServiceHelper.deleteDatabaseDirectory(getProgressionUIModel());
158 getContext().setDbExist(false);
159 }
160
161
162 getProgressionUIModel().setMessage(t("quadrige3.dbManager.action.restoreDb.step.unzipArchive", restoreFile));
163 PersistenceServiceHelper.restoreDatabase(restoreFile.toPath(), getProgressionUIModel());
164
165
166 OpenDbAction openDbAction = getActionFactory().createLogicAction(getHandler(), OpenDbAction.class);
167 openDbAction.setAfterImportDb(true);
168 openDbAction.prepareAction();
169 getActionEngine().runInternalAction(openDbAction);
170
171 }
172
173
174
175
176 @Override
177 public void postSuccessAction() {
178 getHandler().reloadDbManagerText();
179
180 super.postSuccessAction();
181
182 sendMessage(t("quadrige3.flash.info.db.imported", jdbcUrl));
183
184
185 getHandler().changeTitle();
186 }
187
188
189
190
191 @Override
192 public void postFailedAction(Throwable error) {
193 getHandler().reloadDbManagerText();
194 super.postFailedAction(error);
195 }
196
197
198
199
200 @Override
201 public void releaseAction() {
202 restoreFile = backupFile = null;
203 backupDbAction = null;
204 super.releaseAction();
205 }
206
207 }