Search in sources :

Example 1 with FileResource

use of org.hl7.davinci.endpoint.files.FileResource in project CRD by HL7-DaVinci.

the class DataController method getFile.

/**
 * Retrieve a file from the File Store.
 * @param topic (case sensitive)
 * @param fhirVersion (converted to uppercase)
 * @param fileName (case sensitive)
 * @param noconvert
 * @return
 * @throws IOException
 */
@GetMapping(path = "/files/{topic}/{fhirVersion}/{fileName}")
public ResponseEntity<Resource> getFile(@PathVariable String topic, @PathVariable String fhirVersion, @PathVariable String fileName, @RequestParam(required = false) boolean noconvert) throws IOException {
    fhirVersion = fhirVersion.toUpperCase();
    logger.info("GET /files/" + topic + "/" + fhirVersion + "/" + fileName);
    FileResource fileResource = fileStore.getFile(topic, fileName, fhirVersion, !noconvert);
    return processFileResource(fileResource);
}
Also used : FileResource(org.hl7.davinci.endpoint.files.FileResource)

Example 2 with FileResource

use of org.hl7.davinci.endpoint.files.FileResource 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 3 with FileResource

use of org.hl7.davinci.endpoint.files.FileResource in project CRD by HL7-DaVinci.

the class LibraryContentProcessor method base64EncodeToAttachment.

private Attachment base64EncodeToAttachment(FileResource fileResource, String mimeType) {
    Attachment attachment = new Attachment();
    try {
        // base64 encode
        InputStream inputStream = fileResource.getResource().getInputStream();
        byte[] byteData = new byte[inputStream.available()];
        inputStream.read(byteData);
        String encodedData = Base64.encodeBase64String(byteData);
        attachment.setContentType(mimeType);
        Base64BinaryType b64bType = new Base64BinaryType();
        b64bType.setValueAsString(encodedData);
        attachment.setDataElement(b64bType);
    } catch (IOException e) {
        logger.warn("failed to read the data: " + mimeType);
    }
    return attachment;
}
Also used : InputStream(java.io.InputStream) Attachment(org.hl7.fhir.r4.model.Attachment) IOException(java.io.IOException) Base64BinaryType(org.hl7.fhir.r4.model.Base64BinaryType)

Example 4 with FileResource

use of org.hl7.davinci.endpoint.files.FileResource in project CRD by HL7-DaVinci.

the class QuestionnaireValueSetProcessor method findAndLoadValueSet.

/**
 * Finds a value set, loads it and modifies its id and url fields to work in the contains field.
 *
 * @param url The canonical url of the valueset to look for.
 * @param valueSetMap The map of valuesets that have been loaded already.
 * @return The local ID to use for the valueset. null if valueset wasn't found.
 */
private String findAndLoadValueSet(String url, Map<String, ValueSet> valueSetMap, FileStore fileStore, String baseUrl) {
    if (valueSetMap.containsKey(url)) {
        return valueSetMap.get(url).getId();
    }
    FileResource valueSetFileResource;
    ValueSet valueSet;
    // If URL starts with this server's base url, pull out id and search by id
    if (url.startsWith(baseUrl)) {
        String valueSetId = url.split("ValueSet/")[1];
        valueSetFileResource = fileStore.getFhirResourceById("R4", "valueset", valueSetId, baseUrl);
    } else {
        valueSetFileResource = fileStore.getFhirResourceByUrl("R4", "valueset", url, baseUrl);
    }
    if (valueSetFileResource != null) {
        // parse value set and modify ID and #URL to match.
        valueSet = (ValueSet) this.parseFhirFileResource(valueSetFileResource);
        String valueSetId = valueSet.getIdElement().getIdPart();
        valueSet.setId(valueSetId);
        valueSet.setUrl("#" + valueSetId);
        // add it to the value set map so it can be reused
        valueSetMap.put(url, valueSet);
        return valueSetId;
    } else {
        return null;
    }
}
Also used : ValueSet(org.hl7.fhir.r4.model.ValueSet)

Example 5 with FileResource

use of org.hl7.davinci.endpoint.files.FileResource in project CRD by HL7-DaVinci.

the class SubQuestionnaireProcessor method processItem.

/**
 * Determines if this item is a sub-questionnaire reference and returns the items to replace it with. Returns the same list
 * otherwise. Also recursively continues scanning if this is just a grouping item.
 *
 * @param item The item to check for sub-questionnaire referece.
 * @param fileStore The FileStore to be used for fetching sub-questionnaires.
 * @param baseUrl The base url from the server.
 * @param containedList List of contained resources to put in the assembled Questionnaire. This will be filled while iterating.
 * @param extensionList List of extensions to put in the assembled Questionnaire. This will be filled while iterating.
 * @return New list of items to replace this item with.
 */
private List<QuestionnaireItemComponent> processItem(QuestionnaireItemComponent item, FileStore fileStore, String baseUrl, Hashtable<String, org.hl7.fhir.r4.model.Resource> containedList, List<Extension> extensionList) {
    // find if item has an extension is sub-questionnaire
    Extension e = item.getExtensionByUrl("http://hl7.org/fhir/StructureDefinition/sub-questionnaire");
    if (e != null) {
        // read sub questionnaire from file store
        CanonicalType value = e.castToCanonical(e.getValue());
        logger.info("SubQuestionnaireProcessor::parseItem(): Looking for SubQuestionnaire " + value);
        // strip the type off of the id if it is there
        String id = value.asStringValue();
        String[] parts = id.split("/");
        if (parts.length > 1) {
            id = parts[1];
        }
        boolean expandRootItem = false;
        Extension expand = item.getExtensionByUrl("http://hl7.org/fhir/StructureDefinition/sub-questionnaire-expand");
        if (expand != null) {
            expandRootItem = expand.castToBoolean(expand.getValue()).booleanValue();
        }
        FileResource subFileResource = fileStore.getFhirResourceById("R4", "questionnaire", id, baseUrl, false);
        if (subFileResource != null) {
            Questionnaire subQuestionnaire = (Questionnaire) this.parseFhirFileResource(subFileResource);
            if (subQuestionnaire != null) {
                // merge extensions
                for (Extension subExtension : subQuestionnaire.getExtension()) {
                    if (extensionList.stream().noneMatch(ext -> ext.equalsDeep(subExtension))) {
                        extensionList.add(subExtension);
                    }
                }
                // merge contained resources
                for (org.hl7.fhir.r4.model.Resource r : subQuestionnaire.getContained()) {
                    containedList.put(r.getId(), r);
                }
                List<QuestionnaireItemComponent> rootItems = subQuestionnaire.getItem();
                // there are more than one root items in sub questionnaire, don't expand
                if (!expandRootItem || rootItems.size() > 1) {
                    return rootItems;
                } else {
                    return rootItems.get(0).getItem();
                }
            } else {
                // SubQuestionnaire could not be loaded
                logger.warn("SubQuestionnaireProcessor::parseItem(): Could not load SubQuestionnaire " + value.asStringValue());
                return Arrays.asList(item);
            }
        } else {
            // SubQuestionnaire could not be found
            logger.warn("SubQuestionnaireProcessor::parseItem(): Could not find SubQuestionnaire " + value.asStringValue());
            return Arrays.asList(item);
        }
    }
    // parse sub-items
    this.processItemList(item.getItem(), fileStore, baseUrl, containedList, extensionList);
    return Arrays.asList(item);
}
Also used : QuestionnaireItemComponent(org.hl7.fhir.r4.model.Questionnaire.QuestionnaireItemComponent) Questionnaire(org.hl7.fhir.r4.model.Questionnaire) CanonicalType(org.hl7.fhir.r4.model.CanonicalType) Extension(org.hl7.fhir.r4.model.Extension)

Aggregations

FileResource (org.hl7.davinci.endpoint.files.FileResource)5 IOException (java.io.IOException)2 Attachment (org.hl7.fhir.r4.model.Attachment)2 Bundle (org.hl7.fhir.r4.model.Bundle)2 Questionnaire (org.hl7.fhir.r4.model.Questionnaire)2 ByteArrayResource (org.springframework.core.io.ByteArrayResource)2 DataFormatException (ca.uhn.fhir.parser.DataFormatException)1 FileNotFoundException (java.io.FileNotFoundException)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 IBaseResource (org.hl7.fhir.instance.model.api.IBaseResource)1 Base64BinaryType (org.hl7.fhir.r4.model.Base64BinaryType)1 BundleEntryComponent (org.hl7.fhir.r4.model.Bundle.BundleEntryComponent)1 CanonicalType (org.hl7.fhir.r4.model.CanonicalType)1 Extension (org.hl7.fhir.r4.model.Extension)1 Library (org.hl7.fhir.r4.model.Library)1 QuestionnaireItemComponent (org.hl7.fhir.r4.model.Questionnaire.QuestionnaireItemComponent)1 Resource (org.hl7.fhir.r4.model.Resource)1 ValueSet (org.hl7.fhir.r4.model.ValueSet)1 InputStreamResource (org.springframework.core.io.InputStreamResource)1