Skip to content

Commit

Permalink
attempt to fix some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mherman22 committed Jan 10, 2024
1 parent 2e348f0 commit 8907671
Show file tree
Hide file tree
Showing 11 changed files with 86 additions and 176 deletions.
101 changes: 31 additions & 70 deletions api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/BaseDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
import org.hl7.fhir.r4.model.Patient;
import org.hl7.fhir.r4.model.Practitioner;
import org.hl7.fhir.r4.model.codesystems.AdministrativeGender;
import org.openmrs.Concept;
import org.openmrs.PatientIdentifier;
import org.openmrs.module.fhir2.FhirConstants;
import org.openmrs.module.fhir2.api.search.param.PropParam;
Expand Down Expand Up @@ -647,61 +648,35 @@ protected <T> Optional<Predicate> handleLocationReference(OpenmrsFhirCriteriaCon
protected <T> void handleParticipantReference(OpenmrsFhirCriteriaContext<T> criteriaContext,
ReferenceAndListParam participantReference) {
if (participantReference != null) {
if (lacksAlias(criteriaContext, "ep")) {
return;
}

handleAndListParam(participantReference, participantToken -> {
if (participantToken.getChain() != null) {
switch (participantToken.getChain()) {
case Practitioner.SP_IDENTIFIER:
if (lacksAlias(criteriaContext, "p")) {
criteriaContext.getRoot().join("ep.provider").alias("p");
}
return Optional.of(criteriaContext.getCriteriaBuilder()
.like(criteriaContext.getRoot().get("p.identifier"), participantToken.getValue()));
.like(criteriaContext.getRoot().join("encounterProviders").join("provider").get("identifier"), participantToken.getValue()));
case Practitioner.SP_GIVEN:
if ((lacksAlias(criteriaContext, "pro")
&& (lacksAlias(criteriaContext, "ps") && (lacksAlias(criteriaContext, "pn"))))) {
criteriaContext.getRoot().join("ep.provider").alias("pro");
criteriaContext.getRoot().join("pro.person").alias("ps");
criteriaContext.getRoot().join("ps.names").alias("pn");
}
return Optional.of(criteriaContext.getCriteriaBuilder()
.like(criteriaContext.getRoot().get("pn.givenName"), participantToken.getValue()));
.like(criteriaContext.getRoot().join("encounterProviders").join("provider").join("person").join("names").get("givenName"), participantToken.getValue()));
case Practitioner.SP_FAMILY:
if ((lacksAlias(criteriaContext, "pro")
&& (lacksAlias(criteriaContext, "ps") && (lacksAlias(criteriaContext, "pn"))))) {
criteriaContext.getRoot().join("ep.provider").alias("pro");
criteriaContext.getRoot().join("pro.person").alias("ps");
criteriaContext.getRoot().join("ps.names").alias("pn");
}
return Optional.of(criteriaContext.getCriteriaBuilder()
.like(criteriaContext.getRoot().get("pn.familyName"), participantToken.getValue()));
.like(criteriaContext.getRoot().join("encounterProviders").join("provider").join("person").join("names").get("familyName"), participantToken.getValue()));
case Practitioner.SP_NAME:
if ((lacksAlias(criteriaContext, "pro")
&& (lacksAlias(criteriaContext, "ps") && (lacksAlias(criteriaContext, "pn"))))) {
criteriaContext.getRoot().join("ep.provider").alias("pro");
criteriaContext.getRoot().join("pro.person").alias("ps");
criteriaContext.getRoot().join("ps.names").alias("pn");
}

List<Optional<? extends Predicate>> predicateList = new ArrayList<>();
Path<T> givenName = criteriaContext.getRoot().join("encounterProviders").join("provider").join("person").join("names").get("givenName");
Path<T> middleName = criteriaContext.getRoot().join("encounterProviders").join("provider").join("person").join("names").get("middleName");
Path<T> familyName = criteriaContext.getRoot().join("encounterProviders").join("provider").join("person").join("names").get("familyName");

for (String token : StringUtils.split(participantToken.getValue(), " \t,")) {
predicateList.add(propertyLike(criteriaContext, "pn.givenName", token));
predicateList.add(propertyLike(criteriaContext, "pn.middleName", token));
predicateList.add(propertyLike(criteriaContext, "pn.familyName", token));
predicateList.add(propertyLike(criteriaContext, givenName, token));
predicateList.add(propertyLike(criteriaContext, middleName, token));
predicateList.add(propertyLike(criteriaContext, familyName, token));
}

return Optional.of(criteriaContext.getCriteriaBuilder().or(toCriteriaArray(predicateList)));
}
} else {
if (lacksAlias(criteriaContext, "pro")) {
criteriaContext.getRoot().join("ep.provider").alias("pro");
}
return Optional.of(criteriaContext.getCriteriaBuilder().equal(criteriaContext.getRoot().get("pro.uuid"),
participantToken.getValue()));
return Optional.of(criteriaContext.getCriteriaBuilder().equal(criteriaContext.getRoot()
.join("encounterProviders").join("provider").get("uuid"),participantToken.getValue()));
}

return Optional.empty();
Expand All @@ -714,45 +689,34 @@ protected <T> void handleParticipantReference(OpenmrsFhirCriteriaContext<T> crit
protected <T> void handleProviderReference(OpenmrsFhirCriteriaContext<T> criteriaContext,
ReferenceAndListParam providerReference) {
if (providerReference != null) {
criteriaContext.getCriteriaQuery().select(criteriaContext.getRoot().get("orderer"));
handleAndListParam(providerReference, participantToken -> {
if (participantToken.getChain() != null) {
switch (participantToken.getChain()) {
case Practitioner.SP_IDENTIFIER:
return Optional.of(criteriaContext.getCriteriaBuilder()
.like(criteriaContext.getRoot().get("or.identifier"), participantToken.getValue()));
.like(criteriaContext.getRoot().join("orderer").get("identifier"), participantToken.getValue()));
case Practitioner.SP_GIVEN:
if ((lacksAlias(criteriaContext, "ps") && (lacksAlias(criteriaContext, "pn")))) {
criteriaContext.getRoot().join("or.person").alias("ps");
criteriaContext.getRoot().join("ps.names").alias("pn");
}
return Optional.of(criteriaContext.getCriteriaBuilder()
.like(criteriaContext.getRoot().get("pn.givenName"), participantToken.getValue()));
.like(criteriaContext.getRoot().join("orderer").join("person").join("names").get("givenName"), participantToken.getValue()));
case Practitioner.SP_FAMILY:
if ((lacksAlias(criteriaContext, "ps") && (lacksAlias(criteriaContext, "pn")))) {
criteriaContext.getRoot().join("or.person").alias("ps");
criteriaContext.getRoot().join("ps.names").alias("pn");
}
return Optional.of(criteriaContext.getCriteriaBuilder()
.like(criteriaContext.getRoot().get("pn.familyName"), participantToken.getValue()));
.like(criteriaContext.getRoot().join("orderer").join("person").join("names").get("familyName"), participantToken.getValue()));
case Practitioner.SP_NAME:
if ((lacksAlias(criteriaContext, "ps") && (lacksAlias(criteriaContext, "pn")))) {
criteriaContext.getRoot().join("or.person").alias("ps");
criteriaContext.getRoot().join("ps.names").alias("pn");
}

List<Optional<? extends Predicate>> predicateList = new ArrayList<>();
Path<T> givenName = criteriaContext.getRoot().join("orderer").join("person").join("names").get("givenName");
Path<T> middleName = criteriaContext.getRoot().join("orderer").join("person").join("names").get("middleName");
Path<T> familyName = criteriaContext.getRoot().join("orderer").join("person").join("names").get("familyName");

for (String token : StringUtils.split(participantToken.getValue(), " \t,")) {
predicateList.add(propertyLike(criteriaContext, "pn.givenName", token));
predicateList.add(propertyLike(criteriaContext, "pn.middleName", token));
predicateList.add(propertyLike(criteriaContext, "pn.familyName", token));
predicateList.add(propertyLike(criteriaContext, givenName, token));
predicateList.add(propertyLike(criteriaContext, middleName, token));
predicateList.add(propertyLike(criteriaContext, familyName, token));
}

return Optional.of(criteriaContext.getCriteriaBuilder().or(toCriteriaArray(predicateList)));
}
} else {
return Optional.of(criteriaContext.getCriteriaBuilder().equal(criteriaContext.getRoot().get("ro.uuid"),
return Optional.of(criteriaContext.getCriteriaBuilder().equal(criteriaContext.getRoot().join("orderer").get("uuid"),
participantToken.getValue()));
}

Expand All @@ -762,7 +726,7 @@ protected <T> void handleProviderReference(OpenmrsFhirCriteriaContext<T> criteri
}

protected <T> Optional<Predicate> handleCodeableConcept(OpenmrsFhirCriteriaContext<T> criteriaContext,
TokenAndListParam concepts, @Nonnull String conceptAlias, @Nonnull String conceptMapAlias,
TokenAndListParam concepts, @Nonnull Path<T> tPath, @Nonnull String conceptMapAlias,
@Nonnull String conceptReferenceTermAlias) {
if (concepts == null) {
return Optional.empty();
Expand All @@ -776,17 +740,17 @@ protected <T> Optional<Predicate> handleCodeableConcept(OpenmrsFhirCriteriaConte
criteriaContext
.getCriteriaBuilder().or(
criteriaContext.getCriteriaBuilder()
.in(criteriaContext.getRoot().get(String.format("%s", conceptAlias))
.in(criteriaContext.getRoot().get(String.format("%s", tPath))
.in(criteriaContext.getCriteriaBuilder()
.literal(tokensToParams(tokens).map(NumberUtils::toInt)
.collect(Collectors.toList())))),
criteriaContext.getCriteriaBuilder()
.in(criteriaContext.getRoot().get(String.format("%s", conceptAlias))
.in(criteriaContext.getRoot().get(String.format("%s", tPath))
.in(criteriaContext.getCriteriaBuilder().literal(tokensToList(tokens))))));

} else {
if (lacksAlias(criteriaContext, conceptMapAlias)) {
criteriaContext.getRoot().join(String.format("%s.conceptMappings", conceptAlias)).alias(conceptMapAlias);
criteriaContext.getRoot().join(String.format("%s.conceptMappings", tPath)).alias(conceptMapAlias);
criteriaContext.getRoot().join(String.format("%s.conceptReferenceTerm", conceptMapAlias))
.alias(conceptReferenceTermAlias);
}
Expand Down Expand Up @@ -1151,8 +1115,7 @@ protected <T> Collection<String> paramToProps(OpenmrsFhirCriteriaContext<T> crit
protected <T> String paramToProp(OpenmrsFhirCriteriaContext<T> criteriaContext, @Nonnull String param) {
return null;
}

//TODO: remove old propertyLike

protected <T> Optional<Predicate> propertyLike(OpenmrsFhirCriteriaContext<T> criteriaContext,
@Nonnull String propertyName, String value) {
if (value == null) {
Expand All @@ -1161,9 +1124,8 @@ protected <T> Optional<Predicate> propertyLike(OpenmrsFhirCriteriaContext<T> cri

return propertyLike(criteriaContext, propertyName, new StringParam(value));
}

//new
private <T, Y> Optional<? extends Predicate> propertyLike(OpenmrsFhirCriteriaContext<T> criteriaContext, @Nonnull Path<Y> yPath, String value) {

protected <T, Y> Optional<? extends Predicate> propertyLike(OpenmrsFhirCriteriaContext<T> criteriaContext, @Nonnull Path<Y> yPath, String value) {
if (value == null) {
return Optional.empty();
}
Expand All @@ -1172,7 +1134,7 @@ private <T, Y> Optional<? extends Predicate> propertyLike(OpenmrsFhirCriteriaCon
}

@SuppressWarnings("unchecked")
private <T, Y> Optional<? extends Predicate> propertyLike(OpenmrsFhirCriteriaContext<T> criteriaContext, Path<Y> yPath, StringParam param) {
protected <T, Y> Optional<? extends Predicate> propertyLike(OpenmrsFhirCriteriaContext<T> criteriaContext, Path<Y> yPath, StringParam param) {
if (param == null) {
return Optional.empty();
}
Expand All @@ -1188,8 +1150,7 @@ private <T, Y> Optional<? extends Predicate> propertyLike(OpenmrsFhirCriteriaCon

return Optional.of(likePredicate);
}

//TODO: remove old propertyLike

protected <T> Optional<Predicate> propertyLike(OpenmrsFhirCriteriaContext<T> criteriaContext,
@Nonnull String propertyName, StringParam param) {
if (param == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,19 +90,14 @@ protected void setupSearchParams(OpenmrsFhirCriteriaContext<Allergy> criteriaCon

private void handleManifestation(OpenmrsFhirCriteriaContext<Allergy> criteriaContext, TokenAndListParam code) {
if (code != null) {
criteriaContext.getRoot().join("reactions").alias("r");
criteriaContext.getRoot().join("r.reaction").alias("rc");

handleCodeableConcept(criteriaContext, code, "rc", "rcm", "rcrt").ifPresent(criteriaContext::addPredicate);
handleCodeableConcept(criteriaContext, code, criteriaContext.getRoot().join("reactions").join("reaction"), "rcm", "rcrt").ifPresent(criteriaContext::addPredicate);
criteriaContext.finalizeQuery();
}
}

private void handleAllergen(OpenmrsFhirCriteriaContext<Allergy> criteriaContext, TokenAndListParam code) {
if (code != null) {
criteriaContext.getRoot().join("allergen.codedAllergen").alias("ac");

handleCodeableConcept(criteriaContext, code, "ac", "acm", "acrt").ifPresent(criteriaContext::addPredicate);
handleCodeableConcept(criteriaContext, code, criteriaContext.getRoot().join("allergen").get("codedAllergen"), "acm", "acrt").ifPresent(criteriaContext::addPredicate);
criteriaContext.finalizeQuery();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import javax.annotation.Nonnull;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.Predicate;

import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -103,10 +104,10 @@ protected void setupSearchParams(OpenmrsFhirCriteriaContext<Concept> criteriaCon
});
}

@SuppressWarnings("unchecked")
protected void handleTitle(OpenmrsFhirCriteriaContext<Concept> criteriaContext, StringAndListParam titlePattern) {
criteriaContext.getRoot().join("names").alias("cn");
handleAndListParam(titlePattern, (title) -> propertyLike(criteriaContext, "cn.name", title))
.ifPresent(criteriaContext::addPredicate);
handleAndListParam(titlePattern, (title) -> (Optional<Predicate>) propertyLike(criteriaContext, criteriaContext.getRoot().join("names").get("name"), title))
.ifPresent(criteriaContext::addPredicate);
criteriaContext.finalizeQuery();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@ protected void setupSearchParams(OpenmrsFhirCriteriaContext<Condition> criteriaC

private void handleCode(OpenmrsFhirCriteriaContext<Condition> criteriaContext, TokenAndListParam code) {
if (code != null) {
criteriaContext.getRoot().join("condition.coded").alias("cd");
handleCodeableConcept(criteriaContext, code, "cd", "map", "term").ifPresent(criteriaContext::addPredicate);
handleCodeableConcept(criteriaContext, code, criteriaContext.getRoot().join("condition").get("coded"), "map", "term").ifPresent(criteriaContext::addPredicate);
criteriaContext.finalizeQuery();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,7 @@ protected void setupSearchParams(OpenmrsFhirCriteriaContext<FhirDiagnosticReport
private void handleCodedConcept(OpenmrsFhirCriteriaContext<FhirDiagnosticReport> criteriaContext,
TokenAndListParam code) {
if (code != null) {
if (lacksAlias(criteriaContext, "c")) {
criteriaContext.getRoot().join("code");
}
handleCodeableConcept(criteriaContext, code, "c", "cm", "crt").ifPresent(criteriaContext::addPredicate);
handleCodeableConcept(criteriaContext, code, criteriaContext.getRoot().join("code"), "cm", "crt").ifPresent(criteriaContext::addPredicate);
criteriaContext.finalizeQuery();
}
}
Expand Down
Loading

0 comments on commit 8907671

Please sign in to comment.