Search in sources :

Example 91 with IParser

use of org.hl7.fhir.r4b.formats.IParser in project beneficiary-fhir-data by CMSgov.

the class OutpatientClaimTransformerV2Test method before.

@BeforeEach
public void before() {
    claim = generateClaim();
    ExplanationOfBenefit genEob = OutpatientClaimTransformerV2.transform(new MetricRegistry(), claim, Optional.empty());
    IParser parser = fhirContext.newJsonParser();
    String json = parser.encodeResourceToString(genEob);
    eob = parser.parseResource(ExplanationOfBenefit.class, json);
}
Also used : MetricRegistry(com.codahale.metrics.MetricRegistry) ExplanationOfBenefit(org.hl7.fhir.r4.model.ExplanationOfBenefit) IParser(ca.uhn.fhir.parser.IParser) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 92 with IParser

use of org.hl7.fhir.r4b.formats.IParser in project health-patterns by LinuxForHealth.

the class DeIdentifier method postResource.

/**
 * Posts the given Resource to the FHIR server
 *
 * @param fhirResource the resource to post
 * @return the FHIR response of executing the POST operation
 * @throws DeIdentifierException if there is a problem posting the resource
 */
@SuppressWarnings("unchecked")
private String postResource(JsonNode fhirResource) throws DeIdentifierException {
    System.out.println("Initializing FHIR create resource...");
    IParser parser = fhirClient.getFhirContext().newJsonParser();
    // We don't know exactly what type of Resource we are going to POST to FHiR and the FHIR requires
    // that when parsing a Resource a concrete class is used, so we load the concrete type reflexively
    ClassLoader classLoader = this.getClass().getClassLoader();
    Class<? extends Resource> aClass = null;
    String resourceType = getResourceType(fhirResource);
    try {
        aClass = (Class<? extends Resource>) classLoader.loadClass("org.hl7.fhir.r4.model." + resourceType);
    } catch (ClassNotFoundException e) {
        throw new DeIdentifierException("A Resource of type '" + resourceType + "' could not be found in the current HAPI FHIR JAR: " + e.getMessage(), e);
    }
    Resource resource = parser.parseResource(aClass, fhirResource.toString());
    MethodOutcome outcome;
    try {
        outcome = fhirClient.create().resource(resource).encodedJson().execute();
    } catch (BaseServerResponseException e) {
        throw new DeIdentifierException("The FHIR transaction could not be executed: " + e.getMessage(), e);
    }
    System.out.println("FHIR create resource done!");
    return outcome.getId().toString();
}
Also used : Resource(org.hl7.fhir.r4.model.Resource) BaseServerResponseException(ca.uhn.fhir.rest.server.exceptions.BaseServerResponseException) MethodOutcome(ca.uhn.fhir.rest.api.MethodOutcome) IParser(ca.uhn.fhir.parser.IParser)

Example 93 with IParser

use of org.hl7.fhir.r4b.formats.IParser in project health-patterns by LinuxForHealth.

the class CohortService method getPatientsForCohort.

/**
 * Gets the FHIR response with the patients that matched the cohort defined by the given library ID.
 *
 * @param libraryID the library ID
 * @param patientsSubset the subset of patients over which to execute the cohort
 * @param reverseMatch run the CQL such that only the patients that don't match it are returned
 * @return the FHIR JSON response (a Bundle) with all the patients that matched
 * @throws CQLExecutionException if there is a problem executing the library
 */
public String getPatientsForCohort(String libraryID, List<String> patientsSubset, boolean reverseMatch) throws CQLExecutionException {
    List<String> cohortPatients = getPatientIdsForCohort(libraryID, patientsSubset, reverseMatch);
    if (cohortPatients == null) {
        return null;
    }
    String theSearchUrl = fhir.getServerBase() + "/Patient?_id=" + String.join(",", cohortPatients);
    Bundle cohortBundle = fhir.search().byUrl(theSearchUrl).returnBundle(Bundle.class).execute();
    IParser parser = fhir.getFhirContext().newJsonParser();
    String serialized = parser.encodeResourceToString(cohortBundle);
    return serialized;
}
Also used : Bundle(org.hl7.fhir.r4.model.Bundle) IParser(ca.uhn.fhir.parser.IParser)

Example 94 with IParser

use of org.hl7.fhir.r4b.formats.IParser in project BridgeServer2 by Sage-Bionetworks.

the class CRCController method postAppointment.

@PutMapping("/v1/cuimc/appointments")
public ResponseEntity<StatusMessage> postAppointment() {
    App app = httpBasicAuthentication();
    IParser parser = FHIR_CONTEXT.newJsonParser();
    JsonNode data = parseJson(JsonNode.class);
    Appointment appointment = parser.parseResource(Appointment.class, data.toString());
    String userId = findUserId(appointment);
    // They send appointment when it is booked, cancelled, or (rarely) enteredinerror.
    AccountStates state = TESTS_SCHEDULED;
    String apptStatus = data.get("status").asText();
    if ("entered-in-error".equals(apptStatus)) {
        deleteReportAndUpdateState(app, userId);
        return ResponseEntity.ok(new StatusMessage("Appointment deleted."));
    } else if ("cancelled".equals(apptStatus)) {
        state = TESTS_CANCELLED;
    }
    // Columbia wants us to call back to them to get information about the location.
    // And UI team wants geocoding of location to render a map.
    String locationString = findLocation(appointment);
    if (locationString != null) {
        AccountId accountId = parseAccountId(app.getIdentifier(), userId);
        Account account = accountService.getAccount(accountId).orElseThrow(() -> new EntityNotFoundException(Account.class));
        addLocation(data, account, locationString);
    }
    int status = writeReportAndUpdateState(app, userId, data, APPOINTMENT_REPORT, state, true);
    if (status == 200) {
        return ResponseEntity.ok(new StatusMessage("Appointment updated (status = " + apptStatus + ")."));
    }
    return ResponseEntity.created(URI.create("/v1/cuimc/appointments/" + userId)).body(new StatusMessage("Appointment created (status = " + apptStatus + ")."));
}
Also used : App(org.sagebionetworks.bridge.models.apps.App) Appointment(org.hl7.fhir.dstu3.model.Appointment) Account(org.sagebionetworks.bridge.models.accounts.Account) BridgeUtils.parseAccountId(org.sagebionetworks.bridge.BridgeUtils.parseAccountId) AccountId(org.sagebionetworks.bridge.models.accounts.AccountId) JsonNode(com.fasterxml.jackson.databind.JsonNode) EntityNotFoundException(org.sagebionetworks.bridge.exceptions.EntityNotFoundException) ContactPoint(org.hl7.fhir.dstu3.model.ContactPoint) IParser(ca.uhn.fhir.parser.IParser) StatusMessage(org.sagebionetworks.bridge.models.StatusMessage) PutMapping(org.springframework.web.bind.annotation.PutMapping)

Example 95 with IParser

use of org.hl7.fhir.r4b.formats.IParser in project BridgeServer2 by Sage-Bionetworks.

the class CRCController method postObservation.

@PutMapping("/v1/cuimc/observations")
public ResponseEntity<StatusMessage> postObservation() {
    App app = httpBasicAuthentication();
    IParser parser = FHIR_CONTEXT.newJsonParser();
    JsonNode data = parseJson(JsonNode.class);
    Observation observation = parser.parseResource(Observation.class, data.toString());
    AccountStates state = TESTS_AVAILABLE;
    // There are two conditions under which the type of report is considered to be unknown. Either the code
    // is for a test other than the serum test, or the result value is not recognized by our client application,
    // and that's also treated as an unknown type of report.
    String code = getObservationCoding(observation);
    String valueString = getObservationValue(observation);
    if (code == null || valueString == null || !SERUM_TEST_CODES.contains(code) || !SERUM_TEST_STATES.contains(valueString)) {
        state = TESTS_AVAILABLE_TYPE_UNKNOWN;
        LOG.warn("CRC observation in unknown format: code=" + code + ", valueString=" + valueString);
    }
    String userId = findUserId(observation.getSubject());
    int status = writeReportAndUpdateState(app, userId, data, OBSERVATION_REPORT, state, true);
    if (status == 200) {
        return ResponseEntity.ok(new StatusMessage("Observation updated."));
    }
    return ResponseEntity.created(URI.create("/v1/cuimc/observations/" + userId)).body(new StatusMessage("Observation created."));
}
Also used : App(org.sagebionetworks.bridge.models.apps.App) Observation(org.hl7.fhir.dstu3.model.Observation) JsonNode(com.fasterxml.jackson.databind.JsonNode) ContactPoint(org.hl7.fhir.dstu3.model.ContactPoint) IParser(ca.uhn.fhir.parser.IParser) StatusMessage(org.sagebionetworks.bridge.models.StatusMessage) PutMapping(org.springframework.web.bind.annotation.PutMapping)

Aggregations

IParser (ca.uhn.fhir.parser.IParser)89 FhirContext (ca.uhn.fhir.context.FhirContext)43 IOException (java.io.IOException)35 ByteArrayOutputStream (java.io.ByteArrayOutputStream)30 Test (org.junit.Test)24 InputStream (java.io.InputStream)22 IParser (org.hl7.fhir.r5.formats.IParser)19 JsonParser (org.hl7.fhir.r5.formats.JsonParser)18 Test (org.junit.jupiter.api.Test)18 FHIRException (org.hl7.fhir.exceptions.FHIRException)17 ByteArrayInputStream (java.io.ByteArrayInputStream)16 File (java.io.File)16 FileInputStream (java.io.FileInputStream)16 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)15 Bundle (org.hl7.fhir.r4.model.Bundle)14 FileOutputStream (java.io.FileOutputStream)12 Bundle (org.hl7.fhir.dstu3.model.Bundle)12 IBaseResource (org.hl7.fhir.instance.model.api.IBaseResource)12 XmlParser (org.hl7.fhir.r5.formats.XmlParser)12 ArrayList (java.util.ArrayList)11