View Javadoc
1   package fr.ifremer.tutti.ui.swing.content.db.actions;
2   
3   /*
4    * #%L
5    * Tutti :: UI
6    * %%
7    * Copyright (C) 2012 - 2014 Ifremer
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU General Public License as
11   * published by the Free Software Foundation, either version 3 of the 
12   * License, or (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Public 
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/gpl-3.0.html>.
22   * #L%
23   */
24  
25  import com.google.common.base.Preconditions;
26  import fr.ifremer.tutti.TuttiConfiguration;
27  import fr.ifremer.tutti.persistence.ProgressionModel;
28  import fr.ifremer.tutti.ui.swing.content.actions.AbstractMainUITuttiAction;
29  import fr.ifremer.tutti.ui.swing.content.MainUIHandler;
30  import fr.ifremer.tutti.ui.swing.update.TuttiDbUpdaterCallBack;
31  import fr.ifremer.tutti.ui.swing.update.Updates;
32  import org.apache.commons.logging.Log;
33  import org.apache.commons.logging.LogFactory;
34  
35  import java.io.File;
36  
37  import static org.nuiton.i18n.I18n.t;
38  
39  /**
40   * To install (or reinstall) a db from last network one.
41   *
42   * @author Tony Chemit - chemit@codelutin.com
43   * @since 2.4
44   */
45  public class InstallDbAction extends AbstractMainUITuttiAction {
46  
47      /** Logger. */
48      private static final Log log =
49              LogFactory.getLog(InstallDbAction.class);
50  
51      protected File backupFile;
52  
53      protected boolean doBackup;
54  
55      protected String jdbcUrl;
56  
57      public InstallDbAction(MainUIHandler handler) {
58          super(handler, true);
59          setActionDescription(t("tutti.dbManager.action.installDb.tip"));
60      }
61  
62      @Override
63      public boolean prepareAction() throws Exception {
64          boolean doAction = super.prepareAction();
65  
66          if (doAction) {
67  
68              // check db url is reachable
69              doAction = getContext().checkUpdateDataReachable(true);
70          }
71  
72          if (doAction) {
73  
74              ProgressionModel progressionModel = new ProgressionModel();
75              progressionModel.setTotal(2);
76              setProgressionModel(progressionModel);
77          }
78          return doAction;
79      }
80  
81      @Override
82      public void doAction() {
83  
84          ProgressionModel progressionModel = getProgressionModel();
85  
86          // ------------------------------------------------------------------ //
87          // --- install db                                                     //
88          // ------------------------------------------------------------------ //
89  
90          TuttiConfiguration config = getConfig();
91  
92          File current = config.getDataDirectory();
93          String url = config.getUpdateDataUrl();
94  
95          if (log.isInfoEnabled()) {
96              log.info(String.format("Try to install / update db (current data location: %s), using update url: %s", current, url));
97          }
98  
99          progressionModel.increments(t("tutti.dbManager.action.upgradeDb.check"));
100         TuttiDbUpdaterCallBack callback = new TuttiDbUpdaterCallBack(url, this, progressionModel);
101         Updates.doUpdate(config, callback, current);
102 
103         Preconditions.checkState(callback.isDbInstalled());
104 
105         progressionModel.increments(t("tutti.dbManager.action.upgradeDb.opening"));
106 
107         getContext().setDbExist(true);
108 
109         // ------------------------------------------------------------------ //
110         // --- open db                                                        //
111         // ------------------------------------------------------------------ //
112 
113         getActionEngine().runInternalAction(getHandler(), OpenDbAction.class);
114     }
115 }