use of org.hl7.fhir.r5.renderers.utils.BaseWrappers.BaseWrapper 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().addChildren(xn);
}
}
}
return false;
}
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.dstu2.model.DateType) {
x.addText(name + ": " + ((org.hl7.fhir.dstu2.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.dstu2.model.IntegerType) {
x.addText(Integer.toString(((org.hl7.fhir.dstu2.model.IntegerType) e).getValue()));
return true;
} else if (e instanceof org.hl7.fhir.dstu2.model.DecimalType) {
x.addText(((org.hl7.fhir.dstu2.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;
}
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;
}
use of org.hl7.fhir.r5.renderers.utils.BaseWrappers.BaseWrapper in project org.hl7.fhir.core by hapifhir.
the class NarrativeGenerator method renderLeaf.
private void renderLeaf(ResourceWrapper res, BaseWrapper ew, ElementDefinition defn, XhtmlNode x, boolean title, boolean showCodeDetails, Map<String, String> displayHints, String path, int indent, ResourceContext rc) throws FHIRException, UnsupportedEncodingException, IOException {
if (ew == null)
return;
Base e = ew.getBase();
if (e instanceof StringType)
x.addText(((StringType) e).getValue());
else if (e instanceof CodeType)
x.addText(((CodeType) e).getValue());
else if (e instanceof IdType)
x.addText(((IdType) e).getValue());
else if (e instanceof Extension)
return;
else if (e instanceof InstantType)
x.addText(((InstantType) e).toHumanDisplay());
else if (e instanceof DateTimeType) {
if (e.hasPrimitiveValue())
x.addText(((DateTimeType) e).toHumanDisplay());
} else if (e instanceof Base64BinaryType)
x.addText(new Base64().encodeAsString(((Base64BinaryType) e).getValue()));
else if (e instanceof org.hl7.fhir.r4.model.DateType)
x.addText(((org.hl7.fhir.r4.model.DateType) e).toHumanDisplay());
else if (e instanceof Enumeration) {
Object ev = ((Enumeration<?>) e).getValue();
// todo: look up a display name if there is one
x.addText(ev == null ? "" : ev.toString());
} else if (e instanceof BooleanType)
x.addText(((BooleanType) e).getValue().toString());
else if (e instanceof CodeableConcept) {
renderCodeableConcept((CodeableConcept) e, x, showCodeDetails);
} else if (e instanceof Coding) {
renderCoding((Coding) e, x, showCodeDetails);
} else if (e instanceof Annotation) {
renderAnnotation((Annotation) e, x);
} else if (e instanceof Identifier) {
renderIdentifier((Identifier) e, x);
} else if (e instanceof org.hl7.fhir.r4.model.IntegerType) {
x.addText(Integer.toString(((org.hl7.fhir.r4.model.IntegerType) e).getValue()));
} else if (e instanceof org.hl7.fhir.r4.model.DecimalType) {
x.addText(((org.hl7.fhir.r4.model.DecimalType) e).getValue().toString());
} else if (e instanceof HumanName) {
renderHumanName((HumanName) e, x);
} else if (e instanceof SampledData) {
renderSampledData((SampledData) e, x);
} else if (e instanceof Address) {
renderAddress((Address) e, x);
} else if (e instanceof ContactPoint) {
renderContactPoint((ContactPoint) e, x);
} else if (e instanceof UriType) {
renderUri((UriType) e, x, defn.getPath(), rc != null && rc.resourceResource != null ? rc.resourceResource.getId() : null);
} else if (e instanceof Timing) {
renderTiming((Timing) e, x);
} else if (e instanceof Range) {
renderRange((Range) e, x);
} else if (e instanceof Quantity) {
renderQuantity((Quantity) e, x, showCodeDetails);
} else if (e instanceof Ratio) {
renderQuantity(((Ratio) e).getNumerator(), x, showCodeDetails);
x.tx("/");
renderQuantity(((Ratio) e).getDenominator(), x, showCodeDetails);
} else if (e instanceof Period) {
Period p = (Period) e;
x.addText(!p.hasStart() ? "??" : p.getStartElement().toHumanDisplay());
x.tx(" --> ");
x.addText(!p.hasEnd() ? "(ongoing)" : p.getEndElement().toHumanDisplay());
} else if (e instanceof Reference) {
Reference r = (Reference) e;
XhtmlNode c = x;
ResourceWithReference tr = null;
if (r.hasReferenceElement()) {
tr = resolveReference(res, r.getReference(), rc);
if (!r.getReference().startsWith("#")) {
if (tr != null && tr.getReference() != null)
c = x.ah(tr.getReference());
else
c = x.ah(r.getReference());
}
}
// what to display: if text is provided, then that. if the reference was resolved, then show the generated narrative
if (r.hasDisplayElement()) {
c.addText(r.getDisplay());
if (tr != null && tr.getResource() != null) {
c.tx(". Generated Summary: ");
generateResourceSummary(c, tr.getResource(), true, r.getReference().startsWith("#"), rc);
}
} else if (tr != null && tr.getResource() != null) {
generateResourceSummary(c, tr.getResource(), r.getReference().startsWith("#"), r.getReference().startsWith("#"), rc);
} else {
c.addText(r.getReference());
}
} else if (e instanceof Resource) {
return;
} else if (e instanceof ElementDefinition) {
x.tx("todo-bundle");
} else if (e != null && !(e instanceof Attachment) && !(e instanceof Narrative) && !(e instanceof Meta)) {
StructureDefinition sd = context.fetchTypeDefinition(e.fhirType());
if (sd == null)
throw new NotImplementedException("type " + e.getClass().getName() + " not handled yet, and no structure found");
else
generateByProfile(res, sd, ew, sd.getSnapshot().getElement(), sd.getSnapshot().getElementFirstRep(), getChildrenForPath(sd.getSnapshot().getElement(), sd.getSnapshot().getElementFirstRep().getPath()), x, path, showCodeDetails, indent + 1, rc);
}
}
use of org.hl7.fhir.r5.renderers.utils.BaseWrappers.BaseWrapper in project org.hl7.fhir.core by hapifhir.
the class ResourceRenderer method fetchResource.
protected ResourceWrapper fetchResource(BaseWrapper subject) throws UnsupportedEncodingException, FHIRException, IOException {
if (context.getResolver() == null)
return null;
PropertyWrapper ref = subject.getChildByName("reference");
if (ref == null || !ref.hasValues()) {
return null;
}
String url = ref.value().getBase().primitiveValue();
ResourceWithReference rr = context.getResolver().resolve(context, url);
return rr == null ? null : rr.getResource();
}
Aggregations