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  
27  import fr.ifremer.quadrige3.core.dao.technical.Assert;
28  import fr.ifremer.quadrige3.core.service.persistence.PersistenceServiceHelper;
29  import fr.ifremer.quadrige3.ui.swing.action.AbstractMainUIAction;
30  import fr.ifremer.quadrige3.ui.swing.content.AbstractMainUIHandler;
31  import org.apache.commons.lang3.time.DurationFormatUtils;
32  import org.apache.commons.logging.Log;
33  import org.apache.commons.logging.LogFactory;
34  import org.nuiton.util.DateUtil;
35  
36  import java.io.File;
37  import java.util.Date;
38  
39  import static org.nuiton.i18n.I18n.t;
40  
41  /**
42   * To export a db in archive.
43   *
44   * @since 1.0
45   */
46  public class BackupDbAction extends AbstractMainUIAction {
47  
48      /**
49       * Logger.
50       */
51      private static final Log LOG = LogFactory.getLog(BackupDbAction.class);
52  
53      private File backupFile;
54  
55      /**
56       * <p>Constructor for ExportDbAction.</p>
57       *
58       * @param handler a {@link AbstractMainUIHandler} object.
59       */
60      public BackupDbAction(AbstractMainUIHandler handler) {
61          super(handler, true);
62      }
63  
64      /**
65       * <p>Setter for the field <code>file</code>.</p>
66       *
67       * @param backupFile a {@link File} object.
68       */
69      public void setBackupFile(File backupFile) {
70          this.backupFile = backupFile;
71      }
72  
73      public File getBackupFile() {
74          return backupFile;
75      }
76  
77      /** {@inheritDoc} */
78      @Override
79      public boolean prepareAction() throws Exception {
80          backupFile = null;
81          boolean doAction = super.prepareAction();
82  
83          if (doAction) {
84          	
85          	String date = DateUtil.formatDate(new Date(), "yyyy-MM-dd");
86              // ask user file where to export db
87              backupFile = saveFile(
88                      getConfig().getDbBackupDirectory(),
89                      String.format("%s-db-%s", getConfig().getApplicationName(), date),
90                  "zip",
91                  t("quadrige3.dbManager.title.choose.dbExportFile"),
92                  t("quadrige3.dbManager.action.chooseDbExportFile"),
93                  "^.*\\.zip", t("quadrige3.common.file.zip")
94              );
95              doAction = backupFile != null;
96          }
97          return doAction;
98      }
99  
100     /** {@inheritDoc} */
101     @Override
102     public void doAction() {
103         long start = System.currentTimeMillis();
104 
105         Assert.notNull(backupFile);
106         if (LOG.isInfoEnabled()) {
107             LOG.info("Will export db to " + backupFile);
108         }
109 
110         // export
111         createProgressionUIModel().setMessage(t("quadrige3.dbManager.action.backupDb.step.createArchive", backupFile));
112         PersistenceServiceHelper.backupDatabase(backupFile.toPath(), getProgressionUIModel());
113 
114         LOG.info("Online backup finished in " + DurationFormatUtils.formatDurationHMS(System.currentTimeMillis() - start));
115     }
116 
117     /** {@inheritDoc} */
118     @Override
119     public void postSuccessAction() {
120         super.postSuccessAction();
121         sendMessage(t("quadrige3.flash.info.db.exported", backupFile));
122 
123         // make sure title is reloaded
124         getHandler().changeTitle();
125     }
126 }