From 61a39b60bb65df2c2dce472569e91a53d50fb630 Mon Sep 17 00:00:00 2001 From: Grahame Grieve Date: Tue, 9 Aug 2022 20:19:20 +1000 Subject: [PATCH] Bundle rendering improvements --- .../r5/renderers/ProfileDrivenRenderer.java | 36 +++++--- .../r5/renderers/utils/RenderingContext.java | 86 +++++++++++-------- .../hl7/fhir/r5/renderers/utils/Resolver.java | 3 + .../hl7/fhir/r5/utils/PackageHackerR5.java | 40 +++++++++ 4 files changed, 120 insertions(+), 45 deletions(-) create mode 100644 org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/utils/PackageHackerR5.java diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/renderers/ProfileDrivenRenderer.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/renderers/ProfileDrivenRenderer.java index 932b251b8..5dad32806 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/renderers/ProfileDrivenRenderer.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/renderers/ProfileDrivenRenderer.java @@ -66,6 +66,7 @@ import org.hl7.fhir.r5.renderers.utils.BaseWrappers.ResourceWrapper; import org.hl7.fhir.r5.renderers.utils.DOMWrappers.BaseWrapperElement; import org.hl7.fhir.r5.renderers.utils.DOMWrappers.ResourceWrapperElement; import org.hl7.fhir.r5.renderers.utils.DirectWrappers; +import org.hl7.fhir.r5.renderers.utils.ElementWrappers; import org.hl7.fhir.r5.renderers.utils.DirectWrappers.BaseWrapperDirect; import org.hl7.fhir.r5.renderers.utils.DirectWrappers.PropertyWrapperDirect; import org.hl7.fhir.r5.renderers.utils.DirectWrappers.ResourceWrapperDirect; @@ -106,7 +107,10 @@ public class ProfileDrivenRenderer extends ResourceRenderer { if (context.isAddGeneratedNarrativeHeader()) { x.para().b().tx("Generated Narrative: "+r.fhirType()); } - if (context.isTechnicalMode()) { + if (!Utilities.noString(r.getId())) { + x.an(r.getId()); + } + if (context.isTechnicalMode() && !context.isContained()) { renderResourceHeader(r, x); } try { @@ -384,7 +388,7 @@ public class ProfileDrivenRenderer extends ResourceRenderer { if (r.getReference() != null && r.getReference().contains("#")) { if (containedIds.contains(r.getReference().substring(1))) { x.ah(r.getReference()).tx("See "+r.getReference()); - } else { + } else { // in this case, we render the resource in line ResourceWrapper rw = null; for (ResourceWrapper t : res.getContained()) { @@ -395,9 +399,17 @@ public class ProfileDrivenRenderer extends ResourceRenderer { if (rw == null) { renderReference(res, x, r); } else { - x.an(rw.getId()); - ResourceRenderer rr = RendererFactory.factory(rw, context.copy().setAddGeneratedNarrativeHeader(false)); - rr.render(parent.blockquote(), rw); + String ref = context.getResolver().urlForContained(context, res.fhirType(), res.getId(), rw.fhirType(), rw.getId()); + if (ref == null) { + x.an(rw.getId()); + RenderingContext ctxtc = context.copy(); + ctxtc.setAddGeneratedNarrativeHeader(false); + ctxtc.setContained(true); + ResourceRenderer rr = RendererFactory.factory(rw, ctxtc); + rr.render(parent.blockquote(), rw); + } else { + x.ah(ref).tx("See "+rw.fhirType()); + } } } } else { @@ -687,11 +699,15 @@ public class ProfileDrivenRenderer extends ResourceRenderer { boolean showCodeDetails, int indent, PropertyWrapper p, ElementDefinition child) throws UnsupportedEncodingException, IOException, EOperationOutcome { Map displayHints = readDisplayHints(child); if ("DomainResource.contained".equals(child.getBase().getPath())) { -// if (p.getValues().size() > 0 && child != null) { -// for (BaseWrapper v : p.getValues()) { -// x.an(v.get("id").primitiveValue()); -// } -// } + System.out.print("-"); +// for (BaseWrapper v : p.getValues()) { +// x.hr(); +// RenderingContext ctxt = context.clone(); +// ctxt.setContained(true); +// ResourceRenderer rnd = RendererFactory.factory(v.fhirType(), ctxt); +// ResourceWrapper rw = new ElementWrappers.ResourceWrapperMetaElement(ctxt, (org.hl7.fhir.r5.elementmodel.Element) v.getBase()); +// rnd.render(x.blockquote(), rw); +// } } else if (!exemptFromRendering(child)) { if (isExtension(p)) { hasExtensions = true; diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/renderers/utils/RenderingContext.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/renderers/utils/RenderingContext.java index 3c8c78d04..9d0f99ceb 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/renderers/utils/RenderingContext.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/renderers/utils/RenderingContext.java @@ -105,6 +105,7 @@ public class RenderingContext { private boolean canonicalUrlsAsLinks; private boolean pretty; private boolean header; + private boolean contained; private ValidationOptions terminologyServiceOptions = new ValidationOptions(); private boolean noSlowLookup; @@ -153,11 +154,53 @@ public class RenderingContext { this.locale = new Locale.Builder().setLanguageTag("en-US").build(); profileUtilities = new ProfileUtilities(worker, null, null); } + public RenderingContext copy() { + RenderingContext res = new RenderingContext(worker, markdown, terminologyServiceOptions, specificationLink, localPrefix, lang, mode); + + res.resolver = resolver; + res.templateProvider = templateProvider; + res.services = services; + res.parser = parser; + + res.headerLevelContext = headerLevelContext; + res.canonicalUrlsAsLinks = canonicalUrlsAsLinks; + res.pretty = pretty; + res.contained = contained; + + res.noSlowLookup = noSlowLookup; + res.tooCostlyNoteEmpty = tooCostlyNoteEmpty; + res.tooCostlyNoteNotEmpty = tooCostlyNoteNotEmpty; + res.tooCostlyNoteEmptyDependent = tooCostlyNoteEmptyDependent; + res.tooCostlyNoteNotEmptyDependent = tooCostlyNoteNotEmptyDependent; + res.codeSystemPropList.addAll(codeSystemPropList); + + res.profileUtilities = profileUtilities; + res.definitionsTarget = definitionsTarget; + res.destDir = destDir; + res.addGeneratedNarrativeHeader = addGeneratedNarrativeHeader; + res.questionnaireMode = questionnaireMode; + res.header = header; + res.selfLink = selfLink; + res.inlineGraphics = inlineGraphics; + res.timeZoneId = timeZoneId; + res.dateTimeFormat = dateTimeFormat; + res.dateFormat = dateFormat; + res.dateYearFormat = dateYearFormat; + res.dateYearMonthFormat = dateYearMonthFormat; + res.targetVersion = targetVersion; + res.locale = locale; + + res.terminologyServiceOptions = terminologyServiceOptions.copy(); + return res; + } + public IWorkerContext getContext() { return worker; } + + // -- 2. Markdown support ------------------------------------------------------- public ProfileUtilities getProfileUtilities() { @@ -338,41 +381,6 @@ public class RenderingContext { return this; } - public RenderingContext copy() { - RenderingContext res = new RenderingContext(worker, markdown, terminologyServiceOptions, specificationLink, localPrefix, lang, mode); - - res.resolver = resolver; - res.templateProvider = templateProvider; - res.services = services; - res.parser = parser; - - res.headerLevelContext = headerLevelContext; - res.canonicalUrlsAsLinks = canonicalUrlsAsLinks; - res.pretty = pretty; - - res.noSlowLookup = noSlowLookup; - res.tooCostlyNoteEmpty = tooCostlyNoteEmpty; - res.tooCostlyNoteNotEmpty = tooCostlyNoteNotEmpty; - res.tooCostlyNoteEmptyDependent = tooCostlyNoteEmptyDependent; - res.tooCostlyNoteNotEmptyDependent = tooCostlyNoteNotEmptyDependent; - res.codeSystemPropList.addAll(codeSystemPropList); - - res.profileUtilities = profileUtilities; - res.definitionsTarget = definitionsTarget; - res.destDir = destDir; - res.addGeneratedNarrativeHeader = addGeneratedNarrativeHeader; - res.questionnaireMode = questionnaireMode; - res.header = header; - res.selfLink = selfLink; - res.inlineGraphics = inlineGraphics; - res.timeZoneId = timeZoneId; - res.dateTimeFormat = dateTimeFormat; - res.dateFormat = dateFormat; - res.dateYearFormat = dateYearFormat; - res.dateYearMonthFormat = dateYearMonthFormat; - - return res; - } public boolean isInlineGraphics() { return inlineGraphics; @@ -556,6 +564,14 @@ public class RenderingContext { public void setMode(ResourceRendererMode mode) { this.mode = mode; } + + public boolean isContained() { + return contained; + } + + public void setContained(boolean contained) { + this.contained = contained; + } } \ No newline at end of file diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/renderers/utils/Resolver.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/renderers/utils/Resolver.java index 13cbb5977..40b0b12c2 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/renderers/utils/Resolver.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/renderers/utils/Resolver.java @@ -18,6 +18,9 @@ public class Resolver { public interface IReferenceResolver { ResourceWithReference resolve(RenderingContext context, String url); + + // returns null if contained resource is inlined + String urlForContained(RenderingContext context, String containingType, String containingId, String containedType, String containedId); } public static class ResourceContext { diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/utils/PackageHackerR5.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/utils/PackageHackerR5.java new file mode 100644 index 000000000..356e37344 --- /dev/null +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/utils/PackageHackerR5.java @@ -0,0 +1,40 @@ +package org.hl7.fhir.r5.utils; + +import org.hl7.fhir.r5.context.CanonicalResourceManager.CanonicalResourceProxy; +import org.hl7.fhir.r5.context.IWorkerContext.PackageVersion; + +public class PackageHackerR5 { + + public static void fixLoadedResource(CanonicalResourceProxy r, PackageVersion packageInfo) { + if ("http://terminology.hl7.org/CodeSystem/v2-0391|2.6".equals(r.getUrl())) { + r.hack("http://terminology.hl7.org/CodeSystem/v2-0391-2.6", "2.6"); + } + if ("http://terminology.hl7.org/CodeSystem/v2-0391|2.4".equals(r.getUrl())) { + r.hack("http://terminology.hl7.org/CodeSystem/v2-0391-2.4", "2.4"); + } + if ("http://terminology.hl7.org/CodeSystem/v2-0360|2.7".equals(r.getUrl())) { + r.hack("http://terminology.hl7.org/CodeSystem/v2-0360-2.7", "2.7"); + } + + if ("http://terminology.hl7.org/CodeSystem/v2-0006|2.1".equals(r.getUrl())) { + r.hack("http://terminology.hl7.org/CodeSystem/v2-0006-2.1", "2.1"); + } + + if ("http://terminology.hl7.org/CodeSystem/v2-0360|2.7".equals(r.getUrl())) { + r.hack("http://terminology.hl7.org/CodeSystem/v2-0360-2.7", "2.7"); + } + + if ("http://terminology.hl7.org/CodeSystem/v2-0006|2.4".equals(r.getUrl())) { + r.hack("http://terminology.hl7.org/CodeSystem/v2-0006-2.4", "2.4"); + } + + if ("http://terminology.hl7.org/CodeSystem/v2-0360|2.3.1".equals(r.getUrl())) { + r.hack("http://terminology.hl7.org/CodeSystem/v2-0360-2.3.1", "2.3.1"); + } + + if (r.hasUrl() && r.getUrl().contains("|")) { + assert false; + } + } + +}