|
20 | 20 | import java.math.BigInteger; |
21 | 21 | import java.util.Date; |
22 | 22 | import java.util.HashMap; |
| 23 | +import java.util.LinkedList; |
23 | 24 | import java.util.List; |
24 | 25 | import java.util.Map; |
25 | 26 | import java.util.stream.Collectors; |
|
38 | 39 | import javax.persistence.criteria.Predicate; |
39 | 40 | import javax.persistence.criteria.Root; |
40 | 41 |
|
| 42 | +import de.symeda.sormas.backend.util.IterableHelper; |
| 43 | +import de.symeda.sormas.backend.util.ModelConstants; |
41 | 44 | import org.apache.commons.collections.CollectionUtils; |
42 | 45 |
|
43 | 46 | import de.symeda.sormas.api.EntityRelevanceStatus; |
@@ -243,26 +246,38 @@ public Map<PathogenTestResultType, Long> getNewTestResultCountByResultType(List< |
243 | 246 | // Avoid parameter limit by joining caseIds to a String instead of n parameters |
244 | 247 | StringBuilder queryBuilder = new StringBuilder(); |
245 | 248 | //@formatter:off |
246 | | - queryBuilder.append("WITH sortedsamples AS (SELECT DISTINCT ON (").append(Sample.ASSOCIATED_CASE).append("_id) ") |
247 | | - .append(Sample.ASSOCIATED_CASE).append("_id, ").append(Sample.PATHOGEN_TEST_RESULT).append(", ").append(Sample.SAMPLE_DATE_TIME) |
248 | | - .append(" FROM ").append(Sample.TABLE_NAME).append(" WHERE (").append(Sample.SPECIMEN_CONDITION).append(" IS NULL OR ") |
249 | | - .append(Sample.SPECIMEN_CONDITION).append(" = '").append(SpecimenCondition.ADEQUATE.name()).append("') AND ").append(Sample.TABLE_NAME) |
250 | | - .append(".").append(Sample.DELETED).append(" = false ORDER BY ").append(Sample.ASSOCIATED_CASE).append("_id, ") |
251 | | - .append(Sample.SAMPLE_DATE_TIME).append(" desc) SELECT sortedsamples.").append(Sample.PATHOGEN_TEST_RESULT).append(", COUNT(") |
252 | | - .append(Sample.ASSOCIATED_CASE).append("_id) FROM sortedsamples JOIN ").append(Case.TABLE_NAME).append(" ON sortedsamples.") |
253 | | - .append(Sample.ASSOCIATED_CASE).append("_id = ").append(Case.TABLE_NAME).append(".id ") |
254 | | - .append(" WHERE sortedsamples.").append(Sample.ASSOCIATED_CASE).append("_id IN (").append(QueryHelper.concatLongs(caseIds)).append(") ") |
255 | | - .append(" GROUP BY sortedsamples." + Sample.PATHOGEN_TEST_RESULT); |
256 | | - //@formatter:on |
257 | | - |
258 | | - Query query = em.createNativeQuery(queryBuilder.toString()); |
259 | | - |
260 | | - @SuppressWarnings("unchecked") |
261 | | - List<Object[]> results = query.getResultList(); |
262 | | - |
263 | | - return results.stream() |
264 | | - .filter(e -> e[0] != null) |
265 | | - .collect(Collectors.toMap(e -> PathogenTestResultType.valueOf((String) e[0]), e -> ((BigInteger) e[1]).longValue())); |
| 249 | + queryBuilder.append("WITH sortedsamples AS (SELECT DISTINCT ON (").append(Sample.ASSOCIATED_CASE).append("_id) ") |
| 250 | + .append(Sample.ASSOCIATED_CASE).append("_id, ").append(Sample.PATHOGEN_TEST_RESULT).append(", ").append(Sample.SAMPLE_DATE_TIME) |
| 251 | + .append(" FROM ").append(Sample.TABLE_NAME).append(" WHERE (").append(Sample.SPECIMEN_CONDITION).append(" IS NULL OR ") |
| 252 | + .append(Sample.SPECIMEN_CONDITION).append(" = '").append(SpecimenCondition.ADEQUATE.name()).append("') AND ").append(Sample.TABLE_NAME) |
| 253 | + .append(".").append(Sample.DELETED).append(" = false ORDER BY ").append(Sample.ASSOCIATED_CASE).append("_id, ") |
| 254 | + .append(Sample.SAMPLE_DATE_TIME).append(" desc) SELECT sortedsamples.").append(Sample.PATHOGEN_TEST_RESULT).append(", COUNT(") |
| 255 | + .append(Sample.ASSOCIATED_CASE).append("_id) FROM sortedsamples JOIN ").append(Case.TABLE_NAME).append(" ON sortedsamples.") |
| 256 | + .append(Sample.ASSOCIATED_CASE).append("_id = ").append(Case.TABLE_NAME).append(".id ") |
| 257 | + .append(" WHERE sortedsamples.").append(Sample.ASSOCIATED_CASE).append("_id IN (:caseIds)") |
| 258 | + .append(" GROUP BY sortedsamples." + Sample.PATHOGEN_TEST_RESULT); |
| 259 | + //@formatter:on |
| 260 | + |
| 261 | + if (caseIds.size() < ModelConstants.PARAMETER_LIMIT) { |
| 262 | + List<Object[]> results; |
| 263 | + Query query = em.createNativeQuery(queryBuilder.toString()); |
| 264 | + query.setParameter("caseIds", caseIds); |
| 265 | + results = query.getResultList(); |
| 266 | + |
| 267 | + return results.stream() |
| 268 | + .filter(e -> e[0] != null) |
| 269 | + .collect(Collectors.toMap(e -> PathogenTestResultType.valueOf((String) e[0]), e -> ((BigInteger) e[1]).longValue())); |
| 270 | + } else { |
| 271 | + List<Object[]> results = new LinkedList<>(); |
| 272 | + IterableHelper.executeBatched(caseIds, ModelConstants.PARAMETER_LIMIT, batchedCaseIds -> { |
| 273 | + Query query = em.createNativeQuery(queryBuilder.toString()); |
| 274 | + query.setParameter("caseIds", batchedCaseIds); |
| 275 | + results.addAll(query.getResultList()); |
| 276 | + }); |
| 277 | + return results.stream() |
| 278 | + .filter(e -> e[0] != null) |
| 279 | + .collect(Collectors.toMap(e -> PathogenTestResultType.valueOf((String) e[0]), e -> ((BigInteger) e[1]).longValue())); |
| 280 | + } |
266 | 281 | } |
267 | 282 |
|
268 | 283 | @Override |
|
0 commit comments