Skip to content

Commit

Permalink
Another f*cking mess
Browse files Browse the repository at this point in the history
  • Loading branch information
ibacher committed Jan 12, 2024
1 parent 31af567 commit 360e162
Show file tree
Hide file tree
Showing 13 changed files with 1,309 additions and 1,516 deletions.
2,236 changes: 1,010 additions & 1,226 deletions api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/BaseDao.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,13 @@ protected void handleHasAndListParam(OpenmrsFhirCriteriaContext<T> criteriaConte
criteriaContext.addJoin("en.orders", "orders");
}
}
Join<DrugOrder,Order> join = criteriaContext.getRoot().join("orders");
Join<DrugOrder, Order> join = criteriaContext.getRoot().join("orders");
// Constrain only on non-voided Drug Orders
// TODO Do these criteria still work?
criteriaContext.addPredicate(criteriaContext.getCriteriaBuilder()
.equal(join.get("voided"), false));
criteriaContext.addPredicate(criteriaContext.getCriteriaBuilder()
.notEqual(join.get("action"), Order.Action.DISCONTINUE));
criteriaContext
.addPredicate(criteriaContext.getCriteriaBuilder().equal(join.get("voided"), false));
criteriaContext.addPredicate(
criteriaContext.getCriteriaBuilder().notEqual(join.get("action"), Order.Action.DISCONTINUE));

String paramName = hasParam.getParameterName();
String paramValue = hasParam.getParameterValue();
Expand Down Expand Up @@ -146,7 +146,8 @@ protected void handleHasAndListParam(OpenmrsFhirCriteriaContext<T> criteriaConte
}
if (MedicationRequest.MedicationRequestStatus.COMPLETED.toString()
.equalsIgnoreCase(paramValue)) {
Predicate notCompletedCriterion = generateNotCompletedOrderQuery(criteriaContext,"orders");
Predicate notCompletedCriterion = generateNotCompletedOrderQuery(criteriaContext,
"orders");
if (notCompletedCriterion != null) {
criteriaContext.getCriteriaBuilder().and(notCompletedCriterion);
}
Expand All @@ -156,12 +157,12 @@ protected void handleHasAndListParam(OpenmrsFhirCriteriaContext<T> criteriaConte
} else if ((FhirConstants.SP_FULFILLER_STATUS).equalsIgnoreCase(paramName)) {
if (paramValue != null) {
criteriaContext.getCriteriaBuilder()
.and(generateFulfillerStatusRestriction(criteriaContext,"orders", paramValue));
.and(generateFulfillerStatusRestriction(criteriaContext, "orders", paramValue));
}
} else if ((FhirConstants.SP_FULFILLER_STATUS + ":not").equalsIgnoreCase(paramName)) {
if (paramValue != null) {
criteriaContext.getCriteriaBuilder()
.and(generateNotFulfillerStatusRestriction(criteriaContext,"orders", paramValue));
criteriaContext.getCriteriaBuilder().and(
generateNotFulfillerStatusRestriction(criteriaContext, "orders", paramValue));
}
}
criteriaContext.finalizeQuery();
Expand Down Expand Up @@ -189,12 +190,14 @@ protected <T> Predicate generateNotCompletedOrderQuery(OpenmrsFhirCriteriaContex
return null;
}

protected <T> Predicate generateFulfillerStatusRestriction(OpenmrsFhirCriteriaContext<T> criteriaContext, String path, String fulfillerStatus) {
protected <T> Predicate generateFulfillerStatusRestriction(OpenmrsFhirCriteriaContext<T> criteriaContext, String path,
String fulfillerStatus) {
// not implemented in Core until 2.2; see override in FhirEncounterDaoImpl_2_2
return null;
}

protected <V> Predicate generateNotFulfillerStatusRestriction(OpenmrsFhirCriteriaContext<V> criteriaContext, String path, String fulfillerStatus) {
protected <V> Predicate generateNotFulfillerStatusRestriction(OpenmrsFhirCriteriaContext<V> criteriaContext, String path,
String fulfillerStatus) {
// not implemented in Core until 2.2; see override in FhirEncounterDaoImpl_2_2
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,46 +161,46 @@ protected OpenmrsFhirCriteriaContext<T> getSearchResultCriteria(SearchParameterM
}

@Override
@SuppressWarnings({ "unchecked","UnstableApiUsage"})
@SuppressWarnings({ "unchecked", "UnstableApiUsage" })
public List<T> getSearchResults(@Nonnull SearchParameterMap theParams) {
OpenmrsFhirCriteriaContext<T> criteriaContext = getSearchResultCriteria(theParams);

handleSort(criteriaContext, theParams.getSortSpec());

//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());
if (theParams.getToIndex() != Integer.MAX_VALUE) {
int maxResults = theParams.getToIndex() - theParams.getFromIndex();
criteriaContext.getEntityManager().createQuery(criteriaContext.getCriteriaQuery()).setMaxResults(maxResults);
}

List<T> results;
if (hasDistinctResults()) {
results = criteriaContext.getEntityManager().createQuery(criteriaContext.getCriteriaQuery()).getResultList();
} else {

handleSort(criteriaContext, theParams.getSortSpec());

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

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);
}
OpenmrsFhirCriteriaContext<Long> longOpenmrsFhirCriteriaContext = createCriteriaContext(Long.class);
longOpenmrsFhirCriteriaContext.getCriteriaQuery().subquery(Long.class).select(longOpenmrsFhirCriteriaContext
.getCriteriaBuilder().countDistinct(longOpenmrsFhirCriteriaContext.getRoot().get("id")));

List<T> results;
if (hasDistinctResults()) {
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")));

longOpenmrsFhirCriteriaContext.getCriteriaQuery().select(longOpenmrsFhirCriteriaContext.getRoot())
.where(longOpenmrsFhirCriteriaContext.getCriteriaBuilder()
.in(longOpenmrsFhirCriteriaContext.getRoot().get("id"))
.value(longOpenmrsFhirCriteriaContext.getCriteriaQuery().subquery(Long.class)));
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(
Expand All @@ -227,7 +227,7 @@ public List<T> getSearchResults(@Nonnull SearchParameterMap theParams) {
}

@Override
@SuppressWarnings({"unchecked","UnstableApiUsage"})
@SuppressWarnings({ "unchecked", "UnstableApiUsage" })
public int getSearchResultsCount(@Nonnull SearchParameterMap theParams) {
OpenmrsFhirCriteriaContext<T> criteriaContext = getSearchResultCriteria(theParams);
applyExactTotal(criteriaContext, theParams);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import static org.openmrs.module.fhir2.FhirConstants.TITLE_SEARCH_HANDLER;

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

import java.util.Collections;
import java.util.List;
Expand All @@ -24,7 +23,6 @@
import org.openmrs.Concept;
import org.openmrs.ConceptMap;
import org.openmrs.ConceptMapType;
import org.openmrs.ConceptReferenceTerm;
import org.openmrs.ConceptSource;
import org.openmrs.api.ConceptService;
import org.openmrs.api.context.Context;
Expand All @@ -50,14 +48,16 @@ public Optional<Concept> getConceptWithSameAsMappingInSource(@Nonnull ConceptSou
@Nonnull String mappingCode) {
OpenmrsFhirCriteriaContext<Concept> criteriaContext = createCriteriaContext(Concept.class);

createConceptMapCriteriaBuilder(conceptSource,mappingCode);
createConceptMapCriteriaBuilder(conceptSource, mappingCode);

criteriaContext.addPredicate(criteriaContext.getCriteriaBuilder()
.or(criteriaContext.getCriteriaBuilder().equal(criteriaContext.getRoot().join("conceptMapType").get("mapType.uuid"),
ConceptMapType.SAME_AS_MAP_TYPE_UUID),
criteriaContext.getCriteriaBuilder().equal(criteriaContext.getRoot().join("conceptMapType").get("mapType.name"), "SAME-AS")));
criteriaContext.addPredicate(criteriaContext.getCriteriaBuilder().or(
criteriaContext.getCriteriaBuilder().equal(criteriaContext.getRoot().join("conceptMapType").get("mapType.uuid"),
ConceptMapType.SAME_AS_MAP_TYPE_UUID),
criteriaContext.getCriteriaBuilder().equal(criteriaContext.getRoot().join("conceptMapType").get("mapType.name"),
"SAME-AS")));

criteriaContext.addOrder(criteriaContext.getCriteriaBuilder().asc(criteriaContext.getRoot().join("concept").get("retired")));
criteriaContext.addOrder(
criteriaContext.getCriteriaBuilder().asc(criteriaContext.getRoot().join("concept").get("retired")));
criteriaContext.finalizeQuery();

return Optional.ofNullable(
Expand All @@ -72,8 +72,8 @@ public List<Concept> getConceptsWithAnyMappingInSource(@Nonnull ConceptSource co
}

OpenmrsFhirCriteriaContext<Concept> criteriaContext = createCriteriaContext(Concept.class);
createConceptMapCriteriaBuilder(conceptSource,mappingCode);

createConceptMapCriteriaBuilder(conceptSource, mappingCode);
criteriaContext.addOrder(criteriaContext.getCriteriaBuilder().asc(criteriaContext.getRoot().get("concept.retired")));

return criteriaContext.getEntityManager().createQuery(criteriaContext.getCriteriaQuery()).getResultList();
Expand Down Expand Up @@ -106,18 +106,17 @@ protected void createConceptMapCriteriaBuilder(@Nonnull ConceptSource conceptSou
criteriaContext.addJoin("conceptMapType", "mapType").finalizeQuery();
criteriaContext.addJoin("concept", "concept").finalizeQuery();


if (Context.getAdministrationService().isDatabaseStringComparisonCaseSensitive()) {
criteriaContext.addPredicate(criteriaContext.getCriteriaBuilder()
.equal(criteriaContext.getCriteriaBuilder()
.lower(criteriaContext.getRoot().get("term.code")), mappingCode.toLowerCase()));
criteriaContext.addPredicate(criteriaContext.getCriteriaBuilder().equal(
criteriaContext.getCriteriaBuilder().lower(criteriaContext.getRoot().get("term.code")),
mappingCode.toLowerCase()));
} else {
criteriaContext.addPredicate(criteriaContext.getCriteriaBuilder()
.equal(criteriaContext.getRoot().get("term.code"), mappingCode));
criteriaContext.addPredicate(
criteriaContext.getCriteriaBuilder().equal(criteriaContext.getRoot().get("term.code"), mappingCode));
}

criteriaContext.addPredicate(criteriaContext.getCriteriaBuilder()
.equal(criteriaContext.getRoot().get("term.conceptSource"), conceptSource))
.finalizeQuery();
criteriaContext.addPredicate(
criteriaContext.getCriteriaBuilder().equal(criteriaContext.getRoot().get("term.conceptSource"), conceptSource))
.finalizeQuery();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,14 @@ public Collection<FhirConceptSource> getFhirConceptSources() {
public Optional<FhirConceptSource> getFhirConceptSourceByUrl(@Nonnull String url) {
OpenmrsFhirCriteriaContext<FhirConceptSource> 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().and(
criteriaContext.getCriteriaBuilder().equal(criteriaContext.getRoot().get("url"), url),
criteriaContext.getCriteriaBuilder().equal(criteriaContext.getRoot().get("retired"), false)));

try {
return Optional.ofNullable(criteriaContext.getEntityManager().createQuery(criteriaContext.finalizeQuery()).getSingleResult());
return Optional.ofNullable(
criteriaContext.getEntityManager().createQuery(criteriaContext.finalizeQuery()).getSingleResult());
}
catch (NoResultException e) {
return Optional.empty();
Expand All @@ -77,14 +78,15 @@ public Optional<FhirConceptSource> getFhirConceptSourceByConceptSourceName(@Nonn
criteriaContext.getCriteriaQuery().select(criteriaContext.getRoot());

Join<FhirConceptSource, ConceptSource> 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().and(
criteriaContext.getCriteriaBuilder().equal(conceptSourceJoin.get("name"), sourceName),
criteriaContext.getCriteriaBuilder().equal(conceptSourceJoin.get("retired"), false),
criteriaContext.getCriteriaBuilder().equal(criteriaContext.getRoot().get("retired"), false)));

try {
return Optional.ofNullable(criteriaContext.getEntityManager().createQuery(criteriaContext.finalizeQuery()).getSingleResult());
return Optional.ofNullable(
criteriaContext.getEntityManager().createQuery(criteriaContext.finalizeQuery()).getSingleResult());
}
catch (NoResultException e) {
return Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@ public class FhirContactPointMapDaoImpl implements FhirContactPointMapDao {
@Override
public Optional<FhirContactPointMap> getFhirContactPointMapByUuid(String uuid) {
OpenmrsFhirCriteriaContext<FhirContactPointMap> criteriaContext = openmrsFhirCriteriaContext();
criteriaContext.addPredicate(criteriaContext.getCriteriaBuilder().equal(criteriaContext.getRoot().get("uuid"), uuid));
criteriaContext
.addPredicate(criteriaContext.getCriteriaBuilder().equal(criteriaContext.getRoot().get("uuid"), uuid));

return Optional.ofNullable(criteriaContext.getEntityManager().createQuery(criteriaContext.finalizeQuery()).getSingleResult());
return Optional.ofNullable(
criteriaContext.getEntityManager().createQuery(criteriaContext.finalizeQuery()).getSingleResult());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import lombok.Setter;
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;
Expand Down Expand Up @@ -62,7 +61,7 @@ public List<String> getSearchResultUuids(@Nonnull SearchParameterMap theParams)
CriteriaQuery<Object[]> criteriaQuery = criteriaBuilder.createQuery(Object[].class);
Root<Encounter> root = criteriaQuery.from(Encounter.class);

criteriaQuery.multiselect(root.get("uuid"), root.get("encounterDatetime"));
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());
Expand Down Expand Up @@ -122,25 +121,26 @@ protected <V> String paramToProp(OpenmrsFhirCriteriaContext<V> criteriaContext,
}

@Override
protected <T> Predicate generateNotCompletedOrderQuery(OpenmrsFhirCriteriaContext<T> 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));
protected <T> Predicate generateNotCompletedOrderQuery(OpenmrsFhirCriteriaContext<T> 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 <T> Predicate generateFulfillerStatusRestriction(OpenmrsFhirCriteriaContext<T> criteriaContext, String path,
String fulfillerStatus) {
return criteriaContext.getCriteriaBuilder()
.equal(criteriaContext.getRoot().join(path)
.get("fulfillerStatus"),Order.FulfillerStatus.valueOf(fulfillerStatus.toUpperCase()));
String fulfillerStatus) {
return criteriaContext.getCriteriaBuilder().equal(criteriaContext.getRoot().join(path).get("fulfillerStatus"),
Order.FulfillerStatus.valueOf(fulfillerStatus.toUpperCase()));
}

@Override
protected <T> Predicate generateNotFulfillerStatusRestriction(OpenmrsFhirCriteriaContext<T> 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())));
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())));
}
}
Loading

0 comments on commit 360e162

Please sign in to comment.