Repository: nifi
Updated Branches:
  refs/heads/master 91cdd78eb -> d4395a8a0


Fixes NIFI-1968 so ExtractHL7Attributes keeps empty components

Signed-off-by: Matt Burgess <mattyb...@apache.org>


Project: http://git-wip-us.apache.org/repos/asf/nifi/repo
Commit: http://git-wip-us.apache.org/repos/asf/nifi/commit/d4395a8a
Tree: http://git-wip-us.apache.org/repos/asf/nifi/tree/d4395a8a
Diff: http://git-wip-us.apache.org/repos/asf/nifi/diff/d4395a8a

Branch: refs/heads/master
Commit: d4395a8a0820e849b27772d330d0bcffbeb8beea
Parents: 91cdd78
Author: Joey Frazee <joey.fra...@icloud.com>
Authored: Sat Jun 4 14:51:18 2016 -0500
Committer: Matt Burgess <mattyb...@apache.org>
Committed: Mon Jun 6 21:37:53 2016 -0400

----------------------------------------------------------------------
 .../processors/hl7/ExtractHL7Attributes.java    | 59 ++++----------------
 .../hl7/TestExtractHL7Attributes.java           | 43 ++++++++++++++
 .../src/test/resources/hypoglycemia.hl7         |  4 +-
 3 files changed, 55 insertions(+), 51 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/nifi/blob/d4395a8a/nifi-nar-bundles/nifi-hl7-bundle/nifi-hl7-processors/src/main/java/org/apache/nifi/processors/hl7/ExtractHL7Attributes.java
----------------------------------------------------------------------
diff --git 
a/nifi-nar-bundles/nifi-hl7-bundle/nifi-hl7-processors/src/main/java/org/apache/nifi/processors/hl7/ExtractHL7Attributes.java
 
b/nifi-nar-bundles/nifi-hl7-bundle/nifi-hl7-processors/src/main/java/org/apache/nifi/processors/hl7/ExtractHL7Attributes.java
index e49a8be..6c35637 100644
--- 
a/nifi-nar-bundles/nifi-hl7-bundle/nifi-hl7-processors/src/main/java/org/apache/nifi/processors/hl7/ExtractHL7Attributes.java
+++ 
b/nifi-nar-bundles/nifi-hl7-bundle/nifi-hl7-processors/src/main/java/org/apache/nifi/processors/hl7/ExtractHL7Attributes.java
@@ -46,14 +46,14 @@ import org.apache.nifi.stream.io.StreamUtils;
 import ca.uhn.hl7v2.DefaultHapiContext;
 import ca.uhn.hl7v2.HL7Exception;
 import ca.uhn.hl7v2.HapiContext;
-import ca.uhn.hl7v2.model.Composite;
 import ca.uhn.hl7v2.model.Group;
 import ca.uhn.hl7v2.model.Message;
-import ca.uhn.hl7v2.model.Primitive;
 import ca.uhn.hl7v2.model.Segment;
 import ca.uhn.hl7v2.model.Structure;
 import ca.uhn.hl7v2.model.Type;
-import ca.uhn.hl7v2.model.Varies;
+import ca.uhn.hl7v2.parser.DefaultEscaping;
+import ca.uhn.hl7v2.parser.EncodingCharacters;
+import ca.uhn.hl7v2.parser.Escaping;
 import ca.uhn.hl7v2.parser.PipeParser;
 import ca.uhn.hl7v2.validation.ValidationContext;
 import ca.uhn.hl7v2.validation.impl.ValidationContextFactory;
@@ -189,60 +189,21 @@ public class ExtractHL7Attributes extends 
AbstractProcessor {
     private Map<String, String> getAttributes(final Segment segment, final 
Integer segmentNum) throws HL7Exception {
         final Map<String, String> attributes = new HashMap<>();
 
+        final EncodingCharacters encoding = 
EncodingCharacters.defaultInstance();
+        final Escaping escaping = new DefaultEscaping();
         for (int i = 1; i <= segment.numFields(); i++) {
             final String fieldName = segment.getName() + (segmentNum == null ? 
"" : "_" + segmentNum) + "." + i;
             final Type[] types = segment.getField(i);
-            final StringBuilder sb = new StringBuilder();
             for (final Type type : types) {
-                final String typeValue = getValue(type);
-                if (!typeValue.isEmpty()) {
-                    sb.append(typeValue).append("^");
-                }
-            }
-
-            if (sb.length() == 0) {
-                continue;
-            }
-            String typeVal = sb.toString();
-            if (typeVal.endsWith("^")) {
-                typeVal = typeVal.substring(0, typeVal.length() - 1);
+                // This maybe should used the escaped values, but that would
+                // change the existing non-broken behavior of the processor
+                final String escapedTypeValue = type.encode();
+                final String unescapedTypeValue = 
escaping.unescape(escapedTypeValue, encoding);
+                attributes.put(fieldName, unescapedTypeValue);
             }
-
-            attributes.put(fieldName, typeVal);
         }
 
         return attributes;
     }
 
-    private String getValue(final Type type) {
-        if (type == null) {
-            return "";
-        }
-
-        if (type instanceof Primitive) {
-            final String value = ((Primitive) type).getValue();
-            return value == null ? "" : value;
-        } else if (type instanceof Composite) {
-            final StringBuilder sb = new StringBuilder();
-            final Composite composite = (Composite) type;
-            for (final Type component : composite.getComponents()) {
-                final String componentValue = getValue(component);
-                if (!componentValue.isEmpty()) {
-                    sb.append(componentValue).append("^");
-                }
-            }
-
-            final String value = sb.toString();
-            if (value.endsWith("^")) {
-                return value.substring(0, value.length() - 1);
-            }
-
-            return value;
-        } else if (type instanceof Varies) {
-            final Varies varies = (Varies) type;
-            return getValue(varies.getData());
-        }
-
-        return "";
-    }
 }

http://git-wip-us.apache.org/repos/asf/nifi/blob/d4395a8a/nifi-nar-bundles/nifi-hl7-bundle/nifi-hl7-processors/src/test/java/org/apache/nifi/processors/hl7/TestExtractHL7Attributes.java
----------------------------------------------------------------------
diff --git 
a/nifi-nar-bundles/nifi-hl7-bundle/nifi-hl7-processors/src/test/java/org/apache/nifi/processors/hl7/TestExtractHL7Attributes.java
 
b/nifi-nar-bundles/nifi-hl7-bundle/nifi-hl7-processors/src/test/java/org/apache/nifi/processors/hl7/TestExtractHL7Attributes.java
index d91a633..563219d 100644
--- 
a/nifi-nar-bundles/nifi-hl7-bundle/nifi-hl7-processors/src/test/java/org/apache/nifi/processors/hl7/TestExtractHL7Attributes.java
+++ 
b/nifi-nar-bundles/nifi-hl7-bundle/nifi-hl7-processors/src/test/java/org/apache/nifi/processors/hl7/TestExtractHL7Attributes.java
@@ -38,6 +38,42 @@ public class TestExtractHL7Attributes {
 
     @Test
     public void testExtract() throws IOException {
+        final SortedMap<String, String> expectedAttributes = new TreeMap<>();
+        // MSH.1 and MSH.2 could be escaped, but it's not clear which is right
+        expectedAttributes.put("MSH.1", "|");
+        expectedAttributes.put("MSH.2", "^~\\&");
+        expectedAttributes.put("MSH.3", "XXXXXXXX");
+        expectedAttributes.put("MSH.5", "HealthProvider");
+        expectedAttributes.put("MSH.9", "ORU^R01");
+        expectedAttributes.put("MSH.10", "Q1111111111111111111");
+        expectedAttributes.put("MSH.11", "P");
+        expectedAttributes.put("MSH.12", "2.3");
+        expectedAttributes.put("OBR_1.1", "1");
+        expectedAttributes.put("OBR_1.2", "341856649^HNAM_ORDERID");
+        expectedAttributes.put("OBR_1.3", "000000000000000000");
+        expectedAttributes.put("OBR_1.4", "648088^Basic Metabolic Panel");
+        expectedAttributes.put("OBR_1.7", "20150101000000");
+        expectedAttributes.put("OBR_1.16", "1620^Johnson^Corey^A");
+        expectedAttributes.put("OBR_1.22", "20150101000000");
+        expectedAttributes.put("OBR_1.25", "F");
+        expectedAttributes.put("OBR_1.36", "20150101000000");
+        expectedAttributes.put("OBX_1.1", "1");
+        expectedAttributes.put("OBX_1.2", "NM");
+        expectedAttributes.put("OBX_1.3", "GLU^Glucose Lvl");
+        expectedAttributes.put("OBX_1.4", "59");
+        expectedAttributes.put("OBX_1.5", "mg/dL");
+        expectedAttributes.put("OBX_1.6", "65-99^65^99");
+        expectedAttributes.put("OBX_1.7", "L");
+        expectedAttributes.put("OBX_1.10", "F");
+        expectedAttributes.put("OBX_1.13", "20150102000000");
+        expectedAttributes.put("PD1.4", "1234567890^LAST^FIRST^M^^^^^NPI");
+        expectedAttributes.put("PID.3", "12345^^^XYZ^MR");
+        expectedAttributes.put("PID.5", "SMITH^JOHN");
+        expectedAttributes.put("PID.7", "19700100");
+        expectedAttributes.put("PID.8", "M");
+        expectedAttributes.put("PID.18", "111111111111");
+        expectedAttributes.put("PID.19", "123456789");
+
         final TestRunner runner = 
TestRunners.newTestRunner(ExtractHL7Attributes.class);
         runner.enqueue(Paths.get("src/test/resources/hypoglycemia.hl7"));
 
@@ -47,6 +83,13 @@ public class TestExtractHL7Attributes {
         final MockFlowFile out = 
runner.getFlowFilesForRelationship(ExtractHL7Attributes.REL_SUCCESS).get(0);
         final SortedMap<String, String> sortedAttrs = new 
TreeMap<>(out.getAttributes());
 
+        for (final Map.Entry<String, String> entry : 
expectedAttributes.entrySet()) {
+            final String key = entry.getKey();
+            final String expected = entry.getValue();
+            final String actual = sortedAttrs.get(key);
+            Assert.assertEquals(key + " segment values do not match", 
expected, actual);
+        }
+
         int mshSegmentCount = 0;
         int obrSegmentCount = 0;
         int obxSegmentCount = 0;

http://git-wip-us.apache.org/repos/asf/nifi/blob/d4395a8a/nifi-nar-bundles/nifi-hl7-bundle/nifi-hl7-processors/src/test/resources/hypoglycemia.hl7
----------------------------------------------------------------------
diff --git 
a/nifi-nar-bundles/nifi-hl7-bundle/nifi-hl7-processors/src/test/resources/hypoglycemia.hl7
 
b/nifi-nar-bundles/nifi-hl7-bundle/nifi-hl7-processors/src/test/resources/hypoglycemia.hl7
index aaff9fc..8716aba 100644
--- 
a/nifi-nar-bundles/nifi-hl7-bundle/nifi-hl7-processors/src/test/resources/hypoglycemia.hl7
+++ 
b/nifi-nar-bundles/nifi-hl7-bundle/nifi-hl7-processors/src/test/resources/hypoglycemia.hl7
@@ -1,5 +1,5 @@
 MSH|^~\&|XXXXXXXX||HealthProvider||||ORU^R01|Q1111111111111111111|P|2.3|
-PID|||111111111||SMITH^JOHN||19700100|M||||||||||111111111111|123456789|
+PID|||12345^^^XYZ^MR||SMITH^JOHN||19700100|M||||||||||111111111111|123456789|
 PD1||||1234567890^LAST^FIRST^M^^^^^NPI|
 OBR|1|341856649^HNAM_ORDERID|000000000000000000|648088^Basic Metabolic 
Panel|||20150101000000|||||||||1620^Johnson^Corey^A||||||20150101000000|||F|||||||||||20150101000000|
-OBX|1|NM|GLU^Glucose Lvl|59|mg/dL|65-99^65^99|L|||F|||20150102000000|
\ No newline at end of file
+OBX|1|NM|GLU^Glucose Lvl|59|mg/dL|65-99^65^99|L|||F|||20150102000000|

Reply via email to