-
Notifications
You must be signed in to change notification settings - Fork 111
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
FM2-649: Implementation of _has parameter for patients #545
Merged
+441
−104
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fa21503
(feat) _has parameter added for patients
icrc-jofrancisco 4c35de8
(test) patient dao tests
icrc-jofrancisco 055c60d
(test) add more patient dao tests
icrc-jofrancisco 827ccdb
(chore) (tests) Update _has parameter in patient provider and tests.
0621277
Merge branch 'master' into feat/has-parameter
icrc-jofrancisco 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,11 +19,16 @@ | |
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.NoSuchElementException; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
import ca.uhn.fhir.rest.param.DateRangeParam; | ||
import ca.uhn.fhir.rest.param.HasAndListParam; | ||
import ca.uhn.fhir.rest.param.StringAndListParam; | ||
import ca.uhn.fhir.rest.param.StringParam; | ||
import ca.uhn.fhir.rest.param.TokenAndListParam; | ||
|
@@ -33,17 +38,24 @@ | |
import org.hibernate.Criteria; | ||
import org.hibernate.criterion.Criterion; | ||
import org.hibernate.sql.JoinType; | ||
import org.openmrs.Cohort; | ||
import org.openmrs.CohortMembership; | ||
import org.openmrs.Patient; | ||
import org.openmrs.PatientIdentifierType; | ||
import org.openmrs.module.fhir2.FhirConstants; | ||
import org.openmrs.module.fhir2.api.dao.FhirGroupDao; | ||
import org.openmrs.module.fhir2.api.dao.FhirPatientDao; | ||
import org.openmrs.module.fhir2.api.search.param.SearchParameterMap; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@Setter(AccessLevel.PACKAGE) | ||
public class FhirPatientDaoImpl extends BasePersonDao<Patient> implements FhirPatientDao { | ||
|
||
@Autowired | ||
private FhirGroupDao groupDao; | ||
|
||
@Override | ||
public Patient getPatientById(@Nonnull Integer id) { | ||
return (Patient) getSessionFactory().getCurrentSession().createCriteria(Patient.class).add(eq("patientId", id)) | ||
|
@@ -111,10 +123,55 @@ protected void setupSearchParams(Criteria criteria, SearchParameterMap theParams | |
case FhirConstants.COMMON_SEARCH_HANDLER: | ||
handleCommonSearchParameters(entry.getValue()).ifPresent(criteria::add); | ||
break; | ||
case FhirConstants.HAS_SEARCH_HANDLER: | ||
entry.getValue().forEach(param -> handleHasAndListParam(criteria, (HasAndListParam) param.getParam())); | ||
break; | ||
} | ||
}); | ||
} | ||
|
||
protected void handleHasAndListParam(Criteria criteria, HasAndListParam hasAndListParam) { | ||
if (hasAndListParam != null) { | ||
List<String> groupIds = new ArrayList<>(); | ||
hasAndListParam.getValuesAsQueryTokens().forEach(hasOrListParam -> { | ||
hasOrListParam.getValuesAsQueryTokens().forEach(hasParam -> { | ||
if (hasParam != null) { | ||
String paramValue = hasParam.getParameterValue(); | ||
switch (hasParam.getTargetResourceType()) { | ||
case FhirConstants.GROUP: | ||
switch (hasParam.getReferenceFieldName()) { | ||
case FhirConstants.INCLUDE_MEMBER_PARAM: | ||
groupIds.add(paramValue); | ||
break; | ||
} | ||
break; | ||
} | ||
} | ||
}); | ||
}); | ||
|
||
if (!groupIds.isEmpty()) { | ||
verifyPatientInGroups(criteria, groupIds); | ||
} | ||
} | ||
} | ||
|
||
private void verifyPatientInGroups(Criteria criteria, List<String> groupIds) { | ||
Set<Integer> patientIds = new HashSet<>(); | ||
groupIds.forEach(groupId -> patientIds.addAll(getGroupMemberIds(groupId))); | ||
|
||
criteria.add(in("patientId", patientIds.isEmpty() ? Collections.emptyList() : patientIds)); | ||
} | ||
|
||
private List<Integer> getGroupMemberIds(String groupId) { | ||
Cohort cohort = groupDao.get(groupId); | ||
if (cohort != null) { | ||
return cohort.getMemberships().stream().map(CohortMembership::getPatientId).collect(Collectors.toList()); | ||
} else { | ||
return Collections.emptyList(); | ||
} | ||
} | ||
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. Can we reformulate this as a subquery? |
||
|
||
private void handlePatientQuery(Criteria criteria, @Nonnull StringAndListParam query) { | ||
if (query == null) { | ||
return; | ||
|
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
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
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
Oops, something went wrong.
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.
Shouldn't we also have some sort of check for the
:id
part mentioned in the PR comments?