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

added method to remove information contained in context

parent 87efeab1
......@@ -54,7 +54,7 @@ import de.sekmi.histream.ext.ExternalSourceType;
@XmlAccessorType(XmlAccessType.NONE)
@XmlType(propOrder={"abstractValue","modifierList","source"})
@XmlSeeAlso({StringValue.class,NumericValue.class})
public class ObservationImpl implements Observation{
public class ObservationImpl implements Observation, Cloneable{
public static final String XML_NAMESPACE="http://sekmi.de/histream/ns/eav-data";
@XmlTransient
protected ObservationFactoryImpl factory;
......@@ -305,5 +305,51 @@ public class ObservationImpl implements Observation{
}
@Override
public ObservationImpl clone(){
ObservationImpl clone = new ObservationImpl(this.factory);
clone.conceptId = this.conceptId;
clone.encounterId = this.conceptId;
clone.endTime = this.endTime;
clone.locationId = this.locationId;
clone.patientId = this.patientId;
clone.providerId = this.providerId;
clone.startTime = this.startTime;
clone.value = this.value; // is immutable?
clone.extensions = this.extensions.clone();
if( this.modifiers != null ){
clone.modifiers = new ArrayList<>(this.modifiers);
}
if( this.source != null ){
clone.source = new ExternalSourceImpl(this.source.getSourceId(), this.source.getSourceTimestamp());
}
return clone;
}
/**
* Removes information from this observation which is already contained in the provided context
* @param patientId patient context
* @param encounterId encounter context
* @param source source context
*/
public void removeContext(String patientId, String encounterId, 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.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()) ){
s.setSourceId(null);
}
if( s.getSourceTimestamp() != null && source.getSourceTimestamp() != null && s.getSourceTimestamp().equals(source.getSourceTimestamp()) ){
s.setSourceTimestamp(null);
}
if( s.getSourceId() == null && s.getSourceTimestamp() == null )s = null;
this.source = s;
}
}
}
......@@ -40,7 +40,7 @@ public class XMLWriter extends AbstractObservationHandler implements Closeable {
private boolean writeFormatted;
private int formattingDepth;
private Meta meta;
private XMLWriter() throws JAXBException{
this.marshaller = JAXBContext.newInstance(ObservationImpl.class, Meta.class).createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
......@@ -213,9 +213,8 @@ public class XMLWriter extends AbstractObservationHandler implements Closeable {
// same encounter as previous fact
// nothing to do
}
// TODO clone observation, remove patient/encounter/source information as it is contained in wrappers
formatIndent();
marshalFactWithContext(observation, thisPatient, thisVisit, null);
marshalFactWithContext(observation, thisPatient, thisVisit, meta.source);
formatNewline();
} catch (JAXBException | XMLStreamException e ) {
......@@ -236,28 +235,11 @@ public class XMLWriter extends AbstractObservationHandler implements Closeable {
* @throws JAXBException errors during marshal operation
*/
public void marshalFactWithContext(Observation fact, Patient patient, Visit visit, ExternalSourceType source) throws ObservationException, JAXBException{
// clone observation, remove patient/encounter/source information as it is contained in wrappers
ObservationImpl o = (ObservationImpl)fact;
String e = o.getEncounterId();
String p = o.getPatientId();
if( patient.getId().equals(p) ){
o.setPatientId(null);
}else{
p = null;
}
if( visit.getId().equals(e) ){
o.setEncounterId(null);
}else{
e = null;
}
marshaller.marshal(fact, writer);
if( p != null ){
o.setPatientId(p);
}
if( e != null ){
o.setEncounterId(e);
}
o = o.clone();
o.removeContext(patient.getId(), visit.getId(), source);
marshaller.marshal(o, writer);
}
@Override
......
......@@ -58,12 +58,13 @@ public class ObservationImplJAXBTest {
ObservationImpl o = new ObservationImpl(of);
o.conceptId = "C"+index;
o.patientId = "P"+index;
o.encounterId = "E"+index;
o.startTime = new DateTimeAccuracy(2015,1,1,index);
switch( index ){
case 0:
// string value
o.setValue(new StringValue("strval"));
o.setSource(new ExternalSourceImpl("source1",Instant.now()));
o.setSource(new ExternalSourceImpl("source1",Instant.parse("2000-01-01T00:00:00Z")));
break;
case 1:
// numeric value without modifiers
......@@ -176,6 +177,19 @@ public class ObservationImplJAXBTest {
validator.validate(new StreamSource(EXAMPLE_FACT_FILES[i]));
}
}
@Test
public void testRemoveContext(){
ObservationImpl o = createObservation(0);
o.removeContext("P0", "E0", 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")));
Assert.assertNull(o.getSource());
}
/*
@Test
public void testModifierListBug() throws JAXBException{
......
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