View Javadoc
1   package fr.ifremer.tutti.ui.swing.update.module;
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 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   * Created on 1/28/15.
39   *
40   * @author Tony Chemit - chemit@codelutin.com
41   * @since 3.13
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                  // ask auth
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              // something bad while updating application
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 }