Commit 76f17a41 authored by R.W.Majeed's avatar R.W.Majeed
Browse files

allow DataSource in constructors in addition to property configuration

parent dbeaa19e
Loading
Loading
Loading
Loading

CHANGELOG.txt

0 → 100644
+2 −0
Original line number Diff line number Diff line
Changelog
i2b2 integration constructors also accept DataSource for database connections.
+19 −13
Original line number Diff line number Diff line
@@ -34,6 +34,8 @@ import java.util.Objects;
import java.util.Set;
import java.util.logging.Logger;

import javax.sql.DataSource;

import de.sekmi.histream.Modifier;
import de.sekmi.histream.Observation;
import de.sekmi.histream.ObservationException;
@@ -84,14 +86,13 @@ public class I2b2Inserter extends AbstractObservationHandler implements Observat
	private int insertCount;
	
	public I2b2Inserter(Map<String,String> config) throws ClassNotFoundException, SQLException{
		this.nullUnitCd = "@"; // technically, null is allowed, but the demodata uses both '@' and ''
		this.nullLocationCd = "@"; // technically, null is allowed, but the demodata only uses '@'
		this.nullValueFlagCd = "@";// technically, null is allowed, but the demodata uses both '@' and ''
		// TODO nullBlob (technically null allowed, but '' is used in demodata)
		this.nullModifierCd = "@"; // null not allowed, @ is used in demodata
		this.nullValueTypeCd = "@"; // TODO check database
		insertCount = 0;
		open(config);
		db = PostgresExtension.getConnection(config, new String[]{"jdbc.","data.jdbc."});
		initialize(config);
	}
	
	public I2b2Inserter(DataSource ds, Map<String,String> config) throws SQLException{
		db = ds.getConnection();
		initialize(config);
	}
	
	
@@ -168,18 +169,23 @@ public class I2b2Inserter extends AbstractObservationHandler implements Observat
		
	}
	/**
	 * Opens a database connection and prepares statements
	 * Initialize the database connection
	 * @throws SQLException if preparation/initialisation failed
	 * @throws ClassNotFoundException if database driver not found 
	 */
	private void open(Map<String,String> props)throws SQLException, ClassNotFoundException{
		db = PostgresExtension.getConnection(props, new String[]{"jdbc.","data.jdbc."});
		db.setAutoCommit(false);
	private void initialize(Map<String,String> props)throws SQLException{
		this.nullUnitCd = "@"; // technically, null is allowed, but the demodata uses both '@' and ''
		this.nullLocationCd = "@"; // technically, null is allowed, but the demodata only uses '@'
		this.nullValueFlagCd = "@";// technically, null is allowed, but the demodata uses both '@' and ''
		// TODO nullBlob (technically null allowed, but '' is used in demodata)
		this.nullModifierCd = "@"; // null not allowed, @ is used in demodata
		this.nullValueTypeCd = "@"; // TODO check database
		this.nullProviderId = props.get("nullProvider");
		if( this.nullProviderId == null ){
			log.warning("property 'nullProvider' missing, using '@' (may violate foreign keys)");
			this.nullProviderId = "@";
		}
		insertCount = 0;
		db.setAutoCommit(false);
		prepareStatements(props);
	}

+19 −1
Original line number Diff line number Diff line
@@ -27,9 +27,10 @@ import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;

import javax.sql.DataSource;

import de.sekmi.histream.DateTimeAccuracy;
import de.sekmi.histream.Extension;
import de.sekmi.histream.Plugin;
@@ -97,11 +98,28 @@ public abstract class PostgresExtension<T> implements Extension<T>, Plugin {
		);
	}

	/**
	 * Open a database connection using configuration properties 
	 * with the given prefixes.
	 * @param propertyPrefixes prefix to the configuration properties
	 * @throws ClassNotFoundException if the database driver could not be loaded
	 * @throws SQLException any SQL exceptions
	 */
	protected void openDatabase(String[] propertyPrefixes) throws ClassNotFoundException, SQLException{
		db = getConnection(config, propertyPrefixes);
		prepareStatements();
	}
	
	/**
	 * Open a database connection using a data source
	 * @param ds data source
	 * @throws SQLException SQL exceptions
	 */
	protected void openDatabase(DataSource ds) throws SQLException{
		db = ds.getConnection();
		prepareStatements();
	}

	@Override
	public void close()throws IOException{

+23 −10
Original line number Diff line number Diff line
@@ -36,11 +36,7 @@ import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;






import javax.sql.DataSource;

import de.sekmi.histream.DateTimeAccuracy;
import de.sekmi.histream.Observation;
@@ -125,19 +121,33 @@ public class PostgresPatientStore extends PostgresExtension<I2b2Patient> impleme
	public PostgresPatientStore(Map<String,String> configuration) throws ClassNotFoundException, SQLException {
		super(configuration);
		this.projectId = config.get("project");
		if( projectId == null ){
			log.warning("property project is null, some things might fail");
		openDatabase(new String[]{"jdbc.","data.jdbc."});
		initialize();
	}
	

	/**
	 * Create a patient store using a {@link DataSource}.
	 * The project id must be specified with the key {@code project}. 
	 * @param ds data source for the connection
	 * @param configuration configuration settings
	 * @throws SQLException SQL error
	 */
	public PostgresPatientStore(DataSource ds, Map<String,String> configuration) throws SQLException{
		super(configuration);
		this.projectId = config.get("project");
		openDatabase(ds);
		initialize();
	}
	private void initialize() throws SQLException{
		this.idSourceDefault = "HIVE";
		this.idSourceSeparator = ':';
//		this.autoInsertSourceId = "HS.auto";
		patientCache = new Hashtable<>(1000);
		idCache = new Hashtable<>(1000);
		openDatabase(new String[]{"jdbc.","data.jdbc."});
		loadMaxPatientNum();
		batchLoad();
	}
	
	private I2b2Patient getCached(int patient_num){
		return patientCache.get(patient_num);
	}
@@ -167,6 +177,9 @@ public class PostgresPatientStore extends PostgresExtension<I2b2Patient> impleme
	protected void prepareStatements()throws SQLException{

		db.setAutoCommit(true);
		if( projectId == null ){
			log.warning("property project is null, some things might fail");
		}
		// TODO: use prefix from configuration to specify tablespace
		insert = db.prepareStatement("INSERT INTO patient_dimension(patient_num, import_date, sourcesystem_cd) VALUES(?,current_timestamp,?)");
		insertIde = db.prepareStatement("INSERT INTO patient_mapping(patient_ide, patient_ide_source, patient_num, patient_ide_status, project_id, import_date, download_date, sourcesystem_cd) values (?,?,?,?,'"+projectId+"',current_timestamp,?,?)");
+30 −3
Original line number Diff line number Diff line
@@ -36,6 +36,8 @@ import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.sql.DataSource;

import de.sekmi.histream.DateTimeAccuracy;
import de.sekmi.histream.Observation;
import de.sekmi.histream.ext.ExternalSourceType;
@@ -80,21 +82,46 @@ public class PostgresVisitStore extends PostgresExtension<I2b2Visit>{
	private PreparedStatement deleteSource;
	private PreparedStatement deleteMapSource;
	
	/**
	 * Create a visit store using configuration settings.
	 * The project id must be specified with the key {@code project}. 
	 * JDBC connection configuration is specified with the key 
	 * prefixes {@code jdbc.*} and {@code data.jdbc.*}
	 * @param configuration key value pairs
	 * @throws ClassNotFoundException database driver not found
	 * @throws SQLException SQL exceptions
	 */
	public PostgresVisitStore(Map<String,String> configuration) throws ClassNotFoundException, SQLException {
		super(configuration);
		this.projectId = config.get("project");
		openDatabase(new String[]{"jdbc.","data.jdbc."});
		initialize();
	}

	/**
	 * Create a visit store using a {@link DataSource}.
	 * The project id must be specified with the key {@code project}. 
	 * @param ds data source for the connection
	 * @param configuration configuration settings
	 * @throws SQLException SQL error
	 */
	public PostgresVisitStore(DataSource ds, Map<String,String> configuration) throws SQLException{
		super(configuration);
		this.projectId = config.get("project");
		openDatabase(ds);
		initialize();
	}
	private void initialize() throws SQLException{
		visitCache = new Hashtable<>();
		idCache = new Hashtable<>();
		projectId = config.get("project");
		idSourceDefault = "HIVE";
		idSourceSeparator = ':';
		
		openDatabase(new String[]{"jdbc.","data.jdbc."});
		db.setAutoCommit(true);

		loadMaxEncounterNum();
		batchLoad();
	}

	@Override
	protected void prepareStatements() throws SQLException {
		// TODO: use prefix from configuration to specify tablespace