From 851d1fc228f0b9f05dd52163f160f791a56411f1 Mon Sep 17 00:00:00 2001 From: Grahame Grieve Date: Tue, 17 Mar 2020 11:13:50 +1100 Subject: [PATCH] fix for 1.4.0 problems --- .../hl7/fhir/r4/utils/ToolingExtensions.java | 8 +++ .../fhir/r5/conformance/ProfileUtilities.java | 2 +- .../fhir/r5/context/BaseWorkerContext.java | 57 ++++++++++++++++++- .../fhir/r5/context/SimpleWorkerContext.java | 11 +--- 4 files changed, 64 insertions(+), 14 deletions(-) diff --git a/org.hl7.fhir.r4/src/main/java/org/hl7/fhir/r4/utils/ToolingExtensions.java b/org.hl7.fhir.r4/src/main/java/org/hl7/fhir/r4/utils/ToolingExtensions.java index e0f70e192..a8b184c7b 100644 --- a/org.hl7.fhir.r4/src/main/java/org/hl7/fhir/r4/utils/ToolingExtensions.java +++ b/org.hl7.fhir.r4/src/main/java/org/hl7/fhir/r4/utils/ToolingExtensions.java @@ -234,6 +234,14 @@ public class ToolingExtensions { } } + public static void addUriExtension(Element e, String url, String uri) { + Extension ex = getExtension(e, url); + if (ex != null) + ex.setValue(new UriType(uri)); + else + e.getExtension().add(Factory.newExtension(url, new UriType(uri), true)); + } + public static void addBooleanExtension(Element e, String url, boolean content) { Extension ex = getExtension(e, url); if (ex != null) diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/conformance/ProfileUtilities.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/conformance/ProfileUtilities.java index 238a8a258..a5dc90406 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/conformance/ProfileUtilities.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/conformance/ProfileUtilities.java @@ -2459,7 +2459,7 @@ public class ProfileUtilities extends TranslatingUtilities { if (derived.hasIsSummaryElement()) { if (!Base.compareDeep(derived.getIsSummaryElement(), base.getIsSummaryElement(), false)) { - if (base.hasIsSummary()) + if (base.hasIsSummary() && !context.getVersion().equals("1.4.0")) // work around a known issue with some 1.4.0 cosntraints throw new Error(context.formatMessage(I18nConstants.ERROR_IN_PROFILE__AT__BASE_ISSUMMARY___DERIVED_ISSUMMARY__, pn, derived.getPath(), base.getIsSummaryElement().asStringValue(), derived.getIsSummaryElement().asStringValue())); base.setIsSummaryElement(derived.getIsSummaryElement().copy()); } else if (trimDifferential) diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/context/BaseWorkerContext.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/context/BaseWorkerContext.java index 04499b2b5..1ce84720e 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/context/BaseWorkerContext.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/context/BaseWorkerContext.java @@ -21,6 +21,7 @@ package org.hl7.fhir.r5.context; */ import java.io.FileNotFoundException; +import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; @@ -41,6 +42,8 @@ import org.hl7.fhir.exceptions.TerminologyServiceException; import org.hl7.fhir.r5.conformance.ProfileUtilities; import org.hl7.fhir.r5.context.IWorkerContext.ILoggingService.LogCategory; import org.hl7.fhir.r5.context.TerminologyCache.CacheToken; +import org.hl7.fhir.r5.formats.IParser.OutputStyle; +import org.hl7.fhir.r5.formats.XmlParser; import org.hl7.fhir.r5.model.BooleanType; import org.hl7.fhir.r5.model.CapabilityStatement; import org.hl7.fhir.r5.model.CodeSystem; @@ -50,6 +53,7 @@ import org.hl7.fhir.r5.model.CodeableConcept; import org.hl7.fhir.r5.model.Coding; import org.hl7.fhir.r5.model.ConceptMap; import org.hl7.fhir.r5.model.Constants; +import org.hl7.fhir.r5.model.ElementDefinition; import org.hl7.fhir.r5.model.ElementDefinition.ElementDefinitionBindingComponent; import org.hl7.fhir.r5.model.Enumerations.PublicationStatus; import org.hl7.fhir.r5.model.ImplementationGuide; @@ -67,6 +71,7 @@ import org.hl7.fhir.r5.model.Resource; import org.hl7.fhir.r5.model.SearchParameter; import org.hl7.fhir.r5.model.StringType; import org.hl7.fhir.r5.model.StructureDefinition; +import org.hl7.fhir.r5.model.StructureDefinition.TypeDerivationRule; import org.hl7.fhir.r5.model.StructureMap; import org.hl7.fhir.r5.model.TerminologyCapabilities; import org.hl7.fhir.r5.model.TerminologyCapabilities.TerminologyCapabilitiesCodeSystemComponent; @@ -126,6 +131,8 @@ public abstract class BaseWorkerContext extends I18nBase implements IWorkerConte } private Object lock = new Object(); // used as a lock for the data that follows + protected String version; + private Map> allResourcesById = new HashMap>(); // all maps are to the full URI @@ -236,9 +243,13 @@ public abstract class BaseWorkerContext extends I18nBase implements IWorkerConte } throw new DefinitionException(formatMessage(I18nConstants.DUPLICATE_RESOURCE_, url)); } - if (r instanceof StructureDefinition) - structures.see((StructureDefinition) m); - else if (r instanceof ValueSet) + if (r instanceof StructureDefinition) { + StructureDefinition sd = (StructureDefinition) m; + if ("1.4.0".equals(version)) { + fixOldSD(sd); + } + structures.see(sd); + } else if (r instanceof ValueSet) valueSets.see((ValueSet) m); else if (r instanceof CodeSystem) codeSystems.see((CodeSystem) m); @@ -264,6 +275,21 @@ public abstract class BaseWorkerContext extends I18nBase implements IWorkerConte } } + public void fixOldSD(StructureDefinition sd) { + if (sd.getDerivation() == TypeDerivationRule.CONSTRAINT && sd.getType().equals("Extension") && sd.getUrl().startsWith("http://hl7.org/fhir/StructureDefinition/")) { + sd.setSnapshot(null); + } + for (ElementDefinition ed : sd.getDifferential().getElement()) { + if (ed.getPath().equals("Extension.url") || ed.getPath().endsWith(".extension.url") ) { + ed.setMin(1); + ed.getBase().setMin(1); + } + if ("extension".equals(ed.getSliceName())) { + ed.setSliceName(null); + } + } + } + /* * Compare business versions, returning "true" if the candidate newer version is in fact newer than the oldVersion * Comparison will work for strictly numeric versions as well as multi-level versions separated by ., -, _, : or space @@ -1277,4 +1303,29 @@ public abstract class BaseWorkerContext extends I18nBase implements IWorkerConte public Map getBinaries() { return binaries; } + + public void finishLoading() { + for (StructureDefinition sd : listStructures()) { + try { + if (sd.getSnapshot().isEmpty()) { + generateSnapshot(sd); +// new XmlParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(Utilities.path("[tmp]", "snapshot", tail(sd.getUrl())+".xml")), sd); + } + } catch (Exception e) { +// System.out.println("Unable to generate snapshot for "+tail(sd.getUrl()) +" from "+tail(sd.getBaseDefinition())+" because "+e.getMessage()); + } + + } + } + + + protected String tail(String url) { + if (Utilities.noString(url)) { + return "noname"; + } + if (url.contains("/")) { + return url.substring(url.lastIndexOf("/")+1); + } + return url; +} } diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/context/SimpleWorkerContext.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/context/SimpleWorkerContext.java index 907f52d2b..47bdc1c70 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/context/SimpleWorkerContext.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/context/SimpleWorkerContext.java @@ -101,7 +101,6 @@ public class SimpleWorkerContext extends BaseWorkerContext implements IWorkerCon } private Questionnaire questionnaire; - private String version; private String revision; private String date; private IValidatorFactory validatorFactory; @@ -179,6 +178,7 @@ public class SimpleWorkerContext extends BaseWorkerContext implements IWorkerCon res.setAllowLoadingDuplicates(true); res.version = pi.getNpm().get("version").getAsString(); res.loadFromPackage(pi, loader, filter); + res.finishLoading(); return res; } @@ -540,15 +540,6 @@ public class SimpleWorkerContext extends BaseWorkerContext implements IWorkerCon return result; } - private String tail(String url) { - if (Utilities.noString(url)) { - return "noname"; - } - if (url.contains("/")) { - return url.substring(url.lastIndexOf("/")+1); - } - return url; - } public void loadBinariesFromFolder(String folder) throws FileNotFoundException, Exception { for (String n : new File(folder).list()) {