Search in sources :

Example 51 with ValueSet

use of org.hl7.fhir.r4.model.ValueSet in project cqf-ruler by DBCG.

the class CacheValueSetsProvider method cacheValuesets.

/**
 * Using basic authentication this {@link Operation Operation} will update
 * any {@link ValueSet Valueset} listed given the {@link Endpoint Endpoint}
 * provided.
 * Any Valuesets that require expansion will be expanded.
 *
 * @param details    the {@link RequestDetails RequestDetails}
 * @param endpointId the {@link Endpoint Endpoint} id
 * @param valuesets  the {@link StringAndListParam list} of {@link ValueSet
 *                   Valueset} ids
 * @param userName   the userName
 * @param password   the password
 * @return the {@link OperationOutcome OperationOutcome} or the resulting
 *         {@link Bundle Bundle}
 */
@Description(shortDefinition = "$cache-valuesets", value = "Using basic authentication this Operation will update any Valueset listed given the Endpoint provided. Any Valuesets that require expansion will be expanded.", example = "Endpoint/example-id/$cache-valuesets?valuesets=valuesetId1&valuesets=valuesetId2&user=user&password=password")
@Operation(name = "cache-valuesets", idempotent = true, type = Endpoint.class)
public Resource cacheValuesets(RequestDetails details, @IdParam IdType endpointId, @OperationParam(name = "valuesets") StringAndListParam valuesets, @OperationParam(name = "user") String userName, @OperationParam(name = "pass") String password) {
    Endpoint endpoint = null;
    try {
        endpoint = this.endpointDao.read(endpointId);
        if (endpoint == null) {
            return createErrorOutcome("Could not find Endpoint/" + endpointId);
        }
    } catch (Exception e) {
        return createErrorOutcome("Could not find Endpoint/" + endpointId + "\n" + e);
    }
    IGenericClient client = Clients.forEndpoint(ourCtx, endpoint);
    if (userName != null || password != null) {
        if (userName == null) {
            return createErrorOutcome("Password was provided, but not a user name.");
        } else if (password == null) {
            return createErrorOutcome("User name was provided, but not a password.");
        }
        BasicAuthInterceptor basicAuth = new BasicAuthInterceptor(userName, password);
        client.registerInterceptor(basicAuth);
    // TODO - more advanced security like bearer tokens, etc...
    }
    try {
        Bundle bundleToPost = new Bundle();
        for (StringOrListParam params : valuesets.getValuesAsQueryTokens()) {
            for (StringParam valuesetId : params.getValuesAsQueryTokens()) {
                bundleToPost.addEntry().setRequest(new Bundle.BundleEntryRequestComponent().setMethod(Bundle.HTTPVerb.PUT).setUrl("ValueSet/" + valuesetId.getValue())).setResource(resolveValueSet(client, valuesetId.getValue()));
            }
        }
        return (Resource) systemDao.transaction(details, bundleToPost);
    } catch (Exception e) {
        return createErrorOutcome(e.getMessage());
    }
}
Also used : Endpoint(org.hl7.fhir.r4.model.Endpoint) BasicAuthInterceptor(ca.uhn.fhir.rest.client.interceptor.BasicAuthInterceptor) IGenericClient(ca.uhn.fhir.rest.client.api.IGenericClient) Bundle(org.hl7.fhir.r4.model.Bundle) Resource(org.hl7.fhir.r4.model.Resource) StringParam(ca.uhn.fhir.rest.param.StringParam) StringOrListParam(ca.uhn.fhir.rest.param.StringOrListParam) Description(ca.uhn.fhir.model.api.annotation.Description) Operation(ca.uhn.fhir.rest.annotation.Operation)

Example 52 with ValueSet

use of org.hl7.fhir.r4.model.ValueSet in project cqf-ruler by DBCG.

the class CacheValueSetsProvider method getCachedValueSet.

private ValueSet getCachedValueSet(ValueSet expandedValueSet) {
    ValueSet clean = expandedValueSet.copy().setExpansion(null);
    Map<String, ValueSet.ConceptSetComponent> concepts = new HashMap<>();
    for (ValueSet.ValueSetExpansionContainsComponent expansion : expandedValueSet.getExpansion().getContains()) {
        if (!expansion.hasSystem()) {
            continue;
        }
        if (concepts.containsKey(expansion.getSystem())) {
            concepts.get(expansion.getSystem()).addConcept(new ValueSet.ConceptReferenceComponent().setCode(expansion.hasCode() ? expansion.getCode() : null).setDisplay(expansion.hasDisplay() ? expansion.getDisplay() : null));
        } else {
            concepts.put(expansion.getSystem(), new ValueSet.ConceptSetComponent().setSystem(expansion.getSystem()).addConcept(new ValueSet.ConceptReferenceComponent().setCode(expansion.hasCode() ? expansion.getCode() : null).setDisplay(expansion.hasDisplay() ? expansion.getDisplay() : null)));
        }
    }
    clean.setCompose(new ValueSet.ValueSetComposeComponent().setInclude(new ArrayList<>(concepts.values())));
    return clean;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ValueSet(org.hl7.fhir.r4.model.ValueSet)

Example 53 with ValueSet

use of org.hl7.fhir.r4.model.ValueSet in project cqf-ruler by DBCG.

the class CacheValueSetsProviderIT method testCacheValueSetsAlreadyExpanded.

// Get help with this....
// @Test
// public void testCacheValueSetsExpandAndAddConcepts() throws Exception {
// Endpoint endpoint = uploadLocalServerEndpoint();
// RequestDetails details = Mockito.mock(RequestDetails.class);
// ValueSet vs =
// uploadValueSet("valueset/valueset-buprenorphine-and-methadone-medications.json");
// vs.getCompose().getInclude().forEach(include -> {
// assertTrue(!include.hasConcept());
// });
// StringAndListParam stringAndListParam =
// getStringAndListParamFromValueSet(vs);
// IGenericClient localClient = createClient(ourCtx, endpoint);
// //
// localClient.operation().onServer().named("updateCodeSystems").withNoParameters(Parameters.class).execute();
// Resource outcomeResource = cacheValueSetsProvider.cacheValuesets(details,
// endpoint.getIdElement(), stringAndListParam, null, null);
// assertTrue(outcomeResource instanceof Bundle);
// Bundle resultBundle = (Bundle) outcomeResource;
// assertTrue(resultBundle.getEntry().size() == 1);
// BundleEntryComponent entry = resultBundle.getEntry().get(0);
// assertTrue(entry.getResponse().getLocation().startsWith("ValueSet/" +
// vs.getIdElement().getIdPart()));
// assertTrue(entry.getResponse().getStatus().equals("200 OK"));
// ValueSet resultingValueSet =
// localClient.read().resource(ValueSet.class).withId(vs.getIdElement()).execute();
// resultingValueSet.getCompose().getInclude().forEach(include -> {
// assertTrue(include.hasConcept());
// });
// }
@Test
public void testCacheValueSetsAlreadyExpanded() throws Exception {
    Endpoint endpoint = uploadLocalServerEndpoint();
    RequestDetails details = Mockito.mock(RequestDetails.class);
    ValueSet vs = uploadValueSet("valueset/valueset-benzodiazepine-medications.json");
    StringAndListParam stringAndListParam = getStringAndListParamFromValueSet(vs);
    Resource outcomeResource = cacheValueSetsProvider.cacheValuesets(details, endpoint.getIdElement(), stringAndListParam, null, null);
    assertTrue(outcomeResource instanceof Bundle);
    Bundle resultBundle = (Bundle) outcomeResource;
    assertEquals(1, resultBundle.getEntry().size());
    BundleEntryComponent entry = resultBundle.getEntry().get(0);
    assertTrue(entry.getResponse().getLocation().startsWith("ValueSet/" + vs.getIdElement().getIdPart()));
    assertEquals("200 OK", entry.getResponse().getStatus());
}
Also used : BundleEntryComponent(org.hl7.fhir.dstu3.model.Bundle.BundleEntryComponent) Endpoint(org.hl7.fhir.dstu3.model.Endpoint) StringAndListParam(ca.uhn.fhir.rest.param.StringAndListParam) Bundle(org.hl7.fhir.dstu3.model.Bundle) Resource(org.hl7.fhir.dstu3.model.Resource) ValueSet(org.hl7.fhir.dstu3.model.ValueSet) RequestDetails(ca.uhn.fhir.rest.api.server.RequestDetails) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) RestIntegrationTest(org.opencds.cqf.ruler.test.RestIntegrationTest)

Example 54 with ValueSet

use of org.hl7.fhir.r4.model.ValueSet in project cqf-ruler by DBCG.

the class CacheValueSetsProviderIT method testCacheValueSetsEndpointNull.

@Test
public void testCacheValueSetsEndpointNull() throws Exception {
    StringAndListParam stringAndListParam = getStringAndListParamFromValueSet("valueset/AcuteInpatient.json");
    RequestDetails details = Mockito.mock(RequestDetails.class);
    Resource outcomeResource = cacheValueSetsProvider.cacheValuesets(details, new Endpoint().getIdElement(), stringAndListParam, null, null);
    validateOutcome(outcomeResource, "Could not find Endpoint/null");
}
Also used : Endpoint(org.hl7.fhir.dstu3.model.Endpoint) StringAndListParam(ca.uhn.fhir.rest.param.StringAndListParam) Resource(org.hl7.fhir.dstu3.model.Resource) RequestDetails(ca.uhn.fhir.rest.api.server.RequestDetails) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) RestIntegrationTest(org.opencds.cqf.ruler.test.RestIntegrationTest)

Example 55 with ValueSet

use of org.hl7.fhir.r4.model.ValueSet in project cqf-ruler by DBCG.

the class CacheValueSetsProviderIT method testCacheValueSetsNoCompose.

@Test
public void testCacheValueSetsNoCompose() throws Exception {
    Endpoint endpoint = uploadLocalServerEndpoint();
    RequestDetails details = Mockito.mock(RequestDetails.class);
    ValueSet vs = uploadValueSet("valueset/valueset-benzodiazepine-medications.json");
    assertTrue(vs.getCompose().getInclude().isEmpty());
    StringAndListParam stringAndListParam = getStringAndListParamFromValueSet(vs);
    Resource outcomeResource = cacheValueSetsProvider.cacheValuesets(details, endpoint.getIdElement(), stringAndListParam, null, null);
    assertTrue(outcomeResource instanceof Bundle);
    Bundle resultBundle = (Bundle) outcomeResource;
    assertEquals(1, resultBundle.getEntry().size());
    BundleEntryComponent entry = resultBundle.getEntry().get(0);
    assertTrue(entry.getResponse().getLocation().startsWith("ValueSet/" + vs.getIdElement().getIdPart()));
    assertEquals("200 OK", entry.getResponse().getStatus());
// ValueSet resultingValueSet = createClient(ourCtx,
// endpoint).read().resource(ValueSet.class).withId(vs.getIdElement()).execute();
// resultingValueSet not returning with a version
// assertTrue(resultingValueSet.getVersion().endsWith("-cached"));
}
Also used : BundleEntryComponent(org.hl7.fhir.dstu3.model.Bundle.BundleEntryComponent) Endpoint(org.hl7.fhir.dstu3.model.Endpoint) StringAndListParam(ca.uhn.fhir.rest.param.StringAndListParam) Bundle(org.hl7.fhir.dstu3.model.Bundle) Resource(org.hl7.fhir.dstu3.model.Resource) ValueSet(org.hl7.fhir.dstu3.model.ValueSet) RequestDetails(ca.uhn.fhir.rest.api.server.RequestDetails) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) RestIntegrationTest(org.opencds.cqf.ruler.test.RestIntegrationTest)

Aggregations

ValueSet (org.hl7.fhir.r5.model.ValueSet)159 ValueSet (org.hl7.fhir.r4.model.ValueSet)116 Test (org.junit.jupiter.api.Test)115 ArrayList (java.util.ArrayList)101 FHIRException (org.hl7.fhir.exceptions.FHIRException)100 IOException (java.io.IOException)97 ValueSet (org.hl7.fhir.dstu3.model.ValueSet)59 ValueSet (org.hl7.fhir.r4b.model.ValueSet)59 FileNotFoundException (java.io.FileNotFoundException)58 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)56 TerminologyServiceException (org.hl7.fhir.exceptions.TerminologyServiceException)46 HashMap (java.util.HashMap)45 Test (org.junit.Test)45 CodeSystem (org.hl7.fhir.r5.model.CodeSystem)43 File (java.io.File)36 XhtmlNode (org.hl7.fhir.utilities.xhtml.XhtmlNode)36 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)31 CommaSeparatedStringBuilder (org.hl7.fhir.utilities.CommaSeparatedStringBuilder)29 RestIntegrationTest (org.opencds.cqf.ruler.test.RestIntegrationTest)29 FileInputStream (java.io.FileInputStream)27