View Javadoc
1   package fr.ifremer.tutti.ui.swing.content.db;
2   
3   /*
4    * #%L
5    * Tutti :: UI
6    * %%
7    * Copyright (C) 2012 - 2014 Ifremer
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU General Public License as
11   * published by the Free Software Foundation, either version 3 of the 
12   * License, or (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Public 
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/gpl-3.0.html>.
22   * #L%
23   */
24  
25  import com.google.common.collect.Maps;
26  import fr.ifremer.tutti.TuttiConfiguration;
27  import fr.ifremer.tutti.ui.swing.TuttiUIContext;
28  import fr.ifremer.tutti.ui.swing.update.Updates;
29  import fr.ifremer.tutti.ui.swing.util.AbstractTuttiUIHandler;
30  import jaxx.runtime.validator.swing.SwingValidator;
31  import org.apache.commons.lang3.StringUtils;
32  import org.apache.commons.logging.Log;
33  import org.apache.commons.logging.LogFactory;
34  import org.nuiton.updater.ApplicationInfo;
35  import org.nuiton.version.Version;
36  
37  import javax.swing.JComponent;
38  import javax.swing.SwingUtilities;
39  import java.util.Map;
40  
41  import static org.nuiton.i18n.I18n.n;
42  import static org.nuiton.i18n.I18n.t;
43  
44  /**
45   * @author Tony Chemit - chemit@codelutin.com
46   * @since 1.0
47   */
48  public class DbManagerUIHandler extends AbstractTuttiUIHandler<TuttiUIContext, DbManagerUI> {
49  
50      /** Logger. */
51      private static final Log log = LogFactory.getLog(DbManagerUIHandler.class);
52  
53      public void updateMessage() {
54  
55          boolean dbExist = getContext().isDbExist();
56          boolean dbLoaded = getContext().isDbLoaded();
57  
58  
59          if (log.isInfoEnabled()) {
60              log.info("Rebuild information text... (dbExist?" + dbExist + "/ dbLoaded?" + dbLoaded + ")");
61          }
62  
63          String message;
64  
65          if (dbExist) {
66  
67              TuttiConfiguration config = getConfig();
68  
69              String jdbcUrl = config.getJdbcUrl();
70  
71              Map<String, String> caracteristics = Maps.newLinkedHashMap();
72  
73              String title;
74  
75              caracteristics.put(t("tutti.dbManager.caracteristic.url"), jdbcUrl);
76  
77              if (dbLoaded) {
78  
79                  Version dbVersion = getContext().getPersistenceService().getSchemaVersion();
80  
81                  // db loaded
82                  title = n("tutti.dbManager.info.db.loaded");
83  
84                  caracteristics.put(t("tutti.dbManager.caracteristic.schemaVersion"), dbVersion.toString());
85  
86              } else {
87  
88                  // no db loaded
89                  title = n("tutti.dbManager.info.no.db.loaded");
90              }
91  
92              // get referential version
93  
94              if (getContext().checkUpdateDataReachable(false)) {
95  //                String urlDb = config.getUpdateDataUrl();
96  
97                  ApplicationInfo updateDbVersion = Updates.getDatabaseUpdateVersion(getConfig());
98  
99                  String currentReferentialVersion = updateDbVersion.oldVersion;
100                 String newReferentialVersion = updateDbVersion.newVersion;
101                 caracteristics.put(t("tutti.dbManager.caracteristic.referentialVersion"), currentReferentialVersion);
102                 if (newReferentialVersion != null) {
103                     caracteristics.put(t("tutti.dbManager.caracteristic.lastReferentialVersion"), newReferentialVersion);
104                 }
105             }
106 
107             StringBuilder caracteristicsToString = new StringBuilder("<ul>");
108             for (Map.Entry<String, String> entry : caracteristics.entrySet()) {
109                 caracteristicsToString.append("<li>");
110                 caracteristicsToString.append(entry.getKey());
111                 caracteristicsToString.append(" : <strong>");
112                 caracteristicsToString.append(entry.getValue());
113                 caracteristicsToString.append("</strong></li>");
114             }
115             caracteristicsToString.append("</ul>");
116 
117             message = t(title, caracteristicsToString);
118 
119         } else {
120 
121             // db does not exist
122             message = t("tutti.dbManager.info.no.db.exist");
123         }
124         String result = "<html><body>" + message + "</body></html>";
125         getUI().getInformationArea().setText(result);
126     }
127 
128     @Override
129     public void beforeInit(DbManagerUI ui) {
130         super.beforeInit(ui);
131         ui.setContextValue(getContext());
132     }
133 
134     @Override
135     public void afterInit(DbManagerUI ui) {
136 
137         initUI(ui);
138 
139         getModel().addPropertyChangeListener(TuttiUIContext.PROPERTY_DB_EXIST, evt -> {
140 
141             Boolean dbExist = (Boolean) evt.getNewValue();
142             String mnemonic;
143             if (dbExist) {
144                 mnemonic = t("tutti.dbManager.action.reinstallDb.mnemonic");
145             } else {
146                 mnemonic = t("tutti.dbManager.action.installDb.mnemonic");
147             }
148             if (StringUtils.isNotBlank(mnemonic)) {
149                 DbManagerUIHandler.this.ui.getInstallOrReinstallDbButton().setMnemonic(mnemonic.charAt(0));
150             }
151 
152         });
153 
154         updateMessage();
155 
156         SwingUtilities.invokeLater(
157                 () -> getContext().getMainUI().getBody().repaint()
158         );
159     }
160 
161     @Override
162     protected JComponent getComponentToFocus() {
163         return getUI().getUpgradeDbButton();
164     }
165 
166     @Override
167     public void onCloseUI() {
168     }
169 
170     @Override
171     public SwingValidator<TuttiUIContext> getValidator() {
172         return null;
173     }
174 
175     public String getInstallButtonText(boolean dbExist) {
176         String result;
177         if (dbExist) {
178             result = t("tutti.dbManager.action.reinstallDb");
179         } else {
180             result = t("tutti.dbManager.action.installDb");
181         }
182         return result;
183     }
184 
185     public String getInstallButtonTip(boolean dbExist) {
186         String result;
187         if (dbExist) {
188             result = t("tutti.dbManager.action.reinstallDb.tip");
189         } else {
190             result = t("tutti.dbManager.action.installDb.tip");
191         }
192         return result;
193     }
194 }