Skip to content

Commit

Permalink
BAH-3266 | Review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
sivareddyp committed Nov 22, 2023
1 parent 9693e24 commit 722e47f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,14 @@ public static List<Concept> getConceptsForNames(List<String> conceptNames, Conce
return new ArrayList<>();
}
private static Concept getConceptInDefaultLocale(ConceptService conceptService, String rootConceptName) {
if (!LocaleUtility.getDefaultLocale().equals(Context.getLocale())) {
List<Concept> conceptsByName = conceptService.getConceptsByName(rootConceptName, LocaleUtility.getDefaultLocale(), false);
for (Concept concept : conceptsByName) {
for (ConceptName conceptname : concept.getNames()) {
if (conceptname.getName().equalsIgnoreCase(rootConceptName) && (conceptname.isPreferred() || conceptname.isFullySpecifiedName())) {
return concept;
}
if (LocaleUtility.getDefaultLocale().equals(Context.getLocale())) {
return null;
}
List<Concept> conceptsByName = conceptService.getConceptsByName(rootConceptName, LocaleUtility.getDefaultLocale(), false);
for (Concept concept : conceptsByName) {
for (ConceptName conceptname : concept.getNames()) {
if (conceptname.getName().equalsIgnoreCase(rootConceptName) && (conceptname.isPreferred() || conceptname.isFullySpecifiedName())) {
return concept;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ public SearchConfig getSearchConfig() {
return new SearchConfig("byFullySpecifiedName", RestConstants.VERSION_1 + "/concept", Arrays.asList("1.8.* - 2.*"), searchQuery);
}

/**
* Searches for concepts by the given parameters. (Currently only supports name and locale (optional))
* @return a list of concepts matching the given parameters.
* @throws APIException
* <strong>Should</strong> return concepts in the specified locale if specified.
* <strong>Should</strong> return concepts in the default locale as well as logged in locale if locale is not specified.
*/

@Override
public PageableResult search(RequestContext context) throws ResponseException {
String conceptName = context.getParameter("name");
Expand Down Expand Up @@ -68,19 +76,26 @@ public PageableResult search(RequestContext context) throws ResponseException {
}
}

/**
* Returns list of unique locales based on the context.getParameter("locale") parameter
* <strong>Should</strong> return List of results for locales: If locale is specified, then return results for that locale.
* If locale is not specified, then return results for logged in locale and default locale.
*/

private List<Locale> getLocales(RequestContext context) {
String locale = context.getParameter("locale");

List<Locale> localeList = new ArrayList<>();
localeList.add(LocaleUtility.getDefaultLocale());

if (locale != null) {
localeList.add(LocaleUtility.fromSpecification(locale));
} else {
localeList.add(Context.getLocale());
if (!LocaleUtility.getDefaultLocale().equals(Context.getLocale())) {
localeList.add(LocaleUtility.getDefaultLocale());
}
}

localeList = localeList.stream().distinct().collect(Collectors.toList());
return localeList;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.openmrs.ConceptSearchResult;
import org.openmrs.api.ConceptNameType;
import org.openmrs.api.ConceptService;
import org.openmrs.api.context.UserContext;
import org.openmrs.module.webservices.rest.web.RequestContext;
import org.openmrs.module.webservices.rest.web.resource.api.SearchConfig;
import org.openmrs.module.webservices.rest.web.resource.impl.NeedsPaging;
Expand Down Expand Up @@ -43,6 +44,9 @@ public class BahmniConceptSearchHandlerTest {
@Mock
RequestContext requestContext;

@Mock
UserContext userContext;

@InjectMocks
private BahmniConceptSearchHandler bahmniConceptSearchHandler;

Expand All @@ -60,11 +64,11 @@ public void shouldSupportVersions1_8To2() {
}

@Test
public void shouldSearchByGivenLocaleAndDefaultLocale_whenLocaleIsSpecified() {
public void shouldSearchByGivenLocale_whenLocaleIsSpecified() {
List<ConceptSearchResult> conceptSearchResults = new ArrayList<>();
ConceptSearchResult result = new ConceptSearchResult();
Concept concept = new Concept();
concept.setId(10);
concept.setId(123);
ConceptName conceptNameFullySpecified = new ConceptName();
conceptNameFullySpecified.setConceptNameType(ConceptNameType.FULLY_SPECIFIED);
conceptNameFullySpecified.setName("Nutritional Values");
Expand All @@ -73,7 +77,6 @@ public void shouldSearchByGivenLocaleAndDefaultLocale_whenLocaleIsSpecified() {
conceptSearchResults.add(result);

List<Locale> localeList = new ArrayList<>();
localeList.add(LocaleUtility.getDefaultLocale());
localeList.add(Locale.FRENCH);

when(conceptService.getConcepts(anyString(), anyList(), anyBoolean(), isNull(), isNull(), isNull(), isNull(), isNull(), any(Integer.class), isNull())).thenReturn(conceptSearchResults);
Expand All @@ -86,7 +89,8 @@ public void shouldSearchByGivenLocaleAndDefaultLocale_whenLocaleIsSpecified() {

verify(conceptService, times(1)).getConcepts("Nutritional Values", localeList, false, null, null, null, null, null, 0, null);
assertEquals(1, searchResults.getPageOfResults().size());
assertEquals(new Integer(10) , searchResults.getPageOfResults().get(0).getId());
assertEquals(1, localeList.size());
assertEquals(new Integer(123) , searchResults.getPageOfResults().get(0).getId());
}

@Test
Expand Down

0 comments on commit 722e47f

Please sign in to comment.