|
43 | 43 | import org.jooq.Select; |
44 | 44 | import org.jooq.SelectHavingStep; |
45 | 45 | import org.jooq.impl.DSL; |
| 46 | +import org.jooq.lambda.tuple.Tuple2; |
46 | 47 | import org.springframework.beans.factory.annotation.Autowired; |
47 | 48 | import org.springframework.stereotype.Repository; |
48 | 49 |
|
|
52 | 53 | import java.util.Map; |
53 | 54 | import java.util.Optional; |
54 | 55 | import java.util.Set; |
| 56 | +import java.util.stream.Collectors; |
55 | 57 |
|
56 | 58 | import static java.util.Collections.emptyList; |
57 | 59 | import static org.finos.waltz.common.Checks.checkNotNull; |
|
62 | 64 | import static org.finos.waltz.schema.tables.AttestationInstance.ATTESTATION_INSTANCE; |
63 | 65 | import static org.finos.waltz.schema.tables.AttestationInstanceRecipient.ATTESTATION_INSTANCE_RECIPIENT; |
64 | 66 | import static org.finos.waltz.schema.tables.AttestationRun.ATTESTATION_RUN; |
| 67 | +import static org.jooq.lambda.tuple.Tuple.tuple; |
65 | 68 |
|
66 | 69 | @Repository |
67 | 70 | public class AttestationRunDao { |
@@ -308,51 +311,55 @@ public int updateRecipientInvolvementGroupId(long attestationRunId, Long recipie |
308 | 311 | public Set<AttestationRunRecipient> findRunRecipients(long runId) { |
309 | 312 |
|
310 | 313 | Field<Boolean> isPendingField = DSL |
311 | | - .nvl2( |
312 | | - ATTESTATION_INSTANCE.ATTESTED_AT, |
313 | | - DSL.value(false), |
314 | | - DSL.value(true)) |
315 | | - .as("isPending"); |
| 314 | + .when(ATTESTATION_INSTANCE.ATTESTED_AT.isNull(), DSL.value(false)) |
| 315 | + .otherwise(DSL.value(true)); |
316 | 316 |
|
317 | | - SelectHavingStep<Record3<String, Boolean, Integer>> qry = dsl |
| 317 | + SelectHavingStep<Record3<String, Boolean, Long>> qry = dsl |
318 | 318 | .select(ATTESTATION_INSTANCE_RECIPIENT.USER_ID, |
319 | | - isPendingField, |
320 | | - DSL.count()) |
| 319 | + isPendingField.as("is_pending_field"), |
| 320 | + ATTESTATION_INSTANCE.ID) |
321 | 321 | .from(ATTESTATION_RUN) |
322 | 322 | .innerJoin(ATTESTATION_INSTANCE) |
323 | 323 | .on(ATTESTATION_INSTANCE.ATTESTATION_RUN_ID.eq(ATTESTATION_RUN.ID)) |
324 | 324 | .innerJoin(ATTESTATION_INSTANCE_RECIPIENT) |
325 | 325 | .on(ATTESTATION_INSTANCE_RECIPIENT.ATTESTATION_INSTANCE_ID.eq(ATTESTATION_INSTANCE.ID)) |
326 | | - .where(ATTESTATION_RUN.ID.eq(runId)) |
327 | | - .groupBy(ATTESTATION_INSTANCE_RECIPIENT.USER_ID, isPendingField); |
| 326 | + .where(ATTESTATION_RUN.ID.eq(runId)); |
328 | 327 |
|
329 | 328 | Map<String, ImmutableAttestationRunRecipient> recipientsByUserId = new HashMap<>(); |
330 | 329 |
|
331 | | - qry.fetch().forEach(r -> { |
332 | | - String userId = r.get(ATTESTATION_INSTANCE_RECIPIENT.USER_ID); |
333 | | - boolean isPending = r.get(isPendingField); |
334 | | - int count = r.get(DSL.count()); |
335 | | - |
336 | | - ImmutableAttestationRunRecipient recipient = recipientsByUserId.getOrDefault( |
337 | | - userId, |
338 | | - ImmutableAttestationRunRecipient.builder() |
339 | | - .userId(userId) |
340 | | - .pendingCount(0) |
341 | | - .completedCount(0) |
342 | | - .build()); |
343 | | - |
344 | | - if (isPending) { |
345 | | - recipient = ImmutableAttestationRunRecipient |
346 | | - .copyOf(recipient) |
347 | | - .withPendingCount(count); |
348 | | - } else { |
349 | | - recipient = ImmutableAttestationRunRecipient |
350 | | - .copyOf(recipient) |
351 | | - .withCompletedCount(count); |
352 | | - } |
353 | | - |
354 | | - recipientsByUserId.put(userId, recipient); |
355 | | - }); |
| 330 | + Map<Tuple2<String, Boolean>, Long> attestationInfo = qry |
| 331 | + .fetchSet(r -> r) |
| 332 | + .stream() |
| 333 | + .collect(Collectors.groupingBy( |
| 334 | + r -> tuple(r.get(ATTESTATION_INSTANCE_RECIPIENT.USER_ID), r.get("is_pending_field", Boolean.class)), |
| 335 | + Collectors.counting())); |
| 336 | + |
| 337 | + attestationInfo |
| 338 | + .forEach((key, value) -> { |
| 339 | + String userId = key.v1; |
| 340 | + boolean isPending = key.v2; |
| 341 | + long count = value; |
| 342 | + |
| 343 | + ImmutableAttestationRunRecipient recipient = recipientsByUserId.getOrDefault( |
| 344 | + userId, |
| 345 | + ImmutableAttestationRunRecipient.builder() |
| 346 | + .userId(userId) |
| 347 | + .pendingCount(0) |
| 348 | + .completedCount(0) |
| 349 | + .build()); |
| 350 | + |
| 351 | + if (isPending) { |
| 352 | + recipient = ImmutableAttestationRunRecipient |
| 353 | + .copyOf(recipient) |
| 354 | + .withPendingCount(count); |
| 355 | + } else { |
| 356 | + recipient = ImmutableAttestationRunRecipient |
| 357 | + .copyOf(recipient) |
| 358 | + .withCompletedCount(count); |
| 359 | + } |
| 360 | + |
| 361 | + recipientsByUserId.put(userId, recipient); |
| 362 | + }); |
356 | 363 |
|
357 | 364 | return SetUtilities.fromCollection(recipientsByUserId.values()); |
358 | 365 | } |
|
0 commit comments