Search in sources :

Example 31 with PlanDefinition

use of org.hl7.fhir.r5.model.PlanDefinition in project cqf-ruler by DBCG.

the class CdsHooksServletIT method testCdsServicesRequest.

@Test
public // TODO: Add Opioid Tests once $apply-cql is implemented.
void testCdsServicesRequest() throws IOException {
    // Server Load
    loadTransaction("HelloWorldPatientView-bundle.json");
    loadResource("hello-world-patient-view-patient.json");
    Patient ourPatient = getClient().read().resource(Patient.class).withId("patient-hello-world-patient-view").execute();
    assertNotNull(ourPatient);
    assertEquals("patient-hello-world-patient-view", ourPatient.getIdElement().getIdPart());
    PlanDefinition ourPlanDefinition = getClient().read().resource(PlanDefinition.class).withId("hello-world-patient-view").execute();
    assertNotNull(ourPlanDefinition);
    Bundle getPlanDefinitions = null;
    int tries = 0;
    do {
        // Can take up to 10 seconds for HAPI to reindex searches
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        tries++;
        getPlanDefinitions = getClient().search().forResource(PlanDefinition.class).returnBundle(Bundle.class).execute();
    } while (getPlanDefinitions.getEntry().size() == 0 && tries < 15);
    assertTrue(getPlanDefinitions.hasEntry());
    // Update fhirServer Base
    String jsonHooksRequest = stringFromResource("request-HelloWorld.json");
    Gson gsonRequest = new Gson();
    JsonObject jsonRequestObject = gsonRequest.fromJson(jsonHooksRequest, JsonObject.class);
    jsonRequestObject.addProperty("fhirServer", getServerBase());
    // Setup Client
    CloseableHttpClient httpClient = HttpClients.createDefault();
    HttpPost request = new HttpPost(ourCdsBase + "/hello-world-patient-view");
    request.setEntity(new StringEntity(jsonRequestObject.toString()));
    request.addHeader("Content-Type", "application/json");
    CloseableHttpResponse response = httpClient.execute(request);
    String result = EntityUtils.toString(response.getEntity());
    Gson gsonResponse = new Gson();
    JsonObject jsonResponseObject = gsonResponse.fromJson(result, JsonObject.class);
    // Ensure Cards
    assertNotNull(jsonResponseObject.get("cards"));
    JsonArray cards = jsonResponseObject.get("cards").getAsJsonArray();
    // Ensure Patient Detail
    assertNotNull(cards.get(0).getAsJsonObject().get("detail"));
    String patientName = cards.get(0).getAsJsonObject().get("detail").getAsString();
    assertEquals("The CDS Service is alive and communicating successfully!", patientName);
    // Ensure Summary
    assertNotNull(cards.get(0));
    assertNotNull(cards.get(0).getAsJsonObject().get("summary"));
    String summary = cards.get(0).getAsJsonObject().get("summary").getAsString();
    assertEquals("Hello World!", summary);
    // Ensure Activity Definition / Suggestions
    assertNotNull(cards.get(0).getAsJsonObject().get("suggestions"));
    JsonArray suggestions = cards.get(0).getAsJsonObject().get("suggestions").getAsJsonArray();
    assertNotNull(suggestions.get(0));
    assertNotNull(suggestions.get(0).getAsJsonObject().get("actions"));
    JsonArray actions = suggestions.get(0).getAsJsonObject().get("actions").getAsJsonArray();
    assertNotNull(actions.get(0));
    assertNotNull(actions.get(0).getAsJsonObject().get("description"));
    String suggestionsDescription = actions.get(0).getAsJsonObject().get("description").getAsString();
    assertEquals("The CDS Service is alive and communicating successfully!", suggestionsDescription);
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpPost(org.apache.http.client.methods.HttpPost) Bundle(org.hl7.fhir.dstu3.model.Bundle) Patient(org.hl7.fhir.dstu3.model.Patient) Gson(com.google.gson.Gson) JsonObject(com.google.gson.JsonObject) JsonArray(com.google.gson.JsonArray) StringEntity(org.apache.http.entity.StringEntity) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) PlanDefinition(org.hl7.fhir.dstu3.model.PlanDefinition) RestIntegrationTest(org.opencds.cqf.ruler.test.RestIntegrationTest) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 32 with PlanDefinition

use of org.hl7.fhir.r5.model.PlanDefinition in project cqf-ruler by DBCG.

the class CdsHooksServletIT method testCdsServicesRequest.

@Test
public // TODO: Debug delay in Client.search().
void testCdsServicesRequest() throws IOException {
    // Server Load
    loadTransaction("Screening-bundle-r4.json");
    Patient ourPatient = getClient().read().resource(Patient.class).withId("HighRiskIDUPatient").execute();
    assertNotNull(ourPatient);
    assertEquals("HighRiskIDUPatient", ourPatient.getIdElement().getIdPart());
    PlanDefinition ourPlanDefinition = getClient().read().resource(PlanDefinition.class).withId("plandefinition-Screening").execute();
    assertNotNull(ourPlanDefinition);
    Bundle getPlanDefinitions = null;
    int tries = 0;
    do {
        // Can take up to 10 seconds for HAPI to reindex searches
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        tries++;
        getPlanDefinitions = getClient().search().forResource(PlanDefinition.class).returnBundle(Bundle.class).execute();
    } while (getPlanDefinitions.getEntry().size() == 0 && tries < 15);
    assertTrue(getPlanDefinitions.hasEntry());
    // Update fhirServer Base
    String jsonHooksRequest = stringFromResource("request-HighRiskIDUPatient.json");
    Gson gsonRequest = new Gson();
    JsonObject jsonRequestObject = gsonRequest.fromJson(jsonHooksRequest, JsonObject.class);
    jsonRequestObject.addProperty("fhirServer", getServerBase());
    // Setup Client
    CloseableHttpClient httpClient = HttpClients.createDefault();
    HttpPost request = new HttpPost(ourCdsBase + "/plandefinition-Screening");
    request.setEntity(new StringEntity(jsonRequestObject.toString()));
    request.addHeader("Content-Type", "application/json");
    CloseableHttpResponse response = httpClient.execute(request);
    String result = EntityUtils.toString(response.getEntity());
    Gson gsonResponse = new Gson();
    JsonObject jsonResponseObject = gsonResponse.fromJson(result, JsonObject.class);
    // Ensure Cards
    assertNotNull(jsonResponseObject.get("cards"));
    JsonArray cards = jsonResponseObject.get("cards").getAsJsonArray();
    // Ensure Patient Detail
    assertNotNull(cards.get(0).getAsJsonObject().get("detail"));
    String patientName = cards.get(0).getAsJsonObject().get("detail").getAsString();
    assertEquals("Ashley Madelyn", patientName);
    // Ensure Summary
    assertNotNull(cards.get(1));
    assertNotNull(cards.get(1).getAsJsonObject().get("summary"));
    String recommendation = cards.get(1).getAsJsonObject().get("summary").getAsString();
    assertEquals("HIV Screening Recommended due to patient being at High Risk for HIV and over three months have passed since previous screening.", recommendation);
    // Ensure Activity Definition / Suggestions
    assertNotNull(cards.get(1).getAsJsonObject().get("suggestions"));
    JsonArray suggestions = cards.get(1).getAsJsonObject().get("suggestions").getAsJsonArray();
    assertNotNull(suggestions.get(0));
    assertNotNull(suggestions.get(0).getAsJsonObject().get("actions"));
    JsonArray actions = suggestions.get(0).getAsJsonObject().get("actions").getAsJsonArray();
    assertNotNull(actions.get(0));
    assertNotNull(actions.get(0).getAsJsonObject().get("resource"));
    JsonObject suggestionsActivityResource = actions.get(0).getAsJsonObject().get("resource").getAsJsonObject();
    assertNotNull(suggestionsActivityResource.get("resourceType"));
    assertEquals("ServiceRequest", suggestionsActivityResource.get("resourceType").getAsString());
    assertNotNull(suggestionsActivityResource.get("subject"));
    String expectedPatientID = ourPatient.getIdElement().getIdPart();
    String actualPatientID = suggestionsActivityResource.get("subject").getAsJsonObject().get("reference").getAsString();
    assertEquals("Patient/" + expectedPatientID, actualPatientID);
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpPost(org.apache.http.client.methods.HttpPost) Bundle(org.hl7.fhir.r4.model.Bundle) Patient(org.hl7.fhir.r4.model.Patient) Gson(com.google.gson.Gson) JsonObject(com.google.gson.JsonObject) JsonArray(com.google.gson.JsonArray) StringEntity(org.apache.http.entity.StringEntity) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) PlanDefinition(org.hl7.fhir.r4.model.PlanDefinition) RestIntegrationTest(org.opencds.cqf.ruler.test.RestIntegrationTest) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 33 with PlanDefinition

use of org.hl7.fhir.r5.model.PlanDefinition in project cqf-ruler by DBCG.

the class DiscoveryResolutionR4 method getPrefetchUrlList.

public PrefetchUrlList getPrefetchUrlList(PlanDefinition planDefinition) {
    PrefetchUrlList prefetchList = new PrefetchUrlList();
    if (planDefinition == null)
        return null;
    if (!isEca(planDefinition))
        return null;
    Library library = resolvePrimaryLibrary(planDefinition);
    // TODO: resolve data requirements
    if (library == null || !library.hasDataRequirement())
        return null;
    for (DataRequirement dataRequirement : library.getDataRequirement()) {
        List<String> requestUrls = createRequestUrl(dataRequirement);
        if (requestUrls != null) {
            prefetchList.addAll(requestUrls);
        }
    }
    return prefetchList;
}
Also used : Library(org.hl7.fhir.r4.model.Library) DataRequirement(org.hl7.fhir.r4.model.DataRequirement)

Example 34 with PlanDefinition

use of org.hl7.fhir.r5.model.PlanDefinition in project cqf-ruler by DBCG.

the class DiscoveryResolutionR4 method resolve.

public DiscoveryResponse resolve() {
    List<PlanDefinition> planDefinitions = search(PlanDefinition.class, Searches.all()).getAllResourcesTyped();
    DiscoveryResponse response = new DiscoveryResponse();
    for (PlanDefinition resource : planDefinitions) {
        response.addElement(new DiscoveryElementR4(resource, getPrefetchUrlList(resource)));
    }
    return response;
}
Also used : PlanDefinition(org.hl7.fhir.r4.model.PlanDefinition)

Example 35 with PlanDefinition

use of org.hl7.fhir.r5.model.PlanDefinition in project cqf-ruler by DBCG.

the class DiscoveryElementR4 method getAsJson.

public JsonObject getAsJson() {
    JsonObject service = new JsonObject();
    if (planDefinition != null) {
        if (planDefinition.hasAction()) {
            // TODO - this needs some work - too naive
            if (planDefinition.getActionFirstRep().hasTrigger()) {
                if (planDefinition.getActionFirstRep().getTriggerFirstRep().hasName()) {
                    service.addProperty("hook", planDefinition.getActionFirstRep().getTriggerFirstRep().getName());
                }
            }
        }
        if (planDefinition.hasName()) {
            service.addProperty("name", planDefinition.getName());
        }
        if (planDefinition.hasTitle()) {
            service.addProperty("title", planDefinition.getTitle());
        }
        if (planDefinition.hasDescription()) {
            service.addProperty("description", planDefinition.getDescription());
        }
        service.addProperty("id", planDefinition.getIdElement().getIdPart());
        if (prefetchUrlList == null) {
            prefetchUrlList = new PrefetchUrlList();
        }
        JsonObject prefetchContent = new JsonObject();
        int itemNo = 0;
        if (!prefetchUrlList.stream().anyMatch(p -> p.equals("Patient/{{context.patientId}}") || p.equals("Patient?_id={{context.patientId}}") || p.equals("Patient?_id=Patient/{{context.patientId}}"))) {
            prefetchContent.addProperty("item1", "Patient?_id={{context.patientId}}");
            ++itemNo;
        }
        for (String item : prefetchUrlList) {
            prefetchContent.addProperty("item" + Integer.toString(++itemNo), item);
        }
        service.add("prefetch", prefetchContent);
        return service;
    }
    return null;
}
Also used : JsonObject(com.google.gson.JsonObject) PlanDefinition(org.hl7.fhir.r4.model.PlanDefinition) JsonObject(com.google.gson.JsonObject)

Aggregations

Complex (org.hl7.fhir.dstu3.utils.formats.Turtle.Complex)8 Complex (org.hl7.fhir.r4.utils.formats.Turtle.Complex)8 PlanDefinition (org.hl7.fhir.dstu3.model.PlanDefinition)7 PlanDefinition (org.hl7.fhir.r4.model.PlanDefinition)7 SystemRequestDetails (ca.uhn.fhir.jpa.partition.SystemRequestDetails)6 Test (org.junit.jupiter.api.Test)6 RestIntegrationTest (org.opencds.cqf.ruler.test.RestIntegrationTest)6 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)6 ArrayList (java.util.ArrayList)5 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)5 JsonObject (com.google.gson.JsonObject)4 DomainResource (org.hl7.fhir.dstu3.model.DomainResource)4 IBaseResource (org.hl7.fhir.instance.model.api.IBaseResource)3 Library (org.hl7.fhir.r4.model.Library)3 StructureDefinition (org.hl7.fhir.r4b.model.StructureDefinition)3 CanonicalResourceProxy (org.hl7.fhir.r5.context.CanonicalResourceManager.CanonicalResourceProxy)3 StructureDefinition (org.hl7.fhir.r5.model.StructureDefinition)3 FhirContext (ca.uhn.fhir.context.FhirContext)2 Operation (ca.uhn.fhir.rest.annotation.Operation)2 RequestDetails (ca.uhn.fhir.rest.api.server.RequestDetails)2