Search in sources :

Example 26 with FHIRVersion

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

the class FhirController method getFhirResourceById.

/**
 * Retrieve a FHIR resource by id
 * @param fhirVersion (converted to uppercase)
 * @param resource (converted to lowercase)
 * @param id (converted to lowercase)
 * @return
 * @throws IOException
 */
@GetMapping(path = "/fhir/{fhirVersion}/{resource}/{id}")
public ResponseEntity<Resource> getFhirResourceById(HttpServletRequest request, @PathVariable String fhirVersion, @PathVariable String resource, @PathVariable String id) throws IOException {
    fhirVersion = fhirVersion.toUpperCase();
    resource = resource.toLowerCase();
    logger.info("GET /fhir/" + fhirVersion + "/" + resource + "/" + id);
    String baseUrl = Utils.getApplicationBaseUrl(request).toString() + "/";
    FileResource fileResource = fileStore.getFhirResourceById(fhirVersion, resource, id, baseUrl);
    return processFileResource(fileResource);
}
Also used : FileResource(org.hl7.davinci.endpoint.files.FileResource)

Example 27 with FHIRVersion

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

the class FhirController method submitFhirResource.

/**
 * Store a FHIR resource.
 */
@PostMapping(path = "/fhir/{fhirVersion}/{resource}", consumes = { MediaType.APPLICATION_JSON_VALUE, "application/fhir+json" })
public ResponseEntity<String> submitFhirResource(HttpServletRequest request, HttpEntity<String> entity, @PathVariable String fhirVersion, @PathVariable String resource) {
    fhirVersion = fhirVersion.toUpperCase();
    resource = resource.toLowerCase();
    logger.info("POST /fhir/" + fhirVersion + "/" + resource);
    FhirResourceInfo fhirResourceInfo = new FhirResourceInfo();
    // pull out the ID and name
    if (fhirVersion.equalsIgnoreCase("R4")) {
        fhirResourceInfo = org.hl7.davinci.r4.Utilities.getFhirResourceInfo(entity.getBody());
    } 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");
    }
    if (!fhirResourceInfo.valid()) {
        logger.warning("submitFhirResource: unsupported FHIR Resource of type: " + fhirResourceInfo.getType() + " (" + fhirVersion + "), not storing");
        HttpStatus status = HttpStatus.BAD_REQUEST;
        MediaType contentType = MediaType.TEXT_PLAIN;
        return ResponseEntity.status(status).contentType(contentType).body("Bad Request");
    }
    // build the FhirResource and save to the database
    FhirResource fhirResource = new FhirResource();
    fhirResource.setFhirVersion(fhirVersion).setResourceType(fhirResourceInfo.getType()).setData((entity.getBody()));
    if (fhirResourceInfo.getId() != null) {
        fhirResource.setId(fhirResourceInfo.getId());
    }
    if (fhirResourceInfo.getName() != null) {
        fhirResource.setName(fhirResourceInfo.getName());
    }
    FhirResource newFhirResource = fhirResourceRepository.save(fhirResource);
    String newId = newFhirResource.getId();
    String statusMsg = "successfully stored " + fhirResourceInfo.getType() + ": " + newId;
    logger.info(statusMsg);
    // build the response
    HttpStatus status = HttpStatus.CREATED;
    MediaType contentType = MediaType.TEXT_PLAIN;
    String baseUrl = Utils.getApplicationBaseUrl(request).toString() + "/";
    return ResponseEntity.status(status).contentType(contentType).header(HttpHeaders.LOCATION, baseUrl + "/fhir/" + fhirVersion + "/" + resource + "?identifier=" + newId).body(statusMsg);
}
Also used : FhirResource(org.hl7.davinci.endpoint.database.FhirResource) FhirResourceInfo(org.hl7.davinci.FhirResourceInfo) HttpStatus(org.springframework.http.HttpStatus) MediaType(org.springframework.http.MediaType)

Example 28 with FHIRVersion

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

the class CdsConnectFileStore method processFhirFiles.

private void processFhirFiles(List<CdsConnectFile> files, String topic) {
    // process the fhir resource files
    // setup the proper FHIR Context for the version of FHIR we are dealing with
    FhirContext r4ctx = new org.hl7.davinci.r4.FhirComponents().getFhirContext();
    IParser r4parser = r4ctx.newJsonParser();
    // suppress the unknown element warnings
    r4parser.setParserErrorHandler(new SuppressParserErrorHandler());
    // process all of the files found within the topic/artifact
    for (CdsConnectFile file : files) {
        String path = file.getPath();
        String filename = file.getFilename();
        if (filename.endsWith(".json")) {
            logger.info("        process: FHIR Resource: " + filename);
            String[] parts = filename.split("-");
            if (parts.length > 2) {
                // String resourceType = parts[0];
                String fhirVersion = parts[1];
                String name = parts[2];
                IBaseResource baseResource = null;
                byte[] fileContents = file.getCqlBundle();
                if (fhirVersion.equalsIgnoreCase("R4")) {
                    baseResource = r4parser.parseResource(new ByteArrayInputStream(fileContents));
                }
                processFhirResource(baseResource, path, filename, fhirVersion, topic);
            }
        }
    }
}
Also used : FhirContext(ca.uhn.fhir.context.FhirContext) SuppressParserErrorHandler(org.hl7.davinci.SuppressParserErrorHandler) IBaseResource(org.hl7.fhir.instance.model.api.IBaseResource) IParser(ca.uhn.fhir.parser.IParser)

Example 29 with FHIRVersion

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

the class GitHubFileStore method processFhirFolder.

private void processFhirFolder(String topic, String fhirVersion, String fhirPath) {
    fhirVersion = fhirVersion.toUpperCase();
    logger.info("      GitHubFileStore::processFhirFolder(): " + fhirVersion + ": " + fhirPath);
    // setup the proper FHIR Context for the version of FHIR we are dealing with
    FhirContext ctx = null;
    if (fhirVersion.equalsIgnoreCase("R4")) {
        ctx = new org.hl7.davinci.r4.FhirComponents().getFhirContext();
    } else {
        logger.warn("unsupported FHIR version: " + fhirVersion + ", skipping folder");
        return;
    }
    IParser parser = ctx.newJsonParser();
    // suppress the unknown element warnings
    parser.setParserErrorHandler(new SuppressParserErrorHandler());
    for (String folder : connection.getDirectory(fhirPath)) {
        if (folder.equalsIgnoreCase("resources")) {
            String fullFolderPath = fhirPath + "/" + folder;
            for (String resource : connection.getDirectory(fullFolderPath)) {
                String filename = resource;
                String fullFilePath = fullFolderPath + "/" + filename;
                logger.info("        process: FHIR Resource: " + filename);
                String[] parts = filename.split("-");
                if (parts.length > 2) {
                    // = parts[0];
                    String resourceType;
                    if (!parts[1].equalsIgnoreCase(fhirVersion)) {
                        logger.warn("GitHubFileStore::processFhirFolder() warning: FhirVersion doesn't match!");
                        continue;
                    }
                    InputStream inputStream = connection.getFile(fullFilePath);
                    if (inputStream != null) {
                        IBaseResource baseResource = parser.parseResource(inputStream);
                        processFhirResource(baseResource, filename, filename, fhirVersion, topic);
                    } else {
                        logger.warn("could not find file: " + fullFilePath);
                        continue;
                    }
                }
            }
        }
    }
}
Also used : FhirContext(ca.uhn.fhir.context.FhirContext) SuppressParserErrorHandler(org.hl7.davinci.SuppressParserErrorHandler) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IBaseResource(org.hl7.fhir.instance.model.api.IBaseResource) IParser(ca.uhn.fhir.parser.IParser)

Example 30 with FHIRVersion

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

the class CqlRule method getFhirVersionFromCqlFile.

private static String getFhirVersionFromCqlFile(byte[] cql) {
    String fhirVersion = "";
    UsingDef usingDef = new UsingDef();
    Pattern pattern = Pattern.compile("using (.*?) version '(.*?)'");
    try {
        Matcher matcher = pattern.matcher(new String(cql));
        while (fhirVersion.isEmpty()) {
            matcher.find();
            if (matcher.groupCount() != 2) {
                throw new RuntimeException("Encountered bad CQL file, could not detect library identifier.");
            }
            if (matcher.group(1).equalsIgnoreCase("FHIR")) {
                fhirVersion = matcher.group(2);
            }
        }
    } catch (Exception e) {
        logger.warn("exception in CqlRule::getFhirVersionFromCqlFile(): " + e.getMessage());
    }
    return fhirVersion;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) UsingDef(org.hl7.elm.r1.UsingDef) IOException(java.io.IOException)

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