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

ObservationHandler can report errors to error handler

parent 1b505140
package de.sekmi.histream;
/**
* Exception which occurred in connection with an observation
*
* @author Raphael
*
*/
public class ObservationException extends Exception{
private static final long serialVersionUID = 1L;
private Observation fact;
public ObservationException(Observation fact, Throwable cause){
super(cause);
this.fact = fact;
}
public ObservationException(String message, Observation fact, Throwable cause){
super(message, cause);
this.fact = fact;
}
public Observation getObservation(){return fact;}
}
\ No newline at end of file
......@@ -21,4 +21,6 @@ public interface ObservationHandler extends Consumer<Observation>{
*/
@Override
void accept(Observation observation);
void setErrorHandler(Consumer<ObservationException> handler);
}
......@@ -9,13 +9,14 @@ import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Enumeration;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import de.sekmi.histream.Modifier;
import de.sekmi.histream.Observation;
import de.sekmi.histream.ObservationException;
import de.sekmi.histream.ObservationHandler;
import de.sekmi.histream.Value;
import de.sekmi.histream.impl.AbstractObservationHandler;
/**
* Inserts observtions in to the i2b2 observation_fact table.
......@@ -43,7 +44,7 @@ import de.sekmi.histream.Value;
* @author marap1
*
*/
public class I2b2Inserter implements ObservationHandler, Closeable{
public class I2b2Inserter extends AbstractObservationHandler implements ObservationHandler, Closeable{
private static final Logger log = Logger.getLogger(I2b2Inserter.class.getName());
private Connection db;
private PreparedStatement insertFact;
......@@ -130,12 +131,11 @@ public class I2b2Inserter implements ObservationHandler, Closeable{
@Override
public void accept(Observation o) {
// TODO Auto-generated method stub
public void acceptOrException(Observation o) throws ObservationException{
try {
insertFact(o, null, 1);
} catch (SQLException e) {
log.log(Level.SEVERE, "Unable to insert fact", e);
throw new ObservationException(o, e);
}
}
......
package de.sekmi.histream.impl;
import java.util.function.Consumer;
import de.sekmi.histream.Observation;
import de.sekmi.histream.ObservationException;
import de.sekmi.histream.ObservationHandler;
public abstract class AbstractObservationHandler implements ObservationHandler{
Consumer<ObservationException> errorHandler;
@Override
public final void accept(Observation observation) {
try {
acceptOrException(observation);
} catch (ObservationException e) {
if( errorHandler != null )errorHandler.accept(e);
else throw new RuntimeException("Exception encountered, no error handler", e);
} // don't catch runtime exceptions
}
/**
* Accept method which allows exceptions. Exceptions are passed to the error handler
* specified via {@link #setErrorHandler(Consumer)}.
* @param observation
* @throws ObservationException
*/
protected abstract void acceptOrException(Observation observation)throws ObservationException;
@Override
public void setErrorHandler(Consumer<ObservationException> handler) {
this.errorHandler = handler;
}
}
package de.sekmi.histream.impl;
import java.util.function.Consumer;
import org.junit.Assert;
import de.sekmi.histream.Observation;
import de.sekmi.histream.ObservationHandler;
public class TestObservationHandler implements ObservationHandler{
public class TestObservationHandler implements Consumer<Observation>{
public static interface Tester{
void test(Observation observation);
......
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