- 
                Notifications
    You must be signed in to change notification settings 
- Fork 546
RESTWS-927: Extend encounter search endpoint to filter by encounter forms and obs concepts #596
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
          
     Draft
      
      
            jecihjoy
  wants to merge
  5
  commits into
  openmrs:master
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
jecihjoy:extend-encounter-search
  
      
      
   
  
    
  
  
  
 
  
      
    base: master
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
  
     Draft
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            5 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      380af4b
              
                 Add ability to search encounters by forms and obs concepts
              
              
                jecihjoy b9496bd
              
                Tests - wip
              
              
                jecihjoy 4f2997b
              
                Fixed issue with jaxb
              
              
                PatrickWaweru 2aa2bbf
              
                Merge pull request #1 from patnoxozone/fixMLIssues
              
              
                jecihjoy 6a78736
              
                allow extension of order functionalities
              
              
                jecihjoy File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
        
          
          
            114 changes: 114 additions & 0 deletions
          
          114 
        
  ...openmrs/module/webservices/rest/web/v1_0/search/openmrs2_4/EncounterSearchHandler2_4.java
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| /** | ||
| * This Source Code Form is subject to the terms of the Mozilla Public License, | ||
| * v. 2.0. If a copy of the MPL was not distributed with this file, You can | ||
| * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under | ||
| * the terms of the Healthcare Disclaimer located at http://openmrs.org/license. | ||
| * | ||
| * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS | ||
| * graphic logo is a trademark of OpenMRS Inc. | ||
| */ | ||
| package org.openmrs.module.webservices.rest.web.v1_0.search.openmrs2_4; | ||
|  | ||
| import com.google.common.base.Strings; | ||
| import org.openmrs.Encounter; | ||
| import org.openmrs.EncounterType; | ||
| import org.openmrs.Form; | ||
| import org.openmrs.Obs; | ||
| import org.openmrs.Patient; | ||
| import org.openmrs.api.FormService; | ||
| import org.openmrs.api.context.Context; | ||
| import org.openmrs.module.webservices.rest.web.ConversionUtil; | ||
| import org.openmrs.module.webservices.rest.web.RequestContext; | ||
| import org.openmrs.module.webservices.rest.web.RestConstants; | ||
| import org.openmrs.module.webservices.rest.web.api.RestService; | ||
| import org.openmrs.module.webservices.rest.web.resource.api.PageableResult; | ||
| import org.openmrs.module.webservices.rest.web.resource.api.SearchConfig; | ||
| import org.openmrs.module.webservices.rest.web.resource.api.SearchHandler; | ||
| import org.openmrs.module.webservices.rest.web.resource.api.SearchQuery; | ||
| import org.openmrs.module.webservices.rest.web.resource.impl.EmptySearchResult; | ||
| import org.openmrs.module.webservices.rest.web.resource.impl.NeedsPaging; | ||
| import org.openmrs.module.webservices.rest.web.response.ResponseException; | ||
| import org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs1_8.EncounterTypeResource1_8; | ||
| import org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs1_8.PatientResource1_8; | ||
| import org.openmrs.parameter.EncounterSearchCriteria; | ||
| import org.openmrs.parameter.EncounterSearchCriteriaBuilder; | ||
| import org.springframework.stereotype.Component; | ||
|  | ||
| import java.util.*; | ||
| import java.util.stream.Collectors; | ||
|  | ||
| @Component | ||
| public class EncounterSearchHandler2_4 implements SearchHandler { | ||
|  | ||
| private static final String DATE_FROM = "fromdate"; | ||
|  | ||
| private static final String DATE_TO = "todate"; | ||
| private static final String ENCOUNTER_FORMS = "formUuid"; | ||
| private static final String OBS_CONCEPTS = "conceptUuid"; | ||
|  | ||
| private final SearchConfig searchConfig = new SearchConfig("byEncounterForms", RestConstants.VERSION_1 + "/encounter", | ||
| Collections.singletonList("2.4.* - 9.*"), | ||
| Collections.singletonList(new SearchQuery.Builder( | ||
| "Allows you to find Encounter by patient and encounterType (and optionally by encounter forms, obs concepts, from and to date range)") | ||
| .withRequiredParameters("patient").withOptionalParameters("encounterType", DATE_FROM, DATE_TO, ENCOUNTER_FORMS, OBS_CONCEPTS, "order") | ||
| .build())); | ||
|  | ||
| @Override | ||
| public SearchConfig getSearchConfig() { | ||
| return this.searchConfig; | ||
| } | ||
|  | ||
| @Override | ||
| public PageableResult search(RequestContext context) throws ResponseException { | ||
| String patientUuid = context.getRequest().getParameter("patient"); | ||
| String encounterTypeUuid = context.getRequest().getParameter("encounterType"); | ||
|  | ||
| String dateFrom = context.getRequest().getParameter(DATE_FROM); | ||
| String dateTo = context.getRequest().getParameter(DATE_TO); | ||
|  | ||
| String forms = context.getRequest().getParameter(ENCOUNTER_FORMS); | ||
| String concepts = context.getRequest().getParameter(OBS_CONCEPTS); | ||
|  | ||
| Date fromDate = dateFrom != null ? (Date) ConversionUtil.convert(dateFrom, Date.class) : null; | ||
| Date toDate = dateTo != null ? (Date) ConversionUtil.convert(dateTo, Date.class) : null; | ||
|  | ||
| Patient patient = ((PatientResource1_8) Context.getService(RestService.class).getResourceBySupportedClass( | ||
| Patient.class)).getByUniqueId(patientUuid); | ||
| EncounterType encounterType = ((EncounterTypeResource1_8) Context.getService(RestService.class) | ||
| .getResourceBySupportedClass(EncounterType.class)).getByUniqueId(encounterTypeUuid); | ||
|  | ||
| List<Form> formList = new ArrayList<>(); | ||
| if (!Strings.isNullOrEmpty(forms)) { | ||
| FormService formService = Context.getFormService(); | ||
| Arrays.stream(forms.split(",")).map(formService::getFormByUuid).filter(Objects::nonNull).forEach(formList::add); | ||
| } | ||
|  | ||
| List<String> conceptUuidList = Strings.isNullOrEmpty(concepts) ? new ArrayList<>() : Arrays.asList(concepts.split(",")); | ||
|  | ||
| if (patient != null) { | ||
| EncounterSearchCriteriaBuilder encounterSearchCriteriaBuilder = new EncounterSearchCriteriaBuilder() | ||
| .setPatient(patient).setFromDate(fromDate).setToDate(toDate).setIncludeVoided(false); | ||
| if (encounterType != null) { | ||
| encounterSearchCriteriaBuilder.setEncounterTypes(Collections.singletonList(encounterType)); | ||
| } | ||
| encounterSearchCriteriaBuilder.setEnteredViaForms(formList); | ||
|  | ||
| EncounterSearchCriteria encounterSearchCriteria = encounterSearchCriteriaBuilder.createEncounterSearchCriteria(); | ||
|  | ||
| List<Encounter> encounters = Context.getEncounterService().getEncounters(encounterSearchCriteria); | ||
| encounters.forEach(encounter -> { | ||
| if (!conceptUuidList.isEmpty()) { | ||
| List<Obs> obs = encounter.getObs().stream().filter(ob -> conceptUuidList.contains(ob.getConcept().getUuid())).collect(Collectors.toList()); | ||
| encounter.setObs(new HashSet<>(obs)); | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't modify the encounter object directly. This is very dangerous since it could result in an actual update to the database. | ||
| } | ||
| }); | ||
|  | ||
| String order = context.getRequest().getParameter("order"); | ||
| if ("desc".equals(order)) { | ||
| Collections.reverse(encounters); | ||
| } | ||
| return new NeedsPaging<Encounter>(encounters, context); | ||
| } | ||
| return new EmptySearchResult(); | ||
| } | ||
| } | ||
        
          
          
            56 changes: 56 additions & 0 deletions
          
          56 
        
  ...va/org/openmrs/module/webservices/rest/web/v1_0/search/EncounterSearchHandlerTest2_4.java
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| /** | ||
| * This Source Code Form is subject to the terms of the Mozilla Public License, | ||
| * v. 2.0. If a copy of the MPL was not distributed with this file, You can | ||
| * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under | ||
| * the terms of the Healthcare Disclaimer located at http://openmrs.org/license. | ||
| * | ||
| * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS | ||
| * graphic logo is a trademark of OpenMRS Inc. | ||
| */ | ||
| package org.openmrs.module.webservices.rest.web.v1_0.search; | ||
|  | ||
| import org.junit.Assert; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
| import org.openmrs.Encounter; | ||
| import org.openmrs.api.EncounterService; | ||
| import org.openmrs.api.LocationService; | ||
| import org.openmrs.api.context.Context; | ||
| import org.openmrs.module.webservices.rest.SimpleObject; | ||
| import org.openmrs.module.webservices.rest.web.RestTestConstants1_8; | ||
| import org.openmrs.module.webservices.rest.web.RestTestConstants1_9; | ||
| import org.openmrs.module.webservices.rest.web.v1_0.controller.RestControllerTestUtils; | ||
| import org.springframework.mock.web.MockHttpServletRequest; | ||
| import org.springframework.web.bind.annotation.RequestMethod; | ||
|  | ||
| import java.util.List; | ||
|  | ||
| public class EncounterSearchHandlerTest2_4 extends RestControllerTestUtils { | ||
|  | ||
| private static final String ENCOUNTER_TEST_INITIAL_XML = "encounterTestDataset.xml"; | ||
|  | ||
| @Before | ||
| public void init() throws Exception { | ||
| EncounterService service = Context.getEncounterService(); | ||
| executeDataSet(ENCOUNTER_TEST_INITIAL_XML); | ||
| } | ||
|  | ||
| /** | ||
| * @see org.openmrs.module.webservices.rest.web.v1_0.controller.MainResourceControllerTest#getURI() | ||
| */ | ||
|  | ||
| protected String getURI() { | ||
| return "encounter"; | ||
| } | ||
|  | ||
| @Test | ||
| public void searchEncounter_shouldReturnEncounterForAPatient() throws Exception { | ||
| MockHttpServletRequest req = request(RequestMethod.GET, getURI()); | ||
| req.addParameter("s", "byEncounterForms"); | ||
| req.addParameter("patient", "41c6b35e-c093-11e3-be87-005056821db0"); | ||
|  | ||
| SimpleObject result = deserialize(handle(req)); | ||
| List<Object> hits = result.get("results"); | ||
| Assert.assertEquals(3, hits.size()); | ||
| } | ||
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| <?xml version='1.0' encoding='UTF-8'?> | ||
| <dataset> | ||
| <!--patient one--> | ||
| <person person_id="100" gender="M" dead="false" creator="1" date_created="2005-01-01 00:00:00.0" voided="false" uuid="41c6b35e-c093-11e3-be87-005056821db0"/> | ||
| <patient patient_id="100" creator="1" date_created="2005-01-01 00:00:00.0" voided="false"/> | ||
| <person_name person_name_id="100" preferred="true" person_id="100" given_name="John" middle_name=" " family_name="Doe" creator="1" date_created="2005-01-01 00:00:00.0" voided="false" uuid="48076422-c093-11e3-be87-005056821db0"/> | ||
|  | ||
| <!--patient two--> | ||
| <patient patient_id="101" creator="1" date_created="2005-01-01 00:00:00.0" voided="false"/> | ||
| <person person_id="101" gender="M" dead="false" creator="1" date_created="2005-01-01 00:00:00.0" voided="false" uuid="52f87f42-c093-11e3-be87-005056821db0"/> | ||
| <person_name person_name_id="101" preferred="true" person_id="101" given_name="John" middle_name=" " family_name="Doe" creator="1" date_created="2005-01-01 00:00:00.0" voided="false" uuid="57ead7cc-c093-11e3-be87-005056821db0"/> | ||
|  | ||
| <encounter_type encounter_type_id="100" name="sample encounter a" description="sample encounter a" creator="1" date_created="2005-01-01 00:00:00.0" retired="false" uuid="ff7397ea-c090-11e3-be87-005056821db0"/> | ||
| <encounter_type encounter_type_id="101" name="sample encounter b" description="sample encounter b" creator="1" date_created="2005-01-01 00:00:00.0" retired="false" uuid="07d68e11-c091-11e3-be87-005056821db0"/> | ||
|  | ||
| <encounter encounter_id="2000" encounter_type="100" patient_id="100" location_id="1" encounter_datetime="2008-08-01 00:00:00.0" creator="1" date_created="2008-08-18 14:09:05.0" voided="false" void_reason="" uuid="62967e68-96bb-11e0-8d6b-9b9415a91465" /> | ||
| <!-- obs group --> | ||
| <obs obs_id="2000" person_id="100" encounter_id="2000" concept_id="23" obs_datetime="2008-08-01 00:00:00.0" creator="1" date_created="2008-08-18 14:09:05.0" voided="false" uuid="47f18998-96cc-11e0-8d6b-9b9415a91465" /> | ||
| <!-- containing two obs --> | ||
| <obs obs_id="2001" person_id="100" encounter_id="2000" obs_group_id="2000" concept_id="19" value_text="Some text" obs_datetime="2008-08-01 00:00:00.0" creator="1" date_created="2008-08-18 14:09:05.0" voided="false" uuid="5117f5d4-96cc-11e0-8d6b-9b9415a91465" /> | ||
| <obs obs_id="2002" person_id="100" encounter_id="2000" obs_group_id="2000" concept_id="20" value_datetime="2011-06-12 00:00:00.0" obs_datetime="2008-08-01 00:00:00.0" creator="1" date_created="2008-08-18 14:09:05.0" voided="false" uuid="565f39c6-96cc-11e0-8d6b-9b9415a91465" /> | ||
|  | ||
| <encounter encounter_id="2001" encounter_type="101" patient_id="100" location_id="1" encounter_datetime="2008-08-01 00:00:00.0" creator="1" date_created="2008-08-18 14:09:05.0" voided="false" void_reason="" uuid="63ca2dce-c091-11e3-be87-005056821db0" /> | ||
| <!-- obs group --> | ||
| <obs obs_id="2003" person_id="100" encounter_id="2001" concept_id="23" obs_datetime="2008-08-01 00:00:00.0" creator="1" date_created="2008-08-18 14:09:05.0" voided="false" uuid="7ef6b093-c092-11e3-be87-005056821db0" /> | ||
| <!-- containing two obs --> | ||
| <obs obs_id="2004" person_id="100" encounter_id="2001" obs_group_id="2003" concept_id="19" value_text="Some text" obs_datetime="2008-08-01 00:00:00.0" creator="1" date_created="2008-08-18 14:09:05.0" voided="false" uuid="8c6d6d78-c091-11e3-be87-005056821db0" /> | ||
| <obs obs_id="2005" person_id="100" encounter_id="2001" obs_group_id="2003" concept_id="20" value_datetime="2011-06-12 00:00:00.0" obs_datetime="2008-08-01 00:00:00.0" creator="1" date_created="2008-08-18 14:09:05.0" voided="false" uuid="9107afcf-c091-11e3-be87-005056821db0" /> | ||
|  | ||
| <encounter encounter_id="2002" encounter_type="101" patient_id="101" location_id="1" encounter_datetime="2008-08-01 00:00:00.0" creator="1" date_created="2008-08-18 14:09:05.0" voided="false" void_reason="" uuid="8d4561fe-c092-11e3-be87-005056821db0" /> | ||
| <!-- obs group --> | ||
| <obs obs_id="2004" person_id="101" encounter_id="2002" concept_id="23" obs_datetime="2008-08-01 00:00:00.0" creator="1" date_created="2008-08-18 14:09:05.0" voided="false" uuid="94d1f65b-c092-11e3-be87-005056821db0" /> | ||
| <!-- containing two obs --> | ||
| <obs obs_id="2005" person_id="101" encounter_id="2002" obs_group_id="2004" concept_id="19" value_text="Some text" obs_datetime="2008-08-01 00:00:00.0" creator="1" date_created="2008-08-18 14:09:05.0" voided="false" uuid="99479024-c092-11e3-be87-005056821db0" /> | ||
| <obs obs_id="2006" person_id="101" encounter_id="2002" obs_group_id="2004" concept_id="20" value_datetime="2011-06-12 00:00:00.0" obs_datetime="2008-08-01 00:00:00.0" creator="1" date_created="2008-08-18 14:09:05.0" voided="false" uuid="9e0c23de-c092-11e3-be87-005056821db0" /> | ||
| </dataset> | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -1,6 +1,4 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <!DOCTYPE module PUBLIC "-//OpenMRS//DTD OpenMRS Config 1.0//EN" "http://resources.openmrs.org/doctype/config-1.2.dtd"> | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please don't remove this | ||
|  | ||
| <module configVersion="1.2"> | ||
|  | ||
| <!-- Base Module Properties --> | ||
|  | ||
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be done in the EncounterSearchHandler1_9. We only use a newer version if the query cannot be supported on older versions.
I also don't think that the support for extracting only certain obs by concepts is not a good idea for something that's applied to the search on Encounter itself. If we need that, that needs to be separated into a different query.