Search in sources :

Example 1 with IdType

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

the class AppointmentResourceProvider method updateAppointment.

@Update
public MethodOutcome updateAppointment(@IdParam IdType appointmentId, @ResourceParam Appointment appointment) {
    MethodOutcome methodOutcome = new MethodOutcome();
    OperationOutcome operationalOutcome = new OperationOutcome();
    AppointmentDetail appointmentDetail = appointmentResourceConverterToAppointmentDetail(appointment);
    // 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;
    }
    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 + ")");
    }
    // Determin if it is a cancel or an amend
    if (appointmentDetail.getCancellationReason() != null) {
        if (appointmentDetail.getCancellationReason().isEmpty()) {
            operationalOutcome.addIssue().setSeverity(IssueSeverity.ERROR).setDiagnostics("The cancellation reason can not be blank");
            methodOutcome.setOperationOutcome(operationalOutcome);
            return methodOutcome;
        }
        // This is a Cancellation - so copy across fields which can be
        // altered
        boolean cancelComparisonResult = compareAppointmentsForInvalidPropertyCancel(oldAppointmentDetail, appointmentDetail);
        if (cancelComparisonResult) {
            throw OperationOutcomeFactory.buildOperationOutcomeException(new UnclassifiedServerFailureException(400, "Invalid Appointment property has been amended (cancellation)"), SystemCode.BAD_REQUEST, IssueType.FORBIDDEN);
        }
        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);
            }
        }
    } else {
        if (appointment.getStatus().equals("cancelled")) {
            throw OperationOutcomeFactory.buildOperationOutcomeException(new UnclassifiedServerFailureException(400, "Appointment has been cancelled and cannot be amended"), SystemCode.BAD_REQUEST, IssueType.FORBIDDEN);
        }
        boolean amendComparisonResult = compareAppointmentsForInvalidPropertyAmend(appointmentDetail, oldAppointmentDetail);
        if (amendComparisonResult) {
            throw OperationOutcomeFactory.buildOperationOutcomeException(new UnclassifiedServerFailureException(403, "Invalid Appointment property has been amended"), SystemCode.BAD_REQUEST, IssueType.FORBIDDEN);
        }
        // This is an Amend
        oldAppointmentDetail.setComment(appointmentDetail.getComment());
        oldAppointmentDetail.setReasonCode(appointmentDetail.getReasonCode());
        oldAppointmentDetail.setDescription(appointmentDetail.getDescription());
        oldAppointmentDetail.setReasonDisplay(appointmentDetail.getReasonDisplay());
        oldAppointmentDetail.setTypeCode(appointmentDetail.getTypeCode());
        oldAppointmentDetail.setTypeDisplay(appointmentDetail.getTypeDisplay());
        appointmentDetail = oldAppointmentDetail;
    }
    List<SlotDetail> slots = new ArrayList<>();
    for (Long slotId : appointmentDetail.getSlotIds()) {
        SlotDetail slotDetail = slotSearch.findSlotByID(slotId);
        if (slotDetail == null) {
            throw new UnprocessableEntityException("Slot resource reference is not a valid resource");
        }
        slots.add(slotDetail);
    }
    // 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 : UnprocessableEntityException(ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException) AppointmentDetail(uk.gov.hscic.model.appointment.AppointmentDetail) ArrayList(java.util.ArrayList) IdDt(ca.uhn.fhir.model.primitive.IdDt) ResourceVersionConflictException(ca.uhn.fhir.rest.server.exceptions.ResourceVersionConflictException) MethodOutcome(ca.uhn.fhir.rest.api.MethodOutcome) Date(java.util.Date) UnclassifiedServerFailureException(ca.uhn.fhir.rest.server.exceptions.UnclassifiedServerFailureException) OperationOutcome(org.hl7.fhir.dstu3.model.OperationOutcome) SlotDetail(uk.gov.hscic.model.appointment.SlotDetail) Update(ca.uhn.fhir.rest.annotation.Update)

Example 2 with IdType

use of org.hl7.fhir.r4.model.IdType 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 3 with IdType

use of org.hl7.fhir.r4.model.IdType 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 4 with IdType

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

the class MedicationResourceProvider method getMedicationById.

@Read()
public Medication getMedicationById(@IdParam IdType medicationId) {
    MedicationEntity medicationEntity = medicationRepository.findOne(medicationId.getIdPartAsLong());
    if (medicationEntity == null) {
        OperationOutcome operationalOutcome = new OperationOutcome();
        operationalOutcome.addIssue().setSeverity(IssueSeverity.ERROR).setDiagnostics("No medication details found for ID: " + medicationId.getIdPart());
        throw new InternalErrorException("No medication details found for ID: " + medicationId.getIdPart(), operationalOutcome);
    }
    Medication medication = new Medication();
    String resourceId = String.valueOf(medicationEntity.getId());
    String versionId = String.valueOf(medicationEntity.getLastUpdated().getTime());
    String resourceType = medication.getResourceType().toString();
    IdType id = new IdType(resourceType, resourceId, versionId);
    medication.setId(id);
    medication.getMeta().setVersionId(versionId);
    medication.getMeta().setLastUpdated(medicationEntity.getLastUpdated());
    return medication;
}
Also used : OperationOutcome(org.hl7.fhir.dstu3.model.OperationOutcome) Medication(org.hl7.fhir.dstu3.model.Medication) InternalErrorException(ca.uhn.fhir.rest.server.exceptions.InternalErrorException) IdType(org.hl7.fhir.dstu3.model.IdType) Read(ca.uhn.fhir.rest.annotation.Read)

Example 5 with IdType

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

Aggregations

Test (org.junit.Test)240 IdType (org.hl7.fhir.r4.model.IdType)216 IdType (org.hl7.fhir.dstu3.model.IdType)157 BaseFhirProvenanceResourceTest (org.openmrs.module.fhir2.providers.BaseFhirProvenanceResourceTest)87 IBaseResource (org.hl7.fhir.instance.model.api.IBaseResource)47 Test (org.junit.jupiter.api.Test)44 Date (java.util.Date)43 MethodOutcome (ca.uhn.fhir.rest.api.MethodOutcome)41 Bundle (org.hl7.fhir.r4.model.Bundle)40 ArrayList (java.util.ArrayList)37 ResourceNotFoundException (ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException)35 Provenance (org.hl7.fhir.r4.model.Provenance)32 Resource (org.hl7.fhir.r4.model.Resource)32 Coding (org.hl7.fhir.r4.model.Coding)30 CodeableConcept (org.hl7.fhir.r4.model.CodeableConcept)26 Bundle (org.hl7.fhir.dstu3.model.Bundle)24 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)23 List (java.util.List)22 Reference (org.hl7.fhir.r4.model.Reference)20 Reference (org.hl7.fhir.dstu3.model.Reference)19