Commit f775df89 authored by R.W.Majeed's avatar R.W.Majeed

connection and properties unified

parent 8c017829
......@@ -30,6 +30,7 @@ import java.sql.Timestamp;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.logging.Logger;
......@@ -172,7 +173,10 @@ public class I2b2Inserter extends AbstractObservationHandler implements Observat
* @throws ClassNotFoundException if database driver not found
*/
private void open(Map<String,String> props)throws SQLException, ClassNotFoundException{
db = PostgresExtension.getConnection(props);
Properties jdbc = new Properties();
PostgresExtension.copyProperties(props, "jdbc", jdbc);
PostgresExtension.copyProperties(props, "data.jdbc", jdbc);
db = PostgresExtension.getConnection(jdbc);
db.setAutoCommit(false);
this.nullProviderId = props.get("nullProvider");
prepareStatements(props);
......
......@@ -42,7 +42,7 @@ import de.sekmi.histream.Plugin;
*
* @param <T> extension instance type
*/
abstract class PostgresExtension<T> implements Extension<T>, Plugin {
public abstract class PostgresExtension<T> implements Extension<T>, Plugin {
private static final int defaultFetchSize = 10000;
private static final String driver = "org.postgresql.Driver";
protected Map<String,String> config;
......@@ -52,14 +52,35 @@ abstract class PostgresExtension<T> implements Extension<T>, Plugin {
this.config = configuration;
}
public static Connection getConnection(Map<String,String> props) throws SQLException, ClassNotFoundException{
private static Connection getConnection(Map<String,String> props) throws SQLException, ClassNotFoundException{
Class.forName(driver);
Properties jdbcProps = new Properties();
// TODO put only properties relevant to jdbc
jdbcProps.putAll(props);
return DriverManager.getConnection("jdbc:postgresql://"+props.get("host")+":"+props.get("port")+"/"+props.get("database"), jdbcProps);
}
public static Connection getConnection(Properties props) throws SQLException, ClassNotFoundException{
Class.forName(driver);
return DriverManager.getConnection("jdbc:postgresql://"+props.get("host")+":"+props.get("port")+"/"+props.get("database"), props);
}
/**
* Each key in src that starts with keyPrefix is copied (without the prefix) and its value to dest
* @param src map containing key,value pairs
* @param keyPrefix prefix to match src keys
* @param dest destination properties
*/
public static void copyProperties(Map<String,String> src, String keyPrefix, Properties dest){
src.forEach(
(key,value) -> {
if( key.startsWith(keyPrefix) ){
dest.put(key.substring(keyPrefix.length()), value);
}
}
);
}
protected void open() throws ClassNotFoundException, SQLException{
db = getConnection(config);
prepareStatements();
......
......@@ -22,6 +22,7 @@ import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
import de.sekmi.histream.Plugin;
import de.sekmi.histream.i2b2.PostgresExtension;
import de.sekmi.histream.ontology.Concept;
import de.sekmi.histream.ontology.Ontology;
import de.sekmi.histream.ontology.OntologyException;
......@@ -382,21 +383,6 @@ public class Import implements AutoCloseable{
this.sourceTimestamp = new Timestamp(ontology.lastModified());
}
/**
* Each key in src that starts with keyPrefix is copied (without the prefix) and its value to dest
* @param src map containing key,value pairs
* @param keyPrefix prefix to match src keys
* @param dest destination properties
*/
private void copyProperties(Map<String,String> src, String keyPrefix, Properties dest){
src.forEach(
(key,value) -> {
if( key.startsWith(keyPrefix) ){
dest.put(key.substring(keyPrefix.length()), value);
}
}
);
}
private void openDatabase(Map<String,String> props) throws ClassNotFoundException, SQLException{
Class.forName("org.postgresql.Driver");
......@@ -404,22 +390,22 @@ public class Import implements AutoCloseable{
this.config = props;
sourceId = config.get("meta.sourcesystem_cd");
String connectString = "jdbc:postgresql://"+props.get("jdbc.host")+":"+props.get("jdbc.port")+"/"+props.get("jdbc.database");
//String connectString = "jdbc:postgresql://"+props.get("jdbc.host")+":"+props.get("jdbc.port")+"/"+props.get("jdbc.database");
Properties jdbc;
// use only properties relevant to JDBC
// meta connection
jdbc = new Properties();
copyProperties(config, "meta.jdbc.", jdbc);
copyProperties(config, "jdbc.", jdbc);
dbMeta = DriverManager.getConnection(connectString, jdbc);
PostgresExtension.copyProperties(config, "jdbc.", jdbc);
PostgresExtension.copyProperties(config, "meta.jdbc.", jdbc);
dbMeta = PostgresExtension.getConnection(jdbc);
dbMeta.setAutoCommit(true);
// data connection
jdbc = new Properties();
copyProperties(config, "data.jdbc.", jdbc);
copyProperties(config, "jdbc.", jdbc);
dbData = DriverManager.getConnection(connectString, jdbc);
PostgresExtension.copyProperties(config, "jdbc.", jdbc);
PostgresExtension.copyProperties(config, "data.jdbc.", jdbc);
dbData = PostgresExtension.getConnection(jdbc);
dbData.setAutoCommit(true);
prepareStatements();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment