Search in sources :

Example 11 with Sheet

use of org.hl7.fhir.utilities.xls.XLSXmlParser.Sheet 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)

Example 12 with Sheet

use of org.hl7.fhir.utilities.xls.XLSXmlParser.Sheet in project kindling by HL7.

the class SourceParser method loadCompositeType.

private String loadCompositeType(String n, Map<String, org.hl7.fhir.definitions.model.TypeDefn> map, String fmm, boolean isAbstract) throws Exception {
    TypeParser tp = new TypeParser(version.toString());
    List<TypeRef> ts = tp.parse(n, false, null, context, true);
    definitions.getKnownTypes().addAll(ts);
    StandardsStatus status = loadStatus(n);
    String nv = loadNormativeVersion(n);
    try {
        TypeRef t = ts.get(0);
        File csv = new CSFile(dtDir + t.getName().toLowerCase() + ".xml");
        if (csv.exists()) {
            OldSpreadsheetParser p = new OldSpreadsheetParser("core", new CSFileInputStream(csv), csv.getName(), csv.getAbsolutePath(), definitions, srcDir, logger, registry, version, context, genDate, isAbstract, page, true, ini, wg("fhir"), definitions.getProfileIds(), fpUsages, page.getConceptMaps(), exceptionIfExcelNotNormalised, page.packageInfo(), page.getRc());
            org.hl7.fhir.definitions.model.TypeDefn el = p.parseCompositeType();
            el.setFmmLevel(fmm);
            el.setStandardsStatus(status);
            el.setNormativeVersion(nv);
            map.put(t.getName(), el);
            genTypeProfile(el);
            errors.addAll(p.getErrors());
            return el.getName();
        } else {
            String p = ini.getStringProperty("types", n);
            csv = new CSFile(dtDir + p.toLowerCase() + ".xml");
            if (!csv.exists())
                throw new Exception("unable to find a definition for " + n + " in " + p);
            XLSXmlParser xls = new XLSXmlParser(new CSFileInputStream(csv), csv.getAbsolutePath());
            new XLSXmlNormaliser(csv.getAbsolutePath(), exceptionIfExcelNotNormalised).go();
            Sheet sheet = xls.getSheets().get("Restrictions");
            boolean found = false;
            for (int i = 0; i < sheet.rows.size(); i++) {
                if (sheet.getColumn(i, "Name").equals(n)) {
                    found = true;
                    Invariant inv = new Invariant();
                    inv.setId(n);
                    inv.setEnglish(sheet.getColumn(i, "Rules"));
                    inv.setOcl(sheet.getColumn(i, "OCL"));
                    inv.setXpath(sheet.getColumn(i, "XPath"));
                    inv.setExpression(sheet.getColumn(i, "Expression"));
                    inv.setExplanation(sheet.getColumn(i, "Explanation"));
                    inv.setTurtle(sheet.getColumn(i, "RDF"));
                    ProfiledType pt = new ProfiledType();
                    pt.setDefinition(sheet.getColumn(i, "Definition"));
                    pt.setDescription(sheet.getColumn(i, "Rules"));
                    String structure = sheet.getColumn(i, "Structure");
                    if (!Utilities.noString(structure)) {
                        String[] parts = structure.split("\\;");
                        for (String pp : parts) {
                            String[] words = pp.split("\\=");
                            pt.getRules().put(words[0], words[1]);
                        }
                    }
                    pt.setName(n);
                    pt.setBaseType(p);
                    pt.setInvariant(inv);
                    definitions.getConstraints().put(n, pt);
                }
            }
            if (!found)
                throw new Exception("Unable to find definition for " + n);
            return n;
        }
    } catch (Exception e) {
        throw new Exception("Unable to load " + n + ": " + e.getMessage(), e);
    }
}
Also used : Invariant(org.hl7.fhir.definitions.model.Invariant) ProfiledType(org.hl7.fhir.definitions.model.ProfiledType) TypeRef(org.hl7.fhir.definitions.model.TypeRef) XLSXmlParser(org.hl7.fhir.utilities.xls.XLSXmlParser) XLSXmlNormaliser(org.hl7.fhir.utilities.xls.XLSXmlNormaliser) CSFile(org.hl7.fhir.utilities.CSFile) IOException(java.io.IOException) FHIRException(org.hl7.fhir.exceptions.FHIRException) FileNotFoundException(java.io.FileNotFoundException) SAXException(org.xml.sax.SAXException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) OldSpreadsheetParser(org.hl7.fhir.definitions.parsers.spreadsheets.OldSpreadsheetParser) TypeDefn(org.hl7.fhir.definitions.model.TypeDefn) StandardsStatus(org.hl7.fhir.utilities.StandardsStatus) IniFile(org.hl7.fhir.utilities.IniFile) File(java.io.File) CSFile(org.hl7.fhir.utilities.CSFile) TextFile(org.hl7.fhir.utilities.TextFile) Sheet(org.hl7.fhir.utilities.xls.XLSXmlParser.Sheet) CSFileInputStream(org.hl7.fhir.utilities.CSFileInputStream)

Example 13 with Sheet

use of org.hl7.fhir.utilities.xls.XLSXmlParser.Sheet in project kindling by HL7.

the class SourceParser method loadStatusCodes.

private void loadStatusCodes() throws FileNotFoundException, Exception {
    XLSXmlParser xml = new XLSXmlParser(new CSFileInputStream(srcDir + "status-codes.xml"), "Status Codes");
    new XLSXmlNormaliser(srcDir + "status-codes.xml", exceptionIfExcelNotNormalised).go();
    Sheet sheet = xml.getSheets().get("Status Codes");
    for (int row = 0; row < sheet.rows.size(); row++) {
        String path = sheet.getColumn(row, "Path");
        ArrayList<String> codes = new ArrayList<String>();
        for (int i = 1; i <= 80; i++) {
            String s = sheet.getColumn(row, "c" + Integer.toString(i));
            if (s.endsWith("?"))
                s = s.substring(0, s.length() - 1);
            codes.add(s);
        }
        definitions.getStatusCodes().put(path, codes);
    }
}
Also used : ArrayList(java.util.ArrayList) XLSXmlParser(org.hl7.fhir.utilities.xls.XLSXmlParser) XLSXmlNormaliser(org.hl7.fhir.utilities.xls.XLSXmlNormaliser) Sheet(org.hl7.fhir.utilities.xls.XLSXmlParser.Sheet) CSFileInputStream(org.hl7.fhir.utilities.CSFileInputStream)

Example 14 with Sheet

use of org.hl7.fhir.utilities.xls.XLSXmlParser.Sheet in project kindling by HL7.

the class SpreadSheetCreator method addOpParam.

private int addOpParam(XSSFSheet sheet, XSSFSheet bindings, String path, OperationDefinitionParameterComponent param, int rowCount) {
    path = path + "." + param.getName();
    Row row = sheet.createRow(rowCount++);
    int columnCount = 0;
    addCell(path, row, columnCount++);
    addCell(use(param), row, columnCount++);
    addCell("" + param.getMin() + ".." + param.getMax(), row, columnCount++);
    addCell(type(param), row, columnCount++);
    addCell(bind(param, bindings), row, columnCount++);
    addCell(null, row, columnCount++);
    addCell(param.getDocumentation(), row, columnCount++);
    addCell(null, row, columnCount++);
    addCell(null, row, columnCount++);
    addCell(null, row, columnCount++);
    addCell(null, row, columnCount++);
    addCell(null, row, columnCount++);
    addCell(status(param), row, columnCount++);
    addCell(ext(param, BuildExtensions.EXT_COMMITTEE_NOTES), row, columnCount++);
    for (OperationDefinitionParameterComponent pp : param.getPart()) {
        rowCount = addOpParam(sheet, bindings, path, pp, rowCount);
    }
    return rowCount;
}
Also used : Row(org.apache.poi.ss.usermodel.Row) OperationDefinitionParameterComponent(org.hl7.fhir.r5.model.OperationDefinition.OperationDefinitionParameterComponent)

Example 15 with Sheet

use of org.hl7.fhir.utilities.xls.XLSXmlParser.Sheet in project kindling by HL7.

the class SpreadSheetReloader method processPacks.

private void processPacks(XSSFWorkbook excel) throws FHIRFormatError, FileNotFoundException, IOException {
    ListResource list = (ListResource) parseXml(fnPacks());
    list.getEntry().clear();
    for (int i = 0; i < excel.getNumberOfSheets(); i++) {
        XSSFSheet sheet = excel.getSheetAt(i);
        if (sheet.getSheetName().startsWith("Pack ")) {
            String n = sheet.getSheetName().substring(5);
            list.addEntry().getItem().setReference("ImplementationGuide/" + resourceName + "-" + n);
            processPack(sheet, n);
        }
    }
    saveXml(fnPacks(), list);
}
Also used : XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) ListResource(org.hl7.fhir.r5.model.ListResource)

Aggregations

FHIRException (org.hl7.fhir.exceptions.FHIRException)18 Sheet (org.hl7.fhir.utilities.xls.XLSXmlParser.Sheet)15 Sheet (org.apache.poi.ss.usermodel.Sheet)14 Row (org.apache.poi.ss.usermodel.Row)13 XSSFSheet (org.apache.poi.xssf.usermodel.XSSFSheet)12 ArrayList (java.util.ArrayList)8 XSSFRow (org.apache.poi.xssf.usermodel.XSSFRow)8 ElementDefn (org.hl7.fhir.definitions.model.ElementDefn)8 Invariant (org.hl7.fhir.definitions.model.Invariant)8 XLSXmlParser (org.hl7.fhir.utilities.xls.XLSXmlParser)8 TypeRef (org.hl7.fhir.definitions.model.TypeRef)7 File (java.io.File)6 BindingSpecification (org.hl7.fhir.definitions.model.BindingSpecification)5 ResourceDefn (org.hl7.fhir.definitions.model.ResourceDefn)5 IniFile (org.hl7.fhir.utilities.IniFile)5 HashMap (java.util.HashMap)4 ConditionalFormattingRule (org.apache.poi.ss.usermodel.ConditionalFormattingRule)4 FontFormatting (org.apache.poi.ss.usermodel.FontFormatting)4 PatternFormatting (org.apache.poi.ss.usermodel.PatternFormatting)4 SheetConditionalFormatting (org.apache.poi.ss.usermodel.SheetConditionalFormatting)4