1 package fr.ifremer.tutti.ui.swing.update;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
47
48
49
50
51 public abstract class TuttiUpdaterCallBackSupport implements ApplicationUpdaterCallback {
52
53
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
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 }