Skip to content

Commit

Permalink
not sure
Browse files Browse the repository at this point in the history
  • Loading branch information
mherman22 committed Jan 20, 2024
1 parent c18e1ef commit 9bf6d22
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -569,24 +569,18 @@ protected <T,U> Optional<Predicate> handleLocationReference(OpenmrsFhirCriteriaC
if (token.getChain() != null) {
switch (token.getChain()) {
case Location.SP_NAME:
return propertyLike(criteriaContext, getRootOrJoin(criteriaContext, locationAlias.getAlias()), "name",
token.getValue());
return propertyLike(criteriaContext, locationAlias, "name", token.getValue());
case Location.SP_ADDRESS_CITY:
return propertyLike(criteriaContext, getRootOrJoin(criteriaContext, locationAlias.getAlias()), "cityVillage",
token.getValue());
return propertyLike(criteriaContext, locationAlias, "cityVillage", token.getValue());
case Location.SP_ADDRESS_STATE:
return propertyLike(criteriaContext, getRootOrJoin(criteriaContext, locationAlias.getAlias()), "stateProvince",
token.getValue());
return propertyLike(criteriaContext, locationAlias, "stateProvince", token.getValue());
case Location.SP_ADDRESS_POSTALCODE:
return propertyLike(criteriaContext, getRootOrJoin(criteriaContext, locationAlias.getAlias()), "postalCode",
token.getValue());
return propertyLike(criteriaContext, locationAlias, "postalCode", token.getValue());
case Location.SP_ADDRESS_COUNTRY:
return propertyLike(criteriaContext, getRootOrJoin(criteriaContext, locationAlias.getAlias()), "country",
token.getValue());
return propertyLike(criteriaContext, locationAlias, "country", token.getValue());
}
} else {
// Join<?,?> join = criteriaContext.addJoin("location",locationAlias.getAlias());
return Optional.of(criteriaContext.getCriteriaBuilder().equal(getRootOrJoin(criteriaContext, locationAlias.getAlias()).get("uuid"), token.getValue()));
return Optional.of(criteriaContext.getCriteriaBuilder().equal(locationAlias.get("uuid"), token.getValue()));
}

return Optional.empty();
Expand Down Expand Up @@ -898,14 +892,13 @@ protected <T, U> Optional<Predicate> handlePersonAddress(OpenmrsFhirCriteriaCont
}

protected <T,U> Optional<Predicate> handleMedicationReference(OpenmrsFhirCriteriaContext<T,U> criteriaContext,
@Nonnull String medicationAlias, ReferenceAndListParam medicationReference) {
@Nonnull From<?,?> medicationAlias, ReferenceAndListParam medicationReference) {
if (medicationReference == null) {
return Optional.empty();
}

return handleAndListParam(criteriaContext.getCriteriaBuilder(), medicationReference,
token -> Optional.of(criteriaContext.getCriteriaBuilder()
.equal(getRootOrJoin(criteriaContext, medicationAlias).get("uuid"), token.getIdPart())));
token -> Optional.of(criteriaContext.getCriteriaBuilder().equal(medicationAlias.get("uuid"), token.getIdPart())));
}

protected <T,U> Optional<Predicate> handleMedicationRequestReference(OpenmrsFhirCriteriaContext<T,U> criteriaContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import org.openmrs.Obs;
import org.openmrs.OpenmrsObject;
import org.openmrs.Order;
import org.openmrs.Patient;
import org.openmrs.Person;
import org.openmrs.Retireable;
import org.openmrs.TestOrder;
Expand Down Expand Up @@ -90,6 +91,8 @@ public abstract class BaseFhirDao<T extends OpenmrsObject & Auditable> extends B

private final String testOrder = getIdPropertyName(TestOrder.class);

private final String patient = getIdPropertyName(Patient.class);

@SuppressWarnings({ "UnstableApiUsage" })
protected BaseFhirDao() {
this.isRetireable = Retireable.class.isAssignableFrom(typeToken.getRawType());
Expand Down Expand Up @@ -193,6 +196,8 @@ public List<T> getSearchResults(@Nonnull SearchParameterMap theParams) {
handleIdPropertyOrdering(criteriaContext, obs);
} else if (FhirTask.class.isAssignableFrom(BaseOpenmrsObject.class)) {
handleIdPropertyOrdering(criteriaContext, fhirTask);
} else if (Patient.class.isAssignableFrom(BaseOpenmrsObject.class)) {
handleIdPropertyOrdering(criteriaContext, patient);
}

criteriaContext.getEntityManager().createQuery(criteriaContext.getCriteriaQuery())
Expand Down Expand Up @@ -220,6 +225,8 @@ public List<T> getSearchResults(@Nonnull SearchParameterMap theParams) {
handleIdPropertySelection(selectionList, root, criteriaBuilder, obs);
}else if (FhirTask.class.isAssignableFrom(BaseOpenmrsObject.class)) {
handleIdPropertySelection(selectionList, root, criteriaBuilder, fhirTask);
} else if (Patient.class.isAssignableFrom(BaseOpenmrsObject.class)) {
handleIdPropertySelection(selectionList, root, criteriaBuilder, patient);
}

criteriaQuery.multiselect(selectionList);
Expand Down Expand Up @@ -250,6 +257,8 @@ public List<T> getSearchResults(@Nonnull SearchParameterMap theParams) {
handleIdPropertyInCondition(idsCriteriaQuery, idsRoot, ids, obs);
} else if (FhirTask.class.isAssignableFrom(BaseOpenmrsObject.class)) {
handleIdPropertyInCondition(idsCriteriaQuery, idsRoot, ids, fhirTask);
} else if (Patient.class.isAssignableFrom(BaseOpenmrsObject.class)) {
handleIdPropertyInCondition(idsCriteriaQuery, idsRoot, ids, patient);
}

results = em.createQuery(idsCriteriaQuery).getResultList();
Expand All @@ -265,6 +274,8 @@ public List<T> getSearchResults(@Nonnull SearchParameterMap theParams) {
handleIdPropertyOrdering(criteriaBuilder, idsCriteriaQuery, idsRoot, obs);
} else if (FhirTask.class.isAssignableFrom(BaseOpenmrsObject.class)) {
handleIdPropertyOrdering(criteriaBuilder, idsCriteriaQuery, idsRoot, fhirTask);
} else if (Patient.class.isAssignableFrom(BaseOpenmrsObject.class)) {
handleIdPropertyOrdering(criteriaBuilder, idsCriteriaQuery, idsRoot, patient);
}

results = criteriaContext.getEntityManager().createQuery(criteriaContext.getCriteriaQuery()).getResultList();
Expand Down Expand Up @@ -292,6 +303,8 @@ public int getSearchResultsCount(@Nonnull SearchParameterMap theParams) {
criteriaContext.getCriteriaQuery().select(criteriaContext.getCriteriaBuilder().countDistinct(criteriaContext.getRoot().get(fhirTask)));
} else if (TestOrder.class.isAssignableFrom(BaseOpenmrsObject.class)) {
criteriaContext.getCriteriaQuery().select(criteriaContext.getCriteriaBuilder().countDistinct(criteriaContext.getRoot().get(testOrder)));
} else if (Patient.class.isAssignableFrom(BaseOpenmrsObject.class)) {
criteriaContext.getCriteriaQuery().select(criteriaContext.getCriteriaBuilder().countDistinct(criteriaContext.getRoot().get(patient)));
}
}
return criteriaContext.getEntityManager().createQuery(criteriaContext.getCriteriaQuery()).getSingleResult().intValue();
Expand Down Expand Up @@ -526,8 +539,8 @@ private void handleIdPropertyInCondition(CriteriaQuery<T> idsCriteriaQuery, Root
*/
private String getIdPropertyName(Class<? extends BaseOpenmrsObject> openmrsEntityClass) {
return Stream
.of(Pair.of(Person.class, "personId"), Pair.of(Encounter.class, "encounterId"), Pair.of(Obs.class, "obsId"), Pair.of(FhirTask.class, "id"), Pair.of(TestOrder.class,"orderId"))
.filter(pair -> pair.getLeft().isAssignableFrom(openmrsEntityClass)).findFirst().map(Pair::getRight)
.of(Pair.of(Person.class, "personId"), Pair.of(Encounter.class, "encounterId"), Pair.of(Obs.class, "obsId"), Pair.of(FhirTask.class, "id"),
Pair.of(TestOrder.class,"orderId"), Pair.of(Patient.class,"patientId")).filter(pair -> pair.getLeft().isAssignableFrom(openmrsEntityClass)).findFirst().map(Pair::getRight)
.orElseThrow(() -> new IllegalArgumentException("Unsupported entity type: " + openmrsEntityClass.getName()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,17 +155,19 @@ protected <V,U> Collection<Order> paramToProps(OpenmrsFhirCriteriaContext<V,U> c

@Override
protected <V,U> String paramToProp(OpenmrsFhirCriteriaContext<V,U> criteriaContext, @Nonnull String param) {
From<?, ?> person = getPersonProperty(criteriaContext);
From<?, ?> address =criteriaContext.addJoin(person, "addresses", "pad");
switch (param) {
case SP_BIRTHDATE:
return "birthdate";
address.get("birthdate");
case SP_ADDRESS_CITY:
return "pad.cityVillage";
address.get("cityVillage");
case SP_ADDRESS_STATE:
return "pad.stateProvince";
address.get("stateProvince");
case SP_ADDRESS_POSTALCODE:
return "pad.postalCode";
address.get("postalCode");
case SP_ADDRESS_COUNTRY:
return "pad.country";
address.get("country");
default:
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ protected <U> void setupSearchParams(OpenmrsFhirCriteriaContext<DrugOrder,U> cri
case FhirConstants.FULFILLER_STATUS_SEARCH_HANDLER:
entry.getValue()
.forEach(param -> handleFulfillerStatus(criteriaContext, (TokenAndListParam) param.getParam())
.ifPresent(criteriaContext::addPredicate));
criteriaContext.finalizeQuery();
.ifPresent(c -> criteriaContext.addPredicate(c).finalizeQuery()));
break;
case FhirConstants.ENCOUNTER_REFERENCE_SEARCH_HANDLER:
entry.getValue().forEach(
Expand All @@ -84,21 +83,16 @@ protected <U> void setupSearchParams(OpenmrsFhirCriteriaContext<DrugOrder,U> cri
(ReferenceAndListParam) participantReference.getParam()));
break;
case FhirConstants.MEDICATION_REFERENCE_SEARCH_HANDLER:
From<?,?> medicationAlias = criteriaContext.addJoin("drug","d");
entry.getValue().forEach(
d -> handleMedicationReference(criteriaContext, "d", (ReferenceAndListParam) d.getParam())
.ifPresent(c -> {
criteriaContext.getRoot().join("drug").alias("d");
criteriaContext.addPredicate(c);
criteriaContext.finalizeQuery();
}));
d -> handleMedicationReference(criteriaContext, medicationAlias, (ReferenceAndListParam) d.getParam())
.ifPresent(c -> criteriaContext.addPredicate(c).finalizeQuery()));
break;
case FhirConstants.STATUS_SEARCH_HANDLER:
entry.getValue().forEach(param -> handleStatus(criteriaContext, (TokenAndListParam) param.getParam())
.ifPresent(criteriaContext::addPredicate));
criteriaContext.finalizeQuery();
.ifPresent(c -> criteriaContext.addPredicate(c).finalizeQuery()));
case FhirConstants.COMMON_SEARCH_HANDLER:
handleCommonSearchParameters(criteriaContext, entry.getValue()).ifPresent(criteriaContext::addPredicate);
criteriaContext.finalizeQuery();
handleCommonSearchParameters(criteriaContext, entry.getValue()).ifPresent(c -> criteriaContext.addPredicate(c).finalizeQuery());
break;
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,7 @@ protected <U> void setupSearchParams(OpenmrsFhirCriteriaContext<Patient,U> crite
break;
case FhirConstants.GENDER_SEARCH_HANDLER:
entry.getValue().forEach(p -> handleGender(criteriaContext, getPersonProperty(criteriaContext),
p.getPropertyName(), (TokenAndListParam) p.getParam()).ifPresent(criteriaContext::addPredicate));
criteriaContext.finalizeQuery();
p.getPropertyName(), (TokenAndListParam) p.getParam()).ifPresent(c -> criteriaContext.addPredicate(c).finalizeQuery()));
break;
case FhirConstants.IDENTIFIER_SEARCH_HANDLER:
entry.getValue().forEach(
Expand All @@ -120,21 +119,18 @@ protected <U> void setupSearchParams(OpenmrsFhirCriteriaContext<Patient,U> crite
case FhirConstants.DATE_RANGE_SEARCH_HANDLER:
entry.getValue()
.forEach(dateRangeParam -> handleDateRange(criteriaContext, dateRangeParam.getPropertyName(),
(DateRangeParam) dateRangeParam.getParam()).ifPresent(criteriaContext::addPredicate));
criteriaContext.finalizeQuery();
(DateRangeParam) dateRangeParam.getParam()).ifPresent(c -> criteriaContext.addPredicate(c).finalizeQuery()));
break;
case FhirConstants.BOOLEAN_SEARCH_HANDLER:
entry.getValue().forEach(
b -> handleBoolean(criteriaContext, b.getPropertyName(), (TokenAndListParam) b.getParam())
.ifPresent(criteriaContext::addPredicate));
criteriaContext.finalizeQuery();
.ifPresent(c -> criteriaContext.addPredicate(c).finalizeQuery()));
break;
case FhirConstants.ADDRESS_SEARCH_HANDLER:
handleAddresses(criteriaContext, entry);
break;
case FhirConstants.COMMON_SEARCH_HANDLER:
handleCommonSearchParameters(criteriaContext, entry.getValue()).ifPresent(criteriaContext::addPredicate);
criteriaContext.finalizeQuery();
handleCommonSearchParameters(criteriaContext, entry.getValue()).ifPresent(c -> criteriaContext.addPredicate(c).finalizeQuery());
break;
}
});
Expand Down

0 comments on commit 9bf6d22

Please sign in to comment.