Search in sources :

Example 56 with FHIRVersion

use of org.hl7.fhir.r5.model.Enumerations.FHIRVersion in project org.hl7.fhir.core by hapifhir.

the class ProfileUtilities method makeBaseDefinition.

public static StructureDefinition makeBaseDefinition(FHIRVersion fhirVersion) {
    StructureDefinition base = new StructureDefinition();
    base.setId("Base");
    base.setUrl("http://hl7.org/fhir/StructureDefinition/Base");
    base.setVersion(fhirVersion.toCode());
    base.setName("Base");
    base.setStatus(PublicationStatus.ACTIVE);
    base.setDate(new Date());
    base.setFhirVersion(fhirVersion);
    base.setKind(StructureDefinitionKind.COMPLEXTYPE);
    base.setAbstract(true);
    base.setType("Base");
    ElementDefinition e = base.getSnapshot().getElementFirstRep();
    e.setId("Base");
    e.setPath("Base");
    e.setMin(0);
    e.setMax("*");
    e.getBase().setPath("Base");
    e.getBase().setMin(0);
    e.getBase().setMax("*");
    e.setIsModifier(false);
    e = base.getDifferential().getElementFirstRep();
    e.setId("Base");
    e.setPath("Base");
    e.setMin(0);
    e.setMax("*");
    return base;
}
Also used : StructureDefinition(org.hl7.fhir.r5.model.StructureDefinition) ElementDefinition(org.hl7.fhir.r5.model.ElementDefinition) Date(java.util.Date)

Example 57 with FHIRVersion

use of org.hl7.fhir.r5.model.Enumerations.FHIRVersion in project eCRNow by drajer-health.

the class LaunchController method getEncounterById.

public IBaseResource getEncounterById(LaunchDetails launchDetails) {
    String fhirVersion = launchDetails.getFhirVersion();
    String encounterId = launchDetails.getEncounterId();
    IBaseResource encounterResource = null;
    String EncounterError = "Error in getting Encounter resource by Id: " + encounterId;
    logger.info("Getting Encounter data by ID {}", encounterId);
    try {
        FhirContext fhirContext = fhirContextInitializer.getFhirContext(fhirVersion);
        IGenericClient fhirClient = fhirContextInitializer.createClient(fhirContext, launchDetails);
        if (!StringUtils.isEmpty(encounterId)) {
            return fhirClient.read().resource("Encounter").withId(encounterId).execute();
        }
        IBaseBundle bundle = fhirContextInitializer.getResourceByPatientId(launchDetails, fhirClient, fhirContext, "Encounter");
        if (bundle != null && !bundle.isEmpty()) {
            if (fhirVersion.equalsIgnoreCase(FhirVersionEnum.R4.toString())) {
                Bundle r4Bundle = (Bundle) bundle;
                Map<org.hl7.fhir.r4.model.Encounter, Date> encounterMap = new HashMap<>();
                for (BundleEntryComponent entry : r4Bundle.getEntry()) {
                    org.hl7.fhir.r4.model.Encounter encounterEntry = (org.hl7.fhir.r4.model.Encounter) entry.getResource();
                    encounterId = encounterEntry.getIdElement().getIdPart();
                    logger.info("Received Encounter Id ========> {}", encounterId);
                    encounterMap.put(encounterEntry, encounterEntry.getMeta().getLastUpdated());
                }
                encounterResource = Collections.max(encounterMap.entrySet(), Map.Entry.comparingByValue()).getKey();
                launchDetails.setEncounterId(encounterResource.getIdElement().getIdPart());
                return encounterResource;
            }
            if (fhirVersion.equalsIgnoreCase(FhirVersionEnum.DSTU2.toString())) {
                ca.uhn.fhir.model.dstu2.resource.Bundle dstu2Bundle = (ca.uhn.fhir.model.dstu2.resource.Bundle) bundle;
                Map<Encounter, Date> encounterMap = new HashMap<>();
                for (Entry entry : dstu2Bundle.getEntry()) {
                    Encounter encounterEntry = (Encounter) entry.getResource();
                    encounterId = encounterEntry.getIdElement().getIdPart();
                    logger.info("Received Encounter Id ========> {}", encounterId);
                    encounterMap.put(encounterEntry, encounterEntry.getMeta().getLastUpdated());
                }
                encounterResource = Collections.max(encounterMap.entrySet(), Map.Entry.comparingByValue()).getKey();
                launchDetails.setEncounterId(encounterResource.getIdElement().getIdPart());
                return encounterResource;
            }
        }
        logger.error(EncounterError);
        throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, EncounterError);
    } catch (ResourceNotFoundException notFoundException) {
        logger.error(EncounterError, notFoundException);
        throw new ResponseStatusException(HttpStatus.NOT_FOUND, EncounterError, notFoundException);
    } catch (Exception e) {
        logger.error(EncounterError, e);
        throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, EncounterError, e);
    }
}
Also used : FhirContext(ca.uhn.fhir.context.FhirContext) HashMap(java.util.HashMap) IGenericClient(ca.uhn.fhir.rest.client.api.IGenericClient) IBaseBundle(org.hl7.fhir.instance.model.api.IBaseBundle) Entry(ca.uhn.fhir.model.dstu2.resource.Bundle.Entry) Encounter(ca.uhn.fhir.model.dstu2.resource.Encounter) ResourceNotFoundException(ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException) IBaseBundle(org.hl7.fhir.instance.model.api.IBaseBundle) Bundle(org.hl7.fhir.r4.model.Bundle) Date(java.util.Date) ParseException(java.text.ParseException) ResponseStatusException(org.springframework.web.server.ResponseStatusException) ResourceNotFoundException(ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) BundleEntryComponent(org.hl7.fhir.r4.model.Bundle.BundleEntryComponent) IBaseResource(org.hl7.fhir.instance.model.api.IBaseResource) ResponseStatusException(org.springframework.web.server.ResponseStatusException)

Example 58 with FHIRVersion

use of org.hl7.fhir.r5.model.Enumerations.FHIRVersion in project CRD by HL7-DaVinci.

the class CqlRule method build.

private void build(HashMap<String, byte[]> cqlFiles, HashMap<String, byte[]> jsonElmFiles, HashMap<String, byte[]> xmlElmFiles) {
    // build a list of all of the CQL Libraries
    List<CqlRule.CqlLibrary> cqlLibraries = new ArrayList<>();
    for (String fileName : cqlFiles.keySet()) {
        logger.debug("CqlRule: file: " + fileName);
        CqlRule.CqlLibrary cqlLibrary = new CqlRule.CqlLibrary();
        cqlLibrary.cql = cqlFiles.get(fileName);
        // only add those that are the right fhir version
        String fhirVersionFromCql = getFhirVersionFromCqlFile(cqlLibrary.cql);
        // last character of fhirVersion ("R4" => "4") and first character of FHIR version from file ("3.0.0" => "3")
        if (fhirVersion.substring(fhirVersion.length() - 1).equalsIgnoreCase(fhirVersionFromCql.substring(0, 1))) {
            String fileNameWithoutExtension = fileName.substring(0, fileName.length() - 4);
            String xmlElmName = fileNameWithoutExtension + ".xml";
            if (xmlElmFiles.containsKey(xmlElmName)) {
                cqlLibrary.xlmElm = true;
                cqlLibrary.elm = xmlElmFiles.get(xmlElmName);
            }
            String jsonElmName = fileNameWithoutExtension + ".json";
            if (jsonElmFiles.containsKey(jsonElmName)) {
                cqlLibrary.xlmElm = false;
                cqlLibrary.elm = jsonElmFiles.get(jsonElmName);
            }
            cqlLibraries.add(cqlLibrary);
        }
    }
    precompiled = false;
    for (CqlLibrary cqlLibrary : cqlLibraries) {
        if (precompiled) {
            if (cqlLibrary.elm == null) {
                throw new RuntimeException("Package indicated CQL was precompiled, but elm xml missing.");
            }
        // TODO: need to set rulePackage.elmLibraries and mainCqlLibraryId
        } else {
            InputStream cqlStream = new ByteArrayInputStream(cqlLibrary.cql);
            VersionedIdentifier id = getIdFromCqlFile(cqlLibrary.cql);
            String fhirVersionFromFile = getFhirVersionFromCqlFile(cqlLibrary.cql);
            logger.info("CqlRule::Constructor() add id: " + id.getId() + ", fhir version: " + fhirVersionFromFile);
            if (rawCqlLibraries.containsKey(fhirVersionFromFile)) {
                // logger.info("CqlRule::Constructor() add rawCqlLibraries add: " + id.getId());
                rawCqlLibraries.get(fhirVersionFromFile).put(id, cqlStream);
            } else {
                HashMap<VersionedIdentifier, InputStream> map = new HashMap<>();
                map.put(id, cqlStream);
                // logger.info("CqlRule::Constructor() add rawCqlLibraries new: " + id.getId());
                rawCqlLibraries.put(fhirVersionFromFile, map);
            }
            if (id.getId().equals(mainCqlLibraryName)) {
                // logger.info("CqlRule::Constructor() add mainCqlLibraryId: " + id.getId());
                mainCqlLibraryId.put(fhirVersionFromFile, id);
            }
        }
    }
}
Also used : VersionedIdentifier(org.hl7.elm.r1.VersionedIdentifier) ByteArrayInputStream(java.io.ByteArrayInputStream) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList)

Example 59 with FHIRVersion

use of org.hl7.fhir.r5.model.Enumerations.FHIRVersion in project CRD by HL7-DaVinci.

the class FhirController method questionnaireForOrderOperation.

/**
 * FHIR Operation to retrieve all of the Questionnaires and CQL files associated with a given request.
 * @param fhirVersion (converted to uppercase)
 * @return FHIR Bundle
 * @throws IOException
 */
@PostMapping(path = "/fhir/{fhirVersion}/Questionnaire/$questionnaire-package", consumes = { MediaType.APPLICATION_JSON_VALUE, "application/fhir+json" })
public ResponseEntity<String> questionnaireForOrderOperation(HttpServletRequest request, HttpEntity<String> entity, @PathVariable String fhirVersion) {
    fhirVersion = fhirVersion.toUpperCase();
    String baseUrl = Utils.getApplicationBaseUrl(request).toString() + "/";
    logger.info("POST /fhir/" + fhirVersion + "/Questionnaire/$Questionnaire-package");
    String resource = null;
    if (fhirVersion.equalsIgnoreCase("R4")) {
        QuestionnairePackageOperation operation = new QuestionnairePackageOperation(fileStore, baseUrl);
        resource = operation.execute(entity.getBody());
        if (resource == null) {
            logger.warning("bad parameters");
            HttpStatus status = HttpStatus.BAD_REQUEST;
            MediaType contentType = MediaType.TEXT_PLAIN;
            return ResponseEntity.status(status).contentType(contentType).body("Bad Parameters");
        }
    } else {
        logger.warning("unsupported FHIR version: " + fhirVersion + ", not storing");
        HttpStatus status = HttpStatus.BAD_REQUEST;
        MediaType contentType = MediaType.TEXT_PLAIN;
        return ResponseEntity.status(status).contentType(contentType).body("Bad Request");
    }
    return ResponseEntity.status(HttpStatus.OK).contentType(MediaType.APPLICATION_JSON).body(resource);
}
Also used : HttpStatus(org.springframework.http.HttpStatus) MediaType(org.springframework.http.MediaType) QuestionnairePackageOperation(org.hl7.davinci.endpoint.fhir.r4.QuestionnairePackageOperation)

Example 60 with FHIRVersion

use of org.hl7.fhir.r5.model.Enumerations.FHIRVersion in project CRD by HL7-DaVinci.

the class FhirController method searchFhirResource.

/**
 * Retrieve a FHIR resource by name.
 * @param fhirVersion (converted to uppercase)
 * @param resource (converted to lowercase)
 * @param name (converted to lowercase)
 * @param url The Canonical URL of the resource.
 * @return
 * @throws IOException
 */
// ?name={TopicFormName}
@GetMapping(path = "/fhir/{fhirVersion}/{resource}")
public ResponseEntity<Resource> searchFhirResource(HttpServletRequest request, @PathVariable String fhirVersion, @PathVariable String resource, @RequestParam(required = false) String name, @RequestParam(required = false) String url, @RequestParam(required = false) String topic) throws IOException {
    fhirVersion = fhirVersion.toUpperCase();
    resource = resource.toLowerCase();
    String baseUrl = Utils.getApplicationBaseUrl(request).toString() + "/";
    FileResource fileResource = null;
    if (name != null) {
        name = name.toLowerCase();
        logger.info("GET /fhir/" + fhirVersion + "/" + resource + "?name=" + name);
        fileResource = fileStore.getFhirResourceByName(fhirVersion, resource, name, baseUrl);
    } else if (url != null) {
        logger.info("GET /fhir/" + fhirVersion + "/" + resource + "?url=" + url);
        fileResource = fileStore.getFhirResourceByUrl(fhirVersion, resource, url, baseUrl);
    } else if (topic != null) {
        topic = topic.toLowerCase();
        logger.info("GET /fhir/" + fhirVersion + "/" + resource + "?topic=" + topic);
        fileResource = fileStore.getFhirResourcesByTopicAsBundle(fhirVersion, resource, topic, baseUrl);
    }
    return processFileResource(fileResource);
}
Also used : FileResource(org.hl7.davinci.endpoint.files.FileResource)

Aggregations

ArrayList (java.util.ArrayList)10 IBaseResource (org.hl7.fhir.instance.model.api.IBaseResource)9 JsonObject (com.google.gson.JsonObject)8 Date (java.util.Date)8 IOException (java.io.IOException)7 FhirContext (ca.uhn.fhir.context.FhirContext)6 JsonArray (com.google.gson.JsonArray)6 HashMap (java.util.HashMap)5 FhirVersionEnum (ca.uhn.fhir.context.FhirVersionEnum)4 IParser (ca.uhn.fhir.parser.IParser)4 Complex (org.hl7.fhir.r4.utils.formats.Turtle.Complex)4 CommaSeparatedStringBuilder (org.hl7.fhir.utilities.CommaSeparatedStringBuilder)4 JsonPrimitive (com.google.gson.JsonPrimitive)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 Complex (org.hl7.fhir.dstu2016may.formats.RdfGenerator.Complex)3 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)3 FHIRException (org.hl7.fhir.exceptions.FHIRException)3 Bundle (org.hl7.fhir.r4.model.Bundle)3 StructureDefinition (org.hl7.fhir.r5.model.StructureDefinition)3 CascadingDeleteInterceptor (ca.uhn.fhir.jpa.interceptor.CascadingDeleteInterceptor)2