From e85eb48a7572fe40eba631ce45bfd2d68fab5b0b Mon Sep 17 00:00:00 2001 From: Kavitha S Date: Thu, 2 Feb 2023 23:31:50 +0530 Subject: [PATCH 01/10] BAH-2763 | add implementation for sending prescription over sms --- .../bahmni/test/builder/PersonBuilder.java | 14 +++ bahmnicore-api/pom.xml | 5 + .../contract/SMS/PrescriptionSMS.java | 22 ++++ .../bahmnicore/contract/SMS/SMSRequest.java | 26 ++++ .../bahmnicore/properties/SMSProperties.java | 37 ++++++ .../service/BahmniDrugOrderService.java | 7 ++ .../module/bahmnicore/service/SMSService.java | 12 ++ .../impl/BahmniDrugOrderServiceImpl.java | 81 +++++++++++++ .../service/impl/SMSServiceImpl.java | 70 +++++++++++ .../bahmnicore/util/BahmniDateUtil.java | 11 ++ .../impl/BahmniDrugOrderServiceImplTest.java | 113 ++++++++++++++++++ .../service/impl/SMSServiceImplIT.java | 84 +++++++++++++ .../controller/BahmniDrugOrderController.java | 39 +++++- .../BahmniDrugOrderControllerTest.java | 82 ++++++++++++- 14 files changed, 601 insertions(+), 2 deletions(-) create mode 100644 bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/contract/SMS/PrescriptionSMS.java create mode 100644 bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/contract/SMS/SMSRequest.java create mode 100644 bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/properties/SMSProperties.java create mode 100644 bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/SMSService.java create mode 100644 bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/SMSServiceImpl.java create mode 100644 bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/service/impl/SMSServiceImplIT.java diff --git a/bahmni-test-commons/src/test/java/org/bahmni/test/builder/PersonBuilder.java b/bahmni-test-commons/src/test/java/org/bahmni/test/builder/PersonBuilder.java index 48b87bcb77..73fa5348f3 100644 --- a/bahmni-test-commons/src/test/java/org/bahmni/test/builder/PersonBuilder.java +++ b/bahmni-test-commons/src/test/java/org/bahmni/test/builder/PersonBuilder.java @@ -1,6 +1,10 @@ package org.bahmni.test.builder; import org.openmrs.Person; +import org.openmrs.PersonName; + +import java.util.HashSet; +import java.util.Set; public class PersonBuilder { @@ -15,6 +19,16 @@ public PersonBuilder withUUID(String patientUuid) { return this; } + public PersonBuilder withPersonName(String personNameValue) { + PersonName personName = new PersonName(); + personName.setGivenName(personNameValue); + personName.setId(2); + Set personNames = new HashSet<>(); + personNames.add(personName); + person.setNames(personNames); + return this; + } + public Person build() { return person; } diff --git a/bahmnicore-api/pom.xml b/bahmnicore-api/pom.xml index 83f5ff206d..b2a83be767 100644 --- a/bahmnicore-api/pom.xml +++ b/bahmnicore-api/pom.xml @@ -205,6 +205,11 @@ org.openmrs.module emrapi-api-1.12 + + org.apache.httpcomponents + httpclient + 4.5.14 + diff --git a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/contract/SMS/PrescriptionSMS.java b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/contract/SMS/PrescriptionSMS.java new file mode 100644 index 0000000000..9d9ded49a3 --- /dev/null +++ b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/contract/SMS/PrescriptionSMS.java @@ -0,0 +1,22 @@ +package org.bahmni.module.bahmnicore.contract.SMS; + +public class PrescriptionSMS { + private String visitUuid; + private String locale = "en"; + + public String getVisitUuid() { + return visitUuid; + } + + public void setVisitUuid(String visitUuid) { + this.visitUuid = visitUuid; + } + + public String getLocale() { + return locale; + } + + public void setLocale(String locale) { + this.locale = locale; + } +} \ No newline at end of file diff --git a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/contract/SMS/SMSRequest.java b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/contract/SMS/SMSRequest.java new file mode 100644 index 0000000000..4a37ad4288 --- /dev/null +++ b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/contract/SMS/SMSRequest.java @@ -0,0 +1,26 @@ +package org.bahmni.module.bahmnicore.contract.SMS; + +import org.codehaus.jackson.annotate.JsonProperty; + +public class SMSRequest { + private String phoneNumber; + private String message; + + @JsonProperty + public String getPhoneNumber() { + return phoneNumber; + } + + public void setPhoneNumber(String phoneNumber) { + this.phoneNumber = phoneNumber; + } + + @JsonProperty + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } +} \ No newline at end of file diff --git a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/properties/SMSProperties.java b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/properties/SMSProperties.java new file mode 100644 index 0000000000..8c2a259d70 --- /dev/null +++ b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/properties/SMSProperties.java @@ -0,0 +1,37 @@ +package org.bahmni.module.bahmnicore.properties; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.openmrs.util.OpenmrsUtil; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.Properties; + +public class SMSProperties { + private static Properties properties; + private static Log log = LogFactory.getLog(SMSProperties.class); + + public static void load() { + String propertyFile = new File(OpenmrsUtil.getApplicationDataDirectory(), "sms.properties").getAbsolutePath(); + log.info(String.format("Reading sms properties from : %s", propertyFile)); + try { + properties = new Properties(System.getProperties()); + properties.load(new FileInputStream(propertyFile)); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + public static String getProperty(String key){ + if(properties == null){ + load(); + } + return properties.getProperty(key); + } + + public static void initalize(Properties props) { + properties = props; + } +} diff --git a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/BahmniDrugOrderService.java b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/BahmniDrugOrderService.java index 6d57f6a571..9a4187f3b8 100644 --- a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/BahmniDrugOrderService.java +++ b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/BahmniDrugOrderService.java @@ -38,4 +38,11 @@ List getInactiveDrugOrders(String patientUuid, Set concepts, List getDrugOrders(String patientUuid, Boolean isActive, Set conceptsToFilter, Set conceptsToExclude, String patientProgramUuid) throws ParseException; + + Map getMergedDrugOrderMap(List drugOrderList); + + String getPrescriptionAsString(Map drugOrderDurationMap); + + String getAllProviderAsString(List drugOrders); + } diff --git a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/SMSService.java b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/SMSService.java new file mode 100644 index 0000000000..28ded7f4c1 --- /dev/null +++ b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/SMSService.java @@ -0,0 +1,12 @@ +package org.bahmni.module.bahmnicore.service; + +import org.openmrs.Patient; + +import java.util.Date; + +public interface SMSService { + + String getPrescriptionMessage(String lang, Date visitDate, Patient patient, String location, String providerDetail, String prescriptionDetail); + + Object sendSMS(String phoneNumber, String message); +} diff --git a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/BahmniDrugOrderServiceImpl.java b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/BahmniDrugOrderServiceImpl.java index 5f54ca0c5b..2c34acf072 100644 --- a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/BahmniDrugOrderServiceImpl.java +++ b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/BahmniDrugOrderServiceImpl.java @@ -7,6 +7,7 @@ import org.bahmni.module.bahmnicore.contract.drugorder.DrugOrderConfigResponse; import org.bahmni.module.bahmnicore.contract.drugorder.OrderFrequencyData; import org.bahmni.module.bahmnicore.dao.OrderDao; +import org.bahmni.module.bahmnicore.properties.SMSProperties; import org.bahmni.module.bahmnicore.service.BahmniDrugOrderService; import org.bahmni.module.bahmnicore.service.BahmniProgramWorkflowService; import org.openmrs.CareSetting; @@ -30,6 +31,7 @@ import org.openmrs.module.emrapi.utils.HibernateLazyLoader; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import org.springframework.util.StringUtils; import java.io.IOException; import java.text.ParseException; @@ -39,9 +41,13 @@ import java.util.Date; import java.util.List; import java.util.Map; +import java.util.LinkedHashMap; import java.util.Set; +import java.util.LinkedHashSet; import java.util.stream.Collectors; +import static org.bahmni.module.bahmnicore.util.BahmniDateUtil.convertUTCToGivenFormat; + @Service public class BahmniDrugOrderServiceImpl implements BahmniDrugOrderService { private ConceptService conceptService; @@ -167,6 +173,81 @@ public List getAllDrugOrders(String patientUuid, String patientProgramUui return orderDao.getAllOrders(patientByUuid, orderTypeByUuid, conceptsForDrugs, drugConceptsToBeExcluded, encounters); } + @Override + public Map getMergedDrugOrderMap(List drugOrderList) { + Map mergedDrugOrderMap = new LinkedHashMap<>(); + for(BahmniDrugOrder drugOrder : drugOrderList) { + BahmniDrugOrder foundDrugOrder = mergedDrugOrderMap.entrySet().stream() + .map(x -> x.getKey()) + .filter( existingOrder -> + areValuesEqual(existingOrder.getDrugNonCoded(), drugOrder.getDrugNonCoded()) && + (existingOrder.getDrug()!=null && drugOrder.getDrug()!=null && + areValuesEqual(existingOrder.getDrug().getUuid(), drugOrder.getDrug().getUuid())) && + areValuesEqual(existingOrder.getInstructions(), drugOrder.getInstructions()) && + compareDosingInstruction(existingOrder.getDosingInstructions(), drugOrder.getDosingInstructions()) && + areValuesEqual(existingOrder.getDosingInstructions().getRoute(), drugOrder.getDosingInstructions().getRoute()) && + areValuesEqual(existingOrder.getDosingInstructions().getAdministrationInstructions(), drugOrder.getDosingInstructions().getAdministrationInstructions()) && + areValuesEqual(existingOrder.getDosingInstructions().getAsNeeded(), drugOrder.getDosingInstructions().getAsNeeded()) && + areValuesEqual(existingOrder.getDateStopped(), drugOrder.getDateStopped()) && + getDateDifferenceInDays(existingOrder.getEffectiveStopDate(), drugOrder.getEffectiveStartDate()) <= 1.0 ) + .findFirst() + .orElse(null); + if (foundDrugOrder!=null) { + mergedDrugOrderMap.put(foundDrugOrder, mergedDrugOrderMap.get(foundDrugOrder)+drugOrder.getDuration()); + } else { + mergedDrugOrderMap.put(drugOrder, drugOrder.getDuration()); + } + } + return mergedDrugOrderMap; + } + + @Override + public String getPrescriptionAsString(Map drugOrderDurationMap) { + String prescriptionString = ""; + int counter = 1; + for (Map.Entry entry : drugOrderDurationMap.entrySet()) { + prescriptionString += counter++ + ". " + getDrugOrderString(entry.getKey(), drugOrderDurationMap.get(entry.getKey())) + "\n"; + } + return prescriptionString; + } + + @Override + public String getAllProviderAsString(List drugOrders) { + Set providerSet = new LinkedHashSet(); + for (BahmniDrugOrder drugOrder : drugOrders) { + providerSet.add("Dr " + drugOrder.getProvider().getName()); + } + return StringUtils.collectionToCommaDelimitedString(providerSet); + } + + private String getDrugOrderString(BahmniDrugOrder drugOrder, Integer duration) { + String drugOrderString = drugOrder.getDrug().getName(); + drugOrderString += ", " + (drugOrder.getDosingInstructions().getDose().intValue()) + " " + drugOrder.getDosingInstructions().getDoseUnits(); + drugOrderString += ", " + drugOrder.getDosingInstructions().getFrequency() + "-" + duration.toString() + " " + drugOrder.getDurationUnits(); + drugOrderString += ", start from " + convertUTCToGivenFormat(drugOrder.getEffectiveStartDate(), "dd-MM-yyyy", SMSProperties.getProperty("sms.timeZone")); + if(drugOrder.getDateStopped() != null) + drugOrderString += ", stopped on " + convertUTCToGivenFormat(drugOrder.getDateStopped(), "dd-MM-yyyy", SMSProperties.getProperty("sms.timeZone")); + return drugOrderString; + } + + private Boolean areValuesEqual(Object value1, Object value2) { + if(value1 != null && value2 != null) { + return value1.equals(value2); + } + return (value1 == null && value2 == null); + }; + + private Boolean compareDosingInstruction(EncounterTransaction.DosingInstructions value1, EncounterTransaction.DosingInstructions value2) { + String doseAndFrequency1 = value1.getDose() + " " + value1.getDoseUnits() + ", " + value1.getFrequency(); + String doseAndFrequency2 = value2.getDose() + " " + value2.getDoseUnits() + ", " + value2.getFrequency(); + return doseAndFrequency1.equals(doseAndFrequency2); + }; + + private double getDateDifferenceInDays(Date date1, Date date2){ + long diff = date2.getTime() - date1.getTime(); + return (diff / (1000*60*60*24)); + } + private List fetchOrderAttributeConcepts() { Concept orderAttributesConceptSet = conceptService.getConceptByName(BahmniOrderAttribute.ORDER_ATTRIBUTES_CONCEPT_SET_NAME); if(orderAttributesConceptSet != null){ diff --git a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/SMSServiceImpl.java b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/SMSServiceImpl.java new file mode 100644 index 0000000000..5b1c40c700 --- /dev/null +++ b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/SMSServiceImpl.java @@ -0,0 +1,70 @@ +package org.bahmni.module.bahmnicore.service.impl; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import org.bahmni.module.bahmnicore.contract.SMS.SMSRequest; +import org.bahmni.module.bahmnicore.properties.SMSProperties; +import org.bahmni.module.bahmnicore.service.BahmniDrugOrderService; +import org.bahmni.module.bahmnicore.service.SMSService; +import org.openmrs.Patient; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.Date; + +import static org.bahmni.module.bahmnicore.util.BahmniDateUtil.convertUTCToGivenFormat; + +import com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.http.HttpResponse; +import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.HttpClientBuilder; + +@Service +public class SMSServiceImpl implements SMSService { + private static Logger logger = LogManager.getLogger(BahmniDrugOrderService.class); + + @Autowired + public SMSServiceImpl() {} + + @Override + public Object sendSMS(String phoneNumber, String message) { + try { + SMSRequest smsRequest = new SMSRequest(); + smsRequest.setPhoneNumber(phoneNumber); + smsRequest.setMessage(message); + + ObjectMapper Obj = new ObjectMapper(); + String jsonObject = Obj.writeValueAsString(smsRequest); + StringEntity params = new StringEntity(jsonObject); + + HttpPost request = new HttpPost(SMSProperties.getProperty("sms.url")); + request.addHeader("content-type", "application/json"); + request.setEntity(params); + HttpClient httpClient = HttpClientBuilder.create().build(); + HttpResponse response = httpClient.execute(request); + + return response.getStatusLine(); + } catch (Exception e) { + logger.error("Exception occured in sending sms ", e); + throw new RuntimeException("Exception occured in sending sms ", e); + } + } + + @Override + public String getPrescriptionMessage(String lang, Date visitDate, Patient patient, String location, String providerDetail, String prescriptionDetail) { + String prescriptionSMSContent = SMSProperties.getProperty("sms." + lang + ".prescriptionSMS"); + prescriptionSMSContent = prescriptionSMSContent.replace("#visitDate", convertUTCToGivenFormat(visitDate, "dd-MM-yyyy", SMSProperties.getProperty("sms.timeZone"))); + prescriptionSMSContent = prescriptionSMSContent.replace("#patientName",patient.getGivenName() + " " + patient.getFamilyName()); + prescriptionSMSContent = prescriptionSMSContent.replace("#gender", patient.getGender()); + prescriptionSMSContent = prescriptionSMSContent.replace("#age", patient.getAge().toString()); + prescriptionSMSContent = prescriptionSMSContent.replace("#doctorDetail", providerDetail); + prescriptionSMSContent = prescriptionSMSContent.replace("#location", location); + prescriptionSMSContent = prescriptionSMSContent.replace("#prescriptionDetails", prescriptionDetail); + return prescriptionSMSContent; + } + +} diff --git a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/util/BahmniDateUtil.java b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/util/BahmniDateUtil.java index f157433edb..4c329a6907 100644 --- a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/util/BahmniDateUtil.java +++ b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/util/BahmniDateUtil.java @@ -2,6 +2,7 @@ import org.apache.commons.lang3.StringUtils; +import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; @@ -47,4 +48,14 @@ public static Date convertToLocalDateFromUTC(String dateString) throws ParseExce simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); return simpleDateFormat.parse(dateString); } + + public static String convertUTCToGivenFormat(Date dateTime, String format, String timeZone) { + if (dateTime == null || StringUtils.isEmpty(format) || StringUtils.isEmpty(timeZone)) { + return null; + } + DateFormat givenFormat = new SimpleDateFormat(format); + TimeZone givenTimeZone = TimeZone.getTimeZone(timeZone); + givenFormat.setTimeZone(givenTimeZone); + return givenFormat.format(dateTime); + } } diff --git a/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/service/impl/BahmniDrugOrderServiceImplTest.java b/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/service/impl/BahmniDrugOrderServiceImplTest.java index db08d279f4..0af1d477e2 100644 --- a/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/service/impl/BahmniDrugOrderServiceImplTest.java +++ b/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/service/impl/BahmniDrugOrderServiceImplTest.java @@ -1,9 +1,12 @@ package org.bahmni.module.bahmnicore.service.impl; +import org.apache.commons.lang3.time.DateUtils; import org.bahmni.module.bahmnicore.dao.OrderDao; +import org.bahmni.module.bahmnicore.properties.SMSProperties; import org.bahmni.module.bahmnicore.service.BahmniProgramWorkflowService; import org.junit.Before; import org.junit.Test; +import org.junit.runner.RunWith; import org.mockito.ArgumentCaptor; import org.mockito.InjectMocks; import org.mockito.Mock; @@ -16,13 +19,22 @@ import org.openmrs.api.OrderService; import org.openmrs.api.PatientService; import org.openmrs.module.bahmniemrapi.drugorder.contract.BahmniDrugOrder; +import org.openmrs.module.emrapi.encounter.domain.EncounterTransaction; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; import java.text.ParseException; +import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.HashSet; import java.util.List; +import java.util.Map; +import java.util.LinkedHashMap; +import java.util.Locale; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Matchers.any; @@ -35,6 +47,8 @@ import static org.mockito.Mockito.when; import static org.mockito.MockitoAnnotations.initMocks; +@RunWith(PowerMockRunner.class) +@PrepareForTest({SMSProperties.class}) public class BahmniDrugOrderServiceImplTest { public static final String PATIENT_PROGRAM_UUID = "patient-program-uuid"; @@ -113,4 +127,103 @@ public void shouldNotConsiderEncountersToFetchDrugOrdersIfPatientProgramUuidIsNu verify(orderDao).getAllOrders(mockPatient, mockOrderType,conceptsToFilter, null, encounters); verifyNoMoreInteractions(bahmniProgramWorkflowService); } + + @Test + public void shouldReturnMergedDrugOrderAsMap() throws Exception { + List bahmniDrugOrderList = buildBahmniDrugOrderList(); + Map mergedDrugOrderMap = bahmniDrugOrderService.getMergedDrugOrderMap(bahmniDrugOrderList); + Map expectedMergedDrugOrderMap = new LinkedHashMap<>(); + expectedMergedDrugOrderMap.put(bahmniDrugOrderList.get(0), 10); + expectedMergedDrugOrderMap.put(bahmniDrugOrderList.get(2), 3); + assertEquals(expectedMergedDrugOrderMap, mergedDrugOrderMap); + } + + @Test + public void shouldReturnPrescriptionAsString() throws Exception { + PowerMockito.mockStatic(SMSProperties.class); + when(SMSProperties.getProperty("sms.timeZone")).thenReturn("IST"); + Date drugOrderStartDate = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH).parse("January 30, 2023"); + EncounterTransaction.DrugOrder etDrugOrder = createETDrugOrder("1", "Paracetamol", 2.0, "Once a day", drugOrderStartDate, 5); + BahmniDrugOrder bahmniDrugOrder = createBahmniDrugOrder(null, etDrugOrder); + Map drugOrderDurationMap = new LinkedHashMap<>(); + drugOrderDurationMap.put(bahmniDrugOrder, 10); + String prescriptionString = bahmniDrugOrderService.getPrescriptionAsString(drugOrderDurationMap); + String expectedPrescriptionString = "1. Paracetamol, 2 tab (s), Once a day-10 Days, start from 30-01-2023\n"; + assertEquals(expectedPrescriptionString, prescriptionString); + } + + @Test + public void shouldReturnAllUniqueProvidersAsString() throws Exception { + List bahmniDrugOrderList = buildBahmniDrugOrderList(); + String providerString = bahmniDrugOrderService.getAllProviderAsString(bahmniDrugOrderList); + String expectedProviderString = "Dr Harry,Dr Grace"; + assertEquals(expectedProviderString, providerString); + } + + private List buildBahmniDrugOrderList() { + List bahmniDrugOrderList = new ArrayList<>(); + try { + EncounterTransaction.Provider provider = createETProvider("1", "Harry"); + Date drugOrderStartDate = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH).parse("January 30, 2023"); + EncounterTransaction.DrugOrder etDrugOrder = createETDrugOrder("1", "Paracetamol", 2.0, "Once a day", drugOrderStartDate, 5); + BahmniDrugOrder bahmniDrugOrder = createBahmniDrugOrder(provider, etDrugOrder); + bahmniDrugOrderList.add(bahmniDrugOrder); + + provider = createETProvider("2", "Grace"); + drugOrderStartDate = DateUtils.addDays(drugOrderStartDate, 5); + etDrugOrder = createETDrugOrder("1", "Paracetamol", 2.0, "Once a day", drugOrderStartDate, 5); + bahmniDrugOrder = createBahmniDrugOrder(provider, etDrugOrder); + bahmniDrugOrderList.add(bahmniDrugOrder); + + provider = createETProvider("1", "Harry"); + drugOrderStartDate = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH).parse("January 30, 2023"); + etDrugOrder = createETDrugOrder("2", "Amoxicillin", 1.0, "Twice a day", drugOrderStartDate, 3); + bahmniDrugOrder = createBahmniDrugOrder(provider, etDrugOrder); + bahmniDrugOrderList.add(bahmniDrugOrder); + } catch (ParseException e) { + e.printStackTrace(); + } + return bahmniDrugOrderList; + } + + private BahmniDrugOrder createBahmniDrugOrder(EncounterTransaction.Provider provider, EncounterTransaction.DrugOrder etDrugOrder) { + BahmniDrugOrder bahmniDrugOrder = new BahmniDrugOrder(); + bahmniDrugOrder.setDrugOrder(etDrugOrder); + bahmniDrugOrder.setProvider(provider); + return bahmniDrugOrder; + } + + private EncounterTransaction.Provider createETProvider(String uuid, String name) { + EncounterTransaction.Provider provider = new EncounterTransaction.Provider(); + provider.setUuid(uuid); + provider.setName(name); + return provider; + } + + private EncounterTransaction.DrugOrder createETDrugOrder(String drugUuid, String drugName, Double dose, String frequency, Date effectiveStartDate, Integer duration) { + EncounterTransaction.Drug encounterTransactionDrug = new EncounterTransaction.Drug(); + encounterTransactionDrug.setUuid(drugUuid); + encounterTransactionDrug.setName(drugName); + + EncounterTransaction.DosingInstructions dosingInstructions = new EncounterTransaction.DosingInstructions(); + dosingInstructions.setAdministrationInstructions("{\"instructions\":\"As directed\"}"); + dosingInstructions.setAsNeeded(false); + dosingInstructions.setDose(dose); + dosingInstructions.setDoseUnits("tab (s)"); + dosingInstructions.setFrequency(frequency); + dosingInstructions.setNumberOfRefills(0); + dosingInstructions.setRoute("UNKNOWN"); + + EncounterTransaction.DrugOrder drugOrder = new EncounterTransaction.DrugOrder(); + drugOrder.setOrderType("Drug Order"); + drugOrder.setDrug(encounterTransactionDrug); + drugOrder.setDosingInstructions(dosingInstructions); + drugOrder.setDuration(duration); + drugOrder.setDurationUnits("Days"); + drugOrder.setEffectiveStartDate(effectiveStartDate); + drugOrder.setEffectiveStopDate(DateUtils.addDays(effectiveStartDate, duration)); + drugOrder.setVoided(false); + + return drugOrder; + } } diff --git a/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/service/impl/SMSServiceImplIT.java b/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/service/impl/SMSServiceImplIT.java new file mode 100644 index 0000000000..9210bb4283 --- /dev/null +++ b/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/service/impl/SMSServiceImplIT.java @@ -0,0 +1,84 @@ +package org.bahmni.module.bahmnicore.service.impl; + +import org.bahmni.module.bahmnicore.properties.SMSProperties; +import org.bahmni.test.builder.PersonBuilder; +import org.bahmni.test.builder.VisitBuilder; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; +import org.openmrs.Visit; +import org.openmrs.Person; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PowerMockIgnore; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Locale; + +import static org.hamcrest.Matchers.instanceOf; +import static org.hamcrest.Matchers.not; +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.*; + +@RunWith(PowerMockRunner.class) +@PrepareForTest({SMSProperties.class}) +@PowerMockIgnore("javax.management.*") +public class SMSServiceImplIT { + + private SMSServiceImpl smsService; + + @Rule + public ExpectedException expectedEx = ExpectedException.none(); + + @Before + public void setUp() throws Exception { + smsService = new SMSServiceImpl(); + } + + @Test + public void shouldReturnPrescriptionSMS() throws ParseException { + PowerMockito.mockStatic(SMSProperties.class); + when(SMSProperties.getProperty("sms.en.prescriptionSMS")).thenReturn("Date: #visitDate\nPrescription For Patient: #patientName, #gender, #age years. \nDoctor: #doctorDetail (#location)\n#prescriptionDetails"); + when(SMSProperties.getProperty("sms.timeZone")).thenReturn("IST"); + Date visitDate = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH).parse("January 30, 2023"); + Date birthDate = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH).parse("January 30, 2010"); + Person person = new PersonBuilder().withUUID("puuid").withPersonName("testPersonName").build(); + person.setGender("M"); + person.setBirthdate(birthDate); + Visit visit = new VisitBuilder().withPerson(person).withUUID("vuuid").withStartDatetime(visitDate).build(); + + String prescriptionContent = smsService.getPrescriptionMessage("en", visit.getStartDatetime(), visit.getPatient(), "Bahmni", "Superman", "1. Paracetamol 150 mg/ml, 50 ml, Immediately-1 Days, start from 31-01-2023"); + String expectedPrescriptionContent = "Date: 30-01-2023\n" + + "Prescription For Patient: testPersonName null, M, 13 years. \n" + + "Doctor: Superman (Bahmni)\n" + + "1. Paracetamol 150 mg/ml, 50 ml, Immediately-1 Days, start from 31-01-2023"; + assertEquals(expectedPrescriptionContent, prescriptionContent); + } + + @Test + public void shouldThrowNullPointerExceptionOnNullUrl() throws Exception { + PowerMockito.mockStatic(SMSProperties.class); + when(SMSProperties.getProperty("sms.url")).thenReturn(null); + expectedEx.expect(RuntimeException.class); + expectedEx.expectMessage("Exception occured in sending sms"); + expectedEx.expectCause(instanceOf(java.lang.NullPointerException.class)); + smsService.sendSMS("+919999999999", "Welcome"); + } + + @Test + public void shouldNotThrowNullPointerExceptionOnValidUrl() throws Exception { + PowerMockito.mockStatic(SMSProperties.class); + when(SMSProperties.getProperty("sms.url")).thenReturn("http://google.com"); + expectedEx.expect(RuntimeException.class); + expectedEx.expectMessage("Exception occured in sending sms"); + expectedEx.isAnyExceptionExpected(); + expectedEx.expectCause(not(instanceOf(java.lang.NullPointerException.class))); + smsService.sendSMS("+919999999999", "Welcome"); + } + +} diff --git a/bahmnicore-omod/src/main/java/org/bahmni/module/bahmnicore/web/v1_0/controller/BahmniDrugOrderController.java b/bahmnicore-omod/src/main/java/org/bahmni/module/bahmnicore/web/v1_0/controller/BahmniDrugOrderController.java index afc0306678..6d7f6a76f4 100644 --- a/bahmnicore-omod/src/main/java/org/bahmni/module/bahmnicore/web/v1_0/controller/BahmniDrugOrderController.java +++ b/bahmnicore-omod/src/main/java/org/bahmni/module/bahmnicore/web/v1_0/controller/BahmniDrugOrderController.java @@ -3,11 +3,15 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import org.bahmni.module.bahmnicore.contract.SMS.PrescriptionSMS; import org.bahmni.module.bahmnicore.service.BahmniDrugOrderService; import org.bahmni.module.bahmnicore.service.BahmniObsService; +import org.bahmni.module.bahmnicore.service.BahmniVisitService; +import org.bahmni.module.bahmnicore.service.SMSService; import org.bahmni.module.bahmnicore.util.BahmniDateUtil; import org.openmrs.Concept; import org.openmrs.DrugOrder; +import org.openmrs.Visit; import org.openmrs.api.ConceptService; import org.openmrs.module.bahmniemrapi.drugorder.contract.BahmniDrugOrder; import org.openmrs.module.bahmniemrapi.drugorder.contract.BahmniOrderAttribute; @@ -21,6 +25,7 @@ import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.RequestBody; import java.io.IOException; import java.text.ParseException; @@ -35,6 +40,7 @@ import java.util.List; import java.util.Map; import java.util.Set; +import java.util.stream.Collectors; @Controller public class BahmniDrugOrderController extends BaseRestController { @@ -53,9 +59,17 @@ public class BahmniDrugOrderController extends BaseRestController { private BahmniDrugOrderMapper bahmniDrugOrderMapper; - public BahmniDrugOrderController(BahmniDrugOrderService drugOrderService) { + @Autowired + private BahmniVisitService bahmniVisitService; + + @Autowired + private SMSService smsService; + + public BahmniDrugOrderController(BahmniDrugOrderService drugOrderService, BahmniVisitService bahmniVisitService, SMSService smsService) { this.drugOrderService = drugOrderService; this.bahmniDrugOrderMapper = new BahmniDrugOrderMapper(); + this.bahmniVisitService = bahmniVisitService; + this.smsService = smsService; } public BahmniDrugOrderController() { this.bahmniDrugOrderMapper = new BahmniDrugOrderMapper(); @@ -129,6 +143,18 @@ public List getDrugOrderDetails(@RequestParam(value = "patientU return drugOrderService.getDrugOrders(patientUuid, isActive, drugConceptsToBeFiltered, drugConceptsToBeExcluded, patientProgramUuid); } + @RequestMapping(value = baseUrl+ "/sendPrescriptionSMS", method = RequestMethod.POST) + @ResponseBody + public Object sendPrescriptionSMS(@RequestBody PrescriptionSMS prescription) throws Exception{ + Visit visit = bahmniVisitService.getVisitSummary(prescription.getVisitUuid()); + List drugOrderList = getSortedBahmniDrugOrdersForVisit(visit.getPatient().getUuid(), visit.getUuid()); + Map mergedDrugOrderMap = drugOrderService.getMergedDrugOrderMap(drugOrderList); + String providerString = drugOrderService.getAllProviderAsString(drugOrderList); + String prescriptionString = drugOrderService.getPrescriptionAsString(mergedDrugOrderMap); + String prescriptionSMSContent = smsService.getPrescriptionMessage(prescription.getLocale(), visit.getStartDatetime(), visit.getPatient(), visit.getLocation().toString(), providerString, prescriptionString); + return smsService.sendSMS(visit.getPatient().getAttribute("phoneNumber").getValue(), prescriptionSMSContent); + } + Set getDrugConcepts(String drugConceptSetName){ if(drugConceptSetName == null) return null; Set drugConcepts = new HashSet<>(); @@ -214,4 +240,15 @@ private Map> groupDrugOrdersAccordingToOrderS return groupedDrugOrders; } + + private List getSortedBahmniDrugOrdersForVisit(String patientUuid, String visitUuid) { + List visitUuidList = new ArrayList<>(); + visitUuidList.add(visitUuid); + List drugOrderList = getPrescribedOrders(visitUuidList, patientUuid, + null,null, null, null, null); + List sortedDrugOrderList = drugOrderList.stream() + .sorted(Comparator.comparing(BahmniDrugOrder::getEffectiveStartDate)) + .collect(Collectors.toList()); + return sortedDrugOrderList; + } } diff --git a/bahmnicore-omod/src/test/java/org/bahmni/module/bahmnicore/web/v1_0/controller/BahmniDrugOrderControllerTest.java b/bahmnicore-omod/src/test/java/org/bahmni/module/bahmnicore/web/v1_0/controller/BahmniDrugOrderControllerTest.java index 9ba211900f..1706c9a745 100644 --- a/bahmnicore-omod/src/test/java/org/bahmni/module/bahmnicore/web/v1_0/controller/BahmniDrugOrderControllerTest.java +++ b/bahmnicore-omod/src/test/java/org/bahmni/module/bahmnicore/web/v1_0/controller/BahmniDrugOrderControllerTest.java @@ -1,27 +1,57 @@ package org.bahmni.module.bahmnicore.web.v1_0.controller; +import org.bahmni.module.bahmnicore.contract.SMS.PrescriptionSMS; +import org.bahmni.module.bahmnicore.properties.SMSProperties; import org.bahmni.module.bahmnicore.service.BahmniDrugOrderService; +import org.bahmni.module.bahmnicore.service.BahmniObsService; +import org.bahmni.module.bahmnicore.service.BahmniVisitService; +import org.bahmni.module.bahmnicore.service.SMSService; +import org.bahmni.test.builder.PersonBuilder; +import org.bahmni.test.builder.VisitBuilder; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; +import org.openmrs.Visit; +import org.openmrs.Person; +import org.openmrs.Location; +import org.openmrs.PersonAttribute; +import org.openmrs.PersonAttributeType; import org.openmrs.api.ConceptService; +import org.openmrs.module.bahmniemrapi.drugorder.contract.BahmniDrugOrder; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; +import java.text.SimpleDateFormat; import java.util.Set; +import java.util.ArrayList; +import java.util.Date; +import java.util.Locale; +import java.util.HashSet; import static org.junit.Assert.assertNull; import static org.mockito.Mockito.when; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; import static org.mockito.MockitoAnnotations.initMocks; -@RunWith(MockitoJUnitRunner.class) +@RunWith(PowerMockRunner.class) +@PrepareForTest({SMSProperties.class}) public class BahmniDrugOrderControllerTest { @Mock ConceptService conceptService; @Mock BahmniDrugOrderService bahmniDrugOrderService; + @Mock + BahmniVisitService bahmniVisitService; + @Mock + SMSService smsService; + @Mock + BahmniObsService bahmniObsService; @InjectMocks BahmniDrugOrderController bahmniDrugOrderController; @@ -44,4 +74,54 @@ public void shouldReturnNullIfDrugConceptNameIsNull() { Set drugConcepts = bahmniDrugOrderController.getDrugConcepts(null); assertNull(drugConcepts); } + + @Test + public void shouldReturnErrorResponseForSendingPrescriptionSMS() throws Exception { + PrescriptionSMS prescriptionSMS = new PrescriptionSMS(); + prescriptionSMS.setVisitUuid("visit-uuid"); + prescriptionSMS.setLocale("en"); + Visit visit = createVisitForTest(); + String sampleSMSContent = "Date: 30-01-2023\n" + + "Prescription For Patient: testPersonName null, M, 13 years.\n" + + "Doctor: Superman (Bahmni)\n" + + "1. Paracetamol 150 mg/ml, 50 ml, Immediately-1 Days, start from 31-01-2023"; + + PowerMockito.mockStatic(SMSProperties.class); + when(SMSProperties.getProperty("sms.en.prescriptionSMS")).thenReturn("Date: #visitDate\nPrescription For Patient: #patientName, #gender, #age years.\nDoctor: #doctorDetail (#location)\n#prescriptionDetails"); + when(SMSProperties.getProperty("sms.timeZone")).thenReturn("IST"); + when(SMSProperties.getProperty("sms.url")).thenReturn("dummyurl"); + when(bahmniVisitService.getVisitSummary(prescriptionSMS.getVisitUuid())).thenReturn(visit); + when(bahmniDrugOrderService.getMergedDrugOrderMap(new ArrayList())).thenReturn(null); + when(bahmniDrugOrderService.getAllProviderAsString(new ArrayList())).thenReturn("Superman"); + when(bahmniDrugOrderService.getPrescriptionAsString(null)).thenReturn("1. Paracetamol 150 mg/ml, 50 ml, Immediately-1 Days, start from 31-01-2023"); + when(smsService.getPrescriptionMessage(prescriptionSMS.getLocale(), visit.getStartDatetime(), visit.getPatient(), + "Bahmni", "Superman", "1. Paracetamol 150 mg/ml, 50 ml, Immediately-1 Days, start from 31-01-2023")) + .thenReturn(sampleSMSContent); + bahmniDrugOrderController.sendPrescriptionSMS(prescriptionSMS); + + verify(smsService, times(1)).sendSMS("+919999999999", sampleSMSContent); + } + + private Visit createVisitForTest() throws Exception { + Date visitDate = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH).parse("January 30, 2023"); + Date birthDate = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH).parse("January 30, 2010"); + Person person = new PersonBuilder().withUUID("puuid").withPersonName("testPersonName").build(); + person.setGender("M"); + person.setBirthdate(birthDate); + PersonAttribute pa = new PersonAttribute(); + pa.setValue("+919999999999"); + PersonAttributeType pat = new PersonAttributeType(); + pat.setName("phoneNumber"); + pa.setAttributeType(pat); + Set paSet = new HashSet<>(); + paSet.add(pa); + person.setAttributes(paSet); + + Visit visit = new VisitBuilder().withPerson(person).withUUID("visit-uuid").withStartDatetime(visitDate).build(); + Location location = new Location(); + location.setName("Bahmni"); + visit.setLocation(location); + + return visit; + } } \ No newline at end of file From 5fa9953429666c733ae67705a98cc51acd2c5e86 Mon Sep 17 00:00:00 2001 From: Kavitha S Date: Fri, 3 Feb 2023 13:32:37 +0530 Subject: [PATCH 02/10] BAH-2763 | refactor to get parent location of the visit --- .../module/bahmnicore/service/BahmniVisitService.java | 3 +++ .../bahmnicore/service/impl/BahmniVisitServiceImpl.java | 8 ++++++++ .../web/v1_0/controller/BahmniDrugOrderController.java | 5 +++-- .../v1_0/controller/BahmniDrugOrderControllerTest.java | 1 + 4 files changed, 15 insertions(+), 2 deletions(-) diff --git a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/BahmniVisitService.java b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/BahmniVisitService.java index 65f5618e3d..2a0ac89bd1 100644 --- a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/BahmniVisitService.java +++ b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/BahmniVisitService.java @@ -1,6 +1,7 @@ package org.bahmni.module.bahmnicore.service; import org.openmrs.Encounter; +import org.openmrs.Location; import org.openmrs.Visit; import java.util.List; @@ -11,4 +12,6 @@ public interface BahmniVisitService { Visit getVisitSummary(String visitUuid); List getAdmitAndDischargeEncounters(Integer visitId); + + String getParentLocationNameForVisit(Location location); } diff --git a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/BahmniVisitServiceImpl.java b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/BahmniVisitServiceImpl.java index 0b5cc86631..a1ffac18d0 100644 --- a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/BahmniVisitServiceImpl.java +++ b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/BahmniVisitServiceImpl.java @@ -3,6 +3,7 @@ import org.bahmni.module.bahmnicore.dao.VisitDao; import org.bahmni.module.bahmnicore.service.BahmniVisitService; import org.openmrs.Encounter; +import org.openmrs.Location; import org.openmrs.Visit; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -33,4 +34,11 @@ public Visit getVisitSummary(String visitUuid) { public List getAdmitAndDischargeEncounters(Integer visitId) { return visitDao.getAdmitAndDischargeEncounters(visitId); } + + @Override + public String getParentLocationNameForVisit(Location location) { + if(location.getParentLocation()!=null) + getParentLocationNameForVisit(location.getParentLocation()); + return location.getName(); + } } diff --git a/bahmnicore-omod/src/main/java/org/bahmni/module/bahmnicore/web/v1_0/controller/BahmniDrugOrderController.java b/bahmnicore-omod/src/main/java/org/bahmni/module/bahmnicore/web/v1_0/controller/BahmniDrugOrderController.java index 6d7f6a76f4..6ab8d47225 100644 --- a/bahmnicore-omod/src/main/java/org/bahmni/module/bahmnicore/web/v1_0/controller/BahmniDrugOrderController.java +++ b/bahmnicore-omod/src/main/java/org/bahmni/module/bahmnicore/web/v1_0/controller/BahmniDrugOrderController.java @@ -145,13 +145,14 @@ public List getDrugOrderDetails(@RequestParam(value = "patientU @RequestMapping(value = baseUrl+ "/sendPrescriptionSMS", method = RequestMethod.POST) @ResponseBody - public Object sendPrescriptionSMS(@RequestBody PrescriptionSMS prescription) throws Exception{ + public Object sendPrescriptionSMS(@RequestBody PrescriptionSMS prescription) throws Exception { Visit visit = bahmniVisitService.getVisitSummary(prescription.getVisitUuid()); + String locationName = bahmniVisitService.getParentLocationNameForVisit(visit.getLocation()); List drugOrderList = getSortedBahmniDrugOrdersForVisit(visit.getPatient().getUuid(), visit.getUuid()); Map mergedDrugOrderMap = drugOrderService.getMergedDrugOrderMap(drugOrderList); String providerString = drugOrderService.getAllProviderAsString(drugOrderList); String prescriptionString = drugOrderService.getPrescriptionAsString(mergedDrugOrderMap); - String prescriptionSMSContent = smsService.getPrescriptionMessage(prescription.getLocale(), visit.getStartDatetime(), visit.getPatient(), visit.getLocation().toString(), providerString, prescriptionString); + String prescriptionSMSContent = smsService.getPrescriptionMessage(prescription.getLocale(), visit.getStartDatetime(), visit.getPatient(), locationName, providerString, prescriptionString); return smsService.sendSMS(visit.getPatient().getAttribute("phoneNumber").getValue(), prescriptionSMSContent); } diff --git a/bahmnicore-omod/src/test/java/org/bahmni/module/bahmnicore/web/v1_0/controller/BahmniDrugOrderControllerTest.java b/bahmnicore-omod/src/test/java/org/bahmni/module/bahmnicore/web/v1_0/controller/BahmniDrugOrderControllerTest.java index 1706c9a745..52742ec2f5 100644 --- a/bahmnicore-omod/src/test/java/org/bahmni/module/bahmnicore/web/v1_0/controller/BahmniDrugOrderControllerTest.java +++ b/bahmnicore-omod/src/test/java/org/bahmni/module/bahmnicore/web/v1_0/controller/BahmniDrugOrderControllerTest.java @@ -91,6 +91,7 @@ public void shouldReturnErrorResponseForSendingPrescriptionSMS() throws Exceptio when(SMSProperties.getProperty("sms.timeZone")).thenReturn("IST"); when(SMSProperties.getProperty("sms.url")).thenReturn("dummyurl"); when(bahmniVisitService.getVisitSummary(prescriptionSMS.getVisitUuid())).thenReturn(visit); + when(bahmniVisitService.getParentLocationNameForVisit(visit.getLocation())).thenReturn(visit.getLocation().getName()); when(bahmniDrugOrderService.getMergedDrugOrderMap(new ArrayList())).thenReturn(null); when(bahmniDrugOrderService.getAllProviderAsString(new ArrayList())).thenReturn("Superman"); when(bahmniDrugOrderService.getPrescriptionAsString(null)).thenReturn("1. Paracetamol 150 mg/ml, 50 ml, Immediately-1 Days, start from 31-01-2023"); From a944d13470286b0408d668205d1211269a531ff5 Mon Sep 17 00:00:00 2001 From: Kavitha S Date: Mon, 6 Feb 2023 20:38:57 +0530 Subject: [PATCH 03/10] BAH-2763 | refactor to wrap with authorization and reading sms templates --- .../service/BahmniDrugOrderService.java | 5 +- .../service/SharePrescriptionService.java | 9 ++ .../impl/BahmniDrugOrderServiceImpl.java | 55 +++++++-- .../service/impl/SMSServiceImpl.java | 29 +++-- .../impl/SharePrescriptionServiceImpl.java | 45 +++++++ .../impl/BahmniDrugOrderServiceImplTest.java | 22 ++-- .../service/impl/SMSServiceImplIT.java | 59 ++++++--- .../impl/SharePrescriptionServiceImplIT.java | 112 ++++++++++++++++++ .../controller/BahmniDrugOrderController.java | 38 ++---- bahmnicore-omod/src/main/resources/config.xml | 6 + .../src/main/resources/messages.properties | 6 +- .../BahmniDrugOrderControllerTest.java | 76 +----------- 12 files changed, 312 insertions(+), 150 deletions(-) create mode 100644 bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/SharePrescriptionService.java create mode 100644 bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/SharePrescriptionServiceImpl.java create mode 100644 bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/service/impl/SharePrescriptionServiceImplIT.java diff --git a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/BahmniDrugOrderService.java b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/BahmniDrugOrderService.java index 9a4187f3b8..309b059266 100644 --- a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/BahmniDrugOrderService.java +++ b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/BahmniDrugOrderService.java @@ -15,6 +15,7 @@ import java.util.List; import java.util.Map; import java.util.Set; +import java.util.Locale; public interface BahmniDrugOrderService { List getActiveDrugOrders(String patientUuid, Date startDate, Date endDate); @@ -39,9 +40,11 @@ List getInactiveDrugOrders(String patientUuid, Set concepts, List getDrugOrders(String patientUuid, Boolean isActive, Set conceptsToFilter, Set conceptsToExclude, String patientProgramUuid) throws ParseException; + List getSortedBahmniDrugOrdersForVisit(String patientUuid, String visitUuid); + Map getMergedDrugOrderMap(List drugOrderList); - String getPrescriptionAsString(Map drugOrderDurationMap); + String getPrescriptionAsString(Map drugOrderDurationMap, Locale locale); String getAllProviderAsString(List drugOrders); diff --git a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/SharePrescriptionService.java b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/SharePrescriptionService.java new file mode 100644 index 0000000000..2f8c3c10c9 --- /dev/null +++ b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/SharePrescriptionService.java @@ -0,0 +1,9 @@ +package org.bahmni.module.bahmnicore.service; + +import org.bahmni.module.bahmnicore.contract.SMS.PrescriptionSMS; +import org.openmrs.annotation.Authorized; + +public interface SharePrescriptionService { + @Authorized({"app:clinical"}) + Object sendPresciptionSMS(PrescriptionSMS prescription); +} diff --git a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/BahmniDrugOrderServiceImpl.java b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/BahmniDrugOrderServiceImpl.java index 2c34acf072..cc3db8a9eb 100644 --- a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/BahmniDrugOrderServiceImpl.java +++ b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/BahmniDrugOrderServiceImpl.java @@ -7,8 +7,8 @@ import org.bahmni.module.bahmnicore.contract.drugorder.DrugOrderConfigResponse; import org.bahmni.module.bahmnicore.contract.drugorder.OrderFrequencyData; import org.bahmni.module.bahmnicore.dao.OrderDao; -import org.bahmni.module.bahmnicore.properties.SMSProperties; import org.bahmni.module.bahmnicore.service.BahmniDrugOrderService; +import org.bahmni.module.bahmnicore.service.BahmniObsService; import org.bahmni.module.bahmnicore.service.BahmniProgramWorkflowService; import org.openmrs.CareSetting; import org.openmrs.Concept; @@ -26,6 +26,7 @@ import org.openmrs.module.bahmniemrapi.drugorder.contract.BahmniDrugOrder; import org.openmrs.module.bahmniemrapi.drugorder.contract.BahmniOrderAttribute; import org.openmrs.module.bahmniemrapi.drugorder.mapper.BahmniDrugOrderMapper; +import org.openmrs.module.bahmniemrapi.encountertransaction.contract.BahmniObservation; import org.openmrs.module.emrapi.encounter.ConceptMapper; import org.openmrs.module.emrapi.encounter.domain.EncounterTransaction; import org.openmrs.module.emrapi.utils.HibernateLazyLoader; @@ -45,6 +46,9 @@ import java.util.Set; import java.util.LinkedHashSet; import java.util.stream.Collectors; +import java.util.Locale; +import java.util.Arrays; +import java.util.Comparator; import static org.bahmni.module.bahmnicore.util.BahmniDateUtil.convertUTCToGivenFormat; @@ -57,21 +61,22 @@ public class BahmniDrugOrderServiceImpl implements BahmniDrugOrderService { private ConceptMapper conceptMapper = new ConceptMapper(); private BahmniProgramWorkflowService bahmniProgramWorkflowService; private BahmniDrugOrderMapper bahmniDrugOrderMapper; - + private BahmniObsService bahmniObsService; private static final String GP_DOSING_INSTRUCTIONS_CONCEPT_UUID = "order.dosingInstructionsConceptUuid"; private static Logger logger = LogManager.getLogger(BahmniDrugOrderService.class); - + private final static String SMS_TIMEZONE = "bahmni.sms.timezone"; @Autowired - public BahmniDrugOrderServiceImpl(ConceptService conceptService, OrderService orderService, - PatientService patientService, OrderDao orderDao, BahmniProgramWorkflowService bahmniProgramWorkflowService) { + public BahmniDrugOrderServiceImpl(ConceptService conceptService, OrderService orderService, PatientService patientService, OrderDao orderDao, + BahmniProgramWorkflowService bahmniProgramWorkflowService, BahmniObsService bahmniObsService) { this.conceptService = conceptService; this.orderService = orderService; this.openmrsPatientService = patientService; this.orderDao = orderDao; this.bahmniProgramWorkflowService = bahmniProgramWorkflowService; this.bahmniDrugOrderMapper = new BahmniDrugOrderMapper(); + this.bahmniObsService = bahmniObsService; } @@ -173,6 +178,19 @@ public List getAllDrugOrders(String patientUuid, String patientProgramUui return orderDao.getAllOrders(patientByUuid, orderTypeByUuid, conceptsForDrugs, drugConceptsToBeExcluded, encounters); } + @Override + public List getSortedBahmniDrugOrdersForVisit(String patientUuid, String visitUuid) { + List drugOrderList = getPrescribedDrugOrders(Arrays.asList(visitUuid), patientUuid, null,null, null, null, null); + List bahmniDrugOrderList = getBahmniDrugOrdersForVisit(patientUuid, drugOrderList); + Collections.sort(bahmniDrugOrderList, new Comparator() { + @Override + public int compare(BahmniDrugOrder o1, BahmniDrugOrder o2) { + return o1.getEffectiveStartDate().compareTo(o2.getEffectiveStartDate()); + } + }); + return bahmniDrugOrderList; + } + @Override public Map getMergedDrugOrderMap(List drugOrderList) { Map mergedDrugOrderMap = new LinkedHashMap<>(); @@ -202,11 +220,11 @@ public Map getMergedDrugOrderMap(List } @Override - public String getPrescriptionAsString(Map drugOrderDurationMap) { + public String getPrescriptionAsString(Map drugOrderDurationMap, Locale locale) { String prescriptionString = ""; int counter = 1; for (Map.Entry entry : drugOrderDurationMap.entrySet()) { - prescriptionString += counter++ + ". " + getDrugOrderString(entry.getKey(), drugOrderDurationMap.get(entry.getKey())) + "\n"; + prescriptionString += counter++ + ". " + getDrugOrderString(entry.getKey(), drugOrderDurationMap.get(entry.getKey()), locale) + "\n"; } return prescriptionString; } @@ -220,13 +238,13 @@ public String getAllProviderAsString(List drugOrders) { return StringUtils.collectionToCommaDelimitedString(providerSet); } - private String getDrugOrderString(BahmniDrugOrder drugOrder, Integer duration) { + private String getDrugOrderString(BahmniDrugOrder drugOrder, Integer duration, Locale locale) { String drugOrderString = drugOrder.getDrug().getName(); drugOrderString += ", " + (drugOrder.getDosingInstructions().getDose().intValue()) + " " + drugOrder.getDosingInstructions().getDoseUnits(); drugOrderString += ", " + drugOrder.getDosingInstructions().getFrequency() + "-" + duration.toString() + " " + drugOrder.getDurationUnits(); - drugOrderString += ", start from " + convertUTCToGivenFormat(drugOrder.getEffectiveStartDate(), "dd-MM-yyyy", SMSProperties.getProperty("sms.timeZone")); + drugOrderString += ", start from " + convertUTCToGivenFormat(drugOrder.getEffectiveStartDate(), "dd-MM-yyyy", Context.getMessageSourceService().getMessage(SMS_TIMEZONE, null, locale)); if(drugOrder.getDateStopped() != null) - drugOrderString += ", stopped on " + convertUTCToGivenFormat(drugOrder.getDateStopped(), "dd-MM-yyyy", SMSProperties.getProperty("sms.timeZone")); + drugOrderString += ", stopped on " + convertUTCToGivenFormat(drugOrder.getDateStopped(), "dd-MM-yyyy", Context.getMessageSourceService().getMessage(SMS_TIMEZONE, null, locale)); return drugOrderString; } @@ -248,6 +266,22 @@ private double getDateDifferenceInDays(Date date1, Date date2){ return (diff / (1000*60*60*24)); } + private Collection getOrdAttributeConcepts() { + Concept orderAttribute = conceptService.getConceptByName(BahmniOrderAttribute.ORDER_ATTRIBUTES_CONCEPT_SET_NAME); + return orderAttribute == null ? Collections.EMPTY_LIST : orderAttribute.getSetMembers(); + } + + private List getBahmniDrugOrdersForVisit(String patientUuid, List drugOrders) { + Map drugOrderMap = getDiscontinuedDrugOrders(drugOrders); + try { + Collection orderAttributeObs = bahmniObsService.observationsFor(patientUuid, getOrdAttributeConcepts(), null, null, false, null, null, null); + return bahmniDrugOrderMapper.mapToResponse(drugOrders, orderAttributeObs, drugOrderMap , null); + } catch (IOException e) { + logger.error("Could not parse drug order", e); + throw new RuntimeException("Could not parse drug order", e); + } + } + private List fetchOrderAttributeConcepts() { Concept orderAttributesConceptSet = conceptService.getConceptByName(BahmniOrderAttribute.ORDER_ATTRIBUTES_CONCEPT_SET_NAME); if(orderAttributesConceptSet != null){ @@ -281,7 +315,6 @@ private List getFrequencies() { .collect(Collectors.toList()); } - private List getActiveDrugOrders(String patientUuid, Date asOfDate, Set conceptsToFilter, Set conceptsToExclude, Date startDate, Date endDate, Collection encounters) { Patient patient = openmrsPatientService.getPatientByUuid(patientUuid); diff --git a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/SMSServiceImpl.java b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/SMSServiceImpl.java index 5b1c40c700..1598996952 100644 --- a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/SMSServiceImpl.java +++ b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/SMSServiceImpl.java @@ -1,18 +1,21 @@ package org.bahmni.module.bahmnicore.service.impl; +import org.apache.commons.lang.StringUtils; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.bahmni.module.bahmnicore.contract.SMS.SMSRequest; -import org.bahmni.module.bahmnicore.properties.SMSProperties; import org.bahmni.module.bahmnicore.service.BahmniDrugOrderService; import org.bahmni.module.bahmnicore.service.SMSService; import org.openmrs.Patient; +import org.openmrs.api.context.Context; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.text.MessageFormat; import java.util.Date; +import java.util.Locale; import static org.bahmni.module.bahmnicore.util.BahmniDateUtil.convertUTCToGivenFormat; @@ -26,6 +29,9 @@ @Service public class SMSServiceImpl implements SMSService { private static Logger logger = LogManager.getLogger(BahmniDrugOrderService.class); + private final static String PRESCRIPTION_SMS_TEMPLATE = "bahmni.prescriptionSMSTemplate"; + private final static String SMS_TIMEZONE = "bahmni.sms.timezone"; + private final static String SMS_URL = "bahmni.sms.url"; @Autowired public SMSServiceImpl() {} @@ -41,7 +47,7 @@ public Object sendSMS(String phoneNumber, String message) { String jsonObject = Obj.writeValueAsString(smsRequest); StringEntity params = new StringEntity(jsonObject); - HttpPost request = new HttpPost(SMSProperties.getProperty("sms.url")); + HttpPost request = new HttpPost(Context.getMessageSourceService().getMessage(SMS_URL, null, new Locale("en"))); request.addHeader("content-type", "application/json"); request.setEntity(params); HttpClient httpClient = HttpClientBuilder.create().build(); @@ -56,15 +62,16 @@ public Object sendSMS(String phoneNumber, String message) { @Override public String getPrescriptionMessage(String lang, Date visitDate, Patient patient, String location, String providerDetail, String prescriptionDetail) { - String prescriptionSMSContent = SMSProperties.getProperty("sms." + lang + ".prescriptionSMS"); - prescriptionSMSContent = prescriptionSMSContent.replace("#visitDate", convertUTCToGivenFormat(visitDate, "dd-MM-yyyy", SMSProperties.getProperty("sms.timeZone"))); - prescriptionSMSContent = prescriptionSMSContent.replace("#patientName",patient.getGivenName() + " " + patient.getFamilyName()); - prescriptionSMSContent = prescriptionSMSContent.replace("#gender", patient.getGender()); - prescriptionSMSContent = prescriptionSMSContent.replace("#age", patient.getAge().toString()); - prescriptionSMSContent = prescriptionSMSContent.replace("#doctorDetail", providerDetail); - prescriptionSMSContent = prescriptionSMSContent.replace("#location", location); - prescriptionSMSContent = prescriptionSMSContent.replace("#prescriptionDetails", prescriptionDetail); - return prescriptionSMSContent; + String smsTimeZone = Context.getMessageSourceService().getMessage(SMS_TIMEZONE, null, new Locale(lang)); + String smsTemplate = Context.getAdministrationService().getGlobalProperty(PRESCRIPTION_SMS_TEMPLATE); + Object[] arguments = {convertUTCToGivenFormat(visitDate, "dd-MM-yyyy", smsTimeZone), + patient.getGivenName() + " " + patient.getFamilyName(), patient.getGender(), patient.getAge().toString(), + providerDetail, location, prescriptionDetail}; + if (StringUtils.isBlank(smsTemplate)) { + return Context.getMessageSourceService().getMessage(PRESCRIPTION_SMS_TEMPLATE, arguments, new Locale(lang)); + } else { + return new MessageFormat(smsTemplate).format(arguments); + } } } diff --git a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/SharePrescriptionServiceImpl.java b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/SharePrescriptionServiceImpl.java new file mode 100644 index 0000000000..490698028f --- /dev/null +++ b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/SharePrescriptionServiceImpl.java @@ -0,0 +1,45 @@ +package org.bahmni.module.bahmnicore.service.impl; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.bahmni.module.bahmnicore.contract.SMS.PrescriptionSMS; +import org.bahmni.module.bahmnicore.service.BahmniDrugOrderService; +import org.bahmni.module.bahmnicore.service.BahmniVisitService; +import org.bahmni.module.bahmnicore.service.SMSService; +import org.bahmni.module.bahmnicore.service.SharePrescriptionService; +import org.openmrs.Visit; +import org.openmrs.module.bahmniemrapi.drugorder.contract.BahmniDrugOrder; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.Locale; +import java.util.Map; + +@Service +public class SharePrescriptionServiceImpl implements SharePrescriptionService { + private static Logger logger = LogManager.getLogger(BahmniDrugOrderService.class); + private BahmniVisitService bahmniVisitService; + private BahmniDrugOrderService drugOrderService; + private SMSService smsService; + + @Autowired + public SharePrescriptionServiceImpl(BahmniVisitService bahmniVisitService, BahmniDrugOrderService drugOrderService, SMSService smsService) { + this.bahmniVisitService = bahmniVisitService; + this.drugOrderService = drugOrderService; + this.smsService = smsService; + } + + @Override + public Object sendPresciptionSMS(PrescriptionSMS prescription) { + Visit visit = bahmniVisitService.getVisitSummary(prescription.getVisitUuid()); + String locationName = bahmniVisitService.getParentLocationNameForVisit(visit.getLocation()); + List drugOrderList = drugOrderService.getSortedBahmniDrugOrdersForVisit(visit.getPatient().getUuid(), visit.getUuid()); + Map mergedDrugOrderMap = drugOrderService.getMergedDrugOrderMap(drugOrderList); + String providerString = drugOrderService.getAllProviderAsString(drugOrderList); + String prescriptionString = drugOrderService.getPrescriptionAsString(mergedDrugOrderMap, new Locale(prescription.getLocale())); + String prescriptionSMSContent = smsService.getPrescriptionMessage(prescription.getLocale(), visit.getStartDatetime(), visit.getPatient(), locationName, providerString, prescriptionString); + return smsService.sendSMS(visit.getPatient().getAttribute("phoneNumber").getValue(), prescriptionSMSContent); + } + +} diff --git a/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/service/impl/BahmniDrugOrderServiceImplTest.java b/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/service/impl/BahmniDrugOrderServiceImplTest.java index 0af1d477e2..46b29d1041 100644 --- a/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/service/impl/BahmniDrugOrderServiceImplTest.java +++ b/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/service/impl/BahmniDrugOrderServiceImplTest.java @@ -2,7 +2,6 @@ import org.apache.commons.lang3.time.DateUtils; import org.bahmni.module.bahmnicore.dao.OrderDao; -import org.bahmni.module.bahmnicore.properties.SMSProperties; import org.bahmni.module.bahmnicore.service.BahmniProgramWorkflowService; import org.junit.Before; import org.junit.Test; @@ -18,9 +17,10 @@ import org.openmrs.Patient; import org.openmrs.api.OrderService; import org.openmrs.api.PatientService; +import org.openmrs.api.context.Context; +import org.openmrs.messagesource.MessageSourceService; import org.openmrs.module.bahmniemrapi.drugorder.contract.BahmniDrugOrder; import org.openmrs.module.emrapi.encounter.domain.EncounterTransaction; -import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; @@ -46,9 +46,10 @@ import static org.mockito.Mockito.verifyNoMoreInteractions; import static org.mockito.Mockito.when; import static org.mockito.MockitoAnnotations.initMocks; +import static org.powermock.api.mockito.PowerMockito.mockStatic; @RunWith(PowerMockRunner.class) -@PrepareForTest({SMSProperties.class}) +@PrepareForTest({Context.class}) public class BahmniDrugOrderServiceImplTest { public static final String PATIENT_PROGRAM_UUID = "patient-program-uuid"; @@ -62,6 +63,8 @@ public class BahmniDrugOrderServiceImplTest { OrderService orderService; @Mock OrderDao orderDao; + @Mock + MessageSourceService messageSourceService; @InjectMocks BahmniDrugOrderServiceImpl bahmniDrugOrderService; @@ -140,14 +143,15 @@ public void shouldReturnMergedDrugOrderAsMap() throws Exception { @Test public void shouldReturnPrescriptionAsString() throws Exception { - PowerMockito.mockStatic(SMSProperties.class); - when(SMSProperties.getProperty("sms.timeZone")).thenReturn("IST"); - Date drugOrderStartDate = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH).parse("January 30, 2023"); + mockStatic(Context.class); + when(Context.getMessageSourceService()).thenReturn(messageSourceService); + when(messageSourceService.getMessage("bahmni.sms.timezone", null, new Locale("en"))).thenReturn("IST"); + Date drugOrderStartDate = new SimpleDateFormat("MMMM d, yyyy", new Locale("en")).parse("January 30, 2023"); EncounterTransaction.DrugOrder etDrugOrder = createETDrugOrder("1", "Paracetamol", 2.0, "Once a day", drugOrderStartDate, 5); BahmniDrugOrder bahmniDrugOrder = createBahmniDrugOrder(null, etDrugOrder); Map drugOrderDurationMap = new LinkedHashMap<>(); drugOrderDurationMap.put(bahmniDrugOrder, 10); - String prescriptionString = bahmniDrugOrderService.getPrescriptionAsString(drugOrderDurationMap); + String prescriptionString = bahmniDrugOrderService.getPrescriptionAsString(drugOrderDurationMap, new Locale("en")); String expectedPrescriptionString = "1. Paracetamol, 2 tab (s), Once a day-10 Days, start from 30-01-2023\n"; assertEquals(expectedPrescriptionString, prescriptionString); } @@ -164,7 +168,7 @@ private List buildBahmniDrugOrderList() { List bahmniDrugOrderList = new ArrayList<>(); try { EncounterTransaction.Provider provider = createETProvider("1", "Harry"); - Date drugOrderStartDate = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH).parse("January 30, 2023"); + Date drugOrderStartDate = new SimpleDateFormat("MMMM d, yyyy", new Locale("en")).parse("January 30, 2023"); EncounterTransaction.DrugOrder etDrugOrder = createETDrugOrder("1", "Paracetamol", 2.0, "Once a day", drugOrderStartDate, 5); BahmniDrugOrder bahmniDrugOrder = createBahmniDrugOrder(provider, etDrugOrder); bahmniDrugOrderList.add(bahmniDrugOrder); @@ -176,7 +180,7 @@ private List buildBahmniDrugOrderList() { bahmniDrugOrderList.add(bahmniDrugOrder); provider = createETProvider("1", "Harry"); - drugOrderStartDate = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH).parse("January 30, 2023"); + drugOrderStartDate = new SimpleDateFormat("MMMM d, yyyy", new Locale("en")).parse("January 30, 2023"); etDrugOrder = createETDrugOrder("2", "Amoxicillin", 1.0, "Twice a day", drugOrderStartDate, 3); bahmniDrugOrder = createBahmniDrugOrder(provider, etDrugOrder); bahmniDrugOrderList.add(bahmniDrugOrder); diff --git a/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/service/impl/SMSServiceImplIT.java b/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/service/impl/SMSServiceImplIT.java index 9210bb4283..ef6f0a4397 100644 --- a/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/service/impl/SMSServiceImplIT.java +++ b/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/service/impl/SMSServiceImplIT.java @@ -1,6 +1,5 @@ package org.bahmni.module.bahmnicore.service.impl; -import org.bahmni.module.bahmnicore.properties.SMSProperties; import org.bahmni.test.builder.PersonBuilder; import org.bahmni.test.builder.VisitBuilder; import org.junit.Before; @@ -8,9 +7,12 @@ import org.junit.Test; import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; +import org.mockito.Mock; import org.openmrs.Visit; import org.openmrs.Person; -import org.powermock.api.mockito.PowerMockito; +import org.openmrs.api.AdministrationService; +import org.openmrs.api.context.Context; +import org.openmrs.messagesource.MessageSourceService; import org.powermock.core.classloader.annotations.PowerMockIgnore; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; @@ -24,29 +26,58 @@ import static org.hamcrest.Matchers.not; import static org.junit.Assert.assertEquals; import static org.mockito.Mockito.*; +import static org.mockito.MockitoAnnotations.initMocks; +import static org.powermock.api.mockito.PowerMockito.mockStatic; @RunWith(PowerMockRunner.class) -@PrepareForTest({SMSProperties.class}) +@PrepareForTest({Context.class}) @PowerMockIgnore("javax.management.*") public class SMSServiceImplIT { private SMSServiceImpl smsService; - + @Mock + AdministrationService administrationService; + @Mock + MessageSourceService messageSourceService; @Rule public ExpectedException expectedEx = ExpectedException.none(); @Before public void setUp() throws Exception { smsService = new SMSServiceImpl(); + initMocks(this); + mockStatic(Context.class); + when(Context.getAdministrationService()).thenReturn(administrationService); + when(Context.getMessageSourceService()).thenReturn(messageSourceService); + } + + @Test + public void shouldReturnPrescriptionSMSWithGlobalPropertyTemplate() throws ParseException { + when(administrationService.getGlobalProperty("bahmni.prescriptionSMSTemplate")).thenReturn("Date: {0}\nPrescription For Patient: {1}, {2}, {3} years.\nDoctor: {4} ({5})\n{6}"); + when(messageSourceService.getMessage("bahmni.sms.timezone", null, new Locale("en"))).thenReturn("IST"); + Date visitDate = new SimpleDateFormat("MMMM d, yyyy", new Locale("en")).parse("January 30, 2023"); + Date birthDate = new SimpleDateFormat("MMMM d, yyyy", new Locale("en")).parse("January 30, 2010"); + Person person = new PersonBuilder().withUUID("puuid").withPersonName("testPersonName").build(); + person.setGender("M"); + person.setBirthdate(birthDate); + Visit visit = new VisitBuilder().withPerson(person).withUUID("vuuid").withStartDatetime(visitDate).build(); + + String prescriptionContent = smsService.getPrescriptionMessage("en", visit.getStartDatetime(), visit.getPatient(), "Bahmni", "Superman", "1. Paracetamol 150 mg/ml, 50 ml, Immediately-1 Days, start from 31-01-2023"); + String expectedPrescriptionContent = "Date: 30-01-2023\n" + + "Prescription For Patient: testPersonName null, M, 13 years.\n" + + "Doctor: Superman (Bahmni)\n" + + "1. Paracetamol 150 mg/ml, 50 ml, Immediately-1 Days, start from 31-01-2023"; + assertEquals(expectedPrescriptionContent, prescriptionContent); } @Test - public void shouldReturnPrescriptionSMS() throws ParseException { - PowerMockito.mockStatic(SMSProperties.class); - when(SMSProperties.getProperty("sms.en.prescriptionSMS")).thenReturn("Date: #visitDate\nPrescription For Patient: #patientName, #gender, #age years. \nDoctor: #doctorDetail (#location)\n#prescriptionDetails"); - when(SMSProperties.getProperty("sms.timeZone")).thenReturn("IST"); - Date visitDate = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH).parse("January 30, 2023"); - Date birthDate = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH).parse("January 30, 2010"); + public void shouldReturnPrescriptionSMSWithDefaultTemplate() throws ParseException { + Object[] args = {"30-01-2023", "testPersonName null", "M", "13", "Superman", "Bahmni", "1. Paracetamol 150 mg/ml, 50 ml, Immediately-1 Days, start from 31-01-2023"}; + when(administrationService.getGlobalProperty("bahmni.prescriptionSMSTemplate")).thenReturn(null); + when(messageSourceService.getMessage("bahmni.prescriptionSMSTemplate", args, new Locale("en"))).thenReturn("Date: 30-01-2023\nPrescription For Patient: testPersonName null, M, 13 years.\nDoctor: Superman (Bahmni)\n1. Paracetamol 150 mg/ml, 50 ml, Immediately-1 Days, start from 31-01-2023"); + when(messageSourceService.getMessage("bahmni.sms.timezone", null, new Locale("en"))).thenReturn("IST"); + Date visitDate = new SimpleDateFormat("MMMM d, yyyy", new Locale("en")).parse("January 30, 2023"); + Date birthDate = new SimpleDateFormat("MMMM d, yyyy", new Locale("en")).parse("January 30, 2010"); Person person = new PersonBuilder().withUUID("puuid").withPersonName("testPersonName").build(); person.setGender("M"); person.setBirthdate(birthDate); @@ -54,7 +85,7 @@ public void shouldReturnPrescriptionSMS() throws ParseException { String prescriptionContent = smsService.getPrescriptionMessage("en", visit.getStartDatetime(), visit.getPatient(), "Bahmni", "Superman", "1. Paracetamol 150 mg/ml, 50 ml, Immediately-1 Days, start from 31-01-2023"); String expectedPrescriptionContent = "Date: 30-01-2023\n" + - "Prescription For Patient: testPersonName null, M, 13 years. \n" + + "Prescription For Patient: testPersonName null, M, 13 years.\n" + "Doctor: Superman (Bahmni)\n" + "1. Paracetamol 150 mg/ml, 50 ml, Immediately-1 Days, start from 31-01-2023"; assertEquals(expectedPrescriptionContent, prescriptionContent); @@ -62,8 +93,7 @@ public void shouldReturnPrescriptionSMS() throws ParseException { @Test public void shouldThrowNullPointerExceptionOnNullUrl() throws Exception { - PowerMockito.mockStatic(SMSProperties.class); - when(SMSProperties.getProperty("sms.url")).thenReturn(null); + when(messageSourceService.getMessage("bahmni.sms.url", null, new Locale("en"))).thenReturn(null); expectedEx.expect(RuntimeException.class); expectedEx.expectMessage("Exception occured in sending sms"); expectedEx.expectCause(instanceOf(java.lang.NullPointerException.class)); @@ -72,8 +102,7 @@ public void shouldThrowNullPointerExceptionOnNullUrl() throws Exception { @Test public void shouldNotThrowNullPointerExceptionOnValidUrl() throws Exception { - PowerMockito.mockStatic(SMSProperties.class); - when(SMSProperties.getProperty("sms.url")).thenReturn("http://google.com"); + when(messageSourceService.getMessage("bahmni.sms.url", null, new Locale("en"))).thenReturn("http://google.com"); expectedEx.expect(RuntimeException.class); expectedEx.expectMessage("Exception occured in sending sms"); expectedEx.isAnyExceptionExpected(); diff --git a/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/service/impl/SharePrescriptionServiceImplIT.java b/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/service/impl/SharePrescriptionServiceImplIT.java new file mode 100644 index 0000000000..c71e47787d --- /dev/null +++ b/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/service/impl/SharePrescriptionServiceImplIT.java @@ -0,0 +1,112 @@ +package org.bahmni.module.bahmnicore.service.impl; + +import org.bahmni.module.bahmnicore.contract.SMS.PrescriptionSMS; +import org.bahmni.module.bahmnicore.service.BahmniDrugOrderService; +import org.bahmni.module.bahmnicore.service.BahmniVisitService; +import org.bahmni.module.bahmnicore.service.SMSService; +import org.bahmni.test.builder.PersonBuilder; +import org.bahmni.test.builder.VisitBuilder; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.openmrs.Visit; +import org.openmrs.Location; +import org.openmrs.Person; +import org.openmrs.PersonAttribute; +import org.openmrs.PersonAttributeType; + +import org.openmrs.api.AdministrationService; +import org.openmrs.api.context.Context; +import org.openmrs.messagesource.MessageSourceService; +import org.openmrs.module.bahmniemrapi.drugorder.contract.BahmniDrugOrder; +import org.powermock.core.classloader.annotations.PowerMockIgnore; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.Locale; +import java.util.Set; +import java.util.HashSet; + +import static org.mockito.Mockito.when; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.times; +import static org.mockito.MockitoAnnotations.initMocks; + +@RunWith(PowerMockRunner.class) +@PrepareForTest({Context.class}) +@PowerMockIgnore("javax.management.*") +public class SharePrescriptionServiceImplIT { + + @Mock + BahmniDrugOrderService bahmniDrugOrderService; + @Mock + BahmniVisitService bahmniVisitService; + @Mock + SMSService smsService; + @InjectMocks + SharePrescriptionServiceImpl sharePrescriptionService; + @Mock + AdministrationService administrationService; + @Mock + MessageSourceService messageSourceService; + + @Before + public void setUp() throws Exception { + initMocks(this); + } + + @Test + public void shouldCallSendSMSForSendingPrescriptionSMS() throws Exception { + PrescriptionSMS prescriptionSMS = new PrescriptionSMS(); + prescriptionSMS.setVisitUuid("visit-uuid"); + prescriptionSMS.setLocale("en"); + Visit visit = createVisitForTest(); + String sampleSMSContent = "Date: 30-01-2023\n" + + "Prescription For Patient: testPersonName null, M, 13 years.\n" + + "Doctor: Superman (Bahmni)\n" + + "1. Paracetamol 150 mg/ml, 50 ml, Immediately-1 Days, start from 31-01-2023"; + + when(administrationService.getGlobalProperty("bahmni.prescriptionSMSTemplate")).thenReturn("Date: {0}\nPrescription For Patient: {1}, {2}, {3} years.\nDoctor: {4} ({5})\n{6}"); + when(messageSourceService.getMessage("bahmni.sms.timezone", null, new Locale("en"))).thenReturn("IST"); + when(messageSourceService.getMessage("bahmni.sms.url", null, new Locale("en"))).thenReturn(null); + when(bahmniVisitService.getVisitSummary(prescriptionSMS.getVisitUuid())).thenReturn(visit); + when(bahmniVisitService.getParentLocationNameForVisit(visit.getLocation())).thenReturn(visit.getLocation().getName()); + when(bahmniDrugOrderService.getMergedDrugOrderMap(new ArrayList())).thenReturn(null); + when(bahmniDrugOrderService.getAllProviderAsString(new ArrayList())).thenReturn("Superman"); + when(bahmniDrugOrderService.getPrescriptionAsString(null, new Locale("en"))).thenReturn("1. Paracetamol 150 mg/ml, 50 ml, Immediately-1 Days, start from 31-01-2023"); + when(smsService.getPrescriptionMessage(prescriptionSMS.getLocale(), visit.getStartDatetime(), visit.getPatient(), + "Bahmni", "Superman", "1. Paracetamol 150 mg/ml, 50 ml, Immediately-1 Days, start from 31-01-2023")) + .thenReturn(sampleSMSContent); + sharePrescriptionService.sendPresciptionSMS(prescriptionSMS); + verify(smsService, times(1)).sendSMS("+919999999999", sampleSMSContent); + } + + private Visit createVisitForTest() throws Exception { + Date visitDate = new SimpleDateFormat("MMMM d, yyyy", new Locale("en")).parse("January 30, 2023"); + Date birthDate = new SimpleDateFormat("MMMM d, yyyy", new Locale("en")).parse("January 30, 2010"); + Person person = new PersonBuilder().withUUID("puuid").withPersonName("testPersonName").build(); + person.setGender("M"); + person.setBirthdate(birthDate); + PersonAttribute pa = new PersonAttribute(); + pa.setValue("+919999999999"); + PersonAttributeType pat = new PersonAttributeType(); + pat.setName("phoneNumber"); + pa.setAttributeType(pat); + Set paSet = new HashSet<>(); + paSet.add(pa); + person.setAttributes(paSet); + + Visit visit = new VisitBuilder().withPerson(person).withUUID("visit-uuid").withStartDatetime(visitDate).build(); + Location location = new Location(); + location.setName("Bahmni"); + visit.setLocation(location); + + return visit; + } + +} diff --git a/bahmnicore-omod/src/main/java/org/bahmni/module/bahmnicore/web/v1_0/controller/BahmniDrugOrderController.java b/bahmnicore-omod/src/main/java/org/bahmni/module/bahmnicore/web/v1_0/controller/BahmniDrugOrderController.java index 6ab8d47225..5cd8fd8af1 100644 --- a/bahmnicore-omod/src/main/java/org/bahmni/module/bahmnicore/web/v1_0/controller/BahmniDrugOrderController.java +++ b/bahmnicore-omod/src/main/java/org/bahmni/module/bahmnicore/web/v1_0/controller/BahmniDrugOrderController.java @@ -6,12 +6,10 @@ import org.bahmni.module.bahmnicore.contract.SMS.PrescriptionSMS; import org.bahmni.module.bahmnicore.service.BahmniDrugOrderService; import org.bahmni.module.bahmnicore.service.BahmniObsService; -import org.bahmni.module.bahmnicore.service.BahmniVisitService; -import org.bahmni.module.bahmnicore.service.SMSService; +import org.bahmni.module.bahmnicore.service.SharePrescriptionService; import org.bahmni.module.bahmnicore.util.BahmniDateUtil; import org.openmrs.Concept; import org.openmrs.DrugOrder; -import org.openmrs.Visit; import org.openmrs.api.ConceptService; import org.openmrs.module.bahmniemrapi.drugorder.contract.BahmniDrugOrder; import org.openmrs.module.bahmniemrapi.drugorder.contract.BahmniOrderAttribute; @@ -40,7 +38,6 @@ import java.util.List; import java.util.Map; import java.util.Set; -import java.util.stream.Collectors; @Controller public class BahmniDrugOrderController extends BaseRestController { @@ -55,21 +52,17 @@ public class BahmniDrugOrderController extends BaseRestController { @Autowired private ConceptService conceptService; + @Autowired + private SharePrescriptionService sharePrescriptionServiceService; + private static Logger logger = LogManager.getLogger(BahmniDrugOrderController.class); private BahmniDrugOrderMapper bahmniDrugOrderMapper; - @Autowired - private BahmniVisitService bahmniVisitService; - - @Autowired - private SMSService smsService; - - public BahmniDrugOrderController(BahmniDrugOrderService drugOrderService, BahmniVisitService bahmniVisitService, SMSService smsService) { + public BahmniDrugOrderController(BahmniDrugOrderService drugOrderService, SharePrescriptionService sharePrescriptionServiceService) { this.drugOrderService = drugOrderService; this.bahmniDrugOrderMapper = new BahmniDrugOrderMapper(); - this.bahmniVisitService = bahmniVisitService; - this.smsService = smsService; + this.sharePrescriptionServiceService = sharePrescriptionServiceService; } public BahmniDrugOrderController() { this.bahmniDrugOrderMapper = new BahmniDrugOrderMapper(); @@ -146,14 +139,7 @@ public List getDrugOrderDetails(@RequestParam(value = "patientU @RequestMapping(value = baseUrl+ "/sendPrescriptionSMS", method = RequestMethod.POST) @ResponseBody public Object sendPrescriptionSMS(@RequestBody PrescriptionSMS prescription) throws Exception { - Visit visit = bahmniVisitService.getVisitSummary(prescription.getVisitUuid()); - String locationName = bahmniVisitService.getParentLocationNameForVisit(visit.getLocation()); - List drugOrderList = getSortedBahmniDrugOrdersForVisit(visit.getPatient().getUuid(), visit.getUuid()); - Map mergedDrugOrderMap = drugOrderService.getMergedDrugOrderMap(drugOrderList); - String providerString = drugOrderService.getAllProviderAsString(drugOrderList); - String prescriptionString = drugOrderService.getPrescriptionAsString(mergedDrugOrderMap); - String prescriptionSMSContent = smsService.getPrescriptionMessage(prescription.getLocale(), visit.getStartDatetime(), visit.getPatient(), locationName, providerString, prescriptionString); - return smsService.sendSMS(visit.getPatient().getAttribute("phoneNumber").getValue(), prescriptionSMSContent); + return sharePrescriptionServiceService.sendPresciptionSMS(prescription); } Set getDrugConcepts(String drugConceptSetName){ @@ -242,14 +228,4 @@ private Map> groupDrugOrdersAccordingToOrderS return groupedDrugOrders; } - private List getSortedBahmniDrugOrdersForVisit(String patientUuid, String visitUuid) { - List visitUuidList = new ArrayList<>(); - visitUuidList.add(visitUuid); - List drugOrderList = getPrescribedOrders(visitUuidList, patientUuid, - null,null, null, null, null); - List sortedDrugOrderList = drugOrderList.stream() - .sorted(Comparator.comparing(BahmniDrugOrder::getEffectiveStartDate)) - .collect(Collectors.toList()); - return sortedDrugOrderList; - } } diff --git a/bahmnicore-omod/src/main/resources/config.xml b/bahmnicore-omod/src/main/resources/config.xml index 1a6d99757e..c2b58e49cc 100644 --- a/bahmnicore-omod/src/main/resources/config.xml +++ b/bahmnicore-omod/src/main/resources/config.xml @@ -166,4 +166,10 @@ A list of UUIDs indicating extra Patient Identifier Types that should be displayed + + bahmni.prescriptionSMSTemplate + Date: {0}\nPrescription For Patient: {1}, {2}, {3} years.\nDoctor: {4} ({5})\n{6} + Template to use while sending prescription in sms for a visit. Format is define like in Java MessageFormat + + diff --git a/bahmnicore-omod/src/main/resources/messages.properties b/bahmnicore-omod/src/main/resources/messages.properties index cd2616a57a..89fb8eb609 100644 --- a/bahmnicore-omod/src/main/resources/messages.properties +++ b/bahmnicore-omod/src/main/resources/messages.properties @@ -1 +1,5 @@ -@MODULE_ID@.title=Bahmni Core \ No newline at end of file +@MODULE_ID@.title=Bahmni Core + +bahmni.prescriptionSMSTemplate=Date: {0}\nPrescription For Patient: {1}, {2}, {3} years.\nDoctor: {4} ({5})\n{6} +bahmni.sms.timezone=IST +bahmni.sms.url=http://otp/notification/sms \ No newline at end of file diff --git a/bahmnicore-omod/src/test/java/org/bahmni/module/bahmnicore/web/v1_0/controller/BahmniDrugOrderControllerTest.java b/bahmnicore-omod/src/test/java/org/bahmni/module/bahmnicore/web/v1_0/controller/BahmniDrugOrderControllerTest.java index 52742ec2f5..8c60e1aaf1 100644 --- a/bahmnicore-omod/src/test/java/org/bahmni/module/bahmnicore/web/v1_0/controller/BahmniDrugOrderControllerTest.java +++ b/bahmnicore-omod/src/test/java/org/bahmni/module/bahmnicore/web/v1_0/controller/BahmniDrugOrderControllerTest.java @@ -1,36 +1,17 @@ package org.bahmni.module.bahmnicore.web.v1_0.controller; import org.bahmni.module.bahmnicore.contract.SMS.PrescriptionSMS; -import org.bahmni.module.bahmnicore.properties.SMSProperties; import org.bahmni.module.bahmnicore.service.BahmniDrugOrderService; -import org.bahmni.module.bahmnicore.service.BahmniObsService; -import org.bahmni.module.bahmnicore.service.BahmniVisitService; -import org.bahmni.module.bahmnicore.service.SMSService; -import org.bahmni.test.builder.PersonBuilder; -import org.bahmni.test.builder.VisitBuilder; +import org.bahmni.module.bahmnicore.service.SharePrescriptionService; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; -import org.openmrs.Visit; -import org.openmrs.Person; -import org.openmrs.Location; -import org.openmrs.PersonAttribute; -import org.openmrs.PersonAttributeType; import org.openmrs.api.ConceptService; -import org.openmrs.module.bahmniemrapi.drugorder.contract.BahmniDrugOrder; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; -import java.text.SimpleDateFormat; import java.util.Set; -import java.util.ArrayList; -import java.util.Date; -import java.util.Locale; -import java.util.HashSet; import static org.junit.Assert.assertNull; import static org.mockito.Mockito.when; @@ -38,8 +19,7 @@ import static org.mockito.Mockito.verify; import static org.mockito.MockitoAnnotations.initMocks; -@RunWith(PowerMockRunner.class) -@PrepareForTest({SMSProperties.class}) +@RunWith(MockitoJUnitRunner.class) public class BahmniDrugOrderControllerTest { @Mock @@ -47,11 +27,7 @@ public class BahmniDrugOrderControllerTest { @Mock BahmniDrugOrderService bahmniDrugOrderService; @Mock - BahmniVisitService bahmniVisitService; - @Mock - SMSService smsService; - @Mock - BahmniObsService bahmniObsService; + SharePrescriptionService sharePrescriptionService; @InjectMocks BahmniDrugOrderController bahmniDrugOrderController; @@ -76,53 +52,11 @@ public void shouldReturnNullIfDrugConceptNameIsNull() { } @Test - public void shouldReturnErrorResponseForSendingPrescriptionSMS() throws Exception { + public void shouldCallSendPrescriptionSMSServiceMethod() throws Exception { PrescriptionSMS prescriptionSMS = new PrescriptionSMS(); prescriptionSMS.setVisitUuid("visit-uuid"); prescriptionSMS.setLocale("en"); - Visit visit = createVisitForTest(); - String sampleSMSContent = "Date: 30-01-2023\n" + - "Prescription For Patient: testPersonName null, M, 13 years.\n" + - "Doctor: Superman (Bahmni)\n" + - "1. Paracetamol 150 mg/ml, 50 ml, Immediately-1 Days, start from 31-01-2023"; - - PowerMockito.mockStatic(SMSProperties.class); - when(SMSProperties.getProperty("sms.en.prescriptionSMS")).thenReturn("Date: #visitDate\nPrescription For Patient: #patientName, #gender, #age years.\nDoctor: #doctorDetail (#location)\n#prescriptionDetails"); - when(SMSProperties.getProperty("sms.timeZone")).thenReturn("IST"); - when(SMSProperties.getProperty("sms.url")).thenReturn("dummyurl"); - when(bahmniVisitService.getVisitSummary(prescriptionSMS.getVisitUuid())).thenReturn(visit); - when(bahmniVisitService.getParentLocationNameForVisit(visit.getLocation())).thenReturn(visit.getLocation().getName()); - when(bahmniDrugOrderService.getMergedDrugOrderMap(new ArrayList())).thenReturn(null); - when(bahmniDrugOrderService.getAllProviderAsString(new ArrayList())).thenReturn("Superman"); - when(bahmniDrugOrderService.getPrescriptionAsString(null)).thenReturn("1. Paracetamol 150 mg/ml, 50 ml, Immediately-1 Days, start from 31-01-2023"); - when(smsService.getPrescriptionMessage(prescriptionSMS.getLocale(), visit.getStartDatetime(), visit.getPatient(), - "Bahmni", "Superman", "1. Paracetamol 150 mg/ml, 50 ml, Immediately-1 Days, start from 31-01-2023")) - .thenReturn(sampleSMSContent); bahmniDrugOrderController.sendPrescriptionSMS(prescriptionSMS); - - verify(smsService, times(1)).sendSMS("+919999999999", sampleSMSContent); - } - - private Visit createVisitForTest() throws Exception { - Date visitDate = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH).parse("January 30, 2023"); - Date birthDate = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH).parse("January 30, 2010"); - Person person = new PersonBuilder().withUUID("puuid").withPersonName("testPersonName").build(); - person.setGender("M"); - person.setBirthdate(birthDate); - PersonAttribute pa = new PersonAttribute(); - pa.setValue("+919999999999"); - PersonAttributeType pat = new PersonAttributeType(); - pat.setName("phoneNumber"); - pa.setAttributeType(pat); - Set paSet = new HashSet<>(); - paSet.add(pa); - person.setAttributes(paSet); - - Visit visit = new VisitBuilder().withPerson(person).withUUID("visit-uuid").withStartDatetime(visitDate).build(); - Location location = new Location(); - location.setName("Bahmni"); - visit.setLocation(location); - - return visit; + verify(sharePrescriptionService, times(1)).sendPresciptionSMS(prescriptionSMS); } } \ No newline at end of file From 9ec8bc7a729951d395e9652a3cc9d9fb147e1747 Mon Sep 17 00:00:00 2001 From: Kavitha S Date: Mon, 6 Feb 2023 21:13:42 +0530 Subject: [PATCH 04/10] BAH-2763 | refactor to add line separator in message --- .../bahmnicore/properties/SMSProperties.java | 37 ------------------- .../service/impl/SMSServiceImpl.java | 4 +- .../controller/BahmniDrugOrderController.java | 1 - 3 files changed, 2 insertions(+), 40 deletions(-) delete mode 100644 bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/properties/SMSProperties.java diff --git a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/properties/SMSProperties.java b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/properties/SMSProperties.java deleted file mode 100644 index 8c2a259d70..0000000000 --- a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/properties/SMSProperties.java +++ /dev/null @@ -1,37 +0,0 @@ -package org.bahmni.module.bahmnicore.properties; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.openmrs.util.OpenmrsUtil; - -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.util.Properties; - -public class SMSProperties { - private static Properties properties; - private static Log log = LogFactory.getLog(SMSProperties.class); - - public static void load() { - String propertyFile = new File(OpenmrsUtil.getApplicationDataDirectory(), "sms.properties").getAbsolutePath(); - log.info(String.format("Reading sms properties from : %s", propertyFile)); - try { - properties = new Properties(System.getProperties()); - properties.load(new FileInputStream(propertyFile)); - } catch (IOException e) { - throw new RuntimeException(e); - } - } - - public static String getProperty(String key){ - if(properties == null){ - load(); - } - return properties.getProperty(key); - } - - public static void initalize(Properties props) { - properties = props; - } -} diff --git a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/SMSServiceImpl.java b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/SMSServiceImpl.java index 1598996952..f737993bdd 100644 --- a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/SMSServiceImpl.java +++ b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/SMSServiceImpl.java @@ -68,9 +68,9 @@ public String getPrescriptionMessage(String lang, Date visitDate, Patient patien patient.getGivenName() + " " + patient.getFamilyName(), patient.getGender(), patient.getAge().toString(), providerDetail, location, prescriptionDetail}; if (StringUtils.isBlank(smsTemplate)) { - return Context.getMessageSourceService().getMessage(PRESCRIPTION_SMS_TEMPLATE, arguments, new Locale(lang)); + return Context.getMessageSourceService().getMessage(PRESCRIPTION_SMS_TEMPLATE, arguments, new Locale(lang)).replace("\\n", System.lineSeparator()); } else { - return new MessageFormat(smsTemplate).format(arguments); + return new MessageFormat(smsTemplate).format(arguments).replace("\\n", System.lineSeparator()); } } diff --git a/bahmnicore-omod/src/main/java/org/bahmni/module/bahmnicore/web/v1_0/controller/BahmniDrugOrderController.java b/bahmnicore-omod/src/main/java/org/bahmni/module/bahmnicore/web/v1_0/controller/BahmniDrugOrderController.java index 5cd8fd8af1..def8ae4149 100644 --- a/bahmnicore-omod/src/main/java/org/bahmni/module/bahmnicore/web/v1_0/controller/BahmniDrugOrderController.java +++ b/bahmnicore-omod/src/main/java/org/bahmni/module/bahmnicore/web/v1_0/controller/BahmniDrugOrderController.java @@ -227,5 +227,4 @@ private Map> groupDrugOrdersAccordingToOrderS return groupedDrugOrders; } - } From 3c567a9d0548a234aec229b102ab1c1c5e94c8fa Mon Sep 17 00:00:00 2001 From: Kavitha S Date: Wed, 8 Feb 2023 13:17:53 +0530 Subject: [PATCH 05/10] BAH-2763 | refactor on review comments --- bahmnicore-api/pom.xml | 1 + .../service/BahmniDrugOrderService.java | 6 +- .../service/SharePrescriptionService.java | 4 +- .../impl/BahmniDrugOrderServiceImpl.java | 82 ++++++++++--------- .../service/impl/SMSServiceImpl.java | 12 ++- .../impl/SharePrescriptionServiceImpl.java | 8 +- .../impl/BahmniDrugOrderServiceImplTest.java | 12 +-- bahmnicore-omod/src/main/resources/config.xml | 15 +++- 8 files changed, 81 insertions(+), 59 deletions(-) diff --git a/bahmnicore-api/pom.xml b/bahmnicore-api/pom.xml index b2a83be767..fdcc273bd3 100644 --- a/bahmnicore-api/pom.xml +++ b/bahmnicore-api/pom.xml @@ -209,6 +209,7 @@ org.apache.httpcomponents httpclient 4.5.14 + provided diff --git a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/BahmniDrugOrderService.java b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/BahmniDrugOrderService.java index 309b059266..9a7c5c4d76 100644 --- a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/BahmniDrugOrderService.java +++ b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/BahmniDrugOrderService.java @@ -40,11 +40,11 @@ List getInactiveDrugOrders(String patientUuid, Set concepts, List getDrugOrders(String patientUuid, Boolean isActive, Set conceptsToFilter, Set conceptsToExclude, String patientProgramUuid) throws ParseException; - List getSortedBahmniDrugOrdersForVisit(String patientUuid, String visitUuid); + List getBahmniDrugOrdersForVisit(String patientUuid, String visitUuid); - Map getMergedDrugOrderMap(List drugOrderList); + Map getMergedDrugOrderMap(List drugOrderList); - String getPrescriptionAsString(Map drugOrderDurationMap, Locale locale); + String getPrescriptionAsString(Map drugOrderDurationMap, Locale locale); String getAllProviderAsString(List drugOrders); diff --git a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/SharePrescriptionService.java b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/SharePrescriptionService.java index 2f8c3c10c9..cd4801a1b2 100644 --- a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/SharePrescriptionService.java +++ b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/SharePrescriptionService.java @@ -2,8 +2,10 @@ import org.bahmni.module.bahmnicore.contract.SMS.PrescriptionSMS; import org.openmrs.annotation.Authorized; +import org.springframework.transaction.annotation.Transactional; public interface SharePrescriptionService { - @Authorized({"app:clinical"}) + @Transactional(readOnly = true) + @Authorized({"Send Prescription SMS"}) Object sendPresciptionSMS(PrescriptionSMS prescription); } diff --git a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/BahmniDrugOrderServiceImpl.java b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/BahmniDrugOrderServiceImpl.java index cc3db8a9eb..7924ae3fd7 100644 --- a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/BahmniDrugOrderServiceImpl.java +++ b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/BahmniDrugOrderServiceImpl.java @@ -179,51 +179,54 @@ public List getAllDrugOrders(String patientUuid, String patientProgramUui } @Override - public List getSortedBahmniDrugOrdersForVisit(String patientUuid, String visitUuid) { - List drugOrderList = getPrescribedDrugOrders(Arrays.asList(visitUuid), patientUuid, null,null, null, null, null); - List bahmniDrugOrderList = getBahmniDrugOrdersForVisit(patientUuid, drugOrderList); - Collections.sort(bahmniDrugOrderList, new Comparator() { - @Override - public int compare(BahmniDrugOrder o1, BahmniDrugOrder o2) { - return o1.getEffectiveStartDate().compareTo(o2.getEffectiveStartDate()); - } - }); - return bahmniDrugOrderList; + public List getBahmniDrugOrdersForVisit(String patientUuid, String visitUuid) { + try { + List drugOrderList = getPrescribedDrugOrders(Arrays.asList(visitUuid), patientUuid, null,null, null, null, null); + Map drugOrderMap = getDiscontinuedDrugOrders(drugOrderList); + Collection orderAttributeObs = bahmniObsService.observationsFor(patientUuid, getOrdAttributeConcepts(), null, null, false, null, null, null); + List bahmniDrugOrderList = bahmniDrugOrderMapper.mapToResponse(drugOrderList, orderAttributeObs, drugOrderMap , null); + Collections.sort(bahmniDrugOrderList, new Comparator() { + @Override + public int compare(BahmniDrugOrder o1, BahmniDrugOrder o2) { + return o1.getEffectiveStartDate().compareTo(o2.getEffectiveStartDate()); + } + }); + return bahmniDrugOrderList; + } catch (IOException e) { + logger.error("Could not parse drug order", e); + throw new RuntimeException("Could not parse drug order", e); + } } @Override - public Map getMergedDrugOrderMap(List drugOrderList) { - Map mergedDrugOrderMap = new LinkedHashMap<>(); + public Map getMergedDrugOrderMap(List drugOrderList) { + Map mergedDrugOrderMap = new LinkedHashMap<>(); for(BahmniDrugOrder drugOrder : drugOrderList) { BahmniDrugOrder foundDrugOrder = mergedDrugOrderMap.entrySet().stream() .map(x -> x.getKey()) .filter( existingOrder -> - areValuesEqual(existingOrder.getDrugNonCoded(), drugOrder.getDrugNonCoded()) && - (existingOrder.getDrug()!=null && drugOrder.getDrug()!=null && - areValuesEqual(existingOrder.getDrug().getUuid(), drugOrder.getDrug().getUuid())) && - areValuesEqual(existingOrder.getInstructions(), drugOrder.getInstructions()) && - compareDosingInstruction(existingOrder.getDosingInstructions(), drugOrder.getDosingInstructions()) && - areValuesEqual(existingOrder.getDosingInstructions().getRoute(), drugOrder.getDosingInstructions().getRoute()) && - areValuesEqual(existingOrder.getDosingInstructions().getAdministrationInstructions(), drugOrder.getDosingInstructions().getAdministrationInstructions()) && - areValuesEqual(existingOrder.getDosingInstructions().getAsNeeded(), drugOrder.getDosingInstructions().getAsNeeded()) && - areValuesEqual(existingOrder.getDateStopped(), drugOrder.getDateStopped()) && - getDateDifferenceInDays(existingOrder.getEffectiveStopDate(), drugOrder.getEffectiveStartDate()) <= 1.0 ) + compareDrugOrders(existingOrder, drugOrder) ) .findFirst() .orElse(null); if (foundDrugOrder!=null) { - mergedDrugOrderMap.put(foundDrugOrder, mergedDrugOrderMap.get(foundDrugOrder)+drugOrder.getDuration()); + String durationWithUnits = mergedDrugOrderMap.get(foundDrugOrder); + if(drugOrder.getDurationUnits() == foundDrugOrder.getDurationUnits()) + durationWithUnits = (foundDrugOrder.getDuration()+drugOrder.getDuration()) + " " + drugOrder.getDurationUnits(); + else + durationWithUnits += " + " + drugOrder.getDuration() + " " + drugOrder.getDurationUnits(); + mergedDrugOrderMap.put(foundDrugOrder, durationWithUnits); } else { - mergedDrugOrderMap.put(drugOrder, drugOrder.getDuration()); + mergedDrugOrderMap.put(drugOrder, drugOrder.getDuration()+" "+drugOrder.getDurationUnits()); } } return mergedDrugOrderMap; } @Override - public String getPrescriptionAsString(Map drugOrderDurationMap, Locale locale) { + public String getPrescriptionAsString(Map drugOrderDurationMap, Locale locale) { String prescriptionString = ""; int counter = 1; - for (Map.Entry entry : drugOrderDurationMap.entrySet()) { + for (Map.Entry entry : drugOrderDurationMap.entrySet()) { prescriptionString += counter++ + ". " + getDrugOrderString(entry.getKey(), drugOrderDurationMap.get(entry.getKey()), locale) + "\n"; } return prescriptionString; @@ -238,10 +241,10 @@ public String getAllProviderAsString(List drugOrders) { return StringUtils.collectionToCommaDelimitedString(providerSet); } - private String getDrugOrderString(BahmniDrugOrder drugOrder, Integer duration, Locale locale) { + private String getDrugOrderString(BahmniDrugOrder drugOrder, String duration, Locale locale) { String drugOrderString = drugOrder.getDrug().getName(); drugOrderString += ", " + (drugOrder.getDosingInstructions().getDose().intValue()) + " " + drugOrder.getDosingInstructions().getDoseUnits(); - drugOrderString += ", " + drugOrder.getDosingInstructions().getFrequency() + "-" + duration.toString() + " " + drugOrder.getDurationUnits(); + drugOrderString += ", " + drugOrder.getDosingInstructions().getFrequency() + "-" + duration; drugOrderString += ", start from " + convertUTCToGivenFormat(drugOrder.getEffectiveStartDate(), "dd-MM-yyyy", Context.getMessageSourceService().getMessage(SMS_TIMEZONE, null, locale)); if(drugOrder.getDateStopped() != null) drugOrderString += ", stopped on " + convertUTCToGivenFormat(drugOrder.getDateStopped(), "dd-MM-yyyy", Context.getMessageSourceService().getMessage(SMS_TIMEZONE, null, locale)); @@ -271,17 +274,6 @@ private Collection getOrdAttributeConcepts() { return orderAttribute == null ? Collections.EMPTY_LIST : orderAttribute.getSetMembers(); } - private List getBahmniDrugOrdersForVisit(String patientUuid, List drugOrders) { - Map drugOrderMap = getDiscontinuedDrugOrders(drugOrders); - try { - Collection orderAttributeObs = bahmniObsService.observationsFor(patientUuid, getOrdAttributeConcepts(), null, null, false, null, null, null); - return bahmniDrugOrderMapper.mapToResponse(drugOrders, orderAttributeObs, drugOrderMap , null); - } catch (IOException e) { - logger.error("Could not parse drug order", e); - throw new RuntimeException("Could not parse drug order", e); - } - } - private List fetchOrderAttributeConcepts() { Concept orderAttributesConceptSet = conceptService.getConceptByName(BahmniOrderAttribute.ORDER_ATTRIBUTES_CONCEPT_SET_NAME); if(orderAttributesConceptSet != null){ @@ -336,4 +328,16 @@ private List mapOrderToDrugOrder(List orders){ return drugOrders; } + private boolean compareDrugOrders(BahmniDrugOrder existingOrder, BahmniDrugOrder newDrugOrder) { + return (existingOrder.getDrug()!=null && newDrugOrder.getDrug()!=null && + areValuesEqual(existingOrder.getDrug().getUuid(), newDrugOrder.getDrug().getUuid())) && + areValuesEqual(existingOrder.getDrugNonCoded(), newDrugOrder.getDrugNonCoded()) && + areValuesEqual(existingOrder.getInstructions(), newDrugOrder.getInstructions()) && + compareDosingInstruction(existingOrder.getDosingInstructions(), newDrugOrder.getDosingInstructions()) && + areValuesEqual(existingOrder.getDosingInstructions().getRoute(), newDrugOrder.getDosingInstructions().getRoute()) && + areValuesEqual(existingOrder.getDosingInstructions().getAdministrationInstructions(), newDrugOrder.getDosingInstructions().getAdministrationInstructions()) && + areValuesEqual(existingOrder.getDosingInstructions().getAsNeeded(), newDrugOrder.getDosingInstructions().getAsNeeded()) && + areValuesEqual(existingOrder.getDateStopped(), newDrugOrder.getDateStopped()) && + getDateDifferenceInDays(existingOrder.getEffectiveStopDate(), newDrugOrder.getEffectiveStartDate()) <= 1.0 ; + } } diff --git a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/SMSServiceImpl.java b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/SMSServiceImpl.java index f737993bdd..78e99a2ee9 100644 --- a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/SMSServiceImpl.java +++ b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/SMSServiceImpl.java @@ -1,6 +1,9 @@ package org.bahmni.module.bahmnicore.service.impl; import org.apache.commons.lang.StringUtils; +import org.apache.http.HttpResponse; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -10,7 +13,6 @@ import org.openmrs.Patient; import org.openmrs.api.context.Context; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.text.MessageFormat; @@ -20,11 +22,8 @@ import static org.bahmni.module.bahmnicore.util.BahmniDateUtil.convertUTCToGivenFormat; import com.fasterxml.jackson.databind.ObjectMapper; -import org.apache.http.HttpResponse; -import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; -import org.apache.http.impl.client.HttpClientBuilder; @Service public class SMSServiceImpl implements SMSService { @@ -33,7 +32,6 @@ public class SMSServiceImpl implements SMSService { private final static String SMS_TIMEZONE = "bahmni.sms.timezone"; private final static String SMS_URL = "bahmni.sms.url"; - @Autowired public SMSServiceImpl() {} @Override @@ -50,9 +48,9 @@ public Object sendSMS(String phoneNumber, String message) { HttpPost request = new HttpPost(Context.getMessageSourceService().getMessage(SMS_URL, null, new Locale("en"))); request.addHeader("content-type", "application/json"); request.setEntity(params); - HttpClient httpClient = HttpClientBuilder.create().build(); + CloseableHttpClient httpClient = HttpClients.createDefault(); HttpResponse response = httpClient.execute(request); - + httpClient.close(); return response.getStatusLine(); } catch (Exception e) { logger.error("Exception occured in sending sms ", e); diff --git a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/SharePrescriptionServiceImpl.java b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/SharePrescriptionServiceImpl.java index 490698028f..9a6ae83968 100644 --- a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/SharePrescriptionServiceImpl.java +++ b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/SharePrescriptionServiceImpl.java @@ -8,9 +8,11 @@ import org.bahmni.module.bahmnicore.service.SMSService; import org.bahmni.module.bahmnicore.service.SharePrescriptionService; import org.openmrs.Visit; +import org.openmrs.annotation.Authorized; import org.openmrs.module.bahmniemrapi.drugorder.contract.BahmniDrugOrder; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import java.util.List; import java.util.Locale; @@ -31,11 +33,13 @@ public SharePrescriptionServiceImpl(BahmniVisitService bahmniVisitService, Bahmn } @Override + @Transactional(readOnly = true) + @Authorized({"Send Prescription"}) public Object sendPresciptionSMS(PrescriptionSMS prescription) { Visit visit = bahmniVisitService.getVisitSummary(prescription.getVisitUuid()); String locationName = bahmniVisitService.getParentLocationNameForVisit(visit.getLocation()); - List drugOrderList = drugOrderService.getSortedBahmniDrugOrdersForVisit(visit.getPatient().getUuid(), visit.getUuid()); - Map mergedDrugOrderMap = drugOrderService.getMergedDrugOrderMap(drugOrderList); + List drugOrderList = drugOrderService.getBahmniDrugOrdersForVisit(visit.getPatient().getUuid(), visit.getUuid()); + Map mergedDrugOrderMap = drugOrderService.getMergedDrugOrderMap(drugOrderList); String providerString = drugOrderService.getAllProviderAsString(drugOrderList); String prescriptionString = drugOrderService.getPrescriptionAsString(mergedDrugOrderMap, new Locale(prescription.getLocale())); String prescriptionSMSContent = smsService.getPrescriptionMessage(prescription.getLocale(), visit.getStartDatetime(), visit.getPatient(), locationName, providerString, prescriptionString); diff --git a/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/service/impl/BahmniDrugOrderServiceImplTest.java b/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/service/impl/BahmniDrugOrderServiceImplTest.java index 46b29d1041..cefabc97ed 100644 --- a/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/service/impl/BahmniDrugOrderServiceImplTest.java +++ b/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/service/impl/BahmniDrugOrderServiceImplTest.java @@ -134,10 +134,10 @@ public void shouldNotConsiderEncountersToFetchDrugOrdersIfPatientProgramUuidIsNu @Test public void shouldReturnMergedDrugOrderAsMap() throws Exception { List bahmniDrugOrderList = buildBahmniDrugOrderList(); - Map mergedDrugOrderMap = bahmniDrugOrderService.getMergedDrugOrderMap(bahmniDrugOrderList); - Map expectedMergedDrugOrderMap = new LinkedHashMap<>(); - expectedMergedDrugOrderMap.put(bahmniDrugOrderList.get(0), 10); - expectedMergedDrugOrderMap.put(bahmniDrugOrderList.get(2), 3); + Map mergedDrugOrderMap = bahmniDrugOrderService.getMergedDrugOrderMap(bahmniDrugOrderList); + Map expectedMergedDrugOrderMap = new LinkedHashMap<>(); + expectedMergedDrugOrderMap.put(bahmniDrugOrderList.get(0), "10 Days"); + expectedMergedDrugOrderMap.put(bahmniDrugOrderList.get(2), "3 Days"); assertEquals(expectedMergedDrugOrderMap, mergedDrugOrderMap); } @@ -149,8 +149,8 @@ public void shouldReturnPrescriptionAsString() throws Exception { Date drugOrderStartDate = new SimpleDateFormat("MMMM d, yyyy", new Locale("en")).parse("January 30, 2023"); EncounterTransaction.DrugOrder etDrugOrder = createETDrugOrder("1", "Paracetamol", 2.0, "Once a day", drugOrderStartDate, 5); BahmniDrugOrder bahmniDrugOrder = createBahmniDrugOrder(null, etDrugOrder); - Map drugOrderDurationMap = new LinkedHashMap<>(); - drugOrderDurationMap.put(bahmniDrugOrder, 10); + Map drugOrderDurationMap = new LinkedHashMap<>(); + drugOrderDurationMap.put(bahmniDrugOrder, "10 Days"); String prescriptionString = bahmniDrugOrderService.getPrescriptionAsString(drugOrderDurationMap, new Locale("en")); String expectedPrescriptionString = "1. Paracetamol, 2 tab (s), Once a day-10 Days, start from 30-01-2023\n"; assertEquals(expectedPrescriptionString, prescriptionString); diff --git a/bahmnicore-omod/src/main/resources/config.xml b/bahmnicore-omod/src/main/resources/config.xml index c2b58e49cc..c05e2694ed 100644 --- a/bahmnicore-omod/src/main/resources/config.xml +++ b/bahmnicore-omod/src/main/resources/config.xml @@ -97,6 +97,10 @@ app:lab-lite Will give access to Lab Lite app + + Send Prescription + Will give access to send prescription via sms + org.openmrs.module.bahmniemrapi.encountertransaction.service.BahmniEncounterTransactionService @@ -165,7 +169,16 @@ A list of UUIDs indicating extra Patient Identifier Types that should be displayed - + + bahmni.enableSMSPrescriptionOption + false + Boolean to enable sending sms with prescription details + + + bahmni.enableEmailPrescriptionOption + false + Boolean to enable sending email with prescription details + bahmni.prescriptionSMSTemplate Date: {0}\nPrescription For Patient: {1}, {2}, {3} years.\nDoctor: {4} ({5})\n{6} From a3e705e1b82f3849ee852f5f8bbc3b5dcd553502 Mon Sep 17 00:00:00 2001 From: Kavitha S Date: Wed, 8 Feb 2023 17:59:23 +0530 Subject: [PATCH 06/10] BAH-2763 | refactoring and renaming methods and configurable formats --- .../service/BahmniDrugOrderService.java | 2 +- .../service/BahmniVisitService.java | 2 +- .../impl/BahmniDrugOrderServiceImpl.java | 20 ++++++++++--------- .../service/impl/BahmniVisitServiceImpl.java | 6 +++--- .../service/impl/SMSServiceImpl.java | 4 +++- .../impl/SharePrescriptionServiceImpl.java | 5 +++-- .../impl/BahmniDrugOrderServiceImplTest.java | 6 ++++-- .../service/impl/SMSServiceImplIT.java | 2 ++ .../impl/SharePrescriptionServiceImplIT.java | 13 ++++++------ .../src/main/resources/messages.properties | 3 ++- 10 files changed, 36 insertions(+), 27 deletions(-) diff --git a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/BahmniDrugOrderService.java b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/BahmniDrugOrderService.java index 9a7c5c4d76..2d97102a21 100644 --- a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/BahmniDrugOrderService.java +++ b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/BahmniDrugOrderService.java @@ -46,6 +46,6 @@ List getDrugOrders(String patientUuid, Boolean isActive, Set drugOrderDurationMap, Locale locale); - String getAllProviderAsString(List drugOrders); + Set getUniqueProviderNames(List drugOrders); } diff --git a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/BahmniVisitService.java b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/BahmniVisitService.java index 2a0ac89bd1..c5527152a1 100644 --- a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/BahmniVisitService.java +++ b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/BahmniVisitService.java @@ -13,5 +13,5 @@ public interface BahmniVisitService { List getAdmitAndDischargeEncounters(Integer visitId); - String getParentLocationNameForVisit(Location location); + Location getParentLocationForVisit(Location location); } diff --git a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/BahmniDrugOrderServiceImpl.java b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/BahmniDrugOrderServiceImpl.java index 7924ae3fd7..a76837054d 100644 --- a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/BahmniDrugOrderServiceImpl.java +++ b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/BahmniDrugOrderServiceImpl.java @@ -32,7 +32,6 @@ import org.openmrs.module.emrapi.utils.HibernateLazyLoader; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; -import org.springframework.util.StringUtils; import java.io.IOException; import java.text.ParseException; @@ -45,6 +44,7 @@ import java.util.LinkedHashMap; import java.util.Set; import java.util.LinkedHashSet; +import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; import java.util.Locale; import java.util.Arrays; @@ -66,6 +66,7 @@ public class BahmniDrugOrderServiceImpl implements BahmniDrugOrderService { private static final String GP_DOSING_INSTRUCTIONS_CONCEPT_UUID = "order.dosingInstructionsConceptUuid"; private static Logger logger = LogManager.getLogger(BahmniDrugOrderService.class); private final static String SMS_TIMEZONE = "bahmni.sms.timezone"; + private final static String SMS_DATEFORMAT = "bahmni.sms.dateformat"; @Autowired public BahmniDrugOrderServiceImpl(ConceptService conceptService, OrderService orderService, PatientService patientService, OrderDao orderDao, @@ -227,27 +228,28 @@ public String getPrescriptionAsString(Map drugOrderDura String prescriptionString = ""; int counter = 1; for (Map.Entry entry : drugOrderDurationMap.entrySet()) { - prescriptionString += counter++ + ". " + getDrugOrderString(entry.getKey(), drugOrderDurationMap.get(entry.getKey()), locale) + "\n"; + prescriptionString += counter++ + ". " + getDrugOrderAsString(entry.getKey(), drugOrderDurationMap.get(entry.getKey()), locale) + "\n"; } return prescriptionString; } - @Override - public String getAllProviderAsString(List drugOrders) { + public Set getUniqueProviderNames(List drugOrders) { Set providerSet = new LinkedHashSet(); for (BahmniDrugOrder drugOrder : drugOrders) { providerSet.add("Dr " + drugOrder.getProvider().getName()); } - return StringUtils.collectionToCommaDelimitedString(providerSet); + return providerSet; } - private String getDrugOrderString(BahmniDrugOrder drugOrder, String duration, Locale locale) { + private String getDrugOrderAsString(BahmniDrugOrder drugOrder, String duration, Locale locale) { String drugOrderString = drugOrder.getDrug().getName(); drugOrderString += ", " + (drugOrder.getDosingInstructions().getDose().intValue()) + " " + drugOrder.getDosingInstructions().getDoseUnits(); drugOrderString += ", " + drugOrder.getDosingInstructions().getFrequency() + "-" + duration; - drugOrderString += ", start from " + convertUTCToGivenFormat(drugOrder.getEffectiveStartDate(), "dd-MM-yyyy", Context.getMessageSourceService().getMessage(SMS_TIMEZONE, null, locale)); + drugOrderString += ", start from " + convertUTCToGivenFormat(drugOrder.getEffectiveStartDate(), + Context.getMessageSourceService().getMessage(SMS_DATEFORMAT, null, locale), Context.getMessageSourceService().getMessage(SMS_TIMEZONE, null, locale)); if(drugOrder.getDateStopped() != null) - drugOrderString += ", stopped on " + convertUTCToGivenFormat(drugOrder.getDateStopped(), "dd-MM-yyyy", Context.getMessageSourceService().getMessage(SMS_TIMEZONE, null, locale)); + drugOrderString += ", stopped on " + convertUTCToGivenFormat(drugOrder.getDateStopped(), + Context.getMessageSourceService().getMessage(SMS_DATEFORMAT, null, locale), Context.getMessageSourceService().getMessage(SMS_TIMEZONE, null, locale)); return drugOrderString; } @@ -266,7 +268,7 @@ private Boolean compareDosingInstruction(EncounterTransaction.DosingInstructions private double getDateDifferenceInDays(Date date1, Date date2){ long diff = date2.getTime() - date1.getTime(); - return (diff / (1000*60*60*24)); + return TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS); } private Collection getOrdAttributeConcepts() { diff --git a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/BahmniVisitServiceImpl.java b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/BahmniVisitServiceImpl.java index a1ffac18d0..1c33d8a05e 100644 --- a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/BahmniVisitServiceImpl.java +++ b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/BahmniVisitServiceImpl.java @@ -36,9 +36,9 @@ public List getAdmitAndDischargeEncounters(Integer visitId) { } @Override - public String getParentLocationNameForVisit(Location location) { + public Location getParentLocationForVisit(Location location) { if(location.getParentLocation()!=null) - getParentLocationNameForVisit(location.getParentLocation()); - return location.getName(); + getParentLocationForVisit(location.getParentLocation()); + return location; } } diff --git a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/SMSServiceImpl.java b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/SMSServiceImpl.java index 78e99a2ee9..e1ff5fba14 100644 --- a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/SMSServiceImpl.java +++ b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/SMSServiceImpl.java @@ -30,6 +30,7 @@ public class SMSServiceImpl implements SMSService { private static Logger logger = LogManager.getLogger(BahmniDrugOrderService.class); private final static String PRESCRIPTION_SMS_TEMPLATE = "bahmni.prescriptionSMSTemplate"; private final static String SMS_TIMEZONE = "bahmni.sms.timezone"; + private final static String SMS_DATEFORMAT = "bahmni.sms.dateformat"; private final static String SMS_URL = "bahmni.sms.url"; public SMSServiceImpl() {} @@ -61,8 +62,9 @@ public Object sendSMS(String phoneNumber, String message) { @Override public String getPrescriptionMessage(String lang, Date visitDate, Patient patient, String location, String providerDetail, String prescriptionDetail) { String smsTimeZone = Context.getMessageSourceService().getMessage(SMS_TIMEZONE, null, new Locale(lang)); + String smsDateFormat = Context.getMessageSourceService().getMessage(SMS_DATEFORMAT, null, new Locale(lang)); String smsTemplate = Context.getAdministrationService().getGlobalProperty(PRESCRIPTION_SMS_TEMPLATE); - Object[] arguments = {convertUTCToGivenFormat(visitDate, "dd-MM-yyyy", smsTimeZone), + Object[] arguments = {convertUTCToGivenFormat(visitDate, smsDateFormat, smsTimeZone), patient.getGivenName() + " " + patient.getFamilyName(), patient.getGender(), patient.getAge().toString(), providerDetail, location, prescriptionDetail}; if (StringUtils.isBlank(smsTemplate)) { diff --git a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/SharePrescriptionServiceImpl.java b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/SharePrescriptionServiceImpl.java index 9a6ae83968..74b8db5723 100644 --- a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/SharePrescriptionServiceImpl.java +++ b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/SharePrescriptionServiceImpl.java @@ -13,6 +13,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import org.springframework.util.StringUtils; import java.util.List; import java.util.Locale; @@ -37,10 +38,10 @@ public SharePrescriptionServiceImpl(BahmniVisitService bahmniVisitService, Bahmn @Authorized({"Send Prescription"}) public Object sendPresciptionSMS(PrescriptionSMS prescription) { Visit visit = bahmniVisitService.getVisitSummary(prescription.getVisitUuid()); - String locationName = bahmniVisitService.getParentLocationNameForVisit(visit.getLocation()); + String locationName = bahmniVisitService.getParentLocationForVisit(visit.getLocation()).getName(); List drugOrderList = drugOrderService.getBahmniDrugOrdersForVisit(visit.getPatient().getUuid(), visit.getUuid()); Map mergedDrugOrderMap = drugOrderService.getMergedDrugOrderMap(drugOrderList); - String providerString = drugOrderService.getAllProviderAsString(drugOrderList); + String providerString = StringUtils.collectionToCommaDelimitedString(drugOrderService.getUniqueProviderNames(drugOrderList)); String prescriptionString = drugOrderService.getPrescriptionAsString(mergedDrugOrderMap, new Locale(prescription.getLocale())); String prescriptionSMSContent = smsService.getPrescriptionMessage(prescription.getLocale(), visit.getStartDatetime(), visit.getPatient(), locationName, providerString, prescriptionString); return smsService.sendSMS(visit.getPatient().getAttribute("phoneNumber").getValue(), prescriptionSMSContent); diff --git a/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/service/impl/BahmniDrugOrderServiceImplTest.java b/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/service/impl/BahmniDrugOrderServiceImplTest.java index cefabc97ed..88e9a6282c 100644 --- a/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/service/impl/BahmniDrugOrderServiceImplTest.java +++ b/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/service/impl/BahmniDrugOrderServiceImplTest.java @@ -23,6 +23,7 @@ import org.openmrs.module.emrapi.encounter.domain.EncounterTransaction; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; +import org.springframework.util.StringUtils; import java.text.ParseException; import java.text.SimpleDateFormat; @@ -146,6 +147,7 @@ public void shouldReturnPrescriptionAsString() throws Exception { mockStatic(Context.class); when(Context.getMessageSourceService()).thenReturn(messageSourceService); when(messageSourceService.getMessage("bahmni.sms.timezone", null, new Locale("en"))).thenReturn("IST"); + when(messageSourceService.getMessage("bahmni.sms.dateformat", null, new Locale("en"))).thenReturn("dd-MM-yyyy"); Date drugOrderStartDate = new SimpleDateFormat("MMMM d, yyyy", new Locale("en")).parse("January 30, 2023"); EncounterTransaction.DrugOrder etDrugOrder = createETDrugOrder("1", "Paracetamol", 2.0, "Once a day", drugOrderStartDate, 5); BahmniDrugOrder bahmniDrugOrder = createBahmniDrugOrder(null, etDrugOrder); @@ -157,9 +159,9 @@ public void shouldReturnPrescriptionAsString() throws Exception { } @Test - public void shouldReturnAllUniqueProvidersAsString() throws Exception { + public void shouldReturnAllUniqueProviderNames() throws Exception { List bahmniDrugOrderList = buildBahmniDrugOrderList(); - String providerString = bahmniDrugOrderService.getAllProviderAsString(bahmniDrugOrderList); + String providerString = StringUtils.collectionToCommaDelimitedString(bahmniDrugOrderService.getUniqueProviderNames(bahmniDrugOrderList)); String expectedProviderString = "Dr Harry,Dr Grace"; assertEquals(expectedProviderString, providerString); } diff --git a/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/service/impl/SMSServiceImplIT.java b/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/service/impl/SMSServiceImplIT.java index ef6f0a4397..4e7617de25 100644 --- a/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/service/impl/SMSServiceImplIT.java +++ b/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/service/impl/SMSServiceImplIT.java @@ -55,6 +55,7 @@ public void setUp() throws Exception { public void shouldReturnPrescriptionSMSWithGlobalPropertyTemplate() throws ParseException { when(administrationService.getGlobalProperty("bahmni.prescriptionSMSTemplate")).thenReturn("Date: {0}\nPrescription For Patient: {1}, {2}, {3} years.\nDoctor: {4} ({5})\n{6}"); when(messageSourceService.getMessage("bahmni.sms.timezone", null, new Locale("en"))).thenReturn("IST"); + when(messageSourceService.getMessage("bahmni.sms.dateformat", null, new Locale("en"))).thenReturn("dd-MM-yyyy"); Date visitDate = new SimpleDateFormat("MMMM d, yyyy", new Locale("en")).parse("January 30, 2023"); Date birthDate = new SimpleDateFormat("MMMM d, yyyy", new Locale("en")).parse("January 30, 2010"); Person person = new PersonBuilder().withUUID("puuid").withPersonName("testPersonName").build(); @@ -76,6 +77,7 @@ public void shouldReturnPrescriptionSMSWithDefaultTemplate() throws ParseExcepti when(administrationService.getGlobalProperty("bahmni.prescriptionSMSTemplate")).thenReturn(null); when(messageSourceService.getMessage("bahmni.prescriptionSMSTemplate", args, new Locale("en"))).thenReturn("Date: 30-01-2023\nPrescription For Patient: testPersonName null, M, 13 years.\nDoctor: Superman (Bahmni)\n1. Paracetamol 150 mg/ml, 50 ml, Immediately-1 Days, start from 31-01-2023"); when(messageSourceService.getMessage("bahmni.sms.timezone", null, new Locale("en"))).thenReturn("IST"); + when(messageSourceService.getMessage("bahmni.sms.dateformat", null, new Locale("en"))).thenReturn("dd-MM-yyyy"); Date visitDate = new SimpleDateFormat("MMMM d, yyyy", new Locale("en")).parse("January 30, 2023"); Date birthDate = new SimpleDateFormat("MMMM d, yyyy", new Locale("en")).parse("January 30, 2010"); Person person = new PersonBuilder().withUUID("puuid").withPersonName("testPersonName").build(); diff --git a/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/service/impl/SharePrescriptionServiceImplIT.java b/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/service/impl/SharePrescriptionServiceImplIT.java index c71e47787d..c6f7ed2ba4 100644 --- a/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/service/impl/SharePrescriptionServiceImplIT.java +++ b/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/service/impl/SharePrescriptionServiceImplIT.java @@ -29,8 +29,8 @@ import java.util.ArrayList; import java.util.Date; import java.util.Locale; -import java.util.Set; -import java.util.HashSet; +import java.util.stream.Collectors; +import java.util.stream.Stream; import static org.mockito.Mockito.when; import static org.mockito.Mockito.verify; @@ -73,11 +73,12 @@ public void shouldCallSendSMSForSendingPrescriptionSMS() throws Exception { when(administrationService.getGlobalProperty("bahmni.prescriptionSMSTemplate")).thenReturn("Date: {0}\nPrescription For Patient: {1}, {2}, {3} years.\nDoctor: {4} ({5})\n{6}"); when(messageSourceService.getMessage("bahmni.sms.timezone", null, new Locale("en"))).thenReturn("IST"); + when(messageSourceService.getMessage("bahmni.sms.dateformat", null, new Locale("en"))).thenReturn("dd-MM-yyyy"); when(messageSourceService.getMessage("bahmni.sms.url", null, new Locale("en"))).thenReturn(null); when(bahmniVisitService.getVisitSummary(prescriptionSMS.getVisitUuid())).thenReturn(visit); - when(bahmniVisitService.getParentLocationNameForVisit(visit.getLocation())).thenReturn(visit.getLocation().getName()); + when(bahmniVisitService.getParentLocationForVisit(visit.getLocation())).thenReturn(visit.getLocation()); when(bahmniDrugOrderService.getMergedDrugOrderMap(new ArrayList())).thenReturn(null); - when(bahmniDrugOrderService.getAllProviderAsString(new ArrayList())).thenReturn("Superman"); + when(bahmniDrugOrderService.getUniqueProviderNames(new ArrayList())).thenReturn(Stream.of("Superman").collect(Collectors.toSet())); when(bahmniDrugOrderService.getPrescriptionAsString(null, new Locale("en"))).thenReturn("1. Paracetamol 150 mg/ml, 50 ml, Immediately-1 Days, start from 31-01-2023"); when(smsService.getPrescriptionMessage(prescriptionSMS.getLocale(), visit.getStartDatetime(), visit.getPatient(), "Bahmni", "Superman", "1. Paracetamol 150 mg/ml, 50 ml, Immediately-1 Days, start from 31-01-2023")) @@ -97,9 +98,7 @@ private Visit createVisitForTest() throws Exception { PersonAttributeType pat = new PersonAttributeType(); pat.setName("phoneNumber"); pa.setAttributeType(pat); - Set paSet = new HashSet<>(); - paSet.add(pa); - person.setAttributes(paSet); + person.setAttributes(Stream.of(pa).collect(Collectors.toSet())); Visit visit = new VisitBuilder().withPerson(person).withUUID("visit-uuid").withStartDatetime(visitDate).build(); Location location = new Location(); diff --git a/bahmnicore-omod/src/main/resources/messages.properties b/bahmnicore-omod/src/main/resources/messages.properties index 89fb8eb609..e4d5120a1a 100644 --- a/bahmnicore-omod/src/main/resources/messages.properties +++ b/bahmnicore-omod/src/main/resources/messages.properties @@ -2,4 +2,5 @@ bahmni.prescriptionSMSTemplate=Date: {0}\nPrescription For Patient: {1}, {2}, {3} years.\nDoctor: {4} ({5})\n{6} bahmni.sms.timezone=IST -bahmni.sms.url=http://otp/notification/sms \ No newline at end of file +bahmni.sms.url=http://otp/notification/sms +bahmni.sms.dateformat=dd-MM-yyyy \ No newline at end of file From ae43b6996621e08d8919370621a0025bf63ae5ae Mon Sep 17 00:00:00 2001 From: Kavitha S Date: Wed, 8 Feb 2023 18:10:37 +0530 Subject: [PATCH 07/10] BAH-2763 | renamed method getOrdAttributeConcepts --- .../bahmnicore/service/impl/BahmniDrugOrderServiceImpl.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/BahmniDrugOrderServiceImpl.java b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/BahmniDrugOrderServiceImpl.java index a76837054d..8f576d6e72 100644 --- a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/BahmniDrugOrderServiceImpl.java +++ b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/BahmniDrugOrderServiceImpl.java @@ -184,7 +184,7 @@ public List getBahmniDrugOrdersForVisit(String patientUuid, Str try { List drugOrderList = getPrescribedDrugOrders(Arrays.asList(visitUuid), patientUuid, null,null, null, null, null); Map drugOrderMap = getDiscontinuedDrugOrders(drugOrderList); - Collection orderAttributeObs = bahmniObsService.observationsFor(patientUuid, getOrdAttributeConcepts(), null, null, false, null, null, null); + Collection orderAttributeObs = bahmniObsService.observationsFor(patientUuid, getOrderAttributeConcepts(), null, null, false, null, null, null); List bahmniDrugOrderList = bahmniDrugOrderMapper.mapToResponse(drugOrderList, orderAttributeObs, drugOrderMap , null); Collections.sort(bahmniDrugOrderList, new Comparator() { @Override @@ -271,7 +271,7 @@ private double getDateDifferenceInDays(Date date1, Date date2){ return TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS); } - private Collection getOrdAttributeConcepts() { + private Collection getOrderAttributeConcepts() { Concept orderAttribute = conceptService.getConceptByName(BahmniOrderAttribute.ORDER_ATTRIBUTES_CONCEPT_SET_NAME); return orderAttribute == null ? Collections.EMPTY_LIST : orderAttribute.getSetMembers(); } From 047392eda1c94cfe20b11d3f72d6f6e4d0e7e2fb Mon Sep 17 00:00:00 2001 From: Kavitha S Date: Wed, 8 Feb 2023 20:18:57 +0530 Subject: [PATCH 08/10] BAH-2763 | refactor to consolidate the params for message building in an array --- .../module/bahmnicore/service/SMSService.java | 2 +- .../bahmnicore/service/impl/SMSServiceImpl.java | 13 +++++++------ .../service/impl/SharePrescriptionServiceImpl.java | 3 ++- .../bahmnicore/service/impl/SMSServiceImplIT.java | 12 ++++++------ .../impl/SharePrescriptionServiceImplIT.java | 6 ++---- 5 files changed, 18 insertions(+), 18 deletions(-) diff --git a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/SMSService.java b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/SMSService.java index 28ded7f4c1..09e6456820 100644 --- a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/SMSService.java +++ b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/SMSService.java @@ -6,7 +6,7 @@ public interface SMSService { - String getPrescriptionMessage(String lang, Date visitDate, Patient patient, String location, String providerDetail, String prescriptionDetail); + String getPrescriptionMessage(Object[] prescriptionArguments); Object sendSMS(String phoneNumber, String message); } diff --git a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/SMSServiceImpl.java b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/SMSServiceImpl.java index e1ff5fba14..a8b734fce3 100644 --- a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/SMSServiceImpl.java +++ b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/SMSServiceImpl.java @@ -60,15 +60,16 @@ public Object sendSMS(String phoneNumber, String message) { } @Override - public String getPrescriptionMessage(String lang, Date visitDate, Patient patient, String location, String providerDetail, String prescriptionDetail) { - String smsTimeZone = Context.getMessageSourceService().getMessage(SMS_TIMEZONE, null, new Locale(lang)); - String smsDateFormat = Context.getMessageSourceService().getMessage(SMS_DATEFORMAT, null, new Locale(lang)); + public String getPrescriptionMessage(Object[] prescriptionArguments) { + String smsTimeZone = Context.getMessageSourceService().getMessage(SMS_TIMEZONE, null, new Locale((String) prescriptionArguments[0])); + String smsDateFormat = Context.getMessageSourceService().getMessage(SMS_DATEFORMAT, null, new Locale((String) prescriptionArguments[0])); String smsTemplate = Context.getAdministrationService().getGlobalProperty(PRESCRIPTION_SMS_TEMPLATE); - Object[] arguments = {convertUTCToGivenFormat(visitDate, smsDateFormat, smsTimeZone), + Patient patient = (Patient) prescriptionArguments[2]; + Object[] arguments = {convertUTCToGivenFormat((Date) prescriptionArguments[1], smsDateFormat, smsTimeZone), patient.getGivenName() + " " + patient.getFamilyName(), patient.getGender(), patient.getAge().toString(), - providerDetail, location, prescriptionDetail}; + (String) prescriptionArguments[4], (String) prescriptionArguments[3], (String) prescriptionArguments[5]}; if (StringUtils.isBlank(smsTemplate)) { - return Context.getMessageSourceService().getMessage(PRESCRIPTION_SMS_TEMPLATE, arguments, new Locale(lang)).replace("\\n", System.lineSeparator()); + return Context.getMessageSourceService().getMessage(PRESCRIPTION_SMS_TEMPLATE, arguments, new Locale((String) prescriptionArguments[0])).replace("\\n", System.lineSeparator()); } else { return new MessageFormat(smsTemplate).format(arguments).replace("\\n", System.lineSeparator()); } diff --git a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/SharePrescriptionServiceImpl.java b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/SharePrescriptionServiceImpl.java index 74b8db5723..bc74331eed 100644 --- a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/SharePrescriptionServiceImpl.java +++ b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/SharePrescriptionServiceImpl.java @@ -43,7 +43,8 @@ public Object sendPresciptionSMS(PrescriptionSMS prescription) { Map mergedDrugOrderMap = drugOrderService.getMergedDrugOrderMap(drugOrderList); String providerString = StringUtils.collectionToCommaDelimitedString(drugOrderService.getUniqueProviderNames(drugOrderList)); String prescriptionString = drugOrderService.getPrescriptionAsString(mergedDrugOrderMap, new Locale(prescription.getLocale())); - String prescriptionSMSContent = smsService.getPrescriptionMessage(prescription.getLocale(), visit.getStartDatetime(), visit.getPatient(), locationName, providerString, prescriptionString); + Object[] prescriptionArguments = {prescription.getLocale(), visit.getStartDatetime(), visit.getPatient(), locationName, providerString, prescriptionString}; + String prescriptionSMSContent = smsService.getPrescriptionMessage(prescriptionArguments); return smsService.sendSMS(visit.getPatient().getAttribute("phoneNumber").getValue(), prescriptionSMSContent); } diff --git a/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/service/impl/SMSServiceImplIT.java b/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/service/impl/SMSServiceImplIT.java index 4e7617de25..fea1b4f00e 100644 --- a/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/service/impl/SMSServiceImplIT.java +++ b/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/service/impl/SMSServiceImplIT.java @@ -62,8 +62,8 @@ public void shouldReturnPrescriptionSMSWithGlobalPropertyTemplate() throws Parse person.setGender("M"); person.setBirthdate(birthDate); Visit visit = new VisitBuilder().withPerson(person).withUUID("vuuid").withStartDatetime(visitDate).build(); - - String prescriptionContent = smsService.getPrescriptionMessage("en", visit.getStartDatetime(), visit.getPatient(), "Bahmni", "Superman", "1. Paracetamol 150 mg/ml, 50 ml, Immediately-1 Days, start from 31-01-2023"); + Object[] prescriptionArguments = {"en", visit.getStartDatetime(), visit.getPatient(), "Bahmni", "Superman", "1. Paracetamol 150 mg/ml, 50 ml, Immediately-1 Days, start from 31-01-2023"}; + String prescriptionContent = smsService.getPrescriptionMessage(prescriptionArguments); String expectedPrescriptionContent = "Date: 30-01-2023\n" + "Prescription For Patient: testPersonName null, M, 13 years.\n" + "Doctor: Superman (Bahmni)\n" + @@ -73,9 +73,9 @@ public void shouldReturnPrescriptionSMSWithGlobalPropertyTemplate() throws Parse @Test public void shouldReturnPrescriptionSMSWithDefaultTemplate() throws ParseException { - Object[] args = {"30-01-2023", "testPersonName null", "M", "13", "Superman", "Bahmni", "1. Paracetamol 150 mg/ml, 50 ml, Immediately-1 Days, start from 31-01-2023"}; + Object[] arguments = {"30-01-2023", "testPersonName null", "M", "13", "Superman", "Bahmni", "1. Paracetamol 150 mg/ml, 50 ml, Immediately-1 Days, start from 31-01-2023"}; when(administrationService.getGlobalProperty("bahmni.prescriptionSMSTemplate")).thenReturn(null); - when(messageSourceService.getMessage("bahmni.prescriptionSMSTemplate", args, new Locale("en"))).thenReturn("Date: 30-01-2023\nPrescription For Patient: testPersonName null, M, 13 years.\nDoctor: Superman (Bahmni)\n1. Paracetamol 150 mg/ml, 50 ml, Immediately-1 Days, start from 31-01-2023"); + when(messageSourceService.getMessage("bahmni.prescriptionSMSTemplate", arguments, new Locale("en"))).thenReturn("Date: 30-01-2023\nPrescription For Patient: testPersonName null, M, 13 years.\nDoctor: Superman (Bahmni)\n1. Paracetamol 150 mg/ml, 50 ml, Immediately-1 Days, start from 31-01-2023"); when(messageSourceService.getMessage("bahmni.sms.timezone", null, new Locale("en"))).thenReturn("IST"); when(messageSourceService.getMessage("bahmni.sms.dateformat", null, new Locale("en"))).thenReturn("dd-MM-yyyy"); Date visitDate = new SimpleDateFormat("MMMM d, yyyy", new Locale("en")).parse("January 30, 2023"); @@ -84,8 +84,8 @@ public void shouldReturnPrescriptionSMSWithDefaultTemplate() throws ParseExcepti person.setGender("M"); person.setBirthdate(birthDate); Visit visit = new VisitBuilder().withPerson(person).withUUID("vuuid").withStartDatetime(visitDate).build(); - - String prescriptionContent = smsService.getPrescriptionMessage("en", visit.getStartDatetime(), visit.getPatient(), "Bahmni", "Superman", "1. Paracetamol 150 mg/ml, 50 ml, Immediately-1 Days, start from 31-01-2023"); + Object[] prescriptionArguments = {"en", visit.getStartDatetime(), visit.getPatient(), "Bahmni", "Superman", "1. Paracetamol 150 mg/ml, 50 ml, Immediately-1 Days, start from 31-01-2023"}; + String prescriptionContent = smsService.getPrescriptionMessage(prescriptionArguments); String expectedPrescriptionContent = "Date: 30-01-2023\n" + "Prescription For Patient: testPersonName null, M, 13 years.\n" + "Doctor: Superman (Bahmni)\n" + diff --git a/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/service/impl/SharePrescriptionServiceImplIT.java b/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/service/impl/SharePrescriptionServiceImplIT.java index c6f7ed2ba4..0df07684e3 100644 --- a/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/service/impl/SharePrescriptionServiceImplIT.java +++ b/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/service/impl/SharePrescriptionServiceImplIT.java @@ -70,7 +70,7 @@ public void shouldCallSendSMSForSendingPrescriptionSMS() throws Exception { "Prescription For Patient: testPersonName null, M, 13 years.\n" + "Doctor: Superman (Bahmni)\n" + "1. Paracetamol 150 mg/ml, 50 ml, Immediately-1 Days, start from 31-01-2023"; - + Object[] prescriptionArguments = {prescriptionSMS.getLocale(), visit.getStartDatetime(), visit.getPatient(), "Bahmni", "Superman", "1. Paracetamol 150 mg/ml, 50 ml, Immediately-1 Days, start from 31-01-2023"}; when(administrationService.getGlobalProperty("bahmni.prescriptionSMSTemplate")).thenReturn("Date: {0}\nPrescription For Patient: {1}, {2}, {3} years.\nDoctor: {4} ({5})\n{6}"); when(messageSourceService.getMessage("bahmni.sms.timezone", null, new Locale("en"))).thenReturn("IST"); when(messageSourceService.getMessage("bahmni.sms.dateformat", null, new Locale("en"))).thenReturn("dd-MM-yyyy"); @@ -80,9 +80,7 @@ public void shouldCallSendSMSForSendingPrescriptionSMS() throws Exception { when(bahmniDrugOrderService.getMergedDrugOrderMap(new ArrayList())).thenReturn(null); when(bahmniDrugOrderService.getUniqueProviderNames(new ArrayList())).thenReturn(Stream.of("Superman").collect(Collectors.toSet())); when(bahmniDrugOrderService.getPrescriptionAsString(null, new Locale("en"))).thenReturn("1. Paracetamol 150 mg/ml, 50 ml, Immediately-1 Days, start from 31-01-2023"); - when(smsService.getPrescriptionMessage(prescriptionSMS.getLocale(), visit.getStartDatetime(), visit.getPatient(), - "Bahmni", "Superman", "1. Paracetamol 150 mg/ml, 50 ml, Immediately-1 Days, start from 31-01-2023")) - .thenReturn(sampleSMSContent); + when(smsService.getPrescriptionMessage(prescriptionArguments)).thenReturn(sampleSMSContent); sharePrescriptionService.sendPresciptionSMS(prescriptionSMS); verify(smsService, times(1)).sendSMS("+919999999999", sampleSMSContent); } From 784c9681849beb9c4dc889f54d6356888f36bb13 Mon Sep 17 00:00:00 2001 From: Kavitha S Date: Fri, 10 Feb 2023 18:31:27 +0530 Subject: [PATCH 09/10] Revert "BAH-2763 | refactor to consolidate the params for message building in an array" This reverts commit 047392eda1c94cfe20b11d3f72d6f6e4d0e7e2fb. --- .../module/bahmnicore/service/SMSService.java | 2 +- .../bahmnicore/service/impl/SMSServiceImpl.java | 13 ++++++------- .../service/impl/SharePrescriptionServiceImpl.java | 3 +-- .../bahmnicore/service/impl/SMSServiceImplIT.java | 12 ++++++------ .../impl/SharePrescriptionServiceImplIT.java | 6 ++++-- 5 files changed, 18 insertions(+), 18 deletions(-) diff --git a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/SMSService.java b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/SMSService.java index 09e6456820..28ded7f4c1 100644 --- a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/SMSService.java +++ b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/SMSService.java @@ -6,7 +6,7 @@ public interface SMSService { - String getPrescriptionMessage(Object[] prescriptionArguments); + String getPrescriptionMessage(String lang, Date visitDate, Patient patient, String location, String providerDetail, String prescriptionDetail); Object sendSMS(String phoneNumber, String message); } diff --git a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/SMSServiceImpl.java b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/SMSServiceImpl.java index a8b734fce3..e1ff5fba14 100644 --- a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/SMSServiceImpl.java +++ b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/SMSServiceImpl.java @@ -60,16 +60,15 @@ public Object sendSMS(String phoneNumber, String message) { } @Override - public String getPrescriptionMessage(Object[] prescriptionArguments) { - String smsTimeZone = Context.getMessageSourceService().getMessage(SMS_TIMEZONE, null, new Locale((String) prescriptionArguments[0])); - String smsDateFormat = Context.getMessageSourceService().getMessage(SMS_DATEFORMAT, null, new Locale((String) prescriptionArguments[0])); + public String getPrescriptionMessage(String lang, Date visitDate, Patient patient, String location, String providerDetail, String prescriptionDetail) { + String smsTimeZone = Context.getMessageSourceService().getMessage(SMS_TIMEZONE, null, new Locale(lang)); + String smsDateFormat = Context.getMessageSourceService().getMessage(SMS_DATEFORMAT, null, new Locale(lang)); String smsTemplate = Context.getAdministrationService().getGlobalProperty(PRESCRIPTION_SMS_TEMPLATE); - Patient patient = (Patient) prescriptionArguments[2]; - Object[] arguments = {convertUTCToGivenFormat((Date) prescriptionArguments[1], smsDateFormat, smsTimeZone), + Object[] arguments = {convertUTCToGivenFormat(visitDate, smsDateFormat, smsTimeZone), patient.getGivenName() + " " + patient.getFamilyName(), patient.getGender(), patient.getAge().toString(), - (String) prescriptionArguments[4], (String) prescriptionArguments[3], (String) prescriptionArguments[5]}; + providerDetail, location, prescriptionDetail}; if (StringUtils.isBlank(smsTemplate)) { - return Context.getMessageSourceService().getMessage(PRESCRIPTION_SMS_TEMPLATE, arguments, new Locale((String) prescriptionArguments[0])).replace("\\n", System.lineSeparator()); + return Context.getMessageSourceService().getMessage(PRESCRIPTION_SMS_TEMPLATE, arguments, new Locale(lang)).replace("\\n", System.lineSeparator()); } else { return new MessageFormat(smsTemplate).format(arguments).replace("\\n", System.lineSeparator()); } diff --git a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/SharePrescriptionServiceImpl.java b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/SharePrescriptionServiceImpl.java index bc74331eed..74b8db5723 100644 --- a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/SharePrescriptionServiceImpl.java +++ b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/SharePrescriptionServiceImpl.java @@ -43,8 +43,7 @@ public Object sendPresciptionSMS(PrescriptionSMS prescription) { Map mergedDrugOrderMap = drugOrderService.getMergedDrugOrderMap(drugOrderList); String providerString = StringUtils.collectionToCommaDelimitedString(drugOrderService.getUniqueProviderNames(drugOrderList)); String prescriptionString = drugOrderService.getPrescriptionAsString(mergedDrugOrderMap, new Locale(prescription.getLocale())); - Object[] prescriptionArguments = {prescription.getLocale(), visit.getStartDatetime(), visit.getPatient(), locationName, providerString, prescriptionString}; - String prescriptionSMSContent = smsService.getPrescriptionMessage(prescriptionArguments); + String prescriptionSMSContent = smsService.getPrescriptionMessage(prescription.getLocale(), visit.getStartDatetime(), visit.getPatient(), locationName, providerString, prescriptionString); return smsService.sendSMS(visit.getPatient().getAttribute("phoneNumber").getValue(), prescriptionSMSContent); } diff --git a/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/service/impl/SMSServiceImplIT.java b/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/service/impl/SMSServiceImplIT.java index fea1b4f00e..4e7617de25 100644 --- a/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/service/impl/SMSServiceImplIT.java +++ b/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/service/impl/SMSServiceImplIT.java @@ -62,8 +62,8 @@ public void shouldReturnPrescriptionSMSWithGlobalPropertyTemplate() throws Parse person.setGender("M"); person.setBirthdate(birthDate); Visit visit = new VisitBuilder().withPerson(person).withUUID("vuuid").withStartDatetime(visitDate).build(); - Object[] prescriptionArguments = {"en", visit.getStartDatetime(), visit.getPatient(), "Bahmni", "Superman", "1. Paracetamol 150 mg/ml, 50 ml, Immediately-1 Days, start from 31-01-2023"}; - String prescriptionContent = smsService.getPrescriptionMessage(prescriptionArguments); + + String prescriptionContent = smsService.getPrescriptionMessage("en", visit.getStartDatetime(), visit.getPatient(), "Bahmni", "Superman", "1. Paracetamol 150 mg/ml, 50 ml, Immediately-1 Days, start from 31-01-2023"); String expectedPrescriptionContent = "Date: 30-01-2023\n" + "Prescription For Patient: testPersonName null, M, 13 years.\n" + "Doctor: Superman (Bahmni)\n" + @@ -73,9 +73,9 @@ public void shouldReturnPrescriptionSMSWithGlobalPropertyTemplate() throws Parse @Test public void shouldReturnPrescriptionSMSWithDefaultTemplate() throws ParseException { - Object[] arguments = {"30-01-2023", "testPersonName null", "M", "13", "Superman", "Bahmni", "1. Paracetamol 150 mg/ml, 50 ml, Immediately-1 Days, start from 31-01-2023"}; + Object[] args = {"30-01-2023", "testPersonName null", "M", "13", "Superman", "Bahmni", "1. Paracetamol 150 mg/ml, 50 ml, Immediately-1 Days, start from 31-01-2023"}; when(administrationService.getGlobalProperty("bahmni.prescriptionSMSTemplate")).thenReturn(null); - when(messageSourceService.getMessage("bahmni.prescriptionSMSTemplate", arguments, new Locale("en"))).thenReturn("Date: 30-01-2023\nPrescription For Patient: testPersonName null, M, 13 years.\nDoctor: Superman (Bahmni)\n1. Paracetamol 150 mg/ml, 50 ml, Immediately-1 Days, start from 31-01-2023"); + when(messageSourceService.getMessage("bahmni.prescriptionSMSTemplate", args, new Locale("en"))).thenReturn("Date: 30-01-2023\nPrescription For Patient: testPersonName null, M, 13 years.\nDoctor: Superman (Bahmni)\n1. Paracetamol 150 mg/ml, 50 ml, Immediately-1 Days, start from 31-01-2023"); when(messageSourceService.getMessage("bahmni.sms.timezone", null, new Locale("en"))).thenReturn("IST"); when(messageSourceService.getMessage("bahmni.sms.dateformat", null, new Locale("en"))).thenReturn("dd-MM-yyyy"); Date visitDate = new SimpleDateFormat("MMMM d, yyyy", new Locale("en")).parse("January 30, 2023"); @@ -84,8 +84,8 @@ public void shouldReturnPrescriptionSMSWithDefaultTemplate() throws ParseExcepti person.setGender("M"); person.setBirthdate(birthDate); Visit visit = new VisitBuilder().withPerson(person).withUUID("vuuid").withStartDatetime(visitDate).build(); - Object[] prescriptionArguments = {"en", visit.getStartDatetime(), visit.getPatient(), "Bahmni", "Superman", "1. Paracetamol 150 mg/ml, 50 ml, Immediately-1 Days, start from 31-01-2023"}; - String prescriptionContent = smsService.getPrescriptionMessage(prescriptionArguments); + + String prescriptionContent = smsService.getPrescriptionMessage("en", visit.getStartDatetime(), visit.getPatient(), "Bahmni", "Superman", "1. Paracetamol 150 mg/ml, 50 ml, Immediately-1 Days, start from 31-01-2023"); String expectedPrescriptionContent = "Date: 30-01-2023\n" + "Prescription For Patient: testPersonName null, M, 13 years.\n" + "Doctor: Superman (Bahmni)\n" + diff --git a/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/service/impl/SharePrescriptionServiceImplIT.java b/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/service/impl/SharePrescriptionServiceImplIT.java index 0df07684e3..c6f7ed2ba4 100644 --- a/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/service/impl/SharePrescriptionServiceImplIT.java +++ b/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/service/impl/SharePrescriptionServiceImplIT.java @@ -70,7 +70,7 @@ public void shouldCallSendSMSForSendingPrescriptionSMS() throws Exception { "Prescription For Patient: testPersonName null, M, 13 years.\n" + "Doctor: Superman (Bahmni)\n" + "1. Paracetamol 150 mg/ml, 50 ml, Immediately-1 Days, start from 31-01-2023"; - Object[] prescriptionArguments = {prescriptionSMS.getLocale(), visit.getStartDatetime(), visit.getPatient(), "Bahmni", "Superman", "1. Paracetamol 150 mg/ml, 50 ml, Immediately-1 Days, start from 31-01-2023"}; + when(administrationService.getGlobalProperty("bahmni.prescriptionSMSTemplate")).thenReturn("Date: {0}\nPrescription For Patient: {1}, {2}, {3} years.\nDoctor: {4} ({5})\n{6}"); when(messageSourceService.getMessage("bahmni.sms.timezone", null, new Locale("en"))).thenReturn("IST"); when(messageSourceService.getMessage("bahmni.sms.dateformat", null, new Locale("en"))).thenReturn("dd-MM-yyyy"); @@ -80,7 +80,9 @@ public void shouldCallSendSMSForSendingPrescriptionSMS() throws Exception { when(bahmniDrugOrderService.getMergedDrugOrderMap(new ArrayList())).thenReturn(null); when(bahmniDrugOrderService.getUniqueProviderNames(new ArrayList())).thenReturn(Stream.of("Superman").collect(Collectors.toSet())); when(bahmniDrugOrderService.getPrescriptionAsString(null, new Locale("en"))).thenReturn("1. Paracetamol 150 mg/ml, 50 ml, Immediately-1 Days, start from 31-01-2023"); - when(smsService.getPrescriptionMessage(prescriptionArguments)).thenReturn(sampleSMSContent); + when(smsService.getPrescriptionMessage(prescriptionSMS.getLocale(), visit.getStartDatetime(), visit.getPatient(), + "Bahmni", "Superman", "1. Paracetamol 150 mg/ml, 50 ml, Immediately-1 Days, start from 31-01-2023")) + .thenReturn(sampleSMSContent); sharePrescriptionService.sendPresciptionSMS(prescriptionSMS); verify(smsService, times(1)).sendSMS("+919999999999", sampleSMSContent); } From cd06f0b709be42e7cf5aaf73be2173f1f974a01c Mon Sep 17 00:00:00 2001 From: Kavitha S Date: Sat, 11 Feb 2023 22:03:00 +0530 Subject: [PATCH 10/10] BAH-2763 | refactor exposed methods and updated tests --- .../service/BahmniDrugOrderService.java | 8 -- .../service/BahmniVisitService.java | 3 - .../module/bahmnicore/service/SMSService.java | 5 +- .../impl/BahmniDrugOrderServiceImpl.java | 93 ------------- .../service/impl/BahmniVisitServiceImpl.java | 7 - .../service/impl/SMSServiceImpl.java | 12 +- .../impl/SharePrescriptionServiceImpl.java | 123 +++++++++++++++++- .../impl/BahmniDrugOrderServiceImplTest.java | 123 +----------------- .../service/impl/SMSServiceImplIT.java | 10 +- .../impl/SharePrescriptionServiceImplIT.java | 77 +++++++++-- 10 files changed, 206 insertions(+), 255 deletions(-) diff --git a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/BahmniDrugOrderService.java b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/BahmniDrugOrderService.java index 2d97102a21..4166221ede 100644 --- a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/BahmniDrugOrderService.java +++ b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/BahmniDrugOrderService.java @@ -15,7 +15,6 @@ import java.util.List; import java.util.Map; import java.util.Set; -import java.util.Locale; public interface BahmniDrugOrderService { List getActiveDrugOrders(String patientUuid, Date startDate, Date endDate); @@ -41,11 +40,4 @@ List getDrugOrders(String patientUuid, Boolean isActive, Set getBahmniDrugOrdersForVisit(String patientUuid, String visitUuid); - - Map getMergedDrugOrderMap(List drugOrderList); - - String getPrescriptionAsString(Map drugOrderDurationMap, Locale locale); - - Set getUniqueProviderNames(List drugOrders); - } diff --git a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/BahmniVisitService.java b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/BahmniVisitService.java index c5527152a1..65f5618e3d 100644 --- a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/BahmniVisitService.java +++ b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/BahmniVisitService.java @@ -1,7 +1,6 @@ package org.bahmni.module.bahmnicore.service; import org.openmrs.Encounter; -import org.openmrs.Location; import org.openmrs.Visit; import java.util.List; @@ -12,6 +11,4 @@ public interface BahmniVisitService { Visit getVisitSummary(String visitUuid); List getAdmitAndDischargeEncounters(Integer visitId); - - Location getParentLocationForVisit(Location location); } diff --git a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/SMSService.java b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/SMSService.java index 28ded7f4c1..33f58f3c57 100644 --- a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/SMSService.java +++ b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/SMSService.java @@ -1,12 +1,15 @@ package org.bahmni.module.bahmnicore.service; +import org.openmrs.Location; import org.openmrs.Patient; import java.util.Date; +import java.util.List; +import java.util.Locale; public interface SMSService { - String getPrescriptionMessage(String lang, Date visitDate, Patient patient, String location, String providerDetail, String prescriptionDetail); + String getPrescriptionMessage(Locale locale, Date visitDate, Patient patient, Location location, List providerList, String prescriptionDetail); Object sendSMS(String phoneNumber, String message); } diff --git a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/BahmniDrugOrderServiceImpl.java b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/BahmniDrugOrderServiceImpl.java index 8f576d6e72..298c094a37 100644 --- a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/BahmniDrugOrderServiceImpl.java +++ b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/BahmniDrugOrderServiceImpl.java @@ -41,17 +41,11 @@ import java.util.Date; import java.util.List; import java.util.Map; -import java.util.LinkedHashMap; import java.util.Set; -import java.util.LinkedHashSet; -import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; -import java.util.Locale; import java.util.Arrays; import java.util.Comparator; -import static org.bahmni.module.bahmnicore.util.BahmniDateUtil.convertUTCToGivenFormat; - @Service public class BahmniDrugOrderServiceImpl implements BahmniDrugOrderService { private ConceptService conceptService; @@ -65,8 +59,6 @@ public class BahmniDrugOrderServiceImpl implements BahmniDrugOrderService { private static final String GP_DOSING_INSTRUCTIONS_CONCEPT_UUID = "order.dosingInstructionsConceptUuid"; private static Logger logger = LogManager.getLogger(BahmniDrugOrderService.class); - private final static String SMS_TIMEZONE = "bahmni.sms.timezone"; - private final static String SMS_DATEFORMAT = "bahmni.sms.dateformat"; @Autowired public BahmniDrugOrderServiceImpl(ConceptService conceptService, OrderService orderService, PatientService patientService, OrderDao orderDao, @@ -199,78 +191,6 @@ public int compare(BahmniDrugOrder o1, BahmniDrugOrder o2) { } } - @Override - public Map getMergedDrugOrderMap(List drugOrderList) { - Map mergedDrugOrderMap = new LinkedHashMap<>(); - for(BahmniDrugOrder drugOrder : drugOrderList) { - BahmniDrugOrder foundDrugOrder = mergedDrugOrderMap.entrySet().stream() - .map(x -> x.getKey()) - .filter( existingOrder -> - compareDrugOrders(existingOrder, drugOrder) ) - .findFirst() - .orElse(null); - if (foundDrugOrder!=null) { - String durationWithUnits = mergedDrugOrderMap.get(foundDrugOrder); - if(drugOrder.getDurationUnits() == foundDrugOrder.getDurationUnits()) - durationWithUnits = (foundDrugOrder.getDuration()+drugOrder.getDuration()) + " " + drugOrder.getDurationUnits(); - else - durationWithUnits += " + " + drugOrder.getDuration() + " " + drugOrder.getDurationUnits(); - mergedDrugOrderMap.put(foundDrugOrder, durationWithUnits); - } else { - mergedDrugOrderMap.put(drugOrder, drugOrder.getDuration()+" "+drugOrder.getDurationUnits()); - } - } - return mergedDrugOrderMap; - } - - @Override - public String getPrescriptionAsString(Map drugOrderDurationMap, Locale locale) { - String prescriptionString = ""; - int counter = 1; - for (Map.Entry entry : drugOrderDurationMap.entrySet()) { - prescriptionString += counter++ + ". " + getDrugOrderAsString(entry.getKey(), drugOrderDurationMap.get(entry.getKey()), locale) + "\n"; - } - return prescriptionString; - } - - public Set getUniqueProviderNames(List drugOrders) { - Set providerSet = new LinkedHashSet(); - for (BahmniDrugOrder drugOrder : drugOrders) { - providerSet.add("Dr " + drugOrder.getProvider().getName()); - } - return providerSet; - } - - private String getDrugOrderAsString(BahmniDrugOrder drugOrder, String duration, Locale locale) { - String drugOrderString = drugOrder.getDrug().getName(); - drugOrderString += ", " + (drugOrder.getDosingInstructions().getDose().intValue()) + " " + drugOrder.getDosingInstructions().getDoseUnits(); - drugOrderString += ", " + drugOrder.getDosingInstructions().getFrequency() + "-" + duration; - drugOrderString += ", start from " + convertUTCToGivenFormat(drugOrder.getEffectiveStartDate(), - Context.getMessageSourceService().getMessage(SMS_DATEFORMAT, null, locale), Context.getMessageSourceService().getMessage(SMS_TIMEZONE, null, locale)); - if(drugOrder.getDateStopped() != null) - drugOrderString += ", stopped on " + convertUTCToGivenFormat(drugOrder.getDateStopped(), - Context.getMessageSourceService().getMessage(SMS_DATEFORMAT, null, locale), Context.getMessageSourceService().getMessage(SMS_TIMEZONE, null, locale)); - return drugOrderString; - } - - private Boolean areValuesEqual(Object value1, Object value2) { - if(value1 != null && value2 != null) { - return value1.equals(value2); - } - return (value1 == null && value2 == null); - }; - - private Boolean compareDosingInstruction(EncounterTransaction.DosingInstructions value1, EncounterTransaction.DosingInstructions value2) { - String doseAndFrequency1 = value1.getDose() + " " + value1.getDoseUnits() + ", " + value1.getFrequency(); - String doseAndFrequency2 = value2.getDose() + " " + value2.getDoseUnits() + ", " + value2.getFrequency(); - return doseAndFrequency1.equals(doseAndFrequency2); - }; - - private double getDateDifferenceInDays(Date date1, Date date2){ - long diff = date2.getTime() - date1.getTime(); - return TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS); - } - private Collection getOrderAttributeConcepts() { Concept orderAttribute = conceptService.getConceptByName(BahmniOrderAttribute.ORDER_ATTRIBUTES_CONCEPT_SET_NAME); return orderAttribute == null ? Collections.EMPTY_LIST : orderAttribute.getSetMembers(); @@ -329,17 +249,4 @@ private List mapOrderToDrugOrder(List orders){ } return drugOrders; } - - private boolean compareDrugOrders(BahmniDrugOrder existingOrder, BahmniDrugOrder newDrugOrder) { - return (existingOrder.getDrug()!=null && newDrugOrder.getDrug()!=null && - areValuesEqual(existingOrder.getDrug().getUuid(), newDrugOrder.getDrug().getUuid())) && - areValuesEqual(existingOrder.getDrugNonCoded(), newDrugOrder.getDrugNonCoded()) && - areValuesEqual(existingOrder.getInstructions(), newDrugOrder.getInstructions()) && - compareDosingInstruction(existingOrder.getDosingInstructions(), newDrugOrder.getDosingInstructions()) && - areValuesEqual(existingOrder.getDosingInstructions().getRoute(), newDrugOrder.getDosingInstructions().getRoute()) && - areValuesEqual(existingOrder.getDosingInstructions().getAdministrationInstructions(), newDrugOrder.getDosingInstructions().getAdministrationInstructions()) && - areValuesEqual(existingOrder.getDosingInstructions().getAsNeeded(), newDrugOrder.getDosingInstructions().getAsNeeded()) && - areValuesEqual(existingOrder.getDateStopped(), newDrugOrder.getDateStopped()) && - getDateDifferenceInDays(existingOrder.getEffectiveStopDate(), newDrugOrder.getEffectiveStartDate()) <= 1.0 ; - } } diff --git a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/BahmniVisitServiceImpl.java b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/BahmniVisitServiceImpl.java index 1c33d8a05e..10ce41fca5 100644 --- a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/BahmniVisitServiceImpl.java +++ b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/BahmniVisitServiceImpl.java @@ -3,7 +3,6 @@ import org.bahmni.module.bahmnicore.dao.VisitDao; import org.bahmni.module.bahmnicore.service.BahmniVisitService; import org.openmrs.Encounter; -import org.openmrs.Location; import org.openmrs.Visit; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -35,10 +34,4 @@ public List getAdmitAndDischargeEncounters(Integer visitId) { return visitDao.getAdmitAndDischargeEncounters(visitId); } - @Override - public Location getParentLocationForVisit(Location location) { - if(location.getParentLocation()!=null) - getParentLocationForVisit(location.getParentLocation()); - return location; - } } diff --git a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/SMSServiceImpl.java b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/SMSServiceImpl.java index e1ff5fba14..ffa56726ca 100644 --- a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/SMSServiceImpl.java +++ b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/SMSServiceImpl.java @@ -10,6 +10,7 @@ import org.bahmni.module.bahmnicore.contract.SMS.SMSRequest; import org.bahmni.module.bahmnicore.service.BahmniDrugOrderService; import org.bahmni.module.bahmnicore.service.SMSService; +import org.openmrs.Location; import org.openmrs.Patient; import org.openmrs.api.context.Context; @@ -17,6 +18,7 @@ import java.text.MessageFormat; import java.util.Date; +import java.util.List; import java.util.Locale; import static org.bahmni.module.bahmnicore.util.BahmniDateUtil.convertUTCToGivenFormat; @@ -60,15 +62,15 @@ public Object sendSMS(String phoneNumber, String message) { } @Override - public String getPrescriptionMessage(String lang, Date visitDate, Patient patient, String location, String providerDetail, String prescriptionDetail) { - String smsTimeZone = Context.getMessageSourceService().getMessage(SMS_TIMEZONE, null, new Locale(lang)); - String smsDateFormat = Context.getMessageSourceService().getMessage(SMS_DATEFORMAT, null, new Locale(lang)); + public String getPrescriptionMessage(Locale locale, Date visitDate, Patient patient, Location location, List providerList, String prescriptionDetail) { + String smsTimeZone = Context.getMessageSourceService().getMessage(SMS_TIMEZONE, null, locale); + String smsDateFormat = Context.getMessageSourceService().getMessage(SMS_DATEFORMAT, null, locale); String smsTemplate = Context.getAdministrationService().getGlobalProperty(PRESCRIPTION_SMS_TEMPLATE); Object[] arguments = {convertUTCToGivenFormat(visitDate, smsDateFormat, smsTimeZone), patient.getGivenName() + " " + patient.getFamilyName(), patient.getGender(), patient.getAge().toString(), - providerDetail, location, prescriptionDetail}; + org.springframework.util.StringUtils.collectionToCommaDelimitedString(providerList), location.getName(), prescriptionDetail}; if (StringUtils.isBlank(smsTemplate)) { - return Context.getMessageSourceService().getMessage(PRESCRIPTION_SMS_TEMPLATE, arguments, new Locale(lang)).replace("\\n", System.lineSeparator()); + return Context.getMessageSourceService().getMessage(PRESCRIPTION_SMS_TEMPLATE, arguments, locale).replace("\\n", System.lineSeparator()); } else { return new MessageFormat(smsTemplate).format(arguments).replace("\\n", System.lineSeparator()); } diff --git a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/SharePrescriptionServiceImpl.java b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/SharePrescriptionServiceImpl.java index 74b8db5723..82ee776c23 100644 --- a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/SharePrescriptionServiceImpl.java +++ b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/SharePrescriptionServiceImpl.java @@ -7,17 +7,27 @@ import org.bahmni.module.bahmnicore.service.BahmniVisitService; import org.bahmni.module.bahmnicore.service.SMSService; import org.bahmni.module.bahmnicore.service.SharePrescriptionService; +import org.openmrs.Location; import org.openmrs.Visit; import org.openmrs.annotation.Authorized; +import org.openmrs.api.context.Context; import org.openmrs.module.bahmniemrapi.drugorder.contract.BahmniDrugOrder; +import org.openmrs.module.emrapi.encounter.domain.EncounterTransaction; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import org.springframework.util.StringUtils; import java.util.List; -import java.util.Locale; +import java.util.ArrayList; +import java.util.Set; import java.util.Map; +import java.util.Date; +import java.util.Locale; +import java.util.LinkedHashMap; +import java.util.LinkedHashSet; +import java.util.concurrent.TimeUnit; + +import static org.bahmni.module.bahmnicore.util.BahmniDateUtil.convertUTCToGivenFormat; @Service public class SharePrescriptionServiceImpl implements SharePrescriptionService { @@ -26,6 +36,9 @@ public class SharePrescriptionServiceImpl implements SharePrescriptionService { private BahmniDrugOrderService drugOrderService; private SMSService smsService; + private final static String SMS_TIMEZONE = "bahmni.sms.timezone"; + private final static String SMS_DATEFORMAT = "bahmni.sms.dateformat"; + @Autowired public SharePrescriptionServiceImpl(BahmniVisitService bahmniVisitService, BahmniDrugOrderService drugOrderService, SMSService smsService) { this.bahmniVisitService = bahmniVisitService; @@ -38,13 +51,109 @@ public SharePrescriptionServiceImpl(BahmniVisitService bahmniVisitService, Bahmn @Authorized({"Send Prescription"}) public Object sendPresciptionSMS(PrescriptionSMS prescription) { Visit visit = bahmniVisitService.getVisitSummary(prescription.getVisitUuid()); - String locationName = bahmniVisitService.getParentLocationForVisit(visit.getLocation()).getName(); + Location location = getParentLocationForVisit(visit.getLocation()); List drugOrderList = drugOrderService.getBahmniDrugOrdersForVisit(visit.getPatient().getUuid(), visit.getUuid()); - Map mergedDrugOrderMap = drugOrderService.getMergedDrugOrderMap(drugOrderList); - String providerString = StringUtils.collectionToCommaDelimitedString(drugOrderService.getUniqueProviderNames(drugOrderList)); - String prescriptionString = drugOrderService.getPrescriptionAsString(mergedDrugOrderMap, new Locale(prescription.getLocale())); - String prescriptionSMSContent = smsService.getPrescriptionMessage(prescription.getLocale(), visit.getStartDatetime(), visit.getPatient(), locationName, providerString, prescriptionString); + List uniqueProviderList = getUniqueProviderNames(drugOrderList); + String prescriptionString = getPrescriptionAsString(drugOrderList, new Locale(prescription.getLocale())); + String prescriptionSMSContent = smsService.getPrescriptionMessage(new Locale(prescription.getLocale()), visit.getStartDatetime(), visit.getPatient(), + location, uniqueProviderList, prescriptionString); return smsService.sendSMS(visit.getPatient().getAttribute("phoneNumber").getValue(), prescriptionSMSContent); } + private Location getParentLocationForVisit(Location location) { + if (location.getParentLocation() != null && isVisitLocation(location.getParentLocation())) { + return getParentLocationForVisit(location.getParentLocation()); + } else { + return location; + } + } + + private Boolean isVisitLocation(Location location) { + return (location.getTags().stream().filter(tag -> tag.getName().equalsIgnoreCase("Visit Location")) != null); + } + + private Map getMergedDrugOrderMap(List drugOrderList) { + Map mergedDrugOrderMap = new LinkedHashMap<>(); + for(BahmniDrugOrder drugOrder : drugOrderList) { + BahmniDrugOrder foundDrugOrder = mergedDrugOrderMap.entrySet().stream() + .map(x -> x.getKey()) + .filter( existingOrder -> + compareDrugOrders(existingOrder, drugOrder) ) + .findFirst() + .orElse(null); + if (foundDrugOrder!=null) { + String durationWithUnits = mergedDrugOrderMap.get(foundDrugOrder); + if(drugOrder.getDurationUnits() == foundDrugOrder.getDurationUnits()) + durationWithUnits = (foundDrugOrder.getDuration()+drugOrder.getDuration()) + " " + drugOrder.getDurationUnits(); + else + durationWithUnits += " + " + drugOrder.getDuration() + " " + drugOrder.getDurationUnits(); + mergedDrugOrderMap.put(foundDrugOrder, durationWithUnits); + } else { + mergedDrugOrderMap.put(drugOrder, drugOrder.getDuration()+" "+drugOrder.getDurationUnits()); + } + } + return mergedDrugOrderMap; + } + + private boolean compareDrugOrders(BahmniDrugOrder existingOrder, BahmniDrugOrder newDrugOrder) { + return (existingOrder.getDrug()!=null && newDrugOrder.getDrug()!=null && + areValuesEqual(existingOrder.getDrug().getUuid(), newDrugOrder.getDrug().getUuid())) && + areValuesEqual(existingOrder.getDrugNonCoded(), newDrugOrder.getDrugNonCoded()) && + areValuesEqual(existingOrder.getInstructions(), newDrugOrder.getInstructions()) && + compareDosingInstruction(existingOrder.getDosingInstructions(), newDrugOrder.getDosingInstructions()) && + areValuesEqual(existingOrder.getDosingInstructions().getRoute(), newDrugOrder.getDosingInstructions().getRoute()) && + areValuesEqual(existingOrder.getDosingInstructions().getAdministrationInstructions(), newDrugOrder.getDosingInstructions().getAdministrationInstructions()) && + areValuesEqual(existingOrder.getDosingInstructions().getAsNeeded(), newDrugOrder.getDosingInstructions().getAsNeeded()) && + areValuesEqual(existingOrder.getDateStopped(), newDrugOrder.getDateStopped()) && + getDateDifferenceInDays(existingOrder.getEffectiveStopDate(), newDrugOrder.getEffectiveStartDate()) <= 1.0 ; + } + + private Boolean areValuesEqual(Object value1, Object value2) { + if(value1 != null && value2 != null) { + return value1.equals(value2); + } + return (value1 == null && value2 == null); + }; + + private Boolean compareDosingInstruction(EncounterTransaction.DosingInstructions value1, EncounterTransaction.DosingInstructions value2) { + String doseAndFrequency1 = value1.getDose() + " " + value1.getDoseUnits() + ", " + value1.getFrequency(); + String doseAndFrequency2 = value2.getDose() + " " + value2.getDoseUnits() + ", " + value2.getFrequency(); + return doseAndFrequency1.equals(doseAndFrequency2); + }; + + private double getDateDifferenceInDays(Date date1, Date date2){ + long diff = date2.getTime() - date1.getTime(); + return TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS); + } + + public String getPrescriptionAsString(List drugOrders, Locale locale) { + Map drugOrderDurationMap = getMergedDrugOrderMap(drugOrders); + String prescriptionString = ""; + int counter = 1; + for (Map.Entry entry : drugOrderDurationMap.entrySet()) { + prescriptionString += counter++ + ". " + getDrugOrderAsString(entry.getKey(), drugOrderDurationMap.get(entry.getKey()), locale) + "\n"; + } + return prescriptionString; + } + + public List getUniqueProviderNames(List drugOrders) { + Set providerSet = new LinkedHashSet(); + for (BahmniDrugOrder drugOrder : drugOrders) { + providerSet.add("Dr " + drugOrder.getProvider().getName()); + } + return new ArrayList<>(providerSet); + } + + private String getDrugOrderAsString(BahmniDrugOrder drugOrder, String duration, Locale locale) { + String drugOrderString = drugOrder.getDrug().getName(); + drugOrderString += ", " + (drugOrder.getDosingInstructions().getDose().intValue()) + " " + drugOrder.getDosingInstructions().getDoseUnits(); + drugOrderString += ", " + drugOrder.getDosingInstructions().getFrequency() + "-" + duration; + drugOrderString += ", start from " + convertUTCToGivenFormat(drugOrder.getEffectiveStartDate(), + Context.getMessageSourceService().getMessage(SMS_DATEFORMAT, null, locale), Context.getMessageSourceService().getMessage(SMS_TIMEZONE, null, locale)); + if(drugOrder.getDateStopped() != null) + drugOrderString += ", stopped on " + convertUTCToGivenFormat(drugOrder.getDateStopped(), + Context.getMessageSourceService().getMessage(SMS_DATEFORMAT, null, locale), Context.getMessageSourceService().getMessage(SMS_TIMEZONE, null, locale)); + return drugOrderString; + } + } diff --git a/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/service/impl/BahmniDrugOrderServiceImplTest.java b/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/service/impl/BahmniDrugOrderServiceImplTest.java index 88e9a6282c..bbfd6ff5c1 100644 --- a/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/service/impl/BahmniDrugOrderServiceImplTest.java +++ b/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/service/impl/BahmniDrugOrderServiceImplTest.java @@ -1,11 +1,9 @@ package org.bahmni.module.bahmnicore.service.impl; -import org.apache.commons.lang3.time.DateUtils; import org.bahmni.module.bahmnicore.dao.OrderDao; import org.bahmni.module.bahmnicore.service.BahmniProgramWorkflowService; import org.junit.Before; import org.junit.Test; -import org.junit.runner.RunWith; import org.mockito.ArgumentCaptor; import org.mockito.InjectMocks; import org.mockito.Mock; @@ -17,25 +15,14 @@ import org.openmrs.Patient; import org.openmrs.api.OrderService; import org.openmrs.api.PatientService; -import org.openmrs.api.context.Context; -import org.openmrs.messagesource.MessageSourceService; import org.openmrs.module.bahmniemrapi.drugorder.contract.BahmniDrugOrder; -import org.openmrs.module.emrapi.encounter.domain.EncounterTransaction; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; -import org.springframework.util.StringUtils; import java.text.ParseException; -import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.HashSet; import java.util.List; -import java.util.Map; -import java.util.LinkedHashMap; -import java.util.Locale; -import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Matchers.any; @@ -47,10 +34,7 @@ import static org.mockito.Mockito.verifyNoMoreInteractions; import static org.mockito.Mockito.when; import static org.mockito.MockitoAnnotations.initMocks; -import static org.powermock.api.mockito.PowerMockito.mockStatic; -@RunWith(PowerMockRunner.class) -@PrepareForTest({Context.class}) public class BahmniDrugOrderServiceImplTest { public static final String PATIENT_PROGRAM_UUID = "patient-program-uuid"; @@ -64,8 +48,6 @@ public class BahmniDrugOrderServiceImplTest { OrderService orderService; @Mock OrderDao orderDao; - @Mock - MessageSourceService messageSourceService; @InjectMocks BahmniDrugOrderServiceImpl bahmniDrugOrderService; @@ -100,7 +82,7 @@ public void shouldGetActiveDrugOrdersOfAPatientProgram() throws ParseException { when(orderDao.getActiveOrders(any(Patient.class), any(OrderType.class), any(CareSetting.class), dateArgumentCaptor.capture(), anySet(), eq(null), eq(null), eq(null), anyCollection())).thenReturn(new ArrayList()); - bahmniDrugOrderService.getDrugOrders(PATIENT_UUID, true, conceptsToFilter, null, PATIENT_PROGRAM_UUID); + bahmniDrugOrderService.getDrugOrders(PATIENT_UUID, true, conceptsToFilter, null, PATIENT_PROGRAM_UUID); final Date value = dateArgumentCaptor.getValue(); verify(orderDao).getActiveOrders(mockPatient, mockOrderType, mockCareSetting, value, conceptsToFilter, null, null, null, encounters); @@ -131,105 +113,4 @@ public void shouldNotConsiderEncountersToFetchDrugOrdersIfPatientProgramUuidIsNu verify(orderDao).getAllOrders(mockPatient, mockOrderType,conceptsToFilter, null, encounters); verifyNoMoreInteractions(bahmniProgramWorkflowService); } - - @Test - public void shouldReturnMergedDrugOrderAsMap() throws Exception { - List bahmniDrugOrderList = buildBahmniDrugOrderList(); - Map mergedDrugOrderMap = bahmniDrugOrderService.getMergedDrugOrderMap(bahmniDrugOrderList); - Map expectedMergedDrugOrderMap = new LinkedHashMap<>(); - expectedMergedDrugOrderMap.put(bahmniDrugOrderList.get(0), "10 Days"); - expectedMergedDrugOrderMap.put(bahmniDrugOrderList.get(2), "3 Days"); - assertEquals(expectedMergedDrugOrderMap, mergedDrugOrderMap); - } - - @Test - public void shouldReturnPrescriptionAsString() throws Exception { - mockStatic(Context.class); - when(Context.getMessageSourceService()).thenReturn(messageSourceService); - when(messageSourceService.getMessage("bahmni.sms.timezone", null, new Locale("en"))).thenReturn("IST"); - when(messageSourceService.getMessage("bahmni.sms.dateformat", null, new Locale("en"))).thenReturn("dd-MM-yyyy"); - Date drugOrderStartDate = new SimpleDateFormat("MMMM d, yyyy", new Locale("en")).parse("January 30, 2023"); - EncounterTransaction.DrugOrder etDrugOrder = createETDrugOrder("1", "Paracetamol", 2.0, "Once a day", drugOrderStartDate, 5); - BahmniDrugOrder bahmniDrugOrder = createBahmniDrugOrder(null, etDrugOrder); - Map drugOrderDurationMap = new LinkedHashMap<>(); - drugOrderDurationMap.put(bahmniDrugOrder, "10 Days"); - String prescriptionString = bahmniDrugOrderService.getPrescriptionAsString(drugOrderDurationMap, new Locale("en")); - String expectedPrescriptionString = "1. Paracetamol, 2 tab (s), Once a day-10 Days, start from 30-01-2023\n"; - assertEquals(expectedPrescriptionString, prescriptionString); - } - - @Test - public void shouldReturnAllUniqueProviderNames() throws Exception { - List bahmniDrugOrderList = buildBahmniDrugOrderList(); - String providerString = StringUtils.collectionToCommaDelimitedString(bahmniDrugOrderService.getUniqueProviderNames(bahmniDrugOrderList)); - String expectedProviderString = "Dr Harry,Dr Grace"; - assertEquals(expectedProviderString, providerString); - } - - private List buildBahmniDrugOrderList() { - List bahmniDrugOrderList = new ArrayList<>(); - try { - EncounterTransaction.Provider provider = createETProvider("1", "Harry"); - Date drugOrderStartDate = new SimpleDateFormat("MMMM d, yyyy", new Locale("en")).parse("January 30, 2023"); - EncounterTransaction.DrugOrder etDrugOrder = createETDrugOrder("1", "Paracetamol", 2.0, "Once a day", drugOrderStartDate, 5); - BahmniDrugOrder bahmniDrugOrder = createBahmniDrugOrder(provider, etDrugOrder); - bahmniDrugOrderList.add(bahmniDrugOrder); - - provider = createETProvider("2", "Grace"); - drugOrderStartDate = DateUtils.addDays(drugOrderStartDate, 5); - etDrugOrder = createETDrugOrder("1", "Paracetamol", 2.0, "Once a day", drugOrderStartDate, 5); - bahmniDrugOrder = createBahmniDrugOrder(provider, etDrugOrder); - bahmniDrugOrderList.add(bahmniDrugOrder); - - provider = createETProvider("1", "Harry"); - drugOrderStartDate = new SimpleDateFormat("MMMM d, yyyy", new Locale("en")).parse("January 30, 2023"); - etDrugOrder = createETDrugOrder("2", "Amoxicillin", 1.0, "Twice a day", drugOrderStartDate, 3); - bahmniDrugOrder = createBahmniDrugOrder(provider, etDrugOrder); - bahmniDrugOrderList.add(bahmniDrugOrder); - } catch (ParseException e) { - e.printStackTrace(); - } - return bahmniDrugOrderList; - } - - private BahmniDrugOrder createBahmniDrugOrder(EncounterTransaction.Provider provider, EncounterTransaction.DrugOrder etDrugOrder) { - BahmniDrugOrder bahmniDrugOrder = new BahmniDrugOrder(); - bahmniDrugOrder.setDrugOrder(etDrugOrder); - bahmniDrugOrder.setProvider(provider); - return bahmniDrugOrder; - } - - private EncounterTransaction.Provider createETProvider(String uuid, String name) { - EncounterTransaction.Provider provider = new EncounterTransaction.Provider(); - provider.setUuid(uuid); - provider.setName(name); - return provider; - } - - private EncounterTransaction.DrugOrder createETDrugOrder(String drugUuid, String drugName, Double dose, String frequency, Date effectiveStartDate, Integer duration) { - EncounterTransaction.Drug encounterTransactionDrug = new EncounterTransaction.Drug(); - encounterTransactionDrug.setUuid(drugUuid); - encounterTransactionDrug.setName(drugName); - - EncounterTransaction.DosingInstructions dosingInstructions = new EncounterTransaction.DosingInstructions(); - dosingInstructions.setAdministrationInstructions("{\"instructions\":\"As directed\"}"); - dosingInstructions.setAsNeeded(false); - dosingInstructions.setDose(dose); - dosingInstructions.setDoseUnits("tab (s)"); - dosingInstructions.setFrequency(frequency); - dosingInstructions.setNumberOfRefills(0); - dosingInstructions.setRoute("UNKNOWN"); - - EncounterTransaction.DrugOrder drugOrder = new EncounterTransaction.DrugOrder(); - drugOrder.setOrderType("Drug Order"); - drugOrder.setDrug(encounterTransactionDrug); - drugOrder.setDosingInstructions(dosingInstructions); - drugOrder.setDuration(duration); - drugOrder.setDurationUnits("Days"); - drugOrder.setEffectiveStartDate(effectiveStartDate); - drugOrder.setEffectiveStopDate(DateUtils.addDays(effectiveStartDate, duration)); - drugOrder.setVoided(false); - - return drugOrder; - } -} +} \ No newline at end of file diff --git a/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/service/impl/SMSServiceImplIT.java b/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/service/impl/SMSServiceImplIT.java index 4e7617de25..ea5b6edd6f 100644 --- a/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/service/impl/SMSServiceImplIT.java +++ b/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/service/impl/SMSServiceImplIT.java @@ -8,6 +8,7 @@ import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.mockito.Mock; +import org.openmrs.Location; import org.openmrs.Visit; import org.openmrs.Person; import org.openmrs.api.AdministrationService; @@ -19,6 +20,7 @@ import java.text.ParseException; import java.text.SimpleDateFormat; +import java.util.Arrays; import java.util.Date; import java.util.Locale; @@ -62,8 +64,10 @@ public void shouldReturnPrescriptionSMSWithGlobalPropertyTemplate() throws Parse person.setGender("M"); person.setBirthdate(birthDate); Visit visit = new VisitBuilder().withPerson(person).withUUID("vuuid").withStartDatetime(visitDate).build(); + Location location = new Location(); + location.setName("Bahmni"); - String prescriptionContent = smsService.getPrescriptionMessage("en", visit.getStartDatetime(), visit.getPatient(), "Bahmni", "Superman", "1. Paracetamol 150 mg/ml, 50 ml, Immediately-1 Days, start from 31-01-2023"); + String prescriptionContent = smsService.getPrescriptionMessage(new Locale("en"), visit.getStartDatetime(), visit.getPatient(), location, Arrays.asList("Superman"), "1. Paracetamol 150 mg/ml, 50 ml, Immediately-1 Days, start from 31-01-2023"); String expectedPrescriptionContent = "Date: 30-01-2023\n" + "Prescription For Patient: testPersonName null, M, 13 years.\n" + "Doctor: Superman (Bahmni)\n" + @@ -84,8 +88,10 @@ public void shouldReturnPrescriptionSMSWithDefaultTemplate() throws ParseExcepti person.setGender("M"); person.setBirthdate(birthDate); Visit visit = new VisitBuilder().withPerson(person).withUUID("vuuid").withStartDatetime(visitDate).build(); + Location location = new Location(); + location.setName("Bahmni"); - String prescriptionContent = smsService.getPrescriptionMessage("en", visit.getStartDatetime(), visit.getPatient(), "Bahmni", "Superman", "1. Paracetamol 150 mg/ml, 50 ml, Immediately-1 Days, start from 31-01-2023"); + String prescriptionContent = smsService.getPrescriptionMessage(new Locale("en"), visit.getStartDatetime(), visit.getPatient(), location, Arrays.asList("Superman"), "1. Paracetamol 150 mg/ml, 50 ml, Immediately-1 Days, start from 31-01-2023"); String expectedPrescriptionContent = "Date: 30-01-2023\n" + "Prescription For Patient: testPersonName null, M, 13 years.\n" + "Doctor: Superman (Bahmni)\n" + diff --git a/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/service/impl/SharePrescriptionServiceImplIT.java b/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/service/impl/SharePrescriptionServiceImplIT.java index c6f7ed2ba4..6292b73e09 100644 --- a/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/service/impl/SharePrescriptionServiceImplIT.java +++ b/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/service/impl/SharePrescriptionServiceImplIT.java @@ -1,5 +1,6 @@ package org.bahmni.module.bahmnicore.service.impl; +import org.apache.commons.lang3.time.DateUtils; import org.bahmni.module.bahmnicore.contract.SMS.PrescriptionSMS; import org.bahmni.module.bahmnicore.service.BahmniDrugOrderService; import org.bahmni.module.bahmnicore.service.BahmniVisitService; @@ -21,21 +22,26 @@ import org.openmrs.api.context.Context; import org.openmrs.messagesource.MessageSourceService; import org.openmrs.module.bahmniemrapi.drugorder.contract.BahmniDrugOrder; +import org.openmrs.module.emrapi.encounter.domain.EncounterTransaction; import org.powermock.core.classloader.annotations.PowerMockIgnore; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; +import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.Locale; import java.util.stream.Collectors; import java.util.stream.Stream; +import java.util.Arrays; +import java.util.List; import static org.mockito.Mockito.when; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.times; import static org.mockito.MockitoAnnotations.initMocks; +import static org.powermock.api.mockito.PowerMockito.mockStatic; @RunWith(PowerMockRunner.class) @PrepareForTest({Context.class}) @@ -58,6 +64,9 @@ public class SharePrescriptionServiceImplIT { @Before public void setUp() throws Exception { initMocks(this); + mockStatic(Context.class); + when(Context.getAdministrationService()).thenReturn(administrationService); + when(Context.getMessageSourceService()).thenReturn(messageSourceService); } @Test @@ -68,20 +77,17 @@ public void shouldCallSendSMSForSendingPrescriptionSMS() throws Exception { Visit visit = createVisitForTest(); String sampleSMSContent = "Date: 30-01-2023\n" + "Prescription For Patient: testPersonName null, M, 13 years.\n" + - "Doctor: Superman (Bahmni)\n" + - "1. Paracetamol 150 mg/ml, 50 ml, Immediately-1 Days, start from 31-01-2023"; + "Doctor: Dr Harry (Bahmni)\n" + + "1. Paracetamol, 2 tab (s), Once a day-5 Days, start from 30-01-2023\n"; when(administrationService.getGlobalProperty("bahmni.prescriptionSMSTemplate")).thenReturn("Date: {0}\nPrescription For Patient: {1}, {2}, {3} years.\nDoctor: {4} ({5})\n{6}"); when(messageSourceService.getMessage("bahmni.sms.timezone", null, new Locale("en"))).thenReturn("IST"); when(messageSourceService.getMessage("bahmni.sms.dateformat", null, new Locale("en"))).thenReturn("dd-MM-yyyy"); when(messageSourceService.getMessage("bahmni.sms.url", null, new Locale("en"))).thenReturn(null); when(bahmniVisitService.getVisitSummary(prescriptionSMS.getVisitUuid())).thenReturn(visit); - when(bahmniVisitService.getParentLocationForVisit(visit.getLocation())).thenReturn(visit.getLocation()); - when(bahmniDrugOrderService.getMergedDrugOrderMap(new ArrayList())).thenReturn(null); - when(bahmniDrugOrderService.getUniqueProviderNames(new ArrayList())).thenReturn(Stream.of("Superman").collect(Collectors.toSet())); - when(bahmniDrugOrderService.getPrescriptionAsString(null, new Locale("en"))).thenReturn("1. Paracetamol 150 mg/ml, 50 ml, Immediately-1 Days, start from 31-01-2023"); - when(smsService.getPrescriptionMessage(prescriptionSMS.getLocale(), visit.getStartDatetime(), visit.getPatient(), - "Bahmni", "Superman", "1. Paracetamol 150 mg/ml, 50 ml, Immediately-1 Days, start from 31-01-2023")) + when(bahmniDrugOrderService.getBahmniDrugOrdersForVisit(visit.getPatient().getUuid(), visit.getUuid())).thenReturn(buildBahmniDrugOrderList()); + when(smsService.getPrescriptionMessage(new Locale(prescriptionSMS.getLocale()), visit.getStartDatetime(), visit.getPatient(), + visit.getLocation(), Arrays.asList("Dr Harry"), "1. Paracetamol, 2 tab (s), Once a day-5 Days, start from 30-01-2023\n")) .thenReturn(sampleSMSContent); sharePrescriptionService.sendPresciptionSMS(prescriptionSMS); verify(smsService, times(1)).sendSMS("+919999999999", sampleSMSContent); @@ -108,4 +114,59 @@ private Visit createVisitForTest() throws Exception { return visit; } + private List buildBahmniDrugOrderList() { + List bahmniDrugOrderList = new ArrayList<>(); + try { + EncounterTransaction.Provider provider = createETProvider("1", "Harry"); + Date drugOrderStartDate = new SimpleDateFormat("MMMM d, yyyy", new Locale("en")).parse("January 30, 2023"); + EncounterTransaction.DrugOrder etDrugOrder = createETDrugOrder("1", "Paracetamol", 2.0, "Once a day", drugOrderStartDate, 5); + BahmniDrugOrder bahmniDrugOrder = createBahmniDrugOrder(provider, etDrugOrder); + bahmniDrugOrderList.add(bahmniDrugOrder); + } catch (ParseException e) { + e.printStackTrace(); + } + return bahmniDrugOrderList; + } + + private BahmniDrugOrder createBahmniDrugOrder(EncounterTransaction.Provider provider, EncounterTransaction.DrugOrder etDrugOrder) { + BahmniDrugOrder bahmniDrugOrder = new BahmniDrugOrder(); + bahmniDrugOrder.setDrugOrder(etDrugOrder); + bahmniDrugOrder.setProvider(provider); + return bahmniDrugOrder; + } + + private EncounterTransaction.Provider createETProvider(String uuid, String name) { + EncounterTransaction.Provider provider = new EncounterTransaction.Provider(); + provider.setUuid(uuid); + provider.setName(name); + return provider; + } + + private EncounterTransaction.DrugOrder createETDrugOrder(String drugUuid, String drugName, Double dose, String frequency, Date effectiveStartDate, Integer duration) { + EncounterTransaction.Drug encounterTransactionDrug = new EncounterTransaction.Drug(); + encounterTransactionDrug.setUuid(drugUuid); + encounterTransactionDrug.setName(drugName); + + EncounterTransaction.DosingInstructions dosingInstructions = new EncounterTransaction.DosingInstructions(); + dosingInstructions.setAdministrationInstructions("{\"instructions\":\"As directed\"}"); + dosingInstructions.setAsNeeded(false); + dosingInstructions.setDose(dose); + dosingInstructions.setDoseUnits("tab (s)"); + dosingInstructions.setFrequency(frequency); + dosingInstructions.setNumberOfRefills(0); + dosingInstructions.setRoute("UNKNOWN"); + + EncounterTransaction.DrugOrder drugOrder = new EncounterTransaction.DrugOrder(); + drugOrder.setOrderType("Drug Order"); + drugOrder.setDrug(encounterTransactionDrug); + drugOrder.setDosingInstructions(dosingInstructions); + drugOrder.setDuration(duration); + drugOrder.setDurationUnits("Days"); + drugOrder.setEffectiveStartDate(effectiveStartDate); + drugOrder.setEffectiveStopDate(DateUtils.addDays(effectiveStartDate, duration)); + drugOrder.setVoided(false); + + return drugOrder; + } + }