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 all 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 @@ -38,4 +38,6 @@ 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
}
Original file line number Diff line number Diff line change
@@ -0,0 +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(Locale locale, Date visitDate, Patient patient, Location location, List<String> providerList, String prescriptionDetail);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method should have less than 3 params


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 @@ -41,6 +43,8 @@
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.Arrays;
import java.util.Comparator;

@Service
public class BahmniDrugOrderServiceImpl implements BahmniDrugOrderService {
Expand All @@ -51,21 +55,21 @@ 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);


@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 +171,31 @@ 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);
}
}

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 +229,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 @@ -221,5 +249,4 @@ private List<DrugOrder> mapOrderToDrugOrder(List<Order> orders){
}
return drugOrders;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ public Visit getVisitSummary(String visitUuid) {
public List<Encounter> getAdmitAndDischargeEncounters(Integer visitId) {
return visitDao.getAdmitAndDischargeEncounters(visitId);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
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;

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;
import org.springframework.stereotype.Service;

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;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;

@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_DATEFORMAT = "bahmni.sms.dateformat";
private final static String SMS_URL = "bahmni.sms.url";

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);
kavitha-sundararajan marked this conversation as resolved.
Show resolved Hide resolved

HttpPost request = new HttpPost(Context.getMessageSourceService().getMessage(SMS_URL, null, new Locale("en")));
request.addHeader("content-type", "application/json");
request.setEntity(params);
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);
throw new RuntimeException("Exception occured in sending sms ", e);
}
}

@Override
public String getPrescriptionMessage(Locale locale, Date visitDate, Patient patient, Location location, List<String> 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(),
org.springframework.util.StringUtils.collectionToCommaDelimitedString(providerList), location.getName(), prescriptionDetail};
if (StringUtils.isBlank(smsTemplate)) {
return Context.getMessageSourceService().getMessage(PRESCRIPTION_SMS_TEMPLATE, arguments, locale).replace("\\n", System.lineSeparator());
} else {
return new MessageFormat(smsTemplate).format(arguments).replace("\\n", System.lineSeparator());
}
}

}
Loading