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