Search in sources :

Example 36 with EOperationOutcome

use of org.hl7.fhir.r5.utils.EOperationOutcome 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 37 with EOperationOutcome

use of org.hl7.fhir.r5.utils.EOperationOutcome in project org.hl7.fhir.core by hapifhir.

the class BundleRenderer method render.

@Override
public boolean render(XhtmlNode x, Resource r) throws FHIRFormatError, DefinitionException, IOException, FHIRException, EOperationOutcome {
    XhtmlNode n = render((Bundle) r);
    x.addChildren(n.getChildNodes());
    return false;
}
Also used : XhtmlNode(org.hl7.fhir.utilities.xhtml.XhtmlNode)

Example 38 with EOperationOutcome

use of org.hl7.fhir.r5.utils.EOperationOutcome in project org.hl7.fhir.core by hapifhir.

the class BundleRenderer method renderDocument.

private boolean renderDocument(XhtmlNode x, ResourceWrapper b, List<BaseWrapper> entries) throws UnsupportedEncodingException, FHIRException, IOException, EOperationOutcome {
    // from the spec:
    // 
    // When the document is presented for human consumption, applications SHOULD present the collated narrative portions in order:
    // * The subject resource Narrative
    // * The Composition resource Narrative
    // * The section.text Narratives
    ResourceWrapper comp = (ResourceWrapper) entries.get(0).getChildByName("resource").getAsResource();
    ResourceWrapper subject = resolveReference(entries, comp.get("subject"));
    if (subject != null) {
        if (subject.hasNarrative()) {
            x.addChildren(subject.getNarrative());
        } else {
            RendererFactory.factory(subject, context).render(x, subject);
        }
    }
    x.hr();
    if (comp.hasNarrative()) {
        x.addChildren(comp.getNarrative());
        x.hr();
    }
    List<BaseWrapper> sections = comp.children("section");
    for (BaseWrapper section : sections) {
        addSection(x, section, 2, false);
    }
    return false;
}
Also used : ResourceWrapper(org.hl7.fhir.r5.renderers.utils.BaseWrappers.ResourceWrapper) BaseWrapper(org.hl7.fhir.r5.renderers.utils.BaseWrappers.BaseWrapper)

Example 39 with EOperationOutcome

use of org.hl7.fhir.r5.utils.EOperationOutcome 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)

Example 40 with EOperationOutcome

use of org.hl7.fhir.r5.utils.EOperationOutcome in project org.hl7.fhir.core by hapifhir.

the class BundleRenderer method render.

@Override
public boolean render(XhtmlNode x, ResourceWrapper b) throws FHIRFormatError, DefinitionException, IOException, FHIRException, EOperationOutcome {
    List<BaseWrapper> entries = b.children("entry");
    if ("document".equals(b.get("type").primitiveValue())) {
        if (entries.isEmpty() || (entries.get(0).has("resource") && !"Composition".equals(entries.get(0).get("resource").fhirType())))
            throw new FHIRException("Invalid document '" + b.getId() + "' - first entry is not a Composition ('" + entries.get(0).get("resource").fhirType() + "')");
        return renderDocument(x, b, entries);
    } else if ("collection".equals(b.get("type").primitiveValue()) && allEntriesAreHistoryProvenance(entries)) {
    // nothing
    } else {
        XhtmlNode root = new XhtmlNode(NodeType.Element, "div");
        root.para().addText(formatMessage(RENDER_BUNDLE_HEADER_ROOT, b.getId(), b.get("type").primitiveValue()));
        int i = 0;
        for (BaseWrapper be : entries) {
            i++;
            if (be.has("fullUrl")) {
                root.an(makeInternalBundleLink(be.get("fullUrl").primitiveValue()));
            }
            if (be.has("resource") && be.getChildByName("resource").getValues().get(0).has("id")) {
                root.an(be.get("resource").fhirType() + "_" + be.getChildByName("resource").getValues().get(0).get("id").primitiveValue());
            }
            root.hr();
            if (be.has("fullUrl")) {
                root.para().addText(formatMessage(RENDER_BUNDLE_HEADER_ENTRY_URL, Integer.toString(i), be.get("fullUrl").primitiveValue()));
            } else {
                root.para().addText(formatMessage(RENDER_BUNDLE_HEADER_ENTRY, Integer.toString(i)));
            }
            // renderResponse(root, be.getResponse());
            if (be.has("resource")) {
                root.para().addText(formatMessage(RENDER_BUNDLE_RESOURCE, be.get("resource").fhirType()));
                ResourceWrapper rw = be.getChildByName("resource").getAsResource();
                XhtmlNode xn = rw.getNarrative();
                if (xn == null || xn.isEmpty()) {
                    ResourceRenderer rr = RendererFactory.factory(rw, context);
                    try {
                        xn = rr.render(rw);
                    } catch (Exception e) {
                        xn = new XhtmlNode();
                        xn.para().b().tx("Exception generating narrative: " + e.getMessage());
                    }
                }
                root.blockquote().para().addChildren(xn);
            }
        }
    }
    return false;
}
Also used : ResourceWrapper(org.hl7.fhir.r5.renderers.utils.BaseWrappers.ResourceWrapper) BaseWrapper(org.hl7.fhir.r5.renderers.utils.BaseWrappers.BaseWrapper) 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)52 FHIRException (org.hl7.fhir.exceptions.FHIRException)38 ValidationMessage (org.hl7.fhir.utilities.validation.ValidationMessage)37 ArrayList (java.util.ArrayList)35 ProfileUtilities (org.hl7.fhir.dstu3.conformance.ProfileUtilities)14 ElementDefinition (org.hl7.fhir.dstu2.model.ElementDefinition)13 StructureDefinition (org.hl7.fhir.dstu2.model.StructureDefinition)13 ProfileUtilities (org.hl7.fhir.dstu2.utils.ProfileUtilities)13 ElementDefinition (org.hl7.fhir.dstu3.model.ElementDefinition)13 StructureDefinition (org.hl7.fhir.dstu3.model.StructureDefinition)13 FileOutputStream (java.io.FileOutputStream)10 Test (org.junit.jupiter.api.Test)10 IOException (java.io.IOException)9 StructureDefinition (org.hl7.fhir.r5.model.StructureDefinition)9 DomainResource (org.hl7.fhir.r5.model.DomainResource)7 DomainResource (org.hl7.fhir.r4b.model.DomainResource)6 StructureDefinition (org.hl7.fhir.r4b.model.StructureDefinition)6 BaseWrapper (org.hl7.fhir.r4b.renderers.utils.BaseWrappers.BaseWrapper)6 BaseWrapper (org.hl7.fhir.r5.renderers.utils.BaseWrappers.BaseWrapper)6 ElementDefinition (org.hl7.fhir.r4b.model.ElementDefinition)5