Search in sources :

Example 1 with Practitioner

use of org.hl7.fhir.r4.model.Practitioner in project gpconnect-demonstrator by nhsconnect.

the class MedicationAdministrationResourceProvider method getMedicationAdministrationsForPatientId.

@Search
public List<MedicationAdministration> getMedicationAdministrationsForPatientId(@RequiredParam(name = "patient") String patientId) {
    ArrayList<MedicationAdministration> medicationAdministrations = new ArrayList<>();
    List<MedicationAdministrationDetail> medicationAdministrationDetailList = medicationAdministrationSearch.findMedicationAdministrationForPatient(Long.parseLong(patientId));
    if (medicationAdministrationDetailList != null && !medicationAdministrationDetailList.isEmpty()) {
        for (MedicationAdministrationDetail medicationAdministrationDetail : medicationAdministrationDetailList) {
            MedicationAdministration medicationAdministration = new MedicationAdministration();
            String resourceId = String.valueOf(medicationAdministrationDetail.getId());
            String versionId = String.valueOf(medicationAdministrationDetail.getLastUpdated().getTime());
            String resourceType = medicationAdministration.getResourceType().toString();
            IdType id = new IdType(resourceType, resourceId, versionId);
            medicationAdministration.setId(id);
            medicationAdministration.getMeta().setVersionId(versionId);
            medicationAdministration.getMeta().setLastUpdated(medicationAdministrationDetail.getLastUpdated());
            medicationAdministration.addDefinition(new Reference("Patient/" + medicationAdministrationDetail.getPatientId()));
            medicationAdministration.addDefinition(new Reference("Practitioner/" + medicationAdministrationDetail.getPractitionerId()));
            medicationAdministration.setPrescription(new Reference("MedicationOrder/" + medicationAdministrationDetail.getPrescriptionId()));
            medicationAdministration.setEffective(new DateType(medicationAdministrationDetail.getAdministrationDate()));
            medicationAdministration.setMedication(new Reference("Medication/" + medicationAdministrationDetail.getMedicationId()));
            medicationAdministrations.add(medicationAdministration);
        }
    }
    return medicationAdministrations;
}
Also used : MedicationAdministrationDetail(uk.gov.hscic.model.medication.MedicationAdministrationDetail) Reference(org.hl7.fhir.dstu3.model.Reference) ArrayList(java.util.ArrayList) MedicationAdministration(org.hl7.fhir.dstu3.model.MedicationAdministration) DateType(org.hl7.fhir.dstu3.model.DateType) IdType(org.hl7.fhir.dstu3.model.IdType) Search(ca.uhn.fhir.rest.annotation.Search) MedicationAdministrationSearch(uk.gov.hscic.medication.administration.MedicationAdministrationSearch)

Example 2 with Practitioner

use of org.hl7.fhir.r4.model.Practitioner in project gpconnect-demonstrator by nhsconnect.

the class MedicationOrderResourceProvider method medicationOrderDetailsToMedicationOrderResourceConverter.

private MedicationRequest medicationOrderDetailsToMedicationOrderResourceConverter(MedicationOrderDetails medicationOrderDetails) {
    MedicationRequest medicationOrder = new MedicationRequest();
    String resourceId = String.valueOf(medicationOrderDetails.getId());
    String versionId = String.valueOf(medicationOrderDetails.getLastUpdated().getTime());
    String resourceType = medicationOrder.getResourceType().toString();
    IdType id = new IdType(resourceType, resourceId, versionId);
    medicationOrder.setId(id);
    medicationOrder.getMeta().setVersionId(versionId);
    medicationOrder.getMeta().setLastUpdated(medicationOrderDetails.getLastUpdated());
    switch(medicationOrderDetails.getOrderStatus().toLowerCase(Locale.UK)) {
        case "active":
            medicationOrder.setStatus(MedicationRequestStatus.ACTIVE);
            break;
        case "completed":
            medicationOrder.setStatus(MedicationRequestStatus.COMPLETED);
            break;
        case "draft":
            medicationOrder.setStatus(MedicationRequestStatus.DRAFT);
            break;
        case "entered_in_error":
            medicationOrder.setStatus(MedicationRequestStatus.ENTEREDINERROR);
            break;
        case "on_hold":
            medicationOrder.setStatus(MedicationRequestStatus.ONHOLD);
            break;
        case "stopped":
            medicationOrder.setStatus(MedicationRequestStatus.STOPPED);
            break;
    }
    if (medicationOrderDetails.getPatientId() != null) {
        medicationOrder.setSubject(new Reference("Patient/" + medicationOrderDetails.getPatientId()));
    } else {
        medicationOrder.setSubject(new Reference());
    }
    medicationOrder.setRecorder(new Reference("Practitioner/" + medicationOrderDetails.getAutherId()));
    medicationOrder.setMedication(new Reference("Medication/" + medicationOrderDetails.getMedicationId()));
    medicationOrder.addDosageInstruction().setText(medicationOrderDetails.getDosageText());
    MedicationRequestDispenseRequestComponent dispenseRequest = new MedicationRequestDispenseRequestComponent();
    dispenseRequest.addExtension(new Extension(SystemURL.SD_EXTENSION_MEDICATION_QUANTITY_TEXT, new StringDt(medicationOrderDetails.getDispenseQuantityText())));
    dispenseRequest.addExtension(new Extension(SystemURL.SD_EXTENSION_PERSCRIPTION_REPEAT_REVIEW_DATE, new DateTimeDt(medicationOrderDetails.getDispenseReviewDate())));
    dispenseRequest.setId("Medication/" + medicationOrderDetails.getDispenseMedicationId());
    dispenseRequest.setNumberOfRepeatsAllowed(medicationOrderDetails.getDispenseRepeatsAllowed());
    medicationOrder.setDispenseRequest(dispenseRequest);
    return medicationOrder;
}
Also used : DateTimeDt(ca.uhn.fhir.model.primitive.DateTimeDt) MedicationRequestDispenseRequestComponent(org.hl7.fhir.dstu3.model.MedicationRequest.MedicationRequestDispenseRequestComponent) StringDt(ca.uhn.fhir.model.primitive.StringDt)

Example 3 with Practitioner

use of org.hl7.fhir.r4.model.Practitioner in project gpconnect-demonstrator by nhsconnect.

the class PractitionerResourceProvider method practitionerDetailsToPractitionerResourceConverter.

private Practitioner practitionerDetailsToPractitionerResourceConverter(PractitionerDetails practitionerDetails) {
    Identifier identifier = new Identifier().setSystem(SystemURL.ID_SDS_USER_ID).setValue(practitionerDetails.getUserId());
    Practitioner practitioner = new Practitioner().addIdentifier(identifier);
    practitionerDetails.getRoleIds().stream().distinct().map(roleId -> new Identifier().setSystem(SystemURL.ID_SDS_ROLE_PROFILE_ID).setValue(roleId)).forEach(practitioner::addIdentifier);
    String resourceId = String.valueOf(practitionerDetails.getId());
    String versionId = String.valueOf(practitionerDetails.getLastUpdated().getTime());
    String resourceType = practitioner.getResourceType().toString();
    IdType id = new IdType(resourceType, resourceId, versionId);
    practitioner.setId(id);
    practitioner.getMeta().setVersionId(versionId);
    practitioner.getMeta().setLastUpdated(practitionerDetails.getLastUpdated());
    practitioner.getMeta().addProfile(SystemURL.SD_GPC_PRACTITIONER);
    HumanName name = new HumanName().setFamily(practitionerDetails.getNameFamily()).addGiven(practitionerDetails.getNameGiven()).addPrefix(practitionerDetails.getNamePrefix()).setUse(NameUse.USUAL);
    practitioner.addName(name);
    switch(practitionerDetails.getGender().toLowerCase(Locale.UK)) {
        case "male":
            practitioner.setGender(AdministrativeGender.MALE);
            break;
        case "female":
            practitioner.setGender(AdministrativeGender.FEMALE);
            break;
        case "other":
            practitioner.setGender(AdministrativeGender.OTHER);
            break;
        default:
            practitioner.setGender(AdministrativeGender.UNKNOWN);
            break;
    }
    Coding roleCoding = new Coding(SystemURL.VS_SDS_JOB_ROLE_NAME, practitionerDetails.getRoleCode(), practitionerDetails.getRoleDisplay());
    for (int i = 0; i < practitionerDetails.getComCode().size(); i++) {
        Coding comCoding = new Coding(SystemURL.VS_HUMAN_LANGUAGE, practitionerDetails.getComCode().get(i), null).setDisplay(practitionerDetails.getComDisplay().get(i));
        practitioner.addCommunication().addCoding(comCoding);
    }
    return practitioner;
}
Also used : Practitioner(org.hl7.fhir.dstu3.model.Practitioner) IdParam(ca.uhn.fhir.rest.annotation.IdParam) Identifier(org.hl7.fhir.dstu3.model.Identifier) Coding(org.hl7.fhir.dstu3.model.Coding) IdType(org.hl7.fhir.dstu3.model.IdType) Autowired(org.springframework.beans.factory.annotation.Autowired) CodeableConcept(org.hl7.fhir.dstu3.model.CodeableConcept) Extension(org.hl7.fhir.dstu3.model.Extension) RequiredParam(ca.uhn.fhir.rest.annotation.RequiredParam) Locale(java.util.Locale) Search(ca.uhn.fhir.rest.annotation.Search) IResourceProvider(ca.uhn.fhir.rest.server.IResourceProvider) ResourceNotFoundException(ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException) Location(org.hl7.fhir.dstu3.model.Location) Read(ca.uhn.fhir.rest.annotation.Read) Practitioner(org.hl7.fhir.dstu3.model.Practitioner) Sort(ca.uhn.fhir.rest.annotation.Sort) Count(ca.uhn.fhir.rest.annotation.Count) Collectors(java.util.stream.Collectors) SystemURL(uk.gov.hscic.SystemURL) IssueType(org.hl7.fhir.dstu3.model.OperationOutcome.IssueType) TokenParam(ca.uhn.fhir.rest.param.TokenParam) PractitionerDetails(uk.gov.hscic.model.practitioner.PractitionerDetails) AdministrativeGender(org.hl7.fhir.dstu3.model.Enumerations.AdministrativeGender) OperationOutcomeFactory(uk.gov.hscic.OperationOutcomeFactory) List(java.util.List) Component(org.springframework.stereotype.Component) SortSpec(ca.uhn.fhir.rest.api.SortSpec) IdentifierValidator(uk.gov.hscic.common.validators.IdentifierValidator) SystemCode(uk.gov.hscic.SystemCode) Collections(java.util.Collections) HumanName(org.hl7.fhir.dstu3.model.HumanName) NameUse(org.hl7.fhir.dstu3.model.HumanName.NameUse) HumanName(org.hl7.fhir.dstu3.model.HumanName) Identifier(org.hl7.fhir.dstu3.model.Identifier) Coding(org.hl7.fhir.dstu3.model.Coding) IdType(org.hl7.fhir.dstu3.model.IdType)

Example 4 with Practitioner

use of org.hl7.fhir.r4.model.Practitioner in project gpconnect-demonstrator by nhsconnect.

the class AppointmentResourceProvider method appointmentResourceConverterToAppointmentDetail.

/**
 * fhir resource Appointment to AppointmentDetail
 *
 * @param appointment Resource
 * @return populated AppointmentDetail
 */
private AppointmentDetail appointmentResourceConverterToAppointmentDetail(Appointment appointment) {
    appointmentValidation.validateAppointmentExtensions(appointment.getExtension());
    if (appointmentValidation.appointmentDescriptionTooLong(appointment)) {
        throwUnprocessableEntity422_InvalidResourceException("Appointment description cannot be greater then " + APPOINTMENT_DESCRIPTION_LENGTH + " characters");
    }
    AppointmentDetail appointmentDetail = new AppointmentDetail();
    Long id = appointment.getIdElement().getIdPartAsLong();
    appointmentDetail.setId(id);
    appointmentDetail.setLastUpdated(getLastUpdated(appointment.getMeta().getLastUpdated()));
    List<Extension> cnlExtension = appointment.getExtensionsByUrl(SystemURL.SD_EXTENSION_GPC_APPOINTMENT_CANCELLATION_REASON);
    if (cnlExtension != null && !cnlExtension.isEmpty()) {
        IBaseDatatype value = cnlExtension.get(0).getValue();
        if (null == value) {
            throwInvalidRequest400_BadRequestException("Cancellation reason missing.");
        }
        appointmentDetail.setCancellationReason(value.toString());
    }
    appointmentDetail.setStatus(appointment.getStatus().toString().toLowerCase(Locale.UK));
    appointmentDetail.setMinutesDuration(appointment.getMinutesDuration());
    appointmentDetail.setPriority(appointment.getPriority());
    appointmentDetail.setStartDateTime(appointment.getStart());
    appointmentDetail.setEndDateTime(appointment.getEnd());
    List<Long> slotIds = new ArrayList<>();
    for (Reference slotReference : appointment.getSlot()) {
        // #200 check slots for absolute references
        checkReferenceIsRelative(slotReference.getReference());
        try {
            String here = slotReference.getReference().substring("Slot/".length());
            Long slotId = new Long(here);
            slotIds.add(slotId);
        } catch (NumberFormatException ex) {
            throwUnprocessableEntity422_InvalidResourceException(String.format("The slot reference value data type for %s is not valid.", slotReference.getReference()));
        }
    }
    appointmentDetail.setSlotIds(slotIds);
    if (appointmentValidation.appointmentCommentTooLong(appointment)) {
        throwUnprocessableEntity422_InvalidResourceException("Appointment comment cannot be greater than " + APPOINTMENT_COMMENT_LENGTH + " characters");
    }
    appointmentDetail.setComment(appointment.getComment());
    appointmentDetail.setDescription(appointment.getDescription());
    for (AppointmentParticipantComponent participant : appointment.getParticipant()) {
        if (participant.getActor() != null) {
            String participantReference = participant.getActor().getReference();
            String actorIdString = participantReference.substring(participantReference.lastIndexOf("/") + 1);
            Long actorId;
            if (actorIdString.equals("null")) {
                actorId = null;
            } else {
                actorId = Long.valueOf(actorIdString);
            }
            if (participantReference.contains("Patient/")) {
                appointmentDetail.setPatientId(actorId);
            } else if (participantReference.contains("Practitioner/")) {
                appointmentDetail.setPractitionerId(actorId);
            } else if (participantReference.contains("Location/")) {
                appointmentDetail.setLocationId(actorId);
            }
        } else {
            throwUnprocessableEntity422_InvalidResourceException("Participant Actor cannot be null");
        }
    }
    if (appointment.getCreated() != null) {
        Date created = appointment.getCreated();
        appointmentDetail.setCreated(created);
    }
    // #200 check extensions for absolute references
    List<Extension> extensions = appointment.getExtension();
    for (Extension extension : extensions) {
        try {
            Reference reference = (Reference) extension.getValue();
            if (reference != null) {
                checkReferenceIsRelative(reference.getReference());
            }
        } catch (ClassCastException ex) {
        }
    }
    List<Resource> contained = appointment.getContained();
    if (contained != null && !contained.isEmpty()) {
        Resource org = contained.get(0);
        Organization bookingOrgRes = (Organization) org;
        BookingOrgDetail bookingOrgDetail = new BookingOrgDetail();
        appointmentValidation.validateBookingOrganizationValuesArePresent(bookingOrgRes);
        bookingOrgDetail.setName(bookingOrgRes.getName());
        // #198 add system and optional use type. Pick system phone if > 1 and available otherwise use the one supplied
        Iterator<ContactPoint> iter = bookingOrgRes.getTelecom().iterator();
        ContactPoint telecom = bookingOrgRes.getTelecomFirstRep();
        while (iter.hasNext()) {
            ContactPoint thisTelecom = iter.next();
            if (thisTelecom.getSystem() == ContactPoint.ContactPointSystem.PHONE) {
                telecom = thisTelecom;
                break;
            }
        }
        bookingOrgDetail.setTelephone(telecom.getValue());
        bookingOrgDetail.setSystem(telecom.getSystem().toString());
        if (telecom.getUse() != null && !telecom.getUse().toString().trim().isEmpty()) {
            bookingOrgDetail.setUsetype(telecom.getUse().toString());
        }
        if (!bookingOrgRes.getIdentifier().isEmpty()) {
            bookingOrgDetail.setOrgCode(bookingOrgRes.getIdentifierFirstRep().getValue());
            String system = bookingOrgRes.getIdentifierFirstRep().getSystem();
            // check that organization identifier system is https://fhir.nhs.uk/Id/ods-organization-code
            if (system != null && !system.trim().isEmpty()) {
                if (!system.equals(ID_ODS_ORGANIZATION_CODE)) {
                    throwUnprocessableEntity422_InvalidResourceException("Appointment organisation identifier system must be an ODS code!");
                }
            } else {
                throwUnprocessableEntity422_InvalidResourceException("Appointment organisation identifier system must be populated!");
            }
        }
        bookingOrgDetail.setAppointmentDetail(appointmentDetail);
        appointmentDetail.setBookingOrganization(bookingOrgDetail);
        // 1.2.7
        appointmentDetail.setServiceCategory(appointment.getServiceCategory().getText());
        appointmentDetail.setServiceType(appointment.getServiceTypeFirstRep().getText());
    }
    return appointmentDetail;
}
Also used : AppointmentParticipantComponent(org.hl7.fhir.dstu3.model.Appointment.AppointmentParticipantComponent) IBaseDatatype(org.hl7.fhir.instance.model.api.IBaseDatatype) AppointmentDetail(uk.gov.hscic.model.appointment.AppointmentDetail) BookingOrgDetail(uk.gov.hscic.model.appointment.BookingOrgDetail)

Example 5 with Practitioner

use of org.hl7.fhir.r4.model.Practitioner in project gpconnect-demonstrator by nhsconnect.

the class AppointmentResourceProvider method updateAppointment.

/**
 * amend or cancel an existing appointment
 *
 * @param appointmentId
 * @param appointment Resource
 * @param theRequest required to access the interaction id
 * @return MethodOutcome
 */
@Update
public MethodOutcome updateAppointment(@IdParam IdType appointmentId, @ResourceParam Appointment appointment, HttpServletRequest theRequest) {
    MethodOutcome methodOutcome = new MethodOutcome();
    OperationOutcome operationalOutcome = new OperationOutcome();
    AppointmentDetail appointmentDetail = appointmentResourceConverterToAppointmentDetail(appointment);
    Meta meta = appointment.getMeta();
    final List<UriType> profiles = meta.getProfile();
    // #203 validations
    VC.execute(new VC[] { new VC(() -> profiles.isEmpty(), () -> "Meta element must be present in Appointment"), new VC(() -> !profiles.get(0).getValue().equalsIgnoreCase(SD_GPC_APPOINTMENT), // what to do if > 1 meta profile element?
    () -> "Meta.profile " + profiles.get(0).getValue() + " is not equal to " + SD_GPC_APPOINTMENT), // #203
    new VC(() -> !appointment.getReason().isEmpty(), () -> "Appointment reason shouldn't be provided!"), new VC(() -> !appointment.getSpecialty().isEmpty(), () -> "Appointment speciality shouldn't be provided!"), // new VC(() -> !appointment.getServiceType().isEmpty(), () -> "Appointment service type shouldn't be provided!"),
    new VC(() -> !appointment.getAppointmentType().isEmpty(), () -> "Appointment type shouldn't be provided!"), new VC(() -> !appointment.getIndication().isEmpty(), () -> "Appointment indication shouldn't be provided!"), new VC(() -> !appointment.getSupportingInformation().isEmpty(), () -> "Appointment supporting information shouldn't be provided!"), new VC(() -> !appointment.getIncomingReferral().isEmpty(), () -> "Appointment incoming referral shouldn't be provided!") });
    // URL ID and Resource ID must be the same
    if (!Objects.equals(appointmentId.getIdPartAsLong(), appointmentDetail.getId())) {
        operationalOutcome.addIssue().setSeverity(IssueSeverity.ERROR).setDiagnostics("Id in URL (" + appointmentId.getIdPart() + ") should match Id in Resource (" + appointmentDetail.getId() + ")");
        methodOutcome.setOperationOutcome(operationalOutcome);
        return methodOutcome;
    }
    // Make sure there is an existing appointment to be amended
    AppointmentDetail oldAppointmentDetail = appointmentSearch.findAppointmentByID(appointmentId.getIdPartAsLong());
    if (oldAppointmentDetail == null) {
        operationalOutcome.addIssue().setSeverity(IssueSeverity.ERROR).setDiagnostics("No appointment details found for ID: " + appointmentId.getIdPart());
        methodOutcome.setOperationOutcome(operationalOutcome);
        return methodOutcome;
    }
    // 1.2.7 set the old service type and service category for comparison with incoming update/cancel content values
    SlotDetail slotDetail1 = slotSearch.findSlotByID(oldAppointmentDetail.getSlotIds().get(0));
    oldAppointmentDetail.setServiceType(slotDetail1.getTypeDisply());
    ScheduleDetail scheduleDetail = scheduleSearch.findScheduleByID(slotDetail1.getScheduleReference());
    oldAppointmentDetail.setServiceCategory(scheduleDetail.getTypeDescription());
    String oldAppointmentVersionId = String.valueOf(oldAppointmentDetail.getLastUpdated().getTime());
    String newAppointmentVersionId = appointmentId.getVersionIdPart();
    if (newAppointmentVersionId != null && !newAppointmentVersionId.equalsIgnoreCase(oldAppointmentVersionId)) {
        throw new ResourceVersionConflictException("The specified version (" + newAppointmentVersionId + ") did not match the current resource version (" + oldAppointmentVersionId + ")");
    }
    // check for absolute reference #200
    Iterator<AppointmentParticipantComponent> iter = appointment.getParticipant().iterator();
    while (iter.hasNext()) {
        AppointmentParticipantComponent participant = iter.next();
        if (participant.getActor() != null) {
            checkReferenceIsRelative(participant.getActor().getReference());
        }
    }
    String interactionId = theRequest.getHeader(SSP_INTERACTIONID);
    // Determine if it is a cancel or an amend. This was previously a check for the presence of a cancellation reason
    // but that is not sufficient. We can sefely assume that the interaction id is populated at this point.
    AppointmentOperation appointmentOperation = null;
    final AppointmentDetail fAppointmentDetail = appointmentDetail;
    switch(interactionId) {
        case REST_CANCEL_APPOINTMENT:
            appointmentOperation = AppointmentOperation.CANCEL;
            // added at 1.2.2
            VC.execute(new VC[] { new VC(() -> appointment.getStatus() != AppointmentStatus.CANCELLED, () -> "Status must be \"cancelled\""), // #203
            new VC(() -> isInThePast(fAppointmentDetail.getStartDateTime()), () -> "The cancellation start date cannot be in the past"), new VC(() -> fAppointmentDetail.getCancellationReason() == null, () -> "The cancellation reason must be provided"), // no point in this since fhir forbids empty elements
            new VC(() -> fAppointmentDetail.getCancellationReason().isEmpty(), () -> "The cancellation reason can not be blank") });
            validateAppointmentExtensions(appointment, profiles, appointmentOperation);
            // #172
            String appointmentType = appointment.getAppointmentType().getText();
            if (appointmentType != null) {
                throwUnprocessableEntity422_InvalidResourceException("The appointment type cannot be updated on a cancellation");
            }
            // This is a Cancellation - so copy across fields which can be
            // altered
            List cancelComparisonResult = compareAppointmentsForInvalidProperty(AppointmentOperation.CANCEL, oldAppointmentDetail, appointmentDetail);
            if (cancelComparisonResult.size() > 0) {
                throwUnprocessableEntity422_InvalidResourceException("Invalid Appointment property has been amended (cancellation) " + cancelComparisonResult);
            }
            oldAppointmentDetail.setCancellationReason(appointmentDetail.getCancellationReason());
            String oldStatus = oldAppointmentDetail.getStatus();
            appointmentDetail = oldAppointmentDetail;
            appointmentDetail.setStatus("cancelled");
            if (!"cancelled".equalsIgnoreCase(oldStatus)) {
                for (Long slotId : appointmentDetail.getSlotIds()) {
                    SlotDetail slotDetail = slotSearch.findSlotByID(slotId);
                    // slotDetail.setAppointmentId(null);
                    slotDetail.setFreeBusyType("FREE");
                    slotDetail.setLastUpdated(new Date());
                    slotStore.saveSlot(slotDetail);
                }
            }
            break;
        case REST_UPDATE_APPOINTMENT:
            appointmentOperation = AppointmentOperation.AMEND;
            VC.execute(new VC[] { new VC(() -> appointment.getStatus() != AppointmentStatus.BOOKED, () -> "Status must be \"booked\""), // this subsumes #161 which only inhibited amendment of the cancellation reason in an amend
            new VC(() -> fAppointmentDetail.getCancellationReason() != null, () -> "Cannot amend cancellation reason in appointment amend"), // added at 1.2.2
            new VC(() -> isInThePast(fAppointmentDetail.getStartDateTime()), () -> "The appointment amend start date cannot be in the past") });
            List amendComparisonResult = compareAppointmentsForInvalidProperty(AppointmentOperation.AMEND, oldAppointmentDetail, appointmentDetail);
            if (amendComparisonResult.size() > 0) {
                throwUnprocessableEntity422_InvalidResourceException("Invalid Appointment property has been amended " + amendComparisonResult);
            }
            validateAppointmentExtensions(appointment, profiles, appointmentOperation);
            // This is an Amend
            oldAppointmentDetail.setComment(appointmentDetail.getComment());
            oldAppointmentDetail.setDescription(appointmentDetail.getDescription());
            appointmentDetail = oldAppointmentDetail;
            break;
        default:
            System.err.println("AppointmentResourceProvider.updateAppointment Unhandled interaction id  " + interactionId);
    }
    // we'll get the delivery channel from the slot
    String deliveryChannel = null;
    String practitionerRoleCode = null;
    String practitionerRoleDisplay = null;
    ScheduleDetail schedule = null;
    // Common to both Update and cancel
    // slots valid?
    List<SlotDetail> slots = new ArrayList<>();
    for (Long slotId : appointmentDetail.getSlotIds()) {
        SlotDetail slotDetail = slotSearch.findSlotByID(slotId);
        if (slotDetail == null) {
            throwUnprocessableEntity422_InvalidResourceException("Slot resource reference is not a valid resource");
        }
        if (deliveryChannel == null) {
            deliveryChannel = slotDetail.getDeliveryChannelCode();
        }
        if (schedule == null) {
            // load the schedule so we can get the Practitioner ID
            schedule = scheduleSearch.findScheduleByID(slotDetail.getScheduleReference());
        }
        if (practitionerRoleDisplay == null) {
            practitionerRoleDisplay = schedule.getPractitionerRoleDisplay();
            practitionerRoleCode = schedule.getPractitionerRoleCode();
        }
        slots.add(slotDetail);
    }
    validateUpdateExtensions(deliveryChannel, practitionerRoleDisplay, practitionerRoleCode, appointment.getExtension());
    // dates valid?
    // #203
    Date firstSlotStart = slots.get(0).getStartDateTime();
    Date lastSlotEnd = slots.get(slots.size() - 1).getEndDateTime();
    // need to insert a colon in the timezone string
    String firstSlotStartStr = TIMESTAMP_FORMAT.format(firstSlotStart).replaceFirst("([0-9]{2})([0-9]{2})$", "$1:$2");
    String lastSlotEndStr = TIMESTAMP_FORMAT.format(lastSlotEnd).replaceFirst("([0-9]{2})([0-9]{2})$", "$1:$2");
    VC.execute(new VC[] { new VC(() -> appointment.getStart().compareTo(firstSlotStart) != 0, () -> String.format("Start date '%s' must match start date of first slot '%s'", appointment.getStart(), firstSlotStart)), new VC(() -> appointment.getEnd().compareTo(lastSlotEnd) != 0, () -> String.format("End date '%s' must match end date of last slot '%s'", appointment.getEnd(), lastSlotEnd)), // #218 strings should match exactly
    new VC(() -> !appointment.getStartElement().getValueAsString().equals(firstSlotStartStr), () -> String.format("Start date '%s' must lexically match start date of first slot '%s'", appointment.getStartElement().getValueAsString(), firstSlotStartStr)), new VC(() -> !appointment.getEndElement().getValueAsString().equals(lastSlotEndStr), () -> String.format("End date '%s' must lexically match end date of last slot '%s'", appointment.getEndElement().getValueAsString(), lastSlotEndStr)), new VC(() -> appointment.getSlot().size() != slots.size(), () -> String.format("Slot count mismatch %d provided appointment has %d", appointment.getSlot().size(), slots.size())) });
    // check the slots match
    HashSet<String> hs = new HashSet<>();
    for (SlotDetail slotDetail : slots) {
        hs.add("Slot/" + slotDetail.getId());
    }
    for (Reference reference : appointment.getSlot()) {
        if (!hs.contains(reference.getReference())) {
            throwUnprocessableEntity422_InvalidResourceException(String.format("Provided slot id %s does not exist in booked appointment", reference.getReference()));
        }
    }
    // Update version and
    appointmentDetail.setLastUpdated(new Date());
    // lastUpdated timestamp
    appointmentDetail = appointmentStore.saveAppointment(appointmentDetail, slots);
    methodOutcome.setId(new IdDt("Appointment", appointmentDetail.getId()));
    methodOutcome.setResource(appointmentDetailToAppointmentResourceConverter(appointmentDetail));
    return methodOutcome;
}
Also used : AppointmentParticipantComponent(org.hl7.fhir.dstu3.model.Appointment.AppointmentParticipantComponent) AppointmentDetail(uk.gov.hscic.model.appointment.AppointmentDetail) IdDt(ca.uhn.fhir.model.primitive.IdDt) MethodOutcome(ca.uhn.fhir.rest.api.MethodOutcome) VC(uk.gov.hscic.common.validators.VC) ScheduleDetail(uk.gov.hscic.model.appointment.ScheduleDetail) SlotDetail(uk.gov.hscic.model.appointment.SlotDetail)

Aggregations

Test (org.junit.Test)185 Practitioner (org.hl7.fhir.r4.model.Practitioner)138 IBundleProvider (ca.uhn.fhir.rest.api.server.IBundleProvider)86 IBaseResource (org.hl7.fhir.instance.model.api.IBaseResource)79 SearchParameterMap (org.openmrs.module.fhir2.api.search.param.SearchParameterMap)59 StringAndListParam (ca.uhn.fhir.rest.param.StringAndListParam)54 StringParam (ca.uhn.fhir.rest.param.StringParam)54 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)52 Practitioner (org.hl7.fhir.dstu3.model.Practitioner)39 Test (org.junit.jupiter.api.Test)38 BaseModuleContextSensitiveTest (org.openmrs.test.BaseModuleContextSensitiveTest)37 Identifier (org.hl7.fhir.r4.model.Identifier)32 BundleEntryComponent (org.hl7.fhir.r4.model.Bundle.BundleEntryComponent)30 Resource (org.hl7.fhir.r4.model.Resource)30 Reference (org.hl7.fhir.r4.model.Reference)24 ArrayList (java.util.ArrayList)23 Bundle (org.hl7.fhir.r4.model.Bundle)22 CodeableConcept (org.hl7.fhir.r4.model.CodeableConcept)20 BaseFhirProvenanceResourceTest (org.openmrs.module.fhir2.providers.BaseFhirProvenanceResourceTest)20 Date (java.util.Date)19