Skip to content

Commit

Permalink
[2014] (#19) Handle FHIR MedicationRequest#entryType update
Browse files Browse the repository at this point in the history
  • Loading branch information
col-panic committed May 17, 2018
1 parent 848cee1 commit 653a3f5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.Optional;

import org.hl7.fhir.dstu3.model.Annotation;
import org.hl7.fhir.dstu3.model.CodeType;
import org.hl7.fhir.dstu3.model.CodeableConcept;
import org.hl7.fhir.dstu3.model.Coding;
import org.hl7.fhir.dstu3.model.Dosage;
Expand Down Expand Up @@ -41,6 +42,8 @@
@Component
public class MedicationRequestPrescriptionTransformer implements IFhirTransformer<MedicationRequest, Prescription> {

public static final String EXTENSION_PRESCRIPTION_ENTRYTYPE_URL = "www.elexis.info/extensions/prescription/entrytype";

private PrescriptionEntryTypeFactory entryTypeFactory = new PrescriptionEntryTypeFactory();

@Override
Expand Down Expand Up @@ -75,8 +78,6 @@ public Optional<MedicationRequest> getFhirObject(Prescription localObject) {
medication.setText(textBuilder.toString());
fhirObject.setMedication(medication);



MedicationRequestDispenseRequestComponent dispenseRequest = new MedicationRequestDispenseRequestComponent();
Period dispensePeriod = new Period();
LocalDateTime dateFrom = localObject.getDateFrom();
Expand All @@ -98,7 +99,6 @@ public Optional<MedicationRequest> getFhirObject(Prescription localObject) {
dispenseRequest.setValidityPeriod(dispensePeriod);
fhirObject.setDispenseRequest(dispenseRequest);


if (dateUntil != null) {
if (dateUntil.isBefore(LocalDateTime.now()) || dateUntil.isEqual(dateFrom)) {
statusEnum = MedicationRequestStatus.COMPLETED;
Expand Down Expand Up @@ -136,7 +136,7 @@ public Optional<MedicationRequest> getFhirObject(Prescription localObject) {
fhirObject.setText(narrative);

Extension elexisEntryType = new Extension();
elexisEntryType.setUrl("www.elexis.info/extensions/prescription/entrytype");
elexisEntryType.setUrl(EXTENSION_PRESCRIPTION_ENTRYTYPE_URL);
elexisEntryType
.setValue(new Enumeration<>(entryTypeFactory, EntryType.byNumeric(getNumericEntryType(localObject))));
fhirObject.addExtension(elexisEntryType);
Expand Down Expand Up @@ -242,7 +242,22 @@ public Optional<Prescription> createLocalObject(MedicationRequest fhirObject) {

localObject.setBemerkung(getMedicationRequestRemark(fhirObject));

return Optional.of( (Prescription) PrescriptionService.save(localObject));
Optional<String> prescriptionType = getMedicationRequestPrescriptionType(fhirObject);
prescriptionType.ifPresent(p -> localObject.setPrescriptionType(p));

return Optional.of((Prescription) PrescriptionService.save(localObject));
}
return Optional.empty();
}

private Optional<String> getMedicationRequestPrescriptionType(MedicationRequest fhirObject) {
List<Extension> extensionsEntryType = fhirObject.getExtensionsByUrl(EXTENSION_PRESCRIPTION_ENTRYTYPE_URL);
for (Extension extension : extensionsEntryType) {
try {
EntryType entryType = EntryType.valueOf(((CodeType) extension.getValue()).getValue());
return Optional.of(Integer.toString(entryType.numericValue()));
} catch (IllegalArgumentException iae) {
}
}
return Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package info.elexis.server.fhir.rest.core.resources;


import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
Expand All @@ -13,6 +12,8 @@

import org.hl7.fhir.dstu3.model.Bundle;
import org.hl7.fhir.dstu3.model.Bundle.BundleEntryComponent;
import org.hl7.fhir.dstu3.model.CodeType;
import org.hl7.fhir.dstu3.model.Extension;
import org.hl7.fhir.dstu3.model.MedicationRequest;
import org.hl7.fhir.dstu3.model.MedicationRequest.MedicationRequestStatus;
import org.hl7.fhir.dstu3.model.Patient;
Expand All @@ -22,6 +23,7 @@
import ca.uhn.fhir.rest.api.MethodOutcome;
import ca.uhn.fhir.rest.client.IGenericClient;
import ch.elexis.core.findings.util.ModelUtil;
import ch.elexis.core.model.prescription.EntryType;
import info.elexis.server.core.connector.elexis.jpa.test.TestDatabaseInitializer;
import info.elexis.server.fhir.rest.core.test.AllTests;

Expand All @@ -38,17 +40,16 @@ public static void setupClass() throws IOException, SQLException {

client = ModelUtil.getGenericClient("http://localhost:8380/fhir");
assertNotNull(client);
patient = client.read().resource(Patient.class).withId(AllTests.getTestDatabaseInitializer().getPatient().getId())
.execute();
patient = client.read().resource(Patient.class)
.withId(AllTests.getTestDatabaseInitializer().getPatient().getId()).execute();
assertNotNull(patient);
}

@Test
public void getMedicationRequest() {
// test with full id url
Bundle results = client.search().forResource(MedicationRequest.class)
.where(MedicationRequest.PATIENT.hasId(patient.getId())).returnBundle(Bundle.class)
.execute();
.where(MedicationRequest.PATIENT.hasId(patient.getId())).returnBundle(Bundle.class).execute();
assertNotNull(results);
List<BundleEntryComponent> entries = results.getEntry();
assertFalse(entries.isEmpty());
Expand Down Expand Up @@ -80,8 +81,14 @@ public void updateMedicationRequest() {
assertTrue(activeOrder.isPresent());
MedicationRequest updateOrder = activeOrder.get();
updateOrder.getDosageInstruction().get(0).setText("test");
List<Extension> entryTypes = updateOrder
.getExtensionsByUrl("www.elexis.info/extensions/prescription/entrytype");
assertEquals(EntryType.UNKNOWN.name(), ((CodeType) entryTypes.get(0).getValue()).getValue());
entryTypes.get(0).setValue(new CodeType(EntryType.SYMPTOMATIC_MEDICATION.name()));

// update the medication
MethodOutcome outcome = client.update().resource(updateOrder).execute();

// read and validate change
MedicationRequest oldOrder = client.read().resource(MedicationRequest.class).withId(activeOrder.get().getId())
.execute();
Expand All @@ -91,6 +98,9 @@ public void updateMedicationRequest() {
assertEquals(MedicationRequestStatus.COMPLETED, oldOrder.getStatus());
assertEquals(MedicationRequestStatus.ACTIVE, newOrder.getStatus());
assertEquals("test", newOrder.getDosageInstruction().get(0).getText());
entryTypes = newOrder.getExtensionsByUrl("www.elexis.info/extensions/prescription/entrytype");
assertEquals(EntryType.SYMPTOMATIC_MEDICATION.name(), ((CodeType) entryTypes.get(0).getValue()).getValue());

}

private Optional<MedicationRequest> getActiveOrderWithDosage(List<BundleEntryComponent> orders) {
Expand Down

0 comments on commit 653a3f5

Please sign in to comment.