Add value set expand by URL and use it in vsac

This commit is contained in:
Grahame Grieve 2022-06-13 21:50:11 +03:00
parent e0d4a843ce
commit 62a082dbf3
2 changed files with 47 additions and 3 deletions

View File

@ -1,7 +1,11 @@
package org.hl7.fhir.convertors.misc;
import org.hl7.fhir.exceptions.FHIRException;
import org.hl7.fhir.r4.formats.IParser.OutputStyle;
import org.hl7.fhir.r4.formats.JsonParser;
import org.hl7.fhir.r4.model.OperationOutcome;
import org.hl7.fhir.r4.model.OperationOutcome.IssueSeverity;
import org.hl7.fhir.r4.model.OperationOutcome.IssueType;
import org.hl7.fhir.r4.model.ValueSet;
import org.hl7.fhir.r4.utils.client.FHIRToolingClient;
import org.hl7.fhir.utilities.CSVReader;
@ -12,6 +16,8 @@ import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URISyntaxException;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;
public class VSACImporter extends OIDBasedValueSetImporter {
@ -28,25 +34,42 @@ public class VSACImporter extends OIDBasedValueSetImporter {
private void process(String source, String dest, String apiKey) throws FHIRException, IOException, URISyntaxException {
CSVReader csv = new CSVReader(new FileInputStream(source));
csv.readHeaders();
Map<String, String> errs = new HashMap<>();
FHIRToolingClient fhirToolingClient = new FHIRToolingClient("https://cts.nlm.nih.gov/fhir", "fhir/vsac");
fhirToolingClient.setUsername("apikey");
fhirToolingClient.setPassword(apiKey);
fhirToolingClient.setTimeout(30000);
int i = 0;
int j = 0;
while (csv.line()) {
String oid = csv.cell("OID");
try {
ValueSet vs = fhirToolingClient.read(ValueSet.class, oid);
new JsonParser().compose(new FileOutputStream(Utilities.path(dest, "ValueSet-" + oid + ".json")), vs);
try {
ValueSet vse = fhirToolingClient.expandValueset(vs.getUrl(), null);
vs.setExpansion(vse.getExpansion());
j++;
} catch (Exception e) {
errs.put(oid, "Expansion: " +e.getMessage());
System.out.println(e.getMessage());
}
new JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(Utilities.path(dest, "ValueSet-" + oid + ".json")), vs);
i++;
if (i % 100 == 0) {
System.out.println(i);
System.out.println(":"+i+" ("+j+")");
}
} catch (Exception e) {
System.out.println("Unable to fetch OID " + oid + ": " + e.getMessage());
errs.put(oid, e.getMessage());
}
}
OperationOutcome oo = new OperationOutcome();
for (String oid : errs.keySet()) {
oo.addIssue().setSeverity(IssueSeverity.ERROR).setCode(IssueType.EXCEPTION).setDiagnostics(errs.get(oid)).addLocation(oid);
}
new JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(Utilities.path(dest, "other", "OperationOutcome-vsac-errors.json")), oo);
System.out.println("Done. " + i + " ValueSets");
}
}

View File

@ -1,6 +1,7 @@
package org.hl7.fhir.r4.utils.client;
import okhttp3.Headers;
import okhttp3.Request;
import okhttp3.internal.http2.Header;
import org.hl7.fhir.exceptions.FHIRException;
import org.hl7.fhir.r4.model.*;
@ -363,6 +364,27 @@ public class FHIRToolingClient {
return feed;
}
public ValueSet expandValueset(String vsUrl, Parameters expParams) {
Map<String,String> parameters = new HashMap<>();
parameters.put("url", vsUrl);
org.hl7.fhir.r4.utils.client.network.ResourceRequest<Resource> result = null;
try {
result = client.issueGetResourceRequest(resourceAddress.resolveOperationUri(ValueSet.class, "expand", parameters),
getPreferredResourceFormat(),
generateHeaders(),
"ValueSet/$expand?url=" + vsUrl,
TIMEOUT_OPERATION_EXPAND);
if (result.isUnsuccessfulRequest()) {
throw new EFhirClientException("Server returned error code " + result.getHttpStatus(), (OperationOutcome) result.getPayload());
}
} catch (IOException e) {
e.printStackTrace();
}
return result == null ? null : (ValueSet) result.getPayload();
}
public ValueSet expandValueset(ValueSet source, Parameters expParams) {
Parameters p = expParams == null ? new Parameters() : expParams.copy();
p.addParameter().setName("valueSet").setResource(source);
@ -383,7 +405,6 @@ public class FHIRToolingClient {
return result == null ? null : (ValueSet) result.getPayload();
}
public Parameters lookupCode(Map<String, String> params) {
org.hl7.fhir.r4.utils.client.network.ResourceRequest<Resource> result = null;
try {