Add validation

This commit is contained in:
James Agnew 2015-10-07 18:12:38 -04:00
parent 9fef4f6bab
commit e29e10450c
8 changed files with 118 additions and 113 deletions

View File

@ -35,6 +35,7 @@ public class App {
ourCommands.add(new RunServerCommand());
ourCommands.add(new ExampleDataUploader());
ourCommands.add(new ValidateCommand());
ourCommands.add(new ValidationDataUploader());
Collections.sort(ourCommands);
}

View File

@ -48,6 +48,8 @@ public class RunServerCommand extends BaseCommand {
public void run(CommandLine theCommandLine) throws ParseException {
myPort = parseOptionInteger(theCommandLine, OPTION_P, DEFAULT_PORT);
// ((ch.qos.logback.classic.Logger)LoggerFactory.getLogger("/")).setLevel(Level.ERROR);
ourLog.info("Preparing HAPI FHIR JPA server");
File tempWarFile;
try {
@ -81,6 +83,8 @@ public class RunServerCommand extends BaseCommand {
ourLog.info("Server started on port {}", myPort);
ourLog.info("Web Testing UI : http://localhost:{}/", myPort);
ourLog.info("Server Base URL: http://localhost:{}/baseDstu2/", myPort);
}
public void run(String[] theArgs) {

View File

@ -1,134 +1,157 @@
package ca.uhn.fhir.cli;
import static org.apache.commons.lang3.StringUtils.isBlank;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
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.apache.http.client.ClientProtocolException;
import org.hl7.fhir.instance.model.Bundle;
import org.hl7.fhir.instance.model.Bundle.BundleEntryComponent;
import org.hl7.fhir.instance.model.StructureDefinition;
import org.hl7.fhir.instance.model.ValueSet;
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.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;
public class ValidationDataUploader extends BaseCommand {
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(ValidationDataUploader.class);
public static void main(String[] args) throws Exception {
new ValidationDataUploader().execute();
@Override
public String getCommandDescription() {
return "Uploads the conformance resources (StructureDefinition and ValueSet) from the official FHIR definitions.";
}
private void execute() throws IOException, ClientProtocolException, UnsupportedEncodingException {
ourLog.info("Starting...");
@Override
public String getCommandName() {
return "upload-definitions";
}
FhirContext ctx = FhirContext.forDstu2Hl7Org();
@Override
public Options getOptions() {
Options options = new Options();
Option opt;
IGenericClient client = newClient(ctx,"");
opt = new Option("t", "target", true, "Base URL for the target server (e.g. \"http://example.com/fhir\")");
opt.setRequired(true);
options.addOption(opt);
int total;
int count;
return options;
}
// String vsContents =
// IOUtils.toString(ValidationDataUploader.class.getResourceAsStream("/org/hl7/fhir/instance/model/valueset/valuesets.xml"),
// "UTF-8");
// Bundle bundle = ctx.newXmlParser().parseResource(Bundle.class, vsContents);
//
// int total = bundle.getEntry().size();
// int count = 1;
// for (BundleEntryComponent i : bundle.getEntry()) {
// ValueSet next = (ValueSet) i.getResource();
// next.setId(next.getIdElement().toUnqualifiedVersionless());
//
// ourLog.info("Uploading ValueSet {}/{} : {}", new Object[] {count,total,next.getIdElement().getValue()});
// client.update().resource(next).execute();
//
// count++;
// }
//
// ourLog.info("Finished uploading ValueSets");
@Override
public void run(CommandLine theCommandLine) throws ParseException {
String targetServer = theCommandLine.getOptionValue("t");
if (isBlank(targetServer)) {
throw new ParseException("No target server (-t) specified");
} else if (targetServer.startsWith("http") == false) {
throw new ParseException("Invalid target server specified, must begin with 'http'");
}
// String vsContents = IOUtils.toString(ValidationDataUploader.class.getResourceAsStream("/org/hl7/fhir/instance/model/valueset/v3-codesystems.xml"), "UTF-8");
// Bundle bundle = ctx.newXmlParser().parseResource(Bundle.class, vsContents);
// total = bundle.getEntry().size();
// count = 1;
// for (BundleEntryComponent i : bundle.getEntry()) {
// ValueSet next = (ValueSet) 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++;
// }
FhirContext ctx = FhirContext.forDstu2();
ourLog.info("Uploading definitions to server: " + targetServer);
IGenericClient client = newClient(ctx, targetServer);
long start = System.currentTimeMillis();
String vsContents = IOUtils.toString(ValidationDataUploader.class.getResourceAsStream("/org/hl7/fhir/instance/model/valueset/v2-tables.xml"), "UTF-8");
String vsContents;
try {
vsContents = IOUtils.toString(ValidationDataUploader.class.getResourceAsStream("/org/hl7/fhir/instance/model/valueset/valuesets.xml"), "UTF-8");
} catch (IOException e) {
throw new CommandFailureException(e.toString());
}
Bundle bundle = ctx.newXmlParser().parseResource(Bundle.class, vsContents);
int total = bundle.getEntry().size();
int count = 1;
for (Entry i : bundle.getEntry()) {
ValueSet next = (ValueSet) i.getResource();
next.setId(next.getIdElement().toUnqualifiedVersionless());
ourLog.info("Uploading 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/valueset/v3-codesystems.xml"), "UTF-8");
} catch (IOException e) {
throw new CommandFailureException(e.toString());
}
bundle = ctx.newXmlParser().parseResource(Bundle.class, vsContents);
total = bundle.getEntry().size();
count = 1;
for (BundleEntryComponent i : bundle.getEntry()) {
if (count > 1900) {
for (Entry i : bundle.getEntry()) {
ValueSet next = (ValueSet) 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/valueset/v2-tables.xml"), "UTF-8");
} catch (IOException e) {
throw new CommandFailureException(e.toString());
}
bundle = ctx.newXmlParser().parseResource(Bundle.class, vsContents);
total = bundle.getEntry().size();
count = 1;
for (Entry i : bundle.getEntry()) {
ValueSet next = (ValueSet) i.getResource();
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 = patternResolver.getResources("classpath*:org/hl7/fhir/instance/model/profile/*.profile.xml");
Resource[] mappingLocations;
try {
mappingLocations = patternResolver.getResources("classpath*:org/hl7/fhir/instance/model/profile/*.profile.xml");
} catch (IOException e) {
throw new CommandFailureException(e.toString());
}
total = mappingLocations.length;
count = 1;
for (Resource i : mappingLocations) {
if (count > 140) {
StructureDefinition next = ctx.newXmlParser().parseResource(StructureDefinition.class, IOUtils.toString(i.getInputStream(), "UTF-8"));
StructureDefinition next;
try {
next = ctx.newXmlParser().parseResource(StructureDefinition.class, IOUtils.toString(i.getInputStream(), "UTF-8"));
} catch (DataFormatException | IOException e) {
throw new CommandFailureException(e.toString());
}
next.setId(next.getIdElement().toUnqualifiedVersionless());
ourLog.info("Uploading StructureDefinition {}/{} : {}", new Object[] { count, total, next.getIdElement().getValue() });
try {
client.update().resource(next).execute();
} catch (Exception e) {
ourLog.warn("Failed to upload {} - {}", next.getIdElement().getValue(), e.getMessage());
}
count++;
}
ourLog.info("Finished uploading ValueSets");
}
@Override
public String getCommandDescription() {
// TODO Auto-generated method stub
return null;
}
@Override
public String getCommandName() {
// TODO Auto-generated method stub
return null;
}
@Override
public Options getOptions() {
// TODO Auto-generated method stub
return null;
}
@Override
public void run(CommandLine theCommandLine) throws ParseException {
// TODO Auto-generated method stub
long delay = System.currentTimeMillis() - start;
ourLog.info("Finished uploading definitions to server (took {} ms)", delay);
}
}

View File

@ -8,38 +8,11 @@
</encoder>
</appender>
<logger name="java.sql" additivity="false" level="warn">
<appender-ref ref="STDOUT" />
</logger>
<logger name="ca.uhn.fhir.jpa" additivity="false" level="info">
<appender-ref ref="STDOUT" />
</logger>
<logger name="ca.uhn.fhir.rest" additivity="false" level="warn">
<appender-ref ref="STDOUT" />
</logger>
<logger name="org.eclipse" additivity="false" level="warn">
<appender-ref ref="STDOUT" />
</logger>
<logger name="org.springframework" additivity="false" level="warn">
<appender-ref ref="STDOUT" />
</logger>
<logger name="org.apache" additivity="false" level="info">
<appender-ref ref="STDOUT" />
</logger>
<logger name="org.thymeleaf" additivity="false" level="warn">
<appender-ref ref="STDOUT" />
</logger>
<logger name="org.hibernate" additivity="false" level="warn">
<logger name="ca.uhn.fhir.cli" additivity="false" level="info">
<appender-ref ref="STDOUT" />
</logger>
<!--
<logger name="ca.uhn.fhir.rest.client" additivity="false" level="trace">
<appender-ref ref="STDOUT" />
</logger>
-->
<root level="info">
<root level="warn">
<appender-ref ref="STDOUT" />
</root>

View File

@ -101,17 +101,15 @@ public class FhirResourceDaoSubscriptionDstu2 extends FhirResourceDaoDstu2<Subsc
// SubscriptionCandidateResource
TypedQuery<SubscriptionTable> q = myEntityManager.createNamedQuery("Q_HFJ_SUBSCRIPTION_NEXT_CHECK", SubscriptionTable.class);
q.setParameter("next_check", new Date());
q.setParameter("status", SubscriptionStatusEnum.ACTIVE);
List<SubscriptionTable> subscriptions = q.getResultList();
Collection<Long> subscriptions = mySubscriptionTableDao.finsSubscriptionsWhichNeedToBeChecked(SubscriptionStatusEnum.ACTIVE, new Date());
TransactionTemplate txTemplate = new TransactionTemplate(myTxManager);
txTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
for (final SubscriptionTable nextSubscriptionTable : subscriptions) {
for (final Long nextSubscriptionTablePid : subscriptions) {
txTemplate.execute(new TransactionCallback<Void>() {
@Override
public Void doInTransaction(TransactionStatus theStatus) {
SubscriptionTable nextSubscriptionTable = mySubscriptionTableDao.findOne(nextSubscriptionTablePid);
pollForNewUndeliveredResources(nextSubscriptionTable);
return null;
}

View File

@ -29,6 +29,7 @@ import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import ca.uhn.fhir.jpa.entity.SubscriptionTable;
import ca.uhn.fhir.model.dstu2.valueset.SubscriptionStatusEnum;
public interface ISubscriptionTableDao extends JpaRepository<SubscriptionTable, Long> {
@ -46,4 +47,6 @@ public interface ISubscriptionTableDao extends JpaRepository<SubscriptionTable,
@Query("SELECT t FROM SubscriptionTable t WHERE t.myLastClientPoll < :cutoff OR (t.myLastClientPoll IS NULL AND t.myCreated < :cutoff)")
public Collection<SubscriptionTable> findInactiveBeforeCutoff(@Param("cutoff") Date theCutoff);
@Query("SELECT t.myId FROM SubscriptionTable t WHERE t.myStatus = :status AND t.myNextCheck <= :next_check")
public Collection<Long> finsSubscriptionsWhichNeedToBeChecked(@Param("status") SubscriptionStatusEnum theStatus, @Param("next_check") Date theNextCheck);
}

View File

@ -52,7 +52,6 @@ import ca.uhn.fhir.model.dstu2.valueset.SubscriptionStatusEnum;
})
@NamedQueries({
@NamedQuery(name="Q_HFJ_SUBSCRIPTION_SET_STATUS", query="UPDATE SubscriptionTable t SET t.myStatus = :status WHERE t.myResId = :res_id"),
@NamedQuery(name="Q_HFJ_SUBSCRIPTION_NEXT_CHECK", query="SELECT t FROM SubscriptionTable t WHERE t.myStatus = :status AND t.myNextCheck <= :next_check"),
@NamedQuery(name="Q_HFJ_SUBSCRIPTION_GET_BY_RES", query="SELECT t FROM SubscriptionTable t WHERE t.myResId = :res_id")
})
//@formatter:on

View File

@ -6,6 +6,7 @@ import static org.junit.Assert.fail;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.eclipse.jetty.websocket.api.Session;
@ -19,6 +20,7 @@ import org.hl7.fhir.instance.model.api.IIdType;
import org.junit.Test;
import ca.uhn.fhir.jpa.util.SubscriptionsRequireManualActivationInterceptor;
import ca.uhn.fhir.model.api.ResourceMetadataKeyEnum;
import ca.uhn.fhir.model.dstu2.resource.Observation;
import ca.uhn.fhir.model.dstu2.resource.Patient;
import ca.uhn.fhir.model.dstu2.resource.Subscription;
@ -250,6 +252,7 @@ public class SubscriptionsDstu2Test extends BaseResourceProviderDstu2Test {
mySubscriptionDao.read(new IdDt("Subscription", socket.mySubsId));
Observation obs = new Observation();
ResourceMetadataKeyEnum.PROFILES.put(obs, Collections.singletonList(new IdDt("http://foo")));
obs.getSubject().setReference(pId);
obs.setStatus(ObservationStatusEnum.FINAL);
IIdType afterId1 = myObservationDao.create(obs).getId().toUnqualifiedVersionless();
@ -301,6 +304,7 @@ public class SubscriptionsDstu2Test extends BaseResourceProviderDstu2Test {
IIdType pId = myPatientDao.create(p).getId().toUnqualifiedVersionless();
Subscription subs = new Subscription();
ResourceMetadataKeyEnum.PROFILES.put(subs, Collections.singletonList(new IdDt("http://foo")));
subs.getChannel().setType(SubscriptionChannelTypeEnum.WEBSOCKET);
subs.setCriteria("Observation?subject=Patient/" + pId.getIdPart());
subs.setStatus(SubscriptionStatusEnum.ACTIVE);