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

abstract table with generics

parent df24a214
...@@ -3,14 +3,14 @@ package de.sekmi.histream.etl; ...@@ -3,14 +3,14 @@ package de.sekmi.histream.etl;
import java.io.IOException; import java.io.IOException;
import java.util.function.Supplier; import java.util.function.Supplier;
import de.sekmi.histream.etl.config.PatientTable; import de.sekmi.histream.etl.config.Table;
public class PatientStream implements Supplier<PatientRow>, AutoCloseable{ public class RecordSupplier<R> implements Supplier<R>, AutoCloseable{
RowSupplier rows; RowSupplier rows;
PatientTable table; Table<R> table;
ColumnMap map; ColumnMap map;
public PatientStream(RowSupplier rows, PatientTable table) throws IOException{ public RecordSupplier(RowSupplier rows, Table<R> table) throws IOException{
this.rows = rows; this.rows = rows;
this.table = table; this.table = table;
this.map = table.getColumnMap(rows.getHeaders()); this.map = table.getColumnMap(rows.getHeaders());
...@@ -22,24 +22,20 @@ public class PatientStream implements Supplier<PatientRow>, AutoCloseable{ ...@@ -22,24 +22,20 @@ public class PatientStream implements Supplier<PatientRow>, AutoCloseable{
} }
@Override @Override
public PatientRow get() { public R get() {
Object[] row = rows.get(); Object[] row = rows.get();
if( row == null ){ if( row == null ){
// no more rows // no more rows
return null; return null;
} }
PatientRow p; R p;
try { try {
p = table.fillPatient(map, row); p = table.fillRecord(map, row);
} catch (ParseException e) { } catch (ParseException e) {
throw new UncheckedParseException(e); throw new UncheckedParseException(e);
} }
return p; return p;
} }
} }
package de.sekmi.histream.etl;
public class WideRow {
}
package de.sekmi.histream.etl.config; package de.sekmi.histream.etl.config;
import java.io.IOException;
import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAccessorType;
...@@ -11,7 +10,6 @@ import de.sekmi.histream.DateTimeAccuracy; ...@@ -11,7 +10,6 @@ import de.sekmi.histream.DateTimeAccuracy;
import de.sekmi.histream.etl.ColumnMap; import de.sekmi.histream.etl.ColumnMap;
import de.sekmi.histream.etl.ParseException; import de.sekmi.histream.etl.ParseException;
import de.sekmi.histream.etl.PatientRow; import de.sekmi.histream.etl.PatientRow;
import de.sekmi.histream.etl.PatientStream;
/** /**
* Patient table. Contains patient id and other identifying information. * Patient table. Contains patient id and other identifying information.
...@@ -19,7 +17,7 @@ import de.sekmi.histream.etl.PatientStream; ...@@ -19,7 +17,7 @@ import de.sekmi.histream.etl.PatientStream;
* @author marap1 * @author marap1
* *
*/ */
public class PatientTable extends Table implements WideInterface{ public class PatientTable extends Table<PatientRow> implements WideInterface{
@XmlElement @XmlElement
IDAT idat; IDAT idat;
...@@ -61,7 +59,9 @@ public class PatientTable extends Table implements WideInterface{ ...@@ -61,7 +59,9 @@ public class PatientTable extends Table implements WideInterface{
return map; return map;
} }
public PatientRow fillPatient(ColumnMap map, Object[] row) throws ParseException{
@Override
public PatientRow fillRecord(ColumnMap map, Object[] row) throws ParseException {
PatientRow patient = new PatientRow(); PatientRow patient = new PatientRow();
patient.setId(idat.patientId.valueOf(map, row).toString()); patient.setId(idat.patientId.valueOf(map, row).toString());
patient.setNames((String)idat.firstname.valueOf(map, row), (String)idat.surname.valueOf(map, row)); patient.setNames((String)idat.firstname.valueOf(map, row), (String)idat.surname.valueOf(map, row));
...@@ -71,8 +71,4 @@ public class PatientTable extends Table implements WideInterface{ ...@@ -71,8 +71,4 @@ public class PatientTable extends Table implements WideInterface{
return patient; return patient;
} }
public PatientStream open() throws IOException{
return new PatientStream(source.rows(), this);
}
} }
package de.sekmi.histream.etl.config; package de.sekmi.histream.etl.config;
import java.io.IOException;
import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlTransient; import javax.xml.bind.annotation.XmlTransient;
import de.sekmi.histream.etl.ColumnMap; import de.sekmi.histream.etl.ColumnMap;
import de.sekmi.histream.etl.ParseException;
import de.sekmi.histream.etl.RecordSupplier;
@XmlTransient @XmlTransient
public abstract class Table { public abstract class Table<T> {
@XmlElement(required=true) @XmlElement(required=true)
TableSource source; TableSource source;
...@@ -33,4 +37,11 @@ public abstract class Table { ...@@ -33,4 +37,11 @@ public abstract class Table {
map.registerColumn(m.unit); map.registerColumn(m.unit);
} }
} }
public abstract T fillRecord(ColumnMap map, Object[] row) throws ParseException;
public RecordSupplier<T> open() throws IOException{
return new RecordSupplier<>(source.rows(), this);
}
} }
...@@ -7,8 +7,10 @@ import javax.xml.bind.annotation.XmlElementWrapper; ...@@ -7,8 +7,10 @@ import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlType; import javax.xml.bind.annotation.XmlType;
import de.sekmi.histream.etl.ColumnMap; import de.sekmi.histream.etl.ColumnMap;
import de.sekmi.histream.etl.ParseException;
import de.sekmi.histream.etl.VisitRow;
public class VisitTable extends Table implements WideInterface{ public class VisitTable extends Table<VisitRow> implements WideInterface{
@XmlElement @XmlElement
IDAT idat; IDAT idat;
...@@ -38,4 +40,9 @@ public class VisitTable extends Table implements WideInterface{ ...@@ -38,4 +40,9 @@ public class VisitTable extends Table implements WideInterface{
} }
return map; return map;
} }
@Override
public VisitRow fillRecord(ColumnMap map, Object[] row) throws ParseException {
// TODO Auto-generated method stub
return null;
}
} }
...@@ -4,8 +4,10 @@ import javax.xml.bind.annotation.XmlElement; ...@@ -4,8 +4,10 @@ import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper; import javax.xml.bind.annotation.XmlElementWrapper;
import de.sekmi.histream.etl.ColumnMap; import de.sekmi.histream.etl.ColumnMap;
import de.sekmi.histream.etl.ParseException;
import de.sekmi.histream.etl.WideRow;
public class WideTable extends Table { public class WideTable extends Table<WideRow> {
@XmlElement @XmlElement
DataTableIdat idat; DataTableIdat idat;
...@@ -24,4 +26,10 @@ public class WideTable extends Table { ...@@ -24,4 +26,10 @@ public class WideTable extends Table {
} }
return map; return map;
} }
@Override
public WideRow fillRecord(ColumnMap map, Object[] row) throws ParseException {
// TODO Auto-generated method stub
return null;
}
} }
...@@ -8,7 +8,7 @@ import javax.xml.bind.JAXB; ...@@ -8,7 +8,7 @@ import javax.xml.bind.JAXB;
import org.junit.Test; import org.junit.Test;
import de.sekmi.histream.etl.PatientRow; import de.sekmi.histream.etl.PatientRow;
import de.sekmi.histream.etl.PatientStream; import de.sekmi.histream.etl.RecordSupplier;
import org.junit.Assert; import org.junit.Assert;
public class TestReadTables { public class TestReadTables {
...@@ -19,7 +19,7 @@ public class TestReadTables { ...@@ -19,7 +19,7 @@ public class TestReadTables {
try( InputStream in = getClass().getResourceAsStream("/test-1-datasource.xml") ){ try( InputStream in = getClass().getResourceAsStream("/test-1-datasource.xml") ){
ds = JAXB.unmarshal(in, DataSource.class); ds = JAXB.unmarshal(in, DataSource.class);
} }
try( PatientStream s = ds.patientTable.open() ){ try( RecordSupplier<PatientRow> s = ds.patientTable.open() ){
PatientRow r = s.get(); PatientRow r = s.get();
Assert.assertEquals("1", r.getId()); Assert.assertEquals("1", r.getId());
System.out.println(r.getBirthDate()); System.out.println(r.getBirthDate());
......
Supports Markdown
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