Skip to content

Commit

Permalink
EA-179: Fix Locale issue while saving Medication (#218)
Browse files Browse the repository at this point in the history
* EA-179: Fix Locale issue while saving Medication

* EA-179: Refactored and removed unused imports
  • Loading branch information
SanoferSameera authored Dec 8, 2023
1 parent 51b01ab commit 89b2b9b
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<Concept> 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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,24 @@ public OrderFrequency getOrderFrequencyByName(String conceptName, boolean includ
}
return null;
}

public Concept getDoseUnitsConceptByName(String conceptName) {
List<Concept> dosingUnits = orderService.getDrugDosingUnits();
for (Concept doseUnit : dosingUnits) {
if(doseUnit.getName().getName().equals(conceptName)){
return doseUnit;
}
}
return null;
}

public Concept getDispenseUnitsConceptByName(String conceptName) {
List<Concept> dispensingUnits = orderService.getDrugDispensingUnits();
for (Concept dispensingUnit : dispensingUnits) {
if(dispensingUnit.getName().getName().equals(conceptName)){
return dispensingUnit;
}
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ 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();
when(conceptService.getConceptByName(dosingInstructions.getFrequency())).thenReturn(frequencyConcept);
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);
Expand All @@ -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())));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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>());

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>());

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()));
Expand Down

0 comments on commit 89b2b9b

Please sign in to comment.