Skip to content

Commit

Permalink
[BAH-460][Roni/Martina] Add api for search similar patient using Pers…
Browse files Browse the repository at this point in the history
…onService and Lucene
  • Loading branch information
mduemcke committed May 24, 2018
1 parent 140a6db commit 7ee46a5
Show file tree
Hide file tree
Showing 16 changed files with 228 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class PatientSearchParameters {
private Boolean filterPatientsByLocation;
private String identifier;
private String name;
private String gender;
private String addressFieldName;
private String addressFieldValue;
private Integer start;
Expand Down Expand Up @@ -43,6 +44,7 @@ public PatientSearchParameters(RequestContext context) {
} else {
this.setAddressFieldName("city_village");
}
this.setGender(context.getParameter("gender"));
this.setAddressFieldValue(context.getParameter("addressFieldValue"));
Map parameterMap = context.getRequest().getParameterMap();
this.setAddressSearchResultFields((String[]) parameterMap.get("addressSearchResultsConfig"));
Expand Down Expand Up @@ -71,6 +73,14 @@ public void setName(String name) {
this.name = name;
}

public String getGender() {
return gender;
}

public void setGender(String gender) {
this.gender = gender;
}

public String getAddressFieldName() {
return addressFieldName;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.bahmni.module.bahmnicore.contract.patient.mapper;

import java.util.Objects;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang3.StringUtils;
import org.bahmni.module.bahmnicore.contract.patient.response.PatientResponse;
Expand All @@ -17,11 +16,13 @@

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

import static java.util.Arrays.asList;

public class PatientResponseMapper {
private PatientResponse patientResponse;
private VisitService visitService;
Expand All @@ -34,8 +35,8 @@ public PatientResponseMapper(VisitService visitService, BahmniVisitLocationServi
}

public PatientResponse map(Patient patient, String loginLocationUuid, String[] searchResultFields, String[] addressResultFields, Object programAttributeValue) {
List<String> patientSearchResultFields = searchResultFields != null ? Arrays.asList(searchResultFields) : new ArrayList<>();
List<String> addressSearchResultFields = addressResultFields != null ? Arrays.asList(addressResultFields) : new ArrayList<>();
List<String> patientSearchResultFields = searchResultFields != null ? asList(searchResultFields) : new ArrayList<>();
List<String> addressSearchResultFields = addressResultFields != null ? asList(addressResultFields) : new ArrayList<>();

Integer visitLocationId = bahmniVisitLocationService.getVisitLocation(loginLocationUuid).getLocationId();
List<Visit> activeVisitsByPatient = visitService.getActiveVisitsByPatient(patient);
Expand All @@ -51,7 +52,9 @@ public PatientResponse map(Patient patient, String loginLocationUuid, String[] s
patientResponse.setFamilyName(patient.getFamilyName());
patientResponse.setGender(patient.getGender());
PatientIdentifier primaryIdentifier = patient.getPatientIdentifier();
patientResponse.setIdentifier(primaryIdentifier.getIdentifier());
if(primaryIdentifier != null) {
patientResponse.setIdentifier(primaryIdentifier.getIdentifier());
}
patientResponse.setPatientProgramAttributeValue(programAttributeValue);

mapExtraIdentifiers(patient, primaryIdentifier);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ List<PatientResponse> getPatientsUsingLuceneSearch(String identifier, String nam
String programAttributeFieldName, String[] addressSearchResultFields,
String[] patientSearchResultFields, String loginLocationUuid, Boolean filterPatientsByLocation, Boolean filterOnAllIdentifiers);

List<PatientResponse> getSimilarPatientsUsingLuceneSearch(String name, String gender, String loginLocationUuid, Integer length);

public Patient getPatient(String identifier);

public List<Patient> getPatients(String partialIdentifier, boolean shouldMatchExactPatientId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static java.util.Arrays.asList;

@Component
public class OrderDaoImpl implements OrderDao {
private static final String ORDER_TEMPLATES_DIRECTORY = "ordertemplates";
Expand Down Expand Up @@ -364,7 +365,7 @@ public List<Order> getAllOrders(Patient patientByUuid, OrderType drugOrderTypeUu

return getAllOrders(patientByUuid, drugOrderTypeUuid, null, null, encounters);
}
return getAllOrders(patientByUuid, Arrays.asList(drugOrderTypeUuid), offset, limit);
return getAllOrders(patientByUuid, asList(drugOrderTypeUuid), offset, limit);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,26 @@
import org.openmrs.Patient;
import org.openmrs.PatientIdentifier;
import org.openmrs.PatientIdentifierType;
import org.openmrs.Person;
import org.openmrs.PersonName;
import org.openmrs.RelationshipType;
import org.openmrs.api.context.Context;
import org.openmrs.api.db.hibernate.HibernatePatientDAO;
import org.openmrs.api.db.hibernate.PersonLuceneQuery;
import org.openmrs.api.db.hibernate.search.LuceneQuery;
import org.openmrs.module.bahmniemrapi.visitlocation.BahmniVisitLocationServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

import static java.util.stream.Collectors.reducing;
import static java.util.Arrays.asList;
import static java.util.stream.Collectors.toList;

@Repository
Expand Down Expand Up @@ -88,18 +94,60 @@ public List<PatientResponse> getPatientsUsingLuceneSearch(String identifier, Str
List<PatientResponse> patientResponses = patientIdentifiers.stream()
.map(patientIdentifier -> {
Patient patient = patientIdentifier.getPatient();
if(!uniquePatientIds.contains(patient.getPatientId())) {
PatientResponse patientResponse = patientResponseMapper.map(patient, loginLocationUuid, patientSearchResultFields, addressSearchResultFields,
programAttributes.get(patient.getPatientId()));
uniquePatientIds.add(patient.getPatientId());
return patientResponse;
}else
return null;
return toPatientResponse(patientResponseMapper, patient, loginLocationUuid, addressSearchResultFields, patientSearchResultFields, programAttributes, uniquePatientIds);
}).filter(Objects::nonNull)
.collect(toList());
return patientResponses;
}

@Override
public List<PatientResponse> getSimilarPatientsUsingLuceneSearch(String name, String gender, String loginLocationUuid, Integer length) {
PatientResponseMapper patientResponseMapper = new PatientResponseMapper(Context.getVisitService(),new BahmniVisitLocationServiceImpl(Context.getLocationService()));
List<Patient> patients = getPatientsByNameAndGender(name, gender, length);
List<PatientResponse> patientResponses = patients.stream()
.map(patient -> {return patientResponseMapper.map(patient, loginLocationUuid, null, null,null);}).filter(Objects::nonNull)
.collect(toList());
return patientResponses;
}

private PatientResponse toPatientResponse(PatientResponseMapper patientResponseMapper, Patient patient, String loginLocationUuid, String[] addressSearchResultFields, String[] patientSearchResultFields, Map<Object, Object> programAttributes, Set<Integer> uniquePatientIds) {
if(!uniquePatientIds.contains(patient.getPatientId())) {
PatientResponse patientResponse = patientResponseMapper.map(patient, loginLocationUuid, patientSearchResultFields, addressSearchResultFields,
programAttributes.get(patient.getPatientId()));
uniquePatientIds.add(patient.getPatientId());
return patientResponse;
} else {
return null;
}
}

private List<Patient> getPatientsByNameAndGender(String name, String gender, Integer length) {
HibernatePatientDAO patientDAO = new HibernatePatientDAO();
patientDAO.setSessionFactory(sessionFactory);
List<Patient> patients = new ArrayList<Patient>();
String query = LuceneQuery.escapeQuery(name);
PersonLuceneQuery personLuceneQuery = new PersonLuceneQuery(sessionFactory);
LuceneQuery<PersonName> nameQuery = personLuceneQuery.getPatientNameQueryWithOrParser(query, false);
List<PersonName> persons = nameQuery.list().stream()
.filter(
personName ->
personName.getPreferred()
&& checkGender(personName.getPerson(), gender)
).collect(toList());
persons = persons.subList(0, Math.min(length, persons.size()));
persons.forEach(person -> patients.add(new Patient(person.getPerson())));
return patients;
}


private Boolean checkGender(Person person, String gender) {
if(gender != null && !gender.isEmpty()){
return gender.equals(person.getGender());
} else {
return true;
}
}

private List<PatientIdentifier> getPatientIdentifiers(String identifier, Boolean filterOnAllIdentifiers, Integer offset, Integer length) {
FullTextSession fullTextSession = Search.getFullTextSession(sessionFactory.getCurrentSession());
QueryBuilder queryBuilder = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(PatientIdentifier.class).get();
Expand Down Expand Up @@ -179,7 +227,7 @@ private boolean isValidAddressField(String addressFieldName) {
"LOWER (TABLE_NAME) ='person_address' and LOWER(COLUMN_NAME) IN " +
"( :personAddressField)";
Query queryToGetAddressFields = sessionFactory.getCurrentSession().createSQLQuery(query);
queryToGetAddressFields.setParameterList("personAddressField", Arrays.asList(addressFieldName.toLowerCase()));
queryToGetAddressFields.setParameterList("personAddressField", asList(addressFieldName.toLowerCase()));
List list = queryToGetAddressFields.list();
return list.size() > 0;
}
Expand All @@ -201,7 +249,7 @@ private List<Integer> getPersonAttributeIds(String[] patientAttributes) {
String query = "select person_attribute_type_id from person_attribute_type where name in " +
"( :personAttributeTypeNames)";
Query queryToGetAttributeIds = sessionFactory.getCurrentSession().createSQLQuery(query);
queryToGetAttributeIds.setParameterList("personAttributeTypeNames", Arrays.asList(patientAttributes));
queryToGetAttributeIds.setParameterList("personAttributeTypeNames", asList(patientAttributes));
List list = queryToGetAttributeIds.list();
return (List<Integer>) list;
}
Expand Down Expand Up @@ -229,7 +277,7 @@ public List<Patient> getPatients(String patientIdentifier, boolean shouldMatchEx
}

Patient patient = getPatient(patientIdentifier);
List<Patient> result = (patient == null ? new ArrayList<Patient>() : Arrays.asList(patient));
List<Patient> result = (patient == null ? new ArrayList<Patient>() : asList(patient));
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static java.util.Arrays.asList;

@Component
public class EncounterSessionMatcher implements BaseEncounterMatcher {

Expand Down Expand Up @@ -79,7 +80,7 @@ private Encounter findMatchingEncounter(Visit visit, EncounterParameters encount
if (visit.getId() == null) { // To handle new Visit scenario where visit will not be persisted in DB and we get a visit obj (Called from emr-api).
return null;
}
visits = Arrays.asList(visit);
visits = asList(visit);
}

if (null == encounterParameters.getEncounterDateTime()) {
Expand All @@ -90,7 +91,7 @@ private Encounter findMatchingEncounter(Visit visit, EncounterParameters encount
Collection<Encounter> encounters = this.encounterService.getEncounters(encounterParameters.getPatient(), null,
getSearchStartDate(encounterParameters.getEncounterDateTime()),
encounterParameters.getEncounterDateTime(), new ArrayList<Form>(),
Arrays.asList(encounterParameters.getEncounterType()),
asList(encounterParameters.getEncounterType()),
encounterParameters.getProviders(), null, visits, false);

Map<String, Object> context = encounterParameters.getContext();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package org.bahmni.module.bahmnicore.service;

import org.bahmni.module.bahmnicore.contract.patient.PatientSearchParameters;
import org.bahmni.module.bahmnicore.contract.patient.mapper.PatientResponseMapper;
import org.bahmni.module.bahmnicore.contract.patient.response.PatientConfigResponse;
import org.bahmni.module.bahmnicore.contract.patient.response.PatientResponse;
import org.openmrs.Patient;
import org.openmrs.Person;
import org.openmrs.RelationshipType;

import java.util.List;
Expand All @@ -15,6 +17,8 @@ public interface BahmniPatientService {

List<PatientResponse> luceneSearch(PatientSearchParameters searchParameters);

List<PatientResponse> searchSimilarPatients(PatientSearchParameters searchParameters);

public List<Patient> get(String partialIdentifier, boolean shouldMatchExactPatientId);

public List<RelationshipType> getByAIsToB(String aIsToB);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import java.util.Iterator;
import java.util.List;

import static java.util.Arrays.*;

@Component
public class BahmniDiagnosisServiceImpl implements BahmniDiagnosisService {
private EncounterService encounterService;
Expand Down Expand Up @@ -80,8 +82,8 @@ private void voidAllDiagnosisWithSameInitialDiagnosis(String initialVisitDiagnos
private List<Diagnosis> getDiagnosisByPatient(Patient patient, Visit visit) {
List<Diagnosis> diagnoses = new ArrayList<Diagnosis>();

List<Obs> observations = obsService.getObservations(Arrays.asList((Person) patient), new ArrayList<>(visit.getEncounters()),
Arrays.asList(bahmniDiagnosisMetadata.getDiagnosisSetConcept()), null, null, null, Arrays.asList("obsDatetime"),
List<Obs> observations = obsService.getObservations(asList((Person) patient), new ArrayList<>(visit.getEncounters()),
asList(bahmniDiagnosisMetadata.getDiagnosisSetConcept()), null, null, null, asList("obsDatetime"),
null, null, null, null, false);

Collection<Concept> nonDiagnosisConcepts = emrApiProperties.getSuppressedDiagnosisConcepts();
Expand Down Expand Up @@ -150,9 +152,9 @@ public List<BahmniDiagnosisRequest> getBahmniDiagnosisByPatientAndVisit(String p
private Obs getLatestObsGroupBasedOnAnyDiagnosis(Diagnosis diagnosis, Concept bahmniDiagnosisRevised) {
String initialDiagnosisUuid = bahmniDiagnosisMetadata.findInitialDiagnosisUuid(diagnosis.getExistingObs());

List<Obs> observations = obsService.getObservations(Arrays.asList(diagnosis.getExistingObs().getPerson()), null,
Arrays.asList(bahmniDiagnosisRevised),
Arrays.asList(conceptService.getFalseConcept()), null, null, null,
List<Obs> observations = obsService.getObservations(asList(diagnosis.getExistingObs().getPerson()), null,
asList(bahmniDiagnosisRevised),
asList(conceptService.getFalseConcept()), null, null, null,
null, null, null, null, false);

for (Obs obs : observations) {
Expand Down
Loading

0 comments on commit 7ee46a5

Please sign in to comment.