use of de.symeda.sormas.utils.EventJoins in project SORMAS-Project by hzi-braunschweig.
the class EventService method buildCriteriaFilter.
public Predicate buildCriteriaFilter(EventCriteria eventCriteria, EventQueryContext eventQueryContext) {
CriteriaBuilder cb = eventQueryContext.getCriteriaBuilder();
From<?, Event> from = eventQueryContext.getRoot();
final EventJoins<Event> joins = (EventJoins<Event>) eventQueryContext.getJoins();
Predicate filter = null;
if (eventCriteria.getReportingUserRole() != null) {
filter = CriteriaBuilderHelper.and(cb, filter, cb.isMember(eventCriteria.getReportingUserRole(), joins.getReportingUser().get(User.USER_ROLES)));
}
if (eventCriteria.getDisease() != null) {
filter = CriteriaBuilderHelper.and(cb, filter, cb.equal(from.get(Event.DISEASE), eventCriteria.getDisease()));
}
if (eventCriteria.getDiseaseVariant() != null) {
filter = CriteriaBuilderHelper.and(cb, filter, cb.equal(from.get(Event.DISEASE_VARIANT), eventCriteria.getDiseaseVariant()));
}
if (eventCriteria.getEventStatus() != null) {
filter = CriteriaBuilderHelper.and(cb, filter, cb.equal(from.get(Event.EVENT_STATUS), eventCriteria.getEventStatus()));
}
if (eventCriteria.getRiskLevel() != null) {
filter = CriteriaBuilderHelper.and(cb, filter, cb.equal(from.get(Event.RISK_LEVEL), eventCriteria.getRiskLevel()));
}
if (eventCriteria.getSpecificRisk() != null) {
filter = CriteriaBuilderHelper.and(cb, filter, cb.equal(from.get(Event.SPECIFIC_RISK), eventCriteria.getSpecificRisk()));
}
if (eventCriteria.getEventInvestigationStatus() != null) {
filter = CriteriaBuilderHelper.and(cb, filter, cb.equal(from.get(Event.EVENT_INVESTIGATION_STATUS), eventCriteria.getEventInvestigationStatus()));
}
if (eventCriteria.getEventManagementStatus() != null) {
filter = CriteriaBuilderHelper.and(cb, filter, cb.equal(from.get(Event.EVENT_MANAGEMENT_STATUS), eventCriteria.getEventManagementStatus()));
}
if (eventCriteria.getEventIdentificationSource() != null) {
filter = CriteriaBuilderHelper.and(cb, filter, cb.equal(from.get(Event.EVENT_IDENTIFICATION_SOURCE), eventCriteria.getEventIdentificationSource()));
}
if (eventCriteria.getTypeOfPlace() != null) {
filter = CriteriaBuilderHelper.and(cb, filter, cb.equal(from.get(Event.TYPE_OF_PLACE), eventCriteria.getTypeOfPlace()));
}
if (eventCriteria.getRelevanceStatus() != null) {
if (eventCriteria.getRelevanceStatus() == EntityRelevanceStatus.ACTIVE) {
filter = CriteriaBuilderHelper.and(cb, filter, cb.or(cb.equal(from.get(Event.ARCHIVED), false), cb.isNull(from.get(Event.ARCHIVED))));
} else if (eventCriteria.getRelevanceStatus() == EntityRelevanceStatus.ARCHIVED) {
filter = CriteriaBuilderHelper.and(cb, filter, cb.equal(from.get(Event.ARCHIVED), true));
}
}
if (eventCriteria.getDeleted() != null) {
filter = CriteriaBuilderHelper.and(cb, filter, cb.equal(from.get(Event.DELETED), eventCriteria.getDeleted()));
}
if (eventCriteria.getRegion() != null) {
filter = CriteriaBuilderHelper.and(cb, filter, cb.equal(joins.getRegion().get(Region.UUID), eventCriteria.getRegion().getUuid()));
}
if (eventCriteria.getDistrict() != null) {
filter = CriteriaBuilderHelper.and(cb, filter, cb.equal(joins.getDistrict().get(District.UUID), eventCriteria.getDistrict().getUuid()));
}
if (eventCriteria.getCommunity() != null) {
filter = CriteriaBuilderHelper.and(cb, filter, cb.equal(joins.getCommunity().get(Community.UUID), eventCriteria.getCommunity().getUuid()));
}
if (eventCriteria.getEventEvolutionDateFrom() != null && eventCriteria.getEventEvolutionDateTo() != null) {
filter = CriteriaBuilderHelper.and(cb, filter, cb.between(from.get(Event.EVOLUTION_DATE), eventCriteria.getEventEvolutionDateFrom(), eventCriteria.getEventEvolutionDateTo()));
} else if (eventCriteria.getEventEvolutionDateFrom() != null) {
filter = CriteriaBuilderHelper.and(cb, filter, cb.greaterThanOrEqualTo(from.get(Event.EVOLUTION_DATE), eventCriteria.getEventEvolutionDateFrom()));
} else if (eventCriteria.getEventEvolutionDateTo() != null) {
filter = CriteriaBuilderHelper.and(cb, filter, cb.lessThanOrEqualTo(from.get(Event.EVOLUTION_DATE), eventCriteria.getEventEvolutionDateTo()));
}
if (eventCriteria.getResponsibleUser() != null) {
filter = CriteriaBuilderHelper.and(cb, filter, cb.equal(joins.getResponsibleUser().get(User.UUID), eventCriteria.getResponsibleUser().getUuid()));
}
if (StringUtils.isNotEmpty(eventCriteria.getFreeText())) {
String[] textFilters = eventCriteria.getFreeText().split("\\s+");
for (String textFilter : textFilters) {
if (DataHelper.isNullOrEmpty(textFilter)) {
continue;
}
Predicate likeFilters = cb.or(CriteriaBuilderHelper.ilike(cb, from.get(Event.UUID), textFilter), CriteriaBuilderHelper.unaccentedIlike(cb, from.get(Event.EXTERNAL_ID), textFilter), CriteriaBuilderHelper.unaccentedIlike(cb, from.get(Event.EXTERNAL_TOKEN), textFilter), CriteriaBuilderHelper.unaccentedIlike(cb, from.get(Event.INTERNAL_TOKEN), textFilter), CriteriaBuilderHelper.unaccentedIlike(cb, from.get(Event.EVENT_TITLE), textFilter), CriteriaBuilderHelper.unaccentedIlike(cb, from.get(Event.EVENT_DESC), textFilter), CriteriaBuilderHelper.unaccentedIlike(cb, from.get(Event.SRC_FIRST_NAME), textFilter), CriteriaBuilderHelper.unaccentedIlike(cb, from.get(Event.SRC_LAST_NAME), textFilter), CriteriaBuilderHelper.ilike(cb, from.get(Event.SRC_EMAIL), textFilter), CriteriaBuilderHelper.ilike(cb, from.get(Event.SRC_TEL_NO), textFilter));
filter = CriteriaBuilderHelper.and(cb, filter, likeFilters);
}
}
if (StringUtils.isNotEmpty(eventCriteria.getFreeTextEventParticipants())) {
Join<Event, EventParticipant> eventParticipantJoin = joins.getEventParticipants();
Join<EventParticipant, Person> personJoin = joins.getEventParticipantPersons();
final PersonQueryContext personQueryContext = new PersonQueryContext(cb, eventQueryContext.getQuery(), personJoin);
String[] textFilters = eventCriteria.getFreeTextEventParticipants().split("\\s+");
for (String textFilter : textFilters) {
if (DataHelper.isNullOrEmpty(textFilter)) {
continue;
}
Predicate likeFilters = cb.or(CriteriaBuilderHelper.ilike(cb, eventParticipantJoin.get(EventParticipant.UUID), textFilter), CriteriaBuilderHelper.unaccentedIlike(cb, personJoin.get(Person.FIRST_NAME), textFilter), CriteriaBuilderHelper.unaccentedIlike(cb, personJoin.get(Person.LAST_NAME), textFilter), CriteriaBuilderHelper.ilike(cb, (Expression<String>) personQueryContext.getSubqueryExpression(PersonQueryContext.PERSON_PHONE_SUBQUERY), textFilter), CriteriaBuilderHelper.ilike(cb, (Expression<String>) personQueryContext.getSubqueryExpression(PersonQueryContext.PERSON_EMAIL_SUBQUERY), textFilter));
filter = CriteriaBuilderHelper.and(cb, filter, likeFilters);
}
filter = CriteriaBuilderHelper.and(cb, filter, cb.isFalse(eventParticipantJoin.get(EventParticipant.DELETED)));
}
if (StringUtils.isNotEmpty(eventCriteria.getFreeTextEventGroups())) {
Join<Event, EventGroup> eventGroupJoin = joins.getEventGroup();
String[] textFilters = eventCriteria.getFreeTextEventGroups().split("\\s+");
for (String textFilter : textFilters) {
if (DataHelper.isNullOrEmpty(textFilter)) {
continue;
}
Predicate likeFilters = cb.or(CriteriaBuilderHelper.ilike(cb, eventGroupJoin.get(EventGroup.UUID), textFilter), CriteriaBuilderHelper.unaccentedIlike(cb, eventGroupJoin.get(EventGroup.NAME), textFilter));
filter = CriteriaBuilderHelper.and(cb, filter, likeFilters);
}
}
if (eventCriteria.getSrcType() != null) {
filter = CriteriaBuilderHelper.and(cb, filter, cb.equal(from.get(Event.SRC_TYPE), eventCriteria.getSrcType()));
}
if (eventCriteria.getCaze() != null) {
filter = CriteriaBuilderHelper.and(cb, filter, cb.equal(joins.getEventParticipantCases().get(Case.UUID), eventCriteria.getCaze().getUuid()));
filter = CriteriaBuilderHelper.and(cb, filter, cb.isFalse(joins.getEventParticipants().get(EventParticipant.DELETED)));
}
if (eventCriteria.getPerson() != null) {
filter = CriteriaBuilderHelper.and(cb, filter, cb.in(joins.getEventParticipantPersons().get(Person.UUID)).value(eventCriteria.getPerson().getUuid()), cb.isFalse(joins.getEventParticipants().get(EventParticipant.DELETED)));
}
if (eventCriteria.getFacilityType() != null) {
filter = CriteriaBuilderHelper.and(cb, filter, cb.equal(joins.getLocation().get(Location.FACILITY_TYPE), eventCriteria.getFacilityType()));
}
if (eventCriteria.getFacility() != null) {
filter = CriteriaBuilderHelper.and(cb, filter, cb.equal(joins.getFacility().get(Facility.UUID), eventCriteria.getFacility().getUuid()));
}
if (eventCriteria.getSuperordinateEvent() != null) {
filter = CriteriaBuilderHelper.and(cb, filter, cb.equal(from.get(Event.SUPERORDINATE_EVENT).get(AbstractDomainObject.UUID), eventCriteria.getSuperordinateEvent().getUuid()));
}
if (eventCriteria.getEventGroup() != null) {
filter = CriteriaBuilderHelper.and(cb, filter, cb.equal(joins.getEventGroup().get(EventGroup.UUID), eventCriteria.getEventGroup().getUuid()));
}
if (CollectionUtils.isNotEmpty(eventCriteria.getExcludedUuids())) {
filter = CriteriaBuilderHelper.and(cb, filter, cb.not(from.get(AbstractDomainObject.UUID).in(eventCriteria.getExcludedUuids())));
}
if (Boolean.TRUE.equals(eventCriteria.getHasNoSuperordinateEvent())) {
filter = CriteriaBuilderHelper.and(cb, filter, cb.isNull(from.get(Event.SUPERORDINATE_EVENT)));
}
filter = CriteriaBuilderHelper.and(cb, filter, createEventDateFilter(eventQueryContext.getQuery(), cb, from, eventCriteria));
filter = CriteriaBuilderHelper.and(cb, filter, externalShareInfoService.buildShareCriteriaFilter(eventCriteria, eventQueryContext.getQuery(), cb, from, ExternalShareInfo.EVENT, (latestShareDate) -> createChangeDateFilter(cb, from, latestShareDate)));
return filter;
}
use of de.symeda.sormas.utils.EventJoins in project SORMAS-Project by hzi-braunschweig.
the class EventService method getUuidByCaseUuidOrPersonUuid.
public String getUuidByCaseUuidOrPersonUuid(String searchTerm) {
if (StringUtils.isEmpty(searchTerm)) {
return null;
}
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<String> cq = cb.createQuery(String.class);
Root<Event> root = cq.from(Event.class);
EventJoins<Event> joins = new EventJoins<>(root);
Predicate filter = cb.or(cb.equal(cb.lower(joins.getEventParticipantCases().get(Case.UUID)), searchTerm.toLowerCase()), cb.equal(cb.lower(joins.getEventParticipantPersons().get(Person.UUID)), searchTerm.toLowerCase()));
cq.where(filter);
cq.orderBy(cb.desc(root.get(Event.REPORT_DATE_TIME)));
cq.select(root.get(Event.UUID));
return QueryHelper.getFirstResult(em, cq);
}
use of de.symeda.sormas.utils.EventJoins in project SORMAS-Project by hzi-braunschweig.
the class EventFacadeEjb method getExportList.
@Override
public List<EventExportDto> getExportList(EventCriteria eventCriteria, Collection<String> selectedRows, Integer first, Integer max) {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<EventExportDto> cq = cb.createQuery(EventExportDto.class);
Root<Event> event = cq.from(Event.class);
EventQueryContext eventQueryContext = new EventQueryContext(cb, cq, event);
EventJoins<Event> eventJoins = (EventJoins<Event>) eventQueryContext.getJoins();
Join<Event, Location> location = eventJoins.getLocation();
Join<Location, Region> region = eventJoins.getRegion();
Join<Location, District> district = eventJoins.getDistrict();
Join<Location, Community> community = eventJoins.getCommunity();
Join<Event, User> reportingUser = eventJoins.getReportingUser();
Join<Event, User> responsibleUser = eventJoins.getResponsibleUser();
cq.multiselect(event.get(Event.UUID), event.get(Event.EXTERNAL_ID), event.get(Event.EXTERNAL_TOKEN), event.get(Event.INTERNAL_TOKEN), event.get(Event.EVENT_STATUS), event.get(Event.RISK_LEVEL), event.get(Event.SPECIFIC_RISK), event.get(Event.EVENT_INVESTIGATION_STATUS), event.get(Event.EVENT_INVESTIGATION_START_DATE), event.get(Event.EVENT_INVESTIGATION_END_DATE), event.get(Event.DISEASE), event.get(Event.DISEASE_VARIANT), event.get(Event.DISEASE_DETAILS), event.get(Event.DISEASE_VARIANT_DETAILS), event.get(Event.START_DATE), event.get(Event.END_DATE), event.get(Event.EVOLUTION_DATE), event.get(Event.EVOLUTION_COMMENT), event.get(Event.EVENT_TITLE), event.get(Event.EVENT_DESC), event.get(Event.DISEASE_TRANSMISSION_MODE), event.get(Event.NOSOCOMIAL), event.get(Event.TRANSREGIONAL_OUTBREAK), event.get(Event.MEANS_OF_TRANSPORT), event.get(Event.MEANS_OF_TRANSPORT_DETAILS), region.get(Region.UUID), region.get(Region.NAME), district.get(District.UUID), district.get(District.NAME), community.get(Community.UUID), community.get(Community.NAME), location.get(Location.CITY), location.get(Location.STREET), location.get(Location.HOUSE_NUMBER), location.get(Location.ADDITIONAL_INFORMATION), event.get(Event.SRC_TYPE), event.get(Event.SRC_INSTITUTIONAL_PARTNER_TYPE), event.get(Event.SRC_INSTITUTIONAL_PARTNER_TYPE_DETAILS), event.get(Event.SRC_FIRST_NAME), event.get(Event.SRC_LAST_NAME), event.get(Event.SRC_TEL_NO), event.get(Event.SRC_EMAIL), event.get(Event.SRC_MEDIA_WEBSITE), event.get(Event.SRC_MEDIA_NAME), event.get(Event.SRC_MEDIA_DETAILS), event.get(Event.REPORT_DATE_TIME), reportingUser.get(User.UUID), reportingUser.get(User.FIRST_NAME), reportingUser.get(User.LAST_NAME), responsibleUser.get(User.UUID), responsibleUser.get(User.FIRST_NAME), responsibleUser.get(User.LAST_NAME), JurisdictionHelper.booleanSelector(cb, service.inJurisdictionOrOwned(eventQueryContext)), event.get(Event.EVENT_MANAGEMENT_STATUS), event.get(Event.EVENT_IDENTIFICATION_SOURCE));
cq.distinct(true);
Predicate filter = service.createUserFilter(cb, cq, event);
if (eventCriteria != null) {
Predicate criteriaFilter = service.buildCriteriaFilter(eventCriteria, eventQueryContext);
filter = CriteriaBuilderHelper.and(cb, filter, criteriaFilter);
}
if (CollectionUtils.isNotEmpty(selectedRows)) {
filter = CriteriaBuilderHelper.andInValues(selectedRows, filter, cb, event.get(Event.UUID));
}
cq.where(filter);
cq.orderBy(cb.desc(event.get(Event.REPORT_DATE_TIME)));
List<EventExportDto> exportList = QueryHelper.getResultList(em, cq, first, max);
Map<String, Long> participantCounts = new HashMap<>();
Map<String, Long> caseCounts = new HashMap<>();
Map<String, Long> deathCounts = new HashMap<>();
Map<String, Long> contactCounts = new HashMap<>();
Map<String, Long> contactCountsSourceInEvent = new HashMap<>();
Map<String, EventGroupReferenceDto> latestEventGroupByEventId = new HashMap<>();
Map<String, Long> eventGroupCountByEventId = new HashMap<>();
if (exportList != null && !exportList.isEmpty()) {
List<Object[]> objectQueryList = null;
List<String> eventUuids = exportList.stream().map(EventExportDto::getUuid).collect(Collectors.toList());
// Participant, Case and Death Count
CriteriaQuery<Object[]> participantCQ = cb.createQuery(Object[].class);
Root<EventParticipant> epRoot = participantCQ.from(EventParticipant.class);
Join<EventParticipant, Case> caseJoin = epRoot.join(EventParticipant.RESULTING_CASE, JoinType.LEFT);
Predicate notDeleted = cb.isFalse(epRoot.get(EventParticipant.DELETED));
Predicate isInExportlist = CriteriaBuilderHelper.andInValues(eventUuids, null, cb, epRoot.get(EventParticipant.EVENT).get(AbstractDomainObject.UUID));
participantCQ.multiselect(epRoot.get(EventParticipant.EVENT).get(AbstractDomainObject.UUID), cb.count(epRoot), cb.sum(cb.selectCase().when(cb.isNotNull(epRoot.get(EventParticipant.RESULTING_CASE)), 1).otherwise(0).as(Long.class)), cb.sum(cb.selectCase().when(cb.equal(caseJoin.get(Case.OUTCOME), CaseOutcome.DECEASED), 1).otherwise(0).as(Long.class)));
participantCQ.where(notDeleted, isInExportlist);
participantCQ.groupBy(epRoot.get(EventParticipant.EVENT).get(AbstractDomainObject.UUID));
objectQueryList = em.createQuery(participantCQ).getResultList();
if (objectQueryList != null) {
objectQueryList.forEach(r -> {
participantCounts.put((String) r[0], (Long) r[1]);
caseCounts.put((String) r[0], (Long) r[2]);
deathCounts.put((String) r[0], (Long) r[3]);
});
}
// Contact Count (with and without sourcecase in event) using theta join
CriteriaQuery<Object[]> contactCQ = cb.createQuery(Object[].class);
epRoot = contactCQ.from(EventParticipant.class);
Root<Contact> contactRoot = contactCQ.from(Contact.class);
Predicate participantPersonEqualsContactPerson = cb.equal(epRoot.get(EventParticipant.PERSON), contactRoot.get(Contact.PERSON));
notDeleted = cb.isFalse(epRoot.get(EventParticipant.DELETED));
Predicate contactNotDeleted = cb.isFalse(contactRoot.get(Contact.DELETED));
isInExportlist = CriteriaBuilderHelper.andInValues(eventUuids, null, cb, epRoot.get(EventParticipant.EVENT).get(AbstractDomainObject.UUID));
Subquery<EventParticipant> sourceCaseSubquery = contactCQ.subquery(EventParticipant.class);
Root<EventParticipant> epr2 = sourceCaseSubquery.from(EventParticipant.class);
sourceCaseSubquery.select(epr2);
sourceCaseSubquery.where(cb.equal(epr2.get(EventParticipant.RESULTING_CASE), contactRoot.get(Contact.CAZE)), cb.equal(epr2.get(EventParticipant.EVENT), epRoot.get(EventParticipant.EVENT)));
contactCQ.multiselect(epRoot.get(EventParticipant.EVENT).get(AbstractDomainObject.UUID), cb.count(epRoot), cb.sum(cb.selectCase().when(cb.exists(sourceCaseSubquery), 1).otherwise(0).as(Long.class)));
contactCQ.where(participantPersonEqualsContactPerson, notDeleted, contactNotDeleted, isInExportlist);
contactCQ.groupBy(epRoot.get(EventParticipant.EVENT).get(AbstractDomainObject.UUID));
objectQueryList = em.createQuery(contactCQ).getResultList();
if (objectQueryList != null) {
objectQueryList.forEach(r -> {
contactCounts.put((String) r[0], ((Long) r[1]));
contactCountsSourceInEvent.put((String) r[0], ((Long) r[2]));
});
}
if (featureConfigurationFacade.isFeatureEnabled(FeatureType.EVENT_GROUPS)) {
// Get latest EventGroup with EventGroup count
CriteriaQuery<Object[]> latestEventCQ = cb.createQuery(Object[].class);
Root<Event> eventRoot = latestEventCQ.from(Event.class);
Join<Event, EventGroup> eventGroupJoin = eventRoot.join(Event.EVENT_GROUPS, JoinType.INNER);
isInExportlist = CriteriaBuilderHelper.andInValues(eventUuids, null, cb, eventRoot.get(Event.UUID));
latestEventCQ.where(notDeleted, isInExportlist);
latestEventCQ.multiselect(eventRoot.get(Event.UUID), CriteriaBuilderHelper.windowFirstValueDesc(cb, eventGroupJoin.get(EventGroup.UUID), eventRoot.get(Event.UUID), eventGroupJoin.get(EventGroup.CREATION_DATE)), CriteriaBuilderHelper.windowFirstValueDesc(cb, eventGroupJoin.get(EventGroup.NAME), eventRoot.get(Event.UUID), eventGroupJoin.get(EventGroup.CREATION_DATE)), CriteriaBuilderHelper.windowCount(cb, eventGroupJoin.get(EventGroup.ID), eventRoot.get(Event.UUID)));
objectQueryList = em.createQuery(latestEventCQ).getResultList();
if (objectQueryList != null) {
objectQueryList.forEach(r -> {
EventGroupReferenceDto eventGroupReference = new EventGroupReferenceDto((String) r[1], (String) r[2]);
latestEventGroupByEventId.put((String) r[0], eventGroupReference);
eventGroupCountByEventId.put((String) r[0], ((Number) r[3]).longValue());
});
}
}
}
if (exportList != null) {
for (EventExportDto exportDto : exportList) {
Optional.ofNullable(participantCounts.get(exportDto.getUuid())).ifPresent(exportDto::setParticipantCount);
Optional.ofNullable(caseCounts.get(exportDto.getUuid())).ifPresent(exportDto::setCaseCount);
Optional.ofNullable(deathCounts.get(exportDto.getUuid())).ifPresent(exportDto::setDeathCount);
Optional.ofNullable(contactCounts.get(exportDto.getUuid())).ifPresent(exportDto::setContactCount);
Optional.ofNullable(contactCountsSourceInEvent.get(exportDto.getUuid())).ifPresent(exportDto::setContactCountSourceInEvent);
Optional.ofNullable(latestEventGroupByEventId.get(exportDto.getUuid())).ifPresent(exportDto::setLatestEventGroup);
Optional.ofNullable(eventGroupCountByEventId.get(exportDto.getUuid())).ifPresent(exportDto::setEventGroupCount);
}
}
return exportList;
}
use of de.symeda.sormas.utils.EventJoins in project SORMAS-Project by hzi-braunschweig.
the class EventFacadeEjb method getSubordinateEventUuids.
@Override
public List<String> getSubordinateEventUuids(List<String> uuids) {
if (uuids.isEmpty()) {
return Collections.emptyList();
}
List<String> subordinateEventUuids = new ArrayList<>();
IterableHelper.executeBatched(uuids, ModelConstants.PARAMETER_LIMIT, (batchedUuids) -> {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<String> cq = cb.createQuery(String.class);
Root<Event> from = cq.from(Event.class);
EventJoins<Event> eventJoins = new EventJoins<>(from);
Predicate filters = CriteriaBuilderHelper.and(cb, service.createUserFilter(cb, cq, from), service.createActiveEventsFilter(cb, from), eventJoins.getSuperordinateEvent().get(Event.UUID).in(batchedUuids));
cq.where(filters);
cq.select(from.get(Event.UUID));
subordinateEventUuids.addAll(em.createQuery(cq).getResultList());
});
return subordinateEventUuids;
}
use of de.symeda.sormas.utils.EventJoins in project SORMAS-Project by hzi-braunschweig.
the class EventFacadeEjb method getIndexList.
@Override
public List<EventIndexDto> getIndexList(EventCriteria eventCriteria, Integer first, Integer max, List<SortProperty> sortProperties) {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<EventIndexDto> cq = cb.createQuery(EventIndexDto.class);
Root<Event> event = cq.from(Event.class);
EventQueryContext eventQueryContext = new EventQueryContext(cb, cq, event);
EventJoins<Event> eventJoins = (EventJoins<Event>) eventQueryContext.getJoins();
Join<Event, Location> location = eventJoins.getLocation();
Join<Location, Region> region = eventJoins.getRegion();
Join<Location, District> district = eventJoins.getDistrict();
Join<Location, Community> community = eventJoins.getCommunity();
Join<Event, User> reportingUser = eventJoins.getReportingUser();
Join<Event, User> responsibleUser = eventJoins.getResponsibleUser();
cq.multiselect(event.get(Event.ID), event.get(Event.UUID), event.get(Event.EXTERNAL_ID), event.get(Event.EXTERNAL_TOKEN), event.get(Event.INTERNAL_TOKEN), event.get(Event.EVENT_STATUS), event.get(Event.RISK_LEVEL), event.get(Event.SPECIFIC_RISK), event.get(Event.EVENT_INVESTIGATION_STATUS), event.get(Event.EVENT_MANAGEMENT_STATUS), event.get(Event.DISEASE), event.get(Event.DISEASE_VARIANT), event.get(Event.DISEASE_DETAILS), event.get(Event.START_DATE), event.get(Event.END_DATE), event.get(Event.EVOLUTION_DATE), event.get(Event.EVENT_TITLE), region.get(Region.UUID), region.get(Region.NAME), district.get(District.UUID), district.get(District.NAME), community.get(Community.UUID), community.get(Community.NAME), location.get(Location.CITY), location.get(Location.STREET), location.get(Location.HOUSE_NUMBER), location.get(Location.ADDITIONAL_INFORMATION), event.get(Event.SRC_TYPE), event.get(Event.SRC_FIRST_NAME), event.get(Event.SRC_LAST_NAME), event.get(Event.SRC_TEL_NO), event.get(Event.SRC_MEDIA_WEBSITE), event.get(Event.SRC_MEDIA_NAME), event.get(Event.REPORT_DATE_TIME), reportingUser.get(User.UUID), reportingUser.get(User.FIRST_NAME), reportingUser.get(User.LAST_NAME), responsibleUser.get(User.UUID), responsibleUser.get(User.FIRST_NAME), responsibleUser.get(User.LAST_NAME), JurisdictionHelper.booleanSelector(cb, service.inJurisdictionOrOwned(eventQueryContext)), event.get(Event.CHANGE_DATE), event.get(Event.EVENT_IDENTIFICATION_SOURCE));
Predicate filter = null;
if (eventCriteria != null) {
if (eventCriteria.getUserFilterIncluded()) {
EventUserFilterCriteria eventUserFilterCriteria = new EventUserFilterCriteria();
eventUserFilterCriteria.includeUserCaseAndEventParticipantFilter(true);
filter = service.createUserFilter(cb, cq, event, eventUserFilterCriteria);
}
Predicate criteriaFilter = service.buildCriteriaFilter(eventCriteria, eventQueryContext);
filter = CriteriaBuilderHelper.and(cb, filter, criteriaFilter);
}
if (filter != null) {
cq.where(filter);
}
if (sortProperties != null && sortProperties.size() > 0) {
List<Order> order = new ArrayList<Order>(sortProperties.size());
for (SortProperty sortProperty : sortProperties) {
Expression<?> expression;
switch(sortProperty.propertyName) {
case EventIndexDto.UUID:
case EventIndexDto.EXTERNAL_ID:
case EventIndexDto.EXTERNAL_TOKEN:
case EventIndexDto.INTERNAL_TOKEN:
case EventIndexDto.EVENT_STATUS:
case EventIndexDto.RISK_LEVEL:
case EventIndexDto.SPECIFIC_RISK:
case EventIndexDto.EVENT_INVESTIGATION_STATUS:
case EventIndexDto.EVENT_MANAGEMENT_STATUS:
case EventIndexDto.DISEASE:
case EventIndexDto.DISEASE_VARIANT:
case EventIndexDto.DISEASE_DETAILS:
case EventIndexDto.START_DATE:
case EventIndexDto.EVOLUTION_DATE:
case EventIndexDto.EVENT_TITLE:
case EventIndexDto.SRC_FIRST_NAME:
case EventIndexDto.SRC_LAST_NAME:
case EventIndexDto.SRC_TEL_NO:
case EventIndexDto.SRC_TYPE:
case EventIndexDto.REPORT_DATE_TIME:
case EventIndexDto.EVENT_IDENTIFICATION_SOURCE:
expression = event.get(sortProperty.propertyName);
break;
case EventIndexDto.EVENT_LOCATION:
expression = region.get(Region.NAME);
order.add(sortProperty.ascending ? cb.asc(expression) : cb.desc(expression));
expression = district.get(District.NAME);
order.add(sortProperty.ascending ? cb.asc(expression) : cb.desc(expression));
expression = community.get(Community.NAME);
break;
case EventIndexDto.REGION:
expression = region.get(Region.NAME);
break;
case EventIndexDto.DISTRICT:
expression = district.get(District.NAME);
break;
case EventIndexDto.COMMUNITY:
expression = community.get(Community.NAME);
break;
case EventIndexDto.ADDRESS:
expression = location.get(Location.CITY);
order.add(sortProperty.ascending ? cb.asc(expression) : cb.desc(expression));
expression = location.get(Location.STREET);
order.add(sortProperty.ascending ? cb.asc(expression) : cb.desc(expression));
expression = location.get(Location.HOUSE_NUMBER);
order.add(sortProperty.ascending ? cb.asc(expression) : cb.desc(expression));
expression = location.get(Location.ADDITIONAL_INFORMATION);
break;
case EventIndexDto.REPORTING_USER:
expression = reportingUser.get(User.FIRST_NAME);
order.add(sortProperty.ascending ? cb.asc(expression) : cb.desc(expression));
expression = reportingUser.get(User.LAST_NAME);
break;
case EventIndexDto.RESPONSIBLE_USER:
expression = responsibleUser.get(User.FIRST_NAME);
order.add(sortProperty.ascending ? cb.asc(expression) : cb.desc(expression));
expression = responsibleUser.get(User.LAST_NAME);
break;
default:
throw new IllegalArgumentException(sortProperty.propertyName);
}
order.add(sortProperty.ascending ? cb.asc(expression) : cb.desc(expression));
}
cq.orderBy(order);
} else {
cq.orderBy(cb.desc(event.get(Event.CHANGE_DATE)));
}
cq.distinct(true);
List<EventIndexDto> indexList = QueryHelper.getResultList(em, cq, first, max);
Map<String, Long> participantCounts = new HashMap<>();
Map<String, Long> caseCounts = new HashMap<>();
Map<String, Long> deathCounts = new HashMap<>();
Map<String, Long> contactCounts = new HashMap<>();
Map<String, Long> contactCountsSourceInEvent = new HashMap<>();
Map<String, EventGroupsIndexDto> eventGroupsByEventId = new HashMap<>();
Map<String, ExternalShareInfoCountAndLatestDate> survToolShareCountAndDates = new HashMap<>();
if (CollectionUtils.isNotEmpty(indexList)) {
List<String> eventUuids = indexList.stream().map(EventIndexDto::getUuid).collect(Collectors.toList());
List<Long> eventIds = indexList.stream().map(EventIndexDto::getId).collect(Collectors.toList());
List<Object[]> objectQueryList = null;
// Participant, Case and Death Count
CriteriaQuery<Object[]> participantCQ = cb.createQuery(Object[].class);
Root<EventParticipant> epRoot = participantCQ.from(EventParticipant.class);
Join<EventParticipant, Case> caseJoin = epRoot.join(EventParticipant.RESULTING_CASE, JoinType.LEFT);
Predicate notDeleted = cb.isFalse(epRoot.get(EventParticipant.DELETED));
Predicate isInIndexlist = CriteriaBuilderHelper.andInValues(eventUuids, null, cb, epRoot.get(EventParticipant.EVENT).get(AbstractDomainObject.UUID));
participantCQ.multiselect(epRoot.get(EventParticipant.EVENT).get(AbstractDomainObject.UUID), cb.count(epRoot), cb.sum(cb.selectCase().when(cb.isNotNull(epRoot.get(EventParticipant.RESULTING_CASE)), 1).otherwise(0).as(Long.class)), cb.sum(cb.selectCase().when(cb.equal(caseJoin.get(Case.OUTCOME), CaseOutcome.DECEASED), 1).otherwise(0).as(Long.class)));
participantCQ.where(notDeleted, isInIndexlist);
participantCQ.groupBy(epRoot.get(EventParticipant.EVENT).get(AbstractDomainObject.UUID));
objectQueryList = em.createQuery(participantCQ).getResultList();
if (objectQueryList != null) {
objectQueryList.forEach(r -> {
participantCounts.put((String) r[0], (Long) r[1]);
caseCounts.put((String) r[0], (Long) r[2]);
deathCounts.put((String) r[0], (Long) r[3]);
});
}
// Contact Count (with and without sourcecase in event) using theta join
CriteriaQuery<Object[]> contactCQ = cb.createQuery(Object[].class);
epRoot = contactCQ.from(EventParticipant.class);
Root<Contact> contactRoot = contactCQ.from(Contact.class);
Predicate participantPersonEqualsContactPerson = cb.equal(epRoot.get(EventParticipant.PERSON), contactRoot.get(Contact.PERSON));
notDeleted = cb.isFalse(epRoot.get(EventParticipant.DELETED));
Predicate contactNotDeleted = cb.isFalse(contactRoot.get(Contact.DELETED));
isInIndexlist = CriteriaBuilderHelper.andInValues(eventUuids, null, cb, epRoot.get(EventParticipant.EVENT).get(AbstractDomainObject.UUID));
Subquery<EventParticipant> sourceCaseSubquery = contactCQ.subquery(EventParticipant.class);
Root<EventParticipant> epr2 = sourceCaseSubquery.from(EventParticipant.class);
sourceCaseSubquery.select(epr2);
sourceCaseSubquery.where(cb.equal(epr2.get(EventParticipant.RESULTING_CASE), contactRoot.get(Contact.CAZE)), cb.equal(epr2.get(EventParticipant.EVENT), epRoot.get(EventParticipant.EVENT)));
contactCQ.multiselect(epRoot.get(EventParticipant.EVENT).get(AbstractDomainObject.UUID), cb.count(epRoot), cb.sum(cb.selectCase().when(cb.exists(sourceCaseSubquery), 1).otherwise(0).as(Long.class)));
contactCQ.where(participantPersonEqualsContactPerson, notDeleted, contactNotDeleted, isInIndexlist);
contactCQ.groupBy(epRoot.get(EventParticipant.EVENT).get(AbstractDomainObject.UUID));
objectQueryList = em.createQuery(contactCQ).getResultList();
if (objectQueryList != null) {
objectQueryList.forEach(r -> {
contactCounts.put((String) r[0], ((Long) r[1]));
contactCountsSourceInEvent.put((String) r[0], ((Long) r[2]));
});
}
if (featureConfigurationFacade.isFeatureEnabled(FeatureType.EVENT_GROUPS)) {
// Get latest EventGroup with EventGroup count
CriteriaQuery<Object[]> latestEventCQ = cb.createQuery(Object[].class);
Root<Event> eventRoot = latestEventCQ.from(Event.class);
Join<Event, EventGroup> eventGroupJoin = eventRoot.join(Event.EVENT_GROUPS, JoinType.INNER);
isInIndexlist = CriteriaBuilderHelper.andInValues(eventUuids, null, cb, eventRoot.get(Event.UUID));
latestEventCQ.where(isInIndexlist);
latestEventCQ.multiselect(eventRoot.get(Event.UUID), CriteriaBuilderHelper.windowFirstValueDesc(cb, eventGroupJoin.get(EventGroup.UUID), eventRoot.get(Event.UUID), eventGroupJoin.get(EventGroup.CREATION_DATE)), CriteriaBuilderHelper.windowFirstValueDesc(cb, eventGroupJoin.get(EventGroup.NAME), eventRoot.get(Event.UUID), eventGroupJoin.get(EventGroup.CREATION_DATE)), CriteriaBuilderHelper.windowCount(cb, eventGroupJoin.get(EventGroup.ID), eventRoot.get(Event.UUID)));
objectQueryList = em.createQuery(latestEventCQ).getResultList();
if (objectQueryList != null) {
objectQueryList.forEach(r -> {
EventGroupReferenceDto eventGroupReference = new EventGroupReferenceDto((String) r[1], (String) r[2]);
EventGroupsIndexDto eventGroups = new EventGroupsIndexDto(eventGroupReference, ((Number) r[3]).longValue());
eventGroupsByEventId.put((String) r[0], eventGroups);
});
}
}
if (externalSurveillanceToolFacade.isFeatureEnabled()) {
survToolShareCountAndDates = externalShareInfoService.getEventShareCountAndLatestDate(eventIds).stream().collect(Collectors.toMap(ExternalShareInfoCountAndLatestDate::getAssociatedObjectUuid, Function.identity()));
}
}
if (indexList != null) {
for (EventIndexDto eventDto : indexList) {
Optional.ofNullable(participantCounts.get(eventDto.getUuid())).ifPresent(eventDto::setParticipantCount);
Optional.ofNullable(caseCounts.get(eventDto.getUuid())).ifPresent(eventDto::setCaseCount);
Optional.ofNullable(deathCounts.get(eventDto.getUuid())).ifPresent(eventDto::setDeathCount);
Optional.ofNullable(contactCounts.get(eventDto.getUuid())).ifPresent(eventDto::setContactCount);
Optional.ofNullable(contactCountsSourceInEvent.get(eventDto.getUuid())).ifPresent(eventDto::setContactCountSourceInEvent);
Optional.ofNullable(eventGroupsByEventId.get(eventDto.getUuid())).ifPresent(eventDto::setEventGroups);
Optional.ofNullable(survToolShareCountAndDates.get(eventDto.getUuid())).ifPresent((c) -> {
eventDto.setSurveillanceToolStatus(c.getLatestStatus());
eventDto.setSurveillanceToolLastShareDate(c.getLatestDate());
eventDto.setSurveillanceToolShareCount(c.getCount());
});
}
}
Pseudonymizer pseudonymizer = Pseudonymizer.getDefault(userService::hasRight, I18nProperties.getCaption(Captions.inaccessibleValue));
pseudonymizer.pseudonymizeDtoCollection(EventIndexDto.class, indexList, EventIndexDto::getInJurisdictionOrOwned, (c, isInJurisdiction) -> {
});
return indexList;
}
Aggregations