Search in sources :

Example 46 with DomainResource

use of org.hl7.fhir.r4.model.DomainResource in project org.hl7.fhir.core by hapifhir.

the class BundleRenderer method render.

public XhtmlNode render(Bundle b) throws FHIRFormatError, DefinitionException, IOException, FHIRException, EOperationOutcome {
    if (b.getType() == BundleType.DOCUMENT) {
        if (!b.hasEntry() || !(b.getEntryFirstRep().hasResource() && b.getEntryFirstRep().getResource() instanceof Composition)) {
            throw new FHIRException("Invalid document - first entry is not a Composition");
        }
        XhtmlNode x = new XhtmlNode(NodeType.Element, "div");
        renderDocument(x, b);
        return x;
    } else if ((b.getType() == BundleType.COLLECTION && allEntresAreHistoryProvenance(b))) {
        return null;
    } else {
        XhtmlNode root = new XhtmlNode(NodeType.Element, "div");
        root.para().addText(formatMessage(RENDER_BUNDLE_HEADER_ROOT, b.getId(), b.getType().toCode()));
        int i = 0;
        for (BundleEntryComponent be : b.getEntry()) {
            i++;
            if (be.hasFullUrl())
                root.an(makeInternalBundleLink(be.getFullUrl()));
            if (be.hasResource() && be.getResource().hasId())
                root.an(be.getResource().getResourceType().name() + "_" + be.getResource().getId());
            root.hr();
            if (be.hasFullUrl()) {
                root.para().addText(formatMessage(RENDER_BUNDLE_HEADER_ENTRY_URL, Integer.toString(i), be.getFullUrl()));
            } else {
                root.para().addText(formatMessage(RENDER_BUNDLE_HEADER_ENTRY, Integer.toString(i)));
            }
            if (be.hasRequest())
                renderRequest(root, be.getRequest());
            if (be.hasSearch())
                renderSearch(root, be.getSearch());
            if (be.hasResponse())
                renderResponse(root, be.getResponse());
            if (be.hasResource()) {
                root.para().addText(formatMessage(RENDER_BUNDLE_RESOURCE, be.getResource().fhirType()));
                if (be.hasResource()) {
                    XhtmlNode xn = null;
                    if (be.getResource() instanceof DomainResource) {
                        DomainResource dr = (DomainResource) be.getResource();
                        xn = dr.getText().getDiv();
                    }
                    if (xn == null || xn.isEmpty()) {
                        ResourceRenderer rr = RendererFactory.factory(be.getResource(), context);
                        try {
                            xn = rr.build(be.getResource());
                        } catch (Exception e) {
                            xn = makeExceptionXhtml(e, "generating narrative");
                        }
                    }
                    root.blockquote().getChildNodes().addAll(checkInternalLinks(b, xn.getChildNodes()));
                }
            }
        }
        return root;
    }
}
Also used : Composition(org.hl7.fhir.r4b.model.Composition) BundleEntryComponent(org.hl7.fhir.r4b.model.Bundle.BundleEntryComponent) DomainResource(org.hl7.fhir.r4b.model.DomainResource) FHIRException(org.hl7.fhir.exceptions.FHIRException) DefinitionException(org.hl7.fhir.exceptions.DefinitionException) IOException(java.io.IOException) FHIRException(org.hl7.fhir.exceptions.FHIRException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) XhtmlNode(org.hl7.fhir.utilities.xhtml.XhtmlNode)

Example 47 with DomainResource

use of org.hl7.fhir.r4.model.DomainResource in project org.hl7.fhir.core by hapifhir.

the class ResourceRenderer method inject.

public static void inject(DomainResource r, XhtmlNode x, NarrativeStatus status) {
    if (!x.hasAttribute("xmlns"))
        x.setAttribute("xmlns", "http://www.w3.org/1999/xhtml");
    if (r.hasLanguage()) {
        // use both - see https://www.w3.org/TR/i18n-html-tech-lang/#langvalues
        x.setAttribute("lang", r.getLanguage());
        x.setAttribute("xml:lang", r.getLanguage());
    }
    r.getText().setUserData("renderer.generated", true);
    if (!r.hasText() || !r.getText().hasDiv() || r.getText().getDiv().getChildNodes().isEmpty()) {
        r.setText(new Narrative());
        r.getText().setDiv(x);
        r.getText().setStatus(status);
    } else {
        XhtmlNode n = r.getText().getDiv();
        n.clear();
        n.getChildNodes().addAll(x.getChildNodes());
    }
}
Also used : Narrative(org.hl7.fhir.r4b.model.Narrative) XhtmlNode(org.hl7.fhir.utilities.xhtml.XhtmlNode)

Example 48 with DomainResource

use of org.hl7.fhir.r4.model.DomainResource in project org.hl7.fhir.core by hapifhir.

the class ResourceRenderer method render.

/**
 * given a resource, update it's narrative with the best rendering available
 *
 * @param r - the domain resource in question
 *
 * @throws IOException
 * @throws EOperationOutcome
 * @throws FHIRException
 */
public void render(DomainResource r) throws IOException, FHIRException, EOperationOutcome {
    XhtmlNode x = new XhtmlNode(NodeType.Element, "div");
    boolean ofr = forResource;
    boolean hasExtensions;
    try {
        forResource = true;
        hasExtensions = render(x, r);
    } finally {
        forResource = ofr;
    }
    inject(r, x, hasExtensions ? NarrativeStatus.EXTENSIONS : NarrativeStatus.GENERATED);
}
Also used : XhtmlNode(org.hl7.fhir.utilities.xhtml.XhtmlNode)

Example 49 with DomainResource

use of org.hl7.fhir.r4.model.DomainResource in project org.hl7.fhir.core by hapifhir.

the class ResourceRoundTripTests method test.

@Test
public void test() throws IOException, FHIRException, EOperationOutcome {
    DomainResource res = (DomainResource) new XmlParser().parse(TestingUtilities.loadTestResourceStream("r5", "unicode.xml"));
    RenderingContext rc = new RenderingContext(TestingUtilities.context(), null, null, "http://hl7.org/fhir", "", null, ResourceRendererMode.END_USER);
    RendererFactory.factory(res, rc).render(res);
    IOUtils.copy(TestingUtilities.loadTestResourceStream("r5", "unicode.xml"), new FileOutputStream(TestingUtilities.tempFile("gen", "unicode.xml")));
    new XmlParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(TestingUtilities.tempFile("gen", "unicode.out.xml")), res);
}
Also used : XmlParser(org.hl7.fhir.r4b.formats.XmlParser) RenderingContext(org.hl7.fhir.r4b.renderers.utils.RenderingContext) DomainResource(org.hl7.fhir.r4b.model.DomainResource) FileOutputStream(java.io.FileOutputStream) Test(org.junit.jupiter.api.Test)

Example 50 with DomainResource

use of org.hl7.fhir.r4.model.DomainResource in project org.hl7.fhir.core by hapifhir.

the class BundleRenderer method render.

public XhtmlNode render(Bundle b) throws FHIRFormatError, DefinitionException, IOException, FHIRException, EOperationOutcome {
    if (b.getType() == BundleType.DOCUMENT) {
        if (!b.hasEntry() || !(b.getEntryFirstRep().hasResource() && b.getEntryFirstRep().getResource() instanceof Composition)) {
            throw new FHIRException("Invalid document - first entry is not a Composition");
        }
        XhtmlNode x = new XhtmlNode(NodeType.Element, "div");
        renderDocument(x, b);
        return x;
    } else if ((b.getType() == BundleType.COLLECTION && allEntresAreHistoryProvenance(b))) {
        return null;
    } else {
        XhtmlNode root = new XhtmlNode(NodeType.Element, "div");
        root.para().addText(formatMessage(RENDER_BUNDLE_HEADER_ROOT, b.getId(), b.getType().toCode()));
        int i = 0;
        for (BundleEntryComponent be : b.getEntry()) {
            i++;
            if (be.hasFullUrl())
                root.an(makeInternalBundleLink(be.getFullUrl()));
            if (be.hasResource() && be.getResource().hasId())
                root.an(be.getResource().getResourceType().name() + "_" + be.getResource().getId());
            root.hr();
            if (be.hasFullUrl()) {
                root.para().addText(formatMessage(RENDER_BUNDLE_HEADER_ENTRY_URL, Integer.toString(i), be.getFullUrl()));
            } else {
                root.para().addText(formatMessage(RENDER_BUNDLE_HEADER_ENTRY, Integer.toString(i)));
            }
            if (be.hasRequest())
                renderRequest(root, be.getRequest());
            if (be.hasSearch())
                renderSearch(root, be.getSearch());
            if (be.hasResponse())
                renderResponse(root, be.getResponse());
            if (be.hasResource()) {
                root.para().addText(formatMessage(RENDER_BUNDLE_RESOURCE, be.getResource().fhirType()));
                if (be.hasResource()) {
                    XhtmlNode xn = null;
                    if (be.getResource() instanceof DomainResource) {
                        DomainResource dr = (DomainResource) be.getResource();
                        xn = dr.getText().getDiv();
                    }
                    if (xn == null || xn.isEmpty()) {
                        ResourceRenderer rr = RendererFactory.factory(be.getResource(), context);
                        try {
                            xn = rr.build(be.getResource());
                        } catch (Exception e) {
                            xn = makeExceptionXhtml(e, "generating narrative");
                        }
                    }
                    root.blockquote().para().getChildNodes().addAll(checkInternalLinks(b, xn.getChildNodes()));
                }
            }
        }
        return root;
    }
}
Also used : Composition(org.hl7.fhir.r5.model.Composition) BundleEntryComponent(org.hl7.fhir.r5.model.Bundle.BundleEntryComponent) DomainResource(org.hl7.fhir.r5.model.DomainResource) FHIRException(org.hl7.fhir.exceptions.FHIRException) DefinitionException(org.hl7.fhir.exceptions.DefinitionException) IOException(java.io.IOException) FHIRException(org.hl7.fhir.exceptions.FHIRException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) XhtmlNode(org.hl7.fhir.utilities.xhtml.XhtmlNode)

Aggregations

XhtmlNode (org.hl7.fhir.utilities.xhtml.XhtmlNode)24 FHIRException (org.hl7.fhir.exceptions.FHIRException)22 DomainResource (org.hl7.fhir.r4.model.DomainResource)21 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)16 IOException (java.io.IOException)15 DomainResource (org.hl7.fhir.dstu3.model.DomainResource)15 FileOutputStream (java.io.FileOutputStream)12 ArrayList (java.util.ArrayList)11 CommaSeparatedStringBuilder (org.hl7.fhir.utilities.CommaSeparatedStringBuilder)11 Test (org.junit.jupiter.api.Test)11 Resource (org.hl7.fhir.r4.model.Resource)10 IBaseResource (org.hl7.fhir.instance.model.api.IBaseResource)9 FileNotFoundException (java.io.FileNotFoundException)8 List (java.util.List)8 NotImplementedException (org.apache.commons.lang3.NotImplementedException)8 StructureDefinition (org.hl7.fhir.r5.model.StructureDefinition)8 SystemRequestDetails (ca.uhn.fhir.jpa.partition.SystemRequestDetails)7 File (java.io.File)7 ElementDefn (org.hl7.fhir.definitions.model.ElementDefn)7 RestIntegrationTest (org.opencds.cqf.ruler.test.RestIntegrationTest)7