Skip to content

Commit

Permalink
fixing more errors
Browse files Browse the repository at this point in the history
  • Loading branch information
mherman22 committed Dec 29, 2023
1 parent 3160a80 commit 6e1f363
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -848,33 +851,34 @@ protected <T> void handlePatientReference(OpenmrsFhirCriteriaContext<T> criteria
protected <T> void handlePatientReference(OpenmrsFhirCriteriaContext<T> 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<Optional<? extends Predicate>> criterionList = new ArrayList<>();

for (String token : StringUtils.split(patientToken.getValue(), " \t,")) {
Expand All @@ -885,7 +889,7 @@ protected <T> void handlePatientReference(OpenmrsFhirCriteriaContext<T> 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()));
}

Expand Down Expand Up @@ -1177,7 +1181,6 @@ protected <T> Optional<Predicate> propertyLike(OpenmrsFhirCriteriaContext<T> cri
}

Predicate likePredicate;

if (param.isExact()) {
likePredicate = criteriaContext.getCriteriaBuilder().equal(criteriaContext.getRoot().get(propertyName),
param.getValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -156,11 +161,20 @@ protected OpenmrsFhirCriteriaContext<T> getSearchResultCriteria(SearchParameterM
}

@Override
@SuppressWarnings({ "unchecked","UnstableApiUsage"})
public List<T> getSearchResults(@Nonnull SearchParameterMap theParams) {
OpenmrsFhirCriteriaContext<T> 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());
Expand All @@ -174,6 +188,11 @@ public List<T> getSearchResults(@Nonnull SearchParameterMap theParams) {
results = criteriaContext.getEntityManager().createQuery(criteriaContext.getCriteriaQuery()).getResultList();
} else {

EntityManager em = sessionFactory.getCurrentSession();
CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
CriteriaQuery<Long> criteriaQuery = criteriaBuilder.createQuery(Long.class);
Root<T> root = (Root<T>) criteriaQuery.from(typeToken.getRawType());

OpenmrsFhirCriteriaContext<Long> longOpenmrsFhirCriteriaContext = createCriteriaContext(Long.class);
longOpenmrsFhirCriteriaContext.getCriteriaQuery().subquery(Long.class).select(longOpenmrsFhirCriteriaContext
.getCriteriaBuilder().countDistinct(longOpenmrsFhirCriteriaContext.getRoot().get("id")));
Expand Down Expand Up @@ -208,18 +227,29 @@ public List<T> getSearchResults(@Nonnull SearchParameterMap theParams) {
}

@Override
@SuppressWarnings({"unchecked","UnstableApiUsage"})
public int getSearchResultsCount(@Nonnull SearchParameterMap theParams) {
OpenmrsFhirCriteriaContext<T> criteriaContext = getSearchResultCriteria(theParams);
OpenmrsFhirCriteriaContext<Long> criteria = createCriteriaContext(Long.class);

applyExactTotal(criteriaContext, theParams);

EntityManager em = sessionFactory.getCurrentSession();
CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
CriteriaQuery<Long> criteriaQuery = criteriaBuilder.createQuery(Long.class);
Root<T> root = (Root<T>) 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();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -102,12 +106,14 @@ public List<String> getSearchResultUuids(@Nonnull SearchParameterMap theParams)
if (!theParams.getParameters(FhirConstants.LASTN_ENCOUNTERS_SEARCH_HANDLER).isEmpty()) {
setupSearchParams(criteriaContext, theParams);

OpenmrsFhirCriteriaContext<Object[]> objCriteriaContext = createCriteriaContext(Object[].class);
objCriteriaContext.getCriteriaQuery().multiselect(objCriteriaContext.getRoot().get("uuid"),
objCriteriaContext.getRoot().get("encounterDatetime"));
EntityManager em = sessionFactory.getCurrentSession();
CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
CriteriaQuery<Object[]> criteriaQuery = criteriaBuilder.createQuery(Object[].class);
Root<Encounter> root = criteriaQuery.from(Encounter.class);

List<LastnResult<String>> results = objCriteriaContext.getEntityManager()
.createQuery(objCriteriaContext.getCriteriaQuery()).getResultList().stream()
criteriaQuery.multiselect(root.get("uuid"), root.get("encounterDatetime"));

List<LastnResult<String>> results = em.createQuery(criteriaQuery).getResultList().stream()
.map(array -> new LastnResult<String>(array)).collect(Collectors.toList());

return getTopNRankedIds(results, getMaxParameter(theParams));
Expand All @@ -117,10 +123,13 @@ public List<String> getSearchResultUuids(@Nonnull SearchParameterMap theParams)
setupSearchParams(criteriaContext, theParams);
handleSort(criteriaContext, theParams.getSortSpec());

OpenmrsFhirCriteriaContext<String> 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<String> query = criteriaBuilder.createQuery(String.class);
Root<Encounter> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class FhirRelatedPersonDaoImpl extends BaseFhirDao<Relationship> implemen
@Override
protected void setupSearchParams(OpenmrsFhirCriteriaContext<Relationship> 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:
Expand Down Expand Up @@ -86,26 +86,24 @@ protected <T> Collection<Order> paramToProps(OpenmrsFhirCriteriaContext<T> 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<PersonName> 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(
Expand All @@ -115,26 +113,26 @@ protected <T> Collection<Order> paramToProps(OpenmrsFhirCriteriaContext<T> 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;
Expand Down

0 comments on commit 6e1f363

Please sign in to comment.