Commit 8634f3fa authored by R.W.Majeed's avatar R.W.Majeed

adding close() method to ExportWriter, documentation

parent de6b9391
package de.sekmi.histream.export;
import java.io.Closeable;
import java.io.IOException;
public interface ExportWriter {
/**
* Write table data. Each returned {@link TableWriter} must
* be closed individually.
* <p>
* The {@link #close()} should be called, after the export
* processing is complete.
* </p>
* @author R.W.Majeed
*
*/
public interface ExportWriter extends Closeable{
TableWriter openPatientTable() throws IOException;
TableWriter openVisitTable() throws IOException;
TableWriter openEAVTable(String id) throws IOException;
@Override
void close() throws IOException;
}
......@@ -61,8 +61,15 @@ public class ExportDescriptor {
public static ExportDescriptor parse(InputStream xml){
return JAXB.unmarshal(xml, ExportDescriptor.class);
}
/**
* Get all concepts used for the export. This
* may contain duplicate concepts.
*
* @return all concepts
*/
public Iterable<Concept> allConcepts(){
return concepts.allConcepts();
}
}
......@@ -7,12 +7,33 @@ import java.nio.file.Path;
import de.sekmi.histream.export.ExportWriter;
import de.sekmi.histream.export.TableWriter;
/**
* Write CSV files to a specified directory.
* <p>
* By default, the patients table is named {@code "patients" + getFileExtension()}
* and the visits table is named {@code "visits" + getFileExtension()}. The EAV
* tables are named by their specified ID (also with file extension). The patient and
* visit table names can be changed via {@link #setPatientTableName(String)} and
* {@link #setVisitTableName(String)}.
* </p>
* <p>
* The {@link #close()} method does nothing, since individual files are written
* for each table, which in turn must be closed individually.
* </p>
* <p>
* TODO maybe allow appending to files
* </p>
* @author R.W.Majeed
*
*/
public class CSVWriter implements ExportWriter{
private char fieldSeparator;
private Charset charset;
private Path directory;
private String filenameExtension;
private String patientTableName;
private String visitTableName;
/**
* Create a CSV writer which creates table files
......@@ -26,7 +47,8 @@ public class CSVWriter implements ExportWriter{
this.directory = directory;
this.fieldSeparator = fieldSeparator;
this.filenameExtension = fileSuffix;
this.patientTableName = "patients";
this.visitTableName = "visits";
}
public Charset getCharset(){
......@@ -40,7 +62,7 @@ public class CSVWriter implements ExportWriter{
}
private String fileWithExtension(String name){
return name+filenameExtension;
return name+getFileExtension();
}
/**
......@@ -51,6 +73,15 @@ public class CSVWriter implements ExportWriter{
public void setFileExtension(String suffix){
this.filenameExtension = suffix;
}
public String getFileExtension(){
return filenameExtension;
}
public void setVisitTableName(String tableName){
this.visitTableName = tableName;
}
public void setPatientTableName(String tableName){
this.patientTableName = tableName;
}
/**
* Escape data before it is written to the output file.
* <p>
......@@ -66,16 +97,21 @@ public class CSVWriter implements ExportWriter{
}
@Override
public TableWriter openPatientTable() throws IOException {
return new Table(this, fileWithExtension("patient"));
return new Table(this, fileWithExtension(patientTableName));
}
@Override
public TableWriter openVisitTable() throws IOException {
return new Table(this, fileWithExtension("encounter"));
return new Table(this, fileWithExtension(visitTableName));
}
@Override
public TableWriter openEAVTable(String id) throws IOException {
return new Table(this, fileWithExtension(id));
}
@Override
public void close() {
// no need to close, we are writing individual files
}
}
......@@ -16,10 +16,10 @@ public class ExceptionCausingWriter implements ExportWriter{
private WhereToThrow where;
protected enum WhenToThrow{
Open, WriteHeader, WriteRow, CloseTable
OpenTable, WriteHeader, WriteRow, CloseTable
}
protected enum WhereToThrow{
PatientTable, VisitTable, EAVTable
PatientTable, VisitTable, EAVTable, CloseWriter
}
public ExceptionCausingWriter(WhereToThrow where, WhenToThrow when) {
......@@ -55,20 +55,27 @@ public class ExceptionCausingWriter implements ExportWriter{
}
@Override
public TableWriter openPatientTable() throws IOException {
throwIf(WhereToThrow.PatientTable,WhenToThrow.Open);
throwIf(WhereToThrow.PatientTable,WhenToThrow.OpenTable);
return new ExceptionThrowingTable(WhereToThrow.PatientTable);
}
@Override
public TableWriter openVisitTable() throws IOException {
throwIf(WhereToThrow.VisitTable,WhenToThrow.Open);
throwIf(WhereToThrow.VisitTable,WhenToThrow.OpenTable);
return new ExceptionThrowingTable(WhereToThrow.VisitTable);
}
@Override
public TableWriter openEAVTable(String id) throws IOException {
throwIf(WhereToThrow.EAVTable,WhenToThrow.Open);
throwIf(WhereToThrow.EAVTable,WhenToThrow.OpenTable);
return new ExceptionThrowingTable(WhereToThrow.EAVTable);
}
@Override
public void close() throws IOException {
if( where == WhereToThrow.CloseWriter ){
throw exceptions.get();
}
}
}
......@@ -77,4 +77,8 @@ public class MemoryExportWriter implements ExportWriter{
System.out.println();
}
}
@Override
public void close(){
}
}
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