Skip to content

Commit

Permalink
feat: first draft version of ObdsToFhirBundleMapper
Browse files Browse the repository at this point in the history
  • Loading branch information
chgl committed Nov 25, 2024
1 parent 15525dc commit 8cbb36d
Show file tree
Hide file tree
Showing 4 changed files with 485 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package org.miracum.streams.ume.obdstofhir.mapper.mii;

import de.basisdatensatz.obds.v3.OBDS;
import org.hl7.fhir.r4.model.Bundle;
import org.hl7.fhir.r4.model.Bundle.BundleType;
import org.hl7.fhir.r4.model.Bundle.HTTPVerb;
import org.hl7.fhir.r4.model.Reference;
import org.hl7.fhir.r4.model.Resource;
import org.springframework.stereotype.Service;

@Service
public class ObdsToFhirBundleMapper {

private final PatientMapper patientMapper;
private final ConditionMapper conditionMapper;
private final StrahlentherapieMapper strahlentherapieMapper;
private final SystemischeTherapieProcedureMapper systemischeTherapieProcedureMapper;

public ObdsToFhirBundleMapper(
PatientMapper patientMapper,
ConditionMapper conditionMapper,
SystemischeTherapieProcedureMapper systemischeTherapieProcedureMapper,
StrahlentherapieMapper strahlentherapieMapper) {
this.patientMapper = patientMapper;
this.conditionMapper = conditionMapper;
this.systemischeTherapieProcedureMapper = systemischeTherapieProcedureMapper;
this.strahlentherapieMapper = strahlentherapieMapper;
}

public Bundle map(OBDS obds) {
var bundle = new Bundle();
bundle.setType(BundleType.TRANSACTION);

// TODO: set bundle id... to the patient id? sum of all ids?
// TODO: or one bundle per Patient instead?
for (var obdsPatient : obds.getMengePatient().getPatient()) {
var meldungen = obdsPatient.getMengeMeldung().getMeldung();

// Patient
var patient = patientMapper.map(obdsPatient, meldungen);
var patientReference = new Reference("Patient/" + patient.getId());
addEntryToBundle(bundle, patient);

for (var meldung : meldungen) {
// Diagnose
if (meldung.getDiagnose() != null) {
var condition = conditionMapper.map(meldung, patientReference);
addEntryToBundle(bundle, condition);
}

if (meldung.getSYST() != null) {
var systProcedure =
systemischeTherapieProcedureMapper.map(meldung.getSYST(), patientReference);
addEntryToBundle(bundle, systProcedure);
}

if (meldung.getST() != null) {
var stProcedure = strahlentherapieMapper.map(meldung.getST(), patientReference);
addEntryToBundle(bundle, stProcedure);
}
}
}

return bundle;
}

private static Bundle addEntryToBundle(Bundle bundle, Resource resource) {
var url = String.format("%s/%s", resource.getResourceType(), resource.getIdBase());
bundle.addEntry().setResource(resource).getRequest().setMethod(HTTPVerb.PUT).setUrl(url);
return bundle;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
import org.miracum.streams.ume.obdstofhir.mapper.ObdsToFhirMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service
public class StrahlentherapieMapper extends ObdsToFhirMapper {
private static final Logger LOG = LoggerFactory.getLogger(StrahlentherapieMapper.class);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package org.miracum.streams.ume.obdstofhir.mapper.mii;

import static org.assertj.core.api.Assertions.assertThat;

import ca.uhn.fhir.context.FhirContext;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.fasterxml.jackson.module.jakarta.xmlbind.JakartaXmlBindAnnotationModule;
import de.basisdatensatz.obds.v3.OBDS;
import java.io.IOException;
import org.approvaltests.Approvals;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.miracum.streams.ume.obdstofhir.FhirProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest(
classes = {
FhirProperties.class,
ObdsToFhirBundleMapper.class,
PatientMapper.class,
ConditionMapper.class,
SystemischeTherapieProcedureMapper.class,
StrahlentherapieMapper.class,
})
@EnableConfigurationProperties
class ObdsToFhirBundleMapperTest {
private static ObdsToFhirBundleMapper sut;

@BeforeAll
static void beforeAll(@Autowired ObdsToFhirBundleMapper bundleMapper) {
sut = bundleMapper;
}

@ParameterizedTest
@CsvSource({
"Testpatient_1.xml",
})
void map_withGivenObds_shouldCreateBundleMatchingSnapshot(String sourceFile) throws IOException {
final var resource = this.getClass().getClassLoader().getResource("obds3/" + sourceFile);
assertThat(resource).isNotNull();

final var xmlMapper =
XmlMapper.builder()
.defaultUseWrapper(false)
.addModule(new JakartaXmlBindAnnotationModule())
.addModule(new Jdk8Module())
// added because the Testpatient_*.xml contain the `xsi:schemaLocation` attribute which
// isn't code-generated
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.build();

final var obds = xmlMapper.readValue(resource.openStream(), OBDS.class);

final var bundle = sut.map(obds);

var fhirParser = FhirContext.forR4().newJsonParser().setPrettyPrint(true);
var fhirJson = fhirParser.encodeResourceToString(bundle);
Approvals.verify(
fhirJson, Approvals.NAMES.withParameters(sourceFile).forFile().withExtension(".fhir.json"));
}
}
Loading

0 comments on commit 8cbb36d

Please sign in to comment.