diff --git a/matchbox-engine/src/main/java/ch/ahdis/matchbox/engine/MatchboxEngine.java b/matchbox-engine/src/main/java/ch/ahdis/matchbox/engine/MatchboxEngine.java index e13620760a3..1d2d744b258 100644 --- a/matchbox-engine/src/main/java/ch/ahdis/matchbox/engine/MatchboxEngine.java +++ b/matchbox-engine/src/main/java/ch/ahdis/matchbox/engine/MatchboxEngine.java @@ -1,7 +1,5 @@ package ch.ahdis.matchbox.engine; -import static org.apache.commons.lang3.StringUtils.substring; - /* * #%L * Matchbox Engine @@ -27,13 +25,12 @@ import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; -import java.util.*; +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; import java.util.regex.Pattern; import java.util.stream.Collectors; -import ch.ahdis.matchbox.engine.exception.IgLoadException; -import ch.ahdis.matchbox.engine.exception.MatchboxEngineCreationException; -import ch.ahdis.matchbox.engine.exception.TerminologyServerException; import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.Nullable; import org.hl7.fhir.convertors.factory.VersionConvertorFactory_40_50; @@ -44,9 +41,11 @@ import org.hl7.fhir.r4.model.Resource; import org.hl7.fhir.r5.context.ContextUtilities; import org.hl7.fhir.r5.context.SimpleWorkerContext; +import org.hl7.fhir.r5.context.SimpleWorkerContext.SimpleWorkerContextBuilder; import org.hl7.fhir.r5.elementmodel.Element; import org.hl7.fhir.r5.elementmodel.Manager; import org.hl7.fhir.r5.elementmodel.Manager.FhirFormat; +import org.hl7.fhir.r5.fhirpath.FHIRPathEngine; import org.hl7.fhir.r5.formats.IParser; import org.hl7.fhir.r5.formats.IParser.OutputStyle; import org.hl7.fhir.r5.model.Base; @@ -61,7 +60,6 @@ import org.hl7.fhir.r5.utils.structuremap.StructureMapUtilities; import org.hl7.fhir.r5.utils.validation.IResourceValidator; import org.hl7.fhir.r5.utils.validation.constants.ReferenceValidationPolicy; -import org.hl7.fhir.r5.fhirpath.FHIRPathEngine; import org.hl7.fhir.utilities.ByteProvider; import org.hl7.fhir.utilities.FhirPublication; import org.hl7.fhir.utilities.Utilities; @@ -76,8 +74,10 @@ import org.hl7.fhir.validation.ValidationEngine; import org.hl7.fhir.validation.instance.InstanceValidator; -import ch.ahdis.matchbox.engine.MatchboxEngine.FilesystemPackageCacheMode; import ch.ahdis.matchbox.engine.cli.VersionUtil; +import ch.ahdis.matchbox.engine.exception.IgLoadException; +import ch.ahdis.matchbox.engine.exception.MatchboxEngineCreationException; +import ch.ahdis.matchbox.engine.exception.TerminologyServerException; import ch.ahdis.matchbox.mappinglanguage.MatchboxStructureMapUtilities; import ch.ahdis.matchbox.mappinglanguage.TransformSupportServices; @@ -379,7 +379,7 @@ public String transform(String input, boolean inputJson, String mapUri, boolean StructureMap map = context.fetchResource(StructureMap.class, mapUri); String fhirVersionTarget = getFhirVersion(getCanonicalFromStructureMap(map, StructureMap.StructureMapModelMode.TARGET)); - if (fhirVersionTarget !=null && !fhirVersionTarget.equals(this.getVersion())) { + if (fhirVersionTarget !=null && !fhirVersionTarget.equals(this.getVersion().substring(0, 3))) { log.info("Loading additional FHIR version for Target into context" + fhirVersionTarget); context = getContextForFhirVersion(fhirVersionTarget); } @@ -414,7 +414,7 @@ public org.hl7.fhir.r5.elementmodel.Element transform(ByteProvider source, FhirF StructureMap map = context.fetchResource(StructureMap.class, mapUri); String fhirVersionSource = getFhirVersion(getCanonicalFromStructureMap(map, StructureMap.StructureMapModelMode.SOURCE)); - if (fhirVersionSource !=null && !fhirVersionSource.equals(this.getVersion())) { + if (fhirVersionSource !=null && !fhirVersionSource.equals(this.getVersion().substring(0,3))) { log.info("Loading additional FHIR version for Source into context" + fhirVersionSource); context = getContextForFhirVersion(fhirVersionSource); } @@ -431,14 +431,12 @@ public SimpleWorkerContext getContextForFhirVersion(String fhirVersion) throws FHIRException, IOException { SimpleWorkerContext contextForFhirVersion = null; if (fhirVersion.startsWith("4.0")) { - MatchboxEngine engine = new MatchboxEngineBuilder().getEngineR4(); - contextForFhirVersion = engine.getContext(); + contextForFhirVersion = new SimpleWorkerContextBuilder().fromPackage(NpmPackage.fromPackage(getClass().getResourceAsStream("/hl7.fhir.r4.core.tgz"))); } if (fhirVersion.startsWith("5.0")) { - MatchboxEngine engine = new MatchboxEngineBuilder().getEngineR5(); - contextForFhirVersion = engine.getContext(); + contextForFhirVersion = new SimpleWorkerContextBuilder().fromPackage(NpmPackage.fromPackage(getClass().getResourceAsStream("/hl7.fhir.r5.core.tgz"))); } - if (contextForFhirVersion != null ) { + if (contextForFhirVersion != null) { // we need to copy now all StructureDefinitions from this Version to the new context for (StructureDefinition sd : contextForFhirVersion.listStructures()) { StructureDefinition sdn = sd.copy(); @@ -448,7 +446,6 @@ public SimpleWorkerContext getContextForFhirVersion(String fhirVersion) .setValue(new UriType("http://hl7.org/fhir")); this.getContext().cacheResource(sdn); } - contextForFhirVersion.cacheResource(sd); } } @@ -478,7 +475,7 @@ public org.hl7.fhir.r5.elementmodel.Element transform(org.hl7.fhir.r5.elementmod log.info("Using map " + map.getUrl() + (map.getVersion()!=null ? "|" + map.getVersion() + " " : "" ) + (map.getDateElement() != null && !map.getDateElement().isEmpty() ? "(" + map.getDateElement().asStringValue() + ")" : "")); - org.hl7.fhir.r5.elementmodel.Element resource = getTargetResourceFromStructureMap(map); + org.hl7.fhir.r5.elementmodel.Element resource = getTargetResourceFromStructureMap(map, targetContext); scu.transform(null, src, map, resource); resource.populatePaths(null); @@ -513,9 +510,9 @@ private String getCanonicalFromStructureMap(StructureMap map, StructureMap.Struc return targetTypeUrl; } - private org.hl7.fhir.r5.elementmodel.Element getTargetResourceFromStructureMap(StructureMap map) { + private org.hl7.fhir.r5.elementmodel.Element getTargetResourceFromStructureMap(StructureMap map, SimpleWorkerContext targetContext) { String targetTypeUrl = null; - SimpleWorkerContext context = this.getContext(); + SimpleWorkerContext context = (targetContext!=null ? targetContext : this.getContext()); for (StructureMap.StructureMapStructureComponent component : map.getStructure()) { if (component.getMode() == StructureMap.StructureMapModelMode.TARGET) { targetTypeUrl = component.getUrl(); @@ -529,7 +526,7 @@ private org.hl7.fhir.r5.elementmodel.Element getTargetResourceFromStructureMap(S } if (Utilities.isAbsoluteUrl(targetTypeUrl)) { - int index = targetTypeUrl.indexOf("/"+this.getVersion().substring(0,3)+"/"); + int index = targetTypeUrl.indexOf("/"+context.getVersion().substring(0,3)+"/"); if (index >= 0) { targetTypeUrl = targetTypeUrl.substring(0, index)+targetTypeUrl.substring(index+4); } @@ -548,7 +545,7 @@ private org.hl7.fhir.r5.elementmodel.Element getTargetResourceFromStructureMap(S throw new FHIRException("Unable to find StructureDefinition for target type ('" + targetTypeUrl + "')"); } - return Manager.build(getContext(), structureDefinition); + return Manager.build(context, structureDefinition); } /** diff --git a/matchbox-engine/src/main/java/org/hl7/fhir/r5/context/BaseWorkerContext.java b/matchbox-engine/src/main/java/org/hl7/fhir/r5/context/BaseWorkerContext.java index 73b2a1a4553..a66a1d07ebf 100644 --- a/matchbox-engine/src/main/java/org/hl7/fhir/r5/context/BaseWorkerContext.java +++ b/matchbox-engine/src/main/java/org/hl7/fhir/r5/context/BaseWorkerContext.java @@ -517,7 +517,7 @@ public void cacheResourceFromPackage(Resource r, PackageInformation packageInfo) // matchbox patch for duplicate resources, see https://github.com/ahdis/matchbox/issues/227 // org.hl7.fhir.r5.conformance.R5ExtensionsLoader.loadR5SpecialTypes(R5ExtensionsLoader.java:141) CanonicalResource ex = fetchResourceWithException(m.fhirType(), url); - if (laterVersion(m.getVersion(), ex.getVersion())) { + if (ex.getVersion()!=null && m.getVersion()!=null && laterVersion(m.getVersion(), ex.getVersion())) { logger.logMessage("Note replacing old version: " + formatMessage(I18nConstants.DUPLICATE_RESOURCE_, url, m.getVersion(), ex.getVersion(), ex.fhirType())); dropResource(ex); } else {