Skip to content

Commit

Permalink
BAH 3407|Fix of locale issue in the visit search handler (#244)
Browse files Browse the repository at this point in the history
* BAH-3407|Fix locale issue in search handler

* BAH-3407|Add null check for concept list

* BAH-3407| Fix VisitFormsSearchHandlerTest test cases

* BAH-3407 | Refactor. used conceptsByName method and fix tests

* BAH-3407| Refactor. Usage of locale in VisitFormsSearchHandler

* BAH-3407| Refactor. Usage of locale in VisitFormsSearchHandler and test

* BAH-3407| Refactor. Use of searchLocale or default.

* BAH-3407 | Refactor. get concept name in default locale if null in user locale

* BAH-3407 | Extracted identify locale method to separate class

* BAH-3407 | Fix. test failures

* BAH-3407| Add tests for LocaleResolver class.

* BAH-3407 | Add. test case for searching concept in default locale if match not found in user locale

* BAH-3407| Refactor. Use of static identify locale method.

* BAH-3407| Add check for concept to choose correct locale

* BAH-3407| Fix VisitFormsSearchHandlerTest

* BAH-3407 | Refactor. pass false instead of null

* BAH-3407 | Fix. test failures

---------

Co-authored-by: SanoferSameera <[email protected]>
  • Loading branch information
poojadeshpande01 and SanoferSameera authored Jan 10, 2024
1 parent d0b60bb commit 21f340a
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
import org.openmrs.api.ConceptService;
import org.openmrs.api.ObsService;
import org.openmrs.api.VisitService;
import org.openmrs.api.context.Context;
import org.openmrs.module.bahmniemrapi.encountertransaction.contract.BahmniObservation;
import org.openmrs.module.bahmniemrapi.encountertransaction.mapper.OMRSObsToBahmniObsMapper;
import org.openmrs.util.LocaleUtility;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

Expand Down Expand Up @@ -69,11 +71,20 @@ public Collection<BahmniObservation> observationsFor(String patientUuid, Collect
private List<String> getConceptNames(Collection<Concept> concepts) {
List<String> conceptNames = new ArrayList<>();
for (Concept concept : concepts) {
conceptNames.add(concept.getName().getName());
if(concept != null) {
conceptNames.add(getConceptName(concept, Context.getLocale()));
}
}
return conceptNames;
}

private String getConceptName(Concept concept, Locale searchLocale) {
if (concept.getName(searchLocale) != null)
return concept.getName(searchLocale).getName();
else
return concept.getName(LocaleUtility.getDefaultLocale()).getName();
}

@Override
public Collection<BahmniObservation> observationsFor(String patientUuid, Concept rootConcept, Concept childConcept, Integer numberOfVisits, Date startDate, Date endDate, String patientProgramUuid) {
Collection<Encounter> encounters = programWorkflowService.getEncountersByPatientProgramUuid(patientProgramUuid);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.bahmni.module.bahmnicore.web.v1_0;

import org.openmrs.api.APIException;
import org.openmrs.util.LocaleUtility;

import java.util.Locale;

public class LocaleResolver {

public static Locale identifyLocale(String locale) {
if (locale != null && !locale.isEmpty()) {
Locale searchLocale = LocaleUtility.fromSpecification(locale);
if (searchLocale.getLanguage().isEmpty()) {
throw new APIException("Invalid locale: " + locale);
}
return searchLocale;
} else {
return LocaleUtility.getDefaultLocale();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.bahmni.module.bahmnicore.obs.ObservationsAdder;
import org.bahmni.module.bahmnicore.service.BahmniObsService;
import org.bahmni.module.bahmnicore.util.MiscUtils;
import org.bahmni.module.bahmnicore.web.v1_0.LocaleResolver;
import org.openmrs.Concept;
import org.openmrs.ConceptSearchResult;
import org.openmrs.Visit;
Expand Down Expand Up @@ -35,6 +36,8 @@
import java.util.Set;
import java.util.stream.Collectors;

import static org.bahmni.module.bahmnicore.web.v1_0.LocaleResolver.identifyLocale;

@Controller
@RequestMapping(value = "/rest/" + RestConstants.VERSION_1 + "/bahmnicore/observations")
public class BahmniObservationsController extends BaseRestController {
Expand Down Expand Up @@ -92,18 +95,6 @@ private List<Concept> searchConceptsByName(List<String> conceptNames, Locale sea
return new ArrayList<>(conceptSet);
}

private Locale identifyLocale(String locale) {
if (locale != null && !locale.isEmpty()) {
Locale searchLocale = LocaleUtility.fromSpecification(locale);
if (searchLocale.getLanguage().isEmpty()) {
throw new APIException("Invalid locale: " + locale);
}
return searchLocale;
} else {
return LocaleUtility.getDefaultLocale();
}
}

@RequestMapping(method = RequestMethod.GET, params = {"visitUuid"})
@ResponseBody
public Collection<BahmniObservation> get(@RequestParam(value = "visitUuid", required = true) String visitUuid,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import org.apache.commons.collections.CollectionUtils;
import org.bahmni.module.bahmnicore.service.BahmniProgramWorkflowService;
import org.bahmni.module.bahmnicore.web.v1_0.LocaleResolver;
import org.openmrs.Concept;
import org.openmrs.Encounter;
import org.openmrs.Obs;
import org.openmrs.Patient;
import org.openmrs.PatientProgram;
import org.openmrs.Visit;
import org.openmrs.api.APIException;
import org.openmrs.api.context.Context;
import org.openmrs.module.episodes.Episode;
import org.openmrs.module.episodes.service.EpisodeService;
Expand All @@ -20,22 +22,22 @@
import org.openmrs.module.webservices.rest.web.resource.impl.NeedsPaging;
import org.openmrs.module.webservices.rest.web.response.InvalidSearchException;
import org.openmrs.module.webservices.rest.web.response.ResponseException;
import org.openmrs.util.LocaleUtility;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Locale;

import static java.util.Arrays.asList;
import static org.bahmni.module.bahmnicore.web.v1_0.LocaleResolver.identifyLocale;

@Component
public class VisitFormsSearchHandler implements SearchHandler {
@Autowired
private EpisodeService episodeService;

private final String ALL_OBSERVATION_TEMPLATES = "All Observation Templates";
private final String QUERY_INFORMATION = "Allows you to search All Observation Templates by patientUuid";

Expand All @@ -52,16 +54,22 @@ public PageableResult search(RequestContext context) throws ResponseException {
String patientProgramUuid = context.getRequest().getParameter("patientProgramUuid");
int numberOfVisits = Integer.parseInt(context.getRequest().getParameter("numberOfVisits"));
String[] conceptNames = context.getRequest().getParameterValues("conceptNames");
Locale searchLocale = identifyLocale(context.getRequest().getSession().getAttribute("locale").toString());

Patient patient = Context.getPatientService().getPatientByUuid(patientUuid);
if (patient == null) {
throw new InvalidSearchException("Patient does not exist.");
}
List<String> conceptNamesList = new ArrayList<>();
if (conceptNames == null) {
conceptNamesList = getConcepts(Context.getConceptService().getConcept(ALL_OBSERVATION_TEMPLATES).getSetMembers());
List<Concept> concepts = Context.getConceptService().getConceptsByName(ALL_OBSERVATION_TEMPLATES, Locale.ENGLISH, false);
if(!concepts.isEmpty()){
for (Concept concept : concepts) {
conceptNamesList = getConcepts(concept.getSetMembers(), searchLocale);
}
}
} else {
conceptNamesList = Arrays.asList(conceptNames);
conceptNamesList = asList(conceptNames);
}

List<Encounter> encounterList;
Expand All @@ -71,12 +79,12 @@ public PageableResult search(RequestContext context) throws ResponseException {
encounterList = getEncountersFor(numberOfVisits, patient);
}

List<Obs> finalObsList = getObservations(patient, conceptNamesList, encounterList);
List<Obs> finalObsList = getObservations(patient, conceptNamesList, encounterList, searchLocale);

return new NeedsPaging<Obs>(finalObsList, context);
}

private List<Obs> getObservations(Patient patient, List<String> conceptNames, List<Encounter> encounterList) {
private List<Obs> getObservations(Patient patient, List<String> conceptNames, List<Encounter> encounterList, Locale searchLocale) {
List<Obs> finalObsList = new ArrayList<>();
if (CollectionUtils.isEmpty(encounterList)) {
return finalObsList;
Expand All @@ -86,8 +94,8 @@ private List<Obs> getObservations(Patient patient, List<String> conceptNames, Li

for (Obs obs : initialObsList) {
String name = null;
if(obs.getConcept()!= null && obs.getConcept().getFullySpecifiedName(Locale.ENGLISH) != null){
name = obs.getConcept().getFullySpecifiedName(Locale.ENGLISH).getName();
if(obs.getConcept()!= null){
name = getConceptName(obs.getConcept(), searchLocale);
}
if (conceptNames.contains(name)) {
finalObsList.add(obs);
Expand All @@ -113,18 +121,23 @@ private List<Encounter> getEncountersWithinProgram(String patientProgramUuid) {
return encounterList;
}

private List<String> getConcepts(List<Concept> concepts) {
private List<String> getConcepts(List<Concept> concepts, Locale searchLocale) {
List<String> conceptNames = new ArrayList<>();
if (concepts == null)
return conceptNames;

for (Concept concept : concepts) {
conceptNames.add(concept.getFullySpecifiedName(Locale.ENGLISH).getName());
conceptNames.add(getConceptName(concept, searchLocale));
}

return conceptNames;
}

private String getConceptName(Concept concept, Locale searchLocale) {
if(concept.getFullySpecifiedName(searchLocale) != null)
return concept.getFullySpecifiedName(searchLocale).getName();
else
return concept.getFullySpecifiedName(LocaleUtility.getDefaultLocale()).getName();
}

private List<Visit> listOfVisitsNeeded(int numberOfVisits, Patient patient) {
List<Visit> visitsByPatient = Context.getVisitService().getVisitsByPatient(patient);
int subsetVisits = numberOfVisits;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.bahmni.module.bahmnicore.web.v1_0;

import static org.bahmni.module.bahmnicore.web.v1_0.LocaleResolver.identifyLocale;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import org.junit.Test;
import org.openmrs.util.LocaleUtility;

import java.util.Locale;

public class LocaleResolverTest {

@Test
public void shouldReturnDefaultLocaleIfNull() {
Locale locale = identifyLocale(null);
assertEquals(LocaleUtility.getDefaultLocale(), locale);
}

@Test
public void shouldReturnDefaultLocaleIfEmpty() {
Locale locale = identifyLocale("");
assertEquals(LocaleUtility.getDefaultLocale(), locale);
}

@Test
public void shouldReturnParsedLocaleIfValid() {
Locale locale = identifyLocale("en_US");
assertEquals(new Locale("en", "US"), locale);
}

@Test(expected = AssertionError.class)
public void shouldThrowExceptionIfInvalidLocale() {
identifyLocale("invalid");
fail("Should have thrown exception");
}

}
Loading

0 comments on commit 21f340a

Please sign in to comment.