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

RESTWS-927: Extend encounter search endpoint to filter by encounter forms and obs concepts #596

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion omod-1.8/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
<type>pom</type>
<version>${openmrs.version.1.8}</version><!--$NO-MVN-MAN-VER$-->
</dependency>
</dependencies>
</dependencies>

<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import org.openmrs.module.webservices.rest.web.resource.impl.DelegatingResourceDescription;
import org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs1_10.OrderResource1_10;

@Resource(name = RestConstants.VERSION_1 + "/order", supportedClass = Order.class, supportedOpenmrsVersions = { "2.2.* - 9.*" })
@Resource(name = RestConstants.VERSION_1 + "/order", supportedClass = Order.class, supportedOpenmrsVersions = { "2.2.* - 2.5.*" })
public class OrderResource2_2 extends OrderResource1_10 {

/**
Expand Down
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 {
Copy link
Member

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.


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));
Copy link
Member

Choose a reason for hiding this comment

The 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();
}
}
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());
}
}
36 changes: 36 additions & 0 deletions omod-2.4/src/test/resources/encounterTestDataset.xml
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>
2 changes: 0 additions & 2 deletions omod/src/main/resources/config.xml
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">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't remove this


<module configVersion="1.2">

<!-- Base Module Properties -->
Expand Down
Loading