View Javadoc
1   package fr.ifremer.quadrige3.ui.swing.content.db;
2   
3   /*-
4    * #%L
5    * Quadrige3 Core :: Quadrige3 UI Common
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2017 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (at your option) any later version.
15   *
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   *
21   * You should have received a copy of the GNU Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
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   * To install (or reinstall) a db from last network one.
44   *
45   * @since 2.4
46   */
47  public class InstallDbAction extends AbstractMainUIAction {
48  
49      /**
50       * Logger.
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       * <p>Constructor for InstallDbAction.</p>
60       *
61       * @param handler a {@link AbstractMainUIHandler} object.
62       */
63      public InstallDbAction(AbstractMainUIHandler handler) {
64          super(handler, true);
65          setActionDescription(t("quadrige3.dbManager.action.installDb.tip"));
66      }
67  
68      /**
69       * {@inheritDoc}
70       */
71      @Override
72      public boolean prepareAction() throws Exception {
73          boolean doAction = super.prepareAction();
74          mustBackup = getContext().isDbExist();
75  
76          if (doAction) {
77  
78              // check db url is reachable
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      * {@inheritDoc}
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         // perform backup
122         if (mustBackup) {
123             // execute backup action
124             getActionEngine().runInternalAction(backupDbAction);
125             // close database
126             getContext().closePersistenceService();
127             getContext().clearDbContext();
128             // clean database directory
129             PersistenceServiceHelper.deleteDatabaseDirectory(getProgressionUIModel());
130             getContext().setDbExist(false);
131         }
132 
133         // Make sure there is no version on DB directory.
134         // This can occur during dev, when running DB in server mode
135         File dbVersionFile = new File(getConfig().getDbDirectory(), Daos.DB_VERSION_FILE);
136         if (dbVersionFile.exists()) {
137             FileUtils.deleteQuietly(dbVersionFile);
138         }
139 
140         // ------------------------------------------------------------------ //
141         // --- install db                                                     //
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         // Migrate installed DB (Mantis #40873)
171         PersistenceServiceHelper.migrateDbName();
172 
173         getContext().setDbExist(true);
174         getContext().setDbJustInstalled(true);
175 
176         // ------------------------------------------------------------------ //
177         // --- open db                                                        //
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 }