Sort improvements

This commit is contained in:
jamesagnew 2015-03-09 08:25:51 -04:00
parent 5fd987c497
commit d083bfbc65
2 changed files with 18 additions and 6 deletions

View File

@ -889,7 +889,7 @@ public class FhirResourceDao<T extends IResource> extends BaseFhirDao implements
throw new NotImplementedException("This server does not support _sort specifications of type " + param.getParamType() + " - Can't serve _sort=" + theSort.getParamName()); throw new NotImplementedException("This server does not support _sort specifications of type " + param.getParamType() + " - Can't serve _sort=" + theSort.getParamName());
} }
From<?, ?> stringJoin = theFrom.join(joinAttrName, JoinType.LEFT); From<?, ?> stringJoin = theFrom.join(joinAttrName, JoinType.INNER);
// Predicate p = theBuilder.equal(stringJoin.get("myParamName"), theSort.getParamName()); // Predicate p = theBuilder.equal(stringJoin.get("myParamName"), theSort.getParamName());
// Predicate pn = theBuilder.isNull(stringJoin.get("myParamName")); // Predicate pn = theBuilder.isNull(stringJoin.get("myParamName"));
// thePredicates.add(theBuilder.or(p, pn)); // thePredicates.add(theBuilder.or(p, pn));
@ -1247,7 +1247,7 @@ public class FhirResourceDao<T extends IResource> extends BaseFhirDao implements
} }
} }
final List<Long> pids = new ArrayList<Long>(loadPids); final List<Long> pids;
// Handle sorting if any was provided // Handle sorting if any was provided
if (theParams.getSort() != null && isNotBlank(theParams.getSort().getParamName())) { if (theParams.getSort() != null && isNotBlank(theParams.getSort().getParamName())) {
@ -1256,9 +1256,10 @@ public class FhirResourceDao<T extends IResource> extends BaseFhirDao implements
CriteriaBuilder builder = myEntityManager.getCriteriaBuilder(); CriteriaBuilder builder = myEntityManager.getCriteriaBuilder();
CriteriaQuery<Tuple> cq = builder.createTupleQuery(); CriteriaQuery<Tuple> cq = builder.createTupleQuery();
Root<ResourceTable> from = cq.from(ResourceTable.class); Root<ResourceTable> from = cq.from(ResourceTable.class);
predicates.add(from.get("myId").in(pids)); predicates.add(from.get("myId").in(loadPids));
createSort(builder, from, theParams.getSort(), orders, predicates); createSort(builder, from, theParams.getSort(), orders, predicates);
if (orders.size() > 0) { if (orders.size() > 0) {
Set<Long> originalPids = loadPids;
loadPids = new LinkedHashSet<Long>(); loadPids = new LinkedHashSet<Long>();
cq.multiselect(from.get("myId").as(Long.class)); cq.multiselect(from.get("myId").as(Long.class));
cq.where(predicates.toArray(new Predicate[0])); cq.where(predicates.toArray(new Predicate[0]));
@ -1272,11 +1273,22 @@ public class FhirResourceDao<T extends IResource> extends BaseFhirDao implements
ourLog.info("Sort PID order is now: {}", loadPids); ourLog.info("Sort PID order is now: {}", loadPids);
pids.clear(); pids = new ArrayList<Long>(loadPids);
pids.addAll(loadPids);
// Any ressources which weren't matched by the sort get added to the bottom
for (Long next : originalPids) {
if (loadPids.contains(next)==false) {
pids.add(next);
} }
} }
}else {
pids = new ArrayList<Long>(loadPids);
}
} else {
pids = new ArrayList<Long>(loadPids);
}
IBundleProvider retVal = new IBundleProvider() { IBundleProvider retVal = new IBundleProvider() {
@Override @Override

View File

@ -1568,7 +1568,7 @@ public class FhirResourceDaoTest {
pm.setSort(new SortSpec(Patient.SP_BIRTHDATE).setOrder(SortOrderEnum.DESC)); pm.setSort(new SortSpec(Patient.SP_BIRTHDATE).setOrder(SortOrderEnum.DESC));
actual = toUnqualifiedVersionlessIds(ourPatientDao.search(pm)); actual = toUnqualifiedVersionlessIds(ourPatientDao.search(pm));
assertEquals(4, actual.size()); assertEquals(4, actual.size());
assertThat(actual, contains(id4, id3, id2, id1)); assertThat(actual, contains(id3, id2, id1, id4));
} }