From ba731b0051be4eed1162c6a2779313c2a5926f3a Mon Sep 17 00:00:00 2001 From: Grahame Grieve Date: Tue, 7 Feb 2023 09:53:51 +1000 Subject: [PATCH] Fix R5 loading problem --- .../r5/conformance/R5ExtensionsLoader.java | 66 +++++++++++-------- 1 file changed, 39 insertions(+), 27 deletions(-) diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/conformance/R5ExtensionsLoader.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/conformance/R5ExtensionsLoader.java index c73b13472..52781a39f 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/conformance/R5ExtensionsLoader.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/conformance/R5ExtensionsLoader.java @@ -42,15 +42,17 @@ public class R5ExtensionsLoader { } public class Loadable { - public Loadable(PackageResourceInformation info) { + public Loadable(PackageResourceInformation info, NpmPackage source) { this.info = info; + this.source = source; } private T resource; + private NpmPackage source; private PackageResourceInformation info; public T getResource() throws FHIRFormatError, FileNotFoundException, IOException { if (resource == null) { - CanonicalResource r = (CanonicalResource) json.parse(pck.load(info)); - r.setUserData("path", Utilities.pathURL(pck.getWebLocation(), r.fhirType().toLowerCase()+ "-"+r.getId().toLowerCase()+".html")); + CanonicalResource r = (CanonicalResource) json.parse(source.load(info)); + r.setUserData("path", Utilities.pathURL(source.getWebLocation(), r.fhirType().toLowerCase()+ "-"+r.getId().toLowerCase()+".html")); resource = (T) r; } return resource; @@ -59,12 +61,12 @@ public class R5ExtensionsLoader { private BasePackageCacheManager pcm; private int count; - private NpmPackage pck; + private NpmPackage pckCore; + private NpmPackage pckExt; private Map> valueSets; private Map> codeSystems; private List> structures; private IWorkerContext context; - private PackageInformation pd; private JsonParser json; public R5ExtensionsLoader(BasePackageCacheManager pcm, IWorkerContext context) { @@ -78,22 +80,27 @@ public class R5ExtensionsLoader { } public void load() throws FHIRException, IOException { - pck = pcm.loadPackage("hl7.fhir.r5.core", "current"); - pd = new PackageInformation(pck); + pckCore = pcm.loadPackage("hl7.fhir.r5.core", "current"); + loadDetails(pckCore); + pckExt = pcm.loadPackage("hl7.fhir.uv.extensions", "current"); + loadDetails(pckExt); + } + + private void loadDetails(NpmPackage pck) throws IOException { + json = new JsonParser(); String[] types = new String[] { "StructureDefinition", "ValueSet", "CodeSystem" }; - json = new JsonParser(); for (PackageResourceInformation pri : pck.listIndexedResources(types)) { if (pri.getResourceType().equals("CodeSystem")) { - codeSystems.put(pri.getUrl(), new Loadable(pri)); - codeSystems.put(pri.getUrl()+"|"+pri.getVersion(), new Loadable(pri)); + codeSystems.put(pri.getUrl(), new Loadable(pri, pck)); + codeSystems.put(pri.getUrl()+"|"+pri.getVersion(), new Loadable(pri, pck)); } else if (pri.getResourceType().equals("ValueSet")) { - valueSets.put(pri.getUrl(), new Loadable(pri)); - valueSets.put(pri.getUrl()+"|"+pri.getVersion(), new Loadable(pri)); + valueSets.put(pri.getUrl(), new Loadable(pri, pck)); + valueSets.put(pri.getUrl()+"|"+pri.getVersion(), new Loadable(pri, pck)); } else if (pri.getResourceType().equals("StructureDefinition")) { - structures.add(new Loadable(pri)); + structures.add(new Loadable(pri, pck)); } - } + } } public void loadR5Extensions() throws FHIRException, IOException { @@ -105,9 +112,9 @@ public class R5ExtensionsLoader { if (sd.getDerivation() == TypeDerivationRule.CONSTRAINT) { if (survivesStrippingTypes(sd, context, typeNames)) { count++; - sd.setUserData("path", Utilities.pathURL(pck.getWebLocation(), "extension-"+sd.getId().toLowerCase()+".html")); + sd.setUserData("path", Utilities.pathURL(pckExt.getWebLocation(), "extension-"+sd.getId().toLowerCase()+".html")); registerTerminologies(sd); - context.cacheResourceFromPackage(sd, pd); + context.cacheResourceFromPackage(sd, new PackageInformation(lsd.source)); } } } @@ -119,9 +126,9 @@ public class R5ExtensionsLoader { StructureDefinition sd = lsd.getResource(); if (Utilities.existsInList(sd.getType(), types)) { count++; - sd.setUserData("path", Utilities.pathURL(pck.getWebLocation(), sd.getId().toLowerCase()+".html")); + sd.setUserData("path", Utilities.pathURL(lsd.source.getWebLocation(), sd.getId().toLowerCase()+".html")); registerTerminologies(sd); - context.cacheResourceFromPackage(sd, pd); + context.cacheResourceFromPackage(sd, new PackageInformation(lsd.source)); } } } @@ -132,7 +139,7 @@ public class R5ExtensionsLoader { String vsu = ed.getBinding().getValueSet(); ValueSet vs = context.fetchResource(ValueSet.class, vsu); if (vs == null) { - loadValueSet(vsu, context, valueSets, codeSystems, pd); + loadValueSet(vsu, context, valueSets, codeSystems); } else if (vs.hasVersion()) { ed.getBinding().setValueSet(vs.getUrl()+"|"+vs.getVersion()); } @@ -140,21 +147,22 @@ public class R5ExtensionsLoader { } } - private void loadValueSet(String url, IWorkerContext context, Map> valueSets, Map> codeSystems, PackageInformation pd) throws FHIRFormatError, FileNotFoundException, IOException { + private void loadValueSet(String url, IWorkerContext context, Map> valueSets, Map> codeSystems) throws FHIRFormatError, FileNotFoundException, IOException { if (valueSets.containsKey(url)) { ValueSet vs = valueSets.get(url).getResource(); - context.cacheResourceFromPackage(vs, pd); + context.cacheResourceFromPackage(vs, vs.getSourcePackage()); for (ConceptSetComponent inc : vs.getCompose().getInclude()) { for (CanonicalType t : inc.getValueSet()) { - loadValueSet(t.asStringValue(), context, valueSets, codeSystems, pd); + loadValueSet(t.asStringValue(), context, valueSets, codeSystems); } if (inc.hasSystem() && !inc.hasVersion()) { if (codeSystems.containsKey(inc.getSystem())) { CodeSystem cs = codeSystems.get(inc.getSystem()).getResource(); inc.setVersion(cs.getVersion()); - context.cacheResourceFromPackage(cs, pd); + context.cacheResourceFromPackage(cs, cs.getSourcePackage()); } else if (!context.hasResource(CodeSystem.class, inc.getSystem()) && codeSystems.containsKey(inc.getSystem())) { - context.cacheResourceFromPackage(codeSystems.get(inc.getSystem()).getResource(), pd); + CodeSystem cs1 = codeSystems.get(inc.getSystem()).getResource(); + context.cacheResourceFromPackage(cs1, cs1.getSourcePackage()); } } } @@ -202,11 +210,15 @@ public class R5ExtensionsLoader { } public byte[] getMap() throws IOException { - return pck.hasFile("other", "spec.internals") ? TextFile.streamToBytes(pck.load("other", "spec.internals")) : null; + return pckCore.hasFile("other", "spec.internals") ? TextFile.streamToBytes(pckCore.load("other", "spec.internals")) : null; } - public NpmPackage getPck() { - return pck; + public NpmPackage getPckCore() { + return pckCore; + } + + public NpmPackage getPckExt() { + return pckExt; }