From 6e1f36369b86b40518f8779c1bb0380e481f648c Mon Sep 17 00:00:00 2001 From: mherman22 Date: Fri, 29 Dec 2023 17:03:18 +0300 Subject: [PATCH] fixing more errors --- .../module/fhir2/api/dao/impl/BaseDao.java | 21 ++++++---- .../fhir2/api/dao/impl/BaseFhirDao.java | 42 ++++++++++++++++--- .../api/dao/impl/FhirEncounterDaoImpl.java | 27 ++++++++---- .../dao/impl/FhirRelatedPersonDaoImpl.java | 28 ++++++------- 4 files changed, 79 insertions(+), 39 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 c517d1b3b..417fff8e5 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 @@ -13,6 +13,8 @@ import javax.persistence.EntityManager; import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaQuery; +import javax.persistence.criteria.Join; +import javax.persistence.criteria.JoinType; import javax.persistence.criteria.Order; import javax.persistence.criteria.Predicate; import javax.persistence.criteria.Root; @@ -76,6 +78,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.PatientIdentifier; import org.openmrs.module.fhir2.FhirConstants; import org.openmrs.module.fhir2.api.search.param.PropParam; import org.openmrs.module.fhir2.api.util.LocalDateTimeFactory; @@ -848,33 +851,34 @@ protected void handlePatientReference(OpenmrsFhirCriteriaContext criteria protected void handlePatientReference(OpenmrsFhirCriteriaContext criteriaContext, ReferenceAndListParam patientReference, String associationPath) { if (patientReference != null) { - criteriaContext.getCriteriaQuery().select(criteriaContext.getRoot().get(associationPath)); + criteriaContext.addJoin(associationPath,"p").finalizeQuery(); handleAndListParam(patientReference, patientToken -> { if (patientToken.getChain() != null) { switch (patientToken.getChain()) { case Patient.SP_IDENTIFIER: if (lacksAlias(criteriaContext, "pi")) { - criteriaContext.getCriteriaQuery().select(criteriaContext.getRoot().get("p.identifiers")); + criteriaContext.addJoin("p.identifiers","pi").finalizeQuery(); } return Optional.of(criteriaContext.getCriteriaBuilder() .like(criteriaContext.getRoot().get("pi.identifier"), patientToken.getValue())); case Patient.SP_GIVEN: if (lacksAlias(criteriaContext, "pn")) { - criteriaContext.getCriteriaQuery().select(criteriaContext.getRoot().get("p.names")); + criteriaContext.addJoin("p.names","pn").finalizeQuery(); } return Optional.of(criteriaContext.getCriteriaBuilder() - .like(criteriaContext.getRoot().get("pi.givenName"), patientToken.getValue())); + .like(criteriaContext.getRoot().get("pn.givenName"), patientToken.getValue())); case Patient.SP_FAMILY: if (lacksAlias(criteriaContext, "pn")) { - criteriaContext.getCriteriaQuery().select(criteriaContext.getRoot().get("p.names")); + criteriaContext.addJoin("p.names","pn").finalizeQuery(); } return Optional.of(criteriaContext.getCriteriaBuilder() - .like(criteriaContext.getRoot().get("familyName"), patientToken.getValue())); + .like(criteriaContext.getRoot().get("pn.familyName"), patientToken.getValue())); case Patient.SP_NAME: if (lacksAlias(criteriaContext, "pn")) { - criteriaContext.getCriteriaQuery().select(criteriaContext.getRoot().get("p.names")); + criteriaContext.addJoin("p.names","pn").finalizeQuery(); } + List> criterionList = new ArrayList<>(); for (String token : StringUtils.split(patientToken.getValue(), " \t,")) { @@ -885,7 +889,7 @@ protected void handlePatientReference(OpenmrsFhirCriteriaContext criteria return Optional.of(criteriaContext.getCriteriaBuilder().or(toCriteriaArray(criterionList))); } } else { - return Optional.of(criteriaContext.getCriteriaBuilder().equal(criteriaContext.getRoot().get("uuid"), + return Optional.of(criteriaContext.getCriteriaBuilder().equal(criteriaContext.getRoot().get("p.uuid"), patientToken.getValue())); } @@ -1177,7 +1181,6 @@ protected Optional propertyLike(OpenmrsFhirCriteriaContext cri } Predicate likePredicate; - if (param.isExact()) { likePredicate = criteriaContext.getCriteriaBuilder().equal(criteriaContext.getRoot().get(propertyName), param.getValue()); diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/BaseFhirDao.java b/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/BaseFhirDao.java index 8c4276c01..a580ff3e9 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/BaseFhirDao.java +++ b/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/BaseFhirDao.java @@ -15,8 +15,12 @@ import javax.annotation.Nonnull; import javax.persistence.CacheStoreMode; +import javax.persistence.EntityManager; import javax.persistence.NoResultException; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.Predicate; +import javax.persistence.criteria.Root; import java.util.Collection; import java.util.Collections; @@ -36,6 +40,7 @@ import org.openmrs.Obs; import org.openmrs.OpenmrsObject; import org.openmrs.Order; +import org.openmrs.Person; import org.openmrs.Retireable; import org.openmrs.Voidable; import org.openmrs.aop.RequiredDataAdvice; @@ -156,11 +161,20 @@ protected OpenmrsFhirCriteriaContext getSearchResultCriteria(SearchParameterM } @Override + @SuppressWarnings({ "unchecked","UnstableApiUsage"}) public List getSearchResults(@Nonnull SearchParameterMap theParams) { OpenmrsFhirCriteriaContext criteriaContext = getSearchResultCriteria(theParams); handleSort(criteriaContext, theParams.getSortSpec()); - criteriaContext.addOrder(criteriaContext.getCriteriaBuilder().asc(criteriaContext.getRoot().get("id"))); + + //the id property differs across various openmrs entities + if (Person.class.isAssignableFrom(typeToken.getRawType())) { + criteriaContext.getCriteriaBuilder().asc(criteriaContext.getRoot().get("personId")); + } else if (Encounter.class.isAssignableFrom(typeToken.getRawType())) { + criteriaContext.getCriteriaBuilder().asc(criteriaContext.getRoot().get("encounterId")); + } else if (Obs.class.isAssignableFrom(typeToken.getRawType())) { + criteriaContext.getCriteriaBuilder().asc(criteriaContext.getRoot().get("obsId")); + } criteriaContext.getEntityManager().createQuery(criteriaContext.getCriteriaQuery()) .setFirstResult(theParams.getFromIndex()); @@ -174,6 +188,11 @@ public List getSearchResults(@Nonnull SearchParameterMap theParams) { results = criteriaContext.getEntityManager().createQuery(criteriaContext.getCriteriaQuery()).getResultList(); } else { + EntityManager em = sessionFactory.getCurrentSession(); + CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder(); + CriteriaQuery criteriaQuery = criteriaBuilder.createQuery(Long.class); + Root root = (Root) criteriaQuery.from(typeToken.getRawType()); + OpenmrsFhirCriteriaContext longOpenmrsFhirCriteriaContext = createCriteriaContext(Long.class); longOpenmrsFhirCriteriaContext.getCriteriaQuery().subquery(Long.class).select(longOpenmrsFhirCriteriaContext .getCriteriaBuilder().countDistinct(longOpenmrsFhirCriteriaContext.getRoot().get("id"))); @@ -208,18 +227,29 @@ public List getSearchResults(@Nonnull SearchParameterMap theParams) { } @Override + @SuppressWarnings({"unchecked","UnstableApiUsage"}) public int getSearchResultsCount(@Nonnull SearchParameterMap theParams) { OpenmrsFhirCriteriaContext criteriaContext = getSearchResultCriteria(theParams); - OpenmrsFhirCriteriaContext criteria = createCriteriaContext(Long.class); - applyExactTotal(criteriaContext, theParams); + EntityManager em = sessionFactory.getCurrentSession(); + CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder(); + CriteriaQuery criteriaQuery = criteriaBuilder.createQuery(Long.class); + Root root = (Root) criteriaQuery.from(typeToken.getRawType()); + if (hasDistinctResults()) { - criteria.getCriteriaQuery().select(criteria.getCriteriaBuilder().count(criteria.getRoot())); + criteriaQuery.select(criteriaBuilder.count(root)); } else { - criteria.getCriteriaQuery().select(criteria.getCriteriaBuilder().countDistinct(criteria.getRoot().get("id"))); + //the id property differs across various openmrs entities + if (Person.class.isAssignableFrom(typeToken.getRawType())) { + criteriaQuery.select(criteriaBuilder.countDistinct(root.get("personId"))); + } else if (Encounter.class.isAssignableFrom(typeToken.getRawType())) { + criteriaQuery.select(criteriaBuilder.countDistinct(root.get("encounterId"))); + } else if (Obs.class.isAssignableFrom(typeToken.getRawType())) { + criteriaQuery.select(criteriaBuilder.countDistinct(root.get("obsId"))); + } } - return criteria.getEntityManager().createQuery(criteria.getCriteriaQuery()).getSingleResult().intValue(); + return em.createQuery(criteriaQuery).getSingleResult().intValue(); } /** diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirEncounterDaoImpl.java b/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirEncounterDaoImpl.java index f0938eeed..f0c1fc5f9 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirEncounterDaoImpl.java +++ b/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirEncounterDaoImpl.java @@ -13,8 +13,12 @@ import static org.openmrs.module.fhir2.api.util.LastnOperationUtils.getTopNRankedIds; import javax.annotation.Nonnull; +import javax.persistence.EntityManager; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.Join; import javax.persistence.criteria.Predicate; +import javax.persistence.criteria.Root; import java.util.List; import java.util.Optional; @@ -102,12 +106,14 @@ public List getSearchResultUuids(@Nonnull SearchParameterMap theParams) if (!theParams.getParameters(FhirConstants.LASTN_ENCOUNTERS_SEARCH_HANDLER).isEmpty()) { setupSearchParams(criteriaContext, theParams); - OpenmrsFhirCriteriaContext objCriteriaContext = createCriteriaContext(Object[].class); - objCriteriaContext.getCriteriaQuery().multiselect(objCriteriaContext.getRoot().get("uuid"), - objCriteriaContext.getRoot().get("encounterDatetime")); + EntityManager em = sessionFactory.getCurrentSession(); + CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder(); + CriteriaQuery criteriaQuery = criteriaBuilder.createQuery(Object[].class); + Root root = criteriaQuery.from(Encounter.class); - List> results = objCriteriaContext.getEntityManager() - .createQuery(objCriteriaContext.getCriteriaQuery()).getResultList().stream() + criteriaQuery.multiselect(root.get("uuid"), root.get("encounterDatetime")); + + List> results = em.createQuery(criteriaQuery).getResultList().stream() .map(array -> new LastnResult(array)).collect(Collectors.toList()); return getTopNRankedIds(results, getMaxParameter(theParams)); @@ -117,10 +123,13 @@ public List getSearchResultUuids(@Nonnull SearchParameterMap theParams) setupSearchParams(criteriaContext, theParams); handleSort(criteriaContext, theParams.getSortSpec()); - OpenmrsFhirCriteriaContext context = createCriteriaContext(String.class); - context.getCriteriaQuery().select(context.getRoot().get("uuid")); - return context.getEntityManager().createQuery(context.getCriteriaQuery()).getResultList().stream().distinct() - .collect(Collectors.toList()); + EntityManager em = sessionFactory.getCurrentSession(); + CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder(); + CriteriaQuery query = criteriaBuilder.createQuery(String.class); + Root root = query.from(Encounter.class); + + query.select(root.get("uuid")); + return em.createQuery(query).getResultList().stream().distinct().collect(Collectors.toList()); } private int getMaxParameter(SearchParameterMap theParams) { diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirRelatedPersonDaoImpl.java b/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirRelatedPersonDaoImpl.java index 57592e12e..7136ba5ca 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirRelatedPersonDaoImpl.java +++ b/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirRelatedPersonDaoImpl.java @@ -47,7 +47,7 @@ public class FhirRelatedPersonDaoImpl extends BaseFhirDao implemen @Override protected void setupSearchParams(OpenmrsFhirCriteriaContext criteriaContext, SearchParameterMap theParams) { - criteriaContext.getRoot().join("personA", javax.persistence.criteria.JoinType.INNER).alias("m"); + criteriaContext.addJoin("personA","m").finalizeQuery(); theParams.getParameters().forEach(entry -> { switch (entry.getKey()) { case FhirConstants.NAME_SEARCH_HANDLER: @@ -86,26 +86,24 @@ protected Collection paramToProps(OpenmrsFhirCriteriaContext crite } if (param.startsWith("address") && lacksAlias(criteriaContext, "pad")) { - criteriaContext.getRoot().join("m.addresses", javax.persistence.criteria.JoinType.LEFT).alias("pad"); + criteriaContext.addJoin("m.addresses","pad",javax.persistence.criteria.JoinType.LEFT).finalizeQuery(); } else if (param.equals(SP_NAME) || param.equals(SP_GIVEN) || param.equals(SP_FAMILY)) { if (lacksAlias(criteriaContext, "pn")) { - criteriaContext.getRoot().join("m.names", javax.persistence.criteria.JoinType.LEFT).alias("pn"); + criteriaContext.addJoin("m.names","pn",javax.persistence.criteria.JoinType.LEFT).finalizeQuery(); } Root subRoot = criteriaContext.getCriteriaQuery().subquery(Integer.class).from(PersonName.class); Predicate predicate = criteriaContext.getCriteriaBuilder() .and( - criteriaContext.getCriteriaBuilder().equal(criteriaContext.getRoot().get("pn").get("voided"), false), + criteriaContext.getCriteriaBuilder().equal(criteriaContext.getRoot().get("pn.voided"), false), criteriaContext.getCriteriaBuilder().or( criteriaContext.getCriteriaBuilder().and( criteriaContext.getCriteriaBuilder() - .equal(criteriaContext.getRoot().get("pn").get("preferred"), true), - criteriaContext.getCriteriaBuilder().equal(criteriaContext.getRoot().get("pn").get( - "personNameId"), + .equal(criteriaContext.getRoot().get("pn.preferred"), true), + criteriaContext.getCriteriaBuilder().equal(criteriaContext.getRoot().get("pn.personNameId"), criteriaContext.getCriteriaQuery().subquery(Integer.class).select( - criteriaContext.getCriteriaBuilder().min(criteriaContext.getRoot().get("pn1") - .get("personNameId"))) + criteriaContext.getCriteriaBuilder().min(criteriaContext.getRoot().get("pn1.personNameId"))) .where(criteriaContext.getCriteriaBuilder().and( criteriaContext.getCriteriaBuilder().equal(subRoot.get("preferred"), true), criteriaContext.getCriteriaBuilder().equal( @@ -115,26 +113,26 @@ protected Collection paramToProps(OpenmrsFhirCriteriaContext crite criteriaContext.getCriteriaBuilder() .not(criteriaContext.getCriteriaBuilder() .exists(criteriaContext.getCriteriaQuery().subquery(Integer.class) - .select(criteriaContext.getRoot().get("pn2").get("personNameId")) + .select(criteriaContext.getRoot().get("pn2.personNameId")) .where(criteriaContext.getCriteriaBuilder().and( criteriaContext.getCriteriaBuilder() - .equal(subRoot.get("pn2").get("preferred"), true), + .equal(subRoot.get("pn2.preferred"), true), criteriaContext.getCriteriaBuilder().equal( subRoot.get("pn2").get("person").get("personId"), criteriaContext.getRoot().get("personId")))))), criteriaContext.getCriteriaBuilder().equal( - criteriaContext.getRoot().get("pn").get("personNameId"), + criteriaContext.getRoot().get("pn.personNameId"), criteriaContext.getCriteriaQuery().subquery(Integer.class) .select(criteriaContext.getCriteriaBuilder() - .min(criteriaContext.getRoot().get("pn3").get("personNameId"))) + .min(criteriaContext.getRoot().get("pn3.personNameId"))) .where(criteriaContext.getCriteriaBuilder().and( criteriaContext.getCriteriaBuilder() - .equal(subRoot.get("pn3").get("preferred"), false), + .equal(subRoot.get("pn3.preferred"), false), criteriaContext.getCriteriaBuilder().equal( subRoot.get("pn3").get("person").get("personId"), criteriaContext.getRoot().get("personId")))))), criteriaContext.getCriteriaBuilder() - .isNull(criteriaContext.getRoot().get("pn").get("personNameId")))); + .isNull(criteriaContext.getRoot().get("pn.personNameId")))); criteriaContext.getCriteriaQuery().where(predicate); String[] properties = null;