diff --git a/api-1.12/src/main/java/org/openmrs/module/emrapi/encounter/mapper/DosingInstructionsMapper.java b/api-1.12/src/main/java/org/openmrs/module/emrapi/encounter/mapper/DosingInstructionsMapper.java index aaf5eaef6..e0682faf3 100644 --- a/api-1.12/src/main/java/org/openmrs/module/emrapi/encounter/mapper/DosingInstructionsMapper.java +++ b/api-1.12/src/main/java/org/openmrs/module/emrapi/encounter/mapper/DosingInstructionsMapper.java @@ -30,13 +30,13 @@ public DosingInstructionsMapper(ConceptService conceptService, OrderMetadataServ public DrugOrder map(EncounterTransaction.DosingInstructions dosingInstructions, DrugOrder drugOrder) { drugOrder.setDose(dosingInstructions.getDose()); - drugOrder.setDoseUnits(conceptByName(dosingInstructions.getDoseUnits())); + drugOrder.setDoseUnits(orderMetadataService.getDoseUnitsConceptByName(dosingInstructions.getDoseUnits())); drugOrder.setDosingInstructions(dosingInstructions.getAdministrationInstructions()); drugOrder.setRoute(conceptByName(dosingInstructions.getRoute())); drugOrder.setAsNeeded(dosingInstructions.getAsNeeded()); drugOrder.setFrequency(orderMetadataService.getOrderFrequencyByName(dosingInstructions.getFrequency(), false)); drugOrder.setQuantity(Double.valueOf(dosingInstructions.getQuantity())); - drugOrder.setQuantityUnits(conceptByName(dosingInstructions.getQuantityUnits())); + drugOrder.setQuantityUnits(orderMetadataService.getDispenseUnitsConceptByName(dosingInstructions.getQuantityUnits())); Integer numberOfRefills = dosingInstructions.getNumberOfRefills(); drugOrder.setNumRefills(numberOfRefills == null? 0: numberOfRefills); return drugOrder; diff --git a/api-1.12/src/main/java/org/openmrs/module/emrapi/encounter/mapper/OpenMRSDrugOrderMapper.java b/api-1.12/src/main/java/org/openmrs/module/emrapi/encounter/mapper/OpenMRSDrugOrderMapper.java index c375d9933..e74665bf2 100644 --- a/api-1.12/src/main/java/org/openmrs/module/emrapi/encounter/mapper/OpenMRSDrugOrderMapper.java +++ b/api-1.12/src/main/java/org/openmrs/module/emrapi/encounter/mapper/OpenMRSDrugOrderMapper.java @@ -28,6 +28,9 @@ import org.openmrs.api.context.Context; import org.openmrs.module.emrapi.encounter.domain.EncounterTransaction; import org.openmrs.module.emrapi.encounter.service.OrderMetadataService; +import org.openmrs.util.LocaleUtility; + +import java.util.List; /** * OpenMRSDrugOrderMapper. @@ -70,7 +73,18 @@ public DrugOrder map(EncounterTransaction.DrugOrder drugOrder, Encounter encount openMRSDrugOrder.setUrgency(drugOrder.getScheduledDate() != null ? Order.Urgency.ON_SCHEDULED_DATE : DEFAULT_URGENCY); openMRSDrugOrder.setDuration(drugOrder.getDuration()); openMRSDrugOrder.setSortWeight(drugOrder.getSortWeight()); - openMRSDrugOrder.setDurationUnits(orderMetadataService.getDurationUnitsConceptByName(drugOrder.getDurationUnits())); + + Concept durationUnitsConcept = orderMetadataService.getDurationUnitsConceptByName(drugOrder.getDurationUnits()); + if (durationUnitsConcept != null) { + openMRSDrugOrder.setDurationUnits(durationUnitsConcept); + } + else { + List conceptList = conceptService.getConceptsByName(drugOrder.getDurationUnits(), LocaleUtility.getDefaultLocale(), null); + if (conceptList.size() > 0) { + openMRSDrugOrder.setDurationUnits(conceptList.get(0)); + } + } + openMRSDrugOrder.setAutoExpireDate(drugOrder.getAutoExpireDate()); if(drugOrder.getOrderReasonConcept() != null) { conceptByUuid = conceptService.getConceptByUuid(drugOrder.getOrderReasonConcept().getUuid()); diff --git a/api-1.12/src/main/java/org/openmrs/module/emrapi/encounter/service/OrderMetadataService.java b/api-1.12/src/main/java/org/openmrs/module/emrapi/encounter/service/OrderMetadataService.java index d112f92fe..d498656b8 100644 --- a/api-1.12/src/main/java/org/openmrs/module/emrapi/encounter/service/OrderMetadataService.java +++ b/api-1.12/src/main/java/org/openmrs/module/emrapi/encounter/service/OrderMetadataService.java @@ -49,4 +49,24 @@ public OrderFrequency getOrderFrequencyByName(String conceptName, boolean includ } return null; } + + public Concept getDoseUnitsConceptByName(String conceptName) { + List dosingUnits = orderService.getDrugDosingUnits(); + for (Concept doseUnit : dosingUnits) { + if(doseUnit.getName().getName().equals(conceptName)){ + return doseUnit; + } + } + return null; + } + + public Concept getDispenseUnitsConceptByName(String conceptName) { + List dispensingUnits = orderService.getDrugDispensingUnits(); + for (Concept dispensingUnit : dispensingUnits) { + if(dispensingUnit.getName().getName().equals(conceptName)){ + return dispensingUnit; + } + } + return null; + } } diff --git a/api-1.12/src/test/java/org/openmrs/module/emrapi/encounter/mapper/DosingInstructionsMapperTest.java b/api-1.12/src/test/java/org/openmrs/module/emrapi/encounter/mapper/DosingInstructionsMapperTest.java index b0b1650c5..0c3f39557 100644 --- a/api-1.12/src/test/java/org/openmrs/module/emrapi/encounter/mapper/DosingInstructionsMapperTest.java +++ b/api-1.12/src/test/java/org/openmrs/module/emrapi/encounter/mapper/DosingInstructionsMapperTest.java @@ -46,7 +46,7 @@ public void setup() { public void shouldMapDosingInstructionsToDrugOrder() { DosingInstructions dosingInstructions = DosingInstructionsBuilder.sample(); Concept capsuleConcept = new Concept(); - when(conceptService.getConceptByName(dosingInstructions.getDoseUnits())).thenReturn(capsuleConcept); + when(orderMetadataService.getDoseUnitsConceptByName(dosingInstructions.getDoseUnits())).thenReturn(capsuleConcept); Concept routeConcept = new Concept(); when(conceptService.getConceptByName(dosingInstructions.getRoute())).thenReturn(routeConcept); Concept frequencyConcept = new Concept(); @@ -54,7 +54,7 @@ public void shouldMapDosingInstructionsToDrugOrder() { OrderFrequency orderFrequency = new OrderFrequency(); when(orderMetadataService.getOrderFrequencyByName("QDS", false)).thenReturn(orderFrequency); Concept quantityUnits = new Concept(); - when(conceptService.getConceptByName(dosingInstructions.getQuantityUnits())).thenReturn(quantityUnits); + when(orderMetadataService.getDispenseUnitsConceptByName(dosingInstructions.getQuantityUnits())).thenReturn(quantityUnits); DrugOrder drugOrder = new DrugOrder(); DosingInstructionsMapper dosingInstructionsMapper = new DosingInstructionsMapper(conceptService, orderMetadataService); @@ -67,7 +67,7 @@ public void shouldMapDosingInstructionsToDrugOrder() { assertThat(drugOrder.getRoute(), is(equalTo(routeConcept))); assertThat(drugOrder.getFrequency(), is(equalTo(orderFrequency))); assertThat(drugOrder.getAsNeeded(), is(equalTo(false))); - assertThat(drugOrder.getQuantity(), is(equalTo(Double.valueOf(dosingInstructions.getQuantity())))); + assertThat(drugOrder.getQuantity(), is(equalTo(dosingInstructions.getQuantity()))); assertThat(drugOrder.getQuantityUnits(), is(equalTo(quantityUnits))); assertThat(drugOrder.getNumRefills(), is(equalTo(dosingInstructions.getNumberOfRefills()))); } diff --git a/api-1.12/src/test/java/org/openmrs/module/emrapi/encounter/service/OrderMetadataServiceTest.java b/api-1.12/src/test/java/org/openmrs/module/emrapi/encounter/service/OrderMetadataServiceTest.java index f956fe93f..392044a38 100644 --- a/api-1.12/src/test/java/org/openmrs/module/emrapi/encounter/service/OrderMetadataServiceTest.java +++ b/api-1.12/src/test/java/org/openmrs/module/emrapi/encounter/service/OrderMetadataServiceTest.java @@ -38,6 +38,9 @@ public class OrderMetadataServiceTest extends BaseModuleContextSensitiveTest { public static final String WEEKS_CONCEPT_NAME = "Weeks"; public static final String ONCE_A_DAY_CONCEPT_NAME = "Once A Day"; public static final String TWICE_A_DAY_CONCEPT_NAME = "Twice A Day"; + public static final String MG = "mg"; + public static final String ML = "ml"; + public static final String TABLET = "tablet"; @Mock private OrderService orderService; @@ -105,6 +108,56 @@ public void shouldReturnNullOrderFrequencyForNullInput() { } + @Test + public void shouldGetDoseUnitsConceptByName() throws Exception { + Concept mgConcept = createConcept(MG); + Concept mlConcept = createConcept(ML); + when(orderService.getDrugDosingUnits()).thenReturn(Arrays.asList(mgConcept, mlConcept)); + + Concept doseUnitsConcept = orderMetadataService.getDoseUnitsConceptByName(MG); + + assertThat(doseUnitsConcept, is(mgConcept)); + } + + @Test + public void shouldReturnNullIfDoseUnitsConceptDoesNotExist() throws Exception { + when(orderService.getDrugDosingUnits()).thenReturn(new ArrayList()); + + Concept doseUnitsConcept = orderMetadataService.getDoseUnitsConceptByName(MG); + + assertNull(doseUnitsConcept); + } + + @Test + public void shouldReturnNullDoseUnitsForNullInput() { + assertNull(orderMetadataService.getDoseUnitsConceptByName(null)); + } + + @Test + public void shouldGetDispenseUnitsConceptByName() throws Exception { + Concept tabletConcept = createConcept(TABLET); + Concept mlConcept = createConcept(ML); + when(orderService.getDrugDispensingUnits()).thenReturn(Arrays.asList(tabletConcept, mlConcept)); + + Concept dispensingUnitsConcept = orderMetadataService.getDispenseUnitsConceptByName(TABLET); + + assertThat(dispensingUnitsConcept, is(tabletConcept)); + } + + @Test + public void shouldReturnNullIfDispenseUnitsConceptDoesNotExist() throws Exception { + when(orderService.getDrugDispensingUnits()).thenReturn(new ArrayList()); + + Concept dispensingUnitsConcept = orderMetadataService.getDispenseUnitsConceptByName(TABLET); + + assertNull(dispensingUnitsConcept); + } + + @Test + public void shouldReturnNullDispenseUnitsForNullInput() { + assertNull(orderMetadataService.getDispenseUnitsConceptByName(null)); + } + private Concept createConcept(String conceptName) { Concept concept = new Concept(); concept.setPreferredName(new ConceptName(conceptName, Locale.getDefault()));