Commit 170e35c1 authored by rwm's avatar rwm
Browse files

removeContext can also remove startTime from observation

parent 3f6f05f2
...@@ -332,13 +332,16 @@ public class ObservationImpl implements Observation, Cloneable{ ...@@ -332,13 +332,16 @@ public class ObservationImpl implements Observation, Cloneable{
* @param encounterId encounter context * @param encounterId encounter context
* @param source source context * @param source source context
*/ */
public void removeContext(String patientId, String encounterId, ExternalSourceType source){ public void removeContext(String patientId, String encounterId, DateTimeAccuracy startTime, ExternalSourceType source){
if( this.patientId != null && patientId != null && this.patientId.equals(patientId) ){ if( this.patientId != null && patientId != null && this.patientId.equals(patientId) ){
this.patientId = null; this.patientId = null;
} }
if( this.encounterId != null && encounterId != null && this.encounterId.equals(encounterId) ){ if( this.encounterId != null && encounterId != null && this.encounterId.equals(encounterId) ){
this.encounterId = null; this.encounterId = null;
} }
if( this.startTime != null && startTime != null && this.startTime.equals(startTime) ){
this.startTime = null;
}
if( this.source != null && source != null ){ if( this.source != null && source != null ){
ExternalSourceImpl s = new ExternalSourceImpl(this.source.getSourceId(), this.source.getSourceTimestamp()); ExternalSourceImpl s = new ExternalSourceImpl(this.source.getSourceId(), this.source.getSourceTimestamp());
if( s.getSourceId() != null && source.getSourceId() != null && s.getSourceId().equals(source.getSourceId()) ){ if( s.getSourceId() != null && source.getSourceId() != null && s.getSourceId().equals(source.getSourceId()) ){
......
...@@ -238,7 +238,7 @@ public class XMLWriter extends AbstractObservationHandler implements Closeable { ...@@ -238,7 +238,7 @@ public class XMLWriter extends AbstractObservationHandler implements Closeable {
// clone observation, remove patient/encounter/source information as it is contained in wrappers // clone observation, remove patient/encounter/source information as it is contained in wrappers
ObservationImpl o = (ObservationImpl)fact; ObservationImpl o = (ObservationImpl)fact;
o = o.clone(); o = o.clone();
o.removeContext(patient.getId(), visit.getId(), source); o.removeContext(patient.getId(), visit.getId(), visit.getStartTime(), source);
marshaller.marshal(o, writer); marshaller.marshal(o, writer);
} }
......
...@@ -181,14 +181,15 @@ public class ObservationImplJAXBTest { ...@@ -181,14 +181,15 @@ public class ObservationImplJAXBTest {
@Test @Test
public void testRemoveContext(){ public void testRemoveContext(){
ObservationImpl o = createObservation(0); ObservationImpl o = createObservation(0);
o.removeContext("P0", "E0", new ExternalSourceImpl("source1", Instant.now())); o.removeContext("P0", "E0", null, new ExternalSourceImpl("source1", Instant.now()));
Assert.assertNull(o.patientId); Assert.assertNull(o.patientId);
Assert.assertNull(o.encounterId); Assert.assertNull(o.encounterId);
Assert.assertNull(o.getSource().getSourceId()); Assert.assertNull(o.getSource().getSourceId());
o = createObservation(0); o = createObservation(0);
o.removeContext(null, null, new ExternalSourceImpl("source1", Instant.parse("2000-01-01T00:00:00Z"))); o.removeContext(null, null, o.getStartTime(), new ExternalSourceImpl("source1", Instant.parse("2000-01-01T00:00:00Z")));
Assert.assertNull(o.getSource()); Assert.assertNull(o.getSource());
Assert.assertNull(o.getStartTime());
} }
@Test @Test
......
...@@ -3,38 +3,59 @@ package de.sekmi.histream.io; ...@@ -3,38 +3,59 @@ package de.sekmi.histream.io;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream;
import java.time.Instant; import java.time.Instant;
import java.util.stream.StreamSupport; import java.util.stream.StreamSupport;
import javax.xml.bind.JAXB; import javax.xml.bind.JAXB;
import javax.xml.bind.JAXBException;
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.stream.FactoryConfigurationError; import javax.xml.xpath.XPathConstants;
import javax.xml.stream.XMLStreamException; import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.junit.After;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.w3c.dom.DOMConfiguration;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import de.sekmi.histream.ObservationSupplier; import de.sekmi.histream.ObservationSupplier;
import de.sekmi.histream.impl.ExternalSourceImpl; import de.sekmi.histream.impl.ExternalSourceImpl;
import de.sekmi.histream.impl.Meta; import de.sekmi.histream.impl.Meta;
public class TestXMLWriter { public class TestXMLWriter {
private File debugFile;
private OutputStream debugLog;
@Before
public void setupLog() throws IOException{
// for debugging, set debugLog to System.out
debugFile = File.createTempFile("xmlwriterlog", ".xml");
// debugLog = System.out
debugLog = new FileOutputStream(debugFile);
}
@After
public void cleanLog() throws IOException{
debugLog.close();
if( debugFile != null )
debugFile.delete();
}
@Test @Test
public void testWrite() throws IOException, JAXBException, XMLStreamException, FactoryConfigurationError{ public void testWrite() throws Exception{
FileObservationProviderTest t = new FileObservationProviderTest(); FileObservationProviderTest t = new FileObservationProviderTest();
t.initializeObservationFactory(); t.initializeObservationFactory();
ObservationSupplier s = t.getExampleSupplier(); ObservationSupplier s = t.getExampleSupplier();
XMLWriter w = new XMLWriter(System.out); XMLWriter w = new XMLWriter(debugLog);
Meta.transfer(s, w); Meta.transfer(s, w);
StreamSupport.stream(AbstractObservationParser.nonNullSpliterator(s), false).forEach(w); StreamSupport.stream(AbstractObservationParser.nonNullSpliterator(s), false).forEach(w);
w.close(); w.close();
s.close();
} }
@Test @Test
public void testWriteMeta(){ public void testWriteMeta(){
Meta meta = new Meta(); Meta meta = new Meta();
...@@ -42,30 +63,33 @@ public class TestXMLWriter { ...@@ -42,30 +63,33 @@ public class TestXMLWriter {
meta.source = new ExternalSourceImpl("sid", Instant.now()); meta.source = new ExternalSourceImpl("sid", Instant.now());
meta.order = new Meta.Order(true,false); meta.order = new Meta.Order(true,false);
JAXB.marshal(meta, System.out); JAXB.marshal(meta, debugLog);
meta = new Meta(); meta = new Meta();
meta.etlStrategy = "lala"; meta.etlStrategy = "lala";
meta.source = new ExternalSourceImpl("sid", null); meta.source = new ExternalSourceImpl("sid", null);
meta.order = null; meta.order = null;
JAXB.marshal(meta, System.out); JAXB.marshal(meta, debugLog);
} }
private void normalizeDocument(Document dom){ private static void removeEmptyText(Document doc) throws XPathExpressionException{
DOMConfiguration c = dom.getDomConfig(); XPathFactory xpathFactory = XPathFactory.newInstance();
// XXX doesn't work // XPath to find empty text nodes.
c.setParameter("cdata-sections", "false"); XPathExpression xpathExp = xpathFactory.newXPath().compile(
c.setParameter("element-content-whitespace", "false"); "//text()[normalize-space(.) = '']");
c.setParameter("comments", "false"); NodeList emptyTextNodes = (NodeList)
c.setParameter("normalize-characters", "true"); xpathExp.evaluate(doc, XPathConstants.NODESET);
dom.normalizeDocument();
// Remove each empty text node from document.
for (int i = 0; i < emptyTextNodes.getLength(); i++) {
Node emptyTextNode = emptyTextNodes.item(i);
emptyTextNode.getParentNode().removeChild(emptyTextNode);
}
} }
@Test @Test
public void testReadWriteIdenticalXML() throws Exception{ public void testReadWriteIdenticalXML() throws Exception{
if( true )return;
// TODO include XSD for validating parser
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(true); //dbf.setValidating(true);
dbf.setNamespaceAware(true); dbf.setNamespaceAware(true);
dbf.setCoalescing(true); dbf.setCoalescing(true);
dbf.setIgnoringElementContentWhitespace(true); dbf.setIgnoringElementContentWhitespace(true);
...@@ -74,7 +98,7 @@ public class TestXMLWriter { ...@@ -74,7 +98,7 @@ public class TestXMLWriter {
Document doc1 = db.parse(new File("examples/dwh-jaxb.xml")); Document doc1 = db.parse(new File("examples/dwh-jaxb.xml"));
doc1.normalizeDocument(); doc1.normalizeDocument();
//normalizeDocument(doc1); removeEmptyText(doc1);
File dest = File.createTempFile("xmlwriter", ".xml"); File dest = File.createTempFile("xmlwriter", ".xml");
FileOutputStream out = new FileOutputStream(dest); FileOutputStream out = new FileOutputStream(dest);
...@@ -84,14 +108,15 @@ public class TestXMLWriter { ...@@ -84,14 +108,15 @@ public class TestXMLWriter {
XMLWriter w = new XMLWriter(out); XMLWriter w = new XMLWriter(out);
Meta.transfer(s, w); Meta.transfer(s, w);
StreamSupport.stream(AbstractObservationParser.nonNullSpliterator(s), false).forEach(w); StreamSupport.stream(AbstractObservationParser.nonNullSpliterator(s), false).forEach(w);
s.close();
w.close(); w.close();
out.close(); out.close();
// compare with generated DOM // compare with generated DOM
Document doc2 = db.parse(dest); Document doc2 = db.parse(dest);
dest.delete(); dest.delete();
doc2.normalizeDocument();//normalizeDocument(doc2); doc2.normalizeDocument();
removeEmptyText(doc2);
Assert.assertTrue(doc1.isEqualNode(doc2)); Assert.assertTrue(doc1.isEqualNode(doc2));
} }
......
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