Skip to content

Commit

Permalink
fixing some failing tests in dao/impl
Browse files Browse the repository at this point in the history
  • Loading branch information
mherman22 committed Dec 20, 2023
1 parent 06850a9 commit 3160a80
Show file tree
Hide file tree
Showing 10 changed files with 188 additions and 167 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1041,23 +1041,19 @@ protected <T> Predicate generateSystemQuery(OpenmrsFhirCriteriaContext<T> criter

protected <T> Predicate generateActiveOrderQuery(OpenmrsFhirCriteriaContext<T> 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)));
}

Expand All @@ -1078,15 +1074,11 @@ protected <T> Predicate generateNotCancelledOrderQuery(OpenmrsFhirCriteriaContex
}

protected <T> Predicate generateNotCancelledOrderQuery(OpenmrsFhirCriteriaContext<T> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -111,14 +112,13 @@ protected void handleHasAndListParam(OpenmrsFhirCriteriaContext<T> criteriaConte
criteriaContext.addJoin("en.orders", "orders");
}
}
Join<DrugOrder,Order> 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();
Expand Down Expand Up @@ -146,7 +146,7 @@ protected void handleHasAndListParam(OpenmrsFhirCriteriaContext<T> criteriaConte
}
if (MedicationRequest.MedicationRequestStatus.COMPLETED.toString()
.equalsIgnoreCase(paramValue)) {
Predicate notCompletedCriterion = generateNotCompletedOrderQuery("orders");
Predicate notCompletedCriterion = generateNotCompletedOrderQuery(criteriaContext,"orders");
if (notCompletedCriterion != null) {
criteriaContext.getCriteriaBuilder().and(notCompletedCriterion);
}
Expand All @@ -156,12 +156,12 @@ protected void handleHasAndListParam(OpenmrsFhirCriteriaContext<T> 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();
Expand All @@ -184,17 +184,17 @@ protected abstract void handleEncounterType(OpenmrsFhirCriteriaContext<T> criter
protected abstract void handleParticipant(OpenmrsFhirCriteriaContext<T> criteriaContext,
ReferenceAndListParam referenceAndListParam);

protected Predicate generateNotCompletedOrderQuery(String path) {
protected <T> Predicate generateNotCompletedOrderQuery(OpenmrsFhirCriteriaContext<T> 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 <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 Predicate generateNotFulfillerStatusRestriction(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 @@ -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;
Expand Down Expand Up @@ -157,30 +158,30 @@ protected OpenmrsFhirCriteriaContext<T> getSearchResultCriteria(SearchParameterM
@Override
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")));

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 {

OpenmrsFhirCriteriaContext<Long> 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<T> results;
if (hasDistinctResults()) {
results = criteriaContext.getEntityManager().createQuery(criteriaContext.getCriteriaQuery()).getResultList();
} else {

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)));

//TODO: gonna come back to it later
// handleSort(projectionCriteriaBuilder, theParams.getSortSpec(), this::paramToProps).ifPresent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,30 +50,14 @@ public Optional<Concept> getConceptWithSameAsMappingInSource(@Nonnull ConceptSou
@Nonnull String mappingCode) {
OpenmrsFhirCriteriaContext<Concept> criteriaContext = createCriteriaContext(Concept.class);

//TODO: Change back to method
OpenmrsFhirCriteriaContext<ConceptMap> xontext = createCriteriaContext(ConceptMap.class);
xontext.getCriteriaQuery().select(xontext.getRoot().get("concept"));
Join<ConceptReferenceTerm, ConceptMap> termJoin = xontext.getRoot().join("conceptReferenceTerm");
Join<ConceptMap, ConceptMapType> mapTypeJoin = xontext.getRoot().join("conceptMapType");
Join<ConceptMap, Concept> 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(
Expand All @@ -88,26 +72,9 @@ public List<Concept> getConceptsWithAnyMappingInSource(@Nonnull ConceptSource co
}

OpenmrsFhirCriteriaContext<Concept> criteriaContext = createCriteriaContext(Concept.class);

//TODO: Change back to method
OpenmrsFhirCriteriaContext<ConceptMap> xontext = createCriteriaContext(ConceptMap.class);
xontext.getCriteriaQuery().select(xontext.getRoot().get("concept"));
Join<ConceptMap, ConceptReferenceTerm> termJoin = xontext.getRoot().join("conceptReferenceTerm");
Join<ConceptMap, ConceptMapType> mapTypeJoin = xontext.getRoot().join("conceptMapType");
Join<ConceptMap, Concept> 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();
}
Expand All @@ -132,21 +99,19 @@ protected void handleTitle(OpenmrsFhirCriteriaContext<Concept> criteriaContext,
criteriaContext.finalizeQuery();
}

// protected void createConceptMapCriteriaBuilder(@Nonnull ConceptSource conceptSource, String mappingCode) {
// OpenmrsFhirCriteriaContext<ConceptMap> criteriaContext = createCriteriaContext(ConceptMap.class);
// criteriaContext.getCriteriaQuery().select(criteriaContext.getRoot().get("concept"));
// Join<ConceptMap, ConceptReferenceTerm> termJoin = criteriaContext.getRoot().join("conceptReferenceTerm");
// Join<ConceptMap, ConceptMapType> mapTypeJoin = criteriaContext.getRoot().join("conceptMapType");
// Join<ConceptMap, Concept> 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<ConceptMap> criteriaContext = createCriteriaContext(ConceptMap.class);
criteriaContext.getCriteriaQuery().select(criteriaContext.getRoot().get("concept"));
Join<ConceptMap, ConceptReferenceTerm> 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -56,11 +57,17 @@ 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().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
Expand All @@ -70,13 +77,18 @@ 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().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
Expand Down
Loading

0 comments on commit 3160a80

Please sign in to comment.