Search in sources :

Example 1 with DateOrListParam

use of ca.uhn.fhir.rest.param.DateOrListParam in project gpconnect-demonstrator by nhsconnect.

the class AppointmentResourceProvider method getAppointmentsForPatientIdAndDates.

@Search
public List<Appointment> getAppointmentsForPatientIdAndDates(@RequiredParam(name = "patient") IdType patientLocalId, @Sort SortSpec sort, @Count Integer count, @RequiredParam(name = "start") DateAndListParam startDates) {
    Date startLowerDate = null;
    Date startUpperDate = null;
    List<DateOrListParam> startDateList = startDates.getValuesAsQueryTokens();
    if (startDateList.size() != 2) {
        throw OperationOutcomeFactory.buildOperationOutcomeException(new UnprocessableEntityException("Appointment search must have both 'le' and 'ge' start date parameters."), SystemCode.INVALID_PARAMETER, IssueType.INVALID);
    }
    Pattern dateOnlyPattern = Pattern.compile("[0-9]{4}(-(0[1-9]|1[0-2])(-(0[0-9]|[1-2][0-9]|3[0-1])))");
    boolean lePrefix = false;
    boolean gePrefix = false;
    for (DateOrListParam filter : startDateList) {
        DateParam token = filter.getValuesAsQueryTokens().get(0);
        if (!dateOnlyPattern.matcher(token.getValueAsString()).matches()) {
            throw OperationOutcomeFactory.buildOperationOutcomeException(new UnprocessableEntityException("Search dates must be of the format: yyyy-MM-dd and should not include time or timezone."), SystemCode.INVALID_PARAMETER, IssueType.INVALID);
        }
        if (token.getPrefix() == ParamPrefixEnum.GREATERTHAN_OR_EQUALS) {
            startLowerDate = token.getValue();
            gePrefix = true;
        } else if (token.getPrefix() == ParamPrefixEnum.LESSTHAN_OR_EQUALS) {
            Calendar upper = Calendar.getInstance();
            upper.setTime(token.getValue());
            upper.add(Calendar.HOUR, 23);
            upper.add(Calendar.MINUTE, 59);
            upper.add(Calendar.SECOND, 59);
            startUpperDate = upper.getTime();
            lePrefix = true;
        } else {
            throw OperationOutcomeFactory.buildOperationOutcomeException(new UnprocessableEntityException("Unknown prefix on start date parameter: only le and ge prefixes are allowed."), SystemCode.INVALID_PARAMETER, IssueType.INVALID);
        }
    }
    if (!gePrefix || !lePrefix) {
        throw OperationOutcomeFactory.buildOperationOutcomeException(new UnprocessableEntityException("Parameters must contain two start parameters, one with prefix 'ge' and one with prefix 'le'."), SystemCode.INVALID_PARAMETER, IssueType.INVALID);
    } else if (startLowerDate.before(getYesterday())) {
        throw OperationOutcomeFactory.buildOperationOutcomeException(new UnprocessableEntityException("Search dates from the past: " + startLowerDate.toString() + " are not allowed in appointment search."), SystemCode.INVALID_PARAMETER, IssueType.INVALID);
    } else if (startUpperDate.before(getYesterday())) {
        throw OperationOutcomeFactory.buildOperationOutcomeException(new UnprocessableEntityException("Search dates from the past: " + startUpperDate.toString() + " are not allowed in appointment search."), SystemCode.INVALID_PARAMETER, IssueType.INVALID);
    } else if (startUpperDate.before(startLowerDate)) {
        throw OperationOutcomeFactory.buildOperationOutcomeException(new UnprocessableEntityException("Upper search date must be after the lower search date."), SystemCode.INVALID_PARAMETER, IssueType.INVALID);
    }
    List<Appointment> appointment = appointmentSearch.searchAppointments(patientLocalId.getIdPartAsLong(), startLowerDate, startUpperDate).stream().map(this::appointmentDetailToAppointmentResourceConverter).collect(Collectors.toList());
    List<Appointment> futureAppointments = appointmentValidation.filterToReturnFutureAppointments(appointment);
    if (futureAppointments.isEmpty()) {
        return null;
    }
    // Update startIndex if we do paging
    return count != null ? futureAppointments.subList(0, count) : futureAppointments;
}
Also used : DateOrListParam(ca.uhn.fhir.rest.param.DateOrListParam) Appointment(org.hl7.fhir.dstu3.model.Appointment) UnprocessableEntityException(ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException) Pattern(java.util.regex.Pattern) Calendar(java.util.Calendar) Date(java.util.Date) DateParam(ca.uhn.fhir.rest.param.DateParam) AppointmentSearch(uk.gov.hscic.appointment.appointment.AppointmentSearch) Search(ca.uhn.fhir.rest.annotation.Search) SlotSearch(uk.gov.hscic.appointment.slot.SlotSearch)

Aggregations

Search (ca.uhn.fhir.rest.annotation.Search)1 DateOrListParam (ca.uhn.fhir.rest.param.DateOrListParam)1 DateParam (ca.uhn.fhir.rest.param.DateParam)1 UnprocessableEntityException (ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException)1 Calendar (java.util.Calendar)1 Date (java.util.Date)1 Pattern (java.util.regex.Pattern)1 Appointment (org.hl7.fhir.dstu3.model.Appointment)1 AppointmentSearch (uk.gov.hscic.appointment.appointment.AppointmentSearch)1 SlotSearch (uk.gov.hscic.appointment.slot.SlotSearch)1