Add option to validation uploader to specify FHIR version

This commit is contained in:
James Agnew 2016-04-18 10:07:09 -04:00
parent 533c0c87ab
commit 54f89073fe
2 changed files with 128 additions and 2 deletions

View File

@ -9,6 +9,10 @@ public class TestUtil {
/**
* THIS IS FOR UNIT TESTS ONLY
*
* When we run the unit tests in cobertura, JUnit doesn't seem to clean up static fields which leads to
* tons of memory being used by the end and the JVM crashes in Travis. Manually clearing all of the
* static fields seems to solve this.
*/
@CoverageIgnore
public static void clearAllStaticFieldsForUnitTest() {

View File

@ -3,22 +3,29 @@ package ca.uhn.fhir.cli;
import static org.apache.commons.lang3.StringUtils.isBlank;
import java.io.IOException;
import java.io.InputStreamReader;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.io.IOUtils;
import org.hl7.fhir.dstu3.model.Bundle.BundleEntryComponent;
import org.hl7.fhir.dstu3.model.IdType;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.context.FhirVersionEnum;
import ca.uhn.fhir.model.dstu2.resource.Bundle;
import ca.uhn.fhir.model.dstu2.resource.Bundle.Entry;
import ca.uhn.fhir.model.dstu2.resource.StructureDefinition;
import ca.uhn.fhir.model.dstu2.resource.ValueSet;
import ca.uhn.fhir.parser.DataFormatException;
import ca.uhn.fhir.rest.client.IGenericClient;
import ca.uhn.fhir.rest.server.exceptions.PreconditionFailedException;
import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException;
public class ValidationDataUploader extends BaseCommand {
@ -39,6 +46,8 @@ public class ValidationDataUploader extends BaseCommand {
Options options = new Options();
Option opt;
addFhirVersionOption(options);
opt = new Option("t", "target", true, "Base URL for the target server (e.g. \"http://example.com/fhir\")");
opt.setRequired(true);
options.addOption(opt);
@ -55,10 +64,18 @@ public class ValidationDataUploader extends BaseCommand {
throw new ParseException("Invalid target server specified, must begin with 'http'");
}
FhirContext ctx = FhirContext.forDstu2();
FhirContext ctx = getSpecVersionContext(theCommandLine);
if (ctx.getVersion().getVersion() == FhirVersionEnum.DSTU2) {
uploadDefinitionsDstu2(targetServer, ctx);
} else if (ctx.getVersion().getVersion() == FhirVersionEnum.DSTU3){
uploadDefinitionsDstu3(targetServer, ctx);
}
}
private void uploadDefinitionsDstu2(String targetServer, FhirContext ctx) throws CommandFailureException {
IGenericClient client = newClient(ctx, targetServer);
ourLog.info("Uploading definitions to server: " + targetServer);
IGenericClient client = newClient(ctx, targetServer);
long start = System.currentTimeMillis();
String vsContents;
@ -154,4 +171,109 @@ public class ValidationDataUploader extends BaseCommand {
ourLog.info("Finished uploading definitions to server (took {} ms)", delay);
}
private void uploadDefinitionsDstu3(String targetServer, FhirContext ctx) throws CommandFailureException {
IGenericClient client = newClient(ctx, targetServer);
ourLog.info("Uploading definitions to server: " + targetServer);
long start = System.currentTimeMillis();
String vsContents;
try {
ctx.getVersion().getPathToSchemaDefinitions();
vsContents = IOUtils.toString(ValidationDataUploader.class.getResourceAsStream("/org/hl7/fhir/instance/model/dstu3/valueset/"+"valuesets.xml"), "UTF-8");
} catch (IOException e) {
throw new CommandFailureException(e.toString());
}
org.hl7.fhir.dstu3.model.Bundle bundle = ctx.newXmlParser().parseResource(org.hl7.fhir.dstu3.model.Bundle.class, vsContents);
int total = bundle.getEntry().size();
int count = 1;
for (BundleEntryComponent i : bundle.getEntry()) {
org.hl7.fhir.dstu3.model.Resource next = i.getResource();
next.setId(next.getIdElement().toUnqualifiedVersionless());
ourLog.info("Uploading ValueSet {}/{} : {}", new Object[] { count, total, next.getIdElement().getValue() });
try {
client.update().resource(next).execute();
} catch (UnprocessableEntityException e) {
ourLog.warn("UnprocessableEntityException: " + e.toString());
}
count++;
}
try {
vsContents = IOUtils.toString(ValidationDataUploader.class.getResourceAsStream("/org/hl7/fhir/instance/model/dstu3/valueset/"+"v3-codesystems.xml"), "UTF-8");
} catch (IOException e) {
throw new CommandFailureException(e.toString());
}
bundle = ctx.newXmlParser().parseResource(org.hl7.fhir.dstu3.model.Bundle.class, vsContents);
total = bundle.getEntry().size();
count = 1;
for (BundleEntryComponent i : bundle.getEntry()) {
org.hl7.fhir.dstu3.model.Resource next = i.getResource();
next.setId(next.getIdElement().toUnqualifiedVersionless());
ourLog.info("Uploading v3-codesystems ValueSet {}/{} : {}", new Object[] { count, total, next.getIdElement().getValue() });
client.update().resource(next).execute();
count++;
}
try {
vsContents = IOUtils.toString(ValidationDataUploader.class.getResourceAsStream("/org/hl7/fhir/instance/model/dstu3/valueset/"+"v2-tables.xml"), "UTF-8");
} catch (IOException e) {
throw new CommandFailureException(e.toString());
}
bundle = ctx.newXmlParser().parseResource(org.hl7.fhir.dstu3.model.Bundle.class, vsContents);
total = bundle.getEntry().size();
count = 1;
for (BundleEntryComponent i : bundle.getEntry()) {
org.hl7.fhir.dstu3.model.Resource next = i.getResource();
if (next.getIdElement().isIdPartValidLong()) {
next.setIdElement(new IdType("v2-"+ next.getIdElement().getIdPart()));
}
next.setId(next.getIdElement().toUnqualifiedVersionless());
ourLog.info("Uploading v2-tables ValueSet {}/{} : {}", new Object[] { count, total, next.getIdElement().getValue() });
client.update().resource(next).execute();
count++;
}
ourLog.info("Finished uploading ValueSets");
ResourcePatternResolver patternResolver = new PathMatchingResourcePatternResolver();
Resource[] mappingLocations;
try {
mappingLocations = patternResolver.getResources("classpath*:org/hl7/fhir/instance/model/dstu3/*.xml");
} catch (IOException e) {
throw new CommandFailureException(e.toString());
}
total = mappingLocations.length;
count = 1;
for (Resource i : mappingLocations) {
try {
bundle = ctx.newXmlParser().parseResource(org.hl7.fhir.dstu3.model.Bundle.class, new InputStreamReader(i.getInputStream()));
} catch (Exception e1) {
throw new CommandFailureException(e1.toString());
}
total = bundle.getEntry().size();
count = 1;
for (BundleEntryComponent e : bundle.getEntry()) {
org.hl7.fhir.dstu3.model.StructureDefinition next = (org.hl7.fhir.dstu3.model.StructureDefinition) e.getResource();
next.setId(next.getIdElement().toUnqualifiedVersionless());
ourLog.info("Uploading {} StructureDefinition {}/{} : {}", new Object[] { i.getFilename(), count, total, next.getIdElement().getValue() });
client.update().resource(next).execute();
count++;
}
}
ourLog.info("Finished uploading ValueSets");
long delay = System.currentTimeMillis() - start;
ourLog.info("Finished uploading definitions to server (took {} ms)", delay);
}
}