Commit 170e35c1 authored by rwm's avatar rwm

removeContext can also remove startTime from observation

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