use of org.hl7.fhir.definitions.generators.specification.XPathQueryGenerator 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);
}
}
}
use of org.hl7.fhir.definitions.generators.specification.XPathQueryGenerator in project kindling by HL7.
the class ProfileGenerator method makeSearchParam.
public SearchParameter makeSearchParam(StructureDefinition p, String id, String rn, SearchParameterDefn spd, ResourceDefn rd) throws Exception {
boolean shared;
boolean created = true;
SearchParameter sp;
if (definitions.getCommonSearchParameters().containsKey(rn + "::" + spd.getCode())) {
shared = true;
CommonSearchParameter csp = definitions.getCommonSearchParameters().get(rn + "::" + spd.getCode());
if (csp.getDefinition() == null) {
sp = new SearchParameter();
csp.setDefinition(sp);
sp.setId(csp.getId());
} else {
created = false;
sp = csp.getDefinition();
}
} else {
shared = false;
sp = new SearchParameter();
sp.setId(id.replace("[", "").replace("]", ""));
}
spd.setCommonId(sp.getId());
if (created) {
sp.setUrl("http://hl7.org/fhir/SearchParameter/" + sp.getId());
sp.setVersion(version.toCode());
if (context.getSearchParameter(sp.getUrl()) != null)
throw new Exception("Duplicated Search Parameter " + sp.getUrl());
context.cacheResource(sp);
spd.setResource(sp);
definitions.addNs(sp.getUrl(), "Search Parameter: " + sp.getName(), rn.toLowerCase() + ".html#search");
sp.setStatus(spd.getStandardsStatus() == StandardsStatus.NORMATIVE ? PublicationStatus.fromCode("active") : PublicationStatus.fromCode("draft"));
StandardsStatus sst = ToolingExtensions.getStandardsStatus(sp);
if (sst == null || (spd.getStandardsStatus() == null && spd.getStandardsStatus().isLowerThan(sst)))
ToolingExtensions.setStandardsStatus(sp, spd.getStandardsStatus(), spd.getNormativeVersion());
sp.setExperimental(p.getExperimental());
sp.setName(spd.getCode());
sp.setCode(spd.getCode());
sp.setDate(genDate.getTime());
sp.setPublisher(p.getPublisher());
for (ContactDetail tc : p.getContact()) {
ContactDetail t = sp.addContact();
if (tc.hasNameElement())
t.setNameElement(tc.getNameElement().copy());
for (ContactPoint ts : tc.getTelecom()) t.getTelecom().add(ts.copy());
}
if (!definitions.hasResource(p.getType()) && !p.getType().equals("Resource") && !p.getType().equals("DomainResource"))
throw new Exception("unknown resource type " + p.getType());
sp.setType(getSearchParamType(spd.getType()));
if (sp.getType() == SearchParamType.REFERENCE && spd.isHierarchy()) {
sp.addModifier(SearchParameter.SearchModifierCode.BELOW);
sp.addModifier(SearchParameter.SearchModifierCode.ABOVE);
}
if (shared) {
sp.setDescription("Multiple Resources: \r\n\r\n* [" + rn + "](" + rn.toLowerCase() + ".html): " + spd.getDescription() + "\r\n");
} else
sp.setDescription(preProcessMarkdown(spd.getDescription(), "Search Description"));
if (!Utilities.noString(spd.getExpression()))
sp.setExpression(spd.getExpression());
// addModifiers(sp);
addComparators(sp);
String xpath = Utilities.noString(spd.getXPath()) ? new XPathQueryGenerator(this.definitions, null, null).generateXpath(spd.getPaths(), rn) : spd.getXPath();
if (xpath != null) {
if (xpath.contains("[x]"))
xpath = convertToXpath(xpath);
sp.setXpath(xpath);
sp.setXpathUsage(spd.getxPathUsage());
}
if (sp.getType() == SearchParamType.COMPOSITE) {
for (CompositeDefinition cs : spd.getComposites()) {
SearchParameterDefn cspd = findSearchParameter(rd, cs.getDefinition());
if (cspd != null)
sp.addComponent().setExpression(cs.getExpression()).setDefinition(cspd.getUrl());
else
sp.addComponent().setExpression(cs.getExpression()).setDefinition("http://hl7.org/fhir/SearchParameter/" + rn + "-" + cs.getDefinition());
}
sp.setMultipleOr(false);
}
sp.addBase(p.getType());
} else {
if (sp.getType() != getSearchParamType(spd.getType()))
throw new FHIRException("Type mismatch on common parameter: expected " + sp.getType().toCode() + " but found " + getSearchParamType(spd.getType()).toCode());
if (!sp.getDescription().contains("[" + rn + "](" + rn.toLowerCase() + ".html)"))
sp.setDescription(sp.getDescription() + "* [" + rn + "](" + rn.toLowerCase() + ".html): " + spd.getDescription() + "\r\n");
// ext.addExtension("description", new MarkdownType(spd.getDescription()));
if (!Utilities.noString(spd.getExpression()) && !sp.getExpression().contains(spd.getExpression()))
sp.setExpression(sp.getExpression() + " | " + spd.getExpression());
String xpath = new XPathQueryGenerator(this.definitions, null, null).generateXpath(spd.getPaths(), rn);
if (xpath != null) {
if (xpath.contains("[x]"))
xpath = convertToXpath(xpath);
if (sp.getXpath() != null && !sp.getXpath().contains(xpath))
sp.setXpath(sp.getXpath() + " | " + xpath);
if (sp.getXpathUsage() != spd.getxPathUsage())
throw new FHIRException("Usage mismatch on common parameter: expected " + sp.getXpathUsage().toCode() + " but found " + spd.getxPathUsage().toCode());
}
boolean found = false;
for (CodeType ct : sp.getBase()) found = found || p.getType().equals(ct.asStringValue());
if (!found)
sp.addBase(p.getType());
}
spd.setUrl(sp.getUrl());
for (String target : spd.getWorkingTargets()) {
if ("Any".equals(target) == true) {
for (String resourceName : definitions.sortedResourceNames()) {
boolean found = false;
for (CodeType st : sp.getTarget()) found = found || st.asStringValue().equals(resourceName);
if (!found)
sp.addTarget(resourceName);
}
} else {
boolean found = false;
for (CodeType st : sp.getTarget()) found = found || st.asStringValue().equals(target);
if (!found)
sp.addTarget(target);
}
}
return sp;
}
use of org.hl7.fhir.definitions.generators.specification.XPathQueryGenerator in project kindling by HL7.
the class Publisher method makeSearchParam.
private CapabilityStatementRestResourceSearchParamComponent makeSearchParam(String rn, SearchParameterDefn i) throws Exception {
CapabilityStatementRestResourceSearchParamComponent result = new CapabilityStatement.CapabilityStatementRestResourceSearchParamComponent();
result.setName(i.getCode());
result.setDefinition("http://hl7.org/fhir/SearchParameter/" + i.getCommonId());
result.setType(getSearchParamType(i.getType()));
result.setDocumentation(i.getDescription());
if (Utilities.noString(i.getXPath()))
// used elsewhere later
i.setXPath(new XPathQueryGenerator(page.getDefinitions(), page, page.getQa()).generateXpath(i.getPaths(), rn));
return result;
}
Aggregations