Search in sources :

Example 1 with OperationOutcome

use of org.hl7.fhir.r4b.model.OperationOutcome in project cqf-ruler by DBCG.

the class ProcessMessageProvider method processMessageBundle.

@Operation(name = "$process-message-bundle", idempotent = false)
public Bundle processMessageBundle(HttpServletRequest theServletRequest, RequestDetails theRequestDetails, @OperationParam(name = "content", min = 1, max = 1) @Description(formalDefinition = "The message to process (or, if using asynchronous messaging, it may be a response message to accept)") Bundle theMessageToProcess) {
    logger.info("Validating the Bundle");
    Bundle bundle = theMessageToProcess;
    Boolean errorExists = false;
    OperationOutcome outcome = validateBundle(errorExists, bundle);
    if (!errorExists) {
        IVersionSpecificBundleFactory bundleFactory = this.getFhirContext().newBundleFactory();
        bundle.setId(getUUID());
        bundleFactory.initializeWithBundleResource(bundle);
        Bundle dafBundle = (Bundle) bundleFactory.getResourceBundle();
        dafBundle.setTimestamp(new Date());
        this.getDaoRegistry().getResourceDao(Bundle.class).create(dafBundle);
        MessageHeader messageHeader = null;
        String patientId = null;
        String commId = null;
        List<MessageHeader> headers = BundleUtil.toListOfResourcesOfType(this.getFhirContext(), bundle, MessageHeader.class);
        for (MessageHeader mh : headers) {
            messageHeader = mh;
            messageHeader.setId(getUUID());
            Meta meta = messageHeader.getMeta();
            meta.setLastUpdated(new Date());
            messageHeader.setMeta(meta);
        }
        List<IBaseResource> resources = new ArrayList<>();
        List<Patient> patients = BundleUtil.toListOfResourcesOfType(this.getFhirContext(), bundle, Patient.class);
        for (Patient p : patients) {
            patientId = p.getId();
            p.setId(p.getIdElement().toVersionless());
            resources.add(p);
        }
        List<Bundle> bundles = BundleUtil.toListOfResourcesOfType(this.getFhirContext(), bundle, Bundle.class);
        for (Bundle b : bundles) {
            patientId = this.processBundle(b, theRequestDetails);
            b.setId(b.getIdElement().toVersionless());
            resources.add(b);
        }
        for (BundleEntryComponent e : bundle.getEntry()) {
            Resource r = e.getResource();
            if (r == null) {
                continue;
            }
            if (r.fhirType().equals("Bundle") || r.fhirType().equals("MessageHeader") || r.fhirType().equals("Patient")) {
                continue;
            }
            r.setId(r.getIdElement().toVersionless());
            resources.add(r);
        }
        if (patientId != null) {
            commId = constructAndSaveCommunication(patientId);
        }
        if (messageHeader == null) {
            messageHeader = constructMessageHeaderResource();
            BundleEntryComponent entryComp = new BundleEntryComponent();
            entryComp.setResource(messageHeader);
            dafBundle.addEntry(entryComp);
        }
        if (commId != null) {
            List<Reference> referenceList = new ArrayList<>();
            Reference commRef = new Reference();
            commRef.setReference("Communication/" + commId);
            referenceList.add(commRef);
            messageHeader.setFocus(referenceList);
        }
        IVersionSpecificBundleFactory newBundleFactory = this.getFhirContext().newBundleFactory();
        newBundleFactory.addResourcesToBundle(resources, BundleTypeEnum.TRANSACTION, theRequestDetails.getFhirServerBase(), null, null);
        Bundle transactionBundle = (Bundle) newBundleFactory.getResourceBundle();
        for (BundleEntryComponent entry : transactionBundle.getEntry()) {
            UriType uri = new UriType(theRequestDetails.getFhirServerBase() + "/" + entry.getResource().fhirType() + "/" + entry.getResource().getIdElement().getIdPart());
            Enumeration<HTTPVerb> method = new Enumeration<>(new HTTPVerbEnumFactory());
            method.setValue(HTTPVerb.PUT);
            entry.setRequest(new BundleEntryRequestComponent(method, uri));
        }
        @SuppressWarnings("unchecked") IFhirSystemDao<Bundle, Meta> fhirSystemDao = this.getDaoRegistry().getSystemDao();
        fhirSystemDao.transaction(theRequestDetails, transactionBundle);
        return dafBundle;
    } else {
        BundleEntryComponent entryComp = new BundleEntryComponent();
        entryComp.setResource(outcome);
        bundle.addEntry(entryComp);
        return bundle;
    }
}
Also used : HTTPVerb(org.hl7.fhir.r4.model.Bundle.HTTPVerb) Meta(org.hl7.fhir.r4.model.Meta) ArrayList(java.util.ArrayList) UriType(org.hl7.fhir.r4.model.UriType) BundleEntryRequestComponent(org.hl7.fhir.r4.model.Bundle.BundleEntryRequestComponent) OperationOutcome(org.hl7.fhir.r4.model.OperationOutcome) Enumeration(org.hl7.fhir.r4.model.Enumeration) Bundle(org.hl7.fhir.r4.model.Bundle) Reference(org.hl7.fhir.r4.model.Reference) IBaseResource(org.hl7.fhir.instance.model.api.IBaseResource) Resource(org.hl7.fhir.r4.model.Resource) IVersionSpecificBundleFactory(ca.uhn.fhir.rest.api.IVersionSpecificBundleFactory) Patient(org.hl7.fhir.r4.model.Patient) HTTPVerbEnumFactory(org.hl7.fhir.r4.model.Bundle.HTTPVerbEnumFactory) Date(java.util.Date) BundleEntryComponent(org.hl7.fhir.r4.model.Bundle.BundleEntryComponent) MessageHeader(org.hl7.fhir.r4.model.MessageHeader) IBaseResource(org.hl7.fhir.instance.model.api.IBaseResource) Operation(ca.uhn.fhir.rest.annotation.Operation)

Example 2 with OperationOutcome

use of org.hl7.fhir.r4b.model.OperationOutcome in project cqf-ruler by DBCG.

the class HelloWorldProvider method hello_world.

/**
 * Implements the $hello-world operation found in the
 * <a href="https://www.hl7.org/fhir/clinicalreasoning-module.html">FHIR CR
 * Module</a>
 *
 * @return a greeting
 */
@Description(shortDefinition = "returns a greeting", value = "Implements the $hello-world operation found in the <a href=\"https://www.hl7.org/fhir/clinicalreasoning-module.html\">FHIR CR Module</a>")
@Operation(idempotent = true, name = "$hello-world")
public OperationOutcome hello_world() {
    OperationOutcome outcome = new OperationOutcome();
    outcome.addIssue().setDiagnostics(helloWorldProperties.getMessage());
    return outcome;
}
Also used : OperationOutcome(org.hl7.fhir.r4.model.OperationOutcome) Description(ca.uhn.fhir.model.api.annotation.Description) Operation(ca.uhn.fhir.rest.annotation.Operation)

Example 3 with OperationOutcome

use of org.hl7.fhir.r4b.model.OperationOutcome in project cqf-ruler by DBCG.

the class HelloWorldProviderIT method testHelloWorldConfig.

@Test
public void testHelloWorldConfig() {
    OperationOutcome outcome = getClient().operation().onServer().named("$hello-world").withNoParameters(Parameters.class).returnResourceType(OperationOutcome.class).execute();
    assertNotNull(outcome);
    assertEquals("Howdy", outcome.getIssueFirstRep().getDiagnostics());
}
Also used : OperationOutcome(org.hl7.fhir.r4.model.OperationOutcome) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) RestIntegrationTest(org.opencds.cqf.ruler.test.RestIntegrationTest)

Example 4 with OperationOutcome

use of org.hl7.fhir.r4b.model.OperationOutcome 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.dstu3.model.Endpoint) BasicAuthInterceptor(ca.uhn.fhir.rest.client.interceptor.BasicAuthInterceptor) IGenericClient(ca.uhn.fhir.rest.client.api.IGenericClient) Bundle(org.hl7.fhir.dstu3.model.Bundle) Resource(org.hl7.fhir.dstu3.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 5 with OperationOutcome

use of org.hl7.fhir.r4b.model.OperationOutcome in project cqf-ruler by DBCG.

the class CodeSystemUpdateProvider method performCodeSystemUpdate.

public OperationOutcome performCodeSystemUpdate(List<ValueSet> valueSets) {
    OperationOutcome response = new OperationOutcome();
    List<String> codeSystems = new ArrayList<>();
    // Possible for this to run out of memory with really large ValueSets and
    // CodeSystems.
    Map<String, Set<String>> codesBySystem = new HashMap<>();
    for (ValueSet vs : valueSets) {
        if (vs.hasCompose() && vs.getCompose().hasInclude()) {
            for (ValueSet.ConceptSetComponent csc : vs.getCompose().getInclude()) {
                if (!csc.hasSystem() || !csc.hasConcept()) {
                    continue;
                }
                String system = csc.getSystem();
                if (!codesBySystem.containsKey(system)) {
                    codesBySystem.put(system, new HashSet<>());
                }
                Set<String> codes = codesBySystem.get(system);
                codes.addAll(csc.getConcept().stream().map(ValueSet.ConceptReferenceComponent::getCode).collect(Collectors.toList()));
            }
        }
    }
    for (Map.Entry<String, Set<String>> entry : codesBySystem.entrySet()) {
        String system = entry.getKey();
        CodeSystem codeSystem = getCodeSystemByUrl(system);
        updateCodeSystem(codeSystem.setUrl(system), getUnionDistinctCodes(entry.getValue(), codeSystem));
        codeSystems.add(codeSystem.getUrl());
    }
    if (codeSystems.size() > 0) {
        return buildIssue(response, "information", "informational", "Successfully updated the following CodeSystems: " + String.join(", ", codeSystems));
    } else {
        return buildIssue(response, "information", "informational", "No code systems were updated");
    }
}
Also used : HashSet(java.util.HashSet) IFhirResourceDaoValueSet(ca.uhn.fhir.jpa.api.dao.IFhirResourceDaoValueSet) Set(java.util.Set) ValueSet(org.hl7.fhir.dstu3.model.ValueSet) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CodeSystem(org.hl7.fhir.dstu3.model.CodeSystem) IFhirResourceDaoCodeSystem(ca.uhn.fhir.jpa.api.dao.IFhirResourceDaoCodeSystem) OperationOutcome(org.hl7.fhir.dstu3.model.OperationOutcome) IFhirResourceDaoValueSet(ca.uhn.fhir.jpa.api.dao.IFhirResourceDaoValueSet) ValueSet(org.hl7.fhir.dstu3.model.ValueSet) HashMap(java.util.HashMap) Map(java.util.Map) SearchParameterMap(ca.uhn.fhir.jpa.searchparam.SearchParameterMap)

Aggregations

Test (org.junit.Test)214 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)181 OperationOutcome (org.hl7.fhir.r4.model.OperationOutcome)161 OperationOutcome (org.hl7.fhir.dstu3.model.OperationOutcome)144 Test (org.junit.jupiter.api.Test)42 BaseFhirIntegrationTest (org.openmrs.module.fhir2.BaseFhirIntegrationTest)31 IOException (java.io.IOException)26 IBaseOperationOutcome (org.hl7.fhir.instance.model.api.IBaseOperationOutcome)24 OperationOutcomeIssueComponent (org.hl7.fhir.r4.model.OperationOutcome.OperationOutcomeIssueComponent)23 MethodOutcome (ca.uhn.fhir.rest.api.MethodOutcome)22 OperationOutcome (org.hl7.fhir.r5.model.OperationOutcome)20 IdType (org.hl7.fhir.dstu3.model.IdType)17 IdType (org.hl7.fhir.r4.model.IdType)17 CodeableConcept (org.hl7.fhir.dstu3.model.CodeableConcept)15 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)15 URISyntaxException (java.net.URISyntaxException)13 GET (javax.ws.rs.GET)12 Path (javax.ws.rs.Path)12 Produces (javax.ws.rs.Produces)12 Header (org.apache.http.Header)11