Java Code Examples for org.geotools.jdbc.JDBCDataStoreFactory

The following examples show how to use org.geotools.jdbc.JDBCDataStoreFactory. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
/** Populate name map. */
private static void populateNameMap() {
    Iterator<DataStoreFactorySpi> datastore = DataStoreFinder.getAvailableDataStores();

    while (datastore.hasNext()) {
        DataStoreFactorySpi dSPI = datastore.next();

        Param dbType = null;
        for (Param param : dSPI.getParametersInfo()) {
            if (param.key.equals(JDBCDataStoreFactory.DBTYPE.key)) {
                dbType = param;
                break;
            }
        }
        if (dbType != null) {
            nameMap.put(dSPI.getDisplayName(), (String) dbType.sample);
        }
    }
}
 
Example 2
/**
 * Gets the database connection.
 *
 * @param filename the filename
 * @return the new connection
 */
public static DatabaseConnection getConnection(String filename) {
    List<FileHandlerInterface> list = getFileHandlers();

    if (filename != null) {
        for (FileHandlerInterface handler : list) {
            for (String fileExtension : handler.getFileExtensionList()) {
                if (filename.endsWith(fileExtension)) {
                    String dbConnectionType = fileHandlerMap.get(handler);

                    DatabaseConnection dbConnection = createDefault(dbConnectionType);

                    if (dbConnection != null) {
                        Map<String, String> connectionDataMap = new HashMap<>();

                        connectionDataMap.put(JDBCDataStoreFactory.DATABASE.key, filename);
                        dbConnection.setConnectionDataMap(connectionDataMap);
                    }
                    return dbConnection;
                }
            }
        }
    }
    return null;
}
 
Example 3
/**
 * Display property map.
 *
 * @param propertyMap the property map
 */
private void displayPropertyMap(Map<String, Object> propertyMap) {
    reset();
    for (Entry<String, Object> entry : propertyMap.entrySet()) {
        if (!JDBCDataStoreFactory.PASSWD.key.equals(entry.getKey())) {
            model.addRow(new String[] {entry.getKey(), entry.getValue().toString()});
        }
    }
    model.fireTableDataChanged();
}
 
Example 4
/**
 * Creates a new DatabaseConnection object for H2.
 *
 * @return the database connection
 */
public static DatabaseConnection createH2() {
    List<DatabaseConnectionField> list = new ArrayList<>();

    list.add(new DatabaseConnectionField(H2DataStoreFactory.DATABASE));
    list.add(new DatabaseConnectionField(H2DataStoreFactory.USER));

    H2DataStoreFactory factory = new H2DataStoreFactory();

    return new DatabaseConnection(
            H2DataStoreFactory.DBTYPE,
            factory.getDisplayName(),
            false,
            list,
            new DatabaseConnectionNameInterface() {
                /** The Constant serialVersionUID. */
                private static final long serialVersionUID = 1L;

                @Override
                public String getConnectionName(
                        String duplicatePrefix,
                        int noOfTimesDuplicated,
                        Map<String, String> properties) {
                    String connectionName =
                            Localisation.getString(
                                    DatabaseConnectionFactory.class,
                                    Localisation.COMMON_NOT_SET);
                    String databaseName = properties.get(JDBCDataStoreFactory.DATABASE.key);
                    if (databaseName != null) {
                        File f = new File(databaseName);
                        if (f.isFile()) {
                            connectionName = f.getName();
                        }
                    }

                    return createConnectionName(
                            connectionName, noOfTimesDuplicated, duplicatePrefix);
                }
            });
}
 
Example 5
/**
 * Instantiates a new database connection field.
 *
 * @param param the param
 */
public DatabaseConnectionField(Param param) {
    super();
    this.param = param;
    if (param.key.equals(JDBCDataStoreFactory.USER.key)) {
        username = true;
    }

    password = param.isPassword();
}
 
Example 6
Source Project: gama   Source File: GamaSqlConnection.java    License: GNU General Public License v3.0 5 votes vote down vote up
private Map<String, Object> createConnectionParams(final IScope scope) {
	final Map<String, Object> connectionParameters = new HashMap<>();

	if (dbtype.equalsIgnoreCase(GamaSqlConnection.POSTGRES) || dbtype.equalsIgnoreCase(GamaSqlConnection.POSTGIS)) {
		connectionParameters.put("host", host);
		connectionParameters.put("dbtype", dbtype);
		connectionParameters.put("port", port);
		connectionParameters.put("database", database);
		connectionParameters.put("user", user);
		connectionParameters.put("passwd", passwd);
		// advanced

	} else if (dbtype.equalsIgnoreCase(GamaSqlConnection.MYSQL)) {
		connectionParameters.put(MySQLDataStoreFactory.DBTYPE.key, dbtype);
		connectionParameters.put(JDBCDataStoreFactory.HOST.key, host);
		connectionParameters.put(MySQLDataStoreFactory.PORT.key, Integer.valueOf(port));
		connectionParameters.put(JDBCDataStoreFactory.DATABASE.key, database);
		connectionParameters.put(JDBCDataStoreFactory.USER.key, user);
		connectionParameters.put(JDBCDataStoreFactory.PASSWD.key, passwd);
	} else if (dbtype.equalsIgnoreCase(GamaSqlConnection.MSSQL)) {
		connectionParameters.put("host", host);
		connectionParameters.put("dbtype", dbtype);
		connectionParameters.put("port", port);
		connectionParameters.put("database", database);
		connectionParameters.put("user", user);
		connectionParameters.put("passwd", passwd);

	} else if (dbtype.equalsIgnoreCase(GamaSqlConnection.SQLITE)) {
		final String DBRelativeLocation = FileUtils.constructAbsoluteFilePath(scope, database, true);
		// String EXTRelativeLocation =
		// GamaPreferences.LIB_SPATIALITE.value(scope).getPath();
		connectionParameters.put("dbtype", dbtype);
		connectionParameters.put("database", DBRelativeLocation);

	}
	return connectionParameters;
}
 
Example 7
/**
 * Creates a new DatabaseConnection object for a GeoPackage.
 *
 * @return the database connection
 */
public static DatabaseConnection createGeoPackage() {
    List<DatabaseConnectionField> list = new ArrayList<>();

    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();
    return new DatabaseConnection(
            GeoPkgDataStoreFactory.DBTYPE,
            factory.getDisplayName(),
            false,
            list,
            new DatabaseConnectionNameInterface() {

                /** The Constant serialVersionUID. */
                private static final long serialVersionUID = 1L;

                @Override
                public String getConnectionName(
                        String duplicatePrefix,
                        int noOfTimesDuplicated,
                        Map<String, String> properties) {
                    String connectionName =
                            Localisation.getString(
                                    DatabaseConnectionFactory.class,
                                    Localisation.COMMON_NOT_SET);
                    String databaseName = properties.get(JDBCDataStoreFactory.DATABASE.key);
                    if (databaseName != null) {
                        File f = new File(databaseName);
                        if (f.isFile()) {
                            connectionName = f.getName();
                        }
                    }

                    return createConnectionName(
                            connectionName, noOfTimesDuplicated, duplicatePrefix);
                }
            });
}
 
Example 8
/**
 * Creates a new DatabaseConnection object for a GeoPackage.
 *
 * @return the database connection
 */
public static DatabaseConnection createSpatiaLite() {
    List<DatabaseConnectionField> list = new ArrayList<>();

    list.add(new DatabaseConnectionField(SpatiaLiteDataStoreFactory.DATABASE));

    FileNameExtensionFilter filter =
            new FileNameExtensionFilter(
                    Localisation.getString(
                                    DatabaseConnector.class,
                                    "DatabaseConnectorGeoPkg.fileExtension")
                            + " (*."
                            + SPATIALITE_FILE_EXTENSION
                            + ")",
                    SPATIALITE_FILE_EXTENSION);
    list.add(new DatabaseConnectionField(SpatiaLiteDataStoreFactory.USER, filter));

    SpatiaLiteDataStoreFactory factory = new SpatiaLiteDataStoreFactory();

    return new DatabaseConnection(
            SpatiaLiteDataStoreFactory.DBTYPE,
            factory.getDisplayName(),
            false,
            list,
            new DatabaseConnectionNameInterface() {
                /** The Constant serialVersionUID. */
                private static final long serialVersionUID = 1L;

                @Override
                public String getConnectionName(
                        String duplicatePrefix,
                        int noOfTimesDuplicated,
                        Map<String, String> properties) {
                    String connectionName =
                            Localisation.getString(
                                    DatabaseConnectionFactory.class,
                                    Localisation.COMMON_NOT_SET);
                    String databaseName = properties.get(JDBCDataStoreFactory.DATABASE.key);
                    if (databaseName != null) {
                        File f = new File(databaseName);
                        if (f.isFile()) {
                            connectionName = f.getName();
                        }
                    }

                    return createConnectionName(
                            connectionName, noOfTimesDuplicated, duplicatePrefix);
                }
            });
}
 
Example 9
/**
 * Creates a new DatabaseConnection object for DB2.
 *
 * @return the database connection
 */
public static DatabaseConnection createDB2() {
    List<DatabaseConnectionField> list = new ArrayList<>();

    list.add(new DatabaseConnectionField(DB2NGDataStoreFactory.HOST));
    list.add(new DatabaseConnectionField(DB2NGDataStoreFactory.PORT));
    Param tabSchema = new Param("tabschema", String.class, "Schema", false);
    list.add(new DatabaseConnectionField(tabSchema));
    list.add(new DatabaseConnectionField(DB2NGDataStoreFactory.DATABASE));
    list.add(new DatabaseConnectionField(DB2NGDataStoreFactory.USER));
    list.add(new DatabaseConnectionField(DB2NGDataStoreFactory.PASSWD));

    DB2NGDataStoreFactory factory = new DB2NGDataStoreFactory();

    return new DatabaseConnection(
            DB2NGDataStoreFactory.DBTYPE,
            factory.getDisplayName(),
            true,
            list,
            new DatabaseConnectionNameInterface() {
                /** The Constant serialVersionUID. */
                private static final long serialVersionUID = 1L;

                @Override
                public String getConnectionName(
                        String duplicatePrefix,
                        int noOfTimesDuplicated,
                        Map<String, String> properties) {
                    String connectionName =
                            String.format(
                                    CONNECTION_STRING_4,
                                    properties.get("tabschema"),
                                    properties.get(JDBCDataStoreFactory.DATABASE.key),
                                    properties.get(JDBCDataStoreFactory.HOST.key),
                                    properties.get(JDBCDataStoreFactory.PORT.key));

                    return createConnectionName(
                            connectionName, noOfTimesDuplicated, duplicatePrefix);
                }
            });
}
 
Example 10
/**
 * Finish initializing the layer.
 *
 * @throws LayerException oops
 */
@PostConstruct
protected void initFeatures() throws LayerException {
	if (null == layerInfo) {
		return;
	}
	crs = geoService.getCrs2(layerInfo.getCrs());
	setFeatureSourceName(layerInfo.getFeatureInfo().getDataSourceName());
	try {
		if (null == super.getDataStore()) {
			Map<String, Object> params = new HashMap<String, Object>();
			if (null != url) {
				params.put(ShapefileDataStoreFactory.URLP.key, url);
			}
			if (null != dbtype) {
				params.put(JDBCDataStoreFactory.DBTYPE.key, dbtype);
			}
			if (null != parameters) {
				for (Parameter parameter : parameters) {
					params.put(parameter.getName(), parameter.getValue());
				}
			}
			if (null != dataSource) {
				params.put(JDBCDataStoreFactory.DATASOURCE.key, dataSource);
				// these are apparently required but not used
				params.put(JDBCDataStoreFactory.DATABASE.key, "some_database");
				params.put(JDBCDataStoreFactory.USER.key, "some_user");
				params.put(JDBCDataStoreFactory.PASSWD.key, "some_password");
				params.put(JDBCDataStoreFactory.HOST.key, "some host");
				params.put(JDBCDataStoreFactory.PORT.key, "0");
			}
			DataStore store = DataStoreFactory.create(params);
			super.setDataStore(store);
		}
		if (null == super.getDataStore()) {
			return;
		}
		this.featureModel = new GeoToolsFeatureModel(super.getDataStore(), layerInfo.getFeatureInfo()
				.getDataSourceName(), geoService.getSridFromCrs(layerInfo.getCrs()), converterService);
		featureModel.setLayerInfo(layerInfo);
		featureModelUsable = true;

	} catch (IOException ioe) {
		if (MAGIC_STRING_LIBRARY_MISSING.equals(ioe.getMessage())) {
			throw new LayerException(ioe, ExceptionCode.LAYER_MODEL_IO_EXCEPTION, url);
		} else {
			featureModelUsable = false;
			log.warn("The layer could not be correctly initialized: " + getId(), ioe);
		}
	} catch (LayerException le) {
		featureModelUsable = false;
		log.warn("The layer could not be correctly initialized: " + getId(), le);
	} catch (RuntimeException e) {
		featureModelUsable = false;
		log.warn("The layer could not be correctly initialized: " + getId(), e);
	}
}