View Javadoc
1   package fr.ifremer.tutti.ui.swing.update;
2   
3   /*
4    * #%L
5    * Tutti :: UI
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2012 - 2015 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU General Public License as
13   * published by the Free Software Foundation, either version 3 of the
14   * License, or (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 General Public
22   * License along with this program.  If not, see
23   * <http://www.gnu.org/licenses/gpl-3.0.html>.
24   * #L%
25   */
26  
27  import com.google.common.collect.Maps;
28  import com.google.common.collect.Sets;
29  import fr.ifremer.tutti.persistence.ProgressionModel;
30  import fr.ifremer.tutti.ui.swing.TuttiUIContext;
31  import fr.ifremer.tutti.ui.swing.util.actions.LongActionSupport;
32  import fr.ifremer.tutti.ui.swing.update.module.ModuleUpdaterSupport;
33  import fr.ifremer.tutti.ui.swing.updater.UpdateModule;
34  import org.apache.commons.logging.Log;
35  import org.apache.commons.logging.LogFactory;
36  import org.nuiton.jaxx.application.swing.action.ApplicationActionException;
37  import org.nuiton.updater.ApplicationInfo;
38  import org.nuiton.updater.ApplicationUpdaterCallback;
39  
40  import java.util.Map;
41  import java.util.Set;
42  
43  import static org.nuiton.i18n.I18n.t;
44  
45  /**
46   * Created on 1/28/15.
47   *
48   * @author Tony Chemit - chemit@codelutin.com
49   * @since 3.12.1
50   */
51  public abstract class TuttiUpdaterCallBackSupport implements ApplicationUpdaterCallback {
52  
53      /** Logger. */
54      private static final Log log = LogFactory.getLog(TuttiReportUpdaterCallBack.class);
55  
56      protected final TuttiUIContext context;
57  
58      protected ProgressionModel progressionModel;
59  
60      protected final LongActionSupport action;
61  
62      protected final String url;
63  
64      protected final Map<UpdateModule, ModuleUpdaterSupport> allUpdaters;
65  
66      /**
67       * Modules to update.
68       */
69      protected Set<UpdateModule> modulesToUpdate;
70  
71      protected boolean applicationUpdated;
72  
73      public TuttiUpdaterCallBackSupport(String url, Map<UpdateModule, ModuleUpdaterSupport> allUpdaters, LongActionSupport action,
74                                         ProgressionModel progressionModel) {
75          this.url = url;
76          this.action = action;
77          this.allUpdaters = allUpdaters;
78          this.context = action.getContext();
79          this.progressionModel = progressionModel;
80      }
81  
82      public void setModulesToUpdate(UpdateModule... modulesToUpdate) {
83          this.modulesToUpdate = Sets.newHashSet(modulesToUpdate);
84      }
85  
86      public boolean isApplicationUpdated() {
87          return applicationUpdated;
88      }
89  
90      public String getUrl() {
91          return url;
92      }
93  
94      public ProgressionModel getProgressionModel() {
95          return progressionModel;
96      }
97  
98      @Override
99      public Map<String, ApplicationInfo> updateToDo(Map<String, ApplicationInfo> appToUpdate) {
100 
101         Map<String, ApplicationInfo> result = Maps.newHashMap();
102 
103         for (UpdateModule updateModule : modulesToUpdate) {
104             ModuleUpdaterSupport moduleUpdaterSupport = getModuleUpdater(updateModule);
105             ApplicationInfo info = moduleUpdaterSupport.updateToDo(context, appToUpdate);
106             if (info != null) {
107                 result.put(info.name, info);
108             }
109         }
110 
111         return result;
112 
113     }
114 
115     @Override
116     public void startUpdate(ApplicationInfo info) {
117 
118         for (UpdateModule updateModule : modulesToUpdate) {
119 
120             ModuleUpdaterSupport moduleUpdaterSupport = getModuleUpdater(updateModule);
121             if (moduleUpdaterSupport.matchUpdate(info)) {
122 
123                 String moduleLabel = moduleUpdaterSupport.getLabel();
124                 String message = t("tutti.applicationUpdater.startUpdate", moduleLabel, info.newVersion);
125                 if (log.isInfoEnabled()) {
126                     log.info(message);
127                 }
128                 progressionModel.setMessage(message);
129 
130             }
131 
132         }
133 
134     }
135 
136     @Override
137     public void updateDone(Map<String, ApplicationInfo> appToUpdate, Map<String, Exception> appUpdateError) {
138 
139         boolean doRestart = false;
140         for (UpdateModule updateModule : modulesToUpdate) {
141 
142             ModuleUpdaterSupport moduleUpdaterSupport = getModuleUpdater(updateModule);
143             try {
144 
145                 boolean updateDone = moduleUpdaterSupport.updateDone(context, appToUpdate, appUpdateError);
146                 if (updateDone) {
147                     doRestart = true;
148                 }
149 
150             } catch (ApplicationUpdateException e) {
151                 throw ApplicationActionException.propagateError(action, e);
152             }
153         }
154 
155         if (doRestart) {
156 
157             applicationUpdated = true;
158         }
159 
160     }
161 
162     @Override
163     public void aborted(String propertiesURL, Exception eee) {
164         if (log.isErrorEnabled()) {
165             log.error("Could not update from " + propertiesURL, eee);
166         }
167         throw ApplicationActionException.propagateError(action, eee);
168     }
169 
170     protected ModuleUpdaterSupport getModuleUpdater(UpdateModule updateModule) {
171         return allUpdaters.get(updateModule);
172     }
173 
174 }