View Javadoc
1   package fr.ifremer.quadrige2.ui.swing.common.content.db;
2   
3   /*-
4    * #%L
5    * Quadrige2 Core :: Quadrige2 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  
27  import fr.ifremer.quadrige2.core.exception.Quadrige2BusinessException;
28  import fr.ifremer.quadrige2.ui.swing.common.action.AbstractMainUIAction;
29  import fr.ifremer.quadrige2.ui.swing.common.callback.DatabaseUpdaterCallBack;
30  import fr.ifremer.quadrige2.ui.swing.common.content.AbstractMainUIHandler;
31  import fr.ifremer.quadrige2.ui.swing.common.model.ProgressionModel;
32  import org.apache.commons.io.FileUtils;
33  import org.apache.commons.logging.Log;
34  import org.apache.commons.logging.LogFactory;
35  import org.nuiton.updater.ApplicationUpdater;
36  
37  import java.io.File;
38  
39  import static org.nuiton.i18n.I18n.t;
40  
41  /**
42   * To install (or reinstall) a db from last network one.
43   *
44   * @since 2.4
45   */
46  public class InstallDbAction extends AbstractMainUIAction {
47  
48      /**
49       * Logger.
50       */
51      private static final Log LOG = LogFactory.getLog(InstallDbAction.class);
52  
53      /**
54       * <p>Constructor for InstallDbAction.</p>
55       *
56       * @param handler a {@link AbstractMainUIHandler} object.
57       */
58      public InstallDbAction(AbstractMainUIHandler handler) {
59          super(handler, true);
60          setActionDescription(t("quadrige2.dbManager.action.installDb.tip"));
61      }
62  
63      /** {@inheritDoc} */
64      @Override
65      public boolean prepareAction() throws Exception {
66          boolean doAction = super.prepareAction();
67  
68          if (doAction) {
69  
70              // check db url is reachable
71              doAction = getContext().checkUpdateReachable(getConfig().getInstallDbUrl(), true);
72          }
73          return doAction;
74      }
75  
76      /** {@inheritDoc} */
77      @Override
78      public void doAction() throws Exception {
79  
80          ProgressionModel progressionModel = new ProgressionModel();
81          setProgressionModel(progressionModel);
82          progressionModel.setTotal(2);
83  
84          // Make sure there is no version on DB directory.
85          // This can occur during dev, when running DB in server mode
86          File dbVersionFile = new File(getConfig().getDbDirectory(), "version.appup");
87          if (dbVersionFile.exists()) {
88              FileUtils.deleteQuietly(dbVersionFile);
89          }
90  
91          // ------------------------------------------------------------------ //
92          // --- install db                                                     //
93          // ------------------------------------------------------------------ //
94          File current = getConfig().getDataDirectory();
95          String url = getConfig().getInstallDbUrl();
96  
97          if (LOG.isInfoEnabled()) {
98              LOG.info(String.format("Try to install / update db (current data location: %s), using update url: %s", current, url));
99          }
100 
101         File dest = new File(getConfig().getBaseDirectory(), "NEW");
102 
103         progressionModel.increments(t("quadrige2.dbManager.action.upgradeDb.check"));
104         DatabaseUpdaterCallBack callback
105             = new DatabaseUpdaterCallBack(this, progressionModel);
106         ApplicationUpdater up = new ApplicationUpdater();
107         up.update(url,
108             current,
109             dest,
110             false,
111             callback,
112             progressionModel);
113 
114         if (!callback.isDbInstalled()) {
115             throw new Quadrige2BusinessException(t("quadrige2.dbManager.action.installDb.error"));
116         }
117 
118         progressionModel.increments(t("quadrige2.dbManager.action.upgradeDb.opening"));
119 
120         getContext().setDbExist(true);
121         getContext().setDbJustInstalled(true);
122 
123         // ------------------------------------------------------------------ //
124         // --- open db                                                        //
125         // ------------------------------------------------------------------ //
126         getActionEngine().runInternalAction(getHandler(), OpenDbAction.class);
127     }
128 }