Search in sources :

Example 66 with BaseWrapper

use of org.hl7.fhir.r5.renderers.utils.BaseWrappers.BaseWrapper in project org.hl7.fhir.core by hapifhir.

the class NarrativeGenerator method splitExtensions.

private List<PropertyWrapper> splitExtensions(StructureDefinition profile, List<PropertyWrapper> children) throws UnsupportedEncodingException, IOException, FHIRException {
    List<PropertyWrapper> results = new ArrayList<PropertyWrapper>();
    Map<String, PropertyWrapper> map = new HashMap<String, PropertyWrapper>();
    for (PropertyWrapper p : children) if (p.getName().equals("extension") || p.getName().equals("modifierExtension")) {
        // we're going to split these up, and create a property for each url
        if (p.hasValues()) {
            for (BaseWrapper v : p.getValues()) {
                Extension ex = (Extension) v.getBase();
                String url = ex.getUrl();
                StructureDefinition ed = context.fetchResource(StructureDefinition.class, url);
                if (p.getName().equals("modifierExtension") && ed == null)
                    throw new DefinitionException("Unknown modifier extension " + url);
                PropertyWrapper pe = map.get(p.getName() + "[" + url + "]");
                if (pe == null) {
                    if (ed == null) {
                        if (url.startsWith("http://hl7.org/fhir"))
                            throw new DefinitionException("unknown extension " + url);
                        System.out.println("unknown extension " + url);
                        pe = new PropertyWrapperDirect(new Property(p.getName() + "[" + url + "]", p.getTypeCode(), p.getDefinition(), p.getMinCardinality(), p.getMaxCardinality(), ex));
                    } else {
                        ElementDefinition def = ed.getSnapshot().getElement().get(0);
                        pe = new PropertyWrapperDirect(new Property(p.getName() + "[" + url + "]", "Extension", def.getDefinition(), def.getMin(), def.getMax().equals("*") ? Integer.MAX_VALUE : Integer.parseInt(def.getMax()), ex));
                        ((PropertyWrapperDirect) pe).wrapped.setStructure(ed);
                    }
                    results.add(pe);
                } else
                    pe.getValues().add(v);
            }
        }
    } else
        results.add(p);
    return results;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Extension(org.hl7.fhir.dstu2016may.model.Extension) StructureDefinition(org.hl7.fhir.dstu2016may.model.StructureDefinition) DefinitionException(org.hl7.fhir.exceptions.DefinitionException) ElementDefinition(org.hl7.fhir.dstu2016may.model.ElementDefinition) Property(org.hl7.fhir.dstu2016may.model.Property)

Example 67 with BaseWrapper

use of org.hl7.fhir.r5.renderers.utils.BaseWrappers.BaseWrapper in project org.hl7.fhir.core by hapifhir.

the class NarrativeGenerator method generateByProfile.

private void generateByProfile(ResourceWrapper res, StructureDefinition profile, BaseWrapper e, List<ElementDefinition> allElements, ElementDefinition defn, List<ElementDefinition> children, XhtmlNode x, String path, boolean showCodeDetails) throws FHIRException, UnsupportedEncodingException, IOException {
    if (children.isEmpty()) {
        renderLeaf(res, e, defn, x, false, showCodeDetails, readDisplayHints(defn));
    } else {
        for (PropertyWrapper p : splitExtensions(profile, e.children())) {
            if (p.hasValues()) {
                ElementDefinition child = getElementDefinition(children, path + "." + p.getName(), p);
                if (child != null) {
                    Map<String, String> displayHints = readDisplayHints(child);
                    if (!exemptFromRendering(child)) {
                        List<ElementDefinition> grandChildren = getChildrenForPath(allElements, path + "." + p.getName());
                        filterGrandChildren(grandChildren, path + "." + p.getName(), p);
                        if (p.getValues().size() > 0 && child != null) {
                            if (isPrimitive(child)) {
                                XhtmlNode para = x.addTag("p");
                                String name = p.getName();
                                if (name.endsWith("[x]"))
                                    name = name.substring(0, name.length() - 3);
                                if (showCodeDetails || !isDefaultValue(displayHints, p.getValues())) {
                                    para.addTag("b").addText(name);
                                    para.addText(": ");
                                    if (renderAsList(child) && p.getValues().size() > 1) {
                                        XhtmlNode list = x.addTag("ul");
                                        for (BaseWrapper v : p.getValues()) renderLeaf(res, v, child, list.addTag("li"), false, showCodeDetails, displayHints);
                                    } else {
                                        boolean first = true;
                                        for (BaseWrapper v : p.getValues()) {
                                            if (first)
                                                first = false;
                                            else
                                                para.addText(", ");
                                            renderLeaf(res, v, child, para, false, showCodeDetails, displayHints);
                                        }
                                    }
                                }
                            } else if (canDoTable(path, p, grandChildren)) {
                                x.addTag("h3").addText(Utilities.capitalize(Utilities.camelCase(Utilities.pluralizeMe(p.getName()))));
                                XhtmlNode tbl = x.addTag("table").setAttribute("class", "grid");
                                XhtmlNode tr = tbl.addTag("tr");
                                // work around problem with empty table rows
                                tr.addTag("td").addText("-");
                                addColumnHeadings(tr, grandChildren);
                                for (BaseWrapper v : p.getValues()) {
                                    if (v != null) {
                                        tr = tbl.addTag("tr");
                                        // work around problem with empty table rows
                                        tr.addTag("td").addText("*");
                                        addColumnValues(res, tr, grandChildren, v, showCodeDetails, displayHints);
                                    }
                                }
                            } else {
                                for (BaseWrapper v : p.getValues()) {
                                    if (v != null) {
                                        XhtmlNode bq = x.addTag("blockquote");
                                        bq.addTag("p").addTag("b").addText(p.getName());
                                        generateByProfile(res, profile, v, allElements, child, grandChildren, bq, path + "." + p.getName(), showCodeDetails);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : ElementDefinition(org.hl7.fhir.dstu2016may.model.ElementDefinition) XhtmlNode(org.hl7.fhir.utilities.xhtml.XhtmlNode)

Example 68 with BaseWrapper

use of org.hl7.fhir.r5.renderers.utils.BaseWrappers.BaseWrapper in project org.hl7.fhir.core by hapifhir.

the class NarrativeGenerator method generateResourceSummary.

private void generateResourceSummary(XhtmlNode x, ResourceWrapper res, boolean textAlready, boolean showCodeDetails) throws FHIRException, UnsupportedEncodingException, IOException {
    if (!textAlready) {
        XhtmlNode div = res.getNarrative();
        if (div != null) {
            if (div.allChildrenAreText())
                x.getChildNodes().addAll(div.getChildNodes());
            if (div.getChildNodes().size() == 1 && div.getChildNodes().get(0).allChildrenAreText())
                x.getChildNodes().addAll(div.getChildNodes().get(0).getChildNodes());
        }
        x.addText("Generated Summary: ");
    }
    String path = res.getName();
    StructureDefinition profile = context.fetchResource(StructureDefinition.class, path);
    if (profile == null)
        x.addText("unknown resource " + path);
    else {
        boolean firstElement = true;
        boolean last = false;
        for (PropertyWrapper p : res.children()) {
            ElementDefinition child = getElementDefinition(profile.getSnapshot().getElement(), path + "." + p.getName(), p);
            if (p.getValues().size() > 0 && p.getValues().get(0) != null && child != null && isPrimitive(child) && includeInSummary(child)) {
                if (firstElement)
                    firstElement = false;
                else if (last)
                    x.addText("; ");
                boolean first = true;
                last = false;
                for (BaseWrapper v : p.getValues()) {
                    if (first)
                        first = false;
                    else if (last)
                        x.addText(", ");
                    last = displayLeaf(res, v, child, x, p.getName(), showCodeDetails) || last;
                }
            }
        }
    }
}
Also used : StructureDefinition(org.hl7.fhir.dstu2016may.model.StructureDefinition) ElementDefinition(org.hl7.fhir.dstu2016may.model.ElementDefinition) XhtmlNode(org.hl7.fhir.utilities.xhtml.XhtmlNode)

Example 69 with BaseWrapper

use of org.hl7.fhir.r5.renderers.utils.BaseWrappers.BaseWrapper in project org.hl7.fhir.core by hapifhir.

the class NarrativeGenerator method displayLeaf.

private boolean displayLeaf(ResourceWrapper res, BaseWrapper ew, ElementDefinition defn, XhtmlNode x, String name, boolean showCodeDetails) throws FHIRException, UnsupportedEncodingException, IOException {
    if (ew == null)
        return false;
    Base e = ew.getBase();
    Map<String, String> displayHints = readDisplayHints(defn);
    if (name.endsWith("[x]"))
        name = name.substring(0, name.length() - 3);
    if (!showCodeDetails && e instanceof PrimitiveType && isDefault(displayHints, ((PrimitiveType) e)))
        return false;
    if (e instanceof StringType) {
        x.addText(name + ": " + ((StringType) e).getValue());
        return true;
    } else if (e instanceof CodeType) {
        x.addText(name + ": " + ((CodeType) e).getValue());
        return true;
    } else if (e instanceof IdType) {
        x.addText(name + ": " + ((IdType) e).getValue());
        return true;
    } else if (e instanceof DateTimeType) {
        x.addText(name + ": " + ((DateTimeType) e).toHumanDisplay());
        return true;
    } else if (e instanceof InstantType) {
        x.addText(name + ": " + ((InstantType) e).toHumanDisplay());
        return true;
    } else if (e instanceof Extension) {
        x.addText("Extensions: todo");
        return true;
    } else if (e instanceof org.hl7.fhir.dstu2016may.model.DateType) {
        x.addText(name + ": " + ((org.hl7.fhir.dstu2016may.model.DateType) e).toHumanDisplay());
        return true;
    } else if (e instanceof Enumeration) {
        // todo: look up a display name if there is one
        x.addText(((Enumeration<?>) e).getValue().toString());
        return true;
    } else if (e instanceof BooleanType) {
        if (((BooleanType) e).getValue()) {
            x.addText(name);
            return true;
        }
    } else if (e instanceof CodeableConcept) {
        renderCodeableConcept((CodeableConcept) e, x, showCodeDetails);
        return true;
    } else if (e instanceof Coding) {
        renderCoding((Coding) e, x, showCodeDetails);
        return true;
    } else if (e instanceof Annotation) {
        renderAnnotation((Annotation) e, x, showCodeDetails);
        return true;
    } else if (e instanceof org.hl7.fhir.dstu2016may.model.IntegerType) {
        x.addText(Integer.toString(((org.hl7.fhir.dstu2016may.model.IntegerType) e).getValue()));
        return true;
    } else if (e instanceof org.hl7.fhir.dstu2016may.model.DecimalType) {
        x.addText(((org.hl7.fhir.dstu2016may.model.DecimalType) e).getValue().toString());
        return true;
    } else if (e instanceof Identifier) {
        renderIdentifier((Identifier) e, x);
        return true;
    } else if (e instanceof HumanName) {
        renderHumanName((HumanName) e, x);
        return true;
    } else if (e instanceof SampledData) {
        renderSampledData((SampledData) e, x);
        return true;
    } else if (e instanceof Address) {
        renderAddress((Address) e, x);
        return true;
    } else if (e instanceof ContactPoint) {
        renderContactPoint((ContactPoint) e, x);
        return true;
    } else if (e instanceof Timing) {
        renderTiming((Timing) e, x);
        return true;
    } else if (e instanceof Quantity) {
        renderQuantity((Quantity) e, x, showCodeDetails);
        return true;
    } else if (e instanceof Ratio) {
        renderQuantity(((Ratio) e).getNumerator(), x, showCodeDetails);
        x.addText("/");
        renderQuantity(((Ratio) e).getDenominator(), x, showCodeDetails);
        return true;
    } else if (e instanceof Period) {
        Period p = (Period) e;
        x.addText(name + ": ");
        x.addText(!p.hasStart() ? "??" : p.getStartElement().toHumanDisplay());
        x.addText(" --> ");
        x.addText(!p.hasEnd() ? "(ongoing)" : p.getEndElement().toHumanDisplay());
        return true;
    } else if (e instanceof Reference) {
        Reference r = (Reference) e;
        if (r.hasDisplayElement())
            x.addText(r.getDisplay());
        else if (r.hasReferenceElement()) {
            ResourceWithReference tr = resolveReference(res, r.getReference());
            // getDisplayForReference(tr.getReference()));
            x.addText(tr == null ? r.getReference() : "????");
        } else
            x.addText("??");
        return true;
    } else if (e instanceof Narrative) {
        return false;
    } else if (e instanceof Resource) {
        return false;
    } else if (!(e instanceof Attachment))
        throw new NotImplementedException("type " + e.getClass().getName() + " not handled yet");
    return false;
}
Also used : Address(org.hl7.fhir.dstu2016may.model.Address) StringType(org.hl7.fhir.dstu2016may.model.StringType) NotImplementedException(org.apache.commons.lang3.NotImplementedException) Attachment(org.hl7.fhir.dstu2016may.model.Attachment) HumanName(org.hl7.fhir.dstu2016may.model.HumanName) ContactPoint(org.hl7.fhir.dstu2016may.model.ContactPoint) Identifier(org.hl7.fhir.dstu2016may.model.Identifier) Coding(org.hl7.fhir.dstu2016may.model.Coding) Narrative(org.hl7.fhir.dstu2016may.model.Narrative) SampledData(org.hl7.fhir.dstu2016may.model.SampledData) PrimitiveType(org.hl7.fhir.dstu2016may.model.PrimitiveType) Ratio(org.hl7.fhir.dstu2016may.model.Ratio) InstantType(org.hl7.fhir.dstu2016may.model.InstantType) Enumeration(org.hl7.fhir.dstu2016may.model.Enumeration) Reference(org.hl7.fhir.dstu2016may.model.Reference) BooleanType(org.hl7.fhir.dstu2016may.model.BooleanType) Resource(org.hl7.fhir.dstu2016may.model.Resource) DomainResource(org.hl7.fhir.dstu2016may.model.DomainResource) Quantity(org.hl7.fhir.dstu2016may.model.Quantity) Period(org.hl7.fhir.dstu2016may.model.Period) Base(org.hl7.fhir.dstu2016may.model.Base) Annotation(org.hl7.fhir.dstu2016may.model.Annotation) IdType(org.hl7.fhir.dstu2016may.model.IdType) Extension(org.hl7.fhir.dstu2016may.model.Extension) DateTimeType(org.hl7.fhir.dstu2016may.model.DateTimeType) CodeType(org.hl7.fhir.dstu2016may.model.CodeType) EventTiming(org.hl7.fhir.dstu2016may.model.Timing.EventTiming) Timing(org.hl7.fhir.dstu2016may.model.Timing) CodeableConcept(org.hl7.fhir.dstu2016may.model.CodeableConcept)

Example 70 with BaseWrapper

use of org.hl7.fhir.r5.renderers.utils.BaseWrappers.BaseWrapper in project org.hl7.fhir.core by hapifhir.

the class PatientRenderer method display.

@Override
public String display(ResourceWrapper pat) throws UnsupportedEncodingException, IOException {
    Identifier id = null;
    PropertyWrapper pw = getProperty(pat, "identifier");
    for (BaseWrapper t : pw.getValues()) {
        id = chooseId(id, (Identifier) t.getBase());
    }
    pw = getProperty(pat, "name");
    HumanName n = null;
    for (BaseWrapper t : pw.getValues()) {
        n = chooseName(n, (HumanName) t);
    }
    String gender = null;
    pw = getProperty(pat, "gender");
    if (valued(pw)) {
        pw.value().getBase().primitiveValue();
    }
    DateType dt = null;
    pw = getProperty(pat, "birthDate");
    if (valued(pw)) {
        dt = (DateType) pw.value().getBase();
    }
    return display(n, gender, dt, id);
}
Also used : PropertyWrapper(org.hl7.fhir.r4b.renderers.utils.BaseWrappers.PropertyWrapper) HumanName(org.hl7.fhir.r4b.model.HumanName) BaseWrapper(org.hl7.fhir.r4b.renderers.utils.BaseWrappers.BaseWrapper) Identifier(org.hl7.fhir.r4b.model.Identifier) DateType(org.hl7.fhir.r4b.model.DateType)

Aggregations

XhtmlNode (org.hl7.fhir.utilities.xhtml.XhtmlNode)48 BaseWrapper (org.hl7.fhir.r4b.renderers.utils.BaseWrappers.BaseWrapper)20 BaseWrapper (org.hl7.fhir.r5.renderers.utils.BaseWrappers.BaseWrapper)20 PropertyWrapper (org.hl7.fhir.r4b.renderers.utils.BaseWrappers.PropertyWrapper)15 PropertyWrapper (org.hl7.fhir.r5.renderers.utils.BaseWrappers.PropertyWrapper)15 ArrayList (java.util.ArrayList)14 NotImplementedException (org.apache.commons.lang3.NotImplementedException)11 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)7 ElementDefinition (org.hl7.fhir.r4b.model.ElementDefinition)7 ElementDefinition (org.hl7.fhir.r5.model.ElementDefinition)7 Base64 (org.apache.commons.codec.binary.Base64)6 ElementDefinition (org.hl7.fhir.dstu3.model.ElementDefinition)6 HashMap (java.util.HashMap)5 ElementDefinition (org.hl7.fhir.dstu2016may.model.ElementDefinition)5 ResourceWrapper (org.hl7.fhir.r4b.renderers.utils.BaseWrappers.ResourceWrapper)5 ResourceWrapper (org.hl7.fhir.r5.renderers.utils.BaseWrappers.ResourceWrapper)5 ElementDefinition (org.hl7.fhir.dstu2.model.ElementDefinition)4 StructureDefinition (org.hl7.fhir.dstu3.model.StructureDefinition)3 Base (org.hl7.fhir.r4b.model.Base)3 Extension (org.hl7.fhir.r4b.model.Extension)3