View Javadoc
1   package fr.ifremer.quadrige3.ui.swing.callback;
2   
3   /*-
4    * #%L
5    * Quadrige3 Core :: Quadrige3 UI Common
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2017 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  
27  import com.google.common.collect.Lists;
28  import com.google.common.collect.Maps;
29  import fr.ifremer.quadrige3.ui.swing.ApplicationUIContext;
30  import fr.ifremer.quadrige3.ui.swing.action.AbstractAction;
31  import fr.ifremer.quadrige3.ui.swing.model.ProgressionUIModel;
32  import org.apache.commons.logging.Log;
33  import org.apache.commons.logging.LogFactory;
34  import org.nuiton.jaxx.application.ApplicationTechnicalException;
35  import org.nuiton.jaxx.application.swing.action.ApplicationActionException;
36  import org.nuiton.updater.ApplicationInfo;
37  import org.nuiton.updater.ApplicationUpdaterCallback;
38  
39  import java.util.List;
40  import java.util.Map;
41  
42  import static org.nuiton.i18n.I18n.n;
43  import static org.nuiton.i18n.I18n.t;
44  
45  /**
46   * CallBack to update config.
47   *
48   * @author Ludovic Pecquot
49   * @since 1.2.2
50   */
51  public class DataUpdaterCallBack implements ApplicationUpdaterCallback {
52  
53      /**
54       * Logger.
55       */
56      private static final Log LOG = LogFactory.getLog(DataUpdaterCallBack.class);
57  
58      public enum UpdateType {
59  
60          CONFIG(n("quadrige3.update.config"));
61  
62          private final String i18nKey;
63  
64          UpdateType(String i18nKey) {
65              this.i18nKey = i18nKey;
66          }
67  
68          public String getLabel() {
69              return t(i18nKey);
70          }
71      }
72  
73      protected final ApplicationUIContext context;
74  
75      protected List<UpdateType> types;
76  
77      protected final ProgressionUIModel progressionModel;
78  
79      protected boolean dataUpdated;
80  
81      protected final AbstractAction<?, ?, ?> action;
82  
83      /**
84       * <p>Constructor for DataUpdaterCallBack.</p>
85       *
86       * @param action a {@link AbstractAction} object.
87       * @param progressionModel a {@link ProgressionUIModel} object.
88       */
89      public DataUpdaterCallBack(AbstractAction<?, ?, ?> action, ProgressionUIModel progressionModel) {
90          this.action = action;
91          this.context = action.getContext();
92          this.progressionModel = progressionModel;
93      }
94  
95      /**
96       * <p>Setter for the field <code>types</code>.</p>
97       *
98       * @param types a {@link DataUpdaterCallBack.UpdateType} object.
99       */
100     public void setTypes(UpdateType... types) {
101         this.types = Lists.newArrayList(types);
102     }
103 
104     /**
105      * <p>isDataUpdated.</p>
106      *
107      * @return a boolean.
108      */
109     public boolean isDataUpdated() {
110         return dataUpdated;
111     }
112 
113     /** {@inheritDoc} */
114     @Override
115     public Map<String, ApplicationInfo> updateToDo(Map<String, ApplicationInfo> appToUpdate) {
116         Map<String, ApplicationInfo> result = Maps.newHashMap();
117 
118         for (UpdateType type : types) {
119             ApplicationInfo info = getInfo(type, appToUpdate);
120             if (info != null) {
121                 result.put(info.name, info);
122             }
123         }
124         return result;
125     }
126 
127     /** {@inheritDoc} */
128     @Override
129     public void startUpdate(ApplicationInfo info) {
130         if (UpdateType.CONFIG.name().toLowerCase().equals(info.name)) {
131             progressionModel.setMessage(t("quadrige3.applicationUpdater.startUpdate.config", info.newVersion));
132         }
133     }
134 
135     /** {@inheritDoc} */
136     @Override
137     public void updateDone(Map<String, ApplicationInfo> appToUpdate, Map<String, Exception> appUpdateError) {
138 
139         if (updateDoneConfig(appToUpdate, appUpdateError)) {
140             dataUpdated = true;
141         }
142     }
143 
144     /** {@inheritDoc} */
145     @Override
146     public void aborted(String propertiesURL, Exception eee) {
147         if (LOG.isErrorEnabled()) {
148             LOG.error("Could not update from " + propertiesURL, eee);
149         }
150         throw ApplicationActionException.propagateError(action, eee);
151     }
152 
153     /**
154      * <p>updateDoneConfig.</p>
155      *
156      * @param appToUpdate a {@link Map} object.
157      * @param appUpdateError a {@link Map} object.
158      * @return a boolean.
159      */
160     protected boolean updateDoneConfig(Map<String, ApplicationInfo> appToUpdate,
161         Map<String, Exception> appUpdateError) {
162         boolean doRestart = false;
163         Exception error = getError(UpdateType.CONFIG, appUpdateError);
164         if (error != null) {
165 
166             // something bad while updating config
167             throw ApplicationActionException.propagateError(
168                 action, new ApplicationTechnicalException(t("quadrige3.applicationUpdater.config.error"), error));
169         }
170         ApplicationInfo info = getInfo(UpdateType.CONFIG, appToUpdate);
171         if (info != null) {
172 
173             if (LOG.isInfoEnabled()) {
174                 LOG.info(String.format(
175                     "A config update was downloaded (oldVersion: %s, newVersion: %s), will process it and application will restart",
176                     info.oldVersion, info.newVersion));
177             }
178             doRestart = true;
179 
180             // Don't update config now, but let the updater do the job to prevent properties loose (Mantis #46826)
181             // Get the new config file
182 //            try {
183 //
184 //                // Load file
185 //                Properties p = new Properties();
186 //                File f = new File(info.destDir, "config/" + context.getConfiguration().getApplicationConfig().getConfigFileName());
187 //                p.load(Files.newReader(f, Charsets.UTF_8));
188 //
189 //                // Updates or create missing properties and save all
190 //                context.getConfiguration().overridesOptions(p);
191 //                context.getConfiguration().save();
192 //
193 //                // Create local version.appup
194 //                ApplicationUpdater.createVersionFile(context.getConfiguration().getConfigDirectory(), info.newVersion);
195 //
196 //            } catch (IOException ex) {
197 //                throw ApplicationActionException.propagateError(
198 //                    action, new ApplicationTechnicalException(t("quadrige3.applicationUpdater.config.io.error"), ex));
199 //            }
200         }
201         return doRestart;
202     }
203 
204     /**
205      * <p>getInfo.</p>
206      *
207      * @param type a {@link DataUpdaterCallBack.UpdateType} object.
208      * @param appToUpdate a {@link Map} object.
209      * @return a {@link ApplicationInfo} object.
210      */
211     protected ApplicationInfo getInfo(UpdateType type, Map<String, ApplicationInfo> appToUpdate) {
212         return appToUpdate.get(type.name().toLowerCase());
213     }
214 
215     /**
216      * <p>getError.</p>
217      *
218      * @param type a {@link DataUpdaterCallBack.UpdateType} object.
219      * @param appUpdateError a {@link Map} object.
220      * @return a {@link Exception} object.
221      */
222     protected Exception getError(UpdateType type, Map<String, Exception> appUpdateError) {
223         return appUpdateError.get(type.name().toLowerCase());
224     }
225 }