Search in sources :

Example 21 with DOCUMENT

use of org.hl7.fhir.r4.model.Bundle.BundleType.DOCUMENT in project summary-care-record-api by NHSDigital.

the class DiagnosisMapper method map.

@SneakyThrows
public List<Resource> map(Node document) {
    var resources = new ArrayList<Resource>();
    NodeList pertinentNodes = xmlUtils.getNodeListByXPath(document, PERTINENT_CRET_BASE_PATH);
    for (int i = 0; i < pertinentNodes.getLength(); i++) {
        Node pertinentCREType = xmlUtils.getNodeAndDetachFromParent(pertinentNodes, i);
        var pertinentCRETypeCode = xmlUtils.getValueByXPath(pertinentCREType, PERTINENT_CODE_CODE_XPATH);
        var pertinentCRETypeDisplay = xmlUtils.getValueByXPath(pertinentCREType, PERTINENT_CODE_DISPLAY_XPATH);
        NodeList diagnosisNodes = xmlUtils.getNodeListByXPath(pertinentCREType, DIAGNOSIS_BASE_PATH);
        for (int j = 0; j < diagnosisNodes.getLength(); j++) {
            Node node = xmlUtils.getNodeAndDetachFromParent(diagnosisNodes, j);
            CodedEntry entry = codedEntryMapper.getCommonCodedEntryValues(node);
            if (COVID_19_ENTRIES.contains(entry.getCodeValue())) {
                var pertinentSupportingInfo = xmlUtils.getOptionalValueByXPath(node, DIAGNOSIS_PERTINENT_SUPPORTING_INFO_XPATH);
                var condition = new Condition();
                condition.setId(entry.getId());
                condition.addIdentifier().setValue(entry.getId());
                condition.setCode(new CodeableConcept().addCoding(new Coding().setCode(entry.getCodeValue()).setSystem(SNOMED_SYSTEM).setDisplay(entry.getCodeDisplay())));
                setConditionStatus(condition, entry.getStatus());
                condition.addCategory(new CodeableConcept(new Coding().setSystem(SNOMED_SYSTEM).setCode(pertinentCRETypeCode).setDisplay(pertinentCRETypeDisplay)));
                if (entry.getEffectiveTimeHigh().isPresent()) {
                    condition.setOnset(entry.getEffectiveTimeLow().get());
                    condition.setAbatement(entry.getEffectiveTimeHigh().get());
                } else {
                    condition.setOnset(entry.getEffectiveTimeLow().get());
                }
                resources.add(condition);
                pertinentSupportingInfo.map(value -> new Annotation().setText(value)).ifPresent(condition::addNote);
                xmlUtils.getNodesByXPath(node, DIAGNOSIS_PERTINENT_FINDINGS_XPATH).stream().map(it -> xmlUtils.getValueByXPath(it, DIAGNOSIS_PERTINENT_FINDING_ID_XPATH)).map(it -> new Reference(new Observation().setId(it))).map(reference -> new Condition.ConditionEvidenceComponent().addDetail(reference)).forEach(condition::addEvidence);
                mapEncounter(node, condition, resources);
            }
        }
    }
    return resources;
}
Also used : Condition(org.hl7.fhir.r4.model.Condition) SneakyThrows(lombok.SneakyThrows) DateTimeType(org.hl7.fhir.r4.model.DateTimeType) RequiredArgsConstructor(lombok.RequiredArgsConstructor) CodeableConcept(org.hl7.fhir.r4.model.CodeableConcept) Autowired(org.springframework.beans.factory.annotation.Autowired) Condition(org.hl7.fhir.r4.model.Condition) Reference(org.hl7.fhir.r4.model.Reference) ArrayList(java.util.ArrayList) RelatedPerson(org.hl7.fhir.r4.model.RelatedPerson) Encounter(org.hl7.fhir.r4.model.Encounter) Node(org.w3c.dom.Node) CodedEntryMapper(uk.nhs.adaptors.scr.mappings.from.hl7.common.CodedEntryMapper) Observation(org.hl7.fhir.r4.model.Observation) NodeList(org.w3c.dom.NodeList) Period(org.hl7.fhir.r4.model.Period) Resource(org.hl7.fhir.r4.model.Resource) FINISHED(org.hl7.fhir.r4.model.Encounter.EncounterStatus.FINISHED) PractitionerRole(org.hl7.fhir.r4.model.PractitionerRole) EncounterParticipantComponent(org.hl7.fhir.r4.model.Encounter.EncounterParticipantComponent) Component(org.springframework.stereotype.Component) List(java.util.List) XmlToFhirMapper.parseDate(uk.nhs.adaptors.scr.mappings.from.hl7.XmlToFhirMapper.parseDate) Coding(org.hl7.fhir.r4.model.Coding) FhirHelper.randomUUID(uk.nhs.adaptors.scr.utils.FhirHelper.randomUUID) Optional(java.util.Optional) XmlUtils(uk.nhs.adaptors.scr.utils.XmlUtils) Annotation(org.hl7.fhir.r4.model.Annotation) CodedEntry(uk.nhs.adaptors.scr.mappings.from.hl7.common.CodedEntry) Reference(org.hl7.fhir.r4.model.Reference) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) Annotation(org.hl7.fhir.r4.model.Annotation) CodedEntry(uk.nhs.adaptors.scr.mappings.from.hl7.common.CodedEntry) Coding(org.hl7.fhir.r4.model.Coding) Observation(org.hl7.fhir.r4.model.Observation) CodeableConcept(org.hl7.fhir.r4.model.CodeableConcept) SneakyThrows(lombok.SneakyThrows)

Example 22 with DOCUMENT

use of org.hl7.fhir.r4.model.Bundle.BundleType.DOCUMENT in project summary-care-record-api by NHSDigital.

the class GpSummaryMapper method addAuthorOrganisation.

private void addAuthorOrganisation(Node document, List<Resource> resources, Composition composition) {
    xmlUtils.detachOptionalNodeByXPath(document, GP_SUMMARY_AUTHOR_AGENT_ORG_SDS_XPATH).ifPresent(agentOrganisationSds -> {
        Organization organisation = organisationSdsMapper.mapOrganizationSds(agentOrganisationSds);
        resources.add(organisation);
        composition.addAuthor(new Reference(organisation));
    });
    xmlUtils.detachOptionalNodeByXPath(document, GP_SUMMARY_AUTHOR_AGENT_ORG_XPATH).ifPresent(agentOrganisation -> {
        List<? extends Resource> organisationResources = agentOrganisationMapper.map(agentOrganisation);
        resources.addAll(organisationResources);
        composition.addAuthor(findPractitionerRole(organisationResources));
    });
}
Also used : Organization(org.hl7.fhir.r4.model.Organization) Reference(org.hl7.fhir.r4.model.Reference)

Example 23 with DOCUMENT

use of org.hl7.fhir.r4.model.Bundle.BundleType.DOCUMENT in project summary-care-record-api by NHSDigital.

the class GpSummaryMapper method map.

@SneakyThrows
public List<Resource> map(Node document) {
    var gpSummaryId = xmlUtils.getValueByXPath(document, GP_SUMMARY_ID_XPATH);
    var gpSummaryCodeCode = xmlUtils.getValueByXPath(document, GP_SUMMARY_CODE_CODE_XPATH);
    var gpSummaryCodeDisplayName = xmlUtils.getValueByXPath(document, GP_SUMMARY_CODE_DISPLAY_NAME_XPATH);
    var gpSummaryStatusCode = xmlUtils.getValueByXPath(document, GP_SUMMARY_STATUS_CODE_XPATH);
    var gpSummaryEffectiveTime = parseDate(xmlUtils.getValueByXPath(document, GP_SUMMARY_EFFECTIVE_TIME_XPATH), InstantType.class);
    var authorTime = parseDate(xmlUtils.getValueByXPath(document, GP_SUMMARY_AUTHOR_TIME_XPATH), DateTimeType.class);
    var replacementOfPriorMessageRefIdRoot = xmlUtils.getOptionalValueByXPath(document, REPLACEMENT_OF_PRIOR_MESSAGE_REF_ID_ROOT_XPATH);
    var pertinentRootCreTypeCodeCode = xmlUtils.getValueByXPath(document, PERTINENT_ROOT_CRE_TYPE_CODE_CODE_XPATH);
    var pertinentRootCreTypeCodeDisplayName = xmlUtils.getValueByXPath(document, PERTINENT_ROOT_CRE_TYPE_CODE_DISPLAY_NAME_XPATH);
    var presentationTextValue = xmlUtils.detachOptionalNodeByXPath(document, PRESENTATION_TEXT_VALUE);
    var eventId = xmlUtils.getValueByXPath(document, EVENT_ID_XPATH);
    List<Resource> resources = new ArrayList<>();
    var composition = new Composition();
    composition.setId(eventId);
    composition.setIdentifier(new Identifier().setValue(gpSummaryId).setSystem("https://tools.ietf.org/html/rfc4122"));
    composition.setType(new CodeableConcept().addCoding(new Coding().setCode(gpSummaryCodeCode).setSystem(SNOMED_SYSTEM).setDisplay(gpSummaryCodeDisplayName)));
    composition.setMeta(new Meta().setLastUpdatedElement(gpSummaryEffectiveTime));
    composition.setStatus(mapCompositionStatus(gpSummaryStatusCode));
    composition.setDateElement(authorTime);
    replacementOfPriorMessageRefIdRoot.ifPresent(val -> composition.addRelatesTo(new Composition.CompositionRelatesToComponent().setTarget(new Identifier().setValue(val)).setCode(REPLACES)));
    composition.addCategory(new CodeableConcept().addCoding(new Coding().setCode(pertinentRootCreTypeCodeCode).setSystem(SNOMED_SYSTEM).setDisplay(pertinentRootCreTypeCodeDisplayName)));
    Map<String, List<String>> references = sectionReferences(document);
    presentationTextValue.map(htmlParser::parse).map(Collection::stream).ifPresent(it -> it.forEach(section -> {
        if (section.getTitle() != null && CODED_ENTRY_RESOURCE_MAP.keySet().contains(section.getTitle()) && references.containsKey(section.getTitle())) {
            for (String codedEntryId : references.get(section.getTitle())) {
                section.addEntry(new Reference(CODED_ENTRY_RESOURCE_MAP.get(section.getTitle()) + "/" + codedEntryId));
            }
        }
        composition.addSection(section);
    }));
    resources.add(composition);
    addAuthor(document, resources, composition);
    return resources;
}
Also used : SneakyThrows(lombok.SneakyThrows) DateTimeType(org.hl7.fhir.r4.model.DateTimeType) Collection(java.util.Collection) Identifier(org.hl7.fhir.r4.model.Identifier) RequiredArgsConstructor(lombok.RequiredArgsConstructor) CodeableConcept(org.hl7.fhir.r4.model.CodeableConcept) Resource(org.hl7.fhir.r4.model.Resource) Autowired(org.springframework.beans.factory.annotation.Autowired) HashMap(java.util.HashMap) Composition(org.hl7.fhir.r4.model.Composition) Reference(org.hl7.fhir.r4.model.Reference) PractitionerRole(org.hl7.fhir.r4.model.PractitionerRole) ArrayList(java.util.ArrayList) Organization(org.hl7.fhir.r4.model.Organization) List(java.util.List) InstantType(org.hl7.fhir.r4.model.InstantType) Component(org.springframework.stereotype.Component) XmlToFhirMapper.parseDate(uk.nhs.adaptors.scr.mappings.from.hl7.XmlToFhirMapper.parseDate) Coding(org.hl7.fhir.r4.model.Coding) Map(java.util.Map) Node(org.w3c.dom.Node) REPLACES(org.hl7.fhir.r4.model.Composition.DocumentRelationshipType.REPLACES) XmlUtils(uk.nhs.adaptors.scr.utils.XmlUtils) Meta(org.hl7.fhir.r4.model.Meta) Meta(org.hl7.fhir.r4.model.Meta) Composition(org.hl7.fhir.r4.model.Composition) Reference(org.hl7.fhir.r4.model.Reference) Resource(org.hl7.fhir.r4.model.Resource) ArrayList(java.util.ArrayList) Identifier(org.hl7.fhir.r4.model.Identifier) Coding(org.hl7.fhir.r4.model.Coding) ArrayList(java.util.ArrayList) List(java.util.List) CodeableConcept(org.hl7.fhir.r4.model.CodeableConcept) SneakyThrows(lombok.SneakyThrows)

Example 24 with DOCUMENT

use of org.hl7.fhir.r4.model.Bundle.BundleType.DOCUMENT in project summary-care-record-api by NHSDigital.

the class AcsService method setPermission.

public void setPermission(RequestData requestData) {
    Parameters parameters = fhirParser.parseResource(requestData.getBody(), Parameters.class);
    ParametersParameterComponent parameter = getSetPermissionParameter(parameters);
    UserInfo userInfo = identityService.getUserInfo(requestData.getAuthorization());
    String acsRequest = prepareAcsRequest(parameter, requestData, getUserRoleCode(userInfo, requestData.getNhsdSessionUrid()), userInfo.getId());
    Response<Document> response = spineClient.sendAcsData(acsRequest, requestData.getNhsdAsid());
    spineDetectedIssuesHandler.handleDetectedIssues(spineResponseParser.getDetectedIssues(response.getBody()));
}
Also used : Parameters(org.hl7.fhir.r4.model.Parameters) UserInfo(uk.nhs.adaptors.scr.clients.identity.UserInfo) Document(org.w3c.dom.Document) ParametersParameterComponent(org.hl7.fhir.r4.model.Parameters.ParametersParameterComponent)

Example 25 with DOCUMENT

use of org.hl7.fhir.r4.model.Bundle.BundleType.DOCUMENT in project summary-care-record-api by NHSDigital.

the class GetScrService method getScr.

@LogExecutionTime
public Bundle getScr(String nhsNumber, String compositionId, String nhsdAsid, String clientIp) {
    Document scrIdXml = getScrIdRawXml(nhsNumber, nhsdAsid, clientIp);
    checkDetectedIssues(scrIdXml);
    EventListQueryResponse response = eventListQueryResponseParser.parseXml(scrIdXml);
    if (StringUtils.equals(response.getLatestScrId(), compositionId)) {
        Document document = getScrRawXml(response.getLatestScrId(), nhsNumber, nhsdAsid, clientIp);
        logXml("Received SCR XML: {}", document);
        checkDetectedIssues(document);
        var bundle = interactionMapper.map(document);
        Patient patient = recordTargetMapper.mapPatient(document);
        Stream.of(gpSummaryMapper, diagnosisMapper, findingMapper).map(mapper -> mapper.map(document)).flatMap(resources -> resources.stream()).peek(it -> setPatientReferences(it, patient)).map(resource -> getBundleEntryComponent(resource)).forEach(bundle::addEntry);
        bundle.addEntry(getBundleEntryComponent(patient));
        bundle.setTotal(bundle.getEntry().size());
        return bundle;
    } else {
        return interactionMapper.mapToEmpty();
    }
}
Also used : SpineClientContract(uk.nhs.adaptors.scr.clients.spine.SpineClientContract) TemplateUtils(uk.nhs.adaptors.scr.utils.TemplateUtils) Mustache(com.github.mustachejava.Mustache) RequiredArgsConstructor(lombok.RequiredArgsConstructor) Identifier(org.hl7.fhir.r4.model.Identifier) Response(uk.nhs.adaptors.scr.clients.spine.SpineHttpClient.Response) CodeableConcept(org.hl7.fhir.r4.model.CodeableConcept) DocumentReferenceContextComponent(org.hl7.fhir.r4.model.DocumentReference.DocumentReferenceContextComponent) Autowired(org.springframework.beans.factory.annotation.Autowired) EventListQueryResponse(uk.nhs.adaptors.scr.models.EventListQueryResponse) Reference(org.hl7.fhir.r4.model.Reference) OffsetDateTime.now(java.time.OffsetDateTime.now) StringUtils(org.apache.commons.lang3.StringUtils) RelatedPerson(org.hl7.fhir.r4.model.RelatedPerson) EventListQueryResponseParser(uk.nhs.adaptors.scr.models.EventListQueryResponseParser) GpSummaryMapper(uk.nhs.adaptors.scr.mappings.from.hl7.GpSummaryMapper) Attachment(org.hl7.fhir.r4.model.Attachment) DocumentReferenceContentComponent(org.hl7.fhir.r4.model.DocumentReference.DocumentReferenceContentComponent) Document(org.w3c.dom.Document) Arrays.asList(java.util.Arrays.asList) BundleEntryComponent(org.hl7.fhir.r4.model.Bundle.BundleEntryComponent) SpineConfiguration(uk.nhs.adaptors.scr.config.SpineConfiguration) CURRENT(org.hl7.fhir.r4.model.Enumerations.DocumentReferenceStatus.CURRENT) HtmlParser.serialize(uk.nhs.adaptors.scr.mappings.from.hl7.HtmlParser.serialize) Patient(org.hl7.fhir.r4.model.Patient) DiagnosisMapper(uk.nhs.adaptors.scr.mappings.from.hl7.DiagnosisMapper) MATCH(org.hl7.fhir.r4.model.Bundle.SearchEntryMode.MATCH) TemplateUtils.loadTemplate(uk.nhs.adaptors.scr.utils.TemplateUtils.loadTemplate) DocumentReference(org.hl7.fhir.r4.model.DocumentReference) ScrConfiguration(uk.nhs.adaptors.scr.config.ScrConfiguration) InteractionMapper(uk.nhs.adaptors.scr.mappings.from.hl7.InteractionMapper) Resource(org.hl7.fhir.r4.model.Resource) SEARCHSET(org.hl7.fhir.r4.model.Bundle.BundleType.SEARCHSET) Composition(org.hl7.fhir.r4.model.Composition) EventQueryParams(uk.nhs.adaptors.scr.models.EventQueryParams) Slf4j(lombok.extern.slf4j.Slf4j) Component(org.springframework.stereotype.Component) Stream(java.util.stream.Stream) FindingMapper(uk.nhs.adaptors.scr.mappings.from.hl7.FindingMapper) Coding(org.hl7.fhir.r4.model.Coding) MDC(org.slf4j.MDC) EventListQueryParams(uk.nhs.adaptors.scr.models.EventListQueryParams) DateTimeFormatter(java.time.format.DateTimeFormatter) UTC(java.time.ZoneOffset.UTC) FhirHelper.randomUUID(uk.nhs.adaptors.scr.utils.FhirHelper.randomUUID) Bundle(org.hl7.fhir.r4.model.Bundle) RecordTargetMapper(uk.nhs.adaptors.scr.mappings.from.hl7.RecordTargetMapper) LogExecutionTime(uk.nhs.adaptors.scr.logging.LogExecutionTime) EventListQueryResponse(uk.nhs.adaptors.scr.models.EventListQueryResponse) Patient(org.hl7.fhir.r4.model.Patient) Document(org.w3c.dom.Document) LogExecutionTime(uk.nhs.adaptors.scr.logging.LogExecutionTime)

Aggregations

Document (org.w3c.dom.Document)48 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)37 DocumentBuilder (javax.xml.parsers.DocumentBuilder)36 IOException (java.io.IOException)33 FHIRException (org.hl7.fhir.exceptions.FHIRException)33 ArrayList (java.util.ArrayList)28 Element (org.w3c.dom.Element)20 CSFileInputStream (org.hl7.fhir.utilities.CSFileInputStream)16 XhtmlNode (org.hl7.fhir.utilities.xhtml.XhtmlNode)15 HashMap (java.util.HashMap)14 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)13 FileNotFoundException (java.io.FileNotFoundException)12 ByteArrayInputStream (java.io.ByteArrayInputStream)11 List (java.util.List)11 CSFile (org.hl7.fhir.utilities.CSFile)11 File (java.io.File)9 UnsupportedEncodingException (java.io.UnsupportedEncodingException)9 XmlGenerator (org.hl7.fhir.utilities.xml.XmlGenerator)9 Identifier (org.hl7.fhir.r4.model.Identifier)8 TextFile (org.hl7.fhir.utilities.TextFile)8