View Javadoc
1   package fr.ifremer.quadrige3.ui.swing.action;
2   
3   /*
4    * #%L
5    * Reef DB :: UI
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2014 - 2015 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  import fr.ifremer.quadrige3.ui.swing.Application;
27  import fr.ifremer.quadrige3.ui.swing.callback.DataUpdaterCallBack;
28  import fr.ifremer.quadrige3.ui.swing.content.AbstractMainUIHandler;
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  import org.nuiton.updater.ApplicationUpdater;
32  
33  import java.io.File;
34  
35  import static org.nuiton.i18n.I18n.t;
36  
37  /**
38   * To update Data using the {@link ApplicationUpdater} mecanism. Can be Configuration, I18n and Help from exploitation url
39   *
40   * @author Ludovic Pecquot
41   * @since 1.2.2
42   */
43  public class UpdateDataAction extends AbstractMainUIAction {
44  
45      private static final Log LOG = LogFactory.getLog(UpdateDataAction.class);
46  
47      public UpdateDataAction(AbstractMainUIHandler handler) {
48          super(handler, true);
49          setActionDescription(t("quadrige3.applicationUpdater.data.action"));
50          types = DataUpdaterCallBack.UpdateType.values();
51      }
52  
53      private DataUpdaterCallBack.UpdateType[] types;
54  
55      private boolean reload;
56      private boolean silent = false;
57  
58      /** {@inheritDoc} */
59      @Override
60      public boolean prepareAction() throws Exception {
61          boolean doAction = super.prepareAction();
62  
63          if (doAction) {
64              // check application url is reachable
65              doAction = getContext().checkUpdateReachable(getConfig().getUpdateDataUrl(), true);
66          }
67          return doAction;
68      }
69  
70      /** {@inheritDoc} */
71      @Override
72      public void releaseAction() {
73          super.releaseAction();
74          types = DataUpdaterCallBack.UpdateType.values();
75      }
76  
77      /** {@inheritDoc} */
78      @Override
79      public void doAction() {
80  
81          reload = false;
82  
83          File current = getConfig().getBaseDirectory();
84          if (current == null || !current.exists()) {
85  
86              // can not update application
87              if (LOG.isWarnEnabled()) {
88                  LOG.warn("No application base directory defined, skip updates.");
89              }
90          } else {
91  
92              String url = getConfig().getUpdateDataUrl();
93              File dest = new File(getConfig().getBaseDirectory(), "NEW");
94  
95              if (LOG.isInfoEnabled()) {
96                  LOG.info(String.format("Try to update i18N, help or config for exploitation context (current application location: %s), using update url: %s", current, url));
97              }
98  
99              createProgressionUIModel();
100             getProgressionUIModel().setMessage(t("quadrige3.applicationUpdater.checkUpdates"));
101 
102             DataUpdaterCallBack callback
103                 = new DataUpdaterCallBack(this, getProgressionUIModel());
104 
105             callback.setTypes(types);
106 
107             ApplicationUpdater up = new ApplicationUpdater();
108             up.update(url,
109                 current,
110                 dest,
111                 false,
112                 callback,
113                     getProgressionUIModel());
114 
115             if (callback.isDataUpdated()) {
116 
117                 reload = true;
118 
119             } else {
120 
121                 sendMessage(t("quadrige3.applicationUpdater.data.noUpdate"));
122             }
123         }
124     }
125 
126     /**
127      * <p>Setter for the field <code>types</code>.</p>
128      *
129      * @param types a {@link DataUpdaterCallBack.UpdateType} object.
130      */
131     public void setTypes(DataUpdaterCallBack.UpdateType... types) {
132         this.types = types;
133     }
134 
135     public void setSilent(boolean silent) {
136         this.silent = silent;
137     }
138 
139     /** {@inheritDoc} */
140     @Override
141     public void postSuccessAction() {
142         super.postSuccessAction();
143 
144         if (silent) return;
145 
146         if (reload) {
147             // wait 1 second to be sure action ui is up
148             try {
149                 Thread.sleep(1000);
150             } catch (InterruptedException e) {
151                 if (LOG.isWarnEnabled()) {
152                     LOG.warn("Could not wait 1 second...", e);
153                 }
154             }
155 
156             // tell user restart will be done
157             getHandler().showSuccessMessage(t("quadrige3.applicationUpdater.success.title"),
158                 t("quadrige3.applicationUpdater.data.success.message"));
159 
160             CloseApplicationAction action = getContext().getActionFactory().createLogicAction(
161                 getHandler(), CloseApplicationAction.class);
162             action.setExitCode(Application.UPDATE_EXIT_CODE);
163             getActionEngine().runAction(action);
164         }
165     }
166 
167     /**
168      * <p>isReload.</p>
169      *
170      * @return a boolean.
171      */
172     public boolean isReload() {
173         return reload;
174     }
175 }