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 fr.ifremer.quadrige3.core.dao.technical.Assert;
27  import fr.ifremer.quadrige3.core.service.persistence.PersistenceServiceHelper;
28  import fr.ifremer.quadrige3.ui.swing.action.AbstractMainUIAction;
29  import fr.ifremer.quadrige3.ui.swing.content.AbstractMainUIHandler;
30  import org.apache.commons.logging.Log;
31  import org.apache.commons.logging.LogFactory;
32  
33  import java.io.File;
34  
35  import static org.nuiton.i18n.I18n.t;
36  
37  /**
38   * To import a db and use it.
39   *
40   * @since 1.1
41   */
42  public class RestoreDbAction extends AbstractMainUIAction {
43  
44      /**
45       * Logger.
46       */
47      private static final Log LOG = LogFactory.getLog(RestoreDbAction.class);
48  
49      private boolean mustBackup;
50      private BackupDbAction backupDbAction;
51      private File backupFile;
52      private File restoreFile;
53      private String jdbcUrl;
54  
55      /**
56       * <p>Constructor for RestoreDbAction.</p>
57       *
58       * @param handler a {@link AbstractMainUIHandler} object.
59       */
60      public RestoreDbAction(AbstractMainUIHandler handler) {
61          super(handler, true);
62          setActionDescription(t("quadrige3.dbManager.action.restoreDb.tip"));
63      }
64  
65      /**
66       * <p>Setter for the field <code>restoreFile</code>.</p>
67       *
68       * @param restoreFile a {@link File} object.
69       */
70      public void setRestoreFile(File restoreFile) {
71          this.restoreFile = restoreFile;
72      }
73  
74      /**
75       * {@inheritDoc}
76       */
77      @Override
78      public boolean prepareAction() throws Exception {
79  
80          boolean doAction = super.prepareAction();
81          mustBackup = getContext().isDbExist();
82  
83          if (doAction) {
84  
85              if (mustBackup) {
86  
87                  displayInfoMessage(
88                          t("quadrige3.dbManager.title.backup.db"),
89                          t("quadrige3.dbManager.action.restoreDb.backup.db")
90                  );
91  
92                  backupDbAction = getActionFactory().createLogicAction(getHandler(), BackupDbAction.class);
93                  backupDbAction.prepareAction();
94                  backupFile = backupDbAction.getBackupFile();
95  
96                  if (backupFile == null) {
97  
98                      displayWarningMessage(
99                              t("quadrige3.dbManager.title.backup.db"),
100                             t("quadrige3.dbManager.action.restoreDb.no.backup.db.choosen")
101                     );
102 
103                     doAction = false;
104                 }
105 
106             }
107 
108             if (doAction && restoreFile == null) {
109 
110                 // choose file to import
111                 restoreFile = chooseFile(
112                         t("quadrige3.dbManager.title.choose.dbImportFile"),
113                         t("quadrige3.dbManager.action.chooseDbFile"),
114                         "^.*\\.zip", t("quadrige3.common.file.zip")
115                 );
116 
117                 if (restoreFile == null) {
118 
119                     displayWarningMessage(
120                             t("quadrige3.dbManager.title.choose.dbImportFile"),
121                             t("quadrige3.dbManager.action.restoreDb.no.import.file.choosen")
122                     );
123 
124                     doAction = false;
125                 }
126             }
127         }
128         return doAction;
129     }
130 
131     /**
132      * {@inheritDoc}
133      */
134     @Override
135     public void doAction() throws Exception {
136         jdbcUrl = getConfig().getJdbcUrl();
137         Assert.notNull(restoreFile);
138         if (mustBackup) {
139             Assert.notNull(backupDbAction);
140             Assert.notNull(backupFile);
141         }
142 
143         if (LOG.isInfoEnabled()) {
144             LOG.info("Will import db: " + restoreFile);
145         }
146 
147         createProgressionUIModel();
148 
149         // perform backup
150         if (mustBackup) {
151             // execute backup action
152             getActionEngine().runInternalAction(backupDbAction);
153             // close database
154             getContext().closePersistenceService();
155             getContext().clearDbContext();
156             // clean database directory
157             PersistenceServiceHelper.deleteDatabaseDirectory(getProgressionUIModel());
158             getContext().setDbExist(false);
159         }
160 
161         // restore database
162         getProgressionUIModel().setMessage(t("quadrige3.dbManager.action.restoreDb.step.unzipArchive", restoreFile));
163         PersistenceServiceHelper.restoreDatabase(restoreFile.toPath(), getProgressionUIModel());
164 
165         // open database
166         OpenDbAction openDbAction = getActionFactory().createLogicAction(getHandler(), OpenDbAction.class);
167         openDbAction.setAfterImportDb(true);
168         openDbAction.prepareAction();
169         getActionEngine().runInternalAction(openDbAction);
170 
171     }
172 
173     /**
174      * {@inheritDoc}
175      */
176     @Override
177     public void postSuccessAction() {
178         getHandler().reloadDbManagerText();
179 
180         super.postSuccessAction();
181 
182         sendMessage(t("quadrige3.flash.info.db.imported", jdbcUrl));
183 
184         // make sure title is reloaded
185         getHandler().changeTitle();
186     }
187 
188     /**
189      * {@inheritDoc}
190      */
191     @Override
192     public void postFailedAction(Throwable error) {
193         getHandler().reloadDbManagerText();
194         super.postFailedAction(error);
195     }
196 
197     /**
198      * {@inheritDoc}
199      */
200     @Override
201     public void releaseAction() {
202         restoreFile = backupFile = null;
203         backupDbAction = null;
204         super.releaseAction();
205     }
206 
207 }