Search in sources :

Example 1 with XVerExtensionManager

use of org.hl7.fhir.r4b.utils.XVerExtensionManager in project org.hl7.fhir.core by hapifhir.

the class SimpleWorkerContext method generateSnapshot.

@Override
public void generateSnapshot(StructureDefinition p, boolean logical) throws DefinitionException, FHIRException {
    if ((!p.hasSnapshot() || isProfileNeedsRegenerate(p)) && (logical || p.getKind() != StructureDefinitionKind.LOGICAL)) {
        if (!p.hasBaseDefinition())
            throw new DefinitionException(formatMessage(I18nConstants.PROFILE___HAS_NO_BASE_AND_NO_SNAPSHOT, p.getName(), p.getUrl()));
        StructureDefinition sd = fetchResource(StructureDefinition.class, p.getBaseDefinition());
        if (sd == null && "http://hl7.org/fhir/StructureDefinition/Base".equals(p.getBaseDefinition())) {
            sd = ProfileUtilities.makeBaseDefinition(p.getFhirVersion());
        }
        if (sd == null) {
            throw new DefinitionException(formatMessage(I18nConstants.PROFILE___BASE__COULD_NOT_BE_RESOLVED, p.getName(), p.getUrl(), p.getBaseDefinition()));
        }
        List<ValidationMessage> msgs = new ArrayList<ValidationMessage>();
        List<String> errors = new ArrayList<String>();
        ProfileUtilities pu = new ProfileUtilities(this, msgs, this);
        pu.setAutoFixSliceNames(true);
        pu.setThrowException(false);
        if (xverManager == null) {
            xverManager = new XVerExtensionManager(this);
        }
        pu.setXver(xverManager);
        if (sd.getDerivation() == TypeDerivationRule.CONSTRAINT) {
            pu.sortDifferential(sd, p, p.getUrl(), errors, true);
        }
        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));
        pu.generateSnapshot(sd, p, p.getUrl(), Utilities.extractBaseUrl(sd.getUserString("path")), p.getName());
        for (ValidationMessage msg : msgs) {
            if ((!ignoreProfileErrors && msg.getLevel() == ValidationMessage.IssueSeverity.ERROR) || msg.getLevel() == ValidationMessage.IssueSeverity.FATAL)
                throw new DefinitionException(formatMessage(I18nConstants.PROFILE___ELEMENT__ERROR_GENERATING_SNAPSHOT_, p.getName(), p.getUrl(), msg.getLocation(), msg.getMessage()));
        }
        if (!p.hasSnapshot())
            throw new FHIRException(formatMessage(I18nConstants.PROFILE___ERROR_GENERATING_SNAPSHOT, p.getName(), p.getUrl()));
        pu = null;
    }
}
Also used : StructureDefinition(org.hl7.fhir.r4b.model.StructureDefinition) ValidationMessage(org.hl7.fhir.utilities.validation.ValidationMessage) ProfileUtilities(org.hl7.fhir.r4b.conformance.ProfileUtilities) XVerExtensionManager(org.hl7.fhir.r4b.utils.XVerExtensionManager) ArrayList(java.util.ArrayList) DefinitionException(org.hl7.fhir.exceptions.DefinitionException) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Example 2 with XVerExtensionManager

use of org.hl7.fhir.r4b.utils.XVerExtensionManager in project org.hl7.fhir.core by hapifhir.

the class SimpleWorkerContext method generateSnapshot.

@Override
public void generateSnapshot(StructureDefinition p, boolean logical) throws FHIRException {
    if ((!p.hasSnapshot() || isProfileNeedsRegenerate(p)) && (logical || p.getKind() != StructureDefinitionKind.LOGICAL)) {
        if (!p.hasBaseDefinition())
            throw new DefinitionException(formatMessage(I18nConstants.PROFILE___HAS_NO_BASE_AND_NO_SNAPSHOT, p.getName(), p.getUrl()));
        StructureDefinition sd = fetchResource(StructureDefinition.class, p.getBaseDefinition());
        if (sd == null && "http://hl7.org/fhir/StructureDefinition/Base".equals(p.getBaseDefinition())) {
            sd = ProfileUtilities.makeBaseDefinition(p.getFhirVersion());
        }
        if (sd == null) {
            throw new DefinitionException(formatMessage(I18nConstants.PROFILE___BASE__COULD_NOT_BE_RESOLVED, p.getName(), p.getUrl(), p.getBaseDefinition()));
        }
        List<ValidationMessage> msgs = new ArrayList<ValidationMessage>();
        List<String> errors = new ArrayList<String>();
        ProfileUtilities pu = new ProfileUtilities(this, msgs, this);
        pu.setAutoFixSliceNames(true);
        pu.setThrowException(false);
        if (xverManager == null) {
            xverManager = new XVerExtensionManager(this);
        }
        pu.setXver(xverManager);
        if (sd.getDerivation() == TypeDerivationRule.CONSTRAINT) {
            pu.sortDifferential(sd, p, p.getUrl(), errors, true);
        }
        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));
        pu.generateSnapshot(sd, p, p.getUrl(), sd.getUserString("webroot"), p.getName());
        for (ValidationMessage msg : msgs) {
            if ((!ignoreProfileErrors && msg.getLevel() == ValidationMessage.IssueSeverity.ERROR) || msg.getLevel() == ValidationMessage.IssueSeverity.FATAL)
                throw new DefinitionException(formatMessage(I18nConstants.PROFILE___ELEMENT__ERROR_GENERATING_SNAPSHOT_, p.getName(), p.getUrl(), msg.getLocation(), msg.getMessage()));
        }
        if (!p.hasSnapshot())
            throw new FHIRException(formatMessage(I18nConstants.PROFILE___ERROR_GENERATING_SNAPSHOT, p.getName(), p.getUrl()));
        pu = null;
    }
}
Also used : ValidationMessage(org.hl7.fhir.utilities.validation.ValidationMessage) ProfileUtilities(org.hl7.fhir.r5.conformance.ProfileUtilities) XVerExtensionManager(org.hl7.fhir.r5.utils.XVerExtensionManager) ArrayList(java.util.ArrayList) DefinitionException(org.hl7.fhir.exceptions.DefinitionException) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Example 3 with XVerExtensionManager

use of org.hl7.fhir.r4b.utils.XVerExtensionManager in project org.hl7.fhir.core by hapifhir.

the class SnapShotGenerationTests method testGen.

private void testGen(boolean fail, TestDetails test, SnapShotGenerationTestsContext context) throws Exception {
    if (!Utilities.noString(test.register)) {
        List<ValidationMessage> messages = new ArrayList<ValidationMessage>();
        ProfileUtilities pu = new ProfileUtilities(TestingUtilities.getSharedWorkerContext(), messages, null);
        pu.setNewSlicingProcessing(true);
        for (StructureDefinition sd : test.included) {
            pu.setIds(sd, false);
        }
        for (StructureDefinition sd : test.included) {
            if (!TestingUtilities.getSharedWorkerContext().hasResource(StructureDefinition.class, sd.getUrl())) {
                TestingUtilities.getSharedWorkerContext().cacheResource(sd);
            }
        }
        StructureDefinition base = TestingUtilities.getSharedWorkerContext().fetchResource(StructureDefinition.class, test.included.get(0).getBaseDefinition());
        if (base != null) {
            pu.generateSnapshot(base, test.included.get(0), test.included.get(0).getUrl(), "http://test.org/profile", test.included.get(0).getName());
        }
        int ec = 0;
        for (ValidationMessage vm : messages) {
            if (vm.getLevel() == IssueSeverity.ERROR) {
                System.out.println(vm.summary());
                ec++;
            }
        }
        if (ec > 0)
            throw new FHIRException("register gen failed: " + messages.toString());
    }
    StructureDefinition base = getSD(test.getSource().getBaseDefinition(), context);
    if (!base.getUrl().equals(test.getSource().getBaseDefinition()))
        throw new Exception("URL mismatch on base: " + base.getUrl() + " wanting " + test.getSource().getBaseDefinition());
    StructureDefinition output = test.getSource().copy();
    ProfileUtilities pu = new ProfileUtilities(TestingUtilities.getSharedWorkerContext(), messages, new TestPKP());
    pu.setNewSlicingProcessing(test.isNewSliceProcessing());
    pu.setThrowException(false);
    pu.setDebug(test.isDebug());
    pu.setIds(test.getSource(), false);
    if (!TestingUtilities.getSharedWorkerContext().hasPackage(CommonPackages.ID_XVER, CommonPackages.VER_XVER)) {
        NpmPackage npm = new FilesystemPackageCacheManager(true, ToolsVersion.TOOLS_VERSION).loadPackage(CommonPackages.ID_XVER, CommonPackages.VER_XVER);
        TestingUtilities.getSharedWorkerContext().loadFromPackage(npm, new TestPackageLoader(new String[] { "StructureDefinition" }), new String[] { "StructureDefinition" });
    }
    pu.setXver(new XVerExtensionManager(TestingUtilities.getSharedWorkerContext()));
    if (test.isSort()) {
        List<String> errors = new ArrayList<String>();
        int lastCount = output.getDifferential().getElement().size();
        pu.sortDifferential(base, output, test.getSource().getName(), errors, false);
        if (errors.size() > 0)
            throw new FHIRException("Sort failed: " + errors.toString());
    }
    try {
        messages.clear();
        pu.generateSnapshot(base, output, test.getSource().getUrl(), "http://test.org/profile", test.getSource().getName());
        List<ValidationMessage> ml = new ArrayList<>();
        for (ValidationMessage vm : messages) {
            if (vm.getLevel() == IssueSeverity.ERROR) {
                ml.add(vm);
            }
        }
        if (ml.size() > 0) {
            throw new FHIRException("Snapshot Generation failed: " + ml.toString());
        }
    } catch (Throwable e) {
        System.out.println("\r\nException: " + e.getMessage());
        throw e;
    }
    if (output.getDifferential().hasElement()) {
        RenderingContext rc = new RenderingContext(TestingUtilities.getSharedWorkerContext(), null, null, "http://hl7.org/fhir", "", null, ResourceRendererMode.END_USER);
        rc.setDestDir(Utilities.path("[tmp]", "snapshot"));
        rc.setProfileUtilities(new ProfileUtilities(TestingUtilities.getSharedWorkerContext(), null, new TestPKP()));
        RendererFactory.factory(output, rc).render(output);
    }
    if (!fail) {
        test.output = output;
        TestingUtilities.getSharedWorkerContext().cacheResource(output);
        File dst = new File(TestingUtilities.tempFile("snapshot", test.getId() + "-expected.xml"));
        if (dst.exists())
            dst.delete();
        IOUtils.copy(TestingUtilities.loadTestResourceStream("r5", "snapshot-generation", test.getId() + "-expected.xml"), new FileOutputStream(dst));
        new XmlParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(TestingUtilities.tempFile("snapshot", test.getId() + "-actual.xml")), output);
        StructureDefinition t1 = test.expected.copy();
        t1.setText(null);
        StructureDefinition t2 = test.output.copy();
        t2.setText(null);
        Assertions.assertTrue(t1.equalsDeep(t2), "Output does not match expected");
    }
}
Also used : FilesystemPackageCacheManager(org.hl7.fhir.utilities.npm.FilesystemPackageCacheManager) RenderingContext(org.hl7.fhir.r5.renderers.utils.RenderingContext) XmlParser(org.hl7.fhir.r5.formats.XmlParser) ValidationMessage(org.hl7.fhir.utilities.validation.ValidationMessage) ArrayList(java.util.ArrayList) FHIRException(org.hl7.fhir.exceptions.FHIRException) FileNotFoundException(java.io.FileNotFoundException) SAXException(org.xml.sax.SAXException) NotImplementedException(org.apache.commons.lang3.NotImplementedException) DefinitionException(org.hl7.fhir.exceptions.DefinitionException) PathEngineException(org.hl7.fhir.exceptions.PathEngineException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) FHIRException(org.hl7.fhir.exceptions.FHIRException) StructureDefinition(org.hl7.fhir.r5.model.StructureDefinition) TestPackageLoader(org.hl7.fhir.r5.test.utils.TestPackageLoader) ProfileUtilities(org.hl7.fhir.r5.conformance.ProfileUtilities) NpmPackage(org.hl7.fhir.utilities.npm.NpmPackage) XVerExtensionManager(org.hl7.fhir.r5.utils.XVerExtensionManager) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Example 4 with XVerExtensionManager

use of org.hl7.fhir.r4b.utils.XVerExtensionManager in project org.hl7.fhir.core by hapifhir.

the class ProfileDrivenRenderer method splitExtensions.

private List<PropertyWrapper> splitExtensions(StructureDefinition profile, List<PropertyWrapper> children) throws UnsupportedEncodingException, IOException, FHIRException {
    List<PropertyWrapper> results = new ArrayList<PropertyWrapper>();
    Map<String, PropertyWrapper> map = new HashMap<String, PropertyWrapper>();
    for (PropertyWrapper p : children) if (p.getName().equals("extension") || p.getName().equals("modifierExtension")) {
        // we're going to split these up, and create a property for each url
        if (p.hasValues()) {
            for (BaseWrapper v : p.getValues()) {
                Extension ex = (Extension) v.getBase();
                String url = ex.getUrl();
                StructureDefinition ed = getContext().getWorker().fetchResource(StructureDefinition.class, url);
                if (ed == null) {
                    if (xverManager == null) {
                        xverManager = new XVerExtensionManager(context.getWorker());
                    }
                    if (xverManager.matchingUrl(url) && xverManager.status(url) == XVerExtensionStatus.Valid) {
                        ed = xverManager.makeDefinition(url);
                        getContext().getWorker().generateSnapshot(ed);
                        getContext().getWorker().cacheResource(ed);
                    }
                }
                if (p.getName().equals("modifierExtension") && ed == null) {
                    throw new DefinitionException("Unknown modifier extension " + url);
                }
                PropertyWrapper pe = map.get(p.getName() + "[" + url + "]");
                if (pe == null) {
                    if (ed == null) {
                        if (url.startsWith("http://hl7.org/fhir") && !url.startsWith("http://hl7.org/fhir/us")) {
                            throw new DefinitionException("unknown extension " + url);
                        }
                        // System.out.println("unknown extension "+url);
                        pe = new PropertyWrapperDirect(this.context, new Property(p.getName() + "[" + url + "]", p.getTypeCode(), p.getDefinition(), p.getMinCardinality(), p.getMaxCardinality(), ex), null);
                    } else {
                        ElementDefinition def = ed.getSnapshot().getElement().get(0);
                        pe = new PropertyWrapperDirect(this.context, new Property(p.getName() + "[" + url + "]", "Extension", def.getDefinition(), def.getMin(), def.getMax().equals("*") ? Integer.MAX_VALUE : Integer.parseInt(def.getMax()), ex), ed.getSnapshot().getElementFirstRep());
                        ((PropertyWrapperDirect) pe).getWrapped().setStructure(ed);
                    }
                    results.add(pe);
                } else
                    pe.getValues().add(v);
            }
        }
    } else
        results.add(p);
    return results;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) PropertyWrapperDirect(org.hl7.fhir.r4b.renderers.utils.DirectWrappers.PropertyWrapperDirect) PropertyWrapper(org.hl7.fhir.r4b.renderers.utils.BaseWrappers.PropertyWrapper) Extension(org.hl7.fhir.r4b.model.Extension) BaseWrapper(org.hl7.fhir.r4b.renderers.utils.BaseWrappers.BaseWrapper) StructureDefinition(org.hl7.fhir.r4b.model.StructureDefinition) XVerExtensionManager(org.hl7.fhir.r4b.utils.XVerExtensionManager) DefinitionException(org.hl7.fhir.exceptions.DefinitionException) ElementDefinition(org.hl7.fhir.r4b.model.ElementDefinition) Property(org.hl7.fhir.r4b.model.Property)

Example 5 with XVerExtensionManager

use of org.hl7.fhir.r4b.utils.XVerExtensionManager in project org.hl7.fhir.core by hapifhir.

the class SnapShotGenerationTests method testGen.

private void testGen(boolean fail, TestDetails test, SnapShotGenerationTestsContext context) throws Exception {
    if (!Utilities.noString(test.register)) {
        List<ValidationMessage> messages = new ArrayList<ValidationMessage>();
        ProfileUtilities pu = new ProfileUtilities(TestingUtilities.context(), messages, null);
        pu.setNewSlicingProcessing(true);
        for (StructureDefinition sd : test.included) {
            pu.setIds(sd, false);
        }
        for (StructureDefinition sd : test.included) {
            if (!TestingUtilities.context().hasResource(StructureDefinition.class, sd.getUrl())) {
                TestingUtilities.context().cacheResource(sd);
            }
        }
        StructureDefinition base = TestingUtilities.context().fetchResource(StructureDefinition.class, test.included.get(0).getBaseDefinition());
        if (base != null) {
            pu.generateSnapshot(base, test.included.get(0), test.included.get(0).getUrl(), "http://test.org/profile", test.included.get(0).getName());
        }
        int ec = 0;
        for (ValidationMessage vm : messages) {
            if (vm.getLevel() == IssueSeverity.ERROR) {
                System.out.println(vm.summary());
                ec++;
            }
        }
        if (ec > 0)
            throw new FHIRException("register gen failed: " + messages.toString());
    }
    StructureDefinition base = getSD(test.getSource().getBaseDefinition(), context);
    if (!base.getUrl().equals(test.getSource().getBaseDefinition()))
        throw new Exception("URL mismatch on base: " + base.getUrl() + " wanting " + test.getSource().getBaseDefinition());
    StructureDefinition output = test.getSource().copy();
    ProfileUtilities pu = new ProfileUtilities(TestingUtilities.context(), messages, new TestPKP());
    pu.setNewSlicingProcessing(test.isNewSliceProcessing());
    pu.setThrowException(false);
    pu.setDebug(test.isDebug());
    pu.setIds(test.getSource(), false);
    if (!TestingUtilities.context().hasPackage(CommonPackages.ID_XVER, CommonPackages.VER_XVER)) {
        NpmPackage npm = new FilesystemPackageCacheManager(true, ToolsVersion.TOOLS_VERSION).loadPackage(CommonPackages.ID_XVER, CommonPackages.VER_XVER);
        TestingUtilities.context().loadFromPackage(npm, new TestLoader(new String[] { "StructureDefinition" }), new String[] { "StructureDefinition" });
    }
    pu.setXver(new XVerExtensionManager(TestingUtilities.context()));
    if (test.isSort()) {
        List<String> errors = new ArrayList<String>();
        int lastCount = output.getDifferential().getElement().size();
        pu.sortDifferential(base, output, test.getSource().getName(), errors, false);
        if (errors.size() > 0)
            throw new FHIRException("Sort failed: " + errors.toString());
    }
    try {
        messages.clear();
        pu.generateSnapshot(base, output, test.getSource().getUrl(), "http://test.org/profile", test.getSource().getName());
        List<ValidationMessage> ml = new ArrayList<>();
        for (ValidationMessage vm : messages) {
            if (vm.getLevel() == IssueSeverity.ERROR) {
                ml.add(vm);
            }
        }
        if (ml.size() > 0) {
            throw new FHIRException("Snapshot Generation failed: " + ml.toString());
        }
    } catch (Throwable e) {
        System.out.println("\r\nException: " + e.getMessage());
        throw e;
    }
    if (output.getDifferential().hasElement()) {
        RenderingContext rc = new RenderingContext(TestingUtilities.context(), null, null, "http://hl7.org/fhir", "", null, ResourceRendererMode.END_USER);
        rc.setDestDir(Utilities.path("[tmp]", "snapshot"));
        rc.setProfileUtilities(new ProfileUtilities(TestingUtilities.context(), null, new TestPKP()));
        RendererFactory.factory(output, rc).render(output);
    }
    if (!fail) {
        test.output = output;
        TestingUtilities.context().cacheResource(output);
        File dst = new File(TestingUtilities.tempFile("snapshot", test.getId() + "-expected.xml"));
        if (dst.exists())
            dst.delete();
        IOUtils.copy(TestingUtilities.loadTestResourceStream("r5", "snapshot-generation", test.getId() + "-expected.xml"), new FileOutputStream(dst));
        new XmlParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(TestingUtilities.tempFile("snapshot", test.getId() + "-actual.xml")), output);
        StructureDefinition t1 = test.expected.copy();
        t1.setText(null);
        StructureDefinition t2 = test.output.copy();
        t2.setText(null);
        Assertions.assertTrue(t1.equalsDeep(t2), "Output does not match expected");
    }
}
Also used : FilesystemPackageCacheManager(org.hl7.fhir.utilities.npm.FilesystemPackageCacheManager) RenderingContext(org.hl7.fhir.r4b.renderers.utils.RenderingContext) XmlParser(org.hl7.fhir.r4b.formats.XmlParser) ValidationMessage(org.hl7.fhir.utilities.validation.ValidationMessage) ArrayList(java.util.ArrayList) FHIRException(org.hl7.fhir.exceptions.FHIRException) FileNotFoundException(java.io.FileNotFoundException) SAXException(org.xml.sax.SAXException) NotImplementedException(org.apache.commons.lang3.NotImplementedException) DefinitionException(org.hl7.fhir.exceptions.DefinitionException) PathEngineException(org.hl7.fhir.exceptions.PathEngineException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) FHIRException(org.hl7.fhir.exceptions.FHIRException) StructureDefinition(org.hl7.fhir.r4b.model.StructureDefinition) ProfileUtilities(org.hl7.fhir.r4b.conformance.ProfileUtilities) NpmPackage(org.hl7.fhir.utilities.npm.NpmPackage) XVerExtensionManager(org.hl7.fhir.r4b.utils.XVerExtensionManager) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Aggregations

ArrayList (java.util.ArrayList)6 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)6 FHIRException (org.hl7.fhir.exceptions.FHIRException)4 ValidationMessage (org.hl7.fhir.utilities.validation.ValidationMessage)4 StructureDefinition (org.hl7.fhir.r4b.model.StructureDefinition)3 XVerExtensionManager (org.hl7.fhir.r4b.utils.XVerExtensionManager)3 XVerExtensionManager (org.hl7.fhir.r5.utils.XVerExtensionManager)3 File (java.io.File)2 FileNotFoundException (java.io.FileNotFoundException)2 FileOutputStream (java.io.FileOutputStream)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 NotImplementedException (org.apache.commons.lang3.NotImplementedException)2 PathEngineException (org.hl7.fhir.exceptions.PathEngineException)2 ProfileUtilities (org.hl7.fhir.r4b.conformance.ProfileUtilities)2 ProfileUtilities (org.hl7.fhir.r5.conformance.ProfileUtilities)2 StructureDefinition (org.hl7.fhir.r5.model.StructureDefinition)2 FilesystemPackageCacheManager (org.hl7.fhir.utilities.npm.FilesystemPackageCacheManager)2 NpmPackage (org.hl7.fhir.utilities.npm.NpmPackage)2