Skip to content

Commit

Permalink
Less mess
Browse files Browse the repository at this point in the history
  • Loading branch information
ibacher committed Jan 19, 2024
1 parent a443799 commit 8604017
Show file tree
Hide file tree
Showing 10 changed files with 139 additions and 143 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ protected Optional<Predicate> handleOrListParamBySystem(CriteriaBuilder criteria
* @return a {@link Criterion} to be added to the query indicating that the property matches the
* given value
*/
protected <T> Optional<Predicate> handleBoolean(OpenmrsFhirCriteriaContext<T> criteriaContext, String propertyName,
protected <T, U> Optional<Predicate> handleBoolean(OpenmrsFhirCriteriaContext<T, U> criteriaContext, String propertyName,
TokenAndListParam booleanToken) {
if (booleanToken == null) {
return Optional.empty();
Expand All @@ -357,7 +357,7 @@ protected <T> Optional<Predicate> handleBoolean(OpenmrsFhirCriteriaContext<T> cr
});
}

protected <T> Optional<Predicate> handleBooleanProperty(OpenmrsFhirCriteriaContext<T> criteriaContext,
protected <T, U> Optional<Predicate> handleBooleanProperty(OpenmrsFhirCriteriaContext<T, U> criteriaContext,
String propertyName, boolean booleanVal) {
return Optional
.of(criteriaContext.getCriteriaBuilder().equal(criteriaContext.getRoot().get(propertyName), booleanVal));
Expand All @@ -370,7 +370,7 @@ protected <T> Optional<Predicate> handleBooleanProperty(OpenmrsFhirCriteriaConte
* @param dateRangeParam the {@link DateRangeParam} to handle
* @return a {@link Criterion} to be added to the query for the indicated date range
*/
protected <T> Optional<Predicate> handleDateRange(OpenmrsFhirCriteriaContext<T> criteriaContext, String propertyName,
protected <T, U> Optional<Predicate> handleDateRange(OpenmrsFhirCriteriaContext<T, U> criteriaContext, String propertyName,
DateRangeParam dateRangeParam) {
if (dateRangeParam == null) {
return Optional.empty();
Expand All @@ -388,7 +388,7 @@ protected <T> Optional<Predicate> handleDateRange(OpenmrsFhirCriteriaContext<T>
* @param dateParam the {@link DateParam} to handle
* @return a {@link Predicate} to be added to the query for the indicate date param
*/
protected <T> Optional<Predicate> handleDate(OpenmrsFhirCriteriaContext<T> criteriaContext, String propertyName,
protected <T, U> Optional<Predicate> handleDate(OpenmrsFhirCriteriaContext<T, U> criteriaContext, String propertyName,
DateParam dateParam) {
if (dateParam == null) {
return Optional.empty();
Expand Down Expand Up @@ -434,7 +434,7 @@ protected <T> Optional<Predicate> handleDate(OpenmrsFhirCriteriaContext<T> crite
return Optional.empty();
}

protected <T> Optional<Predicate> handleQuantity(OpenmrsFhirCriteriaContext<T> criteriaContext, String propertyName,
protected <T, U> Optional<Predicate> handleQuantity(OpenmrsFhirCriteriaContext<T, U> criteriaContext, String propertyName,
QuantityParam quantityParam) {
if (quantityParam == null) {
return Optional.empty();
Expand Down Expand Up @@ -487,7 +487,7 @@ protected <T> Optional<Predicate> handleQuantity(OpenmrsFhirCriteriaContext<T> c
return Optional.empty();
}

protected <T> Optional<Predicate> handleQuantity(OpenmrsFhirCriteriaContext<T> criteriaContext,
protected <T, U> Optional<Predicate> handleQuantity(OpenmrsFhirCriteriaContext<T, U> criteriaContext,
@Nonnull String propertyName, QuantityAndListParam quantityAndListParam) {
if (quantityAndListParam == null) {
return Optional.empty();
Expand All @@ -497,12 +497,12 @@ protected <T> Optional<Predicate> handleQuantity(OpenmrsFhirCriteriaContext<T> c
quantityParam -> handleQuantity(criteriaContext, propertyName, quantityParam));
}

protected <T> void handleEncounterReference(OpenmrsFhirCriteriaContext<T> criteriaContext,
protected <T, U> void handleEncounterReference(OpenmrsFhirCriteriaContext<T, U> criteriaContext,
ReferenceAndListParam encounterReference, @Nonnull String encounterAlias) {
handleEncounterReference(criteriaContext, encounterReference, encounterAlias, "encounter");
}

protected <T> void handleEncounterReference(OpenmrsFhirCriteriaContext<T> criteriaContext,
protected <T, U> void handleEncounterReference(OpenmrsFhirCriteriaContext<T, U> criteriaContext,
ReferenceAndListParam encounterReference, @Nonnull String encounterAlias, @Nonnull String associationPath) {

if (encounterReference == null) {
Expand All @@ -528,7 +528,7 @@ protected <T> void handleEncounterReference(OpenmrsFhirCriteriaContext<T> criter
}).ifPresent(criteriaContext::addPredicate);
}

protected <T> Optional<Predicate> handleGender(OpenmrsFhirCriteriaContext<T> criteriaContext,
protected <T, U> Optional<Predicate> handleGender(OpenmrsFhirCriteriaContext<T, U> criteriaContext, From<?, ?> from,
@Nonnull String propertyName, TokenAndListParam gender) {
if (gender == null) {
return Optional.empty();
Expand All @@ -539,27 +539,22 @@ protected <T> Optional<Predicate> handleGender(OpenmrsFhirCriteriaContext<T> cri
AdministrativeGender administrativeGender = AdministrativeGender.fromCode(token.getValue());

if (administrativeGender == null) {
return Optional
.of(criteriaContext.getCriteriaBuilder().isNull(criteriaContext.getRoot().get(propertyName)));
return Optional.of(criteriaContext.getCriteriaBuilder().isNull(from.get(propertyName)));
}

switch (administrativeGender) {
case MALE:
return Optional.of(
criteriaContext.getCriteriaBuilder().like(criteriaContext.getRoot().get(propertyName), "M"));
return Optional.of(criteriaContext.getCriteriaBuilder().like(from.get(propertyName), "M"));
case FEMALE:
return Optional.of(
criteriaContext.getCriteriaBuilder().like(criteriaContext.getRoot().get(propertyName), "F"));
return Optional.of(criteriaContext.getCriteriaBuilder().like(from.get(propertyName), "F"));
case OTHER:
case UNKNOWN:
case NULL:
return Optional.of(
criteriaContext.getCriteriaBuilder().isNull(criteriaContext.getRoot().get(propertyName)));
return Optional.of(criteriaContext.getCriteriaBuilder().isNull(from.get(propertyName)));
}
}
catch (FHIRException ignored) {}
return Optional.of(
criteriaContext.getCriteriaBuilder().like(criteriaContext.getRoot().get(propertyName), token.getValue()));
return Optional.of(criteriaContext.getCriteriaBuilder().like(from.get(propertyName), token.getValue()));
});
}

Expand Down Expand Up @@ -674,7 +669,7 @@ protected <T> void handleParticipantReference(OpenmrsFhirCriteriaContext<T> crit
protected <T> void handleProviderReference(OpenmrsFhirCriteriaContext<T> criteriaContext,
ReferenceAndListParam providerReference) {
if (providerReference != null) {
Join<?, ?> orderer = criteriaContext.addJoin("orderer", "or");
Join<?, ?> orderer = criteriaContext.addJoin("orderer", "ord");

handleAndListParam(criteriaContext.getCriteriaBuilder(), providerReference, participantToken -> {
if (participantToken.getChain() != null) {
Expand All @@ -693,7 +688,7 @@ protected <T> void handleProviderReference(OpenmrsFhirCriteriaContext<T> criteri
Join<?, ?> ordererPerson = criteriaContext.addJoin(orderer, "person", "ps");
Join<?, ?> ordererName = criteriaContext.addJoin(ordererPerson, "names", "pn");

return Optional.of(criteriaContext.getCriteriaBuilder().like(ordererName.get("pn.familyName"),
return Optional.of(criteriaContext.getCriteriaBuilder().like(ordererName.get("familyName"),
participantToken.getValue()));
}
case Practitioner.SP_NAME: {
Expand Down Expand Up @@ -727,6 +722,7 @@ protected <T> Optional<Predicate> handleCodeableConcept(OpenmrsFhirCriteriaConte
if (concepts == null) {
return Optional.empty();
}

Optional<Join<?, ?>> conceptAliasJoin = criteriaContext.getJoin(conceptAlias);
return conceptAliasJoin
.map(join -> handleAndListParamBySystem(criteriaContext.getCriteriaBuilder(), concepts, (system, tokens) -> {
Expand Down Expand Up @@ -756,26 +752,18 @@ protected <T> Optional<Predicate> handleCodeableConcept(OpenmrsFhirCriteriaConte

protected <T> void handleNames(OpenmrsFhirCriteriaContext<T> criteriaContext, StringAndListParam name,
StringAndListParam given, StringAndListParam family) {
handleNames(criteriaContext, name, given, family, null);
handleNames(criteriaContext, name, given, family, criteriaContext.getRoot());
}

protected <T> void handleNames(OpenmrsFhirCriteriaContext<T> criteriaContext, StringAndListParam name,
StringAndListParam given, StringAndListParam family, String personAlias) {
StringAndListParam given, StringAndListParam family, From<?, ?> person) {

if (name == null && given == null && family == null) {
return;
}

Join<?, ?> personAliasJoin = criteriaContext.getRoot().join(personAlias);
Join<?, ?> personNameAliasJoin = criteriaContext.addJoin(personAliasJoin, "names", "pn");

if (!criteriaContext.getJoin("pn").isPresent()) {
if (StringUtils.isNotBlank(personAlias)) {
criteriaContext.getCriteriaBuilder().equal(personNameAliasJoin.get("voided"), false);
} else {
criteriaContext.getCriteriaBuilder().equal(personNameAliasJoin.get("voided"), false);
}
}
Join<?, ?> personNameAliasJoin = criteriaContext.addJoin(person, "names", "pn",
(personNameAlias) -> criteriaContext.getCriteriaBuilder().equal(personNameAlias.get("voided"), false));

if (name != null) {
handleAndListParamAsStream(criteriaContext.getCriteriaBuilder(), name,
Expand Down Expand Up @@ -818,23 +806,27 @@ protected <T> void handlePatientReference(OpenmrsFhirCriteriaContext<T> criteria
if (patientToken.getChain() != null) {
switch (patientToken.getChain()) {
case Patient.SP_IDENTIFIER:
@SuppressWarnings("unchecked")
Join<?, ?> associationPathIdentifiersJoin = criteriaContext.addJoin(associationPathJoin,
"identifiers", "pi");
return Optional.of(criteriaContext.getCriteriaBuilder()
.like(associationPathIdentifiersJoin.get("identifier"), patientToken.getValue()));
case Patient.SP_GIVEN: {
@SuppressWarnings("unchecked")
Join<?, ?> associationPathNamesJoin = criteriaContext.addJoin(associationPathJoin, "names",
"pn");
return Optional.of(criteriaContext.getCriteriaBuilder()
.like(associationPathNamesJoin.get("givenName"), patientToken.getValue()));
}
case Patient.SP_FAMILY: {
@SuppressWarnings("unchecked")
Join<?, ?> associationPathNamesJoin = criteriaContext.addJoin(associationPathJoin, "names",
"pn");
return Optional.of(criteriaContext.getCriteriaBuilder()
.like(associationPathNamesJoin.get("familyName"), patientToken.getValue()));
}
case Patient.SP_NAME:
@SuppressWarnings("unchecked")
Join<?, ?> associationPathNamesJoin = criteriaContext.addJoin(associationPathJoin, "names",
"pn");

Expand All @@ -860,7 +852,7 @@ protected <T> void handlePatientReference(OpenmrsFhirCriteriaContext<T> criteria
}
}

protected <T> Optional<Predicate> handleCommonSearchParameters(OpenmrsFhirCriteriaContext<T> criteriaContext,
protected <T, U> Optional<Predicate> handleCommonSearchParameters(OpenmrsFhirCriteriaContext<T, U> criteriaContext,
List<PropParam<?>> theCommonParams) {
List<Optional<? extends Predicate>> criterionList = new ArrayList<>();

Expand All @@ -886,10 +878,10 @@ protected <T> Optional<Predicate> handleCommonSearchParameters(OpenmrsFhirCriter
* @param param the DateRangeParam used to query for _lastUpdated
* @return an optional criterion for the query
*/
protected abstract <T> Optional<Predicate> handleLastUpdated(OpenmrsFhirCriteriaContext<T> criteriaContext,
protected abstract <T, U> Optional<Predicate> handleLastUpdated(OpenmrsFhirCriteriaContext<T, U> criteriaContext,
DateRangeParam param);

protected <T> Optional<Predicate> handlePersonAddress(OpenmrsFhirCriteriaContext<T> criteriaContext, String aliasPrefix,
protected <T, U> Optional<Predicate> handlePersonAddress(OpenmrsFhirCriteriaContext<T, U> criteriaContext, String aliasPrefix,
StringAndListParam city, StringAndListParam state, StringAndListParam postalCode, StringAndListParam country) {
if (city == null && state == null && postalCode == null && country == null) {
return Optional.empty();
Expand Down Expand Up @@ -1318,18 +1310,18 @@ protected <T> Optional<Predicate> handleAgeByDateProperty(OpenmrsFhirCriteriaCon
return Optional.empty();
}

protected <T> OpenmrsFhirCriteriaContext<T> createCriteriaContext(Class<? super T> rootType) {
protected <T, U> OpenmrsFhirCriteriaContext<T, U> createCriteriaContext(Class<? super T> rootType) {
EntityManager em = sessionFactory.getCurrentSession();
CriteriaBuilder cb = em.getCriteriaBuilder();
@SuppressWarnings("unchecked")
CriteriaQuery<T> cq = (CriteriaQuery<T>) cb.createQuery(rootType);
CriteriaQuery<U> cq = (CriteriaQuery<U>) cb.createQuery(rootType);
@SuppressWarnings("unchecked")
Root<T> root = (Root<T>) cq.from(rootType);

return new OpenmrsFhirCriteriaContext<>(em, cb, cq, root);
}

protected <T> From<?, ?> getRootOrJoin(OpenmrsFhirCriteriaContext<T> criteriaContext, String alias) {
protected <T, U> From<?, ?> getRootOrJoin(OpenmrsFhirCriteriaContext<T, U> criteriaContext, String alias) {
if (alias.isEmpty()) {
return criteriaContext.getRoot();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ protected BaseFhirDao() {
@Transactional(readOnly = true)
public T get(@Nonnull String uuid) {
@SuppressWarnings({ "UnstableApiUsage" })
OpenmrsFhirCriteriaContext<T> criteriaContext = createCriteriaContext(typeToken.getRawType());
OpenmrsFhirCriteriaContext<T, T> criteriaContext = createCriteriaContext(typeToken.getRawType());

criteriaContext.getCriteriaQuery().select(criteriaContext.getRoot());
criteriaContext
Expand All @@ -110,7 +110,7 @@ public T get(@Nonnull String uuid) {
@Transactional(readOnly = true)
public List<T> get(@Nonnull Collection<String> uuids) {
@SuppressWarnings({ "UnstableApiUsage" })
OpenmrsFhirCriteriaContext<T> criteriaContext = createCriteriaContext(typeToken.getRawType());
OpenmrsFhirCriteriaContext<T, T> criteriaContext = createCriteriaContext(typeToken.getRawType());

criteriaContext.getCriteriaQuery().select(criteriaContext.getRoot());
criteriaContext.addPredicate(criteriaContext.getRoot().get("uuid").in(uuids));
Expand Down Expand Up @@ -152,8 +152,8 @@ public T delete(@Nonnull String uuid) {
}

@SuppressWarnings("UnstableApiUsage")
protected OpenmrsFhirCriteriaContext<T> getSearchResultCriteria(SearchParameterMap theParams) {
OpenmrsFhirCriteriaContext<T> criteriaContext = createCriteriaContext(typeToken.getRawType());
protected <U> OpenmrsFhirCriteriaContext<T, U> getSearchResultCriteria(SearchParameterMap theParams) {
OpenmrsFhirCriteriaContext<T, U> criteriaContext = createCriteriaContext(typeToken.getRawType());

if (isVoidable) {
handleVoidable(criteriaContext);
Expand All @@ -169,7 +169,7 @@ protected OpenmrsFhirCriteriaContext<T> getSearchResultCriteria(SearchParameterM
@Override
@SuppressWarnings({ "unchecked", "UnstableApiUsage" })
public List<T> getSearchResults(@Nonnull SearchParameterMap theParams) {
OpenmrsFhirCriteriaContext<T> criteriaContext = getSearchResultCriteria(theParams);
OpenmrsFhirCriteriaContext<T, T> criteriaContext = getSearchResultCriteria(theParams);

handleSort(criteriaContext, theParams.getSortSpec());

Expand Down Expand Up @@ -257,29 +257,25 @@ public List<T> getSearchResults(@Nonnull SearchParameterMap theParams) {
}

@Override
@SuppressWarnings({ "unchecked", "UnstableApiUsage" })
@SuppressWarnings({ "UnstableApiUsage" })
public int getSearchResultsCount(@Nonnull SearchParameterMap theParams) {
OpenmrsFhirCriteriaContext<T> criteriaContext = getSearchResultCriteria(theParams);
OpenmrsFhirCriteriaContext<T, Long> criteriaContext = getSearchResultCriteria(theParams);
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()) {
criteriaQuery.select(criteriaBuilder.count(root));
criteriaContext.getCriteriaQuery().select(criteriaContext.getCriteriaBuilder().count(criteriaContext.getRoot()));
} else {
//the id property differs across various openmrs entities
if (Person.class.isAssignableFrom(typeToken.getRawType())) {
criteriaQuery.select(criteriaBuilder.countDistinct(root.get("personId")));
criteriaContext.getCriteriaQuery().select(criteriaContext.getCriteriaBuilder().countDistinct(criteriaContext.getRoot().get("personId")));
} else if (Encounter.class.isAssignableFrom(typeToken.getRawType())) {
criteriaQuery.select(criteriaBuilder.countDistinct(root.get("encounterId")));
criteriaContext.getCriteriaQuery().select(criteriaContext.getCriteriaBuilder().countDistinct(criteriaContext.getRoot().get("encounterId")));
} else if (Obs.class.isAssignableFrom(typeToken.getRawType())) {
criteriaQuery.select(criteriaBuilder.countDistinct(root.get("obsId")));
criteriaContext.getCriteriaQuery().select(criteriaContext.getCriteriaBuilder().countDistinct(criteriaContext.getRoot().get("obsId")));
}
}
return em.createQuery(criteriaQuery).getSingleResult().intValue();

return criteriaContext.getEntityManager().createQuery(criteriaContext.getCriteriaQuery()).getSingleResult().intValue();
}

/**
Expand All @@ -298,7 +294,7 @@ protected void createAlias(OpenmrsFhirCriteriaContext<T> criteriaContext, String
}

@Override
protected <T> Optional<Predicate> handleLastUpdated(OpenmrsFhirCriteriaContext<T> criteriaContext,
protected <T, U> Optional<Predicate> handleLastUpdated(OpenmrsFhirCriteriaContext<T, U> criteriaContext,
DateRangeParam param) {
if (isImmutable) {
return handleLastUpdatedImmutable(criteriaContext, param);
Expand Down Expand Up @@ -329,7 +325,7 @@ protected <T> Optional<Predicate> handleLastUpdatedImmutable(OpenmrsFhirCriteria
*/
protected void handleVoidable(OpenmrsFhirCriteriaContext<T> criteriaContext) {
criteriaContext
.addPredicate(criteriaContext.getCriteriaBuilder().equal(criteriaContext.getRoot().get("voided"), true));
.addPredicate(criteriaContext.getCriteriaBuilder().equal(criteriaContext.getRoot().get("voided"), false));
}

/**
Expand All @@ -340,7 +336,7 @@ protected void handleVoidable(OpenmrsFhirCriteriaContext<T> criteriaContext) {
*/
protected void handleRetireable(OpenmrsFhirCriteriaContext<T> criteriaContext) {
criteriaContext
.addPredicate(criteriaContext.getCriteriaBuilder().equal(criteriaContext.getRoot().get("retired"), true));
.addPredicate(criteriaContext.getCriteriaBuilder().equal(criteriaContext.getRoot().get("retired"), false));
}

/**
Expand Down Expand Up @@ -372,12 +368,12 @@ protected T retireObject(T object) {
* @param criteriaContext The {@link OpenmrsFhirCriteriaContext} for the current query
* @param theParams the parameters for this search
*/
protected void setupSearchParams(OpenmrsFhirCriteriaContext<T> criteriaContext, SearchParameterMap theParams) {
protected <U> void setupSearchParams(OpenmrsFhirCriteriaContext<T, U> criteriaContext, SearchParameterMap theParams) {

}

@Override
protected <V> Collection<javax.persistence.criteria.Order> paramToProps(OpenmrsFhirCriteriaContext<V> criteriaContext,
protected <V, U> Collection<javax.persistence.criteria.Order> paramToProps(OpenmrsFhirCriteriaContext<V, U> criteriaContext,
@Nonnull SortState sortState) {
String param = sortState.getParameter();

Expand Down
Loading

0 comments on commit 8604017

Please sign in to comment.