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);
}
}
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);
}
}
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.");
}
}
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;
}
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);
}
}
Aggregations