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 8 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
6 changes: 6 additions & 0 deletions bahmnicore-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,12 @@
<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>
<scope>provided</scope>
</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> getBahmniDrugOrdersForVisit(String patientUuid, String visitUuid);
kavitha-sundararajan marked this conversation as resolved.
Show resolved Hide resolved

Map<BahmniDrugOrder, String> getMergedDrugOrderMap(List<BahmniDrugOrder> drugOrderList);
kavitha-sundararajan marked this conversation as resolved.
Show resolved Hide resolved

String getPrescriptionAsString(Map<BahmniDrugOrder, String> drugOrderDurationMap, Locale locale);
kavitha-sundararajan marked this conversation as resolved.
Show resolved Hide resolved

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

Location getParentLocationForVisit(Location location);
kavitha-sundararajan marked this conversation as resolved.
Show resolved Hide resolved
}
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(Object[] prescriptionArguments);
kavitha-sundararajan marked this conversation as resolved.
Show resolved Hide resolved

Object sendSMS(String phoneNumber, String message);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.bahmni.module.bahmnicore.service;

import org.bahmni.module.bahmnicore.contract.SMS.PrescriptionSMS;
import org.openmrs.annotation.Authorized;
import org.springframework.transaction.annotation.Transactional;

public interface SharePrescriptionService {
@Transactional(readOnly = true)
@Authorized({"Send Prescription SMS"})
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,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;
Expand All @@ -39,8 +41,16 @@
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 {
Expand All @@ -51,21 +61,23 @@ 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";
private final static String SMS_DATEFORMAT = "bahmni.sms.dateformat";

@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 +179,103 @@ public List<Order> getAllDrugOrders(String patientUuid, String patientProgramUui
return orderDao.getAllOrders(patientByUuid, orderTypeByUuid, conceptsForDrugs, drugConceptsToBeExcluded, encounters);
}

@Override
public List<BahmniDrugOrder> getBahmniDrugOrdersForVisit(String patientUuid, String visitUuid) {
try {
List<DrugOrder> drugOrderList = getPrescribedDrugOrders(Arrays.asList(visitUuid), patientUuid, null,null, null, null, null);
Map<String, DrugOrder> drugOrderMap = getDiscontinuedDrugOrders(drugOrderList);
Collection<BahmniObservation> orderAttributeObs = bahmniObsService.observationsFor(patientUuid, getOrderAttributeConcepts(), null, null, false, null, null, null);
List<BahmniDrugOrder> bahmniDrugOrderList = bahmniDrugOrderMapper.mapToResponse(drugOrderList, orderAttributeObs, drugOrderMap , null);
Collections.sort(bahmniDrugOrderList, new Comparator<BahmniDrugOrder>() {
@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<BahmniDrugOrder, String> getMergedDrugOrderMap(List<BahmniDrugOrder> drugOrderList) {
Map<BahmniDrugOrder, String> 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<BahmniDrugOrder, String> drugOrderDurationMap, Locale locale) {
String prescriptionString = "";
int counter = 1;
for (Map.Entry<BahmniDrugOrder, String> entry : drugOrderDurationMap.entrySet()) {
prescriptionString += counter++ + ". " + getDrugOrderAsString(entry.getKey(), drugOrderDurationMap.get(entry.getKey()), locale) + "\n";
}
return prescriptionString;
}

public Set getUniqueProviderNames(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 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<Concept> getOrderAttributeConcepts() {
Concept orderAttribute = conceptService.getConceptByName(BahmniOrderAttribute.ORDER_ATTRIBUTES_CONCEPT_SET_NAME);
return orderAttribute == null ? Collections.EMPTY_LIST : orderAttribute.getSetMembers();
}

private List<EncounterTransaction.Concept> fetchOrderAttributeConcepts() {
Concept orderAttributesConceptSet = conceptService.getConceptByName(BahmniOrderAttribute.ORDER_ATTRIBUTES_CONCEPT_SET_NAME);
if(orderAttributesConceptSet != null){
Expand Down Expand Up @@ -200,7 +309,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 All @@ -222,4 +330,16 @@ private List<DrugOrder> mapOrderToDrugOrder(List<Order> 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 ;
}
}
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 Location getParentLocationForVisit(Location location) {
if(location.getParentLocation()!=null)
getParentLocationForVisit(location.getParentLocation());
return location;
}
}
Loading