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.persistence.ProgressionModel;
27  import fr.ifremer.tutti.ui.swing.RunTutti;
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 import a db after restart application.
41   *
42   * @author Tony Chemit - chemit@codelutin.com
43   * @since 1.1
44   */
45  public class ReimportDbAction extends AbstractMainUITuttiAction {
46  
47      /** Logger. */
48      private static final Log log = LogFactory.getLog(ReimportDbAction.class);
49  
50      protected File backupFile;
51  
52      protected File importFile;
53  
54      protected String jdbcUrl;
55  
56      public ReimportDbAction(MainUIHandler handler) {
57          super(handler, true);
58          setActionDescription(t("tutti.dbManager.action.importDb.tip"));
59      }
60  
61      @Override
62      public boolean prepareAction() throws Exception {
63  
64          jdbcUrl = null;
65          backupFile = importFile = null;
66  
67          boolean doAction = super.prepareAction();
68  
69          if (doAction) {
70  
71              jdbcUrl = getConfig().getJdbcUrl();
72  
73              if (getModel().isDbExist()) {
74  
75                  if (getConfig().isImportDbSkipBackup()) {
76  
77                      if (log.isInfoEnabled()) {
78                          log.info("Skip backup before import, lucky you...");
79                      }
80  
81                  } else {
82  
83                      displayInfoMessage(
84                              t("tutti.dbManager.title.backup.db"),
85                              t("tutti.dbManager.action.importDb.backup.db")
86                      );
87  
88                      // choose backup file
89                      backupFile = saveFile(
90                              getConfig().getDbBackupDirectory(),
91                              "tutti-db-" + ExportDbAction.df.format(new Date()),
92                              "zip",
93                              t("tutti.dbManager.title.choose.dbExportFile"),
94                              t("tutti.dbManager.action.chooseDbExportFile"),
95                              "^.*\\.zip", t("tutti.common.file.zip")
96                      );
97  
98                      if (backupFile == null) {
99  
100                         displayWarningMessage(
101                                 t("tutti.dbManager.title.backup.db"),
102                                 t("tutti.dbManager.action.importdb.no.backup.db.choosen")
103                         );
104 
105                         doAction = false;
106                     }
107 
108                 }
109 
110             }
111 
112             if (doAction && importFile == null) {
113 
114                 // choose file to import
115                 importFile = chooseFile(
116                         t("tutti.dbManager.title.choose.dbImportFile"),
117                         t("tutti.dbManager.action.chooseDbFile"),
118                         "^.*\\.zip", t("tutti.common.file.zip")
119                 );
120 
121                 if (importFile == null) {
122 
123                     displayWarningMessage(
124                             t("tutti.dbManager.title.choose.dbImportFile"),
125                             t("tutti.dbManager.action.importdb.no.import.file.choosen")
126                     );
127 
128                     doAction = false;
129                 }
130             }
131 
132             if (doAction) {
133 
134                 ProgressionModel progressionModel = new ProgressionModel();
135                 progressionModel.setTotal(3 + (backupFile == null ? 0 : 1));
136                 setProgressionModel(progressionModel);
137 
138                 getContext().getPersistenceService().checkImportStructure(importFile);
139             }
140         }
141         return doAction;
142     }
143 
144     @Override
145     public void releaseAction() {
146         importFile = backupFile = null;
147         super.releaseAction();
148     }
149 
150     @Override
151     public void doAction() throws Exception {
152 
153         Preconditions.checkNotNull(importFile);
154 
155         ProgressionModel progressionModel = getProgressionModel();
156 
157         boolean doBackup = backupFile != null;
158 
159         // close db
160         progressionModel.increments(t("tutti.reimport.step.closeDb", jdbcUrl));
161 
162         if (!doBackup) {
163             getContext().getPersistenceService().setSkipShutdownDbWhenClosing();
164         }
165 
166         getContext().closePersistenceService();
167 
168 
169         if (doBackup) {
170 
171             // backup db
172             progressionModel.increments(t("tutti.reimport.step.backupDb", backupFile));
173             getContext().getPersistenceService().exportDb(backupFile);
174 
175         }
176 
177         // clean db context
178         getContext().clearDbContext();
179 
180         // write restart action file (will be load at restart)
181         String actionContent = ImportDbAction.class.getName() + ":" + importFile.getAbsolutePath();
182 
183         File startActionFile = getConfig().getStartActionFile();
184         ApplicationIOUtil.writeContent(startActionFile, actionContent, t("tutti.error.write.startActionFile", startActionFile));
185 
186         // delete db files on exit
187         getContext().deleteDbOnExit();
188 
189         // restart application
190         progressionModel.increments(t("tutti.reimport.step.reloadApplication"));
191         RunTutti.closeTutti(getHandler(), RunTutti.RESTART_EXIT_CODE);
192 
193     }
194 
195 }