From 3160a80255c6bb7707300d3b5b623cb3fae5924c Mon Sep 17 00:00:00 2001 From: mherman22 Date: Wed, 20 Dec 2023 22:36:06 +0300 Subject: [PATCH] fixing some failing tests in dao/impl --- .../module/fhir2/api/dao/impl/BaseDao.java | 24 ++--- .../fhir2/api/dao/impl/BaseEncounterDao.java | 20 ++--- .../fhir2/api/dao/impl/BaseFhirDao.java | 45 +++++----- .../api/dao/impl/FhirConceptDaoImpl.java | 77 +++++----------- .../dao/impl/FhirConceptSourceDaoImpl.java | 32 ++++--- .../api/dao/impl/FhirEncounterDaoImpl.java | 90 +++++++++++++------ .../api/dao/impl/FhirLocationDaoImpl.java | 12 ++- .../impl/FhirMedicationRequestDaoImpl.java | 5 +- .../FhirPatientIdentifierSystemDaoImpl.java | 38 +++++--- .../api/dao/impl/FhirPractitionerDaoImpl.java | 12 ++- 10 files changed, 188 insertions(+), 167 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 f3d078017..c517d1b3b 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 @@ -1041,23 +1041,19 @@ protected Predicate generateSystemQuery(OpenmrsFhirCriteriaContext criter protected Predicate generateActiveOrderQuery(OpenmrsFhirCriteriaContext criteriaContext, String path, Date onDate) { - if (StringUtils.isNotBlank(path)) { - path = path + "."; - } - // ACTIVE = date activated null or less than or equal to current datetime, date stopped null or in the future, auto expire date null or in the future return criteriaContext.getCriteriaBuilder().and( criteriaContext.getCriteriaBuilder().or( - criteriaContext.getCriteriaBuilder().isNull(criteriaContext.getRoot().get(path + "dateActivated")), - criteriaContext.getCriteriaBuilder().lessThan(criteriaContext.getRoot().get(path + "dateActivated"), + criteriaContext.getCriteriaBuilder().isNull(criteriaContext.getRoot().join(path).get("dateActivated")), + criteriaContext.getCriteriaBuilder().lessThan(criteriaContext.getRoot().join(path).get("dateActivated"), onDate)), criteriaContext.getCriteriaBuilder().or( - criteriaContext.getCriteriaBuilder().isNull(criteriaContext.getRoot().get(path + "dateStopped")), - criteriaContext.getCriteriaBuilder().greaterThan(criteriaContext.getRoot().get(path + "dateStopped"), + criteriaContext.getCriteriaBuilder().isNull(criteriaContext.getRoot().join(path).get("dateStopped")), + criteriaContext.getCriteriaBuilder().greaterThan(criteriaContext.getRoot().join(path).get("dateStopped"), onDate)), criteriaContext.getCriteriaBuilder().or( - criteriaContext.getCriteriaBuilder().isNull(criteriaContext.getRoot().get(path + "autoExpireDate")), - criteriaContext.getCriteriaBuilder().greaterThan(criteriaContext.getRoot().get(path + "autoExpireDate"), + criteriaContext.getCriteriaBuilder().isNull(criteriaContext.getRoot().join(path).get("autoExpireDate")), + criteriaContext.getCriteriaBuilder().greaterThan(criteriaContext.getRoot().join(path).get("autoExpireDate"), onDate))); } @@ -1078,15 +1074,11 @@ protected Predicate generateNotCancelledOrderQuery(OpenmrsFhirCriteriaContex } protected Predicate generateNotCancelledOrderQuery(OpenmrsFhirCriteriaContext criteriaContext, String path) { - if (StringUtils.isNotBlank(path)) { - path = path + "."; - } - Date now = new Date(); return criteriaContext.getCriteriaBuilder().or( - criteriaContext.getCriteriaBuilder().isNull(criteriaContext.getRoot().get(path + "dateStopped")), - criteriaContext.getCriteriaBuilder().greaterThan(criteriaContext.getRoot().get(path + "dateStopped"), now)); + criteriaContext.getCriteriaBuilder().isNull(criteriaContext.getRoot().join(path).get("dateStopped")), + criteriaContext.getCriteriaBuilder().greaterThan(criteriaContext.getRoot().join(path).get("dateStopped"), now)); } protected TokenOrListParam convertStringStatusToBoolean(TokenOrListParam statusParam) { diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/BaseEncounterDao.java b/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/BaseEncounterDao.java index 710670f96..3f93951db 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/BaseEncounterDao.java +++ b/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/BaseEncounterDao.java @@ -11,6 +11,7 @@ import static org.openmrs.module.fhir2.FhirConstants.ENCOUNTER_TYPE_REFERENCE_SEARCH_HANDLER; +import javax.persistence.criteria.Join; import javax.persistence.criteria.JoinType; import javax.persistence.criteria.Predicate; @@ -111,14 +112,13 @@ protected void handleHasAndListParam(OpenmrsFhirCriteriaContext criteriaConte criteriaContext.addJoin("en.orders", "orders"); } } + Join join = criteriaContext.getRoot().join("orders"); // Constrain only on non-voided Drug Orders - criteriaContext.addPredicate(criteriaContext.getCriteriaBuilder() - .equal(criteriaContext.getRoot().get("orders.class"), DrugOrder.class)); // TODO Do these criteria still work? criteriaContext.addPredicate(criteriaContext.getCriteriaBuilder() - .equal(criteriaContext.getRoot().get("orders.voided"), false)); + .equal(join.get("voided"), false)); criteriaContext.addPredicate(criteriaContext.getCriteriaBuilder() - .notEqual(criteriaContext.getRoot().get("orders.action"), Order.Action.DISCONTINUE)); + .notEqual(join.get("action"), Order.Action.DISCONTINUE)); String paramName = hasParam.getParameterName(); String paramValue = hasParam.getParameterValue(); @@ -146,7 +146,7 @@ protected void handleHasAndListParam(OpenmrsFhirCriteriaContext criteriaConte } if (MedicationRequest.MedicationRequestStatus.COMPLETED.toString() .equalsIgnoreCase(paramValue)) { - Predicate notCompletedCriterion = generateNotCompletedOrderQuery("orders"); + Predicate notCompletedCriterion = generateNotCompletedOrderQuery(criteriaContext,"orders"); if (notCompletedCriterion != null) { criteriaContext.getCriteriaBuilder().and(notCompletedCriterion); } @@ -156,12 +156,12 @@ protected void handleHasAndListParam(OpenmrsFhirCriteriaContext criteriaConte } else if ((FhirConstants.SP_FULFILLER_STATUS).equalsIgnoreCase(paramName)) { if (paramValue != null) { criteriaContext.getCriteriaBuilder() - .and(generateFulfillerStatusRestriction("orders", paramValue)); + .and(generateFulfillerStatusRestriction(criteriaContext,"orders", paramValue)); } } else if ((FhirConstants.SP_FULFILLER_STATUS + ":not").equalsIgnoreCase(paramName)) { if (paramValue != null) { criteriaContext.getCriteriaBuilder() - .and(generateNotFulfillerStatusRestriction("orders", paramValue)); + .and(generateNotFulfillerStatusRestriction(criteriaContext,"orders", paramValue)); } } criteriaContext.finalizeQuery(); @@ -184,17 +184,17 @@ protected abstract void handleEncounterType(OpenmrsFhirCriteriaContext criter protected abstract void handleParticipant(OpenmrsFhirCriteriaContext criteriaContext, ReferenceAndListParam referenceAndListParam); - protected Predicate generateNotCompletedOrderQuery(String path) { + protected Predicate generateNotCompletedOrderQuery(OpenmrsFhirCriteriaContext criteriaContext, String path) { // not implemented in Core until 2.2; see override in FhirEncounterDaoImpl_2_2 return null; } - protected Predicate generateFulfillerStatusRestriction(String path, String fulfillerStatus) { + protected Predicate generateFulfillerStatusRestriction(OpenmrsFhirCriteriaContext criteriaContext, String path, String fulfillerStatus) { // not implemented in Core until 2.2; see override in FhirEncounterDaoImpl_2_2 return null; } - protected Predicate generateNotFulfillerStatusRestriction(String path, String fulfillerStatus) { + protected Predicate generateNotFulfillerStatusRestriction(OpenmrsFhirCriteriaContext criteriaContext, String path, String fulfillerStatus) { // not implemented in Core until 2.2; see override in FhirEncounterDaoImpl_2_2 return null; } 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 f693ac1a7..8c4276c01 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 @@ -32,6 +32,7 @@ import org.hibernate.proxy.HibernateProxy; import org.hl7.fhir.r4.model.DomainResource; import org.openmrs.Auditable; +import org.openmrs.Encounter; import org.openmrs.Obs; import org.openmrs.OpenmrsObject; import org.openmrs.Order; @@ -157,30 +158,30 @@ protected OpenmrsFhirCriteriaContext getSearchResultCriteria(SearchParameterM @Override public List getSearchResults(@Nonnull SearchParameterMap theParams) { OpenmrsFhirCriteriaContext criteriaContext = getSearchResultCriteria(theParams); - - handleSort(criteriaContext, theParams.getSortSpec()); - criteriaContext.addOrder(criteriaContext.getCriteriaBuilder().asc(criteriaContext.getRoot().get("id"))); - - criteriaContext.getEntityManager().createQuery(criteriaContext.getCriteriaQuery()) - .setFirstResult(theParams.getFromIndex()); - if (theParams.getToIndex() != Integer.MAX_VALUE) { - int maxResults = theParams.getToIndex() - theParams.getFromIndex(); - criteriaContext.getEntityManager().createQuery(criteriaContext.getCriteriaQuery()).setMaxResults(maxResults); - } - - List results; - if (hasDistinctResults()) { - results = criteriaContext.getEntityManager().createQuery(criteriaContext.getCriteriaQuery()).getResultList(); - } else { - OpenmrsFhirCriteriaContext longOpenmrsFhirCriteriaContext = createCriteriaContext(Long.class); - longOpenmrsFhirCriteriaContext.getCriteriaQuery().subquery(Long.class).select(longOpenmrsFhirCriteriaContext - .getCriteriaBuilder().countDistinct(longOpenmrsFhirCriteriaContext.getRoot().get("id"))); + handleSort(criteriaContext, theParams.getSortSpec()); + criteriaContext.addOrder(criteriaContext.getCriteriaBuilder().asc(criteriaContext.getRoot().get("id"))); + + criteriaContext.getEntityManager().createQuery(criteriaContext.getCriteriaQuery()) + .setFirstResult(theParams.getFromIndex()); + if (theParams.getToIndex() != Integer.MAX_VALUE) { + int maxResults = theParams.getToIndex() - theParams.getFromIndex(); + criteriaContext.getEntityManager().createQuery(criteriaContext.getCriteriaQuery()).setMaxResults(maxResults); + } - longOpenmrsFhirCriteriaContext.getCriteriaQuery().select(longOpenmrsFhirCriteriaContext.getRoot()) - .where(longOpenmrsFhirCriteriaContext.getCriteriaBuilder() - .in(longOpenmrsFhirCriteriaContext.getRoot().get("id")) - .value(longOpenmrsFhirCriteriaContext.getCriteriaQuery().subquery(Long.class))); + List results; + if (hasDistinctResults()) { + results = criteriaContext.getEntityManager().createQuery(criteriaContext.getCriteriaQuery()).getResultList(); + } else { + + OpenmrsFhirCriteriaContext longOpenmrsFhirCriteriaContext = createCriteriaContext(Long.class); + longOpenmrsFhirCriteriaContext.getCriteriaQuery().subquery(Long.class).select(longOpenmrsFhirCriteriaContext + .getCriteriaBuilder().countDistinct(longOpenmrsFhirCriteriaContext.getRoot().get("id"))); + + longOpenmrsFhirCriteriaContext.getCriteriaQuery().select(longOpenmrsFhirCriteriaContext.getRoot()) + .where(longOpenmrsFhirCriteriaContext.getCriteriaBuilder() + .in(longOpenmrsFhirCriteriaContext.getRoot().get("id")) + .value(longOpenmrsFhirCriteriaContext.getCriteriaQuery().subquery(Long.class))); //TODO: gonna come back to it later // handleSort(projectionCriteriaBuilder, theParams.getSortSpec(), this::paramToProps).ifPresent( 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 477e10e48..4ffe3fb88 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 @@ -50,30 +50,14 @@ public Optional getConceptWithSameAsMappingInSource(@Nonnull ConceptSou @Nonnull String mappingCode) { OpenmrsFhirCriteriaContext criteriaContext = createCriteriaContext(Concept.class); - //TODO: Change back to method - OpenmrsFhirCriteriaContext xontext = createCriteriaContext(ConceptMap.class); - xontext.getCriteriaQuery().select(xontext.getRoot().get("concept")); - Join termJoin = xontext.getRoot().join("conceptReferenceTerm"); - Join mapTypeJoin = xontext.getRoot().join("conceptMapType"); - Join conceptJoin = xontext.getRoot().join("concept"); - - if (Context.getAdministrationService().isDatabaseStringComparisonCaseSensitive()) { - xontext.addPredicate(xontext.getCriteriaBuilder() - .equal(xontext.getCriteriaBuilder().lower(termJoin.get("code")), mappingCode.toLowerCase())); - } else { - xontext.addPredicate(xontext.getCriteriaBuilder().equal(termJoin.get("code"), mappingCode)); - } - - xontext.addPredicate(xontext.getCriteriaBuilder().equal(termJoin.get("conceptSource"), conceptSource)); - xontext.finalizeQuery(); - //TODO: method stops here + createConceptMapCriteriaBuilder(conceptSource,mappingCode); criteriaContext.addPredicate(criteriaContext.getCriteriaBuilder() - .or(criteriaContext.getCriteriaBuilder().equal(mapTypeJoin.get("uuid"), + .or(criteriaContext.getCriteriaBuilder().equal(criteriaContext.getRoot().join("conceptMapType").get("mapType.uuid"), ConceptMapType.SAME_AS_MAP_TYPE_UUID), - criteriaContext.getCriteriaBuilder().equal(mapTypeJoin.get("name"), "SAME-AS"))); + criteriaContext.getCriteriaBuilder().equal(criteriaContext.getRoot().join("conceptMapType").get("mapType.name"), "SAME-AS"))); - criteriaContext.addOrder(criteriaContext.getCriteriaBuilder().asc(conceptJoin.get("retired"))); + criteriaContext.addOrder(criteriaContext.getCriteriaBuilder().asc(criteriaContext.getRoot().join("concept").get("retired"))); criteriaContext.finalizeQuery(); return Optional.ofNullable( @@ -88,26 +72,9 @@ public List getConceptsWithAnyMappingInSource(@Nonnull ConceptSource co } OpenmrsFhirCriteriaContext criteriaContext = createCriteriaContext(Concept.class); - - //TODO: Change back to method - OpenmrsFhirCriteriaContext xontext = createCriteriaContext(ConceptMap.class); - xontext.getCriteriaQuery().select(xontext.getRoot().get("concept")); - Join termJoin = xontext.getRoot().join("conceptReferenceTerm"); - Join mapTypeJoin = xontext.getRoot().join("conceptMapType"); - Join conceptJoin = xontext.getRoot().join("concept"); - - if (Context.getAdministrationService().isDatabaseStringComparisonCaseSensitive()) { - xontext.addPredicate(xontext.getCriteriaBuilder() - .equal(xontext.getCriteriaBuilder().lower(termJoin.get("code")), mappingCode.toLowerCase())); - } else { - xontext.addPredicate(xontext.getCriteriaBuilder().equal(termJoin.get("code"), mappingCode)); - } - - xontext.addPredicate(xontext.getCriteriaBuilder().equal(termJoin.get("conceptSource"), conceptSource)); - xontext.finalizeQuery(); - //TODO: method stops here + createConceptMapCriteriaBuilder(conceptSource,mappingCode); - criteriaContext.addOrder(criteriaContext.getCriteriaBuilder().asc(conceptJoin.get("retired"))); + criteriaContext.addOrder(criteriaContext.getCriteriaBuilder().asc(criteriaContext.getRoot().join("concept").get("retired"))); return criteriaContext.getEntityManager().createQuery(criteriaContext.getCriteriaQuery()).getResultList(); } @@ -132,21 +99,19 @@ protected void handleTitle(OpenmrsFhirCriteriaContext criteriaContext, criteriaContext.finalizeQuery(); } -// protected void createConceptMapCriteriaBuilder(@Nonnull ConceptSource conceptSource, String mappingCode) { -// OpenmrsFhirCriteriaContext criteriaContext = createCriteriaContext(ConceptMap.class); -// criteriaContext.getCriteriaQuery().select(criteriaContext.getRoot().get("concept")); -// Join termJoin = criteriaContext.getRoot().join("conceptReferenceTerm"); -// Join mapTypeJoin = criteriaContext.getRoot().join("conceptMapType"); -// Join conceptJoin = criteriaContext.getRoot().join("concept"); -// -// if (Context.getAdministrationService().isDatabaseStringComparisonCaseSensitive()) { -// criteriaContext.addPredicate(criteriaContext.getCriteriaBuilder() -// .equal(criteriaContext.getCriteriaBuilder().lower(termJoin.get("code")), mappingCode.toLowerCase())); -// } else { -// criteriaContext.addPredicate(criteriaContext.getCriteriaBuilder().equal(termJoin.get("code"), mappingCode)); -// } -// -// criteriaContext.addPredicate(criteriaContext.getCriteriaBuilder().equal(termJoin.get("conceptSource"), conceptSource)); -// criteriaContext.finalizeQuery(); -// } + protected void createConceptMapCriteriaBuilder(@Nonnull ConceptSource conceptSource, String mappingCode) { + OpenmrsFhirCriteriaContext criteriaContext = createCriteriaContext(ConceptMap.class); + criteriaContext.getCriteriaQuery().select(criteriaContext.getRoot().get("concept")); + Join termJoin = criteriaContext.getRoot().join("conceptReferenceTerm"); + + if (Context.getAdministrationService().isDatabaseStringComparisonCaseSensitive()) { + criteriaContext.addPredicate(criteriaContext.getCriteriaBuilder() + .equal(criteriaContext.getCriteriaBuilder().lower(termJoin.get("code")), mappingCode.toLowerCase())); + } else { + criteriaContext.addPredicate(criteriaContext.getCriteriaBuilder().equal(termJoin.get("code"), mappingCode)); + } + + criteriaContext.addPredicate(criteriaContext.getCriteriaBuilder().equal(termJoin.get("conceptSource"), conceptSource)); + criteriaContext.finalizeQuery(); + } } diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirConceptSourceDaoImpl.java b/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirConceptSourceDaoImpl.java index 326ac07da..0419d3ec0 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirConceptSourceDaoImpl.java +++ b/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirConceptSourceDaoImpl.java @@ -11,6 +11,7 @@ import javax.annotation.Nonnull; import javax.persistence.EntityManager; +import javax.persistence.NoResultException; import javax.persistence.TypedQuery; import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaQuery; @@ -56,11 +57,17 @@ public Collection getFhirConceptSources() { public Optional getFhirConceptSourceByUrl(@Nonnull String url) { OpenmrsFhirCriteriaContext criteriaContext = openmrsFhirCriteriaContext(); criteriaContext.getCriteriaQuery().select(criteriaContext.getRoot()); + + criteriaContext.addPredicate(criteriaContext.getCriteriaBuilder().and(criteriaContext.getCriteriaBuilder() + .equal(criteriaContext.getRoot().get("url"), url),criteriaContext.getCriteriaBuilder() + .equal(criteriaContext.getRoot().get("retired"), false))); - criteriaContext.addPredicate(criteriaContext.getCriteriaBuilder().equal(criteriaContext.getRoot().get("url"), url)); - criteriaContext.addPredicate(criteriaContext.getCriteriaBuilder().equal(criteriaContext.getRoot().get("retired"), false)); - - return Optional.ofNullable(criteriaContext.getEntityManager().createQuery(criteriaContext.finalizeQuery()).getSingleResult()); + try { + return Optional.ofNullable(criteriaContext.getEntityManager().createQuery(criteriaContext.finalizeQuery()).getSingleResult()); + } + catch (NoResultException e) { + return Optional.empty(); + } } @Override @@ -70,13 +77,18 @@ public Optional getFhirConceptSourceByConceptSourceName(@Nonn criteriaContext.getCriteriaQuery().select(criteriaContext.getRoot()); Join conceptSourceJoin = criteriaContext.getRoot().join("conceptSource"); + + criteriaContext.addPredicate(criteriaContext.getCriteriaBuilder().and(criteriaContext.getCriteriaBuilder() + .equal(conceptSourceJoin.get("name"), sourceName),criteriaContext.getCriteriaBuilder() + .equal(conceptSourceJoin.get("retired"), false),criteriaContext.getCriteriaBuilder() + .equal(criteriaContext.getRoot().get("retired"), false))); - criteriaContext.addPredicate(criteriaContext.getCriteriaBuilder().equal(conceptSourceJoin.get("name"), sourceName)); - criteriaContext.addPredicate(criteriaContext.getCriteriaBuilder().equal(conceptSourceJoin.get("voided"), false)); - - criteriaContext.addPredicate(criteriaContext.getCriteriaBuilder().equal(criteriaContext.getRoot().get("voided"), false)); - - return Optional.ofNullable(criteriaContext.getEntityManager().createQuery(criteriaContext.finalizeQuery()).getSingleResult()); + try { + return Optional.ofNullable(criteriaContext.getEntityManager().createQuery(criteriaContext.finalizeQuery()).getSingleResult()); + } + catch (NoResultException e) { + return Optional.empty(); + } } @Override 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 b6302d91e..f0938eeed 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 @@ -27,10 +27,9 @@ import lombok.AccessLevel; import lombok.NonNull; import lombok.Setter; -import org.apache.commons.lang3.StringUtils; -import org.hibernate.criterion.Restrictions; import org.openmrs.Encounter; import org.openmrs.EncounterType; +import org.openmrs.Obs; import org.openmrs.Order; import org.openmrs.module.fhir2.FhirConstants; import org.openmrs.module.fhir2.api.dao.FhirEncounterDao; @@ -47,6 +46,55 @@ public boolean hasDistinctResults() { return false; } + @Override + public List getSearchResults(@NonNull SearchParameterMap theParams) { + OpenmrsFhirCriteriaContext criteriaContext = getSearchResultCriteria(theParams); + handleSort(criteriaContext, theParams.getSortSpec()); + criteriaContext.addOrder(criteriaContext.getCriteriaBuilder().asc(criteriaContext.getRoot().get("encounterId"))); + + criteriaContext.getEntityManager().createQuery(criteriaContext.getCriteriaQuery()).setFirstResult(theParams.getFromIndex()); + if (theParams.getToIndex() != Integer.MAX_VALUE) { + int maxResults = theParams.getToIndex() - theParams.getFromIndex(); + criteriaContext.getEntityManager().createQuery(criteriaContext.getCriteriaQuery()).setMaxResults(maxResults); + } + + List results; + if (hasDistinctResults()) { + results = criteriaContext.getEntityManager().createQuery(criteriaContext.getCriteriaQuery()).getResultList(); + } else { + OpenmrsFhirCriteriaContext longOpenmrsFhirCriteriaContext = createCriteriaContext(Long.class); + longOpenmrsFhirCriteriaContext.getCriteriaQuery().subquery(Long.class).select(longOpenmrsFhirCriteriaContext + .getCriteriaBuilder().countDistinct(longOpenmrsFhirCriteriaContext.getRoot().get("id"))); + + longOpenmrsFhirCriteriaContext.getCriteriaQuery().select(longOpenmrsFhirCriteriaContext.getRoot()) + .where(longOpenmrsFhirCriteriaContext.getCriteriaBuilder() + .in(longOpenmrsFhirCriteriaContext.getRoot().get("id")) + .value(longOpenmrsFhirCriteriaContext.getCriteriaQuery().subquery(Long.class))); + + //TODO: gonna come back to it later + // handleSort(projectionCriteriaBuilder, theParams.getSortSpec(), this::paramToProps).ifPresent( + // orders -> orders.forEach(order -> projectionList.add(Projections.property(order.getPropertyName())))); + // criteria.setProjection(projectionList); + // List ids = new ArrayList<>(); + // if (projectionList.getLength() > 1) { + // for (Object[] o : ((List) criteria.list())) { + // ids.add((Integer) o[0]); + // } + // } else { + // ids = criteria.list(); + // } + + longOpenmrsFhirCriteriaContext.getCriteriaQuery().select(longOpenmrsFhirCriteriaContext.getRoot()).where( + longOpenmrsFhirCriteriaContext.getCriteriaBuilder().in(longOpenmrsFhirCriteriaContext.getRoot().get("id"))); + // Need to reapply ordering + handleSort(criteriaContext, theParams.getSortSpec()); + criteriaContext.addOrder(criteriaContext.getCriteriaBuilder().asc(criteriaContext.getRoot().get("id"))); + + results = criteriaContext.getEntityManager().createQuery(criteriaContext.getCriteriaQuery()).getResultList(); + } + return results.stream().map(this::deproxyResult).collect(Collectors.toList()); + } + @Override public List getSearchResultUuids(@Nonnull SearchParameterMap theParams) { OpenmrsFhirCriteriaContext criteriaContext = createCriteriaContext(Encounter.class); @@ -114,35 +162,25 @@ protected String paramToProp(OpenmrsFhirCriteriaContext criteriaContext, } @Override - protected Predicate generateNotCompletedOrderQuery(String path) { - if (StringUtils.isNotBlank(path)) { - path = path + "."; - } - - return (Predicate) Restrictions.or(Restrictions.isNull(path + "fulfillerStatus"), - Restrictions.ne(path + "fulfillerStatus", Order.FulfillerStatus.COMPLETED)); - + protected Predicate generateNotCompletedOrderQuery(OpenmrsFhirCriteriaContext criteriaContext,String path) { + return criteriaContext.getCriteriaBuilder().or(criteriaContext.getCriteriaBuilder().isNull(criteriaContext.getRoot().join(path).get("fulfillerStatus")), + criteriaContext.getCriteriaBuilder().notEqual(criteriaContext.getRoot().join(path).get("fulfillerStatus"),Order.FulfillerStatus.COMPLETED)); } @Override - protected Predicate generateFulfillerStatusRestriction(String path, String fulfillerStatus) { - - if (StringUtils.isNotBlank(path)) { - path = path + "."; - } - - return (Predicate) Restrictions.eq(path + "fulfillerStatus", - Order.FulfillerStatus.valueOf(fulfillerStatus.toUpperCase())); + protected Predicate generateFulfillerStatusRestriction(OpenmrsFhirCriteriaContext criteriaContext, String path, + String fulfillerStatus) { + return criteriaContext.getCriteriaBuilder() + .equal(criteriaContext.getRoot().join(path) + .get("fulfillerStatus"),Order.FulfillerStatus.valueOf(fulfillerStatus.toUpperCase())); } @Override - protected Predicate generateNotFulfillerStatusRestriction(String path, String fulfillerStatus) { - - if (StringUtils.isNotBlank(path)) { - path = path + "."; - } - - return (Predicate) Restrictions.or(Restrictions.isNull(path + "fulfillerStatus"), - Restrictions.ne(path + "fulfillerStatus", Order.FulfillerStatus.valueOf(fulfillerStatus.toUpperCase()))); + protected Predicate generateNotFulfillerStatusRestriction(OpenmrsFhirCriteriaContext criteriaContext, String path, + String fulfillerStatus) { + return criteriaContext.getCriteriaBuilder().or(criteriaContext.getCriteriaBuilder().isNull(criteriaContext.getRoot().join(path).get("fulfillerStatus")), + criteriaContext.getCriteriaBuilder() + .notEqual(criteriaContext.getRoot() + .join(path).get("fulfillerStatus"),Order.FulfillerStatus.valueOf(fulfillerStatus.toUpperCase()))); } } 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 33d962330..7882f716f 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 @@ -83,14 +83,12 @@ public List getActiveAttributesByLocationAndAttributeTypeUuid OpenmrsFhirCriteriaContext criteriaContext = openmrsFhirCriteriaContext(); criteriaContext.getCriteriaQuery().select(criteriaContext.getRoot()); - criteriaContext.addPredicate(criteriaContext.getCriteriaBuilder() - .equal(criteriaContext.getRoot().join("location").get("id"), location.getId())); - criteriaContext.addPredicate(criteriaContext.getCriteriaBuilder() - .equal(criteriaContext.getRoot().join("attributeType").get("uuid"), locationAttributeTypeUuid)); - criteriaContext - .addPredicate(criteriaContext.getCriteriaBuilder().equal(criteriaContext.getRoot().get("voided"), false)); + criteriaContext.addPredicate(criteriaContext.getCriteriaBuilder().and(criteriaContext.getCriteriaBuilder() + .equal(criteriaContext.getRoot().join("location").get("locationId"), location.getId()),criteriaContext.getCriteriaBuilder() + .equal(criteriaContext.getRoot().join("attributeType").get("uuid"), locationAttributeTypeUuid),criteriaContext.getCriteriaBuilder() + .equal(criteriaContext.getRoot().get("voided"), false))); - return criteriaContext.getEntityManager().createQuery(criteriaContext.getCriteriaQuery()).getResultList(); + return criteriaContext.getEntityManager().createQuery(criteriaContext.finalizeQuery()).getResultList(); } private void handleName(OpenmrsFhirCriteriaContext criteriaContext, StringAndListParam namePattern) { 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 522f4e716..29a2293d8 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 @@ -153,10 +153,11 @@ protected Predicate generateFulfillerStatusRestriction(OpenmrsFhirCriteriaCo private void handleCodedConcept(OpenmrsFhirCriteriaContext criteriaContext, TokenAndListParam code) { if (code != null) { if (lacksAlias(criteriaContext, "c")) { - criteriaContext.getRoot().join("concept").alias("c"); + criteriaContext.getRoot().join("concept"); } - handleCodeableConcept(criteriaContext, code, "c", "cm", "crt").ifPresent(criteriaContext::addPredicate); + handleCodeableConcept(criteriaContext, code, "c", "cm", "crt") + .ifPresent(criteriaContext::addPredicate); criteriaContext.finalizeQuery(); } } diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirPatientIdentifierSystemDaoImpl.java b/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirPatientIdentifierSystemDaoImpl.java index 9b61e2ab9..a0146377c 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirPatientIdentifierSystemDaoImpl.java +++ b/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirPatientIdentifierSystemDaoImpl.java @@ -11,6 +11,7 @@ import javax.annotation.Nonnull; import javax.persistence.EntityManager; +import javax.persistence.NoResultException; import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.Root; @@ -39,23 +40,38 @@ public class FhirPatientIdentifierSystemDaoImpl implements FhirPatientIdentifier @Override public String getUrlByPatientIdentifierType(PatientIdentifierType patientIdentifierType) { - OpenmrsFhirCriteriaContext criteriaContext = openmrsFhirCriteriaContext(); - CriteriaQuery criteriaQuery = criteriaContext.getCriteriaBuilder().createQuery(String.class); + EntityManager em = sessionFactory.getCurrentSession(); + CriteriaBuilder cb = em.getCriteriaBuilder(); + CriteriaQuery cq = cb.createQuery(String.class); + Root root = cq.from(FhirPatientIdentifierSystem.class); + + cq.where(cb.equal(root.join("patientIdentifierType").get("patientIdentifierTypeId"), patientIdentifierType.getId())); + cq.select(root.get("url")); - criteriaContext.addPredicate(criteriaContext.getCriteriaBuilder().equal( - criteriaContext.getRoot().get("patientIdentifierType.patientIdentifierTypeId"), patientIdentifierType.getId())); - criteriaContext.getCriteriaQuery().select(criteriaContext.getRoot().get("url")); - return criteriaContext.getEntityManager().createQuery(criteriaQuery).getSingleResult(); + try { + return em.createQuery(cq).getSingleResult(); + } + catch (NoResultException e) { + return null; + } } @Override public PatientIdentifierType getPatientIdentifierTypeByUrl(String url) { - OpenmrsFhirCriteriaContext criteriaContext = openmrsFhirCriteriaContext(); - criteriaContext.addPredicate(criteriaContext.getCriteriaBuilder().equal(criteriaContext.getRoot().get("url"), url)); - criteriaContext.getCriteriaQuery().select(criteriaContext.getRoot().get("patientIdentifierType")); + EntityManager em = sessionFactory.getCurrentSession(); + CriteriaBuilder cb = em.getCriteriaBuilder(); + CriteriaQuery cq = cb.createQuery(PatientIdentifierType.class); + Root root = cq.from(FhirPatientIdentifierSystem.class); + + cq.where(cb.equal(root.get("url"), url)); + cq.select(root.get("patientIdentifierType")); - return criteriaContext.getEntityManager().createQuery(criteriaContext.finalizeQuery()).getSingleResult() - .getPatientIdentifierType(); + try { + return em.createQuery(cq).getSingleResult(); + } + catch (NoResultException e) { + return null; + } } @Override diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirPractitionerDaoImpl.java b/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirPractitionerDaoImpl.java index 54ca0fb4b..ac1ccfdb9 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirPractitionerDaoImpl.java +++ b/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirPractitionerDaoImpl.java @@ -50,14 +50,12 @@ public List getActiveAttributesByPractitionerAndAttributeType OpenmrsFhirCriteriaContext criteriaContext = openmrsFhirCriteriaContext(); criteriaContext.getCriteriaQuery().select(criteriaContext.getRoot()); - criteriaContext.addPredicate(criteriaContext.getCriteriaBuilder() - .equal(criteriaContext.getRoot().join("provider").get("id"), provider.getId())); - criteriaContext.addPredicate(criteriaContext.getCriteriaBuilder() - .equal(criteriaContext.getRoot().join("AttributeType").get("uuid"), providerAttributeTypeUuid)); - criteriaContext - .addPredicate(criteriaContext.getCriteriaBuilder().equal(criteriaContext.getRoot().get("voided"), false)); + criteriaContext.addPredicate(criteriaContext.getCriteriaBuilder().and(criteriaContext.getCriteriaBuilder() + .equal(criteriaContext.getRoot().join("provider").get("providerId"), provider.getId()),criteriaContext.getCriteriaBuilder() + .equal(criteriaContext.getRoot().join("attributeType").get("uuid"), providerAttributeTypeUuid),criteriaContext.getCriteriaBuilder() + .equal(criteriaContext.getRoot().get("voided"), false))); - return criteriaContext.getEntityManager().createQuery(criteriaContext.getCriteriaQuery()).getResultList(); + return criteriaContext.getEntityManager().createQuery(criteriaContext.finalizeQuery()).getResultList(); } protected OpenmrsFhirCriteriaContext openmrsFhirCriteriaContext() {