Skip to content

Commit

Permalink
reduce number of lines
Browse files Browse the repository at this point in the history
  • Loading branch information
mherman22 committed Jan 18, 2024
1 parent ef5c9cc commit 1ccc055
Showing 1 changed file with 52 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -172,19 +172,8 @@ public List<T> getSearchResults(@Nonnull SearchParameterMap theParams) {
OpenmrsFhirCriteriaContext<T> criteriaContext = getSearchResultCriteria(theParams);

handleSort(criteriaContext, theParams.getSortSpec());

String person = getIdPropertyName(Person.class);
String encounter = getIdPropertyName(Encounter.class);
String obs = getIdPropertyName(Obs.class);
handleIdPropertyOrdering(criteriaContext, getIdPropertyName((Class<? extends BaseOpenmrsObject>) typeToken.getRawType()));

if (Person.class.isAssignableFrom(BaseOpenmrsObject.class)) {
handleIdPropertyOrdering(criteriaContext, person);
} else if (Encounter.class.isAssignableFrom(BaseOpenmrsObject.class)) {
handleIdPropertyOrdering(criteriaContext, encounter);
} else if (Obs.class.isAssignableFrom(BaseOpenmrsObject.class)) {
handleIdPropertyOrdering(criteriaContext, obs);
}

criteriaContext.getEntityManager().createQuery(criteriaContext.getCriteriaQuery())
.setFirstResult(theParams.getFromIndex());
if (theParams.getToIndex() != Integer.MAX_VALUE) {
Expand All @@ -202,55 +191,26 @@ public List<T> getSearchResults(@Nonnull SearchParameterMap theParams) {
Root<T> root = (Root<T>) criteriaQuery.from(typeToken.getRawType());

List<Selection<?>> selectionList = new ArrayList<>();
if (Person.class.isAssignableFrom(BaseOpenmrsObject.class)) {
handleIdPropertySelection(selectionList, root, criteriaBuilder, person);
} else if (Encounter.class.isAssignableFrom(BaseOpenmrsObject.class)) {
handleIdPropertySelection(selectionList, root, criteriaBuilder, encounter);
} else if (Obs.class.isAssignableFrom(BaseOpenmrsObject.class)) {
handleIdPropertySelection(selectionList, root, criteriaBuilder, obs);
}

criteriaQuery.multiselect(selectionList);
handleIdPropertySelection(selectionList, criteriaQuery, root, criteriaBuilder, getIdPropertyName((Class<? extends BaseOpenmrsObject>) typeToken.getRawType()));
handleSort(criteriaContext, theParams.getSortSpec(), this::paramToProps).ifPresent(
orders -> orders.forEach(order -> selectionList.add(root.get(getPropertyName(order.getExpression())))));
List<T> ids = new ArrayList<>();
List<Integer> ids = new ArrayList<>();
if (selectionList.size() > 1) {
for (Object[] o : em.createQuery(criteriaQuery).getResultList()) {
ids.add((T) o[0]);
ids.add((Integer) o[0]);
}
} else {
EntityManager manager = sessionFactory.getCurrentSession();
CriteriaBuilder builder = manager.getCriteriaBuilder();
CriteriaQuery<T> cq = (CriteriaQuery<T>) builder.createQuery(typeToken.getRawType());
Root<T> rt = (Root<T>) cq.from(typeToken.getRawType());
cq.select(rt);
ids = manager.createQuery(cq).getResultList();
return new ArrayList<>(criteriaContext.getEntityManager().createQuery(criteriaContext.getCriteriaQuery()).getResultList());
}

CriteriaQuery<T> idsCriteriaQuery = (CriteriaQuery<T>) criteriaBuilder.createQuery(typeToken.getRawType());
Root<T> idsRoot = (Root<T>) idsCriteriaQuery.from(typeToken.getRawType());

if (Person.class.isAssignableFrom(BaseOpenmrsObject.class)) {
handleIdPropertyInCondition(idsCriteriaQuery, idsRoot, ids, person);
} else if (Encounter.class.isAssignableFrom(BaseOpenmrsObject.class)) {
handleIdPropertyInCondition(idsCriteriaQuery, idsRoot, ids, encounter);
} else if (Obs.class.isAssignableFrom(BaseOpenmrsObject.class)) {
handleIdPropertyInCondition(idsCriteriaQuery, idsRoot, ids, obs);
}

handleIdPropertyInCondition(idsCriteriaQuery, idsRoot, ids, getIdPropertyName((Class<? extends BaseOpenmrsObject>) typeToken.getRawType()));
results = em.createQuery(idsCriteriaQuery).getResultList();

// Need to reapply ordering
handleSort(criteriaContext, theParams.getSortSpec());

if (Person.class.isAssignableFrom(BaseOpenmrsObject.class)) {
handleIdPropertyOrdering(criteriaBuilder,idsCriteriaQuery, idsRoot, person);
} else if (Encounter.class.isAssignableFrom(BaseOpenmrsObject.class)) {
handleIdPropertyOrdering(criteriaBuilder,idsCriteriaQuery, idsRoot, encounter);
} else if (Obs.class.isAssignableFrom(BaseOpenmrsObject.class)) {
handleIdPropertyOrdering(criteriaBuilder, idsCriteriaQuery, idsRoot,obs);
}

handleIdPropertyOrdering(criteriaBuilder,idsCriteriaQuery, idsRoot, getIdPropertyName((Class<? extends BaseOpenmrsObject>) typeToken.getRawType()));
results = criteriaContext.getEntityManager().createQuery(criteriaContext.getCriteriaQuery()).getResultList();
}
return results.stream().map(this::deproxyResult).collect(Collectors.toList());
Expand Down Expand Up @@ -430,6 +390,20 @@ protected void applyExactTotal(OpenmrsFhirCriteriaContext<T> criteriaContext, Se
}
}

/**
* Retrieves the path for the attribute ending with "Id" from the given root.
*
* @param root The root of the JPA criteria query representing the entity.
* @param idPropertyName The name of the property ending with "Id" for which to retrieve the path.
* @return The path representing the attribute ending with "Id," or null if not found.
*/
private <t> Path<Object> getIdPath(Root<t> root, String idPropertyName) {
if (idPropertyName.endsWith("Id")) {
return root.get(idPropertyName);
}
return null;
}

/**
* Extracts the property name from the given JPA Criteria API expression.
*
Expand All @@ -453,7 +427,12 @@ 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.getCriteriaQuery().orderBy(criteriaContext.getCriteriaBuilder().asc(criteriaContext.getRoot().get(idPropertyName)));
Path<Object> idPath = getIdPath(criteriaContext.getRoot(), idPropertyName);
if (idPath != null) {
criteriaContext.getCriteriaQuery().orderBy(criteriaContext.getCriteriaBuilder().asc(idPath));
} else {
throw new IllegalArgumentException("No field ending with 'Id' found in entity");
}
}

/**
Expand All @@ -465,20 +444,33 @@ private void handleIdPropertyOrdering(OpenmrsFhirCriteriaContext<T> criteriaCont
* @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)));
Path<Object> idPath = getIdPath(idsRoot, idPropertyName);
if (idPath != null) {
criteriaQuery.orderBy(criteriaBuilder.asc(idsRoot.get(idPropertyName)));
} else {
throw new IllegalArgumentException("No field ending with 'Id' found in entity");
}
}

/**
* Handles the selection list based on the specified id property name, adding both the property and its distinct count.
*
* @param selectionList The list of selections to be populated.
* @param criteriaQuery The criteria query
* @param root The root of the criteria query.
* @param criteriaBuilder The criteria builder to build selection expressions.
* @param idPropertyName The name of the id property to be used for selection.
*/
private void handleIdPropertySelection(List<Selection<?>> selectionList, Root<T> root, CriteriaBuilder criteriaBuilder, String idPropertyName) {
selectionList.add(root.get(idPropertyName));
selectionList.add(criteriaBuilder.countDistinct(root.get(idPropertyName)));
private void handleIdPropertySelection(List<Selection<?>> selectionList, CriteriaQuery<Object[]> criteriaQuery, Root<T> root, CriteriaBuilder criteriaBuilder, String idPropertyName) {
Path<Object> idPath = getIdPath(root, idPropertyName);
if (idPath != null) {
selectionList.add(root.get(idPropertyName));
selectionList.add(criteriaBuilder.countDistinct(root.get(idPropertyName)));
criteriaQuery.groupBy(root.get(idPropertyName));
criteriaQuery.multiselect(selectionList);
} else {
throw new IllegalArgumentException("No field ending with 'Id' found in entity");
}
}

/**
Expand All @@ -489,8 +481,14 @@ private void handleIdPropertySelection(List<Selection<?>> selectionList, Root<T>
* @param ids The list of ids to be used in the "IN" condition.
* @param idPropertyName The name of the id property to be used in the "IN" condition.
*/
private void handleIdPropertyInCondition(CriteriaQuery<T> idsCriteriaQuery, Root<T> idsRoot, List<T> ids, String idPropertyName) {
idsCriteriaQuery.where(idsRoot.get(idPropertyName).in(ids));
private void handleIdPropertyInCondition(CriteriaQuery<T> idsCriteriaQuery, Root<T> idsRoot, List<Integer> ids, String idPropertyName) {
Path<Object> idPath = getIdPath(idsRoot, idPropertyName);
if (idPath != null) {
idsCriteriaQuery.where(idsRoot.get(idPropertyName).in(ids));
// criteriaContext.addPredicate(criteriaContext.getRoot().get("uuid").in(uuids));
} else {
throw new IllegalArgumentException("No field ending with 'Id' found in entity");
}
}

/**
Expand Down

0 comments on commit 1ccc055

Please sign in to comment.