-
Notifications
You must be signed in to change notification settings - Fork 21
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
COH-45: Implement support to fetch patient lists given patient-UUID #23
Draft
corneliouzbett
wants to merge
2
commits into
master
Choose a base branch
from
COH-45
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
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 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 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
44 changes: 44 additions & 0 deletions
44
api/src/main/java/org/openmrs/module/cohort/api/dao/search/CohortSearchHandler.java
This file contains 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,44 @@ | ||
/* | ||
* 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.cohort.api.dao.search; | ||
|
||
import javax.validation.constraints.NotNull; | ||
|
||
import java.util.Collection; | ||
import java.util.Date; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.hibernate.Criteria; | ||
import org.hibernate.criterion.Restrictions; | ||
import org.openmrs.module.cohort.CohortM; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Slf4j | ||
@Component | ||
@SuppressWarnings("unchecked") | ||
public class CohortSearchHandler extends AbstractSearchHandler { | ||
|
||
public Collection<CohortM> findCohortsByMemberships(@NotNull String patientUuid, Date startDate, Date endDate, | ||
boolean includeVoided) { | ||
Criteria criteria = getCurrentSession().createCriteria(CohortM.class, "c"); | ||
//Exclude/include voided cohorts | ||
if (!includeVoided) { | ||
criteria.add(Restrictions.eq("c.voided", false)); | ||
} | ||
if (patientUuid != null) { | ||
criteria.createCriteria("cohortMembers", "cm").createCriteria("cm.patient", "cmp") | ||
.add(Restrictions.eq("cmp.uuid", patientUuid)); | ||
} | ||
//Fixme handle startDate & endDate | ||
|
||
return criteria.list(); | ||
} | ||
|
||
} |
This file contains 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
70 changes: 70 additions & 0 deletions
70
api/src/test/java/org/openmrs/module/cohort/api/dao/search/CohortSearchHandlerTest.java
This file contains 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,70 @@ | ||
/* | ||
* 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.cohort.api.dao.search; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.hasSize; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.hamcrest.Matchers.notNullValue; | ||
|
||
import java.util.Collection; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.openmrs.module.cohort.CohortM; | ||
import org.openmrs.module.cohort.CohortMember; | ||
import org.openmrs.module.cohort.api.SpringTestConfiguration; | ||
import org.openmrs.test.BaseModuleContextSensitiveTest; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.test.context.ContextConfiguration; | ||
|
||
@ContextConfiguration(classes = SpringTestConfiguration.class, inheritLocations = false) | ||
public class CohortSearchHandlerTest extends BaseModuleContextSensitiveTest { | ||
|
||
private static final String[] COHORT_INITIAL_TEST_DATA_XML = { | ||
"org/openmrs/module/cohort/api/hibernate/db/CohortDaoTest_initialTestData.xml", | ||
"org/openmrs/module/cohort/api/hibernate/db/CohortMemberDaoTest_initialTestData.xml" }; | ||
|
||
private static final String PATIENT_UUID = "61b38324-e2fd-4feb-95b7-9e9a2a4400df"; | ||
|
||
private static final String BAD_PATIENT_UUID = "xx78-bad-patient-uuid-9349hj34l"; | ||
|
||
@Autowired | ||
private CohortSearchHandler searchHandler; | ||
|
||
@Before | ||
public void setup() throws Exception { | ||
for (String data : COHORT_INITIAL_TEST_DATA_XML) { | ||
executeDataSet(data); | ||
} | ||
} | ||
|
||
@Test | ||
public void findCohortsByPatientUuid_shouldReturnCollectionOfCohorts() { | ||
Collection<CohortM> cohorts = searchHandler.findCohortsByMemberships(PATIENT_UUID, null, null, false); | ||
|
||
assertThat(cohorts, notNullValue()); | ||
assertThat(cohorts, hasSize(1)); | ||
for (CohortM cohort : cohorts) { | ||
for (CohortMember cohortMember : cohort.getCohortMembers()) { | ||
assertThat(cohortMember.getPatient().getUuid(), is(PATIENT_UUID)); | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
public void findCohortsByBadPatientUuid_shouldReturnEmptyCollection() { | ||
Collection<CohortM> cohorts = searchHandler.findCohortsByMemberships(BAD_PATIENT_UUID, null, null, false); | ||
|
||
assertThat(cohorts, notNullValue()); | ||
assertThat(cohorts.isEmpty(), is(true)); | ||
} | ||
|
||
} |
This file contains 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
81 changes: 81 additions & 0 deletions
81
omod/src/main/java/org/openmrs/module/cohort/web/search/CohortPatientSearchHandler.java
This file contains 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,81 @@ | ||
/* | ||
* 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.cohort.web.search; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Date; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.openmrs.module.cohort.api.CohortService; | ||
import org.openmrs.module.cohort.web.resource.CohortMainRestController; | ||
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.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.NeedsPaging; | ||
import org.openmrs.module.webservices.rest.web.response.ResponseException; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Qualifier; | ||
import org.springframework.stereotype.Component; | ||
|
||
/* | ||
* Allows searching for cohorts by patient fields i.e. UUID, patient-identifier | ||
*/ | ||
@Slf4j | ||
@Component | ||
public class CohortPatientSearchHandler implements SearchHandler { | ||
|
||
@Autowired | ||
@Qualifier("cohort.cohortService") | ||
private CohortService cohortService; | ||
|
||
/** | ||
* @see org.openmrs.module.webservices.rest.web.resource.api.SearchHandler#getSearchConfig() | ||
*/ | ||
@Override | ||
public SearchConfig getSearchConfig() { | ||
return new SearchConfig("default", RestConstants.VERSION_1 + CohortMainRestController.COHORT_NAMESPACE + "/cohort", | ||
Arrays.asList("1.8.*", "1.9.*", "1.10.*", "1.11.*", "1.12.*", "2.0.*", "2.1.*", "2.2.*", "2.3.*", "2.4.*", | ||
"2.5.*"), | ||
Arrays.asList( | ||
new SearchQuery.Builder("Allows you to find cohorts by patient uuid") | ||
.withRequiredParameters("patient.uuid").build(), | ||
new SearchQuery.Builder("Allow you to find cohort by cohort membership period") | ||
.withOptionalParameters("fromStartDate", "toEndDate").build(), | ||
new SearchQuery.Builder("Include/exclude inactive cohorts").withOptionalParameters("includeInactive") | ||
.build())); | ||
} | ||
|
||
/** | ||
* @see org.openmrs.module.webservices.rest.web.resource.api.SearchHandler#search(org.openmrs.module.webservices.rest.web.RequestContext) | ||
*/ | ||
@Override | ||
public PageableResult search(RequestContext context) throws ResponseException { | ||
String patientUuid = context.getParameter("patient.uuid"); | ||
String fromStartDate = context.getParameter("fromStartDate"); | ||
String toEndDate = context.getParameter("toEndDate"); | ||
String includeInactive = context.getParameter("includeInactive"); | ||
|
||
if (patientUuid != null || fromStartDate != null || toEndDate != null || includeInactive != null) { | ||
Date startDate = fromStartDate != null ? (Date) ConversionUtil.convert(fromStartDate, Date.class) : null; | ||
Date endDate = toEndDate != null ? (Date) ConversionUtil.convert(toEndDate, Date.class) : null; | ||
boolean includeVoided = includeInactive != null && Boolean.getBoolean(includeInactive); | ||
|
||
return new NeedsPaging<>( | ||
new ArrayList<>(cohortService.findCohortsByMembership(patientUuid, startDate, endDate, includeVoided)), | ||
context); | ||
} | ||
return new NeedsPaging<>(new ArrayList<>(), context); | ||
} | ||
} |
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.
"1.8.* - 2.*"