rework OID handling for better OID -> CodeSystem resolution

This commit is contained in:
Grahame Grieve 2024-04-20 23:35:27 +10:00
parent 213363fc1e
commit 9dfa57c2d5
3 changed files with 114 additions and 8 deletions

View File

@ -4,6 +4,7 @@ import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.util.Collection; import java.util.Collection;
import java.util.Date; import java.util.Date;
import java.util.HashSet;
/* /*
Copyright (c) 2011+, HL7, Inc. Copyright (c) 2011+, HL7, Inc.
@ -45,6 +46,7 @@ import org.fhir.ucum.UcumService;
import org.hl7.fhir.exceptions.DefinitionException; import org.hl7.fhir.exceptions.DefinitionException;
import org.hl7.fhir.exceptions.FHIRException; import org.hl7.fhir.exceptions.FHIRException;
import org.hl7.fhir.exceptions.TerminologyServiceException; import org.hl7.fhir.exceptions.TerminologyServiceException;
import org.hl7.fhir.r5.context.IWorkerContext.OIDDefinition;
import org.hl7.fhir.r5.elementmodel.Element; import org.hl7.fhir.r5.elementmodel.Element;
import org.hl7.fhir.r5.formats.IParser; import org.hl7.fhir.r5.formats.IParser;
import org.hl7.fhir.r5.formats.ParserType; import org.hl7.fhir.r5.formats.ParserType;
@ -70,6 +72,7 @@ import org.hl7.fhir.r5.terminologies.utilities.CodingValidationRequest;
import org.hl7.fhir.r5.terminologies.utilities.ValidationResult; import org.hl7.fhir.r5.terminologies.utilities.ValidationResult;
import org.hl7.fhir.r5.utils.validation.IResourceValidator; import org.hl7.fhir.r5.utils.validation.IResourceValidator;
import org.hl7.fhir.r5.utils.validation.ValidationContextCarrier; import org.hl7.fhir.r5.utils.validation.ValidationContextCarrier;
import org.hl7.fhir.utilities.CommaSeparatedStringBuilder;
import org.hl7.fhir.utilities.FhirPublication; import org.hl7.fhir.utilities.FhirPublication;
import org.hl7.fhir.utilities.TimeTracker; import org.hl7.fhir.utilities.TimeTracker;
import org.hl7.fhir.utilities.npm.BasePackageCacheManager; import org.hl7.fhir.utilities.npm.BasePackageCacheManager;
@ -102,6 +105,93 @@ import javax.annotation.Nonnull;
public interface IWorkerContext { public interface IWorkerContext {
public class OIDDefinition {
private String type;
private String oid;
private String url;
private String version;
private String packageSrc;
protected OIDDefinition(String type, String oid, String url, String version, String packageSrc) {
super();
this.type = type;
this.oid = oid;
this.url = url;
this.version = version;
this.packageSrc = packageSrc;
}
public String getType() {
return type;
}
public String getOid() {
return oid;
}
public String getUrl() {
return url;
}
public String getVersion() {
return version;
}
public String getPackageSrc() {
return packageSrc;
}
public String summary() {
return url+(version == null ? "" : "|"+version)+(packageSrc != null ? "("+packageSrc+")" : "");
}
}
public class OIDSummary {
private Set<OIDDefinition> definitions;
private Set<String> urls = new HashSet<>();
protected OIDSummary(Set<OIDDefinition> definitions) {
super();
this.definitions = definitions;
for (OIDDefinition d : definitions) {
urls.add(d.getUrl());
}
}
public Set<OIDDefinition> getDefinitions() {
return definitions;
}
public String describe() {
CommaSeparatedStringBuilder b = new CommaSeparatedStringBuilder();
for (OIDDefinition d : definitions) {
b.append(d.summary());
}
return b.toString();
}
public String chooseBestUrl() {
for (OIDDefinition d : definitions) {
if (d.getPackageSrc() == null) {
return d.getUrl();
}
}
for (OIDDefinition d : definitions) {
if (d.getUrl().startsWith("http://hl7.org/fhir/")) {
return d.getUrl();
}
}
for (OIDDefinition d : definitions) {
if (!d.getUrl().contains("vsac")) {
return d.getUrl();
}
}
return null;
}
public int urlCount() {
return urls.size();
}
public String getUrl() {
return urls.iterator().next();
}
}
/** /**
* Get the version of the base definitions loaded in context * Get the version of the base definitions loaded in context
* This *does not* have to be 5.0 (R5) - the context can load other versions * This *does not* have to be 5.0 (R5) - the context can load other versions
@ -634,7 +724,13 @@ public interface IWorkerContext {
public boolean isForPublication(); public boolean isForPublication();
public void setForPublication(boolean value); public void setForPublication(boolean value);
public Set<String> urlsForOid(boolean codeSystem, String oid); /**
*
* @param oid
* @param resourceType - null to search on all resource types
* @return
*/
public OIDSummary urlsForOid(String oid, String resourceType);
/** /**
* this first does a fetch resource, and if nothing is found, looks in the * this first does a fetch resource, and if nothing is found, looks in the

View File

@ -6,6 +6,7 @@ import java.sql.DriverManager;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
import java.sql.Types;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
@ -33,11 +34,13 @@ public class OidIndexBuilder {
Statement stmt = db.createStatement(); Statement stmt = db.createStatement();
stmt.execute("CREATE TABLE OIDMap (\r\n"+ stmt.execute("CREATE TABLE OIDMap (\r\n"+
"OID nvarchar NOT NULL,\r\n"+ "OID nvarchar NOT NULL,\r\n"+
"TYPE nvarchar NOT NULL,\r\n"+
"URL nvarchar NOT NULL,\r\n"+ "URL nvarchar NOT NULL,\r\n"+
"VERSION nvarchar NOT NULL,\r\n"+
"Status nvarchar NOT NULL,\r\n"+ "Status nvarchar NOT NULL,\r\n"+
"PRIMARY KEY (OID, URL))\r\n"); "PRIMARY KEY (OID, URL))\r\n");
PreparedStatement psql = db.prepareStatement("Insert into OIDMap (OID, URL, Status) values (?, ?, ?)");; PreparedStatement psql = db.prepareStatement("Insert into OIDMap (OID, TYPE, URL, VERSION, Status) values (?, ?, ?, ?, ?)");
for (File f : folder.listFiles()) { for (File f : folder.listFiles()) {
if (!f.getName().startsWith(".") && f.getName().endsWith(".json")) { if (!f.getName().startsWith(".") && f.getName().endsWith(".json")) {
try { try {
@ -61,6 +64,7 @@ public class OidIndexBuilder {
Set<String> oids = new HashSet<String>(); Set<String> oids = new HashSet<String>();
String url = null; String url = null;
String status = json.asString("status"); String status = json.asString("status");
String version = json.asString("version");
if ("NamingSystem".equals(rt)) { if ("NamingSystem".equals(rt)) {
for (JsonObject id : json.getJsonObjects("uniqueId")) { for (JsonObject id : json.getJsonObjects("uniqueId")) {
String t = id.asString("type"); String t = id.asString("type");
@ -73,7 +77,7 @@ public class OidIndexBuilder {
} }
if (url != null) { if (url != null) {
for (String s : oids) { for (String s : oids) {
addOid(psql, matches, s, url, status); addOid(psql, matches, s, rt, url, version, status);
} }
} }
} else { } else {
@ -97,7 +101,7 @@ public class OidIndexBuilder {
} }
if (!oids.isEmpty()) { if (!oids.isEmpty()) {
for (String s : oids) { for (String s : oids) {
addOid(psql, matches, s, url, status); addOid(psql, matches, s, rt, url, version, status);
} }
} }
} }
@ -105,13 +109,19 @@ public class OidIndexBuilder {
} }
} }
private void addOid(PreparedStatement psql, Set<String> matches, String oid, String url, String status) throws SQLException { private void addOid(PreparedStatement psql, Set<String> matches, String oid, String type, String url, String version, String status) throws SQLException {
String key = oid+"@"+url; String key = oid+"@"+url;
if (!matches.contains(key)) { if (!matches.contains(key)) {
matches.add(key); matches.add(key);
psql.setString(1, oid); psql.setString(1, oid);
psql.setString(2, url); psql.setString(2, type);
psql.setString(3, status); psql.setString(3, url);
if (version == null) {
psql.setNull(4, Types.NVARCHAR);
} else {
psql.setString(4, version);
}
psql.setString(5, status);
psql.execute(); psql.execute();
} }

View File

@ -509,7 +509,7 @@ public class SimpleWorkerContext extends BaseWorkerContext implements IWorkerCon
String of = pi.getFolders().get("package").getFolderPath(); String of = pi.getFolders().get("package").getFolderPath();
if (of != null) { if (of != null) {
oidSources.add(new OIDSource(of)); oidSources.add(new OIDSource(of, pi.vid()));
} }
if ((types == null || types.size() == 0) && loader != null) { if ((types == null || types.size() == 0) && loader != null) {