Skip to content

Commit

Permalink
Start of work on subqueries
Browse files Browse the repository at this point in the history
TODO Fix handleCodeableConcept
  • Loading branch information
ibacher committed Feb 9, 2024
1 parent 7e5b921 commit f323d34
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -704,35 +704,35 @@ protected <T, U> void handleProviderReference(OpenmrsFhirCriteriaContext<T, U> c
protected <T, U> Optional<Predicate> handleCodeableConcept(OpenmrsFhirCriteriaContext<T, U> criteriaContext,
TokenAndListParam concepts, @Nonnull From<?, ?> ConceptAliasJoin, @Nonnull String conceptMapAlias,
@Nonnull String conceptReferenceTermAlias) {
if (concepts == null) {
return Optional.empty();
}

Optional<Join<?, ?>> conceptAliasJoin = criteriaContext.getJoin(ConceptAliasJoin.getAlias());
return conceptAliasJoin
.map(join -> handleAndListParamBySystem(criteriaContext.getCriteriaBuilder(), concepts, (system, tokens) -> {
if (system.isEmpty()) {
criteriaContext.getCriteriaBuilder()
.literal(tokensToParams(tokens).map(NumberUtils::toInt).collect(Collectors.toList()));
return Optional
.of(criteriaContext.getCriteriaBuilder().or(
criteriaContext.getCriteriaBuilder()
.in(join.get("conceptId")
.in(criteriaContext.getCriteriaBuilder()
.literal(tokensToParams(tokens).map(NumberUtils::toInt)
.collect(Collectors.toList())))),
criteriaContext.getCriteriaBuilder().in(join.get("uuid")
.in(criteriaContext.getCriteriaBuilder().literal(tokensToList(tokens))))));
} else {
Join<?, ?> conceptMapAliasJoin = criteriaContext.addJoin(join, "conceptMappings", conceptMapAlias);
criteriaContext.addJoin(conceptMapAliasJoin, "conceptReferenceTerm", conceptReferenceTermAlias);
return Optional.of(
generateSystemQuery(criteriaContext, system, tokensToList(tokens), conceptReferenceTermAlias));
}
})).orElse(null);

// if (concepts == null) {
// return Optional.empty();
// }
//
// Optional<Join<?, ?>> conceptAliasJoin = criteriaContext.getJoin(ConceptAliasJoin.getAlias());
// return conceptAliasJoin
// .map(join -> handleAndListParamBySystem(criteriaContext.getCriteriaBuilder(), concepts, (system, tokens) -> {
// if (system.isEmpty()) {
// criteriaContext.getCriteriaBuilder()
// .literal(tokensToParams(tokens).map(NumberUtils::toInt).collect(Collectors.toList()));
// return Optional
// .of(criteriaContext.getCriteriaBuilder().or(
// criteriaContext.getCriteriaBuilder()
// .in(join.get("conceptId")
// .in(criteriaContext.getCriteriaBuilder()
// .literal(tokensToParams(tokens).map(NumberUtils::toInt)
// .collect(Collectors.toList())))),
// criteriaContext.getCriteriaBuilder().in(join.get("uuid")
// .in(criteriaContext.getCriteriaBuilder().literal(tokensToList(tokens))))));
//
// } else {
// Join<?, ?> conceptMapAliasJoin = criteriaContext.addJoin(join, "conceptMappings", conceptMapAlias);
// criteriaContext.addJoin(conceptMapAliasJoin, "conceptReferenceTerm", conceptReferenceTermAlias);
//
// return Optional.of(
// generateSystemQuery(criteriaContext, system, tokensToList(tokens), conceptReferenceTermAlias));
// }
// })).orElse(null);
return Optional.empty();
}

protected <T, U> void handleNames(OpenmrsFhirCriteriaContext<T, U> criteriaContext, StringAndListParam name,
Expand Down Expand Up @@ -950,31 +950,18 @@ protected <T, U> Optional<List<javax.persistence.criteria.Order>> handleSort(
return Optional.of(orderings);
}

@SuppressWarnings("unchecked")
protected <T, U> Predicate generateSystemQuery(OpenmrsFhirCriteriaContext<T, U> criteriaContext, String system,
protected <T, U> Optional<Predicate> generateSystemQuery(OpenmrsFhirCriteriaContext<T, U> criteriaContext, String system,
List<String> codes, String conceptReferenceTermAlias) {
//detached criteria
Specification<FhirConceptSource> spec = (root, query,
cb) -> (Predicate) query.select(root.get("conceptSource")).where(cb.equal(root.get("url"), system));

criteriaContext.getCriteriaQuery().where(spec.toPredicate((Root<FhirConceptSource>) criteriaContext.getRoot(),
(CriteriaQuery<FhirConceptSource>) criteriaContext.getCriteriaQuery(), criteriaContext.getCriteriaBuilder()));
OpenmrsFhirCriteriaSubquery<FhirConceptSource> conceptSourceSubquery = criteriaContext
.addSubquery(FhirConceptSource.class);
conceptSourceSubquery.addPredicate(
conceptSourceSubquery.getCriteriaBuilder().equal(conceptSourceSubquery.getRoot().get("url"), system));

if (codes.size() > 1) {
return criteriaContext.getCriteriaBuilder().and(
criteriaContext.getCriteriaBuilder().equal(
getRootOrJoin(criteriaContext, conceptReferenceTermAlias).get("conceptSource"),
criteriaContext.getCriteriaQuery()),
criteriaContext.getCriteriaBuilder().in(criteriaContext.getRoot()
.get(String.format("%s.code", conceptReferenceTermAlias)).get(codes.toString())));
} else {
return criteriaContext.getCriteriaBuilder().and(
criteriaContext.getCriteriaBuilder().equal(
getRootOrJoin(criteriaContext, conceptReferenceTermAlias).get("conceptSource"),
criteriaContext.getCriteriaQuery()),
criteriaContext.getCriteriaBuilder().equal(
criteriaContext.getRoot().get(String.format("%s.code", conceptReferenceTermAlias)), codes.get(0)));
}
return criteriaContext.getJoin(conceptReferenceTermAlias)
.map((conceptReferenceTermJoin) -> criteriaContext.getCriteriaBuilder().and(
criteriaContext.getCriteriaBuilder().in(conceptReferenceTermJoin.get("conceptSource"))
.value(conceptSourceSubquery.finalizeQuery()),
criteriaContext.getCriteriaBuilder().in(conceptReferenceTermJoin.get("code")).value(codes)));
}

protected <T, U> Predicate generateActiveOrderQuery(OpenmrsFhirCriteriaContext<T, U> criteriaContext, String path,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import javax.persistence.criteria.Order;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Subquery;

import java.util.ArrayList;
import java.util.LinkedHashMap;
Expand Down Expand Up @@ -118,6 +119,11 @@ public class OpenmrsFhirCriteriaContext<T, U> {
});
}

public <V> OpenmrsFhirCriteriaSubquery<V> addSubquery(Class<V> type) {
Subquery<V> subquery = criteriaQuery.subquery(type);
return new OpenmrsFhirCriteriaSubquery<>(criteriaBuilder, subquery, subquery.from(type));
}

public OpenmrsFhirCriteriaContext<T, U> addPredicate(Predicate predicate) {
predicates.add(predicate);
return this;
Expand All @@ -137,10 +143,6 @@ public OpenmrsFhirCriteriaContext<T, U> addResults(T result) {
return Optional.ofNullable(aliases.get(alias));
}

public boolean hasAlias(String alias) {
return aliases.containsKey(alias);
}

public CriteriaQuery<U> finalizeQuery() {
return criteriaQuery.where(predicates.toArray(new Predicate[0])).orderBy(orders);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
*
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
* graphic logo is a trademark of OpenMRS Inc.
*/
package org.openmrs.module.fhir2.api.dao.impl;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Subquery;

import java.util.ArrayList;
import java.util.List;

import lombok.Getter;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public class OpenmrsFhirCriteriaSubquery<Q> {

@Getter
@NonNull
private final CriteriaBuilder criteriaBuilder;

@Getter
@NonNull
Subquery<Q> subquery;

@Getter
@NonNull
Root<Q> root;

private final List<Predicate> predicates = new ArrayList<>();

public OpenmrsFhirCriteriaSubquery<Q> addPredicate(Predicate predicate) {
predicates.add(predicate);
return this;
}

public Subquery<Q> finalizeQuery() {
return subquery.where(predicates.toArray(new Predicate[0]));
}
}

0 comments on commit f323d34

Please sign in to comment.