SampleService extends AbstractCoreAdoService<Sample> { public Map<PathogenTestResultType, Long> getNewTestResultCountByResultType(List<Long> caseIds) { if (CollectionUtils.isEmpty(caseIds)) { return new HashMap<>(); } StringBuilder queryBuilder = new StringBuilder(); queryBuilder.append("WITH sortedsamples AS (SELECT DISTINCT ON (").append(Sample.ASSOCIATED_CASE).append("_id) ") .append(Sample.ASSOCIATED_CASE).append("_id, ").append(Sample.PATHOGEN_TEST_RESULT).append(", ").append(Sample.SAMPLE_DATE_TIME) .append(" FROM ").append(Sample.TABLE_NAME).append(" WHERE (").append(Sample.SPECIMEN_CONDITION).append(" IS NULL OR ") .append(Sample.SPECIMEN_CONDITION).append(" = '").append(SpecimenCondition.ADEQUATE.name()).append("') AND ").append(Sample.TABLE_NAME) .append(".").append(Sample.DELETED).append(" = false ORDER BY ").append(Sample.ASSOCIATED_CASE).append("_id, ") .append(Sample.SAMPLE_DATE_TIME).append(" desc) SELECT sortedsamples.").append(Sample.PATHOGEN_TEST_RESULT).append(", COUNT(") .append(Sample.ASSOCIATED_CASE).append("_id) FROM sortedsamples JOIN ").append(Case.TABLE_NAME).append(" ON sortedsamples.") .append(Sample.ASSOCIATED_CASE).append("_id = ").append(Case.TABLE_NAME).append(".id ") .append(" WHERE sortedsamples.").append(Sample.ASSOCIATED_CASE).append("_id IN (").append(QueryHelper.concatLongs(caseIds)).append(") ") .append(" GROUP BY sortedsamples." + Sample.PATHOGEN_TEST_RESULT); Query query = em.createNativeQuery(queryBuilder.toString()); @SuppressWarnings("unchecked") List<Object[]> results = query.getResultList(); return results.stream() .filter(e -> e[0] != null) .collect(Collectors.toMap(e -> PathogenTestResultType.valueOf((String) e[0]), e -> ((BigInteger) e[1]).longValue())); } SampleService(); List<Sample> findBy(SampleCriteria criteria, User user, String sortProperty, boolean ascending); List<Sample> findBy(SampleCriteria criteria, User user); List<Sample> getAllActiveSamplesAfter(Date date, User user); List<String> getAllActiveUuids(User user); int getSampleCountByCase(Case caze); Sample getReferredFrom(String sampleUuid); List<String> getDeletedUuidsSince(User user, Date since); List<Sample> getByCaseUuids(List<String> caseUuids); Map<PathogenTestResultType, Long> getNewTestResultCountByResultType(List<Long> caseIds); @Override @SuppressWarnings("rawtypes") @Deprecated Predicate createUserFilter(CriteriaBuilder cb, CriteriaQuery cq, From<?, Sample> samplePath); @SuppressWarnings("rawtypes") Predicate createUserFilter(CriteriaQuery cq, CriteriaBuilder cb, SampleJoins joins); Predicate createUserFilterWithoutCase(CriteriaBuilder cb, SampleJoins joins); Predicate buildCriteriaFilter(SampleCriteria criteria, CriteriaBuilder cb, SampleJoins joins); @Override void delete(Sample sample); Predicate createActiveSamplesFilter(CriteriaBuilder cb, Root<Sample> root); Predicate createDefaultFilter(CriteriaBuilder cb, Root<Sample> root);  }
@Test public void testGetNewTestResultCountByResultTypeVariousInClauseCount() { SampleService cut = getBean(SampleService.class); assertThat(cut.getNewTestResultCountByResultType(Collections.emptyList()).entrySet(), is(empty())); assertThat(cut.getNewTestResultCountByResultType(null).entrySet(), is(empty())); assertThat(cut.getNewTestResultCountByResultType(Collections.singletonList(1001L)).entrySet(), is(empty())); assertThat(cut.getNewTestResultCountByResultType(Arrays.asList(1L, 2L)).entrySet(), is(empty())); assertThat(cut.getNewTestResultCountByResultType(Arrays.asList(1L, 2L, 1001L)).entrySet(), is(empty())); assertThat( cut.getNewTestResultCountByResultType(LongStream.rangeClosed(1, 1_000).boxed().collect(Collectors.toList())).entrySet(), is(empty())); assertThat( cut.getNewTestResultCountByResultType(LongStream.rangeClosed(1, 100_000).boxed().collect(Collectors.toList())).entrySet(), is(empty())); }