Commit 67e399fd authored by R.W.Majeed's avatar R.W.Majeed

allow time zone specification for XML output of timestamps

parent b8a123f9
package de.sekmi.histream.io; package de.sekmi.histream.io;
import java.io.OutputStream; import java.io.OutputStream;
import java.time.ZoneId;
import javax.xml.XMLConstants; import javax.xml.XMLConstants;
import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBContext;
...@@ -21,6 +22,7 @@ import de.sekmi.histream.impl.ExternalSourceImpl; ...@@ -21,6 +22,7 @@ import de.sekmi.histream.impl.ExternalSourceImpl;
import de.sekmi.histream.impl.GroupedObservationHandler; import de.sekmi.histream.impl.GroupedObservationHandler;
import de.sekmi.histream.impl.Meta; import de.sekmi.histream.impl.Meta;
import de.sekmi.histream.impl.ObservationImpl; import de.sekmi.histream.impl.ObservationImpl;
import de.sekmi.histream.xml.DateTimeAccuracyAdapter;
/** /**
* Writes observations to a single XML file. Observations must be grouped by patient and encounter. * Writes observations to a single XML file. Observations must be grouped by patient and encounter.
...@@ -36,6 +38,7 @@ public class GroupedXMLWriter extends GroupedObservationHandler{ ...@@ -36,6 +38,7 @@ public class GroupedXMLWriter extends GroupedObservationHandler{
private int formattingDepth; private int formattingDepth;
private Meta meta; private Meta meta;
private int observationCount; private int observationCount;
private ZoneId zoneId;
/** /**
* Used to output XML * Used to output XML
...@@ -101,6 +104,18 @@ public class GroupedXMLWriter extends GroupedObservationHandler{ ...@@ -101,6 +104,18 @@ public class GroupedXMLWriter extends GroupedObservationHandler{
} catch (PropertyException e) { } catch (PropertyException e) {
} }
} }
/**
* Set the timezone id for output of timestamp values.
* @param timeZone zone id or {@code null} to omit zone offset information
*/
public void setZoneId(ZoneId timeZone){
this.zoneId = timeZone;
// modify marshaller to use the timezone for timestamps
DateTimeAccuracyAdapter a = new DateTimeAccuracyAdapter();
a.setZoneId(zoneId);
marshaller.setAdapter(DateTimeAccuracyAdapter.class, a);
}
@Override @Override
protected void beginStream() throws ObservationException{ protected void beginStream() throws ObservationException{
...@@ -162,14 +177,14 @@ public class GroupedXMLWriter extends GroupedObservationHandler{ ...@@ -162,14 +177,14 @@ public class GroupedXMLWriter extends GroupedObservationHandler{
if( visit.getStartTime() != null ){ if( visit.getStartTime() != null ){
formatIndent(); formatIndent();
writer.writeStartElement(XMLConstants.DEFAULT_NS_PREFIX,"start",NAMESPACE); writer.writeStartElement(XMLConstants.DEFAULT_NS_PREFIX,"start",NAMESPACE);
writer.writeCharacters(visit.getStartTime().toPartialIso8601()); writer.writeCharacters(visit.getStartTime().toPartialIso8601(zoneId));
writer.writeEndElement(); writer.writeEndElement();
formatNewline(); formatNewline();
} }
if( visit.getEndTime() != null ){ if( visit.getEndTime() != null ){
formatIndent(); formatIndent();
writer.writeStartElement(XMLConstants.DEFAULT_NS_PREFIX,"end",NAMESPACE); writer.writeStartElement(XMLConstants.DEFAULT_NS_PREFIX,"end",NAMESPACE);
writer.writeCharacters(visit.getEndTime().toPartialIso8601()); writer.writeCharacters(visit.getEndTime().toPartialIso8601(zoneId));
writer.writeEndElement(); writer.writeEndElement();
formatNewline(); formatNewline();
} }
...@@ -270,7 +285,7 @@ public class GroupedXMLWriter extends GroupedObservationHandler{ ...@@ -270,7 +285,7 @@ public class GroupedXMLWriter extends GroupedObservationHandler{
if( patient.getBirthDate() != null ){ if( patient.getBirthDate() != null ){
formatIndent(); formatIndent();
writer.writeStartElement(XMLConstants.DEFAULT_NS_PREFIX,"birthdate",NAMESPACE); writer.writeStartElement(XMLConstants.DEFAULT_NS_PREFIX,"birthdate",NAMESPACE);
writer.writeCharacters(patient.getBirthDate().toPartialIso8601()); writer.writeCharacters(patient.getBirthDate().toPartialIso8601(zoneId));
writer.writeEndElement(); writer.writeEndElement();
formatNewline(); formatNewline();
} }
...@@ -280,7 +295,7 @@ public class GroupedXMLWriter extends GroupedObservationHandler{ ...@@ -280,7 +295,7 @@ public class GroupedXMLWriter extends GroupedObservationHandler{
formatIndent(); formatIndent();
writer.writeStartElement(XMLConstants.DEFAULT_NS_PREFIX,"deceased",NAMESPACE); writer.writeStartElement(XMLConstants.DEFAULT_NS_PREFIX,"deceased",NAMESPACE);
if( patient.getDeathDate() != null ){ if( patient.getDeathDate() != null ){
writer.writeCharacters(patient.getDeathDate().toPartialIso8601()); writer.writeCharacters(patient.getDeathDate().toPartialIso8601(zoneId));
} }
writer.writeEndElement(); writer.writeEndElement();
formatNewline(); formatNewline();
......
...@@ -8,6 +8,7 @@ import java.io.OutputStream; ...@@ -8,6 +8,7 @@ import java.io.OutputStream;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.time.Instant; import java.time.Instant;
import java.time.ZoneId;
import javax.xml.XMLConstants; import javax.xml.XMLConstants;
import javax.xml.bind.JAXB; import javax.xml.bind.JAXB;
...@@ -189,6 +190,18 @@ public class TestXMLWriter { ...@@ -189,6 +190,18 @@ public class TestXMLWriter {
w.close(); w.close();
s.close(); s.close();
} }
@Test
public void testTimestampsWithZoneOffset() throws Exception{
FileObservationProviderTest t = new FileObservationProviderTest();
t.initializeObservationFactory();
ObservationSupplier s = t.getExampleSupplier();
GroupedXMLWriter w = new GroupedXMLWriter(debugLog);
w.setZoneId(ZoneId.of("Asia/Shanghai"));
Meta.transfer(s, w);
Streams.transfer(s, w);
w.close();
s.close();
}
@Test @Test
public void testWriteMeta(){ public void testWriteMeta(){
......
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