Search in sources :

Example 1 with HL7MessageModel

use of io.github.linuxforhealth.hl7.message.HL7MessageModel in project hl7v2-fhir-converter by LinuxForHealth.

the class HL7ResourceReaderTest method testGetMessageTemplatesViaClasspath.

// This tests that messagetemplates are still loaded the old way via class path
// Create a config without base.path.resource and additional.resources.location properties forcing the files to be found via classpath
@Test
void testGetMessageTemplatesViaClasspath() throws IOException {
    try {
        // Set up the config file
        File configFile = new File(folder, "config.properties");
        Properties prop = new Properties();
        prop.put("supported.hl7.messages", "ADT_A01, ORU_R01, PPR_PC1, VXU_V04");
        prop.put("default.zoneid", "+08:00");
        prop.store(new FileOutputStream(configFile), null);
        System.setProperty(CONF_PROP_HOME, configFile.getParent());
        // Get the templates
        Map<String, HL7MessageModel> messagetemplates = ResourceReader.getInstance().getMessageTemplates();
        assertThat(messagetemplates).containsKey("ORU_R01");
        assertThat(messagetemplates.containsKey("ADT_A09")).isFalse();
    } catch (IllegalArgumentException e) {
        throw new IllegalStateException("Failure to initialize the templates for the converter.", e);
    }
}
Also used : HL7MessageModel(io.github.linuxforhealth.hl7.message.HL7MessageModel) FileOutputStream(java.io.FileOutputStream) Properties(java.util.Properties) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 2 with HL7MessageModel

use of io.github.linuxforhealth.hl7.message.HL7MessageModel in project hl7v2-fhir-converter by LinuxForHealth.

the class ResourceReader method getMessageModel.

private HL7MessageModel getMessageModel(String templateName) {
    // Allow for names that already have .yml extension
    String yamlizedTemplateName = templateName.endsWith(".yml") ? templateName : templateName + ".yml";
    String templateFileContent = getResourceInHl7Folder(Constants.MESSAGE_BASE_PATH + yamlizedTemplateName);
    if (StringUtils.isNotBlank(templateFileContent)) {
        try {
            JsonNode parent = ObjectMapperUtil.getYAMLInstance().readTree(templateFileContent);
            Preconditions.checkState(parent != null, "Parent node from template file cannot be null");
            JsonNode resourceNodes = parent.get("resources");
            Preconditions.checkState(resourceNodes != null && !resourceNodes.isEmpty(), "List of resources from Parent node from template file cannot be null or empty");
            List<HL7FHIRResourceTemplateAttributes> templateAttributes = ObjectMapperUtil.getYAMLInstance().convertValue(resourceNodes, new TypeReference<List<HL7FHIRResourceTemplateAttributes>>() {
            });
            List<HL7FHIRResourceTemplate> templates = new ArrayList<>();
            templateAttributes.forEach(t -> templates.add(new HL7FHIRResourceTemplate(t)));
            Preconditions.checkState(templateAttributes != null && !templateAttributes.isEmpty(), "TemplateAttributes generated from template file cannot be null or empty");
            return new HL7MessageModel(templateName, templates);
        } catch (IOException e) {
            throw new IllegalArgumentException("Error encountered in processing the template" + templateName, e);
        }
    } else {
        throw new IllegalArgumentException("File not present:" + templateName);
    }
}
Also used : HL7MessageModel(io.github.linuxforhealth.hl7.message.HL7MessageModel) HL7FHIRResourceTemplateAttributes(io.github.linuxforhealth.hl7.message.HL7FHIRResourceTemplateAttributes) HL7FHIRResourceTemplate(io.github.linuxforhealth.hl7.message.HL7FHIRResourceTemplate) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) ArrayList(java.util.ArrayList) List(java.util.List) IOException(java.io.IOException)

Example 3 with HL7MessageModel

use of io.github.linuxforhealth.hl7.message.HL7MessageModel in project hl7v2-fhir-converter by LinuxForHealth.

the class HL7ToFHIRConverter method convertToBundle.

/**
 * Converts the input HL7 message (String data) into FHIR bundle resource.
 *
 * @param hl7MessageData Message to convert
 * @param options Options for conversion
 * @param engine Hl7Message engine
 * @return Bundle {@link Bundle} resource.
 * @throws UnsupportedOperationException - if message type is not supported
 */
public Bundle convertToBundle(String hl7MessageData, ConverterOptions options, HL7MessageEngine engine) {
    Preconditions.checkArgument(StringUtils.isNotBlank(hl7MessageData), "Input HL7 message cannot be blank");
    if (engine == null) {
        engine = getMessageEngine(options);
    }
    Message hl7message = getHl7Message(hl7MessageData);
    if (hl7message != null) {
        String messageType = HL7DataExtractor.getMessageType(hl7message);
        HL7MessageModel hl7MessageTemplateModel = messagetemplates.get(messageType);
        if (hl7MessageTemplateModel != null) {
            return hl7MessageTemplateModel.convert(hl7message, engine);
        } else {
            throw new UnsupportedOperationException("Message type not yet supported " + messageType);
        }
    } else {
        throw new IllegalArgumentException("Parsed HL7 message was null.");
    }
}
Also used : HL7MessageModel(io.github.linuxforhealth.hl7.message.HL7MessageModel) Message(ca.uhn.hl7v2.model.Message)

Example 4 with HL7MessageModel

use of io.github.linuxforhealth.hl7.message.HL7MessageModel in project hl7v2-fhir-converter by LinuxForHealth.

the class ResourceReader method getMessageTemplates.

/**
 * Returns all message templates in the configured location(s)
 * Relies on the values in config.properties.
 *
 * @return Map of messages, by message title.
 */
public Map<String, HL7MessageModel> getMessageTemplates() {
    Map<String, HL7MessageModel> messagetemplates = new HashMap<>();
    List<String> supportedMessageTemplates = ConverterConfiguration.getInstance().getSupportedMessageTemplates();
    if (hasWildcard(supportedMessageTemplates)) {
        // Code currently assumes we do no use the list of supported messages, once we see an *.
        // In future if needed to merge, it would go here.
        supportedMessageTemplates.clear();
        supportedMessageTemplates = findAllMessageTemplateNames();
    }
    for (String template : supportedMessageTemplates) {
        HL7MessageModel rm = getMessageModel(template);
        messagetemplates.put(com.google.common.io.Files.getNameWithoutExtension(template), rm);
    }
    return messagetemplates;
}
Also used : HL7MessageModel(io.github.linuxforhealth.hl7.message.HL7MessageModel) HashMap(java.util.HashMap)

Example 5 with HL7MessageModel

use of io.github.linuxforhealth.hl7.message.HL7MessageModel in project hl7v2-fhir-converter by LinuxForHealth.

the class HL7ResourceReaderTest method testGetMessageTemplatesViaAdditionalLocationWithDefaultSupportedList.

// This tests that messagetemplates are loaded the new way via configured path + alternate path
// AND that they are found when supported.hl7.messages is omitted and defaults to *
@Test
void testGetMessageTemplatesViaAdditionalLocationWithDefaultSupportedList() throws IOException {
    try {
        // Set up the config file
        File configFile = new File(folder, "config.properties");
        Properties prop = new Properties();
        prop.put("base.path.resource", "src/main/resources");
        prop.put("default.zoneid", "+08:00");
        // supported.hl7.message is omitted on purpose for this test to prove that it defaults to *
        prop.put("additional.resources.location", "src/test/resources/additional_resources");
        prop.store(new FileOutputStream(configFile), null);
        System.setProperty(CONF_PROP_HOME, configFile.getParent());
        // Get the templates ORU_R01 will be found in the base path and ADT_A09 will be found in the additional path
        Map<String, HL7MessageModel> messagetemplates = ResourceReader.getInstance().getMessageTemplates();
        // found in the base path
        assertThat(messagetemplates).containsKey("ORU_R01");
        // found in the additional path
        assertThat(messagetemplates).containsKey("ADT_A09");
    } catch (IllegalArgumentException e) {
        throw new IllegalStateException("Failure to initialize the templates for the converter.", e);
    }
}
Also used : HL7MessageModel(io.github.linuxforhealth.hl7.message.HL7MessageModel) FileOutputStream(java.io.FileOutputStream) Properties(java.util.Properties) File(java.io.File) Test(org.junit.jupiter.api.Test)

Aggregations

HL7MessageModel (io.github.linuxforhealth.hl7.message.HL7MessageModel)6 Test (org.junit.jupiter.api.Test)4 File (java.io.File)3 FileOutputStream (java.io.FileOutputStream)3 Properties (java.util.Properties)3 IOException (java.io.IOException)2 List (java.util.List)2 Message (ca.uhn.hl7v2.model.Message)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 Lists (com.google.common.collect.Lists)1 ResourceModel (io.github.linuxforhealth.api.ResourceModel)1 FHIRContext (io.github.linuxforhealth.fhir.FHIRContext)1 HL7FHIRResourceTemplate (io.github.linuxforhealth.hl7.message.HL7FHIRResourceTemplate)1 HL7FHIRResourceTemplateAttributes (io.github.linuxforhealth.hl7.message.HL7FHIRResourceTemplateAttributes)1 ResourceReader (io.github.linuxforhealth.hl7.resource.ResourceReader)1 ResourceUtils (io.github.linuxforhealth.hl7.segments.util.ResourceUtils)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Collectors (java.util.stream.Collectors)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1