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.actions.AbstractMainUITuttiAction;
29  import fr.ifremer.tutti.ui.swing.content.MainUIHandler;
30  import org.apache.commons.logging.Log;
31  import org.apache.commons.logging.LogFactory;
32  
33  import java.io.File;
34  import java.text.DateFormat;
35  import java.text.SimpleDateFormat;
36  import java.util.Date;
37  
38  import static org.nuiton.i18n.I18n.t;
39  
40  /**
41   * To export a db attached to Tutti.
42   *
43   * @author Tony Chemit - chemit@codelutin.com
44   * @since 1.0
45   */
46  public class ExportAndCleanDbAction extends AbstractMainUITuttiAction {
47  
48      /** Logger. */
49      private static final Log log =
50              LogFactory.getLog(ExportAndCleanDbAction.class);
51  
52      public static final DateFormat df = new SimpleDateFormat("yyy-MM-dd");
53  
54      protected File file;
55  
56      public ExportAndCleanDbAction(MainUIHandler handler) {
57          super(handler, true);
58      }
59  
60      @Override
61      public boolean prepareAction() throws Exception {
62          file = null;
63          boolean doAction = super.prepareAction();
64  
65          if (doAction) {
66  
67              // ask user file where to export db
68  
69              // choose file to import
70              file = saveFile(
71                      "tutti-db-" + df.format(new Date()),
72                      "zip",
73                      t("tutti.dbManager.title.choose.dbExportFile"),
74                      t("tutti.dbManager.action.chooseDbExportFile"),
75                      "^.*\\.zip", t("tutti.common.file.zip")
76              );
77              doAction = file != null;
78          }
79          return doAction;
80      }
81  
82      @Override
83      public void doAction() {
84          Preconditions.checkNotNull(file);
85          if (log.isInfoEnabled()) {
86              log.info("Will export db to " + file);
87          }
88  
89          ProgressionModel progressionModel = new ProgressionModel();
90          setProgressionModel(progressionModel);
91          progressionModel.setTotal(3);
92  
93          // close db
94  
95          progressionModel.setMessage(t("tutti.exportDb.step.closeDb"));
96  
97          // clear all caches
98          getContext().getPersistenceService().clearAllCaches();
99  
100         // close current persistence service
101         getContext().closePersistenceService();
102 
103         // clean db context
104         getContext().clearDbContext();
105 
106         // export db
107         progressionModel.increments(t("tutti.exportDb.step.createArchive", file));
108         getContext().getPersistenceService().exportDb(file);
109 
110         // delete db files on exit
111         getContext().deleteDbOnExit();
112 
113         // Close the application, will exit and restart application
114         progressionModel.increments(t("tutti.exportDb.step.reloadApplication"));
115         RunTutti.closeTutti(getHandler(), RunTutti.RESTART_EXIT_CODE);
116     }
117 
118     @Override
119     public void postSuccessAction() {
120         super.postSuccessAction();
121 
122         sendMessage(t("tutti.flash.info.db.exported.and.clean", file));
123 
124         // make sure title is reloaded
125         getUI().getHandler().changeTitle();
126     }
127 
128 }