From 8907671400c7f64b0c3425f54ac4f32b5135d3e3 Mon Sep 17 00:00:00 2001 From: mherman22 Date: Wed, 10 Jan 2024 22:52:33 +0300 Subject: [PATCH] attempt to fix some tests --- .../module/fhir2/api/dao/impl/BaseDao.java | 101 ++++++------------ .../impl/FhirAllergyIntoleranceDaoImpl.java | 9 +- .../api/dao/impl/FhirConceptDaoImpl.java | 7 +- .../api/dao/impl/FhirConditionDaoImpl.java | 3 +- .../dao/impl/FhirDiagnosticReportDaoImpl.java | 5 +- .../fhir2/api/dao/impl/FhirGroupDaoImpl.java | 42 +++----- .../api/dao/impl/FhirLocationDaoImpl.java | 12 +-- .../api/dao/impl/FhirMedicationDaoImpl.java | 9 +- .../impl/FhirMedicationRequestDaoImpl.java | 6 +- .../api/dao/impl/FhirObservationDaoImpl.java | 62 ++++------- .../dao/impl/FhirServiceRequestDaoImpl.java | 6 +- 11 files changed, 86 insertions(+), 176 deletions(-) diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/BaseDao.java b/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/BaseDao.java index 84bfda916..23c1b0141 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/BaseDao.java +++ b/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/BaseDao.java @@ -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; @@ -647,61 +648,35 @@ protected Optional handleLocationReference(OpenmrsFhirCriteriaCon protected void handleParticipantReference(OpenmrsFhirCriteriaContext 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> predicateList = new ArrayList<>(); + Path givenName = criteriaContext.getRoot().join("encounterProviders").join("provider").join("person").join("names").get("givenName"); + Path middleName = criteriaContext.getRoot().join("encounterProviders").join("provider").join("person").join("names").get("middleName"); + Path 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(); @@ -714,45 +689,34 @@ protected void handleParticipantReference(OpenmrsFhirCriteriaContext crit protected void handleProviderReference(OpenmrsFhirCriteriaContext 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> predicateList = new ArrayList<>(); + Path givenName = criteriaContext.getRoot().join("orderer").join("person").join("names").get("givenName"); + Path middleName = criteriaContext.getRoot().join("orderer").join("person").join("names").get("middleName"); + Path 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())); } @@ -762,7 +726,7 @@ protected void handleProviderReference(OpenmrsFhirCriteriaContext criteri } protected Optional handleCodeableConcept(OpenmrsFhirCriteriaContext criteriaContext, - TokenAndListParam concepts, @Nonnull String conceptAlias, @Nonnull String conceptMapAlias, + TokenAndListParam concepts, @Nonnull Path tPath, @Nonnull String conceptMapAlias, @Nonnull String conceptReferenceTermAlias) { if (concepts == null) { return Optional.empty(); @@ -776,17 +740,17 @@ protected Optional 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); } @@ -1151,8 +1115,7 @@ protected Collection paramToProps(OpenmrsFhirCriteriaContext crit protected String paramToProp(OpenmrsFhirCriteriaContext criteriaContext, @Nonnull String param) { return null; } - - //TODO: remove old propertyLike + protected Optional propertyLike(OpenmrsFhirCriteriaContext criteriaContext, @Nonnull String propertyName, String value) { if (value == null) { @@ -1161,9 +1124,8 @@ protected Optional propertyLike(OpenmrsFhirCriteriaContext cri return propertyLike(criteriaContext, propertyName, new StringParam(value)); } - - //new - private Optional propertyLike(OpenmrsFhirCriteriaContext criteriaContext, @Nonnull Path yPath, String value) { + + protected Optional propertyLike(OpenmrsFhirCriteriaContext criteriaContext, @Nonnull Path yPath, String value) { if (value == null) { return Optional.empty(); } @@ -1172,7 +1134,7 @@ private Optional propertyLike(OpenmrsFhirCriteriaCon } @SuppressWarnings("unchecked") - private Optional propertyLike(OpenmrsFhirCriteriaContext criteriaContext, Path yPath, StringParam param) { + protected Optional propertyLike(OpenmrsFhirCriteriaContext criteriaContext, Path yPath, StringParam param) { if (param == null) { return Optional.empty(); } @@ -1188,8 +1150,7 @@ private Optional propertyLike(OpenmrsFhirCriteriaCon return Optional.of(likePredicate); } - - //TODO: remove old propertyLike + protected Optional propertyLike(OpenmrsFhirCriteriaContext criteriaContext, @Nonnull String propertyName, StringParam param) { if (param == null) { diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirAllergyIntoleranceDaoImpl.java b/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirAllergyIntoleranceDaoImpl.java index 1765ca2a6..859a895d3 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirAllergyIntoleranceDaoImpl.java +++ b/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirAllergyIntoleranceDaoImpl.java @@ -90,19 +90,14 @@ protected void setupSearchParams(OpenmrsFhirCriteriaContext criteriaCon private void handleManifestation(OpenmrsFhirCriteriaContext 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 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(); } } diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirConceptDaoImpl.java b/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirConceptDaoImpl.java index 364ebe2ee..cadd93333 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirConceptDaoImpl.java +++ b/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirConceptDaoImpl.java @@ -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; @@ -103,10 +104,10 @@ protected void setupSearchParams(OpenmrsFhirCriteriaContext criteriaCon }); } + @SuppressWarnings("unchecked") protected void handleTitle(OpenmrsFhirCriteriaContext criteriaContext, StringAndListParam titlePattern) { - criteriaContext.getRoot().join("names").alias("cn"); - handleAndListParam(titlePattern, (title) -> propertyLike(criteriaContext, "cn.name", title)) - .ifPresent(criteriaContext::addPredicate); + handleAndListParam(titlePattern, (title) -> (Optional) propertyLike(criteriaContext, criteriaContext.getRoot().join("names").get("name"), title)) + .ifPresent(criteriaContext::addPredicate); criteriaContext.finalizeQuery(); } diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirConditionDaoImpl.java b/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirConditionDaoImpl.java index 6f3371501..36dcebcc5 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirConditionDaoImpl.java +++ b/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirConditionDaoImpl.java @@ -106,8 +106,7 @@ protected void setupSearchParams(OpenmrsFhirCriteriaContext criteriaC private void handleCode(OpenmrsFhirCriteriaContext 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(); } } diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirDiagnosticReportDaoImpl.java b/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirDiagnosticReportDaoImpl.java index 60565c5e7..92ae00012 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirDiagnosticReportDaoImpl.java +++ b/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirDiagnosticReportDaoImpl.java @@ -66,10 +66,7 @@ protected void setupSearchParams(OpenmrsFhirCriteriaContext 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(); } } diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirGroupDaoImpl.java b/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirGroupDaoImpl.java index a03a0f090..c853ab3d2 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirGroupDaoImpl.java +++ b/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirGroupDaoImpl.java @@ -9,6 +9,8 @@ */ package org.openmrs.module.fhir2.api.dao.impl; +import javax.persistence.criteria.Join; +import javax.persistence.criteria.Path; import javax.persistence.criteria.Predicate; import java.util.ArrayList; @@ -21,6 +23,7 @@ import org.apache.commons.lang3.StringUtils; import org.hl7.fhir.r4.model.Practitioner; import org.openmrs.Cohort; +import org.openmrs.User; import org.openmrs.module.fhir2.FhirConstants; import org.openmrs.module.fhir2.api.dao.FhirGroupDao; import org.openmrs.module.fhir2.api.search.param.SearchParameterMap; @@ -30,9 +33,9 @@ @Setter(AccessLevel.PACKAGE) public class FhirGroupDaoImpl extends BaseFhirDao implements FhirGroupDao { - private static final String NAMES_ALIAS = "ps.names"; + private static final String NAMES_ALIAS = "names"; - private static final String PERSON_ALIAS = "cr.person"; + private static final String PERSON_ALIAS = "person"; @Override protected void setupSearchParams(OpenmrsFhirCriteriaContext criteriaContext, SearchParameterMap theParams) { @@ -52,50 +55,37 @@ protected void setupSearchParams(OpenmrsFhirCriteriaContext criteriaCont protected void handleManagingEntity(OpenmrsFhirCriteriaContext criteriaContext, ReferenceAndListParam participantReference) { if (participantReference != null) { - criteriaContext.getRoot().join("creator"); + Join cohortUserJoin = criteriaContext.getRoot().join("creator"); handleAndListParam(participantReference, participantToken -> { if (participantToken.getChain() != null) { switch (participantToken.getChain()) { // Search by person (the person who created the cohort - creator) uuid case Practitioner.SP_RES_ID: - if ((lacksAlias(criteriaContext, "ps"))) { - criteriaContext.getRoot().join(PERSON_ALIAS); - } return Optional.of(criteriaContext.getCriteriaBuilder() - .like(criteriaContext.getRoot().get("ps.uuid"), participantToken.getValue())); + .like(cohortUserJoin.join(PERSON_ALIAS).get("uuid"), participantToken.getValue())); case Practitioner.SP_GIVEN: - if ((lacksAlias(criteriaContext, "ps") && (lacksAlias(criteriaContext, "pn")))) { - criteriaContext.getRoot().join(PERSON_ALIAS); - criteriaContext.getRoot().join(NAMES_ALIAS); - } return Optional.of(criteriaContext.getCriteriaBuilder() - .like(criteriaContext.getRoot().get("pn.givenName"), participantToken.getValue())); + .like(cohortUserJoin.join(PERSON_ALIAS).join(NAMES_ALIAS).get("givenName"), participantToken.getValue())); case Practitioner.SP_FAMILY: - if ((lacksAlias(criteriaContext, "ps") && (lacksAlias(criteriaContext, "pn")))) { - criteriaContext.getRoot().join(PERSON_ALIAS); - criteriaContext.getRoot().join(NAMES_ALIAS); - } return Optional.of(criteriaContext.getCriteriaBuilder() - .like(criteriaContext.getRoot().get("pn.familyName"), participantToken.getValue())); + .like(cohortUserJoin.join(PERSON_ALIAS).join(NAMES_ALIAS).get("familyName"), participantToken.getValue())); case Practitioner.SP_NAME: - if ((lacksAlias(criteriaContext, "ps") && (lacksAlias(criteriaContext, "pn")))) { - criteriaContext.getRoot().join(PERSON_ALIAS); - criteriaContext.getRoot().join(NAMES_ALIAS); - } - List> criterionList = new ArrayList<>(); + Path givenName = cohortUserJoin.join(PERSON_ALIAS).join(NAMES_ALIAS).get("givenName"); + Path middleName = cohortUserJoin.join(PERSON_ALIAS).join(NAMES_ALIAS).get("middleName"); + Path familyName = cohortUserJoin.join(PERSON_ALIAS).join(NAMES_ALIAS).get("familyName"); for (String token : StringUtils.split(participantToken.getValue(), " \t,")) { - criterionList.add(propertyLike(criteriaContext, "pn.givenName", token)); - criterionList.add(propertyLike(criteriaContext, "pn.middleName", token)); - criterionList.add(propertyLike(criteriaContext, "pn.familyName", token)); + criterionList.add(propertyLike(criteriaContext, givenName, token)); + criterionList.add(propertyLike(criteriaContext, middleName, token)); + criterionList.add(propertyLike(criteriaContext, familyName, token)); } return Optional.of(criteriaContext.getCriteriaBuilder().or(toCriteriaArray(criterionList))); } } else { // Search by creator uuid - return Optional.of(criteriaContext.getCriteriaBuilder().equal(criteriaContext.getRoot().get("cr.uuid"), + return Optional.of(criteriaContext.getCriteriaBuilder().equal(cohortUserJoin.get("uuid"), participantToken.getValue())); } diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirLocationDaoImpl.java b/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirLocationDaoImpl.java index 7882f716f..e0e735178 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirLocationDaoImpl.java +++ b/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirLocationDaoImpl.java @@ -124,10 +124,8 @@ private void handleState(OpenmrsFhirCriteriaContext criteriaContext, S private void handleTag(OpenmrsFhirCriteriaContext criteriaContext, TokenAndListParam tags) { if (tags != null) { - criteriaContext.getRoot().join("tags").alias("t"); - handleAndListParam(tags, - (tag) -> Optional.of( - criteriaContext.getCriteriaBuilder().equal(criteriaContext.getRoot().get("t.name"), tag.getValue()))) + handleAndListParam(tags, (tag) -> Optional.of( + criteriaContext.getCriteriaBuilder().equal(criteriaContext.getRoot().join("tags").get("name"), tag.getValue()))) .ifPresent(criteriaContext::addPredicate); criteriaContext.finalizeQuery(); } @@ -161,17 +159,17 @@ protected String paramToProp(OpenmrsFhirCriteriaContext criteriaContext, } @Override - public LocationTag getLocationTagByName(String tag) { + public LocationTag getLocationTagByName(@Nonnull String tag) { return locationService.getLocationTagByName(tag); } @Override - public LocationTag saveLocationTag(LocationTag tag) { + public LocationTag saveLocationTag(@Nonnull LocationTag tag) { return locationService.saveLocationTag(tag); } @Override - public LocationAttributeType getLocationAttributeTypeByUuid(String uuid) { + public LocationAttributeType getLocationAttributeTypeByUuid(@Nonnull String uuid) { return locationService.getLocationAttributeTypeByUuid(uuid); } diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirMedicationDaoImpl.java b/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirMedicationDaoImpl.java index 62062377f..b820c8cb8 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirMedicationDaoImpl.java +++ b/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirMedicationDaoImpl.java @@ -55,9 +55,8 @@ protected void setupSearchParams(OpenmrsFhirCriteriaContext criteriaContex // TODO: look into DetachedCriteria and how to translate it to jpa criteria api private void handleIngredientCode(OpenmrsFhirCriteriaContext criteriaContext, TokenAndListParam ingredientCode) { if (ingredientCode != null) { - criteriaContext.getRoot().join("ingredients").alias("i"); DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Concept.class, "ic"); - handleCodeableConcept(criteriaContext, ingredientCode, "ic", "icm", "icrt") + handleCodeableConcept(criteriaContext, ingredientCode, criteriaContext.getRoot().join("ingredients").get("concept"), "icm", "icrt") .ifPresent(criteriaContext::addPredicate); criteriaContext.finalizeQuery(); detachedCriteria.setProjection(Projections.property("conceptId")); @@ -67,16 +66,14 @@ private void handleIngredientCode(OpenmrsFhirCriteriaContext criteriaConte private void handleMedicationCode(OpenmrsFhirCriteriaContext criteriaContext, TokenAndListParam code) { if (code != null) { - criteriaContext.getRoot().join("concept").alias("cc"); - handleCodeableConcept(criteriaContext, code, "cc", "ccm", "ccrt").ifPresent(criteriaContext::addPredicate); + handleCodeableConcept(criteriaContext, code, criteriaContext.getRoot().join("concept"), "ccm", "ccrt").ifPresent(criteriaContext::addPredicate); criteriaContext.finalizeQuery(); } } private void handleMedicationDosageForm(OpenmrsFhirCriteriaContext criteriaContext, TokenAndListParam dosageForm) { if (dosageForm != null) { - criteriaContext.getRoot().join("dosageForm").alias("dc"); - handleCodeableConcept(criteriaContext, dosageForm, "dc", "dcm", "dcrt").ifPresent(criteriaContext::addPredicate); + handleCodeableConcept(criteriaContext, dosageForm, criteriaContext.getRoot().join("dosageForm"), "dcm", "dcrt").ifPresent(criteriaContext::addPredicate); criteriaContext.finalizeQuery(); } } diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirMedicationRequestDaoImpl.java b/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirMedicationRequestDaoImpl.java index 29a2293d8..aac5fad19 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirMedicationRequestDaoImpl.java +++ b/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirMedicationRequestDaoImpl.java @@ -152,11 +152,7 @@ protected Predicate generateFulfillerStatusRestriction(OpenmrsFhirCriteriaCo private void handleCodedConcept(OpenmrsFhirCriteriaContext criteriaContext, TokenAndListParam code) { if (code != null) { - if (lacksAlias(criteriaContext, "c")) { - criteriaContext.getRoot().join("concept"); - } - - handleCodeableConcept(criteriaContext, code, "c", "cm", "crt") + handleCodeableConcept(criteriaContext, code, criteriaContext.getRoot().join("concept"), "cm", "crt") .ifPresent(criteriaContext::addPredicate); criteriaContext.finalizeQuery(); } diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirObservationDaoImpl.java b/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirObservationDaoImpl.java index 25267c7d3..baf6c6d0b 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirObservationDaoImpl.java +++ b/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirObservationDaoImpl.java @@ -10,6 +10,7 @@ package org.openmrs.module.fhir2.api.dao.impl; import javax.annotation.Nonnull; +import javax.persistence.criteria.Path; import javax.persistence.criteria.Predicate; import java.util.Date; @@ -217,27 +218,19 @@ protected void setupSearchParams(OpenmrsFhirCriteriaContext criteriaContext private void handleHasMemberReference(OpenmrsFhirCriteriaContext criteriaContext, ReferenceAndListParam hasMemberReference) { + Path obsPath = criteriaContext.getRoot().join("groupMembers").join("concept"); if (hasMemberReference != null) { - if (lacksAlias(criteriaContext, "gm")) { - criteriaContext.getRoot().join("groupMembers").alias("gm"); - } - handleAndListParam(hasMemberReference, hasMemberRef -> { if (hasMemberRef.getChain() != null) { if (Observation.SP_CODE.equals(hasMemberRef.getChain())) { TokenAndListParam code = new TokenAndListParam() .addAnd(new TokenParam().setValue(hasMemberRef.getValue())); - - if (lacksAlias(criteriaContext, "c")) { - criteriaContext.getRoot().join("gm.concept").alias("c"); - } - - return handleCodeableConcept(criteriaContext, code, "c", "cm", "crt"); + return handleCodeableConcept(criteriaContext, code, obsPath, "cm", "crt"); } } else { if (StringUtils.isNotBlank(hasMemberRef.getIdPart())) { return Optional.of(criteriaContext.getCriteriaBuilder() - .equal(criteriaContext.getRoot().get("gm.uuid"), hasMemberRef.getIdPart())); + .equal(criteriaContext.getRoot().join("groupMembers").get("uuid"), hasMemberRef.getIdPart())); } } @@ -254,47 +247,34 @@ private Optional handleValueStringParam(OpenmrsFhirCriteriaContex private void handleCodedConcept(OpenmrsFhirCriteriaContext criteriaContext, TokenAndListParam code) { if (code != null) { - if (lacksAlias(criteriaContext, "c")) { - criteriaContext.getRoot().join("concept"); - } - handleCodeableConcept(criteriaContext, code, "concept", "cm", "crt").ifPresent(criteriaContext::addPredicate); + handleCodeableConcept(criteriaContext, code, criteriaContext.getRoot().join("concept"), "cm", "crt").ifPresent(criteriaContext::addPredicate); criteriaContext.finalizeQuery(); } } private void handleConceptClass(OpenmrsFhirCriteriaContext criteriaContext, TokenAndListParam category) { - if (category != null) { - if (lacksAlias(criteriaContext, "c")) { - criteriaContext.getRoot().join("concept").alias("c"); - } - - if (lacksAlias(criteriaContext, "cc")) { - criteriaContext.getRoot().join("c.conceptClass").alias("cc"); - } + if (category == null) { + handleAndListParam(category, (param) -> { + if (param.getValue() == null) { + return Optional.empty(); + } + OpenmrsFhirCriteriaContext context = createCriteriaContext(String.class); + context.getCriteriaQuery().subquery(String.class).select(context.getRoot().get("uuid")) + .where(context.getCriteriaBuilder().equal(context.getRoot().get("category"), param.getValue())); + + return Optional.of( + context.getCriteriaBuilder() + .in(criteriaContext.getRoot().get("concept").get("conceptClass").get("uuid")) + .value(context.getCriteriaQuery().subquery(String.class))); + }).ifPresent(criteriaContext::addPredicate); + criteriaContext.finalizeQuery(); } - - handleAndListParam(category, (param) -> { - if (param.getValue() == null) { - return Optional.empty(); - } - OpenmrsFhirCriteriaContext context = createCriteriaContext(String.class); - context.getCriteriaQuery().subquery(String.class).select(context.getRoot().get("uuid")) - .where(context.getCriteriaBuilder().equal(context.getRoot().get("category"), param.getValue())); - - return Optional.of( - context.getCriteriaBuilder().in(criteriaContext.getRoot().get("concept").get("conceptClass").get("uuid")) - .value(context.getCriteriaQuery().subquery(String.class))); - }).ifPresent(criteriaContext::addPredicate); - criteriaContext.finalizeQuery(); } private void handleValueCodedConcept(OpenmrsFhirCriteriaContext criteriaContext, TokenAndListParam valueConcept) { if (valueConcept != null) { - if (lacksAlias(criteriaContext, "vc")) { - criteriaContext.getRoot().join("valueCoded").alias("vc"); - } - handleCodeableConcept(criteriaContext, valueConcept, "vc", "vcm", "vcrt") + handleCodeableConcept(criteriaContext, valueConcept, criteriaContext.getRoot().join("valueCoded"), "vcm", "vcrt") .ifPresent(criteriaContext::addPredicate); criteriaContext.finalizeQuery(); } diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirServiceRequestDaoImpl.java b/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirServiceRequestDaoImpl.java index af474b0d4..f8a4bcdb5 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirServiceRequestDaoImpl.java +++ b/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirServiceRequestDaoImpl.java @@ -70,11 +70,7 @@ protected void setupSearchParams(OpenmrsFhirCriteriaContext criteriaC private void handleCodedConcept(OpenmrsFhirCriteriaContext criteriaContext, TokenAndListParam code) { if (code != null) { - if (lacksAlias(criteriaContext, "c")) { - criteriaContext.getRoot().join("concept").alias("c"); - } - - handleCodeableConcept(criteriaContext, code, "c", "cm", "crt").ifPresent(criteriaContext::addPredicate); + handleCodeableConcept(criteriaContext, code, criteriaContext.getRoot().join("concept"), "cm", "crt").ifPresent(criteriaContext::addPredicate); criteriaContext.finalizeQuery(); } }