View Javadoc
1   package fr.ifremer.quadrige3.ui.swing.content.db;
2   
3   /*-
4    * #%L
5    * Quadrige3 Core :: Quadrige3 UI Common
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2017 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (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 Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
24   */
25  
26  
27  import com.google.common.collect.Maps;
28  import fr.ifremer.quadrige3.core.security.QuadrigeUserAuthority;
29  import fr.ifremer.quadrige3.core.security.SecurityContextHelper;
30  import fr.ifremer.quadrige3.core.service.ClientServiceLocator;
31  import fr.ifremer.quadrige3.ui.swing.callback.DatabaseUpdaterCallBack;
32  import fr.ifremer.quadrige3.ui.swing.AbstractUIHandler;
33  import fr.ifremer.quadrige3.ui.swing.ApplicationUIContext;
34  import fr.ifremer.quadrige3.ui.swing.ApplicationUIUtil;
35  import jaxx.runtime.SwingUtil;
36  import org.apache.commons.lang3.StringUtils;
37  import org.apache.commons.logging.Log;
38  import org.apache.commons.logging.LogFactory;
39  import org.nuiton.updater.ApplicationInfo;
40  import org.nuiton.updater.ApplicationUpdater;
41  import org.nuiton.version.Version;
42  
43  import javax.swing.SwingUtilities;
44  import java.util.Map;
45  
46  import static org.nuiton.i18n.I18n.n;
47  import static org.nuiton.i18n.I18n.t;
48  
49  /**
50   * <p>DbManagerUIHandler class.</p>
51   *
52   * @since 1.0
53   */
54  public class DbManagerUIHandler extends AbstractUIHandler<ApplicationUIContext, DbManagerUI> {
55  
56      /**
57       * Logger.
58       */
59      private static final Log LOG = LogFactory.getLog(DbManagerUIHandler.class);
60  
61      /**
62       * <p>updateMessage.</p>
63       */
64      public void updateMessage() {
65  
66          boolean dbExist = getContext().isDbExist();
67          boolean dbLoaded = getContext().isPersistenceLoaded();
68  
69          if (LOG.isDebugEnabled()) {
70              LOG.debug("Rebuild information text... (dbExist?" + dbExist
71                  + "/ dbLoaded?" + dbLoaded + ")");
72          }
73  
74          String message;
75  
76          if (dbExist) {
77  
78  //            String jdbcUrl = config.getJdbcUrl();
79  
80              Map<String, String> characteristics = Maps.newLinkedHashMap();
81  
82              String title;
83  
84              characteristics.put(t("quadrige3.dbManager.characteristic.url"), getConfig().getJdbcUrl());
85  
86              if (dbLoaded) {
87  
88                  Version dbVersion = ClientServiceLocator.instance().getPersistenceService().getDbVersion();
89  
90                  // db loaded
91                  title = n("quadrige3.dbManager.info.db.loaded");
92  
93                  characteristics.put(t("quadrige3.dbManager.characteristic.schemaVersion"), dbVersion == null ? "not found" : dbVersion.toString());
94  
95              } else {
96  
97                  // no db loaded
98                  title = n("quadrige3.dbManager.info.no.db.loaded");
99              }
100 
101             // get referential version
102             String urlDb = getConfig().getUpdateDataUrl();
103             if (getContext().checkUpdateReachable(urlDb, false)) {
104 
105                 ApplicationUpdater up = new ApplicationUpdater();
106                 Map<String, ApplicationInfo> dbVersions = up.getVersions(urlDb, getConfig().getDataDirectory());
107 
108                 ApplicationInfo updateDbVersion = dbVersions.get(DatabaseUpdaterCallBack.DB_UPDATE_NAME);
109 
110                 if (updateDbVersion != null) {
111                     String currentReferentialVersion = updateDbVersion.oldVersion;
112                     String newReferentialVersion = updateDbVersion.newVersion;
113                     characteristics.put(t("quadrige3.dbManager.characteristic.referentialVersion"), currentReferentialVersion);
114                     if (newReferentialVersion != null) {
115                         characteristics.put(t("quadrige3.dbManager.characteristic.lastReferentialVersion"), newReferentialVersion);
116                     }
117                 } else {
118                     characteristics.put(t("quadrige3.dbManager.characteristic.referentialVersion.undefined"), "");
119                 }
120             }
121 
122             StringBuilder characteristicsToString = new StringBuilder("<ul>");
123             for (Map.Entry<String, String> entry : characteristics.entrySet()) {
124                 characteristicsToString.append("<li>");
125                 characteristicsToString.append(entry.getKey());
126                 characteristicsToString.append(" : <strong>");
127                 characteristicsToString.append(entry.getValue());
128                 characteristicsToString.append("</strong></li>");
129             }
130             characteristicsToString.append("</ul>");
131 
132             message = t(title, characteristicsToString);
133 
134         } else {
135 
136             // db does not exist
137             message = t("quadrige3.dbManager.info.no.db.exist");
138         }
139         String result = ApplicationUIUtil.getHtmlString(message);
140         getUI().getInformationArea().setText(result);
141     }
142 
143     /** {@inheritDoc} */
144     @Override
145     public void beforeInit(DbManagerUI ui) {
146         super.beforeInit(ui);
147         ui.setContextValue(getContext());
148 
149         ui.setContextValue(SwingUtil.createActionIcon("manage-db"));
150 
151     }
152 
153     /** {@inheritDoc} */
154     @Override
155     public void afterInit(DbManagerUI ui) {
156 
157         initUI(ui);
158         clearValidators();
159 
160         getModel().addPropertyChangeListener(ApplicationUIContext.PROPERTY_DB_EXIST, evt -> {
161 
162             Boolean dbExist = (Boolean) evt.getNewValue();
163             String mnemonic;
164             if (dbExist) {
165                 mnemonic = t("quadrige3.dbManager.action.reinstallDb.mnemonic");
166             } else {
167                 mnemonic = t("quadrige3.dbManager.action.installDb.mnemonic");
168             }
169             if (StringUtils.isNotBlank(mnemonic)) {
170                 getUI().getInstallDbButton().setMnemonic(mnemonic.charAt(0));
171             }
172 
173         });
174 
175         updateMessage();
176 
177         SwingUtilities.invokeLater(
178                 () -> getContext().getMainUI().getBody().repaint()
179         );
180 
181         // Export all referential to create a install DB : only available for local administrator (see mantis #26694)
182         getUI().getExportAllReferentialToFileButton().setEnabled(
183                 SecurityContextHelper.hasAuthority(QuadrigeUserAuthority.LOCAL_ADMIN)
184         );
185     }
186 
187     /**
188      * <p>getInstallButtonText.</p>
189      *
190      * @param dbExist a boolean.
191      * @return a {@link String} object.
192      */
193     public String getInstallButtonText(boolean dbExist) {
194         String result;
195         if (dbExist) {
196             result = t("quadrige3.dbManager.action.reinstallDb");
197         } else {
198             result = t("quadrige3.dbManager.action.installDb");
199         }
200         return result;
201     }
202 
203     /**
204      * <p>getInstallButtonTip.</p>
205      *
206      * @param dbExist a boolean.
207      * @return a {@link String} object.
208      */
209     public String getInstallButtonTip(boolean dbExist) {
210         String result;
211         if (dbExist) {
212             result = t("quadrige3.dbManager.action.reinstallDb.tip");
213         } else {
214             result = t("quadrige3.dbManager.action.installDb.tip");
215         }
216         return result;
217     }
218 }