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 com.google.common.base.Preconditions;
28  import fr.ifremer.quadrige2.core.dao.technical.Files;
29  import fr.ifremer.quadrige2.core.service.persistence.PersistenceServiceHelper;
30  import fr.ifremer.quadrige2.ui.swing.common.action.AbstractMainUIAction;
31  import fr.ifremer.quadrige2.ui.swing.common.content.AbstractMainUIHandler;
32  import fr.ifremer.quadrige2.ui.swing.common.model.ProgressionModel;
33  import org.apache.commons.logging.Log;
34  import org.apache.commons.logging.LogFactory;
35  import org.nuiton.jaxx.application.ApplicationIOUtil;
36  import org.nuiton.util.DateUtil;
37  
38  import java.io.File;
39  import java.io.IOException;
40  import java.util.Date;
41  
42  import static org.nuiton.i18n.I18n.t;
43  
44  /**
45   * To install (or reinstall) a db from last network one.
46   *
47   * @since 2.4
48   */
49  public class ReinstallDbAction extends AbstractMainUIAction {
50  
51      private static final Log LOG = LogFactory.getLog(ReinstallDbAction.class);
52  
53      private File backupFile;
54  
55      private String jdbcUrl;
56  
57      /**
58       * <p>Constructor for ReinstallDbAction.</p>
59       *
60       * @param handler a {@link AbstractMainUIHandler} object.
61       */
62      public ReinstallDbAction(AbstractMainUIHandler handler) {
63          super(handler, true);
64          setActionDescription(t("quadrige2.dbManager.action.installDb.tip"));
65      }
66  
67      /** {@inheritDoc} */
68      @Override
69      public boolean prepareAction() throws Exception {
70          boolean doAction = super.prepareAction();
71  
72          if (doAction) {
73  
74              // check we can connect to remote install server
75              // check db url is reachable
76              doAction = getContext().checkUpdateReachable(getConfig().getInstallDbUrl(), true);
77          }
78  
79          if (doAction) {
80  
81              jdbcUrl = null;
82              backupFile = null;
83  
84              jdbcUrl = getConfig().getJdbcUrl();
85  
86              if (getModel().isDbExist()) {
87  
88                  displayInfoMessage(
89                          t("quadrige2.dbManager.title.backup.db"),
90                          t("quadrige2.dbManager.action.installDb.backup.db")
91                  );
92  
93                  String date = DateUtil.formatDate(new Date(), "yyy-MM-dd");
94  
95                  // choose backup file
96                  backupFile = saveFile(
97                          getConfig().getDbBackupDirectory(),
98                          String.format("%s-db-%s", getConfig().getApplicationName(), date),
99                          "zip",
100                         t("quadrige2.dbManager.title.choose.dbExportFile"),
101                         t("quadrige2.dbManager.action.chooseDbExportFile"),
102                         "^.*\\.zip", t("quadrige2.common.file.zip")
103                 );
104 
105                 if (backupFile == null) {
106 
107                     displayWarningMessage(
108                             t("quadrige2.dbManager.title.backup.db"),
109                             t("quadrige2.dbManager.action.installDb.no.backup.db.choosen")
110                     );
111 
112                     doAction = false;
113                 }
114             }
115         }
116 
117         return doAction;
118     }
119 
120     /** {@inheritDoc} */
121     @Override
122     public void doAction() throws IOException {
123 
124         Preconditions.checkNotNull(backupFile);
125 
126         ProgressionModel progressionModel = new ProgressionModel();
127         setProgressionModel(progressionModel);
128         progressionModel.setTotal(3);
129 
130         // close db
131         progressionModel.increments(t("quadrige2.dbManager.action.reinstallDb.step.closeDb", jdbcUrl));
132 
133         getContext().closePersistenceService();
134 
135         // backup db
136         progressionModel.increments(t("quadrige2.dbManager.action.reinstallDb.step.backupDb", backupFile));
137         PersistenceServiceHelper.exportDb(backupFile);
138 
139         // clean db context
140         getContext().clearDbContext();
141 
142         // clean directories
143         deleteOldDatabaseDirectory();
144 
145         // launch install db
146         InstallDbAction installDbAction = getActionFactory().createLogicAction(getHandler(), InstallDbAction.class);
147         getActionEngine().runInternalAction(installDbAction);
148 
149     }
150 
151     private void deleteOldDatabaseDirectory() {
152 
153         // delete db directory
154         File dbDirectory = getConfig().getDbDirectory();
155         if (dbDirectory.exists()) {
156 
157             if (LOG.isInfoEnabled()) {
158                 LOG.info("Delete previous database directory: " + dbDirectory);
159             }
160             ApplicationIOUtil.deleteDirectory(dbDirectory, "Could not delete old db directory");
161         }
162 
163         // delete db cache directory
164         File cacheDirectory = getConfig().getCacheDirectory();
165         if (cacheDirectory.exists()) {
166 
167             if (LOG.isInfoEnabled()) {
168                 LOG.info("Delete previous database cache directory: " + cacheDirectory);
169             }
170             ApplicationIOUtil.deleteDirectory(cacheDirectory, "Could not delete old db cache directory");
171         }
172 
173         // delete synchro directory
174         File synchroDirectory = getConfig().getSynchronizationDirectory();
175         if (synchroDirectory.exists()) {
176 
177             if (LOG.isInfoEnabled()) {
178                 LOG.info("Delete previous database data synchro directory: " + synchroDirectory);
179             }
180             // Use a clean directory (see Mantis#0029005)
181             Files.cleanDirectory(synchroDirectory, "Could not delete old synchro directory");
182         }
183 
184         getContext().setDbExist(false);
185     }
186 
187 }