Search in sources :

Example 21 with Patient

use of org.hl7.fhir.dstu3.model.Patient in project loinc2hpo by monarch-initiative.

the class FhirResourceRetriever method retrievePatientFromServer.

/**
 * Retrieve a patient from the reference field of an observation
 * @param subject
 * @return
 */
public static Patient retrievePatientFromServer(Reference subject) throws SubjectNotFoundException, AmbiguousSubjectException {
    List<Patient> patients = new ArrayList<>();
    if (subject.hasReference()) {
        String ref = subject.getReference();
        if (!ref.startsWith(BASEURL) && ref.startsWith("Patient")) {
            ref = BASEURL + "/" + ref;
        }
        Bundle patientBundle = client.search().byUrl(ref).returnBundle(Bundle.class).execute();
        while (true) {
            patientBundle.getEntry().forEach(p -> patients.add((Patient) p.getResource()));
            if (patientBundle.getLink(IBaseBundle.LINK_NEXT) != null) {
                patientBundle = client.loadPage().next(patientBundle).execute();
            } else {
                break;
            }
        }
    } else if (subject.hasIdentifier()) {
        Identifier identifier = subject.getIdentifier();
    // TODO: find patient through the identifier
    }
    if (patients.size() == 1) {
        return patients.iterator().next();
    } else if (patients.isEmpty()) {
        throw new SubjectNotFoundException("Expect one subject, but found none");
    } else {
        throw new AmbiguousSubjectException("Except one subject, but found multiple");
    }
}
Also used : SubjectNotFoundException(org.monarchinitiative.loinc2hpo.exception.SubjectNotFoundException) AmbiguousSubjectException(org.monarchinitiative.loinc2hpo.exception.AmbiguousSubjectException) IBaseBundle(org.hl7.fhir.instance.model.api.IBaseBundle) ArrayList(java.util.ArrayList)

Example 22 with Patient

use of org.hl7.fhir.dstu3.model.Patient in project loinc2hpo by monarch-initiative.

the class FhirResourceRetriever method retrieveObservationFromServer.

/**
 * @TODO: implement it
 * retrieve a patient's observations from FHIR server
 * @param patient
 * @return
 */
public static List<Observation> retrieveObservationFromServer(Patient patient) {
    List<Observation> observationList = new ArrayList<>();
    String id = patient.getId();
    if (id != null) {
        Bundle observationBundle = client.search().forResource(Observation.class).where(new ReferenceClientParam("subject").hasId(id)).prettyPrint().returnBundle(Bundle.class).execute();
        while (true) {
            observationBundle.getEntry().forEach(p -> observationList.add((Observation) p.getResource()));
            if (observationBundle.getLink(IBaseBundle.LINK_NEXT) != null) {
                observationBundle = client.loadPage().next(observationBundle).execute();
            } else {
                break;
            }
        }
    }
    return observationList;
}
Also used : IBaseBundle(org.hl7.fhir.instance.model.api.IBaseBundle) ArrayList(java.util.ArrayList) ReferenceClientParam(ca.uhn.fhir.rest.gclient.ReferenceClientParam)

Example 23 with Patient

use of org.hl7.fhir.dstu3.model.Patient in project syndesis-qe by syndesisio.

the class FhirUtils method insertPatientToFhir.

// entity PATIENT:
public String insertPatientToFhir(MyPatientSpecification ps) {
    Patient patient = new Patient();
    patient.addName().setFamily(ps.getFamilyName()).addGiven(ps.getGivenName());
    checkConnection();
    MethodOutcome outcome = fhirClient.createPatient(patient);
    lastPatientId = this.extractSimpleId(outcome.getId().getValue(), FhirEntity.PATIENT.getName());
    log.info("Patient *{}* has been inserted with id:*{}*", ps.toString(), lastPatientId);
    return lastPatientId;
}
Also used : Patient(org.hl7.fhir.dstu3.model.Patient) MethodOutcome(ca.uhn.fhir.rest.api.MethodOutcome)

Example 24 with Patient

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

the class MedicationStatementResourceProvider method getMedicationStatementResource.

public MedicationStatement getMedicationStatementResource(MedicationStatementDetail statementDetail) {
    MedicationStatement medicationStatement = new MedicationStatement();
    medicationStatement.setId(statementDetail.getId().toString());
    List<Identifier> identifiers = new ArrayList<>();
    Identifier identifier = new Identifier().setSystem("https://fhir.nhs.uk/Id/cross-care-setting-identifier").setValue(statementDetail.getGuid());
    identifiers.add(identifier);
    medicationStatement.setIdentifier(identifiers);
    medicationStatement.setMeta(new Meta().addProfile(SystemURL.SD_GPC_MEDICATION_STATEMENT));
    medicationStatement.addExtension(new Extension(SystemURL.SD_CC_EXT_MEDICATION_STATEMENT_LAST_ISSUE, new DateTimeType(statementDetail.getLastIssueDate(), TemporalPrecisionEnum.DAY)));
    if (statementDetail.getMedicationRequestPlanId() != null) {
        medicationStatement.addBasedOn(new Reference(new IdType("MedicationRequest", statementDetail.getMedicationRequestPlanId())));
    }
    try {
        medicationStatement.setStatus(MedicationStatementStatus.fromCode(statementDetail.getStatusCode()));
    } catch (FHIRException e) {
        throw new UnprocessableEntityException(e.getMessage());
    }
    if (statementDetail.getMedicationId() != null) {
        medicationStatement.setMedication(new Reference(new IdType("Medication", statementDetail.getMedicationId())));
    }
    medicationStatement.setEffective(new Period().setStart(statementDetail.getStartDate()).setEnd(statementDetail.getEndDate()));
    medicationStatement.setDateAsserted(statementDetail.getDateAsserted());
    if (statementDetail.getPatientId() != null)
        medicationStatement.setSubject(new Reference(new IdType("Patient", statementDetail.getPatientId())));
    try {
        medicationStatement.setTaken(statementDetail.getTakenCode() != null ? MedicationStatementTaken.fromCode(statementDetail.getTakenCode()) : MedicationStatementTaken.UNK);
    } catch (FHIRException e) {
        throw new UnprocessableEntityException(e.getMessage());
    }
    setReasonCodes(medicationStatement, statementDetail);
    setNotes(medicationStatement, statementDetail);
    String dosageText = statementDetail.getDosageText();
    medicationStatement.addDosage(new Dosage().setText(dosageText == null || dosageText.trim().isEmpty() ? NO_INFORMATION_AVAILABLE : dosageText).setPatientInstruction(statementDetail.getDosagePatientInstruction()));
    String prescribingAgency = statementDetail.getPrescribingAgency();
    if (prescribingAgency != null && !prescribingAgency.trim().isEmpty()) {
        String prescribingAgencyDisplay = "";
        if (prescribingAgency.equalsIgnoreCase("prescribed-at-gp-practice")) {
            prescribingAgencyDisplay = "Prescribed at GP practice";
        } else if (prescribingAgency.equalsIgnoreCase("prescribed-by-another-organisation")) {
            prescribingAgencyDisplay = "Prescribed by another organisation";
        }
        Coding coding = new Coding(SystemURL.CS_CC_PRESCRIBING_AGENCY_STU3, prescribingAgency, prescribingAgencyDisplay);
        CodeableConcept codeableConcept = new CodeableConcept().addCoding(coding);
        medicationStatement.addExtension(new Extension(SystemURL.SD_EXTENSION_CC_PRESCRIBING_AGENCY, codeableConcept));
    }
    // #281 1.2.5 add dosageLastChanged
    Date dosageLastChanged = statementDetail.getDosageLastChanged();
    if (dosageLastChanged != null) {
        medicationStatement.addExtension(new Extension(SystemURL.SD_EXTENSION_CC_DOSAGE_LAST_CHANGED, new DateTimeType(dosageLastChanged)));
    }
    return medicationStatement;
}
Also used : UnprocessableEntityException(ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException) ArrayList(java.util.ArrayList) FHIRException(org.hl7.fhir.exceptions.FHIRException) Date(java.util.Date)

Example 25 with Patient

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

the class PatientResourceProvider method registerPatientResourceConverterToPatientDetail.

private PatientDetails registerPatientResourceConverterToPatientDetail(Patient patientResource) {
    PatientDetails patientDetails = new PatientDetails();
    HumanName name = patientResource.getNameFirstRep();
    String givenNames = name.getGiven().stream().map(n -> n.getValue()).collect(Collectors.joining(","));
    patientDetails.setForename(givenNames);
    patientDetails.setSurname(name.getFamily());
    patientDetails.setDateOfBirth(patientResource.getBirthDate());
    if (patientResource.getGender() != null) {
        patientDetails.setGender(patientResource.getGender().toString());
    }
    patientDetails.setNhsNumber(patientResource.getIdentifierFirstRep().getValue());
    DateTimeType deceased = (DateTimeType) patientResource.getDeceased();
    if (deceased != null) {
        try {
            patientDetails.setDeceased((deceased.getValue()));
        } catch (ClassCastException cce) {
            throwUnprocessableEntity422_InvalidResourceException("The multiple deceased property is expected to be a datetime");
        }
    }
    // activate patient as temporary
    patientDetails.setRegistrationStartDateTime(new Date());
    // patientDetails.setRegistrationEndDateTime(getRegistrationEndDate(patientResource));
    patientDetails.setRegistrationStatus(ACTIVE_REGISTRATION_STATUS);
    patientDetails.setRegistrationType(TEMPORARY_RESIDENT_REGISTRATION_TYPE);
    updateAddressAndTelecom(patientResource, patientDetails);
    // set some standard values for defaults, ensure managing org is always returned
    // added at 1.2.2 7 is A20047 the default GP Practice
    patientDetails.setManagingOrganization("7");
    return patientDetails;
}
Also used : ParametersParameterComponent(org.hl7.fhir.dstu3.model.Parameters.ParametersParameterComponent) AppointmentResourceProvider(uk.gov.hscic.appointments.AppointmentResourceProvider) Autowired(org.springframework.beans.factory.annotation.Autowired) IdentifierUse(org.hl7.fhir.dstu3.model.Identifier.IdentifierUse) NhsCodeValidator(uk.gov.hscic.util.NhsCodeValidator) OperationOutcomeIssueComponent(org.hl7.fhir.dstu3.model.OperationOutcome.OperationOutcomeIssueComponent) FhirRequestGenericIntercepter.throwInvalidRequest400_BadRequestException(uk.gov.hscic.common.filters.FhirRequestGenericIntercepter.throwInvalidRequest400_BadRequestException) ForbiddenOperationException(ca.uhn.fhir.rest.server.exceptions.ForbiddenOperationException) IResourceProvider(ca.uhn.fhir.rest.server.IResourceProvider) VS_GPC_ERROR_WARNING_CODE(uk.gov.hscic.SystemURL.VS_GPC_ERROR_WARNING_CODE) ParseException(java.text.ParseException) OrganizationSearch(uk.gov.hscic.organization.OrganizationSearch) IdDt(ca.uhn.fhir.model.primitive.IdDt) InvalidRequestException(ca.uhn.fhir.rest.server.exceptions.InvalidRequestException) OrganizationDetails(uk.gov.hscic.model.organization.OrganizationDetails) Count(ca.uhn.fhir.rest.annotation.Count) Collectors(java.util.stream.Collectors) UnclassifiedServerFailureException(ca.uhn.fhir.rest.server.exceptions.UnclassifiedServerFailureException) IssueType(org.hl7.fhir.dstu3.model.OperationOutcome.IssueType) AdministrativeGender(org.hl7.fhir.dstu3.model.Enumerations.AdministrativeGender) SortSpec(ca.uhn.fhir.rest.api.SortSpec) IdentifierValidator(uk.gov.hscic.common.validators.IdentifierValidator) PostConstruct(javax.annotation.PostConstruct) ContactPointSystem(org.hl7.fhir.dstu3.model.ContactPoint.ContactPointSystem) SystemCode(uk.gov.hscic.SystemCode) PopulateMedicationBundle(uk.gov.hscic.medications.PopulateMedicationBundle) Pattern(java.util.regex.Pattern) NameUse(org.hl7.fhir.dstu3.model.HumanName.NameUse) PatientSearch(uk.gov.hscic.patient.details.PatientSearch) AddressType(org.hl7.fhir.dstu3.model.Address.AddressType) java.util(java.util) FhirRequestGenericIntercepter.throwUnprocessableEntity422_InvalidResourceException(uk.gov.hscic.common.filters.FhirRequestGenericIntercepter.throwUnprocessableEntity422_InvalidResourceException) PatientStore(uk.gov.hscic.patient.details.PatientStore) SimpleDateFormat(java.text.SimpleDateFormat) PractitionerRoleResourceProvider(uk.gov.hscic.practitioner.PractitionerRoleResourceProvider) SD_CC_EXT_NHS_COMMUNICATION(uk.gov.hscic.SystemURL.SD_CC_EXT_NHS_COMMUNICATION) PractitionerResourceProvider(uk.gov.hscic.practitioner.PractitionerResourceProvider) Value(org.springframework.beans.factory.annotation.Value) UnprocessableEntityException(ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException) TelecomDetails(uk.gov.hscic.model.telecom.TelecomDetails) AddressUse(org.hl7.fhir.dstu3.model.Address.AddressUse) WORK(org.hl7.fhir.dstu3.model.Address.AddressUse.WORK) org.hl7.fhir.dstu3.model(org.hl7.fhir.dstu3.model) ResourceNotFoundException(ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException) Integer.min(java.lang.Integer.min) ca.uhn.fhir.rest.annotation(ca.uhn.fhir.rest.annotation) PatientDetails(uk.gov.hscic.model.patient.PatientDetails) BundleType(org.hl7.fhir.dstu3.model.Bundle.BundleType) SystemConstants(uk.gov.hscic.SystemConstants) SystemURL(uk.gov.hscic.SystemURL) OLD(org.hl7.fhir.dstu3.model.Address.AddressUse.OLD) TokenParam(ca.uhn.fhir.rest.param.TokenParam) ContactComponent(org.hl7.fhir.dstu3.model.Patient.ContactComponent) OperationOutcomeFactory(uk.gov.hscic.OperationOutcomeFactory) Component(org.springframework.stereotype.Component) StaticElementsHelper(uk.gov.hscic.common.helpers.StaticElementsHelper) DateAndListParam(ca.uhn.fhir.rest.param.DateAndListParam) ContactPointUse(org.hl7.fhir.dstu3.model.ContactPoint.ContactPointUse) OrganizationResourceProvider(uk.gov.hscic.organization.OrganizationResourceProvider) FHIRException(org.hl7.fhir.exceptions.FHIRException) PatientDetails(uk.gov.hscic.model.patient.PatientDetails)

Aggregations

ArrayList (java.util.ArrayList)8 UnprocessableEntityException (ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException)7 Patient (org.hl7.fhir.dstu3.model.Patient)7 FHIRException (org.hl7.fhir.exceptions.FHIRException)7 Reference (org.hl7.fhir.dstu3.model.Reference)6 InvalidRequestException (ca.uhn.fhir.rest.server.exceptions.InvalidRequestException)5 CodeableConcept (org.hl7.fhir.dstu3.model.CodeableConcept)5 Extension (org.hl7.fhir.dstu3.model.Extension)5 IdDt (ca.uhn.fhir.model.primitive.IdDt)4 ResourceNotFoundException (ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException)4 Date (java.util.Date)4 Coding (org.hl7.fhir.dstu3.model.Coding)4 AdministrativeGender (org.hl7.fhir.dstu3.model.Enumerations.AdministrativeGender)4 DateTimeDt (ca.uhn.fhir.model.primitive.DateTimeDt)3 Search (ca.uhn.fhir.rest.annotation.Search)3 Calendar (java.util.Calendar)3 Collectors (java.util.stream.Collectors)3 IdType (org.hl7.fhir.dstu3.model.IdType)3 IBaseBundle (org.hl7.fhir.instance.model.api.IBaseBundle)3 PatientDetails (uk.gov.hscic.model.patient.PatientDetails)3