From ab6f3cfb304dce8c5b631edd58394efc521acac5 Mon Sep 17 00:00:00 2001 From: ayushgarg0694 Date: Thu, 13 Jan 2022 09:13:54 -0500 Subject: [PATCH] refactor Signed-off-by: ayushgarg0694 --- .../hl7/HL7ToFHIRConverter.java | 32 +++++++------------ 1 file changed, 11 insertions(+), 21 deletions(-) diff --git a/src/main/java/io/github/linuxforhealth/hl7/HL7ToFHIRConverter.java b/src/main/java/io/github/linuxforhealth/hl7/HL7ToFHIRConverter.java index 8faf1b59..8f37d8fc 100644 --- a/src/main/java/io/github/linuxforhealth/hl7/HL7ToFHIRConverter.java +++ b/src/main/java/io/github/linuxforhealth/hl7/HL7ToFHIRConverter.java @@ -116,25 +116,10 @@ public String convert(String hl7MessageData) { * @throws UnsupportedOperationException - if message type is not supported */ public String convert(String hl7MessageData, ConverterOptions options) { - Preconditions.checkArgument(StringUtils.isNotBlank(hl7MessageData), - "Input HL7 message cannot be blank"); - Preconditions.checkArgument(options != null, "options cannot be null."); - FHIRContext context = new FHIRContext(options.isPrettyPrint(), options.isValidateResource()); - HL7MessageEngine engine = new HL7MessageEngine(context, options.getBundleType()); - Message hl7message = getHl7Message(hl7MessageData); - if (hl7message != null) { - String messageType = HL7DataExtractor.getMessageType(hl7message); - HL7MessageModel hl7MessageTemplateModel = messagetemplates.get(messageType); - if (hl7MessageTemplateModel != null) { - Bundle bundle = hl7MessageTemplateModel.convert(hl7message, engine); - return engine.getFHIRContext().encodeResourceToString(bundle); - } else { - throw new UnsupportedOperationException("Message type not yet supported " + messageType); - } - } else { - throw new IllegalArgumentException("Parsed HL7 message was null."); - } + HL7MessageEngine engine = getMessageEngine(options); + Bundle bundle = convertToBundle(hl7MessageData, options); + return engine.getFHIRContext().encodeResourceToString(bundle); } /** @@ -149,9 +134,7 @@ public String convert(String hl7MessageData, ConverterOptions options) { public Bundle convertToBundle(String hl7MessageData, ConverterOptions options) { Preconditions.checkArgument(StringUtils.isNotBlank(hl7MessageData), "Input HL7 message cannot be blank"); - Preconditions.checkArgument(options != null, "options cannot be null."); - FHIRContext context = new FHIRContext(options.isPrettyPrint(), options.isValidateResource()); - HL7MessageEngine engine = new HL7MessageEngine(context, options.getBundleType()); + HL7MessageEngine engine = getMessageEngine(options); Message hl7message = getHl7Message(hl7MessageData); if (hl7message != null) { @@ -167,6 +150,13 @@ public Bundle convertToBundle(String hl7MessageData, ConverterOptions options) { } } + private HL7MessageEngine getMessageEngine(ConverterOptions options){ + Preconditions.checkArgument(options != null, "options cannot be null."); + FHIRContext context = new FHIRContext(options.isPrettyPrint(), options.isValidateResource()); + + return new HL7MessageEngine(context, options.getBundleType()); + } + private static Message getHl7Message(String data) { Message hl7message = null; try (InputStream ins = IOUtils.toInputStream(data, StandardCharsets.UTF_8)) {