Merge pull request #943 from hapifhir/gg-202210-bundle-links

Fix problems in links in bundle rendering
This commit is contained in:
Grahame Grieve 2022-10-06 23:15:18 +11:00 committed by GitHub
commit 58b38cd5af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 131 additions and 70 deletions

View File

@ -79,8 +79,13 @@ public class BundleRenderer extends ResourceRenderer {
if (be.has("fullUrl")) { if (be.has("fullUrl")) {
root.an(makeInternalBundleLink(be.get("fullUrl").primitiveValue())); root.an(makeInternalBundleLink(be.get("fullUrl").primitiveValue()));
} }
if (be.has("resource") && be.getChildByName("resource").getValues().get(0).has("id")) { if (be.has("resource")) {
if (be.getChildByName("resource").getValues().get(0).has("id")) {
root.an(be.get("resource").fhirType() + "_" + be.getChildByName("resource").getValues().get(0).get("id").primitiveValue()); root.an(be.get("resource").fhirType() + "_" + be.getChildByName("resource").getValues().get(0).get("id").primitiveValue());
} else {
String id = makeIdFromBundleEntry(be.get("fullUrl").primitiveValue());
root.an(be.get("resource").fhirType() + "_" + id);
}
} }
root.hr(); root.hr();
if (be.has("fullUrl")) { if (be.has("fullUrl")) {
@ -101,6 +106,7 @@ public class BundleRenderer extends ResourceRenderer {
if (xn == null || xn.isEmpty()) { if (xn == null || xn.isEmpty()) {
ResourceRenderer rr = RendererFactory.factory(rw, context); ResourceRenderer rr = RendererFactory.factory(rw, context);
try { try {
rr.setRcontext(new ResourceContext(rcontext, rw));
xn = rr.render(rw); xn = rr.render(rw);
} catch (Exception e) { } catch (Exception e) {
xn = new XhtmlNode(); xn = new XhtmlNode();
@ -128,7 +134,7 @@ public class BundleRenderer extends ResourceRenderer {
if (subject.hasNarrative()) { if (subject.hasNarrative()) {
x.addChildren(subject.getNarrative()); x.addChildren(subject.getNarrative());
} else { } else {
RendererFactory.factory(subject, context).render(x, subject); RendererFactory.factory(subject, context, new ResourceContext(rcontext, subject)).render(x, subject);
} }
} }
x.hr(); x.hr();
@ -204,12 +210,14 @@ public class BundleRenderer extends ResourceRenderer {
if (nx != null && !nx.isEmpty()) { if (nx != null && !nx.isEmpty()) {
x.addChildren(nx); x.addChildren(nx);
} else { } else {
RendererFactory.factory(subject, context).render(x, subject); RendererFactory.factory(subject, context).setRcontext(new ResourceContext(rcontext, subject)).render(x, subject);
} }
} }
x.hr(); x.hr();
if (!comp.getText().hasDiv()) { if (!comp.getText().hasDiv()) {
ResourceRenderer rr = RendererFactory.factory(comp, getContext()); rr.render(comp); ResourceRenderer rr = RendererFactory.factory(comp, getContext());
rr.setRcontext(new ResourceContext(rcontext, comp));
rr.render(comp);
} }
if (comp.getText().hasDiv()) { if (comp.getText().hasDiv()) {
x.addChildren(comp.getText().getDiv()); x.addChildren(comp.getText().getDiv());
@ -292,8 +300,14 @@ public class BundleRenderer extends ResourceRenderer {
if (i > start) { if (i > start) {
if (be.hasFullUrl()) if (be.hasFullUrl())
x.an(makeInternalBundleLink(be.getFullUrl())); x.an(makeInternalBundleLink(be.getFullUrl()));
if (be.hasResource() && be.getResource().hasId()) if (be.hasResource()) {
if (be.getResource().hasId()) {
x.an(be.getResource().getResourceType().name() + "_" + be.getResource().getId()); x.an(be.getResource().getResourceType().name() + "_" + be.getResource().getId());
} else {
String id = makeIdFromBundleEntry(be.getFullUrl());
x.an(be.getResource().getResourceType().name() + "_" + id);
}
}
x.hr(); x.hr();
if (docMode) { if (docMode) {
if (be.hasFullUrl() && be.hasResource()) { if (be.hasFullUrl() && be.hasResource()) {
@ -329,6 +343,7 @@ public class BundleRenderer extends ResourceRenderer {
if (xn == null || xn.isEmpty()) { if (xn == null || xn.isEmpty()) {
ResourceRenderer rr = RendererFactory.factory(be.getResource(), context); ResourceRenderer rr = RendererFactory.factory(be.getResource(), context);
try { try {
rr.setRcontext(new ResourceContext(rcontext, be.getResource()));
xn = rr.build(be.getResource()); xn = rr.build(be.getResource());
} catch (Exception e) { } catch (Exception e) {
xn = makeExceptionXhtml(e, "generating narrative"); xn = makeExceptionXhtml(e, "generating narrative");

View File

@ -381,7 +381,7 @@ public class ProfileDrivenRenderer extends ResourceRenderer {
renderContactPoint(x, c); renderContactPoint(x, c);
} }
} else if (e instanceof UriType) { } else if (e instanceof UriType) {
renderUri(x, (UriType) e, defn.getPath(), rcontext != null && rcontext.getResourceResource() != null ? rcontext.getResourceResource().getId() : null); renderUri(x, (UriType) e, defn.getPath(), rcontext != null && rcontext.getResource() != null ? rcontext.getResource().getId() : null);
} else if (e instanceof Timing) { } else if (e instanceof Timing) {
renderTiming(x, (Timing) e); renderTiming(x, (Timing) e);
} else if (e instanceof Range) { } else if (e instanceof Range) {
@ -418,6 +418,7 @@ public class ProfileDrivenRenderer extends ResourceRenderer {
ctxtc.setAddGeneratedNarrativeHeader(false); ctxtc.setAddGeneratedNarrativeHeader(false);
ctxtc.setContained(true); ctxtc.setContained(true);
ResourceRenderer rr = RendererFactory.factory(rw, ctxtc); ResourceRenderer rr = RendererFactory.factory(rw, ctxtc);
rr.setRcontext(new ResourceContext(rcontext, rw));
rr.render(parent.blockquote(), rw); rr.render(parent.blockquote(), rw);
} else { } else {
x.ah(ref).tx("See "+rw.fhirType()); x.ah(ref).tx("See "+rw.fhirType());
@ -690,12 +691,12 @@ public class ProfileDrivenRenderer extends ResourceRenderer {
x.para().b().tx("Generated Narrative: "+profile.present()+(showCodeDetails ? " with Details" : "")); x.para().b().tx("Generated Narrative: "+profile.present()+(showCodeDetails ? " with Details" : ""));
} }
try { try {
generateByProfile(rcontext.getResourceResource(), profile, rcontext.getResourceResource(), profile.getSnapshot().getElement(), profile.getSnapshot().getElement().get(0), getChildrenForPath(profile, profile.getSnapshot().getElement(), rcontext.getResourceResource().getResourceType().toString()), x, rcontext.getResourceResource().getResourceType().toString(), showCodeDetails); generateByProfile(rcontext.getResource(), profile, rcontext.getResource(), profile.getSnapshot().getElement(), profile.getSnapshot().getElement().get(0), getChildrenForPath(profile, profile.getSnapshot().getElement(), rcontext.getResource().getResourceType().toString()), x, rcontext.getResource().getResourceType().toString(), showCodeDetails);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
x.para().b().style("color: maroon").tx("Exception generating Narrative: "+e.getMessage()); x.para().b().style("color: maroon").tx("Exception generating Narrative: "+e.getMessage());
} }
inject(rcontext.getResourceResource(), x, NarrativeStatus.GENERATED); inject((DomainResource) rcontext.getResource(), x, NarrativeStatus.GENERATED);
return true; return true;
} }

View File

@ -61,6 +61,15 @@ public abstract class ResourceRenderer extends DataRenderer {
this.rcontext = rcontext; this.rcontext = rcontext;
} }
public ResourceContext getRcontext() {
return rcontext;
}
public ResourceRenderer setRcontext(ResourceContext rcontext) {
this.rcontext = rcontext;
return this;
}
public XhtmlNode build(Resource dr) throws FHIRFormatError, DefinitionException, FHIRException, IOException, EOperationOutcome { public XhtmlNode build(Resource dr) throws FHIRFormatError, DefinitionException, FHIRException, IOException, EOperationOutcome {
XhtmlNode x = new XhtmlNode(NodeType.Element, "div"); XhtmlNode x = new XhtmlNode(NodeType.Element, "div");
render(x, dr); render(x, dr);
@ -324,7 +333,11 @@ public abstract class ResourceRenderer extends DataRenderer {
if (rcontext != null) { if (rcontext != null) {
BundleEntryComponent bundleResource = rcontext.resolve(url); BundleEntryComponent bundleResource = rcontext.resolve(url);
if (bundleResource != null) { if (bundleResource != null) {
String bundleUrl = "#" + bundleResource.getResource().getResourceType().name() + "_" + bundleResource.getResource().getId(); String id = bundleResource.getResource().getId();
if (id == null) {
id = makeIdFromBundleEntry(bundleResource.getFullUrl());
}
String bundleUrl = "#" + bundleResource.getResource().getResourceType().name() + "_" + id;
return new ResourceWithReference(bundleUrl, new ResourceWrapperDirect(this.context, bundleResource.getResource())); return new ResourceWithReference(bundleUrl, new ResourceWrapperDirect(this.context, bundleResource.getResource()));
} }
org.hl7.fhir.r5.elementmodel.Element bundleElement = rcontext.resolveElement(url, version); org.hl7.fhir.r5.elementmodel.Element bundleElement = rcontext.resolveElement(url, version);
@ -350,6 +363,16 @@ public abstract class ResourceRenderer extends DataRenderer {
} }
protected String makeIdFromBundleEntry(String url) {
if (url == null) {
return null;
}
if (url.startsWith("urn:uuid:")) {
return url.substring(9).toLowerCase();
}
return fullUrlToAnchor(url);
}
private String fullUrlToAnchor(String url) { private String fullUrlToAnchor(String url) {
return url.replace(":", "").replace("/", "_"); return url.replace(":", "").replace("/", "_");
} }

View File

@ -299,6 +299,10 @@ public class DirectWrappers {
return new PropertyWrapperDirect(context, p); return new PropertyWrapperDirect(context, p);
} }
public Resource getResource() {
return wrapped;
}
} }
} }

View File

@ -286,6 +286,10 @@ public class ElementWrappers {
return null; return null;
} }
public Element getElement() {
return wrapped;
}
} }
public static class PropertyWrapperMetaElement extends RendererWrapperImpl implements PropertyWrapper { public static class PropertyWrapperMetaElement extends RendererWrapperImpl implements PropertyWrapper {

View File

@ -7,15 +7,10 @@ import org.hl7.fhir.r5.model.Parameters;
import org.hl7.fhir.r5.model.Parameters.ParametersParameterComponent; import org.hl7.fhir.r5.model.Parameters.ParametersParameterComponent;
import org.hl7.fhir.r5.model.Resource; import org.hl7.fhir.r5.model.Resource;
import org.hl7.fhir.r5.renderers.utils.BaseWrappers.ResourceWrapper; import org.hl7.fhir.r5.renderers.utils.BaseWrappers.ResourceWrapper;
import org.hl7.fhir.r5.renderers.utils.Resolver.ResourceContextType;
import org.w3c.dom.Element; import org.w3c.dom.Element;
public class Resolver { public class Resolver {
public enum ResourceContextType {
PARAMETERS, BUNDLE
}
public interface IReferenceResolver { public interface IReferenceResolver {
ResourceWithReference resolve(RenderingContext context, String url); ResourceWithReference resolve(RenderingContext context, String url);
@ -24,27 +19,24 @@ public class Resolver {
} }
public static class ResourceContext { public static class ResourceContext {
private ResourceContextType type; private ResourceContext container;
private Resource containerResource;
private org.hl7.fhir.r5.elementmodel.Element containerElement;
DomainResource resourceResource; Resource resource;
org.hl7.fhir.r5.elementmodel.Element resourceElement; org.hl7.fhir.r5.elementmodel.Element element;
public ResourceContext(ResourceContextType type, Resource bundle, DomainResource dr) { public ResourceContext(ResourceContext container, Resource dr) {
super(); super();
this.type = type; this.container = container;
this.containerResource = bundle; this.resource = dr;
this.resourceResource = dr;
} }
public ResourceContext(ResourceContextType type, org.hl7.fhir.r5.elementmodel.Element bundle, org.hl7.fhir.r5.elementmodel.Element dr) { public ResourceContext(ResourceContext container, org.hl7.fhir.r5.elementmodel.Element dr) {
super(); super();
this.type = type; this.container = container;
this.containerElement = bundle; this.element = dr;
this.resourceElement = dr;
} }
// public ResourceContext(Object bundle, Element doc) { // public ResourceContext(Object bundle, Element doc) {
// // TODO Auto-generated constructor stub // // TODO Auto-generated constructor stub
// } // }
@ -54,22 +46,45 @@ public class Resolver {
// } // }
public ResourceContext(ResourceContext container, ResourceWrapper rw) {
super();
this.container = container;
// todo: howto do this better?
if (rw instanceof DirectWrappers.ResourceWrapperDirect) {
this.resource = ((DirectWrappers.ResourceWrapperDirect) rw).getResource();
} else if (rw instanceof ElementWrappers.ResourceWrapperMetaElement) {
this.element = ((ElementWrappers.ResourceWrapperMetaElement) rw).getElement();
} else {
throw new Error("Not supported yet");
}
}
public ResourceContext getContainer() {
return container;
}
public void setContainer(ResourceContext container) {
this.container = container;
}
// public org.hl7.fhir.r5.elementmodel.Element getBundleElement() { // public org.hl7.fhir.r5.elementmodel.Element getBundleElement() {
// return containerElement; // return containerElement;
// } // }
// //
public DomainResource getResourceResource() { public Resource getResource() {
return resourceResource; return resource;
} }
public org.hl7.fhir.r5.elementmodel.Element getResourceElement() { public org.hl7.fhir.r5.elementmodel.Element getElement() {
return resourceElement; return element;
} }
public BundleEntryComponent resolve(String value) { public BundleEntryComponent resolve(String value) {
if (value.startsWith("#")) { if (value.startsWith("#")) {
if (resourceResource != null) { if (resource instanceof DomainResource) {
for (Resource r : resourceResource.getContained()) { DomainResource dr = (DomainResource) resource;
for (Resource r : dr.getContained()) {
if (r.getId().equals(value.substring(1))) { if (r.getId().equals(value.substring(1))) {
BundleEntryComponent be = new BundleEntryComponent(); BundleEntryComponent be = new BundleEntryComponent();
be.setResource(r); be.setResource(r);
@ -79,19 +94,20 @@ public class Resolver {
} }
return null; return null;
} }
if (type == ResourceContextType.BUNDLE) {
if (containerResource != null) { if (resource instanceof Bundle) {
for (BundleEntryComponent be : ((Bundle) containerResource).getEntry()) { Bundle b = (Bundle) resource;
for (BundleEntryComponent be : b.getEntry()) {
if (be.getFullUrl().equals(value)) if (be.getFullUrl().equals(value))
return be; return be;
if (value.equals(be.getResource().fhirType()+"/"+be.getResource().getId())) if (value.equals(be.getResource().fhirType()+"/"+be.getResource().getId()))
return be; return be;
} }
} }
}
if (type == ResourceContextType.PARAMETERS) { if (resource instanceof Parameters) {
if (containerResource != null) { Parameters pp = (Parameters) resource;
for (ParametersParameterComponent p : ((Parameters) containerResource).getParameter()) { for (ParametersParameterComponent p : pp.getParameter()) {
if (p.getResource() != null && value.equals(p.getResource().fhirType()+"/"+p.getResource().getId())) { if (p.getResource() != null && value.equals(p.getResource().fhirType()+"/"+p.getResource().getId())) {
BundleEntryComponent be = new BundleEntryComponent(); BundleEntryComponent be = new BundleEntryComponent();
be.setResource(p.getResource()); be.setResource(p.getResource());
@ -100,23 +116,23 @@ public class Resolver {
} }
} }
} }
}
return null; return container != null ? container.resolve(value) : null;
} }
public org.hl7.fhir.r5.elementmodel.Element resolveElement(String value, String version) { public org.hl7.fhir.r5.elementmodel.Element resolveElement(String value, String version) {
if (value.startsWith("#")) { if (value.startsWith("#")) {
if (resourceElement != null) { if (element != null) {
for (org.hl7.fhir.r5.elementmodel.Element r : resourceElement.getChildrenByName("contained")) { for (org.hl7.fhir.r5.elementmodel.Element r : element.getChildrenByName("contained")) {
if (r.getChildValue("id").equals(value.substring(1))) if (r.getChildValue("id").equals(value.substring(1)))
return r; return r;
} }
} }
return null; return null;
} }
if (type == ResourceContextType.BUNDLE) { if (element != null) {
if (containerElement != null) { if (element.fhirType().equals("Bundle")) {
for (org.hl7.fhir.r5.elementmodel.Element be : containerElement.getChildren("entry")) { for (org.hl7.fhir.r5.elementmodel.Element be : element.getChildren("entry")) {
org.hl7.fhir.r5.elementmodel.Element res = be.getNamedChild("resource"); org.hl7.fhir.r5.elementmodel.Element res = be.getNamedChild("resource");
if (res != null) { if (res != null) {
if (value.equals(be.getChildValue("fullUrl"))) { if (value.equals(be.getChildValue("fullUrl"))) {
@ -132,10 +148,8 @@ public class Resolver {
} }
} }
} }
} if (element.fhirType().equals("Parameters")) {
if (type == ResourceContextType.PARAMETERS) { for (org.hl7.fhir.r5.elementmodel.Element p : element.getChildren("parameter")) {
if (containerElement != null) {
for (org.hl7.fhir.r5.elementmodel.Element p : containerElement.getChildren("parameter")) {
org.hl7.fhir.r5.elementmodel.Element res = p.getNamedChild("resource"); org.hl7.fhir.r5.elementmodel.Element res = p.getNamedChild("resource");
if (res != null && value.equals(res.fhirType()+"/"+res.getChildValue("id"))) { if (res != null && value.equals(res.fhirType()+"/"+res.getChildValue("id"))) {
if (checkVersion(version, res)) { if (checkVersion(version, res)) {
@ -145,7 +159,7 @@ public class Resolver {
} }
} }
} }
return null; return container != null ? container.resolveElement(value, version) : null;
} }
private boolean checkVersion(String version, org.hl7.fhir.r5.elementmodel.Element res) { private boolean checkVersion(String version, org.hl7.fhir.r5.elementmodel.Element res) {

View File

@ -11,7 +11,7 @@ public class JurisdictionUtilities {
} }
public static String getJurisdictionFromLocale(String s) { public static String getJurisdictionFromLocale(String s) {
if (Utilities.existsInList(s, if (Utilities.existsInList(s.toUpperCase(),
"AD", "AE", "AF", "AG", "AI", "AL", "AM", "AO", "AQ", "AD", "AE", "AF", "AG", "AI", "AL", "AM", "AO", "AQ",
"AR", "AS", "AT", "AU", "AW", "AX", "AZ", "BA", "BB", "AR", "AS", "AT", "AU", "AW", "AX", "AZ", "BA", "BB",
"BD", "BE", "BF", "BG", "BH", "BI", "BJ", "BL", "BM", "BD", "BE", "BF", "BG", "BH", "BI", "BJ", "BL", "BM",
@ -41,7 +41,7 @@ public class JurisdictionUtilities {
"UZ", "VA", "VC", "VE", "VG", "VI", "VN", "VU", "WF", "UZ", "VA", "VC", "VE", "VG", "VI", "VN", "VU", "WF",
"WS", "YE", "YT", "ZA", "ZM", "ZW" "WS", "YE", "YT", "ZA", "ZM", "ZW"
)) { )) {
return "urn:iso:std:iso:3166#"+s; return "urn:iso:std:iso:3166#"+s.toUpperCase();
} else { } else {
switch (s) { switch (s) {
case "uv" : return "http://unstats.un.org/unsd/methods/m49/m49.htm#001"; case "uv" : return "http://unstats.un.org/unsd/methods/m49/m49.htm#001";
@ -54,7 +54,7 @@ public class JurisdictionUtilities {
} }
public static String displayJurisdiction(String s) { public static String displayJurisdiction(String s) {
return displayJurisdiction(CodeSystemUtilities.readCoding(s)); return displayJurisdiction(CodeSystemUtilities.readCoding(getJurisdictionFromLocale(s)));
} }
public static String displayJurisdiction(Coding c) { public static String displayJurisdiction(Coding c) {