1 package fr.ifremer.tutti.ui.swing.update.module;
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 fr.ifremer.tutti.ui.swing.TuttiUIContext;
28 import fr.ifremer.tutti.ui.swing.update.ApplicationUpdateException;
29 import fr.ifremer.tutti.ui.swing.updater.UpdateModule;
30 import fr.ifremer.tutti.ui.swing.util.auth.AuthenticationInfo;
31 import org.nuiton.updater.ApplicationInfo;
32
33 import java.util.Map;
34
35 import static org.nuiton.i18n.I18n.t;
36
37
38
39
40
41
42
43 public abstract class ModuleUpdaterSupport {
44
45 protected final UpdateModule updateModule;
46
47 public ModuleUpdaterSupport(UpdateModule updateModule) {
48 this.updateModule = updateModule;
49 }
50
51 public UpdateModule getUpdateModule() {
52 return updateModule;
53 }
54
55 public boolean matchUpdate(ApplicationInfo info) {
56 return updateModule.name().toLowerCase().equals(info.name);
57 }
58
59 public ApplicationInfo updateToDo(TuttiUIContext context, Map<String, ApplicationInfo> appToUpdate) {
60
61 ApplicationInfo info = getInfo(appToUpdate);
62
63 if (info != null) {
64
65 if (info.needAuthentication) {
66
67 AuthenticationInfo authenticationInfo = context.getAuthenticationInfo(info.url);
68 if (authenticationInfo != null) {
69 info.setAuthentication(authenticationInfo.getLogin(), authenticationInfo.getPassword());
70 }
71 }
72 }
73
74 onUpdateToDo(context, info);
75
76 return info;
77
78 }
79
80 public boolean updateDone(TuttiUIContext context,
81 Map<String, ApplicationInfo> appToUpdate,
82 Map<String, Exception> appUpdateError) throws ApplicationUpdateException {
83
84 ApplicationInfo info = getInfo(appToUpdate);
85
86 Exception error = getError(appUpdateError);
87
88 if (error != null) {
89
90 String errorMessage;
91 if (info != null && info.needAuthentication) {
92 errorMessage = t("tutti.update.error.with.auth", getLabel());
93 } else {
94 errorMessage = t("tutti.update.error.with.noauth", getLabel());
95 }
96
97
98 throw new ApplicationUpdateException(updateModule, errorMessage, error);
99
100 }
101
102 boolean doRestart = false;
103 if (info != null) {
104
105 doRestart = true;
106
107 onUpdateDone(context, info);
108
109 }
110 return doRestart;
111 }
112
113 protected abstract void onUpdateToDo(TuttiUIContext context, ApplicationInfo info) ;
114
115 protected abstract void onUpdateDone(TuttiUIContext context, ApplicationInfo info);
116
117 public abstract String getLabel();
118
119 protected ApplicationInfo getInfo(Map<String, ApplicationInfo> appToUpdate) {
120 return appToUpdate.get(updateModule.name().toLowerCase());
121 }
122
123 protected Exception getError(Map<String, Exception> appUpdateError) {
124 return appUpdateError.get(updateModule.name().toLowerCase());
125 }
126 }