Search in sources :

Example 1 with UriType

use of org.hl7.fhir.dstu3.model.UriType in project bunsen by cerner.

the class ConceptMaps method expandMappingsIterator.

private static Iterator<Mapping> expandMappingsIterator(ConceptMap map) {
    List<Mapping> mappings = new ArrayList<>();
    for (ConceptMapGroupComponent group : map.getGroup()) {
        for (SourceElementComponent element : group.getElement()) {
            for (TargetElementComponent target : element.getTarget()) {
                Mapping mapping = new Mapping();
                mapping.setConceptMapUri(map.getUrl());
                mapping.setConceptMapVersion(map.getVersion());
                try {
                    String sourceValue = map.getSource() instanceof UriType ? map.getSourceUriType().getValue() : map.getSourceReference().getReference();
                    mapping.setSourceValueSet(sourceValue);
                    String targetValue = map.getTarget() instanceof UriType ? map.getTargetUriType().getValue() : map.getTargetReference().getReference();
                    mapping.setTargetValueSet(targetValue);
                } catch (FHIRException fhirException) {
                    // an exception.
                    throw new RuntimeException(fhirException);
                }
                mapping.setSourceSystem(group.getSource());
                mapping.setSourceValue(element.getCode());
                mapping.setTargetSystem(group.getTarget());
                mapping.setTargetValue(target.getCode());
                if (target.getEquivalence() != null) {
                    mapping.setEquivalence(target.getEquivalence().toCode());
                }
                mappings.add(mapping);
            }
        }
    }
    return mappings.iterator();
}
Also used : ArrayList(java.util.ArrayList) TargetElementComponent(org.hl7.fhir.dstu3.model.ConceptMap.TargetElementComponent) FHIRException(org.hl7.fhir.exceptions.FHIRException) ConceptMapGroupComponent(org.hl7.fhir.dstu3.model.ConceptMap.ConceptMapGroupComponent) SourceElementComponent(org.hl7.fhir.dstu3.model.ConceptMap.SourceElementComponent) UriType(org.hl7.fhir.dstu3.model.UriType)

Example 2 with UriType

use of org.hl7.fhir.dstu3.model.UriType in project bunsen by cerner.

the class BroadcastableMappingsTest method setUp.

/**
 * Sets up Spark and concept maps for testing.
 */
@BeforeClass
public static void setUp() {
    spark = SparkSession.builder().master("local[2]").appName("BroadcastableMappingsTest").getOrCreate();
    ConceptMap conceptMap = new ConceptMap();
    conceptMap.setUrl("uri:test:concept:map").setVersion("0").setSource(new UriType("uri:test:source:valueset")).setTarget(new UriType("uri:test:target:valueset"));
    ConceptMapGroupComponent group = conceptMap.addGroup().setSource("uri:test:source:system").setTarget("uri:test:target:system");
    group.addElement().setCode("abc").addTarget().setCode("123");
    group.addElement().setCode("def").addTarget().setCode("456");
    ConceptMap delegatingMap = new ConceptMap();
    delegatingMap.setUrl("uri:test:concept:delegating").setVersion("0").setSource(new UriType("uri:test:source:valueset")).setTarget(new UriType("uri:test:target:valueset"));
    delegatingMap.addGroup().setSource("uri:test:source:system").setTarget("uri:test:target:system").setUnmapped(new ConceptMapGroupUnmappedComponent().setMode(ConceptMapGroupUnmappedMode.OTHERMAP).setUrl("uri:test:concept:map"));
    broadcast = BroadcastableMappings.broadcast(spark, ImmutableList.of(conceptMap, delegatingMap));
}
Also used : ConceptMapGroupUnmappedComponent(org.hl7.fhir.dstu3.model.ConceptMap.ConceptMapGroupUnmappedComponent) ConceptMap(org.hl7.fhir.dstu3.model.ConceptMap) ConceptMapGroupComponent(org.hl7.fhir.dstu3.model.ConceptMap.ConceptMapGroupComponent) UriType(org.hl7.fhir.dstu3.model.UriType) BeforeClass(org.junit.BeforeClass)

Example 3 with UriType

use of org.hl7.fhir.dstu3.model.UriType 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)

Example 4 with UriType

use of org.hl7.fhir.dstu3.model.UriType in project bunsen by cerner.

the class ConceptMapsTest method conceptMap.

private static final ConceptMap conceptMap(String url, String version) {
    ConceptMap conceptMap = new ConceptMap();
    conceptMap.setUrl(url);
    conceptMap.setVersion(version);
    conceptMap.setExperimental(true);
    conceptMap.setSource(new UriType("urn:source:valueset"));
    conceptMap.setTarget(new UriType("urn:target:valueset"));
    conceptMap.addGroup().setSource("urn:source:system").setTarget("urn:target:system").addElement().setCode("urn:source:code:a").addTarget().setCode("urn:target:code:1");
    return conceptMap;
}
Also used : ConceptMap(org.hl7.fhir.dstu3.model.ConceptMap) UriType(org.hl7.fhir.dstu3.model.UriType)

Example 5 with UriType

use of org.hl7.fhir.dstu3.model.UriType in project gpconnect-demonstrator by nhsconnect.

the class AppointmentResourceProvider method createAppointment.

/**
 * Create a new appointment
 *
 * @param appointment Resource
 * @return MethodOutcome
 */
@Create
public MethodOutcome createAppointment(@ResourceParam Appointment appointment) {
    Meta meta = appointment.getMeta();
    final List<UriType> profiles = meta.getProfile();
    // #203 validations
    // Uses VC class and lamda functions for brevity and to avoid masses of duplicated code
    // first lamda takes no params and returns a Boolean representing the fail condition ie true for fail
    // second lamda takes no params and returns a String describing the error condition
    // VC.execute evaluates the first lamda and if true theows an InvalidResource exception containg the string from the second lambda
    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), () -> "Meta.profile " + profiles.get(0).getValue() + " is not equal to " + SD_GPC_APPOINTMENT), // what to do if > 1 meta profile element?
    new VC(() -> appointment.getStatus() == null, () -> "No status supplied"), new VC(() -> appointment.getStatus() != AppointmentStatus.BOOKED, () -> "Status must be \"booked\""), new VC(() -> appointment.getStart() == null || appointment.getEnd() == null, () -> "Both start and end date are required"), new VC(() -> appointment.getSlot().isEmpty(), () -> "At least one slot is required"), new VC(() -> appointment.getCreated() == null, () -> "Created must be populated"), new VC(() -> appointment.getParticipant().isEmpty(), () -> "At least one participant is required"), new VC(() -> !appointment.getIdentifierFirstRep().isEmpty() && appointment.getIdentifierFirstRep().getValue() == null, () -> "Appointment identifier value is required"), new VC(() -> appointment.getId() != null, () -> "Appointment id shouldn't be provided!"), new VC(() -> !appointment.getReason().isEmpty(), () -> "Appointment reason shouldn't be provided!"), // #157 refers to typeCode but means specialty  (name was changed)
    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!") });
    boolean patientFound = false;
    boolean locationFound = false;
    for (AppointmentParticipantComponent participant : appointment.getParticipant()) {
        if (participant.getActor().getReference() != null) {
            String reference = participant.getActor().getReference();
            // check for absolute reference #200
            checkReferenceIsRelative(reference);
            if (reference.contains("Patient")) {
                patientFound = true;
            } else if (reference.contains("Location")) {
                locationFound = true;
            }
        } else {
            throwUnprocessableEntity422_InvalidResourceException("Appointment resource is not valid as it does not contain a Participant Actor reference");
        }
    }
    if (!patientFound || !locationFound) {
        throwUnprocessableEntity422_InvalidResourceException("Appointment resource is not a valid resource required valid Patient and Location");
    }
    // variable accessed in a lambda must be declared final
    final boolean fPatientFound = patientFound;
    final boolean fLocationFound = locationFound;
    VC.execute(new VC[] { new VC(() -> !fPatientFound, () -> "Appointment resource is not a valid resource required valid Patient"), new VC(() -> !fLocationFound, () -> "Appointment resource is not a valid resource required valid Location") });
    String locationId = null;
    String practitionerId = null;
    for (AppointmentParticipantComponent participant : appointment.getParticipant()) {
        Reference participantActor = participant.getActor();
        final Boolean searchParticipant = (participantActor != null);
        if (searchParticipant) {
            appointmentValidation.validateParticipantActor(participantActor);
        }
        CodeableConcept participantType = participant.getTypeFirstRep();
        final Boolean validParticipantType = appointmentValidation.validateParticipantType(participantType);
        VC.execute(new VC[] { new VC(() -> !searchParticipant, () -> "Supplied Participant is not valid. Must have an Actor."), new VC(() -> !validParticipantType, () -> "Supplied Participant is not valid. Must have a Type.") });
        // gets the logical id as a string
        if (participantActor.getReference().startsWith("Location")) {
            locationId = appointmentValidation.getId();
        } else if (participantActor.getReference().startsWith("Practitioner")) {
            practitionerId = appointmentValidation.getId();
        }
        appointmentValidation.validateParticipantStatus(participant.getStatus(), participant.getStatusElement(), participant.getStatusElement());
    }
    List<Extension> extensions = validateAppointmentExtensions(appointment, profiles, AppointmentOperation.BOOK);
    // Store New Appointment
    AppointmentDetail appointmentDetail = appointmentResourceConverterToAppointmentDetail(appointment);
    List<SlotDetail> slots = new ArrayList<>();
    // we'll get the delivery channel from the slot
    String deliveryChannel = null;
    String practitionerRoleCode = null;
    String practitionerRoleDisplay = null;
    ScheduleDetail schedule = null;
    String serviceType = null;
    for (Long slotId : appointmentDetail.getSlotIds()) {
        SlotDetail slotDetail = slotSearch.findSlotByID(slotId);
        new VC(() -> slotDetail == null, () -> String.format("Slot resource reference value %s is not a valid resource.", slotId)).execute();
        if (slotDetail.getFreeBusyType().equals("BUSY")) {
            throw OperationOutcomeFactory.buildOperationOutcomeException(new ResourceVersionConflictException(String.format("Slot is already in use.", slotId)), SystemCode.DUPLICATE_REJECTED, IssueType.CONFLICT);
        }
        if (deliveryChannel == null) {
            deliveryChannel = slotDetail.getDeliveryChannelCode();
        } else if (!deliveryChannel.equals(slotDetail.getDeliveryChannelCode())) {
            // added at 1.2.2
            throwUnprocessableEntity422_InvalidResourceException(String.format("Subsequent slot (Slot/%s) delivery channel (%s) is not equal to initial slot delivery channel (%s).", slotId, deliveryChannel, slotDetail.getDeliveryChannelCode()));
        }
        // 1.2.7 check service type is the same as initial service type
        if (serviceType == null) {
            serviceType = slotDetail.getTypeDisply();
        } else if (!serviceType.equals(slotDetail.getTypeDisply())) {
            // added at 1.2.2
            throwUnprocessableEntity422_InvalidResourceException(String.format("Subsequent slot (Slot/%s) service type (%s) is not equal to initial slot service type (%s).", slotId, serviceType, slotDetail.getTypeDisply()));
        }
        if (schedule == null) {
            // load the schedule so we can get the Practitioner ID
            schedule = scheduleSearch.findScheduleByID(slotDetail.getScheduleReference());
            // add practitioner id so we can get the practitioner role
            // practitioner is derived but needs to be stored fgr comparison if provided
            appointmentDetail.setPractitionerId(schedule.getPractitionerId());
        }
        if (practitionerRoleDisplay == null) {
            practitionerRoleDisplay = schedule.getPractitionerRoleDisplay();
            practitionerRoleCode = schedule.getPractitionerRoleCode();
        }
        slots.add(slotDetail);
    }
    // for slot
    validateUpdateExtensions(deliveryChannel, practitionerRoleDisplay, practitionerRoleCode, extensions);
    // #203 check location id matches the one in the schedule
    if (locationId != null && !locationId.equals(schedule.getLocationId().toString())) {
        throwUnprocessableEntity422_InvalidResourceException(String.format("Provided location id (%s) is not equal to Schedule location id (%s)", locationId, schedule.getLocationId().toString()));
    }
    // #203 check practitioner id matches the one in the schedule if there is one
    if (practitionerId != null) {
        if (!practitionerId.equals(schedule.getPractitionerId().toString())) {
            throwUnprocessableEntity422_InvalidResourceException(String.format("Provided practitioner id (%s) is not equal to Schedule practitioner id (%s)", practitionerId, schedule.getPractitionerId().toString()));
        }
    }
    // #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)), // This probably can never happen under fhir
    new VC(() -> appointment.getSlot().isEmpty(), () -> "Slot reference must be populated") });
    int durationFromSlots = (int) (lastSlotEnd.getTime() - firstSlotStart.getTime()) / (1000 * 60);
    // #203 validate optional minutes duration
    if (appointment.hasMinutesDuration()) {
        int providedDuration = appointment.getMinutesDuration();
        new VC(() -> durationFromSlots != providedDuration, () -> String.format("Provided duration (%d minutes) is not equal to actual appointment duration (%d minutes)", providedDuration, durationFromSlots)).execute();
    } else {
        // not provided so write the calculated value into the appointmentDetail object
        appointmentDetail.setMinutesDuration(durationFromSlots);
    }
    // add deliveryChannel #157 removed
    // appointmentDetail.setDeliveryChannel(deliveryChannel);
    appointmentDetail = appointmentStore.saveAppointment(appointmentDetail, slots);
    for (SlotDetail slot : slots) {
        // slot.setAppointmentId(appointmentDetail.getId());
        slot.setFreeBusyType("BUSY");
        slot.setLastUpdated(new Date());
        slotStore.saveSlot(slot);
    }
    // Build response containing the new resource id
    MethodOutcome methodOutcome = new MethodOutcome();
    methodOutcome.setId(new IdDt("Appointment", appointmentDetail.getId()));
    methodOutcome.setResource(appointmentDetailToAppointmentResourceConverter(appointmentDetail));
    methodOutcome.setCreated(Boolean.TRUE);
    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) VC(uk.gov.hscic.common.validators.VC) MethodOutcome(ca.uhn.fhir.rest.api.MethodOutcome) ScheduleDetail(uk.gov.hscic.model.appointment.ScheduleDetail) SlotDetail(uk.gov.hscic.model.appointment.SlotDetail)

Aggregations

UriType (org.hl7.fhir.dstu3.model.UriType)3 IdDt (ca.uhn.fhir.model.primitive.IdDt)2 MethodOutcome (ca.uhn.fhir.rest.api.MethodOutcome)2 AppointmentParticipantComponent (org.hl7.fhir.dstu3.model.Appointment.AppointmentParticipantComponent)2 ConceptMap (org.hl7.fhir.dstu3.model.ConceptMap)2 ConceptMapGroupComponent (org.hl7.fhir.dstu3.model.ConceptMap.ConceptMapGroupComponent)2 VC (uk.gov.hscic.common.validators.VC)2 AppointmentDetail (uk.gov.hscic.model.appointment.AppointmentDetail)2 ScheduleDetail (uk.gov.hscic.model.appointment.ScheduleDetail)2 SlotDetail (uk.gov.hscic.model.appointment.SlotDetail)2 ArrayList (java.util.ArrayList)1 ConceptMapGroupUnmappedComponent (org.hl7.fhir.dstu3.model.ConceptMap.ConceptMapGroupUnmappedComponent)1 SourceElementComponent (org.hl7.fhir.dstu3.model.ConceptMap.SourceElementComponent)1 TargetElementComponent (org.hl7.fhir.dstu3.model.ConceptMap.TargetElementComponent)1 FHIRException (org.hl7.fhir.exceptions.FHIRException)1 BeforeClass (org.junit.BeforeClass)1