Skip to content

Commit

Permalink
for testing purposes
Browse files Browse the repository at this point in the history
  • Loading branch information
mherman22 committed Jan 18, 2024
1 parent 72e956b commit ef5c9cc
Showing 1 changed file with 17 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,7 @@ public List<T> getSearchResults(@Nonnull SearchParameterMap theParams) {
String person = getIdPropertyName(Person.class);
String encounter = getIdPropertyName(Encounter.class);
String obs = getIdPropertyName(Obs.class);

//the id property differs across various openmrs entities

if (Person.class.isAssignableFrom(BaseOpenmrsObject.class)) {
handleIdPropertyOrdering(criteriaContext, person);
} else if (Encounter.class.isAssignableFrom(BaseOpenmrsObject.class)) {
Expand All @@ -203,8 +202,6 @@ public List<T> getSearchResults(@Nonnull SearchParameterMap theParams) {
Root<T> root = (Root<T>) criteriaQuery.from(typeToken.getRawType());

List<Selection<?>> selectionList = new ArrayList<>();

//the id property differs across various openmrs entities
if (Person.class.isAssignableFrom(BaseOpenmrsObject.class)) {
handleIdPropertySelection(selectionList, root, criteriaBuilder, person);
} else if (Encounter.class.isAssignableFrom(BaseOpenmrsObject.class)) {
Expand All @@ -214,7 +211,6 @@ public List<T> getSearchResults(@Nonnull SearchParameterMap theParams) {
}

criteriaQuery.multiselect(selectionList);

handleSort(criteriaContext, theParams.getSortSpec(), this::paramToProps).ifPresent(
orders -> orders.forEach(order -> selectionList.add(root.get(getPropertyName(order.getExpression())))));
List<T> ids = new ArrayList<>();
Expand All @@ -234,7 +230,6 @@ public List<T> getSearchResults(@Nonnull SearchParameterMap theParams) {
CriteriaQuery<T> idsCriteriaQuery = (CriteriaQuery<T>) criteriaBuilder.createQuery(typeToken.getRawType());
Root<T> idsRoot = (Root<T>) idsCriteriaQuery.from(typeToken.getRawType());

//the id property differs across various openmrs entities
if (Person.class.isAssignableFrom(BaseOpenmrsObject.class)) {
handleIdPropertyInCondition(idsCriteriaQuery, idsRoot, ids, person);
} else if (Encounter.class.isAssignableFrom(BaseOpenmrsObject.class)) {
Expand All @@ -247,15 +242,13 @@ public List<T> getSearchResults(@Nonnull SearchParameterMap theParams) {

// Need to reapply ordering
handleSort(criteriaContext, theParams.getSortSpec());
// criteriaContext.addOrder(criteriaContext.getCriteriaBuilder().asc(criteriaContext.getRoot().get("id")));

//the id property differs across various openmrs entities
if (Person.class.isAssignableFrom(BaseOpenmrsObject.class)) {
handleIdPropertyInCondition2(idsCriteriaQuery, idsRoot, ids, person);
handleIdPropertyOrdering(criteriaBuilder,idsCriteriaQuery, idsRoot, person);
} else if (Encounter.class.isAssignableFrom(BaseOpenmrsObject.class)) {
handleIdPropertyInCondition2(idsCriteriaQuery, idsRoot, ids, encounter);
handleIdPropertyOrdering(criteriaBuilder,idsCriteriaQuery, idsRoot, encounter);
} else if (Obs.class.isAssignableFrom(BaseOpenmrsObject.class)) {
handleIdPropertyInCondition2(idsCriteriaQuery, idsRoot, ids, obs);
handleIdPropertyOrdering(criteriaBuilder, idsCriteriaQuery, idsRoot,obs);
}

results = criteriaContext.getEntityManager().createQuery(criteriaContext.getCriteriaQuery()).getResultList();
Expand Down Expand Up @@ -460,7 +453,19 @@ public static String getPropertyName(Expression<?> expression) {
* @param idPropertyName The name of the id property to be used for ordering.
*/
private void handleIdPropertyOrdering(OpenmrsFhirCriteriaContext<T> criteriaContext, String idPropertyName) {
criteriaContext.getCriteriaBuilder().asc(criteriaContext.getRoot().get(idPropertyName));
criteriaContext.getCriteriaQuery().orderBy(criteriaContext.getCriteriaBuilder().asc(criteriaContext.getRoot().get(idPropertyName)));
}

/**
* Handles the ordering of the criteria based on the specified id property name.
*
* @param criteriaBuilder The criteria builder to build expressions
* @param criteriaQuery The criteria query for the idPropertyName.
* @param idsRoot The root of the criteria query for the idPropertyName
* @param idPropertyName The name of the id property to be used for ordering.
*/
private void handleIdPropertyOrdering(CriteriaBuilder criteriaBuilder, CriteriaQuery<T> criteriaQuery, Root<T> idsRoot, String idPropertyName) {
criteriaQuery.orderBy(criteriaBuilder.asc(idsRoot.get(idPropertyName)));
}

/**
Expand Down Expand Up @@ -488,10 +493,6 @@ private void handleIdPropertyInCondition(CriteriaQuery<T> idsCriteriaQuery, Root
idsCriteriaQuery.where(idsRoot.get(idPropertyName).in(ids));
}

private void handleIdPropertyInCondition2(CriteriaQuery<T> idsCriteriaQuery, Root<T> idsRoot, List<T> ids, String idPropertyName) {
idsCriteriaQuery.where(idsRoot.get(idPropertyName).in(ids));
}

/**
* Determines the name of the id property for the given entity class. This method assumes a specific naming convention
* based on the entity type.
Expand Down

0 comments on commit ef5c9cc

Please sign in to comment.