Search in sources :

Example 6 with DatabaseConnectionField

use of com.sldeditor.common.data.DatabaseConnectionField in project sldeditor by robward-scisys.

the class DatabaseConnectionTest method testDatabaseConnection.

/**
 * Test method for
 * {@link com.sldeditor.common.data.DatabaseConnection#DatabaseConnection(com.sldeditor.common.data.DatabaseConnection)}.
 */
@Test
public void testDatabaseConnection() {
    Param param = GeoPkgDataStoreFactory.DBTYPE;
    List<DatabaseConnectionField> expectedDetailList = new ArrayList<DatabaseConnectionField>();
    expectedDetailList.add(new DatabaseConnectionField(GeoPkgDataStoreFactory.DATABASE));
    expectedDetailList.add(new DatabaseConnectionField(GeoPkgDataStoreFactory.USER));
    String expectedDatabaseTypeLabel = "GeoPackage";
    boolean expectedSupportsDuplication = false;
    DatabaseConnection test = new DatabaseConnection(param, expectedDatabaseTypeLabel, expectedSupportsDuplication, expectedDetailList, new DatabaseConnectionName() {

        @Override
        public String getConnectionName(String duplicatePrefix, int noOfDuplicates, Map<String, String> properties) {
            String connectionName = Localisation.getString(DatabaseConnectionTest.class, "common.notSet");
            String databaseName = properties.get(GeoPkgDataStoreFactory.DATABASE.key);
            if (databaseName != null) {
                File f = new File(databaseName);
                if (f.isFile()) {
                    connectionName = f.getName();
                }
            }
            for (int i = 0; i < noOfDuplicates; i++) {
                connectionName = duplicatePrefix + connectionName;
            }
            return connectionName;
        }
    });
    Map<String, String> connectionDataMap = new HashMap<String, String>();
    connectionDataMap.put(GeoPkgDataStoreFactory.DATABASE.key, "test datatbase");
    test.setConnectionDataMap(connectionDataMap);
    assertEquals(test.isSupportsDuplication(), expectedSupportsDuplication);
    assertNotNull(test.getConnectionName());
    assertNotNull(test.getDatabaseConnectionName());
    assertEquals(test.getDetailList(), expectedDetailList);
    assertEquals(test.getDatabaseTypeLabel(), expectedDatabaseTypeLabel);
    assertTrue(test.hashCode() != 0);
    assertEquals(1, test.getExpectedKeys().size());
    assertEquals(GeoPkgDataStoreFactory.DATABASE.key, test.getExpectedKeys().get(0));
    assertEquals(test.getDatabaseType(), param.sample);
    assertEquals(test.getConnectionDataMap().get(GeoPkgDataStoreFactory.DATABASE.key), connectionDataMap.get(GeoPkgDataStoreFactory.DATABASE.key));
    assertEquals(test.getConnectionDataMap().get(GeoPkgDataStoreFactory.DBTYPE.key), GeoPkgDataStoreFactory.DBTYPE.sample);
    String expectedUserName = "test user";
    test.setUserName(expectedUserName);
    assertEquals(test.getUserName(), expectedUserName);
    assertNotNull(test.getDBConnectionParams());
    String expectedPassword = "password123";
    test.setPassword(expectedPassword);
    assertEquals(test.getPassword(), expectedPassword);
    // Duplicate
    DatabaseConnection testCopy = test.duplicate();
    assertTrue(testCopy.compareTo(test) < 0);
    testCopy.update(test);
    assertEquals(testCopy, test);
    assertEquals(testCopy, testCopy);
    testCopy.setUserName("new username");
    assertFalse(testCopy.equals(test));
    testCopy.update(test);
    assertTrue(testCopy.equals(test));
    String actualString = test.encodeAsString();
    DatabaseConnection newTest = DatabaseConnection.decodeString(actualString);
    assertTrue(newTest.equals(test));
}
Also used : DatabaseConnectionField(com.sldeditor.common.data.DatabaseConnectionField) DatabaseConnectionName(com.sldeditor.tool.dbconnectionlist.DatabaseConnectionName) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Param(org.geotools.data.DataAccessFactory.Param) DatabaseConnection(com.sldeditor.common.data.DatabaseConnection) File(java.io.File) Test(org.junit.Test)

Example 7 with DatabaseConnectionField

use of com.sldeditor.common.data.DatabaseConnectionField in project sldeditor by robward-scisys.

the class DatabaseConnector method setConnection.

/*
     * (non-Javadoc)
     * 
     * @see com.sldeditor.tool.dbconnectionlist.DatabaseConnectionConfigInterface#setConnection(com.sldeditor.common.data.DatabaseConnection)
     */
@Override
public void setConnection(DatabaseConnection connection) {
    this.databaseConnection = connection;
    if (connection != null) {
        for (DatabaseConnectionField field : databaseConnection.getDetailList()) {
            createField(field);
            if (field.isUsername()) {
                userKey = field.getKey();
            } else if (field.isPassword()) {
                passwordKey = field.getKey();
            }
        }
        Map<String, String> properties = connection.getConnectionDataMap();
        for (String fieldName : textFieldMap.keySet()) {
            JTextField textField = textFieldMap.get(fieldName);
            textField.setText(properties.get(fieldName));
        }
        if (userKey != null) {
            JTextField textField = textFieldMap.get(userKey);
            textField.setText(connection.getUserName());
        }
        if (passwordKey != null) {
            JTextField textField = textFieldMap.get(passwordKey);
            textField.setText(connection.getPassword());
        }
    }
}
Also used : DatabaseConnectionField(com.sldeditor.common.data.DatabaseConnectionField) JTextField(javax.swing.JTextField)

Example 8 with DatabaseConnectionField

use of com.sldeditor.common.data.DatabaseConnectionField in project sldeditor by robward-scisys.

the class DatabaseConnectionFactory method createGeoPackage.

/**
 * Creates a new DatabaseConnection object for a GeoPackage.
 *
 * @return the database connection
 */
public static DatabaseConnection createGeoPackage() {
    List<DatabaseConnectionField> list = new ArrayList<DatabaseConnectionField>();
    FileNameExtensionFilter filter = new FileNameExtensionFilter(Localisation.getString(DatabaseConnector.class, "DatabaseConnectorGeoPkg.fileExtension") + " (*." + GEOPACKAGE_FILE_EXTENSION + ")", GEOPACKAGE_FILE_EXTENSION);
    list.add(new DatabaseConnectionField(GeoPkgDataStoreFactory.DATABASE, filter));
    list.add(new DatabaseConnectionField(GeoPkgDataStoreFactory.USER));
    GeoPkgDataStoreFactory factory = new GeoPkgDataStoreFactory();
    DatabaseConnection databaseConnection = new DatabaseConnection(GeoPkgDataStoreFactory.DBTYPE, factory.getDisplayName(), false, list, new DatabaseConnectionName() {

        @Override
        public String getConnectionName(String duplicatePrefix, int noOfTimesDuplicated, Map<String, String> properties) {
            String connectionName = Localisation.getString(DatabaseConnectionFactory.class, "common.notSet");
            String databaseName = properties.get(JDBCDataStoreFactory.DATABASE.key);
            if (databaseName != null) {
                File f = new File(databaseName);
                if (f.isFile()) {
                    connectionName = f.getName();
                }
            }
            for (int i = 0; i < noOfTimesDuplicated; i++) {
                connectionName = duplicatePrefix + connectionName;
            }
            return connectionName;
        }
    });
    return databaseConnection;
}
Also used : DatabaseConnectionField(com.sldeditor.common.data.DatabaseConnectionField) ArrayList(java.util.ArrayList) FileNameExtensionFilter(javax.swing.filechooser.FileNameExtensionFilter) GeoPkgDataStoreFactory(org.geotools.geopkg.GeoPkgDataStoreFactory) DatabaseConnection(com.sldeditor.common.data.DatabaseConnection) File(java.io.File)

Example 9 with DatabaseConnectionField

use of com.sldeditor.common.data.DatabaseConnectionField in project sldeditor by robward-scisys.

the class DatabaseConnectionFactory method createSQLServer.

/**
 * Creates a new DatabaseConnection object for SQL Server.
 *
 * @return the database connection
 */
public static DatabaseConnection createSQLServer() {
    List<DatabaseConnectionField> list = new ArrayList<DatabaseConnectionField>();
    list.add(new DatabaseConnectionField(JTDSSqlServerDataStoreFactory.HOST));
    list.add(new DatabaseConnectionField(JTDSSqlServerDataStoreFactory.PORT));
    list.add(new DatabaseConnectionField(JTDSSqlServerDataStoreFactory.INSTANCE));
    list.add(new DatabaseConnectionField(JTDSSqlServerDataStoreFactory.SCHEMA));
    list.add(new DatabaseConnectionField(JTDSSqlServerDataStoreFactory.DATABASE));
    list.add(new DatabaseConnectionField(JTDSSqlServerDataStoreFactory.USER));
    list.add(new DatabaseConnectionField(JTDSSqlServerDataStoreFactory.PASSWD));
    list.add(new DatabaseConnectionField(JTDSSqlServerDataStoreFactory.GEOMETRY_METADATA_TABLE));
    JDTSSQLServerJNDIDataStoreFactory factory = new JDTSSQLServerJNDIDataStoreFactory();
    DatabaseConnection databaseConnection = new DatabaseConnection(JTDSSqlServerDataStoreFactory.DBTYPE, factory.getDisplayName(), true, list, new DatabaseConnectionName() {

        @Override
        public String getConnectionName(String duplicatePrefix, int noOfTimesDuplicated, Map<String, String> properties) {
            String connectionName = String.format("%s/%s@%s:%s", properties.get(JTDSSqlServerDataStoreFactory.INSTANCE.key), properties.get(JTDSSqlServerDataStoreFactory.DATABASE.key), properties.get(JTDSSqlServerDataStoreFactory.HOST.key), properties.get(JTDSSqlServerDataStoreFactory.PORT.key));
            for (int i = 0; i < noOfTimesDuplicated; i++) {
                connectionName = duplicatePrefix + connectionName;
            }
            return connectionName;
        }
    });
    return databaseConnection;
}
Also used : DatabaseConnectionField(com.sldeditor.common.data.DatabaseConnectionField) JDTSSQLServerJNDIDataStoreFactory(org.geotools.data.sqlserver.jtds.JDTSSQLServerJNDIDataStoreFactory) ArrayList(java.util.ArrayList) DatabaseConnection(com.sldeditor.common.data.DatabaseConnection)

Example 10 with DatabaseConnectionField

use of com.sldeditor.common.data.DatabaseConnectionField in project sldeditor by robward-scisys.

the class DatabaseConnectionFactory method createPostgres.

/**
 * Creates a new DatabaseConnection object for Postgres.
 *
 * @return the database connection
 */
public static DatabaseConnection createPostgres() {
    List<DatabaseConnectionField> list = new ArrayList<DatabaseConnectionField>();
    list.add(new DatabaseConnectionField(PostgisNGDataStoreFactory.HOST));
    list.add(new DatabaseConnectionField(PostgisNGDataStoreFactory.PORT));
    list.add(new DatabaseConnectionField(PostgisNGDataStoreFactory.DATABASE));
    list.add(new DatabaseConnectionField(PostgisNGDataStoreFactory.SCHEMA));
    list.add(new DatabaseConnectionField(PostgisNGDataStoreFactory.USER));
    list.add(new DatabaseConnectionField(PostgisNGDataStoreFactory.PASSWD));
    PostgisNGDataStoreFactory factory = new PostgisNGDataStoreFactory();
    DatabaseConnection databaseConnection = new DatabaseConnection(PostgisNGDataStoreFactory.DBTYPE, factory.getDisplayName(), true, list, new DatabaseConnectionName() {

        @Override
        public String getConnectionName(String duplicatePrefix, int noOfTimesDuplicated, Map<String, String> properties) {
            String connectionName = String.format("%s/%s@%s:%s", properties.get(PostgisNGDataStoreFactory.SCHEMA.key), properties.get(PostgisNGDataStoreFactory.DATABASE.key), properties.get(PostgisNGDataStoreFactory.HOST.key), properties.get(PostgisNGDataStoreFactory.PORT.key));
            for (int i = 0; i < noOfTimesDuplicated; i++) {
                connectionName = duplicatePrefix + connectionName;
            }
            return connectionName;
        }
    });
    return databaseConnection;
}
Also used : DatabaseConnectionField(com.sldeditor.common.data.DatabaseConnectionField) ArrayList(java.util.ArrayList) PostgisNGDataStoreFactory(org.geotools.data.postgis.PostgisNGDataStoreFactory) DatabaseConnection(com.sldeditor.common.data.DatabaseConnection)

Aggregations

DatabaseConnectionField (com.sldeditor.common.data.DatabaseConnectionField)11 DatabaseConnection (com.sldeditor.common.data.DatabaseConnection)10 ArrayList (java.util.ArrayList)10 File (java.io.File)4 FileNameExtensionFilter (javax.swing.filechooser.FileNameExtensionFilter)2 Param (org.geotools.data.DataAccessFactory.Param)2 DatabaseConnectionName (com.sldeditor.tool.dbconnectionlist.DatabaseConnectionName)1 HashMap (java.util.HashMap)1 JTextField (javax.swing.JTextField)1 DB2NGDataStoreFactory (org.geotools.data.db2.DB2NGDataStoreFactory)1 H2DataStoreFactory (org.geotools.data.h2.H2DataStoreFactory)1 MySQLDataStoreFactory (org.geotools.data.mysql.MySQLDataStoreFactory)1 OracleNGDataStoreFactory (org.geotools.data.oracle.OracleNGDataStoreFactory)1 PostgisNGDataStoreFactory (org.geotools.data.postgis.PostgisNGDataStoreFactory)1 SpatiaLiteDataStoreFactory (org.geotools.data.spatialite.SpatiaLiteDataStoreFactory)1 JDTSSQLServerJNDIDataStoreFactory (org.geotools.data.sqlserver.jtds.JDTSSQLServerJNDIDataStoreFactory)1 TeradataDataStoreFactory (org.geotools.data.teradata.TeradataDataStoreFactory)1 GeoPkgDataStoreFactory (org.geotools.geopkg.GeoPkgDataStoreFactory)1 Test (org.junit.Test)1