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-3454 | Refactor. search concepts in both user and default locale #249

Merged
merged 2 commits into from
Jan 23, 2024
Merged
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
Expand Up @@ -4,6 +4,7 @@
import org.openmrs.ConceptDatatype;
import org.openmrs.ConceptSearchResult;
import org.openmrs.api.ConceptService;
import org.openmrs.api.context.Context;
import org.openmrs.module.webservices.rest.web.RequestContext;
import org.openmrs.module.webservices.rest.web.RestConstants;
import org.openmrs.module.webservices.rest.web.resource.api.PageableResult;
Expand All @@ -12,13 +13,15 @@
import org.openmrs.module.webservices.rest.web.resource.api.SearchQuery;
import org.openmrs.module.webservices.rest.web.resource.impl.NeedsPaging;
import org.openmrs.module.webservices.rest.web.response.ResponseException;
import org.openmrs.util.LocaleUtility;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

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

@Component
public class BahmniConceptSearchByDataTypeHandler implements SearchHandler {
Expand Down Expand Up @@ -54,8 +57,10 @@ public PageableResult search(RequestContext context) throws ResponseException {

List<Concept> concepts = new ArrayList<>();

List<Locale> localeList = getLocales(context);

List<ConceptSearchResult> conceptsByName =
conceptService.getConcepts(conceptName, null, false, null, null, conceptDatatypes, null, null, context.getStartIndex(), context.getLimit());
conceptService.getConcepts(conceptName, localeList, false, null, null, conceptDatatypes, null, null, context.getStartIndex(), context.getLimit());

for (ConceptSearchResult csr : conceptsByName) {
if (csr.getConcept() != null)
Expand All @@ -66,4 +71,21 @@ public PageableResult search(RequestContext context) throws ResponseException {

}

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

List<Locale> localeList = new ArrayList<>();

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

return localeList;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

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

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
Expand Down Expand Up @@ -68,12 +69,15 @@ public void shouldDelegateSearchOfConceptsToConceptService() {
ConceptDatatype conceptDatatype = new ConceptDatatype();
conceptDatatype.setId(1);
conceptDatatypes.add(conceptDatatype);
List<Locale> localeList = new ArrayList<>();
localeList.add(Locale.ENGLISH);
when(conceptService.getConceptDatatypeByName(DATA_TYPES)).thenReturn(conceptDatatype);
when(conceptService.getConcepts(NAME, null, false, null, null, conceptDatatypes,
when(conceptService.getConcepts(NAME, localeList, false, null, null, conceptDatatypes,
null, null, 0, 10)).thenReturn(conceptSearchResults);

when(requestContext.getParameter("name")).thenReturn(NAME);
when(requestContext.getParameter("dataTypes")).thenReturn(DATA_TYPES);
when(requestContext.getParameter("locale")).thenReturn(Locale.ENGLISH.toString());
when(requestContext.getLimit()).thenReturn(10);

NeedsPaging<Concept> searchResults = (NeedsPaging<Concept>) bahmniConceptSearchByDataTypeHandler.search(requestContext);
Expand Down
Loading