/**
* OrbisGIS is a java GIS application dedicated to research in GIScience.
* OrbisGIS is developed by the GIS group of the DECIDE team of the
* Lab-STICC CNRS laboratory, see <http://www.lab-sticc.fr/>.
*
* The GIS group of the DECIDE team is located at :
*
* Laboratoire Lab-STICC – CNRS UMR 6285
* Equipe DECIDE
* UNIVERSITÉ DE BRETAGNE-SUD
* Institut Universitaire de Technologie de Vannes
* 8, Rue Montaigne - BP 561 56017 Vannes Cedex
*
* OrbisGIS is distributed under GPL 3 license.
*
* Copyright (C) 2007-2014 CNRS (IRSTV FR CNRS 2488)
* Copyright (C) 2015-2017 CNRS (Lab-STICC UMR CNRS 6285)
*
* This file is part of OrbisGIS.
*
* OrbisGIS is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* OrbisGIS is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* OrbisGIS. If not, see <http://www.gnu.org/licenses/>.
*
* For more information, please consult: <http://www.orbisgis.org/>
* or contact directly:
* info_at_ orbisgis.org
*/
package org.orbisgis.geocatalog.impl.sourceWizards.db;
import org.orbisgis.sif.multiInputPanel.*;
import org.xnap.commons.i18n.I18n;
import org.xnap.commons.i18n.I18nFactory;
import java.sql.Driver;
import java.util.List;
/**
*
* @author Erwan Bocher
*/
public class DBUIFactory {
private static final I18n I18N = I18nFactory.getI18n(DBUIFactory.class);
static String DBTYPE = "dbtype";
static String HOST = "host";
static String PORT = "port";
static String DBNAME = "dbname";
static String SSL = "ssl";
static String CONNAME = "conname";
/**
* Create a connection panel to add a new database
*
* @return
*/
public static MultiInputPanel getConnectionPanel(List<Driver> jdbcDrivers) {
int LENGTH = 20;
MultiInputPanel connectionMP = new MultiInputPanel(I18N.tr("Connection parameters"));
connectionMP.addInput(CONNAME, I18N.tr("Connexion name"), I18N.tr("MyConnexion"),
new TextBoxType(LENGTH));
connectionMP.addInput(DBTYPE, I18N.tr("Type of database"),
getDriverInput(jdbcDrivers));
connectionMP.addInput(HOST, I18N.tr("Host"),
"127.0.0.1", new TextBoxType(LENGTH));
connectionMP.addInput(PORT, I18N.tr("Default port"),
"0", new TextBoxType(LENGTH));
connectionMP.addInput(DBNAME, I18N.tr("Database name"),
"database_name", new TextBoxType(LENGTH));
connectionMP.addInput(SSL, I18N.tr("SSL"), new CheckBoxChoice(false));
connectionMP.addValidation(new MIPValidation() {
@Override
public String validate(MultiInputPanel mid) {
if (mid.getInput(CONNAME).isEmpty()) {
return I18N.tr("Please specify a connexion name");
}
if (mid.getInput(DBNAME).isEmpty()) {
return I18N.tr("The database name is mandatory");
}
String host = mid.getInput(HOST);
if (host.isEmpty()) {
return I18N.tr("The host cannot be null");
}
String port = mid.getInput(PORT);
if (port.isEmpty()) {
return I18N.tr("The port field is mandatory");
} else {
try {
Integer portNumber = Integer.valueOf(port);
if (!(portNumber >= 0 && portNumber <= 32767)) {
return I18N.tr("The port number must lie between 0 and 32767.");
}
} catch (NumberFormatException e) {
return I18N.tr("Cannot format the port code into an int");
}
}
return null;
}
});
return connectionMP;
}
/**
* Create a connection panel to edit the database connection parameters
*
* @return
*/
public static MultiInputPanel getEditConnectionPanel(String connexionName, String property, List<Driver> jdbcDrivers) {
int LENGTH = 20;
String[] connectionParams = property.split(",");
MultiInputPanel connectionMP = new MultiInputPanel(I18N.tr("Connection parameters"));
connectionMP.addInput(CONNAME, I18N.tr("Connexion name"), connexionName,
new TextBoxType(LENGTH));
connectionMP.addInput(DBTYPE, I18N.tr("Type of database"), connectionParams[0],
getDriverInput(jdbcDrivers));
connectionMP.addInput(HOST, I18N.tr("Host"),
connectionParams[1], new TextBoxType(LENGTH));
connectionMP.addInput(PORT, I18N.tr("Default port"),
connectionParams[2], new TextBoxType(LENGTH));
connectionMP.addInput(DBNAME, I18N.tr("Database name"),
connectionParams[4], new TextBoxType(LENGTH));
connectionMP.addInput(SSL, I18N.tr("SSL"), new CheckBoxChoice(Boolean.valueOf(connectionParams[3])));
connectionMP.addValidation(new MIPValidation() {
@Override
public String validate(MultiInputPanel mid) {
if (mid.getInput(CONNAME).isEmpty()) {
return I18N.tr("Please specify a connexion name");
}
if (mid.getInput(DBNAME).isEmpty()) {
return I18N.tr("The database name is mandatory");
}
String host = mid.getInput(HOST);
if (host.isEmpty()) {
return I18N.tr("The host cannot be null");
}
String port = mid.getInput(PORT);
if (port.isEmpty()) {
try {
Integer portNumber = Integer.valueOf(port);
if (portNumber >= 0 && portNumber <= 32767) {
return I18N.tr("The port number must be comprise between 0 and 32767");
}
} catch (NumberFormatException e) {
return I18N.tr("Cannot format the port code into an int");
}
}
return null;
}
});
return connectionMP;
}
/**
* Populate a combobox with all supported drivers
* @return
*/
private static InputType getDriverInput(List<Driver> jdbcDrivers) {
String[] ids = new String[jdbcDrivers.size()];
String[] texts = new String[jdbcDrivers.size()];
for(int idDriver = 0; idDriver < jdbcDrivers.size(); idDriver++){
Driver driver = jdbcDrivers.get(idDriver);
ids[idDriver] = driver.getClass().getName();
texts[idDriver] = ids[idDriver];
}
ComboBoxChoice combo = new ComboBoxChoice(ids, texts);
return combo;
}
}