|
55 | 55 | import javax.persistence.criteria.Order; |
56 | 56 | import javax.persistence.criteria.Predicate; |
57 | 57 | import javax.persistence.criteria.Root; |
58 | | -import javax.persistence.criteria.Subquery; |
59 | 58 | import javax.validation.constraints.NotNull; |
60 | 59 |
|
61 | 60 | import org.apache.commons.collections.CollectionUtils; |
|
126 | 125 | import de.symeda.sormas.backend.epidata.EpiDataFacadeEjb; |
127 | 126 | import de.symeda.sormas.backend.epidata.EpiDataFacadeEjb.EpiDataFacadeEjbLocal; |
128 | 127 | import de.symeda.sormas.backend.event.ContactEventSummaryDetails; |
129 | | -import de.symeda.sormas.backend.event.Event; |
130 | | -import de.symeda.sormas.backend.event.EventParticipant; |
131 | 128 | import de.symeda.sormas.backend.event.EventService; |
132 | 129 | import de.symeda.sormas.backend.exposure.Exposure; |
133 | 130 | import de.symeda.sormas.backend.externaljournal.ExternalJournalService; |
@@ -411,20 +408,6 @@ public List<ContactExportDto> getExportList(ContactCriteria contactCriteria, int |
411 | 408 |
|
412 | 409 | ContactJoins joins = new ContactJoins(contact); |
413 | 410 |
|
414 | | - // Events count subquery |
415 | | - Subquery<Long> eventCountSq = cq.subquery(Long.class); |
416 | | - Root<EventParticipant> eventCountRoot = eventCountSq.from(EventParticipant.class); |
417 | | - Join<EventParticipant, Event> eventJoin = eventCountRoot.join(EventParticipant.EVENT, JoinType.INNER); |
418 | | - Join<Person, Contact> contactJoin = eventCountRoot.join(EventParticipant.PERSON, JoinType.INNER).join(Person.CONTACTS, JoinType.INNER); |
419 | | - |
420 | | - eventCountSq.where( |
421 | | - cb.and( |
422 | | - cb.equal(contactJoin.get(Contact.UUID), contact.get(Contact.UUID)), |
423 | | - cb.isFalse(eventJoin.get(Event.DELETED)), |
424 | | - cb.isFalse(eventJoin.get(Event.ARCHIVED)), |
425 | | - cb.isFalse(eventCountRoot.get(EventParticipant.DELETED)))); |
426 | | - eventCountSq.select(cb.countDistinct(eventJoin.get(Event.ID))); |
427 | | - |
428 | 411 | cq.multiselect( |
429 | 412 | Stream.concat( |
430 | 413 | Stream.of( |
@@ -491,7 +474,6 @@ public List<ContactExportDto> getExportList(ContactCriteria contactCriteria, int |
491 | 474 | joins.getEpiData().get(EpiData.ID), |
492 | 475 | joins.getEpiData().get(EpiData.CONTACT_WITH_SOURCE_CASE_KNOWN), |
493 | 476 | contact.get(Contact.RETURNING_TRAVELER), |
494 | | - eventCountSq, |
495 | 477 | contact.get(Contact.EXTERNAL_ID)), |
496 | 478 | listCriteriaBuilder.getJurisdictionSelections(joins)).collect(Collectors.toList())); |
497 | 479 |
|
@@ -544,10 +526,10 @@ public List<ContactExportDto> getExportList(ContactCriteria contactCriteria, int |
544 | 526 | List<Exposure> exposureList = em.createQuery(exposuresCq).setHint(ModelConstants.HINT_HIBERNATE_READ_ONLY, true).getResultList(); |
545 | 527 | Map<Long, List<Exposure>> exposures = exposureList.stream().collect(Collectors.groupingBy(e -> e.getEpiData().getId())); |
546 | 528 |
|
547 | | - // Load latest events info |
548 | | - // Adding a second query here is not perfect, but selecting the last event with a criteria query |
549 | | - // doesn't seem to be possible and using a native query is not an option because of user filters |
550 | | - List<ContactEventSummaryDetails> eventSummaries = eventService.getEventSummaryDetailsByContacts(resultContactsUuids); |
| 529 | + // Load event count and latest events info per contact |
| 530 | + Map<String, List<ContactEventSummaryDetails>> eventSummaries = eventService.getEventSummaryDetailsByContacts(resultContactsUuids) |
| 531 | + .stream() |
| 532 | + .collect(Collectors.groupingBy(ContactEventSummaryDetails::getContactUuid, Collectors.toList())); |
551 | 533 |
|
552 | 534 | // Adding a second query here is not perfect, but selecting the last cooperative visit with a criteria query |
553 | 535 | // doesn't seem to be possible and using a native query is not an option because of user filters |
@@ -597,22 +579,18 @@ public List<ContactExportDto> getExportList(ContactCriteria contactCriteria, int |
597 | 579 | }); |
598 | 580 | } |
599 | 581 |
|
600 | | - if (eventSummaries != null && exportContact.getEventCount() != 0) { |
601 | | - eventSummaries.stream() |
602 | | - .filter(v -> v.getContactUuid().equals(exportContact.getUuid())) |
603 | | - .max(Comparator.comparing(ContactEventSummaryDetails::getEventDate)) |
604 | | - .ifPresent(eventSummary -> { |
605 | | - exportContact.setLatestEventId(eventSummary.getEventUuid()); |
606 | | - exportContact.setLatestEventTitle(eventSummary.getEventTitle()); |
607 | | - }); |
608 | | - } |
| 582 | + List<ContactEventSummaryDetails> contactEvents = eventSummaries.getOrDefault(exportContact.getUuid(), Collections.emptyList()); |
| 583 | + exportContact.setEventCount((long) contactEvents.size()); |
| 584 | + contactEvents.stream().max(Comparator.comparing(ContactEventSummaryDetails::getEventDate)).ifPresent(eventSummary -> { |
| 585 | + exportContact.setLatestEventId(eventSummary.getEventUuid()); |
| 586 | + exportContact.setLatestEventTitle(eventSummary.getEventTitle()); |
| 587 | + }); |
609 | 588 |
|
610 | 589 | pseudonymizer.pseudonymizeDto(ContactExportDto.class, exportContact, inJurisdiction, null); |
611 | 590 | } |
612 | 591 | } |
613 | 592 |
|
614 | 593 | return exportContacts; |
615 | | - |
616 | 594 | } |
617 | 595 |
|
618 | 596 | @Override |
|
0 commit comments