use of de.symeda.sormas.api.VisitOrigin in project SORMAS-Project by hzi-braunschweig.
the class ContactFacadeEjb method getContactFollowUpList.
@Override
public List<ContactFollowUpDto> getContactFollowUpList(ContactCriteria contactCriteria, Date referenceDate, int interval, Integer first, Integer max, List<SortProperty> sortProperties) {
Date end = DateHelper.getEndOfDay(referenceDate);
Date start = DateHelper.getStartOfDay(DateHelper.subtractDays(end, interval));
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<ContactFollowUpDto> cq = cb.createQuery(ContactFollowUpDto.class);
Root<Contact> contact = cq.from(Contact.class);
final ContactQueryContext contactQueryContext = new ContactQueryContext(cb, cq, contact);
final ContactJoins<Contact> joins = (ContactJoins<Contact>) contactQueryContext.getJoins();
cq.multiselect(contact.get(Contact.UUID), contact.get(Contact.CHANGE_DATE), joins.getPerson().get(Person.FIRST_NAME), joins.getPerson().get(Person.LAST_NAME), joins.getContactOfficer().get(User.UUID), joins.getContactOfficer().get(User.FIRST_NAME), joins.getContactOfficer().get(User.LAST_NAME), contact.get(Contact.LAST_CONTACT_DATE), contact.get(Contact.REPORT_DATE_TIME), contact.get(Contact.FOLLOW_UP_UNTIL), joins.getPerson().get(Person.SYMPTOM_JOURNAL_STATUS), contact.get(Contact.DISEASE), jurisdictionSelector(contactQueryContext));
// Only use user filter if no restricting case is specified
Predicate filter = listCriteriaBuilder.buildContactFilter(contactCriteria, contactQueryContext);
if (filter != null) {
cq.where(filter);
}
cq.distinct(true);
if (sortProperties != null && sortProperties.size() > 0) {
List<Order> order = new ArrayList<Order>(sortProperties.size());
for (SortProperty sortProperty : sortProperties) {
Expression<?> expression;
switch(sortProperty.propertyName) {
case FollowUpDto.UUID:
case ContactFollowUpDto.LAST_CONTACT_DATE:
case FollowUpDto.FOLLOW_UP_UNTIL:
expression = contact.get(sortProperty.propertyName);
break;
case FollowUpDto.REPORT_DATE:
expression = contact.get(Contact.REPORT_DATE_TIME);
break;
case FollowUpDto.SYMPTOM_JOURNAL_STATUS:
expression = joins.getPerson().get(Person.SYMPTOM_JOURNAL_STATUS);
break;
case FollowUpDto.FIRST_NAME:
expression = joins.getPerson().get(Person.FIRST_NAME);
break;
case FollowUpDto.LAST_NAME:
expression = joins.getPerson().get(Person.LAST_NAME);
break;
case ContactFollowUpDto.CONTACT_OFFICER:
expression = joins.getContactOfficer().get(User.FIRST_NAME);
order.add(sortProperty.ascending ? cb.asc(expression) : cb.desc(expression));
expression = joins.getContactOfficer().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(contact.get(Contact.CHANGE_DATE)));
}
List<ContactFollowUpDto> resultList = QueryHelper.getResultList(em, cq, first, max);
if (!resultList.isEmpty()) {
List<String> contactUuids = resultList.stream().map(d -> d.getUuid()).collect(Collectors.toList());
CriteriaQuery<Object[]> visitsCq = cb.createQuery(Object[].class);
Root<Contact> visitsCqRoot = visitsCq.from(Contact.class);
Join<Contact, Visit> visitsJoin = visitsCqRoot.join(Contact.VISITS, JoinType.LEFT);
Join<Visit, Symptoms> visitSymptomsJoin = visitsJoin.join(Visit.SYMPTOMS, JoinType.LEFT);
visitsCq.where(CriteriaBuilderHelper.and(cb, contact.get(AbstractDomainObject.UUID).in(contactUuids), cb.isNotEmpty(visitsCqRoot.get(Contact.VISITS)), cb.between(visitsJoin.get(Visit.VISIT_DATE_TIME), start, end)));
visitsCq.multiselect(visitsCqRoot.get(Contact.UUID), visitsJoin.get(Visit.VISIT_DATE_TIME), visitsJoin.get(Visit.VISIT_STATUS), visitsJoin.get(Visit.ORIGIN), visitSymptomsJoin.get(Symptoms.SYMPTOMATIC));
visitsCq.orderBy(cb.asc(visitsJoin.get(Visit.VISIT_DATE_TIME)), cb.asc(visitsJoin.get(Visit.CREATION_DATE)));
List<Object[]> visits = em.createQuery(visitsCq).getResultList();
Map<String, ContactFollowUpDto> resultMap = resultList.stream().collect(Collectors.toMap(ContactFollowUpDto::getUuid, Function.identity()));
Pseudonymizer pseudonymizer = Pseudonymizer.getDefault(userService::hasRight, I18nProperties.getCaption(Captions.inaccessibleValue));
for (ContactFollowUpDto contactFollowUpDto : resultMap.values()) {
contactFollowUpDto.initVisitSize(interval + 1);
boolean isInJurisdiction = contactFollowUpDto.getInJurisdiction();
pseudonymizer.pseudonymizeDto(ContactFollowUpDto.class, contactFollowUpDto, isInJurisdiction, null);
}
for (Object[] v : visits) {
int day = DateHelper.getDaysBetween(start, (Date) v[1]);
VisitResultDto result = getVisitResult((VisitStatus) v[2], (VisitOrigin) v[3], (Boolean) v[4]);
resultMap.get(v[0]).getVisitResults()[day - 1] = result;
}
}
return resultList;
}
Aggregations