|
41 | 41 | import javax.persistence.criteria.Order; |
42 | 42 | import javax.persistence.criteria.Predicate; |
43 | 43 | import javax.persistence.criteria.Root; |
| 44 | +import javax.persistence.criteria.Subquery; |
44 | 45 | import javax.validation.constraints.NotNull; |
45 | 46 |
|
46 | 47 | import de.symeda.sormas.api.Language; |
@@ -583,27 +584,31 @@ public Map<String, Long> getContactCountPerEventParticipant( |
583 | 584 | IterableHelper.executeBatched(eventParticipantUuids, ModelConstants.PARAMETER_LIMIT, e -> { |
584 | 585 | CriteriaBuilder cb = em.getCriteriaBuilder(); |
585 | 586 | CriteriaQuery<Object[]> contactCount = cb.createQuery(Object[].class); |
586 | | - Root<Contact> contact = contactCount.from(Contact.class); |
587 | | - Join<Contact, Person> person = contact.join(Contact.PERSON); |
588 | | - Join<Person, EventParticipant> eventParticipant = person.join(Person.EVENT_PARTICIPANTS); |
589 | | - |
590 | | - contactCount.where( |
591 | | - eventParticipant.get(EventParticipant.UUID).in(eventParticipantUuids), |
592 | | - cb.isFalse(eventParticipant.get(EventParticipant.DELETED)), |
593 | | - cb.isFalse(contact.get(Contact.DELETED))); |
| 587 | + |
| 588 | + Root<EventParticipant> epRoot = contactCount.from(EventParticipant.class); |
| 589 | + Root<Contact> contactRoot = contactCount.from(Contact.class); |
| 590 | + |
| 591 | + Predicate participantPersonEqualsContactPerson = cb.equal(epRoot.get(EventParticipant.PERSON), contactRoot.get(Contact.PERSON)); |
| 592 | + Predicate notDeleted = cb.isFalse(epRoot.get(EventParticipant.DELETED)); |
| 593 | + Predicate contactNotDeleted = cb.isFalse(contactRoot.get(Contact.DELETED)); |
| 594 | + Predicate isInEvent = epRoot.get(EventParticipant.UUID).in(eventParticipantUuids); |
| 595 | + |
594 | 596 | if (Boolean.TRUE.equals(eventParticipantCriteria.getOnlyCountContactsWithSourceCaseInEvent())) { |
595 | | - contactCount.where( |
596 | | - contactCount.getRestriction(), |
597 | | - contact.join(Contact.CAZE) |
598 | | - .get(Case.UUID) |
599 | | - .in( |
600 | | - eventParticipant.join(EventParticipant.EVENT) |
601 | | - .join(Event.EVENT_PERSONS) |
602 | | - .join(EventParticipant.RESULTING_CASE) |
603 | | - .get(Case.UUID))); |
| 597 | + Subquery<EventParticipant> sourceCaseSubquery = contactCount.subquery(EventParticipant.class); |
| 598 | + Root<EventParticipant> epr2 = sourceCaseSubquery.from(EventParticipant.class); |
| 599 | + sourceCaseSubquery.select(epr2); |
| 600 | + sourceCaseSubquery.where( |
| 601 | + cb.equal(epr2.get(EventParticipant.RESULTING_CASE), contactRoot.get(Contact.CAZE)), |
| 602 | + cb.equal(epr2.get(EventParticipant.EVENT), epRoot.get(EventParticipant.EVENT))); |
| 603 | + |
| 604 | + contactCount.multiselect( |
| 605 | + epRoot.get(EventParticipant.UUID), |
| 606 | + cb.sum(cb.selectCase().when(cb.exists(sourceCaseSubquery), 1).otherwise(0).as(Long.class))); |
| 607 | + } else { |
| 608 | + contactCount.multiselect(epRoot.get(EventParticipant.UUID), cb.count(epRoot)); |
604 | 609 | } |
605 | | - contactCount.multiselect(eventParticipant.get(EventParticipant.UUID), cb.countDistinct(contact.get(Contact.UUID))); |
606 | | - contactCount.groupBy(eventParticipant.get(EventParticipant.UUID)); |
| 610 | + contactCount.where(participantPersonEqualsContactPerson, notDeleted, contactNotDeleted, isInEvent); |
| 611 | + contactCount.groupBy(epRoot.get(EventParticipant.UUID)); |
607 | 612 |
|
608 | 613 | List<Object[]> resultList = em.createQuery(contactCount).getResultList(); |
609 | 614 | resultList.forEach(r -> contactCountMap.put((String) r[0], (Long) r[1])); |
|
0 commit comments