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 com.google.common.base.Preconditions;
27 import fr.ifremer.quadrige3.core.dao.technical.Daos;
28 import fr.ifremer.quadrige3.core.exception.QuadrigeBusinessException;
29 import fr.ifremer.quadrige3.core.service.persistence.PersistenceServiceHelper;
30 import fr.ifremer.quadrige3.ui.swing.callback.DatabaseUpdaterCallBack;
31 import fr.ifremer.quadrige3.ui.swing.action.AbstractMainUIAction;
32 import fr.ifremer.quadrige3.ui.swing.content.AbstractMainUIHandler;
33 import org.apache.commons.io.FileUtils;
34 import org.apache.commons.logging.Log;
35 import org.apache.commons.logging.LogFactory;
36 import org.nuiton.updater.ApplicationUpdater;
37
38 import java.io.File;
39
40 import static org.nuiton.i18n.I18n.t;
41
42
43
44
45
46
47 public class InstallDbAction extends AbstractMainUIAction {
48
49
50
51
52 private static final Log LOG = LogFactory.getLog(InstallDbAction.class);
53
54 private boolean mustBackup;
55 private BackupDbAction backupDbAction;
56 private File backupFile;
57
58
59
60
61
62
63 public InstallDbAction(AbstractMainUIHandler handler) {
64 super(handler, true);
65 setActionDescription(t("quadrige3.dbManager.action.installDb.tip"));
66 }
67
68
69
70
71 @Override
72 public boolean prepareAction() throws Exception {
73 boolean doAction = super.prepareAction();
74 mustBackup = getContext().isDbExist();
75
76 if (doAction) {
77
78
79 doAction = getContext().checkUpdateReachable(getConfig().getInstallDbUrl(), true);
80 }
81
82 if (doAction && mustBackup) {
83
84 displayInfoMessage(
85 t("quadrige3.dbManager.title.backup.db"),
86 t("quadrige3.dbManager.action.installDb.backup.db")
87 );
88
89 backupDbAction = getActionFactory().createLogicAction(getHandler(), BackupDbAction.class);
90 backupDbAction.prepareAction();
91 backupFile = backupDbAction.getBackupFile();
92
93 if (backupFile == null) {
94
95 displayWarningMessage(
96 t("quadrige3.dbManager.title.backup.db"),
97 t("quadrige3.dbManager.action.installDb.no.backup.db.choosen")
98 );
99
100 doAction = false;
101 }
102
103 }
104
105 return doAction;
106 }
107
108
109
110
111 @Override
112 public void doAction() {
113
114 if (mustBackup) {
115 Preconditions.checkNotNull(backupDbAction);
116 Preconditions.checkNotNull(backupFile);
117 }
118
119 createProgressionUIModel();
120
121
122 if (mustBackup) {
123
124 getActionEngine().runInternalAction(backupDbAction);
125
126 getContext().closePersistenceService();
127 getContext().clearDbContext();
128
129 PersistenceServiceHelper.deleteDatabaseDirectory(getProgressionUIModel());
130 getContext().setDbExist(false);
131 }
132
133
134
135 File dbVersionFile = new File(getConfig().getDbDirectory(), Daos.DB_VERSION_FILE);
136 if (dbVersionFile.exists()) {
137 FileUtils.deleteQuietly(dbVersionFile);
138 }
139
140
141
142
143 File current = getConfig().getDataDirectory();
144 String url = getConfig().getInstallDbUrl();
145
146 if (LOG.isInfoEnabled()) {
147 LOG.info(String.format("Try to install / update db (current data location: %s), using update url: %s", current, url));
148 }
149
150 File dest = new File(getConfig().getBaseDirectory(), "NEW");
151
152 getProgressionUIModel().setTotal(0);
153 getProgressionUIModel().setMessage(t("quadrige3.dbManager.action.upgradeDb.check"));
154 DatabaseUpdaterCallBack callback
155 = new DatabaseUpdaterCallBack(this, getProgressionUIModel());
156 ApplicationUpdater up = new ApplicationUpdater();
157 up.update(url,
158 current,
159 dest,
160 false,
161 callback,
162 getProgressionUIModel());
163
164 if (!callback.isDbInstalled()) {
165 throw new QuadrigeBusinessException(t("quadrige3.dbManager.action.installDb.error"));
166 }
167
168 getProgressionUIModel().setMessage(t("quadrige3.dbManager.action.upgradeDb.opening"));
169
170
171 PersistenceServiceHelper.migrateDbName();
172
173 getContext().setDbExist(true);
174 getContext().setDbJustInstalled(true);
175
176
177
178
179 getActionEngine().runInternalAction(getHandler(), OpenDbAction.class);
180 }
181
182 @Override
183 protected void releaseAction() {
184 backupFile = null;
185 backupDbAction = null;
186 super.releaseAction();
187 }
188
189 }