Search in sources :

Example 41 with ResourceDefn

use of org.hl7.fhir.definitions.model.ResourceDefn in project kindling by HL7.

the class OldSpreadsheetParser method parseResource.

public ResourceDefn parseResource(boolean isTemplate) throws Exception {
    isProfile = false;
    ResourceDefn root = parseCommonTypeColumns(true, isTemplate);
    readInheritedMappings(root, loadSheet("Inherited Mappings"));
    // readEvents(loadSheet("Events"), root);
    readSearchParams(root, loadSheet("Search"), false);
    if (xls.getSheets().containsKey("Profiles"))
        readPackages(root, loadSheet("Profiles"));
    else
        readPackages(root, loadSheet("Packages"));
    if (!isTemplate)
        readExamples(root, loadSheet("Examples"));
    readOperations(root.getOperations(), loadSheet("Operations"));
    return root;
}
Also used : ResourceDefn(org.hl7.fhir.definitions.model.ResourceDefn)

Example 42 with ResourceDefn

use of org.hl7.fhir.definitions.model.ResourceDefn in project kindling by HL7.

the class OldSpreadsheetParser method parseCommonTypeColumns.

private ResourceDefn parseCommonTypeColumns(boolean isResource, boolean isTemplate) throws Exception {
    ResourceDefn resource = new ResourceDefn();
    resource.setInterface(isTemplate);
    Sheet sheet = loadSheet("Bindings");
    if (sheet != null)
        readBindings(sheet);
    sheet = loadSheet("Invariants");
    Map<String, Invariant> invariants = null;
    if (sheet != null)
        invariants = readInvariants(sheet, title, "Invariants");
    sheet = loadSheet("Data Elements");
    if (sheet == null)
        throw new Exception("No Sheet found for Data Elements");
    for (int row = 0; row < sheet.rows.size(); row++) {
        processLine(resource, sheet, row, invariants, false, null, row == 0);
    }
    // default...
    StandardsStatus ss = StandardsStatus.TRIAL_USE;
    String s = ini.getStringProperty("normative", resource.getName());
    if (!Utilities.noString(s))
        ss = StandardsStatus.NORMATIVE;
    resource.setStatus(ss);
    resource.setRequirements(resource.getRoot().getRequirements());
    resource.addHints(checkIgnoredColumns(sheet));
    if (template != null) {
        resource.setTemplate(template.getRoot());
        copySearchParameters(resource);
        copyInvariants(resource);
        template = null;
    }
    parseMetadata(resource);
    if (invariants != null) {
        for (Invariant inv : invariants.values()) {
            if (Utilities.noString(inv.getContext()))
                throw new Exception("Type " + resource.getRoot().getName() + " Invariant " + inv.getId() + " has no context");
            else {
                ElementDefn ed = findContext(resource.getRoot(), inv.getContext(), "Type " + resource.getRoot().getName() + " Invariant " + inv.getId() + " Context");
                if (ed.getName().endsWith("[x]") && !inv.getContext().endsWith("[x]"))
                    inv.setFixedName(inv.getContext().substring(inv.getContext().lastIndexOf(".") + 1));
                ed.getInvariants().put(inv.getId(), inv);
                if (Utilities.noString(inv.getXpath())) {
                    throw new Exception("Type " + resource.getRoot().getName() + " Invariant " + inv.getId() + " (" + inv.getEnglish() + ") has no XPath statement");
                } else if (inv.getXpath().contains("\""))
                    throw new Exception("Type " + resource.getRoot().getName() + " Invariant " + inv.getId() + " (" + inv.getEnglish() + ") contains a \" character");
                if (Utilities.noString(inv.getExpression())) {
                // This has been disabled for now, per Lloyd McKenzie's request via Skype - jamesagnew
                // throw new Exception("Type "+resource.getRoot().getName()+" Invariant "+inv.getId()+" ("+inv.getEnglish()+") has no Expression statement (in FHIRPath format)");
                } else {
                    fpUsages.add(new FHIRPathUsage(inv.getContext(), isResource ? resource.getName() : "DomainResource", inv.getContext(), null, inv.getExpression(), inv.getXpath()));
                }
            }
        }
    }
    // EK: Future types. But those won't get there.
    if (bindings != null)
        resource.getRoot().getNestedBindings().putAll(bindings);
    scanNestedTypes(resource, resource.getRoot(), resource.getName());
    resolveElementReferences(resource, resource.getRoot());
    resource.getRoot().setAbstractType(isAbstract);
    return resource;
}
Also used : Invariant(org.hl7.fhir.definitions.model.Invariant) ElementDefn(org.hl7.fhir.definitions.model.ElementDefn) FHIRPathUsage(org.hl7.fhir.definitions.validation.FHIRPathUsage) ResourceDefn(org.hl7.fhir.definitions.model.ResourceDefn) Sheet(org.hl7.fhir.utilities.xls.XLSXmlParser.Sheet) StandardsStatus(org.hl7.fhir.utilities.StandardsStatus) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Example 43 with ResourceDefn

use of org.hl7.fhir.definitions.model.ResourceDefn in project kindling by HL7.

the class OldSpreadsheetParser method scanNestedTypes.

private void scanNestedTypes(ResourceDefn parent, ElementDefn root, String parentName) throws Exception {
    for (ElementDefn element : root.getElements()) {
        if (element.hasNestedElements()) {
            String nestedTypeName;
            ElementDefn newCompositeType = new ElementDefn();
            // generated name for this nested type
            if (element.typeCode().startsWith("=")) {
                if (isProfile)
                    throw new Exception("Cannot use '=' types in profiles on " + parentName);
                element.setStatedType(element.typeCode().substring(1));
                nestedTypeName = element.typeCode().substring(1);
            } else {
                nestedTypeName = parentName + Utilities.capitalize(element.getName());
            }
            newCompositeType.setAnonymousTypedGroup(true);
            // Add Component to the actually generated name to avoid
            // confusing between the element name and the element's type
            newCompositeType.setName(nestedTypeName + "Component");
            newCompositeType.setDefinition("A nested type in " + parent.getName() + ": " + element.getDefinition());
            newCompositeType.getElements().addAll(element.getElements());
            if (parent.getRoot().getNestedTypes().containsKey(nestedTypeName))
                throw new Exception("Nested type " + nestedTypeName + " already exist in resource " + parent.getName());
            parent.getRoot().getNestedTypes().put(nestedTypeName, newCompositeType);
            // Clear out the name of the local type, so old code
            // will not see a change.
            element.getTypes().clear();
            element.setDeclaredTypeName(newCompositeType.getName());
            scanNestedTypes(parent, element, nestedTypeName);
        }
    }
}
Also used : ElementDefn(org.hl7.fhir.definitions.model.ElementDefn) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Example 44 with ResourceDefn

use of org.hl7.fhir.definitions.model.ResourceDefn in project kindling by HL7.

the class OldSpreadsheetParser method readSearchParams.

private void readSearchParams(ResourceDefn root2, Sheet sheet, boolean forProfile) throws Exception {
    if (sheet != null) {
        for (int row = 0; row < sheet.rows.size(); row++) {
            if (!sheet.hasColumn(row, "Name"))
                throw new Exception("Search Param has no name " + getLocation(row));
            String n = sheet.getColumn(row, "Name");
            if (!n.startsWith("!")) {
                if (!sheet.hasColumn(row, "Type"))
                    throw new Exception("Search Param " + root2.getName() + "/" + n + " has no type " + getLocation(row));
                if (n.endsWith("-before") || n.endsWith("-after"))
                    throw new Exception("Search Param " + root2.getName() + "/" + n + " includes relative time " + getLocation(row));
                if (root2.getSearchParams().containsKey(n))
                    throw new Exception("Search Param " + root2.getName() + "/" + n + ": duplicate name " + getLocation(row));
                String d = sheet.getColumn(row, "Description");
                SearchType t = readSearchType(sheet.getColumn(row, "Type"), row);
                SearchParameter.XPathUsageType pu = readSearchXPathUsage(sheet.getColumn(row, "Path Usage"), row);
                if (Utilities.noString(sheet.getColumn(row, "Path")) && !root2.getName().equals("Resource") && !root2.getName().equals("DomainResource"))
                    throw new Exception("Search Param " + root2.getName() + "/" + n + " has no path at " + getLocation(row));
                SearchParameterDefn sp = null;
                if (t == SearchType.composite) {
                    List<CompositeDefinition> pn = new ArrayList<CompositeDefinition>();
                    if (Utilities.noString(d))
                        throw new Exception("Search Param " + root2.getName() + "/" + n + " has no description " + getLocation(row));
                    String[] pl = sheet.getColumn(row, "Path").split("\\&");
                    String[] pe = sheet.getColumn(row, "Expression").split("\\;");
                    if (pe.length != pl.length + 1)
                        throw new Exception("Composite Search Param " + root2.getName() + "/" + n + " needs expressions " + getLocation(row));
                    int i = 0;
                    for (String pi : pl) {
                        String p = pi.trim();
                        i++;
                        String e = pe[i].trim();
                        if (!root2.getSearchParams().containsKey(p)) {
                            boolean found = false;
                            if (p.endsWith("[x]"))
                                for (String pan : root2.getSearchParams().keySet()) {
                                    if (pan.startsWith(p.substring(0, p.length() - 3)))
                                        found = true;
                                }
                            if (!found)
                                throw new Exception("Composite Search Param " + root2.getName() + "/" + n + "  refers to an unknown component " + p + " at " + getLocation(row));
                        }
                        pn.add(new CompositeDefinition(p, e));
                    }
                    StandardsStatus ss = root2.getStatus();
                    if (!Utilities.noString(sheet.getColumn(row, "Standards-Status")))
                        ss = StandardsStatus.fromCode(sheet.getColumn(row, "Standards-Status"));
                    sp = new SearchParameterDefn(n, d, t, pu, ss);
                    sp.setExpression(pe[0].trim());
                    sp.getComposites().addAll(pn);
                } else {
                    List<String> pn = new ArrayList<String>();
                    String xp = sheet.getColumn(row, "XPath");
                    String[] pl = sheet.getColumn(row, "Path").split("\\|");
                    boolean hierarchy = false;
                    for (String pi : pl) {
                        String p = pi.trim();
                        ElementDefn e = null;
                        if (!Utilities.noString(p) && !p.startsWith("!") && !p.startsWith("Extension{") && definitions != null) {
                            e = root2.getRoot().getElementForPath(trimIndexes(p), definitions, "search param", true, true);
                        }
                        if (e != null && e.hasHierarchy() && e.getHierarchy())
                            hierarchy = true;
                        if (Utilities.noString(d) && e != null)
                            d = e.getShortDefn();
                        if (p.startsWith("Extension(")) {
                            String url = extractExtensionUrl(p);
                            StructureDefinition ex = context.fetchResource(StructureDefinition.class, url);
                            if (ex == null)
                                throw new Exception("Search Param " + root2.getName() + "/" + n + " refers to unknown extension '" + url + "' " + getLocation(row));
                            if (Utilities.noString(d))
                                d = ex.getDescription();
                            pn.add(p);
                        }
                        if (d == null)
                            throw new Exception("Search Param " + root2.getName() + "/" + n + " has no description " + getLocation(row));
                        if (e != null)
                            pn.add(p);
                        if (t == SearchType.reference) {
                            if (e == null && !forProfile && !sheet.hasColumn(row, "Target Types"))
                                throw new Exception("Search Param " + root2.getName() + "/" + n + " of type reference has wrong path '" + p + "' at " + getLocation(row));
                            if (!forProfile && e != null && (!e.hasType("Reference")) && (!e.hasType("canonical")) && (!e.hasType("Resource")))
                                throw new Exception("Search Param " + root2.getName() + "/" + n + " wrong type. The search type is reference, but the element type is " + e.typeCode());
                        } else {
                            if (e != null && e.hasOnlyType("Reference"))
                                throw new Exception("Search Param " + root2.getName() + "/" + n + " wrong type. The search type is " + t.toString() + ", but the element type is " + e.typeCode());
                            if (t == SearchType.uri) {
                                if (e != null && !(e.typeCode().equals("uri") || e.typeCode().equals("url") || e.typeCode().equals("oid") || e.typeCode().startsWith("canonical(")))
                                    throw new Exception("Search Param " + root2.getName() + "/" + n + " wrong type. The search type is " + t.toString() + ", but the element type is " + e.typeCode());
                            } else {
                                if (e != null && e.typeCode().equals("uri"))
                                    throw new Exception("Search Param " + root2.getName() + "/" + n + " wrong type. The search type is " + t.toString() + ", but the element type is " + e.typeCode());
                            }
                        }
                    }
                    if (!forProfile && t == SearchType.reference && pn.size() == 0 && !sheet.hasColumn(row, "Target Types"))
                        throw new Exception("Search Param " + root2.getName() + "/" + n + " of type reference has no path(s) " + getLocation(row));
                    StandardsStatus ss = root2.getStatus();
                    if (!Utilities.noString(sheet.getColumn(row, "Standards-Status")))
                        ss = StandardsStatus.fromCode(sheet.getColumn(row, "Standards-Status"));
                    sp = new SearchParameterDefn(n, d, t, pu, ss);
                    sp.getPaths().addAll(pn);
                    if (!Utilities.noString(xp))
                        sp.setXPath(xp);
                    if (!Utilities.noString(sheet.getColumn(row, "Expression")))
                        sp.setExpression(sheet.getColumn(row, "Expression"));
                    if (!Utilities.noString(sheet.getColumn(row, "Target Types"))) {
                        sp.setManualTypes(sheet.getColumn(row, "Target Types").split("\\,"));
                    }
                    sp.setHierarchy(hierarchy);
                    CommonSearchParameter csp = definitions.getCommonSearchParameters().get(root2.getName() + "::" + n);
                    if (csp != null)
                        for (String s : csp.getResources()) {
                            if (!root2.getName().equals(s))
                                sp.getOtherResources().add(s);
                        }
                }
                root2.getSearchParams().put(n, sp);
            }
        }
    }
}
Also used : SearchParameterDefn(org.hl7.fhir.definitions.model.SearchParameterDefn) ArrayList(java.util.ArrayList) ElementDefn(org.hl7.fhir.definitions.model.ElementDefn) FHIRException(org.hl7.fhir.exceptions.FHIRException) CompositeDefinition(org.hl7.fhir.definitions.model.SearchParameterDefn.CompositeDefinition) StructureDefinition(org.hl7.fhir.r5.model.StructureDefinition) CommonSearchParameter(org.hl7.fhir.definitions.model.CommonSearchParameter) SearchType(org.hl7.fhir.definitions.model.SearchParameterDefn.SearchType) CommonSearchParameter(org.hl7.fhir.definitions.model.CommonSearchParameter) SearchParameter(org.hl7.fhir.r5.model.SearchParameter) StandardsStatus(org.hl7.fhir.utilities.StandardsStatus)

Example 45 with ResourceDefn

use of org.hl7.fhir.definitions.model.ResourceDefn in project kindling by HL7.

the class OldSpreadsheetParser method readSearchParams.

/* for profiles that have a "search" tab not tied to a structure */
private void readSearchParams(Profile pack, Sheet sheet, String prefix) throws Exception {
    for (int row = 0; row < sheet.rows.size(); row++) {
        if (!sheet.hasColumn(row, "Name"))
            throw new Exception("Search Param has no name " + getLocation(row));
        String n = sheet.getColumn(row, "Name");
        if (!n.startsWith("!")) {
            SearchParameter sp = new SearchParameter();
            if (!sheet.hasColumn(row, "Type"))
                throw new Exception("Search Param " + pack.getTitle() + "/" + n + " has no type " + getLocation(row));
            if (n.endsWith("-before") || n.endsWith("-after"))
                throw new Exception("Search Param " + pack.getTitle() + "/" + n + " includes relative time " + getLocation(row));
            // if (!n.toLowerCase().equals(n))
            // throw new Exception("Search Param "+pack.getTitle()+"/"+n+" must be all lowercase "+ getLocation(row));
            sp.setVersion(version.toCode());
            sp.setName(n);
            sp.setCode(n);
            if (pack.getProfiles().size() > 0 && pack.getProfiles().get(0).getResource() != null) {
                sp.setStatus(pack.getProfiles().get(0).getResource().getStatus());
                sp.setExperimental(pack.getProfiles().get(0).getResource().getExperimental());
            } else {
                // we just guess
                sp.setStatus(PublicationStatus.DRAFT);
                sp.setExperimental(true);
            }
            String d = sheet.getColumn(row, "Description");
            sp.setType(SearchParamType.fromCode(sheet.getColumn(row, "Type")));
            List<String> pn = new ArrayList<String>();
            String path = sheet.getColumn(row, "Path");
            if (Utilities.noString(path))
                throw new Exception("Search Param " + pack.getTitle() + "/" + n + " has no path");
            if (!path.contains(".") && !path.startsWith("#"))
                throw new Exception("Search Param " + pack.getTitle() + "/" + n + " has an invalid path: " + path);
            ResourceDefn root2 = null;
            if (!path.startsWith("#")) {
                path = path.substring(0, path.indexOf('.'));
                if (!pkp.isResource(path))
                    throw new Exception("Ilegal Search Parameter path " + sheet.getColumn(row, "Path"));
                sp.addBase(path);
                sp.setId(pack.getId() + "-" + path + "-" + sp.getName());
                if (definitions != null) {
                    // igtodo (and below)
                    root2 = definitions.getResourceByName(path);
                    if (root2 == null)
                        throw new Exception("Search Param " + pack.getTitle() + "/" + n + " has an invalid path (resource not found)");
                    if (!pkp.isResource(root2.getName()))
                        throw new Exception("Ilegal Search Parameter path " + sheet.getColumn(row, "Path"));
                    sp.getBase().clear();
                    sp.addBase(root2.getName());
                    sp.setId(pack.getId() + "-" + (root2 == null ? "all" : root2.getName()) + "-" + sp.getName());
                }
            }
            if (!Utilities.noString(sheet.getColumn(row, "Target Types")))
                throw new Exception("Search Param " + pack.getTitle() + "/" + n + " has manually specified targets (not allowed)");
            if (root2 != null && root2.getSearchParams().containsKey(n))
                throw new Exception("Search Param " + root2.getName() + "/" + n + ": duplicate name " + getLocation(row));
            if (sp.getType() == SearchParamType.COMPOSITE) {
                throw new Exception("not supported");
            } else {
                String[] pl = sheet.getColumn(row, "Path").split("\\|");
                String xp = sheet.getColumn(row, "XPath");
                for (String pi : pl) {
                    String p = pi.trim();
                    ElementDefn e = null;
                    if (Utilities.noString(p))
                        throw new Exception("Search Param " + root2.getName() + "/" + n + ": empty path " + getLocation(row));
                    if (p.startsWith("#")) {
                        // root less extension search parameter
                        StructureDefinition ex = pack.getExtension(prefix + p.substring(1));
                        if (ex == null)
                            throw new Exception("Search Param " + pack.getTitle() + "/" + n + " refers to unknown extension '" + p + "' " + getLocation(row));
                        e = definitions.getElementDefn("Extension");
                        if (ex.getContext().size() != 1 || ex.getContext().get(0).getType() != ExtensionContextType.ELEMENT)
                            throw new Exception("Search Param " + pack.getTitle() + "/" + n + " refers to an extension with multiple contexts, not not an element context - not supported '" + p + "' " + getLocation(row));
                        path = ex.getContext().get(0).getExpression();
                        if (Utilities.noString(path))
                            throw new Exception("Search Param " + pack.getTitle() + "/" + n + " has no path");
                        if (path.contains("."))
                            path = path.substring(0, path.indexOf('.'));
                        sp.setId(pack.getId() + "-" + path + "-" + sp.getName());
                        root2 = definitions.getResourceByName(path);
                        if (root2 == null)
                            throw new Exception("Search Param " + pack.getTitle() + "/" + n + " has an invalid path (resource not found)");
                        if (root2 != null && root2.getSearchParams().containsKey(n))
                            throw new Exception("Search Param " + root2.getName() + "/" + n + ": duplicate name " + getLocation(row));
                        sp.setId(pack.getId() + "-" + path + "-" + sp.getName());
                        pn.add(ex.getContext().get(0).getExpression() + ".extension{" + ex.getUrl() + "}");
                    } else if (p.contains(".extension{")) {
                        String url = extractExtensionUrl(p);
                        // not created yet?
                        StructureDefinition ex = context.fetchResource(StructureDefinition.class, url);
                        if (ex == null)
                            ex = context.getExtensionStructure(null, url);
                        if (ex == null)
                            throw new Exception("Search Param " + pack.getTitle() + "/" + n + " refers to unknown extension '" + url + "' " + getLocation(row));
                        if (Utilities.noString(d))
                            d = ex.getDescription();
                        if (definitions != null)
                            e = definitions.getElementDefn("Extension");
                        pn.add(p);
                    } else if (!p.startsWith("!") && !p.startsWith("Extension{") && root2 != null) {
                        e = root2.getRoot().getElementForPath(p, definitions, "search param", true, true);
                    }
                    if (e == null && Utilities.noString(d))
                        throw new Exception("unable to resolve sarch param " + p);
                    if (e == null)
                        sp.setExpression(p);
                    if (Utilities.noString(d) && e != null)
                        d = e.getShortDefn();
                    if (d == null)
                        throw new Exception("Search Param " + root2.getName() + "/" + n + " has no description " + getLocation(row));
                    if (e != null)
                        pn.add(p);
                    if (sp.getType() == SearchParamType.REFERENCE) {
                    // no check?
                    } else if (e != null && e.typeCode().startsWith("Reference("))
                        throw new Exception("Search Param " + root2.getName() + "/" + n + " wrong type. The search type is " + sp.getType().toCode() + ", but the element type is " + e.typeCode());
                    sp.setDescription(d);
                }
                sp.setXpath(Utilities.noString(xp) ? new XPathQueryGenerator(definitions, log, null).generateXpath(pn, null) : xp);
                sp.setXpathUsage(readSearchXPathUsage(sheet.getColumn(row, "Path Usage"), row));
            }
            sp.setUrl("http://hl7.org/fhir/SearchParameter/" + sp.getId());
            if (definitions != null)
                definitions.addNs(sp.getUrl(), "Search Parameter " + sp.getName(), pack.getId() + ".html#search");
            if (context.getSearchParameter(sp.getUrl()) != null)
                throw new Exception("Duplicated Search Parameter " + sp.getUrl());
            context.cacheResource(sp);
            pack.getSearchParameters().add(sp);
        }
    }
}
Also used : StructureDefinition(org.hl7.fhir.r5.model.StructureDefinition) ArrayList(java.util.ArrayList) ElementDefn(org.hl7.fhir.definitions.model.ElementDefn) XPathQueryGenerator(org.hl7.fhir.definitions.generators.specification.XPathQueryGenerator) CommonSearchParameter(org.hl7.fhir.definitions.model.CommonSearchParameter) SearchParameter(org.hl7.fhir.r5.model.SearchParameter) ResourceDefn(org.hl7.fhir.definitions.model.ResourceDefn) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Aggregations

ResourceDefn (org.hl7.fhir.definitions.model.ResourceDefn)75 CommaSeparatedStringBuilder (org.hl7.fhir.utilities.CommaSeparatedStringBuilder)42 ArrayList (java.util.ArrayList)38 FHIRException (org.hl7.fhir.exceptions.FHIRException)36 ElementDefn (org.hl7.fhir.definitions.model.ElementDefn)31 StructureDefinition (org.hl7.fhir.r5.model.StructureDefinition)28 File (java.io.File)26 FileOutputStream (java.io.FileOutputStream)25 CSFile (org.hl7.fhir.utilities.CSFile)24 XmlParser (org.hl7.fhir.r5.formats.XmlParser)22 FileNotFoundException (java.io.FileNotFoundException)20 IOException (java.io.IOException)20 Example (org.hl7.fhir.definitions.model.Example)20 Profile (org.hl7.fhir.definitions.model.Profile)20 ContactPoint (org.hl7.fhir.r5.model.ContactPoint)18 IniFile (org.hl7.fhir.utilities.IniFile)18 ImplementationGuideDefn (org.hl7.fhir.definitions.model.ImplementationGuideDefn)16 TransformerException (javax.xml.transform.TransformerException)15 TextFile (org.hl7.fhir.utilities.TextFile)15 SearchParameterDefn (org.hl7.fhir.definitions.model.SearchParameterDefn)14