Skip to content

Commit

Permalink
fm2-347:new changes
Browse files Browse the repository at this point in the history
  • Loading branch information
gitcliff committed Jun 8, 2021
1 parent 81af1aa commit 02ffea8
Show file tree
Hide file tree
Showing 12 changed files with 414 additions and 157 deletions.
2 changes: 2 additions & 0 deletions api/src/main/java/org/openmrs/module/fhir2/FhirConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -296,4 +296,6 @@ public class FhirConstants {
public static final String REVERSE_INCLUDE_SEARCH_HANDLER = "_revinclude.search.handler";

public static final String CONDITION_OBSERVATION_CONCEPT_UUID = "1284AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";

public static final Object PARAMQUALIFIER_STRING_NOT = ":not";
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,15 @@
*/
package org.openmrs.module.fhir2.api.dao.impl;

import static org.hibernate.criterion.Restrictions.and;
import static org.hibernate.criterion.Restrictions.eq;

import javax.annotation.Nonnull;

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

import ca.uhn.fhir.rest.param.DateRangeParam;
import ca.uhn.fhir.rest.param.QuantityAndListParam;
import ca.uhn.fhir.rest.param.ReferenceAndListParam;
import ca.uhn.fhir.rest.param.ReferenceParam;
import ca.uhn.fhir.rest.param.StringAndListParam;
import ca.uhn.fhir.rest.param.TokenAndListParam;
import ca.uhn.fhir.rest.param.TokenParam;
Expand Down Expand Up @@ -64,8 +60,8 @@ protected void setupSearchParams(Criteria criteria, SearchParameterMap theParams
.forEach(category -> handleConceptClass(criteria, (TokenAndListParam) category.getParam()));
break;
case FhirConstants.BASED_ON_REFERENCE_SEARCH_HANDLER:
entry.getValue().forEach(param -> handleReference(criteria, (ReferenceAndListParam) param.getParam(),
"basedOnReferences", "bo"));
entry.getValue().forEach(param -> handleReference("ord", (ReferenceAndListParam) param.getParam())
.ifPresent(c -> criteria.createAlias("order", "ord").add(c)));
break;
case FhirConstants.VALUE_CODED_SEARCH_HANDLER:
entry.getValue().forEach(
Expand Down Expand Up @@ -169,25 +165,12 @@ private void handleValueCodedConcept(Criteria criteria, TokenAndListParam valueC
}
}

private Boolean validReferenceParam(ReferenceParam ref) {
return (ref != null && ref.getIdPart() != null && ref.getResourceType() != null);
}

private void handleReference(Criteria criteria, ReferenceAndListParam reference, String property, String alias) {
handleAndListParam(reference, param -> {
if (validReferenceParam(param)) {
if (lacksAlias(criteria, alias)) {
criteria.createAlias(property, alias);
}

List<Optional<Criterion>> criterionList = new ArrayList<>();
criterionList.add(Optional.of(eq(String.format("%s.reference", alias), param.getIdPart())));
criterionList.add(Optional.of(eq(String.format("%s.type", alias), param.getResourceType())));
return Optional.of(and(toCriteriaArray(criterionList)));
}

private Optional<Criterion> handleReference(@Nonnull String orderAlias, ReferenceAndListParam orderReference) {
if (orderReference == null) {
return Optional.empty();
}).ifPresent(criteria::add);
}
return handleAndListParam(orderReference,
param -> Optional.of(eq(String.format("%s.uuid", orderAlias), param.getIdPart())));
}

@Override
Expand All @@ -204,5 +187,5 @@ protected Obs deproxyResult(Obs result) {
Obs obs = super.deproxyResult(result);
obs.setConcept(deproxyObject(obs.getConcept()));
return obs;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

import static org.hibernate.criterion.Restrictions.and;
import static org.hibernate.criterion.Restrictions.or;
import static org.hibernate.criterion.Restrictions.eq;

import java.util.Optional;
import java.util.stream.Stream;
Expand All @@ -23,12 +22,14 @@
import ca.uhn.fhir.rest.param.HasParam;
import ca.uhn.fhir.rest.param.ReferenceAndListParam;
import ca.uhn.fhir.rest.param.TokenAndListParam;
import org.hibernate.criterion.Subqueries;
import lombok.AccessLevel;
import lombok.Setter;
import org.hibernate.Criteria;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.hibernate.criterion.Subqueries;
import org.openmrs.Obs;
import org.openmrs.TestOrder;
import org.openmrs.module.fhir2.FhirConstants;
Expand Down Expand Up @@ -66,8 +67,8 @@ protected void setupSearchParams(Criteria criteria, SearchParameterMap theParams
(ReferenceAndListParam) participantReference.getParam()));
break;
case FhirConstants.HAS_PROPERTY:
entry.getValue().forEach(hasParameter -> handleHasAndParam(criteria,
(HasAndListParam) hasParameter.getParam()));
entry.getValue()
.forEach(hasParameter -> handleHasAndParam(criteria, (HasAndListParam) hasParameter.getParam()));
break;
case FhirConstants.DATE_RANGE_SEARCH_HANDLER:
entry.getValue().forEach(dateRangeParam -> handleDateRange((DateRangeParam) dateRangeParam.getParam())
Expand Down Expand Up @@ -101,54 +102,71 @@ private Optional<Criterion> handleDateRange(DateRangeParam dateRangeParam) {
Optional.of(or(toCriteriaArray(Stream.of(handleDate("dateStopped", dateRangeParam.getUpperBound()),
handleDate("autoExpireDate", dateRangeParam.getUpperBound())))))))));
}

private void handleHasAndParam(Criteria criteria, HasAndListParam hasAndListParam) {
if (hasAndListParam != null) {
if (hasAndListParam == null) {
return;
}

HasParam param = hasAndListParam.getValuesAsQueryTokens().get(0).getValuesAsQueryTokens().get(0);

DetachedCriteria criteria2 = DetachedCriteria.forClass(Obs.class, "obs");
if (!FhirConstants.OBSERVATION.equals(param.getTargetResourceType())) {
return;
}
if (param.getParameterName().equals("encounter")) {
criteria2.createCriteria("encounter").add(Restrictions.eq("uuid", param.getParameterValue().toString()));
criteria2.setProjection(Projections.property("obs.order"));
criteria.add(Subqueries.propertyIn("id", criteria2));
} else if (param.getParameterName().equals("person")) {
criteria2.createCriteria("person").add(Restrictions.eq("uuid", param.getParameterValue().toString()));
criteria2.setProjection(Projections.property("obs.order"));
criteria.add(Subqueries.propertyIn("id", criteria2));
} else if (param.getParameterName().equals("category")) {
criteria2.createCriteria("concept").createCriteria("conceptClass")
.add(Restrictions.eq("name", param.getParameterValue().toString()));
criteria2.setProjection(Projections.property("obs.order"));
criteria.add(Subqueries.propertyIn("id", criteria2));
} else if (param.getParameterName().equals("code")) {
criteria2.createCriteria("concept")
.add(Restrictions.eq("id", Integer.parseInt(param.getParameterValue().toString())));
criteria2.setProjection(Projections.property("obs.order"));
criteria.add(Subqueries.propertyIn("id", criteria2));
} else {
return;
}

if (lacksAlias(criteria, "obss")) {
criteria.createAlias("obs", "aliasObss");
}

DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Obs.class);

handleAndListParam(hasAndListParam, param -> {
if ( param.getValueAsQueryToken(fhirContext) != null) {
if (lacksAlias(criteria, "o")) {
criteria.createAlias("aliasObss.category", "o");
}
}

return Optional.of(eq("o.category", param.getValueAsQueryToken(fhirContext)));
}).ifPresent(criteria::add);

hasAndListParam.getValuesAsQueryTokens().stream().map(this::handleHasOrParam).filter(Optional::isPresent)
.flatMap(Optional::get).forEach(detachedCriteria::add);


criteria.add(Subqueries.exists(detachedCriteria));
}
.flatMap(Optional::get).forEach(criteria2::add);

criteria.add(Subqueries.exists(criteria2));

}

private Optional<Stream<Criterion>> handleHasOrParam(HasOrListParam hasOrListParam) {
if (hasOrListParam == null) {
return Optional.empty();
}

return Optional.of(hasOrListParam.getValuesAsQueryTokens().stream().map(this::handleHasParam).filter(
Optional::isPresent).map(Optional::get));
return Optional.of(hasOrListParam.getValuesAsQueryTokens().stream().map(this::handleHasParam)
.filter(Optional::isPresent).map(Optional::get));
}

private Optional<Criterion> handleHasParam(HasParam hasParam) {
if (hasParam == null) {
return Optional.empty();
}

if (!FhirConstants.OBSERVATION.equals(hasParam.getTargetResourceType())) {
return Optional.empty();
}
if(!(hasParam.getQueryParameterQualifier().equals(":not"))){
if (!(hasParam.getQueryParameterQualifier().equals(FhirConstants.PARAMQUALIFIER_STRING_NOT))) {
return Optional.empty();
} else {
hasParam.setValueAsQueryToken(fhirContext, hasParam.getParameterName(), hasParam.getQueryParameterQualifier(),
hasParam.getParameterValue());
}
return Optional.empty();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ public IBundleProvider searchForServiceRequests(ReferenceAndListParam patientRef

SearchParameterMap theParams = new SearchParameterMap()
.addParameter(FhirConstants.PATIENT_REFERENCE_SEARCH_HANDLER, patientReference)
.addParameter(FhirConstants.CODED_SEARCH_HANDLER, code)
.addParameter(FhirConstants.HAS_PROPERTY, has)
.addParameter(FhirConstants.CODED_SEARCH_HANDLER, code).addParameter(FhirConstants.HAS_PROPERTY, has)
.addParameter(FhirConstants.ENCOUNTER_REFERENCE_SEARCH_HANDLER, encounterReference)
.addParameter(FhirConstants.PARTICIPANT_REFERENCE_SEARCH_HANDLER, participantReference)
.addParameter(FhirConstants.DATE_RANGE_SEARCH_HANDLER, occurrence)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ public IBundleProvider searchForServiceRequests(
@OptionalParam(name = ServiceRequest.SP_REQUESTER, chainWhitelist = { "", Practitioner.SP_IDENTIFIER,
Practitioner.SP_GIVEN, Practitioner.SP_FAMILY,
Practitioner.SP_NAME }, targetTypes = Practitioner.class) ReferenceAndListParam participantReference,
@OptionalParam(name = ServiceRequest.SP_OCCURRENCE) DateRangeParam occurrence,

@OptionalParam(name = ServiceRequest.SP_OCCURRENCE) DateRangeParam occurrence,
@OptionalParam(name = ServiceRequest.SP_RES_ID) TokenAndListParam uuid,
@OptionalParam(name = "_lastUpdated") DateRangeParam lastUpdated,
@OptionalParam(name = "_has") HasAndListParam has,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@
import ca.uhn.fhir.model.api.Include;
import ca.uhn.fhir.rest.api.server.IBundleProvider;
import ca.uhn.fhir.rest.param.DateRangeParam;
import ca.uhn.fhir.rest.param.HasAndListParam;
import ca.uhn.fhir.rest.param.HasOrListParam;
import ca.uhn.fhir.rest.param.HasParam;
import ca.uhn.fhir.rest.param.ReferenceAndListParam;
import ca.uhn.fhir.rest.param.ReferenceOrListParam;
import ca.uhn.fhir.rest.param.ReferenceParam;
Expand Down Expand Up @@ -339,29 +336,6 @@ public void searchForPeople_shouldAddRelatedResourcesWhenIncluded() {
assertThat(resultList, hasItem(is(instanceOf(Patient.class))));
}

@Test
public void searchForServiceRequest_shouldReturnCollectionOfServiceRequestByHasParameter() {
HasParam hasParam = new HasParam("Observation", "based-on", "category", ":not=laboratory");
HasOrListParam hasOrListParam = new HasOrListParam();
HasAndListParam uuid = new HasAndListParam().addAnd(hasOrListParam.add(hasParam));
SearchParameterMap theParams = new SearchParameterMap().addParameter(FhirConstants.HAS_PROPERTY, hasParam);

when(dao.getSearchResults(any(), any())).thenReturn(Collections.singletonList(order));
when(dao.getSearchResultUuids(any())).thenReturn(Collections.singletonList(SERVICE_REQUEST_UUID));
when(translator.toFhirResource(order)).thenReturn(fhirServiceRequest);
when(searchQuery.getQueryResults(any(), any(), any(), any())).thenReturn(
new SearchQueryBundleProvider<>(theParams, dao, translator, globalPropertyService, searchQueryInclude));
when(searchQueryInclude.getIncludedResources(any(), any())).thenReturn(Collections.emptySet());

IBundleProvider results = serviceRequestService.searchForServiceRequests(null, null, null, null, null, null, null,
uuid, null);
List<IBaseResource> resultList = get(results);

assertThat(results, notNullValue());
assertThat(resultList, not(empty()));
assertThat(resultList.size(), equalTo(1));
}

@Test
public void searchForPeople_shouldAddRelatedResourcesWhenIncludedR3() {
HashSet<Include> includes = new HashSet<>();
Expand Down
Loading

0 comments on commit 02ffea8

Please sign in to comment.