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

added implementation of stream metadata

parent ab53c664
......@@ -29,6 +29,7 @@ import de.sekmi.histream.ext.ExternalSourceType;
* Observation of a single event or a single fact assigned to a single patient.
*
* TODO more details
* TODO remove ExternalSourceType from interfaces, move to property
*
* @author Raphael
*
......
package de.sekmi.histream.impl;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import de.sekmi.histream.ObservationHandler;
import de.sekmi.histream.ObservationSupplier;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Meta {
@XmlElement(name="etl-strategy")
public String etlStrategy;
public ExternalSourceImpl source;
public Order order;
public static class Order{
@XmlAttribute
Boolean grouped;
@XmlAttribute
Boolean sorted;
private Order(){}
public Order(Boolean grouped, Boolean sorted){
this();
this.grouped = grouped;
this.sorted = sorted;
}
}
/**
* Set meta information via keys from {@link ObservationSupplier}
* @param key key
* @param value value
*/
public void set(String key, String value){
switch( key ){
case ObservationSupplier.META_ETL_STRATEGY:
this.etlStrategy = value;
break;
case ObservationSupplier.META_SOURCE_ID:
if( source == null )source = new ExternalSourceImpl();
source.sourceId = value;
break;
case ObservationSupplier.META_SOURCE_TIMESTAMP:
if( source == null )source = new ExternalSourceImpl();
source.sourceTimestamp = javax.xml.bind.DatatypeConverter.parseDateTime(value).toInstant();
break;
}
}
public static final void transfer(ObservationSupplier source, ObservationHandler target){
String keys[] = new String[]{
ObservationSupplier.META_ETL_STRATEGY,
ObservationSupplier.META_SOURCE_ID,
ObservationSupplier.META_SOURCE_TIMESTAMP
};
for( String key : keys ){
target.setMeta(key, source.getMeta(key));
}
}
}
......@@ -20,6 +20,7 @@ import de.sekmi.histream.ext.ExternalSourceType;
import de.sekmi.histream.ext.Patient;
import de.sekmi.histream.ext.Visit;
import de.sekmi.histream.impl.AbstractObservationHandler;
import de.sekmi.histream.impl.Meta;
import de.sekmi.histream.impl.ObservationImpl;
/**
......@@ -38,11 +39,13 @@ public class XMLWriter extends AbstractObservationHandler implements Closeable {
private XMLStreamWriter writer;
private boolean writeFormatted;
private int formattingDepth;
private Meta meta;
private XMLWriter() throws JAXBException{
this.marshaller = JAXBContext.newInstance(ObservationImpl.class).createMarshaller();
this.marshaller = JAXBContext.newInstance(ObservationImpl.class, Meta.class).createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
this.writeFormatted = true;
this.meta = new Meta();
}
public XMLWriter(OutputStream output) throws JAXBException, XMLStreamException, FactoryConfigurationError{
this();
......@@ -50,10 +53,9 @@ public class XMLWriter extends AbstractObservationHandler implements Closeable {
// enable repairing namespaces to remove duplicate namespace declarations by JAXB marshal
factory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.TRUE);
this.writer = factory.createXMLStreamWriter(output);
writeStartDocument();
}
private void writeStartDocument() throws XMLStreamException{
private void writeStartDocument() throws XMLStreamException, JAXBException{
writer.setDefaultNamespace(NAMESPACE);
writer.setPrefix("xsi", XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI);
writer.writeStartDocument();
......@@ -64,11 +66,14 @@ public class XMLWriter extends AbstractObservationHandler implements Closeable {
writer.writeNamespace("xsi", XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI);
formatNewline();
formatPush();
formatIndent();
marshaller.marshal(meta, writer);
formatNewline();
}
@Override
public void setMeta(String key, String value) {
// TODO Auto-generated method stub
meta.set(key, value);
}
private void formatNewline() throws XMLStreamException{
if( writeFormatted )writer.writeCharacters("\n");
......@@ -185,7 +190,8 @@ public class XMLWriter extends AbstractObservationHandler implements Closeable {
Visit thisVisit = observation.getExtension(Visit.class);
try {
if( prevPatient == null ){
// TODO write start document, meta, patient
// write start document, meta, patient
writeStartDocument();
startPatient(thisPatient);
}else if( !prevPatient.getId().equals(thisPatient.getId()) ){
// close patient, write new patient
......
......@@ -2,8 +2,10 @@ package de.sekmi.histream.io;
import java.io.File;
import java.io.IOException;
import java.time.Instant;
import java.util.stream.StreamSupport;
import javax.xml.bind.JAXB;
import javax.xml.bind.JAXBException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
......@@ -15,6 +17,8 @@ import org.junit.Test;
import org.w3c.dom.Document;
import de.sekmi.histream.ObservationSupplier;
import de.sekmi.histream.impl.ExternalSourceImpl;
import de.sekmi.histream.impl.Meta;
public class TestXMLWriter {
......@@ -24,9 +28,22 @@ public class TestXMLWriter {
t.initializeObservationFactory();
ObservationSupplier s = t.getExampleSupplier();
XMLWriter w = new XMLWriter(System.out);
Meta.transfer(s, w);
StreamSupport.stream(AbstractObservationParser.nonNullSpliterator(s), false).forEach(w);
w.close();
}
@Test
public void testWriteMeta(){
Meta meta = new Meta();
meta.etlStrategy = "lala";
meta.source = new ExternalSourceImpl();
meta.source.setSourceId("sid");
meta.source.setSourceTimestamp(Instant.now());
meta.order = new Meta.Order(true,false);
JAXB.marshal(meta, System.out);
}
@Test
public void testReadWriteIdenticalXML() throws Exception{
......
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