Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BAH-2763 | feature for sending prescription in sms #180

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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 {

Expand All @@ -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<PersonName> personNames = new HashSet<>();
personNames.add(personName);
person.setNames(personNames);
return this;
}

public Person build() {
return person;
}
Expand Down
5 changes: 5 additions & 0 deletions bahmnicore-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,11 @@
<groupId>org.openmrs.module</groupId>
<artifactId>emrapi-api-1.12</artifactId>
</dependency>
<dependency>
kavitha-sundararajan marked this conversation as resolved.
Show resolved Hide resolved
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.14</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Locale;

public interface BahmniDrugOrderService {
List<DrugOrder> getActiveDrugOrders(String patientUuid, Date startDate, Date endDate);
Expand All @@ -38,4 +39,13 @@ List<DrugOrder> getInactiveDrugOrders(String patientUuid, Set<Concept> concepts,

List<BahmniDrugOrder> getDrugOrders(String patientUuid, Boolean isActive, Set<Concept> conceptsToFilter, Set<Concept> conceptsToExclude,
String patientProgramUuid) throws ParseException;

List<BahmniDrugOrder> getSortedBahmniDrugOrdersForVisit(String patientUuid, String visitUuid);
kavitha-sundararajan marked this conversation as resolved.
Show resolved Hide resolved

Map<BahmniDrugOrder, Integer> getMergedDrugOrderMap(List<BahmniDrugOrder> drugOrderList);

String getPrescriptionAsString(Map<BahmniDrugOrder, Integer> drugOrderDurationMap, Locale locale);

String getAllProviderAsString(List<BahmniDrugOrder> drugOrders);

}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -11,4 +12,6 @@ public interface BahmniVisitService {
Visit getVisitSummary(String visitUuid);

List<Encounter> getAdmitAndDischargeEncounters(Integer visitId);

String getParentLocationNameForVisit(Location location);
}
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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"})
kavitha-sundararajan marked this conversation as resolved.
Show resolved Hide resolved
Object sendPresciptionSMS(PrescriptionSMS prescription);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.bahmni.module.bahmnicore.contract.drugorder.OrderFrequencyData;
import org.bahmni.module.bahmnicore.dao.OrderDao;
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;
Expand All @@ -25,11 +26,13 @@
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;
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;
Expand All @@ -39,8 +42,15 @@
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 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 {
Expand All @@ -51,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;
}


Expand Down Expand Up @@ -167,6 +178,110 @@ public List<Order> getAllDrugOrders(String patientUuid, String patientProgramUui
return orderDao.getAllOrders(patientByUuid, orderTypeByUuid, conceptsForDrugs, drugConceptsToBeExcluded, encounters);
}

@Override
public List<BahmniDrugOrder> getSortedBahmniDrugOrdersForVisit(String patientUuid, String visitUuid) {
List<DrugOrder> drugOrderList = getPrescribedDrugOrders(Arrays.asList(visitUuid), patientUuid, null,null, null, null, null);
List<BahmniDrugOrder> bahmniDrugOrderList = getBahmniDrugOrdersForVisit(patientUuid, drugOrderList);
Collections.sort(bahmniDrugOrderList, new Comparator<BahmniDrugOrder>() {
@Override
public int compare(BahmniDrugOrder o1, BahmniDrugOrder o2) {
return o1.getEffectiveStartDate().compareTo(o2.getEffectiveStartDate());
}
});
return bahmniDrugOrderList;
}

@Override
public Map<BahmniDrugOrder, Integer> getMergedDrugOrderMap(List<BahmniDrugOrder> drugOrderList) {
kavitha-sundararajan marked this conversation as resolved.
Show resolved Hide resolved
Map<BahmniDrugOrder, Integer> mergedDrugOrderMap = new LinkedHashMap<>();
for(BahmniDrugOrder drugOrder : drugOrderList) {
BahmniDrugOrder foundDrugOrder = mergedDrugOrderMap.entrySet().stream()
.map(x -> x.getKey())
.filter( existingOrder ->
areValuesEqual(existingOrder.getDrugNonCoded(), drugOrder.getDrugNonCoded()) &&
kavitha-sundararajan marked this conversation as resolved.
Show resolved Hide resolved
(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<BahmniDrugOrder, Integer> drugOrderDurationMap, Locale locale) {
String prescriptionString = "";
int counter = 1;
for (Map.Entry<BahmniDrugOrder, Integer> entry : drugOrderDurationMap.entrySet()) {
prescriptionString += counter++ + ". " + getDrugOrderString(entry.getKey(), drugOrderDurationMap.get(entry.getKey()), locale) + "\n";
}
return prescriptionString;
}

@Override
public String getAllProviderAsString(List<BahmniDrugOrder> drugOrders) {
kavitha-sundararajan marked this conversation as resolved.
Show resolved Hide resolved
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, Locale locale) {
kavitha-sundararajan marked this conversation as resolved.
Show resolved Hide resolved
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", Context.getMessageSourceService().getMessage(SMS_TIMEZONE, null, locale));
kavitha-sundararajan marked this conversation as resolved.
Show resolved Hide resolved
if(drugOrder.getDateStopped() != null)
drugOrderString += ", stopped on " + convertUTCToGivenFormat(drugOrder.getDateStopped(), "dd-MM-yyyy", 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 (diff / (1000*60*60*24));
kavitha-sundararajan marked this conversation as resolved.
Show resolved Hide resolved
}

private Collection<Concept> getOrdAttributeConcepts() {
kavitha-sundararajan marked this conversation as resolved.
Show resolved Hide resolved
Concept orderAttribute = conceptService.getConceptByName(BahmniOrderAttribute.ORDER_ATTRIBUTES_CONCEPT_SET_NAME);
return orderAttribute == null ? Collections.EMPTY_LIST : orderAttribute.getSetMembers();
}

private List<BahmniDrugOrder> getBahmniDrugOrdersForVisit(String patientUuid, List<DrugOrder> drugOrders) {
kavitha-sundararajan marked this conversation as resolved.
Show resolved Hide resolved
Map<String, DrugOrder> drugOrderMap = getDiscontinuedDrugOrders(drugOrders);
try {
Collection<BahmniObservation> 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<EncounterTransaction.Concept> fetchOrderAttributeConcepts() {
Concept orderAttributesConceptSet = conceptService.getConceptByName(BahmniOrderAttribute.ORDER_ATTRIBUTES_CONCEPT_SET_NAME);
if(orderAttributesConceptSet != null){
Expand Down Expand Up @@ -200,7 +315,6 @@ private List<OrderFrequencyData> getFrequencies() {
.collect(Collectors.toList());
}


private List<DrugOrder> getActiveDrugOrders(String patientUuid, Date asOfDate, Set<Concept> conceptsToFilter,
Set<Concept> conceptsToExclude, Date startDate, Date endDate, Collection<Encounter> encounters) {
Patient patient = openmrsPatientService.getPatientByUuid(patientUuid);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -33,4 +34,11 @@ public Visit getVisitSummary(String visitUuid) {
public List<Encounter> getAdmitAndDischargeEncounters(Integer visitId) {
return visitDao.getAdmitAndDischargeEncounters(visitId);
}

@Override
public String getParentLocationNameForVisit(Location location) {
kavitha-sundararajan marked this conversation as resolved.
Show resolved Hide resolved
if(location.getParentLocation()!=null)
getParentLocationNameForVisit(location.getParentLocation());
return location.getName();
}
}
Loading