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 fr.ifremer.tutti.TuttiConfiguration;
26  import fr.ifremer.tutti.persistence.ProgressionModel;
27  import fr.ifremer.tutti.ui.swing.TuttiUIContext;
28  import fr.ifremer.tutti.ui.swing.content.actions.AbstractMainUITuttiAction;
29  import fr.ifremer.tutti.ui.swing.content.MainUIHandler;
30  import fr.ifremer.tutti.ui.swing.update.TuttiDbUpdaterCallBack;
31  import fr.ifremer.tutti.ui.swing.update.Updates;
32  import fr.ifremer.tutti.ui.swing.util.AbstractTuttiUIHandler;
33  import org.apache.commons.logging.Log;
34  import org.apache.commons.logging.LogFactory;
35  import org.nuiton.updater.ApplicationInfo;
36  
37  import javax.swing.JOptionPane;
38  import java.io.File;
39  
40  import static org.nuiton.i18n.I18n.t;
41  
42  /**
43   * To update - install database.
44   *
45   * @author Tony Chemit - chemit@codelutin.com
46   * @since 1.0
47   */
48  public class UpdateDbAction extends AbstractMainUITuttiAction {
49  
50      /** Logger. */
51      private static final Log log = LogFactory.getLog(UpdateDbAction.class);
52  
53      protected ApplicationInfo updateDbVersion;
54  
55      public UpdateDbAction(MainUIHandler handler) {
56          super(handler, true);
57          setActionDescription(t("tutti.dbManager.action.upgradeDb.tip"));
58      }
59  
60      @Override
61      public boolean prepareAction() throws Exception {
62          boolean doAction = super.prepareAction();
63  
64          updateDbVersion = null;
65  
66          if (doAction) {
67              // check db url is reachable
68              doAction = getContext().checkUpdateDataReachable(true);
69          }
70  
71          if (doAction) {
72  
73              // get the next db version
74              updateDbVersion = Updates.getDatabaseUpdateVersion(getConfig());
75  
76              if (getContext().isDbExist() &&
77                  updateDbVersion != null &&
78                  updateDbVersion.newVersion != null) {
79  
80                  // ask user if it wants to do the update
81                  String htmlMessage = String.format(
82                          AbstractTuttiUIHandler.CONFIRMATION_FORMAT,
83                          t("tutti.dbManager.updatedb.found", updateDbVersion.newVersion),
84                          t("tutti.common.askBeforeUpdate.help"));
85                  int i = JOptionPane.showConfirmDialog(
86                          getHandler().getUI(),
87                          htmlMessage,
88                          t("tutti.dbManager.title.confirm.updatedb"),
89                          JOptionPane.OK_CANCEL_OPTION,
90                          JOptionPane.QUESTION_MESSAGE);
91  
92                  doAction = i == JOptionPane.OK_OPTION;
93              }
94          }
95          return doAction;
96      }
97  
98      @Override
99      public void doAction() {
100         TuttiUIContext context = getContext();
101         TuttiConfiguration config = getConfig();
102 
103         File current = config.getDataDirectory();
104         String url = config.getUpdateDataUrl();
105 
106         if (log.isInfoEnabled()) {
107             log.info(String.format("Try to install / update db (current data location: %s), using update url: %s", current, url));
108         }
109 
110 //        File dest = new File(config.getBasedir(), "NEW");
111         ProgressionModel progressionModel = new ProgressionModel();
112         context.getActionUI().getModel().setProgressionModel(progressionModel);
113         progressionModel.setMessage(t("tutti.dbManager.action.upgradeDb.check"));
114 
115         TuttiDbUpdaterCallBack callback = new TuttiDbUpdaterCallBack(url, this, progressionModel);
116 
117         Updates.doUpdate(config, callback, current);
118 
119 //        ApplicationUpdater up = new ApplicationUpdater();
120 //
121 //        up.update(url,
122 //                  current,
123 //                  dest,
124 //                  false,
125 //                  callback,
126 //                  progressionModel);
127 
128         if (callback.isDbUpdated()) {
129 
130             sendMessage(t("tutti.dbManager.action.upgradeDb.done", updateDbVersion.newVersion));
131 
132         } else {
133             sendMessage(t("tutti.dbManager.action.upgradeDb.upToDate"));
134         }
135     }
136 
137     @Override
138     public void postSuccessAction() {
139         handler.reloadDbManagerText();
140         super.postSuccessAction();
141     }
142 
143     @Override
144     public void postFailedAction(Throwable error) {
145         handler.reloadDbManagerText();
146         super.postFailedAction(error);
147     }
148 }