View Javadoc
1   package fr.ifremer.tutti.ui.swing.update.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.RunTutti;
28  import fr.ifremer.tutti.ui.swing.TuttiUIContext;
29  import fr.ifremer.tutti.ui.swing.content.actions.AbstractMainUITuttiAction;
30  import fr.ifremer.tutti.ui.swing.content.actions.CloseApplicationAction;
31  import fr.ifremer.tutti.ui.swing.content.MainUIHandler;
32  import fr.ifremer.tutti.ui.swing.update.TuttiReportUpdaterCallBack;
33  import fr.ifremer.tutti.ui.swing.update.Updates;
34  import org.apache.commons.logging.Log;
35  import org.apache.commons.logging.LogFactory;
36  import org.nuiton.updater.ApplicationUpdater;
37  
38  import java.io.File;
39  
40  import static org.nuiton.i18n.I18n.t;
41  
42  /**
43   * To update reports using the {@link ApplicationUpdater} mecanism.
44   *
45   * @author Tony Chemit - chemit@codelutin.com
46   * @since 1.0
47   */
48  public class UpdateReportAction extends AbstractMainUITuttiAction {
49  
50      /** Logger. */
51      private static final Log log =
52              LogFactory.getLog(UpdateReportAction.class);
53  
54      public UpdateReportAction(MainUIHandler handler) {
55          super(handler, true);
56          setActionDescription(t("tutti.main.action.updateReport.tip"));
57      }
58  
59      protected boolean reload;
60  
61      @Override
62      public boolean prepareAction() throws Exception {
63          boolean doAction = super.prepareAction();
64  
65          if (doAction) {
66              // check data url is reachable
67              TuttiUIContext context = getContext();
68              doAction = context.checkUpdateDataReachable(true);
69          }
70          return doAction;
71      }
72  
73      @Override
74      public void doAction() throws Exception {
75  
76          reload = false;
77  
78          TuttiUIContext context = getContext();
79          TuttiConfiguration config = getConfig();
80  
81          File current = config.getBasedir();
82          if (current == null || !current.exists()) {
83  
84              // can not update application
85              if (log.isWarnEnabled()) {
86                  log.warn("No application base directory defined, skip updates.");
87              }
88          } else {
89  
90              String url = config.getUpdateDataUrl();
91  //            File dest = new File(config.getBasedir(), "NEW");
92  
93              if (log.isInfoEnabled()) {
94                  log.info(String.format("Try to update repport (current application location: %s), using update url: %s", current, url));
95              }
96  
97              ProgressionModel progressionModel = new ProgressionModel();
98              context.getActionUI().getModel().setProgressionModel(progressionModel);
99              progressionModel.setMessage(t("tutti.updateApplication.checkUpdates"));
100 
101             TuttiReportUpdaterCallBack callback =
102                     new TuttiReportUpdaterCallBack(url, this, progressionModel);
103 
104             Updates.doUpdate(config, callback, current);
105 
106 //            ApplicationUpdater up = new ApplicationUpdater();
107 //            up.update(url,
108 //                      current,
109 //                      dest,
110 //                      false,
111 //                      callback,
112 //                      progressionModel);
113 
114             if (callback.isApplicationUpdated()) {
115 
116                 reload = true;
117 
118             } else {
119 
120                 sendMessage(t("tutti.updateReport.noUpdate"));
121             }
122         }
123     }
124 
125     @Override
126     public void postSuccessAction() {
127         super.postSuccessAction();
128 
129         if (reload) {
130             // wait 1 second to be sure action ui is up
131             try {
132                 Thread.sleep(1000);
133             } catch (InterruptedException e) {
134                 if (log.isWarnEnabled()) {
135                     log.warn("Could not wait 1 second...", e);
136                 }
137             }
138 
139             // tell user restart will be done
140 
141             getHandler().showSuccessMessage(t("tutti.updateReport.title.success"),
142                                             t("tutti.updateReport.message.success"));
143 
144             CloseApplicationAction action = getContext().getActionFactory().createLogicAction(
145                     getHandler(), CloseApplicationAction.class);
146             action.setExitCode(RunTutti.RESTART_EXIT_CODE);
147             getActionEngine().runAction(action);
148         }
149     }
150 
151     public boolean isReload() {
152         return reload;
153     }
154 }