From 8c4b37980682b4757ebb15392b7daee704742473 Mon Sep 17 00:00:00 2001 From: Grahame Grieve Date: Fri, 13 Dec 2019 13:10:07 +1100 Subject: [PATCH] Add support for Base in logical models on any version of FHIR --- .../fhir/r5/conformance/ProfileUtilities.java | 32 +++++++++++++++++++ .../fhir/r5/context/SimpleWorkerContext.java | 16 ++++++++-- 2 files changed, 45 insertions(+), 3 deletions(-) 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 160bfd11e..2d05f10dd 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 @@ -25,6 +25,7 @@ import java.io.OutputStream; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; +import java.util.Date; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; @@ -66,6 +67,7 @@ import org.hl7.fhir.r5.model.ElementDefinition.TypeRefComponent; import org.hl7.fhir.r5.model.Enumeration; import org.hl7.fhir.r5.model.Enumerations.BindingStrength; import org.hl7.fhir.r5.model.Enumerations.FHIRVersion; +import org.hl7.fhir.r5.model.Enumerations.PublicationStatus; import org.hl7.fhir.r5.model.Extension; import org.hl7.fhir.r5.model.IdType; import org.hl7.fhir.r5.model.IntegerType; @@ -5218,6 +5220,36 @@ public class ProfileUtilities extends TranslatingUtilities { } + public static StructureDefinition makeBaseDefinition(FHIRVersion fhirVersion) { + StructureDefinition base = new StructureDefinition(); + base.setId("Base"); + base.setUrl("http://hl7.org/fhir/StructureDefinition/Base"); + base.setVersion(fhirVersion.toCode()); + base.setName("Base"); + base.setStatus(PublicationStatus.ACTIVE); + base.setDate(new Date()); + base.setFhirVersion(fhirVersion); + base.setKind(StructureDefinitionKind.COMPLEXTYPE); + base.setAbstract(true); + base.setType("Base"); + ElementDefinition e = base.getSnapshot().getElementFirstRep(); + e.setId("Base"); + e.setPath("Base"); + e.setMin(0); + e.setMax("*"); + e.getBase().setPath("Base"); + e.getBase().setMin(0); + e.getBase().setMax("*"); + e.setIsModifier(false); + e = base.getDifferential().getElementFirstRep(); + e.setId("Base"); + e.setPath("Base"); + e.setMin(0); + e.setMax("*"); + return base; + } + + } 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 b9be02d21..725a08b74 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 @@ -594,17 +594,27 @@ public class SimpleWorkerContext extends BaseWorkerContext implements IWorkerCon } public void generateSnapshot(StructureDefinition p) throws DefinitionException, FHIRException { - if (!p.hasSnapshot() && p.getKind() != StructureDefinitionKind.LOGICAL) { + generateSnapshot(p, false); + } + + public void generateSnapshot(StructureDefinition p, boolean logical) throws DefinitionException, FHIRException { + if (!p.hasSnapshot() && (logical || p.getKind() != StructureDefinitionKind.LOGICAL)) { if (!p.hasBaseDefinition()) throw new DefinitionException("Profile "+p.getName()+" ("+p.getUrl()+") has no base and no snapshot"); StructureDefinition sd = fetchResource(StructureDefinition.class, p.getBaseDefinition()); - if (sd == null) + if (sd == null && "http://hl7.org/fhir/StructureDefinition/Base".equals(p.getBaseDefinition())) { + sd = ProfileUtilities.makeBaseDefinition(p.getFhirVersion()); + } + if (sd == null) { throw new DefinitionException("Profile "+p.getName()+" ("+p.getUrl()+") base "+p.getBaseDefinition()+" could not be resolved"); + } List msgs = new ArrayList(); List errors = new ArrayList(); ProfileUtilities pu = new ProfileUtilities(this, msgs, this); pu.setThrowException(false); - pu.sortDifferential(sd, p, p.getUrl(), errors); + if (sd.getDerivation() == TypeDerivationRule.CONSTRAINT) { + pu.sortDifferential(sd, p, p.getUrl(), errors); + } pu.setDebug(false); for (String err : errors) msgs.add(new ValidationMessage(Source.ProfileValidator, IssueType.EXCEPTION, p.getUserString("path"), "Error sorting Differential: "+err, ValidationMessage.IssueSeverity.ERROR));