Search in sources :

Example 46 with FHIRVersion

use of org.hl7.fhir.r5.model.Enumerations.FHIRVersion in project org.hl7.fhir.core by hapifhir.

the class BundleValidator method validateBundle.

public void validateBundle(List<ValidationMessage> errors, Element bundle, NodeStack stack, boolean checkSpecials, ValidatorHostContext hostContext) {
    List<Element> entries = new ArrayList<Element>();
    bundle.getNamedChildren(ENTRY, entries);
    String type = bundle.getNamedChildValue(TYPE);
    type = StringUtils.defaultString(type);
    if (entries.size() == 0) {
        rule(errors, IssueType.INVALID, stack.getLiteralPath(), !(type.equals(DOCUMENT) || type.equals(MESSAGE)), I18nConstants.BUNDLE_BUNDLE_ENTRY_NOFIRST);
    } else {
        // Get the first entry, the MessageHeader
        Element firstEntry = entries.get(0);
        // Get the stack of the first entry
        NodeStack firstStack = stack.push(firstEntry, 1, null, null);
        String fullUrl = firstEntry.getNamedChildValue(FULL_URL);
        if (type.equals(DOCUMENT)) {
            Element resource = firstEntry.getNamedChild(RESOURCE);
            if (rule(errors, IssueType.INVALID, firstEntry.line(), firstEntry.col(), stack.addToLiteralPath(ENTRY, PATH_ARG), resource != null, I18nConstants.BUNDLE_BUNDLE_ENTRY_NOFIRSTRESOURCE)) {
                String id = resource.getNamedChildValue(ID);
                validateDocument(errors, entries, resource, firstStack.push(resource, -1, null, null), fullUrl, id);
            }
            if (!VersionUtilities.isThisOrLater(FHIRVersion._4_0_1.getDisplay(), bundle.getProperty().getStructure().getFhirVersion().getDisplay())) {
                handleSpecialCaseForLastUpdated(bundle, errors, stack);
            }
            checkAllInterlinked(errors, entries, stack, bundle, true);
        }
        if (type.equals(MESSAGE)) {
            Element resource = firstEntry.getNamedChild(RESOURCE);
            String id = resource.getNamedChildValue(ID);
            if (rule(errors, IssueType.INVALID, firstEntry.line(), firstEntry.col(), stack.addToLiteralPath(ENTRY, PATH_ARG), resource != null, I18nConstants.BUNDLE_BUNDLE_ENTRY_NOFIRSTRESOURCE)) {
                validateMessage(errors, entries, resource, firstStack.push(resource, -1, null, null), fullUrl, id);
            }
            checkAllInterlinked(errors, entries, stack, bundle, VersionUtilities.isR5Ver(context.getVersion()));
        }
        if (type.equals(SEARCHSET)) {
            checkSearchSet(errors, bundle, entries, stack);
        }
    // We do not yet have rules requiring that the id and fullUrl match when dealing with messaging Bundles
    // validateResourceIds(errors, entries, stack);
    }
    int count = 0;
    Map<String, Integer> counter = new HashMap<>();
    boolean fullUrlOptional = Utilities.existsInList(type, "transaction", "transaction-response", "batch", "batch-response");
    for (Element entry : entries) {
        NodeStack estack = stack.push(entry, count, null, null);
        String fullUrl = entry.getNamedChildValue(FULL_URL);
        String url = getCanonicalURLForEntry(entry);
        String id = getIdForEntry(entry);
        if (url != null) {
            if (!(!url.equals(fullUrl) || (url.matches(uriRegexForVersion()) && url.endsWith("/" + id))) && !isV3orV2Url(url))
                rule(errors, IssueType.INVALID, entry.line(), entry.col(), stack.addToLiteralPath(ENTRY, PATH_ARG), false, I18nConstants.BUNDLE_BUNDLE_ENTRY_MISMATCHIDURL, url, fullUrl, id);
            rule(errors, IssueType.INVALID, entry.line(), entry.col(), stack.addToLiteralPath(ENTRY, PATH_ARG), !url.equals(fullUrl) || serverBase == null || (url.equals(Utilities.pathURL(serverBase, entry.getNamedChild(RESOURCE).fhirType(), id))), I18nConstants.BUNDLE_BUNDLE_ENTRY_CANONICAL, url, fullUrl);
        }
        if (!VersionUtilities.isR2Ver(context.getVersion())) {
            rule(errors, IssueType.INVALID, entry.line(), entry.col(), estack.getLiteralPath(), fullUrlOptional || fullUrl != null, I18nConstants.BUNDLE_BUNDLE_ENTRY_FULLURL_REQUIRED);
        }
        // check bundle profile requests
        if (entry.hasChild(RESOURCE)) {
            String rtype = entry.getNamedChild(RESOURCE).fhirType();
            int rcount = counter.containsKey(rtype) ? counter.get(rtype) + 1 : 0;
            counter.put(rtype, rcount);
            for (BundleValidationRule bvr : validator.getBundleValidationRules()) {
                if (meetsRule(bvr, rtype, rcount, count)) {
                    StructureDefinition defn = validator.getContext().fetchResource(StructureDefinition.class, bvr.getProfile());
                    if (defn == null) {
                        throw new Error(validator.getContext().formatMessage(I18nConstants.BUNDLE_RULE_PROFILE_UNKNOWN, bvr.getRule(), bvr.getProfile()));
                    } else {
                        Element res = entry.getNamedChild(RESOURCE);
                        NodeStack rstack = estack.push(res, -1, null, null);
                        if (validator.isCrumbTrails()) {
                            res.addMessage(signpost(errors, IssueType.INFORMATIONAL, res.line(), res.col(), stack.getLiteralPath(), I18nConstants.VALIDATION_VAL_PROFILE_SIGNPOST_BUNDLE_PARAM, defn.getUrl()));
                        }
                        stack.resetIds();
                        validator.startInner(hostContext, errors, res, res, defn, rstack, false);
                    }
                }
            }
        }
        // todo: check specials
        count++;
    }
}
Also used : BundleValidationRule(org.hl7.fhir.r5.utils.validation.BundleValidationRule) HashMap(java.util.HashMap) IndexedElement(org.hl7.fhir.validation.instance.utils.IndexedElement) Element(org.hl7.fhir.r5.elementmodel.Element) ArrayList(java.util.ArrayList) NodeStack(org.hl7.fhir.validation.instance.utils.NodeStack) StructureDefinition(org.hl7.fhir.r5.model.StructureDefinition)

Example 47 with FHIRVersion

use of org.hl7.fhir.r5.model.Enumerations.FHIRVersion in project org.hl7.fhir.core by hapifhir.

the class ExtensionDefinitionGenerator method saveResource.

private byte[] saveResource(Resource resource, FHIRVersion v) throws IOException, FHIRException {
    if (v == FHIRVersion._3_0_1) {
        org.hl7.fhir.dstu3.model.Resource res = VersionConvertorFactory_30_40.convertResource(resource, new BaseAdvisor_30_40(false));
        return new org.hl7.fhir.dstu3.formats.JsonParser().composeBytes(res);
    } else if (v == FHIRVersion._1_4_0) {
        org.hl7.fhir.dstu2016may.model.Resource res = VersionConvertorFactory_14_40.convertResource(resource);
        return new org.hl7.fhir.dstu2016may.formats.JsonParser().composeBytes(res);
    } else if (v == FHIRVersion._1_0_2) {
        BaseAdvisor_10_40 advisor = new IGR2ConvertorAdvisor();
        org.hl7.fhir.dstu2.model.Resource res = VersionConvertorFactory_10_40.convertResource(resource, advisor);
        return new org.hl7.fhir.dstu2.formats.JsonParser().composeBytes(res);
    } else if (v == FHIRVersion._4_0_0) {
        return new JsonParser().composeBytes(resource);
    } else
        throw new Error("Unsupported version " + v);
}
Also used : Resource(org.hl7.fhir.r4.model.Resource) IGR2ConvertorAdvisor(org.hl7.fhir.convertors.misc.IGR2ConvertorAdvisor) BaseAdvisor_10_40(org.hl7.fhir.convertors.advisors.impl.BaseAdvisor_10_40) BaseAdvisor_30_40(org.hl7.fhir.convertors.advisors.impl.BaseAdvisor_30_40) JsonParser(org.hl7.fhir.r4.formats.JsonParser)

Example 48 with FHIRVersion

use of org.hl7.fhir.r5.model.Enumerations.FHIRVersion in project org.hl7.fhir.core by hapifhir.

the class StructureDefinition method setProperty.

@Override
public Base setProperty(String name, Base value) throws FHIRException {
    if (name.equals("url")) {
        // UriType
        this.url = castToUri(value);
    } else if (name.equals("identifier")) {
        this.getIdentifier().add(castToIdentifier(value));
    } else if (name.equals("version")) {
        // StringType
        this.version = castToString(value);
    } else if (name.equals("name")) {
        // StringType
        this.name = castToString(value);
    } else if (name.equals("title")) {
        // StringType
        this.title = castToString(value);
    } else if (name.equals("status")) {
        value = new PublicationStatusEnumFactory().fromType(castToCode(value));
        // Enumeration<PublicationStatus>
        this.status = (Enumeration) value;
    } else if (name.equals("experimental")) {
        // BooleanType
        this.experimental = castToBoolean(value);
    } else if (name.equals("date")) {
        // DateTimeType
        this.date = castToDateTime(value);
    } else if (name.equals("publisher")) {
        // StringType
        this.publisher = castToString(value);
    } else if (name.equals("contact")) {
        this.getContact().add(castToContactDetail(value));
    } else if (name.equals("description")) {
        // MarkdownType
        this.description = castToMarkdown(value);
    } else if (name.equals("useContext")) {
        this.getUseContext().add(castToUsageContext(value));
    } else if (name.equals("jurisdiction")) {
        this.getJurisdiction().add(castToCodeableConcept(value));
    } else if (name.equals("purpose")) {
        // MarkdownType
        this.purpose = castToMarkdown(value);
    } else if (name.equals("copyright")) {
        // MarkdownType
        this.copyright = castToMarkdown(value);
    } else if (name.equals("keyword")) {
        this.getKeyword().add(castToCoding(value));
    } else if (name.equals("fhirVersion")) {
        // IdType
        this.fhirVersion = castToId(value);
    } else if (name.equals("mapping")) {
        this.getMapping().add((StructureDefinitionMappingComponent) value);
    } else if (name.equals("kind")) {
        value = new StructureDefinitionKindEnumFactory().fromType(castToCode(value));
        // Enumeration<StructureDefinitionKind>
        this.kind = (Enumeration) value;
    } else if (name.equals("abstract")) {
        // BooleanType
        this.abstract_ = castToBoolean(value);
    } else if (name.equals("contextType")) {
        value = new ExtensionContextEnumFactory().fromType(castToCode(value));
        // Enumeration<ExtensionContext>
        this.contextType = (Enumeration) value;
    } else if (name.equals("context")) {
        this.getContext().add(castToString(value));
    } else if (name.equals("contextInvariant")) {
        this.getContextInvariant().add(castToString(value));
    } else if (name.equals("type")) {
        // CodeType
        this.type = castToCode(value);
    } else if (name.equals("baseDefinition")) {
        // UriType
        this.baseDefinition = castToUri(value);
    } else if (name.equals("derivation")) {
        value = new TypeDerivationRuleEnumFactory().fromType(castToCode(value));
        // Enumeration<TypeDerivationRule>
        this.derivation = (Enumeration) value;
    } else if (name.equals("snapshot")) {
        // StructureDefinitionSnapshotComponent
        this.snapshot = (StructureDefinitionSnapshotComponent) value;
    } else if (name.equals("differential")) {
        // StructureDefinitionDifferentialComponent
        this.differential = (StructureDefinitionDifferentialComponent) value;
    } else
        return super.setProperty(name, value);
    return value;
}
Also used : PublicationStatusEnumFactory(org.hl7.fhir.dstu3.model.Enumerations.PublicationStatusEnumFactory)

Example 49 with FHIRVersion

use of org.hl7.fhir.r5.model.Enumerations.FHIRVersion in project org.hl7.fhir.core by hapifhir.

the class RdfParser method composeStructureDefinition.

protected void composeStructureDefinition(Complex parent, String parentType, String name, StructureDefinition element, int index) {
    if (element == null)
        return;
    Complex t;
    if (Utilities.noString(parentType))
        t = parent;
    else {
        t = parent.predicate("fhir:" + parentType + '.' + name);
    }
    composeDomainResource(t, "StructureDefinition", name, element, index);
    if (element.hasUrlElement())
        composeUri(t, "StructureDefinition", "url", element.getUrlElement(), -1);
    for (int i = 0; i < element.getIdentifier().size(); i++) composeIdentifier(t, "StructureDefinition", "identifier", element.getIdentifier().get(i), i);
    if (element.hasVersionElement())
        composeString(t, "StructureDefinition", "version", element.getVersionElement(), -1);
    if (element.hasNameElement())
        composeString(t, "StructureDefinition", "name", element.getNameElement(), -1);
    if (element.hasTitleElement())
        composeString(t, "StructureDefinition", "title", element.getTitleElement(), -1);
    if (element.hasStatusElement())
        composeEnum(t, "StructureDefinition", "status", element.getStatusElement(), -1);
    if (element.hasExperimentalElement())
        composeBoolean(t, "StructureDefinition", "experimental", element.getExperimentalElement(), -1);
    if (element.hasDateElement())
        composeDateTime(t, "StructureDefinition", "date", element.getDateElement(), -1);
    if (element.hasPublisherElement())
        composeString(t, "StructureDefinition", "publisher", element.getPublisherElement(), -1);
    for (int i = 0; i < element.getContact().size(); i++) composeContactDetail(t, "StructureDefinition", "contact", element.getContact().get(i), i);
    if (element.hasDescriptionElement())
        composeMarkdown(t, "StructureDefinition", "description", element.getDescriptionElement(), -1);
    for (int i = 0; i < element.getUseContext().size(); i++) composeUsageContext(t, "StructureDefinition", "useContext", element.getUseContext().get(i), i);
    for (int i = 0; i < element.getJurisdiction().size(); i++) composeCodeableConcept(t, "StructureDefinition", "jurisdiction", element.getJurisdiction().get(i), i);
    if (element.hasPurposeElement())
        composeMarkdown(t, "StructureDefinition", "purpose", element.getPurposeElement(), -1);
    if (element.hasCopyrightElement())
        composeMarkdown(t, "StructureDefinition", "copyright", element.getCopyrightElement(), -1);
    for (int i = 0; i < element.getKeyword().size(); i++) composeCoding(t, "StructureDefinition", "keyword", element.getKeyword().get(i), i);
    if (element.hasFhirVersionElement())
        composeEnum(t, "StructureDefinition", "fhirVersion", element.getFhirVersionElement(), -1);
    for (int i = 0; i < element.getMapping().size(); i++) composeStructureDefinitionStructureDefinitionMappingComponent(t, "StructureDefinition", "mapping", element.getMapping().get(i), i);
    if (element.hasKindElement())
        composeEnum(t, "StructureDefinition", "kind", element.getKindElement(), -1);
    if (element.hasAbstractElement())
        composeBoolean(t, "StructureDefinition", "abstract", element.getAbstractElement(), -1);
    for (int i = 0; i < element.getContext().size(); i++) composeStructureDefinitionStructureDefinitionContextComponent(t, "StructureDefinition", "context", element.getContext().get(i), i);
    for (int i = 0; i < element.getContextInvariant().size(); i++) composeString(t, "StructureDefinition", "contextInvariant", element.getContextInvariant().get(i), i);
    if (element.hasTypeElement())
        composeUri(t, "StructureDefinition", "type", element.getTypeElement(), -1);
    if (element.hasBaseDefinitionElement())
        composeCanonical(t, "StructureDefinition", "baseDefinition", element.getBaseDefinitionElement(), -1);
    if (element.hasDerivationElement())
        composeEnum(t, "StructureDefinition", "derivation", element.getDerivationElement(), -1);
    if (element.hasSnapshot())
        composeStructureDefinitionStructureDefinitionSnapshotComponent(t, "StructureDefinition", "snapshot", element.getSnapshot(), -1);
    if (element.hasDifferential())
        composeStructureDefinitionStructureDefinitionDifferentialComponent(t, "StructureDefinition", "differential", element.getDifferential(), -1);
}
Also used : Complex(org.hl7.fhir.r4.utils.formats.Turtle.Complex)

Example 50 with FHIRVersion

use of org.hl7.fhir.r5.model.Enumerations.FHIRVersion in project org.hl7.fhir.core by hapifhir.

the class NPMPackageGenerator method buildPackageJson.

private void buildPackageJson(String canonical, PackageType kind, String web, Date date, ImplementationGuide ig, List<String> fhirVersion, boolean notForPublication) throws FHIRException, IOException {
    String dtHuman = new SimpleDateFormat("EEE, MMM d, yyyy HH:mmZ", new Locale("en", "US")).format(date);
    String dt = new SimpleDateFormat("yyyyMMddHHmmss").format(date);
    CommaSeparatedStringBuilder b = new CommaSeparatedStringBuilder();
    if (!ig.hasPackageId()) {
        b.append("packageId");
    }
    if (!ig.hasVersion()) {
        b.append("version");
    }
    if (!ig.hasFhirVersion()) {
        b.append("fhirVersion");
    }
    if (!ig.hasLicense()) {
        b.append("license");
    }
    for (ImplementationGuideDependsOnComponent d : ig.getDependsOn()) {
        if (!d.hasVersion()) {
            b.append("dependsOn.version(" + d.getUri() + ")");
        }
    }
    JsonObject npm = new JsonObject();
    npm.addProperty("name", ig.getPackageId());
    npm.addProperty("version", ig.getVersion());
    igVersion = ig.getVersion();
    npm.addProperty("tools-version", ToolsVersion.TOOLS_VERSION);
    npm.addProperty("type", kind.getCode());
    npm.addProperty("date", dt);
    if (ig.hasLicense()) {
        npm.addProperty("license", ig.getLicense().toCode());
    }
    npm.addProperty("canonical", canonical);
    if (notForPublication) {
        npm.addProperty("notForPublication", true);
    }
    npm.addProperty("url", web);
    if (ig.hasTitle()) {
        npm.addProperty("title", ig.getTitle());
    }
    if (ig.hasDescription()) {
        npm.addProperty("description", ig.getDescription() + " (built " + dtHuman + timezone() + ")");
    }
    JsonArray vl = new JsonArray();
    npm.add("fhirVersions", vl);
    for (String v : fhirVersion) {
        vl.add(new JsonPrimitive(v));
    }
    if (kind != PackageType.CORE) {
        JsonObject dep = new JsonObject();
        npm.add("dependencies", dep);
        for (String v : fhirVersion) {
            String vp = packageForVersion(v);
            if (vp != null) {
                dep.addProperty(vp, v);
            }
        }
        for (ImplementationGuideDependsOnComponent d : ig.getDependsOn()) {
            dep.addProperty(d.getPackageId(), d.getVersion());
        }
    }
    if (ig.hasPublisher()) {
        npm.addProperty("author", ig.getPublisher());
    }
    JsonArray m = new JsonArray();
    for (ContactDetail t : ig.getContact()) {
        String email = email(t.getTelecom());
        String url = url(t.getTelecom());
        if (t.hasName() & (email != null || url != null)) {
            JsonObject md = new JsonObject();
            m.add(md);
            md.addProperty("name", t.getName());
            if (email != null)
                md.addProperty("email", email);
            if (url != null)
                md.addProperty("url", url);
        }
    }
    if (m.size() > 0)
        npm.add("maintainers", m);
    if (ig.getManifest().hasRendering())
        npm.addProperty("homepage", ig.getManifest().getRendering());
    JsonObject dir = new JsonObject();
    npm.add("directories", dir);
    dir.addProperty("lib", "package");
    dir.addProperty("example", "example");
    Gson gson = new GsonBuilder().setPrettyPrinting().create();
    String json = gson.toJson(npm);
    try {
        addFile(Category.RESOURCE, "package.json", json.getBytes("UTF-8"));
    } catch (UnsupportedEncodingException e) {
    }
    packageJ = npm;
    packageManifest = new JsonObject();
    packageManifest.addProperty("version", ig.getVersion());
    packageManifest.addProperty("fhirVersion", fhirVersion.toString());
    packageManifest.addProperty("date", dt);
    packageManifest.addProperty("name", ig.getPackageId());
}
Also used : Locale(java.util.Locale) JsonArray(com.google.gson.JsonArray) ContactDetail(org.hl7.fhir.r4b.model.ContactDetail) ImplementationGuideDependsOnComponent(org.hl7.fhir.r4b.model.ImplementationGuide.ImplementationGuideDependsOnComponent) JsonPrimitive(com.google.gson.JsonPrimitive) GsonBuilder(com.google.gson.GsonBuilder) CommaSeparatedStringBuilder(org.hl7.fhir.utilities.CommaSeparatedStringBuilder) JsonObject(com.google.gson.JsonObject) Gson(com.google.gson.Gson) UnsupportedEncodingException(java.io.UnsupportedEncodingException) SimpleDateFormat(java.text.SimpleDateFormat)

Aggregations

ArrayList (java.util.ArrayList)10 IBaseResource (org.hl7.fhir.instance.model.api.IBaseResource)9 JsonObject (com.google.gson.JsonObject)8 Date (java.util.Date)8 IOException (java.io.IOException)7 FhirContext (ca.uhn.fhir.context.FhirContext)6 JsonArray (com.google.gson.JsonArray)6 HashMap (java.util.HashMap)5 FhirVersionEnum (ca.uhn.fhir.context.FhirVersionEnum)4 IParser (ca.uhn.fhir.parser.IParser)4 Complex (org.hl7.fhir.r4.utils.formats.Turtle.Complex)4 CommaSeparatedStringBuilder (org.hl7.fhir.utilities.CommaSeparatedStringBuilder)4 JsonPrimitive (com.google.gson.JsonPrimitive)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 Complex (org.hl7.fhir.dstu2016may.formats.RdfGenerator.Complex)3 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)3 FHIRException (org.hl7.fhir.exceptions.FHIRException)3 Bundle (org.hl7.fhir.r4.model.Bundle)3 StructureDefinition (org.hl7.fhir.r5.model.StructureDefinition)3 CascadingDeleteInterceptor (ca.uhn.fhir.jpa.interceptor.CascadingDeleteInterceptor)2