use of de.symeda.sormas.backend.immunization.ImmunizationJoins in project SORMAS-Project by hzi-braunschweig.
the class PersonService method getAllAfter.
@Override
public // todo refactor this to use the create user filter form persons
List<Person> getAllAfter(Date date, Integer batchSize, String lastSynchronizedUuid) {
User user = getCurrentUser();
final CriteriaBuilder cb = em.getCriteriaBuilder();
final CriteriaQuery<Person> personsQuery = cb.createQuery(Person.class);
final Root<Person> personsRoot = personsQuery.from(Person.class);
final PersonQueryContext personQueryContext = new PersonQueryContext(cb, personsQuery, personsRoot);
final PersonJoins joins = personQueryContext.getJoins();
// persons by district
Join<Person, Location> address = joins.getAddress();
Predicate districtFilter = cb.equal(address.get(Location.DISTRICT), user.getDistrict());
// date range
if (date != null) {
Predicate dateFilter = createChangeDateFilter(personQueryContext, DateHelper.toTimestampUpper(date), lastSynchronizedUuid);
districtFilter = cb.and(districtFilter, dateFilter);
}
personsQuery.where(districtFilter);
List<Person> districtResultList = getBatchedQueryResults(cb, personsQuery, personsRoot, batchSize);
// persons by case
CriteriaQuery<Person> casePersonsQuery = cb.createQuery(Person.class);
Root<Case> casePersonsRoot = casePersonsQuery.from(Case.class);
Join<Person, Person> casePersonsSelect = casePersonsRoot.join(Case.PERSON);
casePersonsSelect.fetch(Person.ADDRESS);
casePersonsQuery.select(casePersonsSelect);
Predicate casePersonsFilter = caseService.createUserFilter(new CaseQueryContext(cb, casePersonsQuery, new CaseJoins(casePersonsRoot)));
// date range
if (date != null) {
Predicate dateFilter = createChangeDateFilter(cb, casePersonsSelect, DateHelper.toTimestampUpper(date), lastSynchronizedUuid);
if (batchSize == null) {
// include case change dates: When a case is relocated it may become available to another user and this will have to include the person as-well
Predicate caseDateFilter = caseService.createChangeDateFilter(cb, casePersonsRoot, DateHelper.toTimestampUpper(date));
dateFilter = cb.or(dateFilter, caseDateFilter);
}
if (casePersonsFilter != null) {
casePersonsFilter = cb.and(casePersonsFilter, dateFilter);
} else {
casePersonsFilter = dateFilter;
}
}
if (casePersonsFilter != null) {
casePersonsQuery.where(casePersonsFilter);
}
casePersonsQuery.distinct(true);
List<Person> casePersonsResultList = getBatchedQueryResults(cb, casePersonsQuery, casePersonsSelect, batchSize);
// persons by contact
CriteriaQuery<Person> contactPersonsQuery = cb.createQuery(Person.class);
Root<Contact> contactPersonsRoot = contactPersonsQuery.from(Contact.class);
Join<Person, Person> contactPersonsSelect = contactPersonsRoot.join(Contact.PERSON);
contactPersonsSelect.fetch(Person.ADDRESS);
contactPersonsQuery.select(contactPersonsSelect);
Predicate contactPersonsFilter = contactService.createUserFilter(new ContactQueryContext(cb, contactPersonsQuery, new ContactJoins(contactPersonsRoot)));
// date range
if (date != null) {
Predicate dateFilter = createChangeDateFilter(cb, contactPersonsSelect, DateHelper.toTimestampUpper(date), lastSynchronizedUuid);
if (batchSize == null) {
Predicate contactDateFilter = contactService.createChangeDateFilter(cb, contactPersonsRoot, date);
dateFilter = cb.or(dateFilter, contactDateFilter);
}
contactPersonsFilter = and(cb, contactPersonsFilter, dateFilter);
}
if (contactPersonsFilter != null) {
contactPersonsQuery.where(contactPersonsFilter);
}
contactPersonsQuery.distinct(true);
List<Person> contactPersonsResultList = getBatchedQueryResults(cb, contactPersonsQuery, contactPersonsSelect, batchSize);
// persons by event participant
CriteriaQuery<Person> eventPersonsQuery = cb.createQuery(Person.class);
Root<EventParticipant> eventPersonsRoot = eventPersonsQuery.from(EventParticipant.class);
Join<Person, Person> eventPersonsSelect = eventPersonsRoot.join(EventParticipant.PERSON);
eventPersonsSelect.fetch(Person.ADDRESS);
eventPersonsQuery.select(eventPersonsSelect);
Predicate eventPersonsFilter = eventParticipantService.createUserFilter(new EventParticipantQueryContext(cb, eventPersonsQuery, new EventParticipantJoins(eventPersonsRoot)));
// date range
if (date != null) {
Predicate dateFilter = createChangeDateFilter(cb, eventPersonsSelect, DateHelper.toTimestampUpper(date), lastSynchronizedUuid);
if (batchSize == null) {
Predicate eventParticipantDateFilter = eventParticipantService.createChangeDateFilter(cb, eventPersonsRoot, DateHelper.toTimestampUpper(date));
dateFilter = cb.or(dateFilter, eventParticipantDateFilter);
}
eventPersonsFilter = and(cb, eventPersonsFilter, dateFilter);
}
if (eventPersonsFilter != null) {
eventPersonsQuery.where(eventPersonsFilter);
}
eventPersonsQuery.distinct(true);
List<Person> eventPersonsResultList = getBatchedQueryResults(cb, eventPersonsQuery, eventPersonsSelect, batchSize);
// persons by immunization
List<Person> immunizationPersonsResultList = new ArrayList<>();
if (!featureConfigurationFacade.isPropertyValueTrue(FeatureType.IMMUNIZATION_MANAGEMENT, FeatureTypeProperty.REDUCED)) {
CriteriaQuery<Person> immunizationPersonsQuery = cb.createQuery(Person.class);
Root<Immunization> immunizationPersonsRoot = immunizationPersonsQuery.from(Immunization.class);
Join<Immunization, Person> immunizationPersonsSelect = immunizationPersonsRoot.join(Immunization.PERSON);
immunizationPersonsSelect.fetch(Person.ADDRESS);
immunizationPersonsQuery.select(immunizationPersonsSelect);
Predicate immunizationPersonsFilter = immunizationService.createUserFilter(new ImmunizationQueryContext(cb, immunizationPersonsQuery, new ImmunizationJoins(immunizationPersonsRoot)));
// date range
if (date != null) {
Predicate dateFilter = createChangeDateFilter(cb, immunizationPersonsSelect, DateHelper.toTimestampUpper(date), lastSynchronizedUuid);
if (batchSize == null) {
Predicate immunizationDateFilter = immunizationService.createChangeDateFilter(cb, immunizationPersonsRoot, DateHelper.toTimestampUpper(date));
dateFilter = cb.or(dateFilter, immunizationDateFilter);
}
immunizationPersonsFilter = and(cb, immunizationPersonsFilter, dateFilter);
}
if (immunizationPersonsFilter != null) {
immunizationPersonsQuery.where(immunizationPersonsFilter);
}
immunizationPersonsQuery.distinct(true);
immunizationPersonsResultList = getBatchedQueryResults(cb, immunizationPersonsQuery, immunizationPersonsSelect, batchSize);
}
List<Person> travelEntryPersonsResultList = new ArrayList<>();
// if a batch size is given, this is a sync from the mobile app where travel entries are not relevant for now
if (batchSize == null) {
// persons by travel entries
CriteriaQuery<Person> tepQuery = cb.createQuery(Person.class);
Root<TravelEntry> tepRoot = tepQuery.from(TravelEntry.class);
Join<TravelEntry, Person> tepSelect = tepRoot.join(TravelEntry.PERSON);
tepSelect.fetch(Person.ADDRESS);
tepQuery.select(tepSelect);
Predicate tepFilter = travelEntryService.createUserFilter(new TravelEntryQueryContext(cb, tepQuery, new TravelEntryJoins(tepRoot)));
// date range
if (date != null) {
Predicate dateFilter = createChangeDateFilter(cb, tepSelect, DateHelper.toTimestampUpper(date));
Predicate travelEntryDateFilter = travelEntryService.createChangeDateFilter(cb, tepRoot, DateHelper.toTimestampUpper(date));
tepFilter = and(cb, tepFilter, cb.or(dateFilter, travelEntryDateFilter));
}
if (tepFilter != null) {
tepQuery.where(tepFilter);
}
tepQuery.distinct(true);
travelEntryPersonsResultList = em.createQuery(tepQuery).getResultList();
}
return Stream.of(districtResultList, casePersonsResultList, contactPersonsResultList, eventPersonsResultList, immunizationPersonsResultList, travelEntryPersonsResultList).flatMap(List<Person>::stream).distinct().sorted(new ChangeDateUuidComparator<>()).limit(batchSize == null ? Long.MAX_VALUE : batchSize).collect(Collectors.toList());
}
use of de.symeda.sormas.backend.immunization.ImmunizationJoins in project SORMAS-Project by hzi-braunschweig.
the class PersonService method getAllUuids.
@Override
public List<String> getAllUuids() {
CriteriaBuilder cb = em.getCriteriaBuilder();
// persons by LGA
CriteriaQuery<String> lgaQuery = cb.createQuery(String.class);
Root<Person> lgaRoot = lgaQuery.from(Person.class);
Join<Person, Location> address = lgaRoot.join(Person.ADDRESS);
lgaQuery.select(lgaRoot.get(Person.UUID));
Predicate lgaFilter = cb.equal(address.get(Location.DISTRICT), getCurrentUser().getDistrict());
lgaQuery.where(lgaFilter);
List<String> lgaResultList = em.createQuery(lgaQuery).getResultList();
// persons by case
CriteriaQuery<String> casePersonsQuery = cb.createQuery(String.class);
Root<Case> casePersonsRoot = casePersonsQuery.from(Case.class);
Join<Person, Person> casePersonsSelect = casePersonsRoot.join(Case.PERSON);
casePersonsQuery.select(casePersonsSelect.get(Person.UUID));
Predicate casePersonsFilter = caseService.createUserFilter(new CaseQueryContext(cb, casePersonsQuery, new CaseJoins(casePersonsRoot)));
if (casePersonsFilter != null) {
casePersonsQuery.where(casePersonsFilter);
}
casePersonsQuery.distinct(true);
List<String> casePersonsResultList = em.createQuery(casePersonsQuery).getResultList();
// persons by contact
CriteriaQuery<String> contactPersonsQuery = cb.createQuery(String.class);
Root<Contact> contactPersonsRoot = contactPersonsQuery.from(Contact.class);
Join<Person, Person> contactPersonsSelect = contactPersonsRoot.join(Contact.PERSON);
contactPersonsQuery.select(contactPersonsSelect.get(Person.UUID));
Predicate contactPersonsFilter = contactService.createUserFilter(new ContactQueryContext(cb, contactPersonsQuery, new ContactJoins(contactPersonsRoot)));
if (contactPersonsFilter != null) {
contactPersonsQuery.where(contactPersonsFilter);
}
contactPersonsQuery.distinct(true);
List<String> contactPersonsResultList = em.createQuery(contactPersonsQuery).getResultList();
// persons by event participant
CriteriaQuery<String> eventPersonsQuery = cb.createQuery(String.class);
Root<EventParticipant> eventPersonsRoot = eventPersonsQuery.from(EventParticipant.class);
Join<Person, Person> eventPersonsSelect = eventPersonsRoot.join(EventParticipant.PERSON);
eventPersonsQuery.select(eventPersonsSelect.get(Person.UUID));
Predicate eventPersonsFilter = eventParticipantService.createUserFilter(new EventParticipantQueryContext(cb, eventPersonsQuery, new EventParticipantJoins(eventPersonsRoot)));
if (eventPersonsFilter != null) {
eventPersonsQuery.where(eventPersonsFilter);
}
eventPersonsQuery.distinct(true);
List<String> eventPersonsResultList = em.createQuery(eventPersonsQuery).getResultList();
// persons by immunization
List<String> immunizationPersonsResultList = new ArrayList<>();
if (!featureConfigurationFacade.isPropertyValueTrue(FeatureType.IMMUNIZATION_MANAGEMENT, FeatureTypeProperty.REDUCED)) {
CriteriaQuery<String> immunizationPersonsQuery = cb.createQuery(String.class);
Root<Immunization> immunizationPersonsRoot = immunizationPersonsQuery.from(Immunization.class);
Join<Immunization, Person> immunizationPersonsSelect = immunizationPersonsRoot.join(Immunization.PERSON);
immunizationPersonsQuery.select(immunizationPersonsSelect.get(Person.UUID));
Predicate immunizationPersonsFilter = immunizationService.createUserFilter(new ImmunizationQueryContext(cb, immunizationPersonsQuery, new ImmunizationJoins(immunizationPersonsRoot)));
if (immunizationPersonsFilter != null) {
immunizationPersonsQuery.where(immunizationPersonsFilter);
}
immunizationPersonsQuery.distinct(true);
immunizationPersonsResultList = em.createQuery(immunizationPersonsQuery).getResultList();
}
// persons by travel entry
CriteriaQuery<String> travelEntryPersonsQuery = cb.createQuery(String.class);
Root<TravelEntry> travelEntryPersonsRoot = travelEntryPersonsQuery.from(TravelEntry.class);
Join<TravelEntry, Person> travelEntryPersonsSelect = travelEntryPersonsRoot.join(TravelEntry.PERSON);
travelEntryPersonsQuery.select(travelEntryPersonsSelect.get(Person.UUID));
Predicate travelEntryPersonsFilter = travelEntryService.createUserFilter(new TravelEntryQueryContext(cb, travelEntryPersonsQuery, new TravelEntryJoins(travelEntryPersonsRoot)));
if (travelEntryPersonsFilter != null) {
travelEntryPersonsQuery.where(travelEntryPersonsFilter);
}
travelEntryPersonsQuery.distinct(true);
List<String> travelEntryPersonsResultList = em.createQuery(travelEntryPersonsQuery).getResultList();
return Stream.of(lgaResultList, casePersonsResultList, contactPersonsResultList, eventPersonsResultList, immunizationPersonsResultList, travelEntryPersonsResultList).flatMap(List<String>::stream).distinct().collect(Collectors.toList());
}
Aggregations