Search in sources :

Example 61 with FHIRVersion

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

the class CommonFileStore method processFhirFolder.

private void processFhirFolder(String topic, String fhirVersion, File fhirPath) {
    fhirVersion = fhirVersion.toUpperCase();
    logger.info("      CommonFileStore::processFhirFolder(): " + fhirVersion + ": " + fhirPath.getName());
    // 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());
    File[] directories = fhirPath.listFiles();
    for (File folder : directories) {
        if (folder.getName().equalsIgnoreCase("resources") && folder.isDirectory()) {
            File[] resources = folder.listFiles();
            for (File resource : resources) {
                if (resource.isFile()) {
                    String filename = resource.getName();
                    logger.info("        process: FHIR Resource: " + filename);
                    String[] parts = filename.split("-");
                    if (parts.length > 2) {
                        if (!parts[1].equalsIgnoreCase(fhirVersion)) {
                            logger.warn("CommonFileStore::processFhirFolder() warning: FhirVersion doesn't match!");
                            continue;
                        }
                        // parse the the resource file into the correct FHIR
                        IBaseResource baseResource = null;
                        try {
                            baseResource = parser.parseResource(new FileInputStream(resource));
                        } catch (FileNotFoundException e) {
                            logger.warn("could not find file: " + resource.getPath());
                            continue;
                        }
                        processFhirResource(baseResource, filename, 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 62 with FHIRVersion

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

the class CommonFileStore method getFhirResourcesByTopicAsFhirBundle.

public Bundle getFhirResourcesByTopicAsFhirBundle(String fhirVersion, String resourceType, String topic, String baseUrl) {
    List<FileResource> fileResources = getFhirResourcesByTopic(fhirVersion, resourceType, topic, baseUrl);
    Bundle bundle = new Bundle();
    if (fileResources != null && !fileResources.isEmpty()) {
        for (FileResource fileResource : fileResources) {
            if (fileResource != null) {
                Resource resource = null;
                try {
                    // convert the file resource into the fhir resource
                    resource = (Resource) parser.parseResource(fileResource.getResource().getInputStream());
                    BundleEntryComponent entry = new BundleEntryComponent().setResource(resource);
                    bundle.addEntry(entry);
                } catch (IOException ioe) {
                    logger.error("Issue parsing FHIR file resource for preprocessing.", ioe);
                    return null;
                }
            }
        }
    }
    return bundle;
}
Also used : BundleEntryComponent(org.hl7.fhir.r4.model.Bundle.BundleEntryComponent) Bundle(org.hl7.fhir.r4.model.Bundle) ByteArrayResource(org.springframework.core.io.ByteArrayResource) IBaseResource(org.hl7.fhir.instance.model.api.IBaseResource) InputStreamResource(org.springframework.core.io.InputStreamResource) Resource(org.hl7.fhir.r4.model.Resource)

Example 63 with FHIRVersion

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

the class CommonFileStore method getFhirResourcesByTopicAsBundle.

public FileResource getFhirResourcesByTopicAsBundle(String fhirVersion, String resourceType, String topic, String baseUrl) {
    Bundle bundle = getFhirResourcesByTopicAsFhirBundle(fhirVersion, resourceType, topic, baseUrl);
    if (bundle.isEmpty()) {
        return null;
    }
    byte[] resourceData = parser.encodeResourceToString(bundle).getBytes(Charset.defaultCharset());
    FileResource outputFileResource = new FileResource();
    outputFileResource.setResource(new ByteArrayResource(resourceData));
    outputFileResource.setFilename("bundle.json");
    return outputFileResource;
}
Also used : Bundle(org.hl7.fhir.r4.model.Bundle) ByteArrayResource(org.springframework.core.io.ByteArrayResource)

Example 64 with FHIRVersion

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

the class LibraryContentProcessor method processResource.

/**
 * Processes the Library to have content pointing to CQL replaced with embedded base64 encoded CQL file.
 * Only supports relative paths to files being hosted on this server.
 */
@Override
protected Library processResource(Library inputResource, FileStore fileStore, String baseUrl) {
    Library output = inputResource.copy();
    List<Attachment> content = inputResource.getContent();
    logger.info("Attempt to embed CQL (ELM) into Requested Library");
    // if the first value in content is application/elm+json with a url, replace it with base64 encoded data
    if (content.size() > 0) {
        Attachment attachment = content.get(0);
        if (attachment.hasUrl()) {
            String url = attachment.getUrl();
            // make sure this is a relative path
            if (!url.toUpperCase().startsWith("HTTP")) {
                // grab the topic, fhir version, and filename from the url
                String[] urlParts = url.split("/");
                if (urlParts.length >= 4) {
                    if ((attachment.getContentType().equalsIgnoreCase("application/elm+json")) || (attachment.getContentType().equalsIgnoreCase("text/cql"))) {
                        // content is CQL (assuming elm/json is actually CQL)
                        String topic = urlParts[1];
                        String fhirVersion = urlParts[2].toUpperCase();
                        String fileName = urlParts[3];
                        List<Attachment> attachments = new ArrayList<>();
                        // get the CQL data and base64 encode
                        FileResource cqlFileResource = fileStore.getFile(topic, fileName, fhirVersion, false);
                        attachments.add(base64EncodeToAttachment(cqlFileResource, "text/cql"));
                        // get the ELM data and base64 encode
                        FileResource elmFileResource = fileStore.getFile(topic, fileName, fhirVersion, true);
                        attachments.add(base64EncodeToAttachment(elmFileResource, "application/elm+json"));
                        // insert back into the Library
                        output.setContent(attachments);
                    } else {
                        logger.info("Content is not xml or json elm");
                    }
                } else {
                    logger.info("URL doesn't split properly: " + url);
                }
            } else {
                logger.info("URL is NOT relative");
            }
        } else {
            logger.info("Content is not a url");
        }
    } else {
        logger.info("No content in library");
    }
    return output;
}
Also used : ArrayList(java.util.ArrayList) Attachment(org.hl7.fhir.r4.model.Attachment) Library(org.hl7.fhir.r4.model.Library)

Example 65 with FHIRVersion

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

the class RuleFinder method findRules.

public List<RuleMapping> findRules(String topic, String fhirVersion) {
    logger.info("RuleFinder::findRules(" + topic + ", " + fhirVersion + ")");
    List<RuleMapping> ruleList = new ArrayList<>();
    if (ruleMappingRepository == null) {
        logger.warn("RuleFinder::findRules: the ruleMappingRepository is null");
        return ruleList;
    }
    for (RuleMapping rule : ruleMappingRepository.findRules(topic, fhirVersion)) {
        ruleList.add(rule);
    }
    if (ruleList.size() == 0) {
        logger.info("RuleFinder::findRules() returned no results for topic: " + topic + "(" + fhirVersion + ")");
    }
    return ruleList;
}
Also used : ArrayList(java.util.ArrayList) RuleMapping(org.hl7.davinci.endpoint.database.RuleMapping)

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