public VaccinationSchedule get()

in use-cases/vaccination-scheduling/src/main/java/org/acme/vaccinationscheduler/rest/VaccinationScheduleSolverResource.java [41:104]


    public VaccinationSchedule get(@QueryParam("page") Integer page) {
        // Get the solver status before loading the schedule
        // to avoid the race condition that the solver terminates between them
        SolverStatus solverStatus = getSolverStatus();
        VaccinationSchedule schedule = vaccinationScheduleRepository.find();
        schedule.setSolverStatus(solverStatus);
        // Optional pagination because the UI can't handle huge datasets
        if (page != null) {
            if (page < 0) {
                throw new IllegalArgumentException("Unsupported page (" + page + ").");
            }
            int appointmentListSize = schedule.getAppointmentList().size();
            if (appointmentListSize > APPOINTMENT_PAGE_LIMIT) {
                List<VaccineType> vaccineTypeList = schedule.getVaccineTypeList();
                List<VaccinationCenter> vaccinationCenterList = schedule.getVaccinationCenterList();
                List<Appointment> appointmentList;
                List<Person> personList;
                if (appointmentListSize <= APPOINTMENT_PAGE_LIMIT) {
                    appointmentList = schedule.getAppointmentList();
                    personList = schedule.getPersonList();
                } else {
                    Map<VaccinationCenter, Set<String>> boothIdSetMap = new HashMap<>(vaccinationCenterList.size());
                    for (VaccinationCenter vaccinationCenter : vaccinationCenterList) {
                        boothIdSetMap.put(vaccinationCenter, new LinkedHashSet<>());
                    }
                    for (Appointment appointment : schedule.getAppointmentList()) {
                        Set<String> boothIdSet = boothIdSetMap.get(appointment.getVaccinationCenter());
                        boothIdSet.add(appointment.getBoothId());
                    }
                    Map<VaccinationCenter, Set<String>> subBoothIdSetMap = new HashMap<>(vaccinationCenterList.size());
                    boothIdSetMap.forEach((vaccinationCenter, boothIdSet) -> {
                        List<String> boothIdList = new ArrayList<>(boothIdSet);
                        int pageLength = Math.max(1, boothIdList.size() * APPOINTMENT_PAGE_LIMIT / appointmentListSize);
                        subBoothIdSetMap.put(vaccinationCenter, new HashSet<>(
                                // For a page, filter the number of booths per page from each vaccination center
                                boothIdList.subList(page * pageLength,
                                        Math.min(boothIdList.size(), (page + 1) * pageLength))));
                    });
                    appointmentList = schedule.getAppointmentList().stream()
                            .filter(appointment -> subBoothIdSetMap.get(appointment.getVaccinationCenter())
                                    .contains(appointment.getBoothId()))
                            .collect(Collectors.toList());
                    personList = schedule.getPersonList().stream()
                            .filter(person -> person.getAppointment() != null
                                    && subBoothIdSetMap.get(person.getAppointment().getVaccinationCenter())
                                    .contains(person.getAppointment().getBoothId()))
                            .collect(Collectors.toList());

                    List<Person> unassignedPersonList = personList.stream()
                            .filter(person -> person.getAppointment() == null)
                            .collect(Collectors.toList());
                    int pageLength = unassignedPersonList.size() * APPOINTMENT_PAGE_LIMIT / appointmentListSize;
                    personList.addAll(unassignedPersonList.subList(page * pageLength,
                            Math.min(unassignedPersonList.size(), (page + 1) * pageLength)));
                }
                VaccinationSchedule pagedSchedule = new VaccinationSchedule(
                        vaccineTypeList, vaccinationCenterList, appointmentList, personList);
                pagedSchedule.setScore(schedule.getScore());
                pagedSchedule.setSolverStatus(schedule.getSolverStatus());
                return pagedSchedule;
            }
        }
        return schedule;
    }