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 fr.ifremer.tutti.persistence.ProgressionModel;
26  import fr.ifremer.tutti.ui.swing.RunTutti;
27  import fr.ifremer.tutti.ui.swing.TuttiUIContext;
28  import fr.ifremer.tutti.ui.swing.content.MainUIHandler;
29  import fr.ifremer.tutti.ui.swing.content.actions.AbstractMainUITuttiAction;
30  import org.apache.commons.logging.Log;
31  import org.apache.commons.logging.LogFactory;
32  import org.nuiton.jaxx.application.ApplicationIOUtil;
33  
34  import java.io.File;
35  import java.util.Date;
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 ReinstallDbAction extends AbstractMainUITuttiAction {
46  
47      /** Logger. */
48      private static final Log log = LogFactory.getLog(ReinstallDbAction.class);
49  
50      protected File backupFile;
51  
52      protected String jdbcUrl;
53  
54      public ReinstallDbAction(MainUIHandler handler) {
55          super(handler, true);
56          setActionDescription(t("tutti.dbManager.action.installDb.tip"));
57      }
58  
59      @Override
60      public boolean prepareAction() throws Exception {
61          boolean doAction = super.prepareAction();
62  
63          if (doAction) {
64  
65              // check we can connect to remote install server
66  
67              // check db url is reachable
68              TuttiUIContext context = getContext();
69              doAction = context.checkUpdateDataReachable(true);
70          }
71  
72          if (doAction) {
73  
74              jdbcUrl = null;
75              backupFile = null;
76  
77              jdbcUrl = getConfig().getJdbcUrl();
78  
79              if (getModel().isDbExist()) {
80  
81                  if (getConfig().isImportDbSkipBackup()) {
82  
83                      if (log.isInfoEnabled()) {
84                          log.info("Skip backup before import, lucky you...");
85                      }
86  
87                  } else {
88  
89                      displayInfoMessage(
90                              t("tutti.dbManager.title.backup.db"),
91                              t("tutti.dbManager.action.installDb.backup.db")
92                      );
93  
94                      // choose backup file
95                      backupFile = saveFile(
96                              getConfig().getDbBackupDirectory(),
97                              "tutti-db-" + ExportDbAction.df.format(new Date()),
98                              "zip",
99                              t("tutti.dbManager.title.choose.dbExportFile"),
100                             t("tutti.dbManager.action.chooseDbExportFile"),
101                             "^.*\\.zip", t("tutti.common.file.zip")
102                     );
103 
104                     if (backupFile == null) {
105 
106                         displayWarningMessage(
107                                 t("tutti.dbManager.title.backup.db"),
108                                 t("tutti.dbManager.action.installDb.no.backup.db.choosen")
109                         );
110 
111                         doAction = false;
112                     }
113 
114                 }
115 
116             }
117         }
118 
119         if (doAction) {
120 
121             ProgressionModel progressionModel = new ProgressionModel();
122             progressionModel.setTotal(3 + (backupFile == null ? 0 : 1));
123             setProgressionModel(progressionModel);
124         }
125         return doAction;
126     }
127 
128     @Override
129     public void doAction() {
130 
131         ProgressionModel progressionModel = getProgressionModel();
132 
133         boolean doBackup = backupFile != null;
134 
135         // close db
136         progressionModel.increments(t("tutti.reinstallDb.step.closeDb", jdbcUrl));
137 
138         if (!doBackup) {
139             getContext().getPersistenceService().setSkipShutdownDbWhenClosing();
140         }
141         getContext().closePersistenceService();
142 
143         if (doBackup) {
144 
145             // backup db
146             progressionModel.increments(t("tutti.reinstallDb.step.backupDb", backupFile));
147             getContext().getPersistenceService().exportDb(backupFile);
148 
149         }
150 
151         // clean db context
152         getContext().clearDbContext();
153 
154         // write restart action file (will be loaded at restart)
155         String actionContent = InstallDbAction.class.getName();
156 
157         File startActionFile = getConfig().getStartActionFile();
158         ApplicationIOUtil.writeContent(startActionFile, actionContent, t("tutti.error.write.startActionFile", startActionFile));
159 
160         // delete db files on exit
161         getContext().deleteDbOnExit();
162 
163         // restart application
164         progressionModel.increments(t("tutti.reinstallDb.step.reloadApplication"));
165         RunTutti.closeTutti(getHandler(), RunTutti.RESTART_EXIT_CODE);
166 
167     }
168 }