From 1d913459fe412be98338c06094a2e8eb4c37264e Mon Sep 17 00:00:00 2001 From: Grahame Grieve Date: Thu, 25 Apr 2024 21:59:44 +1000 Subject: [PATCH] rework OID handling for better consistency --- .../fhir/r5/context/BaseWorkerContext.java | 15 +++--- .../hl7/fhir/r5/context/IWorkerContext.java | 52 +++++++++++++++---- 2 files changed, 50 insertions(+), 17 deletions(-) diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/context/BaseWorkerContext.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/context/BaseWorkerContext.java index 57de55ec0..e408f6b35 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/context/BaseWorkerContext.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/context/BaseWorkerContext.java @@ -3124,10 +3124,10 @@ public abstract class BaseWorkerContext extends I18nBase implements IWorkerConte } public OIDSummary urlsForOid(String oid, String resourceType, boolean retired) { - Set urls = new HashSet<>(); + OIDSummary summary = new OIDSummary(); if (oid != null) { if (oidCacheManual.containsKey(oid)) { - urls.addAll(oidCacheManual.get(oid)); + summary.addOIDs(oidCacheManual.get(oid)); } for (OIDSource os : oidSources) { if (os.db == null) { @@ -3145,7 +3145,7 @@ public abstract class BaseWorkerContext extends I18nBase implements IWorkerConte String rt = rs.getString(1); String url = rs.getString(2); String version = rs.getString(3); - urls.add(new OIDDefinition(rt, oid, url, version, os.pid)); + summary.addOID(new OIDDefinition(rt, oid, url, version, os.pid)); } } } catch (Exception e) { @@ -3157,18 +3157,19 @@ public abstract class BaseWorkerContext extends I18nBase implements IWorkerConte switch (oid) { case "2.16.840.1.113883.6.1" : - urls.add(new OIDDefinition("CodeSystem", "2.16.840.1.113883.6.1", "http://loinc.org", null, null)); + summary.addOID(new OIDDefinition("CodeSystem", "2.16.840.1.113883.6.1", "http://loinc.org", null, null)); break; case "2.16.840.1.113883.6.8" : - urls.add(new OIDDefinition("CodeSystem", "2.16.840.1.113883.6.8", "http://unitsofmeasure.org", null, null)); + summary.addOID(new OIDDefinition("CodeSystem", "2.16.840.1.113883.6.8", "http://unitsofmeasure.org", null, null)); break; case "2.16.840.1.113883.6.96" : - urls.add(new OIDDefinition("CodeSystem", "2.16.840.1.113883.6.96", "http://snomed.info/sct", null, null)); + summary.addOID(new OIDDefinition("CodeSystem", "2.16.840.1.113883.6.96", "http://snomed.info/sct", null, null)); break; default: } } - return new OIDSummary(urls); + summary.sort(); + return summary; } private Connection connectToOidSource(String folder) { diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/context/IWorkerContext.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/context/IWorkerContext.java index 63c580513..3a58fd8ba 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/context/IWorkerContext.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/context/IWorkerContext.java @@ -2,7 +2,10 @@ package org.hl7.fhir.r5.context; import java.io.FileNotFoundException; import java.io.IOException; +import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; +import java.util.Comparator; import java.util.Date; import java.util.HashSet; @@ -47,6 +50,7 @@ import org.hl7.fhir.exceptions.DefinitionException; import org.hl7.fhir.exceptions.FHIRException; import org.hl7.fhir.exceptions.TerminologyServiceException; import org.hl7.fhir.r5.context.IWorkerContext.OIDDefinition; +import org.hl7.fhir.r5.context.IWorkerContext.OIDDefinitionComparer; import org.hl7.fhir.r5.elementmodel.Element; import org.hl7.fhir.r5.formats.IParser; import org.hl7.fhir.r5.formats.ParserType; @@ -105,6 +109,18 @@ import javax.annotation.Nonnull; public interface IWorkerContext { + public class OIDDefinitionComparer implements Comparator { + + @Override + public int compare(OIDDefinition o1, OIDDefinition o2) { + if (o1.getUrl().equals(o2.getUrl())) { + return -o1.getVersion().compareTo(o2.getVersion()); + } else { + return o1.getUrl().compareTo(o2.getUrl()); + } + } + } + public class OIDDefinition { private String type; private String oid; @@ -116,7 +132,7 @@ public interface IWorkerContext { this.type = type; this.oid = oid; this.url = url; - this.version = version; + this.version = version == null ? "" : version; this.packageSrc = packageSrc; } public String getType() { @@ -137,26 +153,42 @@ public interface IWorkerContext { public String summary() { return url+(version == null ? "" : "|"+version)+(packageSrc != null ? "("+packageSrc+")" : ""); } - + public boolean matches(OIDDefinition t) { + return url.equals(t.url) && version.equals(t.version); + } } public class OIDSummary { - private Set definitions; - private Set urls = new HashSet<>(); + private List definitions = new ArrayList<>(); + private List urls = new ArrayList<>(); - protected OIDSummary(Set definitions) { - super(); - this.definitions = definitions; - for (OIDDefinition d : definitions) { + public void addOID(OIDDefinition d) { + for (OIDDefinition t : definitions) { + if (d.matches(t)) { + return; + } + } + definitions.add(d); + if (!urls.contains(d.getUrl())) { urls.add(d.getUrl()); } } - - public Set getDefinitions() { + + public void addOIDs(Collection collection) { + for (OIDDefinition t : collection) { + addOID(t); + } + } + + public List getDefinitions() { return definitions; } + public void sort() { + Collections.sort(definitions, new OIDDefinitionComparer()); + Collections.sort(urls); + } public String describe() { CommaSeparatedStringBuilder b = new CommaSeparatedStringBuilder(); for (OIDDefinition d : definitions) {