The dropping of repeats has been causing some trouble for us in production, so 
we have investigated further. Within the pipe parser a split method was found 
which we believe is the culprit. I pulled it out, created some tests, and have 
included a proposed replacement split method. Another alternative using commons 
lang was also attempted and included, but it also fails.

See below for code containing splitOriginal, split and splitUseCommons. A set 
of test cases are also included.

Hope this helps

Ian

---code---

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package hapiexamples;

import java.util.ArrayList;
import java.util.StringTokenizer;
import org.apache.commons.lang.StringUtils;

/**
*
* @author vowlesi
*/
public class ReplaceHapiSplitToCorrectLostTilde {

    /**
     * Splits the given composite string into an array of components using the
     * given delimiter.
     *
     * @param composite encoded composite string
     * @param delim delimiter to split upon
     * @return split string
     */
    public static String[] splitOriginal(String composite, String delim) {
        ArrayList<String> components = new ArrayList<String>();

        // defend against evil nulls
        if (composite == null) {
            composite = "";
        }
        if (delim == null) {
            delim = "";
        }

        StringTokenizer tok = new StringTokenizer(composite, delim, true);
        boolean previousTokenWasDelim = true;
        while (tok.hasMoreTokens()) {
            String thisTok = tok.nextToken();
            if (thisTok.equals(delim)) {
                if (previousTokenWasDelim) {
                    components.add(null);
                }
                previousTokenWasDelim = true;
            } else {
                components.add(thisTok);
                previousTokenWasDelim = false;
            }
        }

        String[] ret = new String[components.size()];
        for (int i = 0; i < components.size(); i++) {
            ret[i] = components.get(i);
        }

        return ret;
    }

    /**
     * Splits the given composite string into an array of components using the
     * given delimiter.
     *
     * @param composite encoded composite string
     * @param delim delimiter to split upon
     * @return split string
     */
    public static String[] split(String composite, String delim) {
        // defend against evil nulls
        String escapeTheseForSplit = "\\^|.$()*+?[{";

        if (composite == null) {
            composite = "";
        }
        if (delim == null) {
            String[] ret = new String[]{composite};
            return ret;
        }

        if (escapeTheseForSplit.contains(delim)) {
            delim = "\\" + delim;
        }
        String[] ret = composite.split(delim, -1);

        return ret;
    }

    /**
     * Splits the given composite string into an array of components using the
     * given delimiter.
     *
     * @param composite encoded composite string
     * @param delim delimiter to split upon
     * @return split string
     */
    public static String[] splitUseCommons(String composite, String delim) {
        // defend against evil nulls
        String[] ret = StringUtils.split(composite, delim);

        return ret;
    }
}

--- tests ---

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package hapiexamples;

import static hapiexamples.ReplaceHapiSplitToCorrectLostTilde.split;
import static hapiexamples.ReplaceHapiSplitToCorrectLostTilde.splitOriginal;
import static hapiexamples.ReplaceHapiSplitToCorrectLostTilde.splitUseCommons;
import org.junit.Test;
import static org.junit.Assert.*;

/**
*
* @author vowlesi
*/
public class ReplaceHapiSplitToCorrectLostTildeTest {

    public ReplaceHapiSplitToCorrectLostTildeTest() {
    }

    /**
     * Test of splitOriginal method, of class
     * ReplaceHapiSplitToCorrectLostTilde.
     */
    @Test
    public void testSplitOriginal() {
        System.out.println("splitOriginal");
        String composite = "Example with trailing repeat delimiters~~~";
        String delim = "~";
        String[] result = splitOriginal(composite, delim);
        assertEquals("With 3 trailing delimiters the array result should have 4 
elements", 4, result.length);
    }

    /**
     * Test of proposed replacement split method
     * All possible delimiter values within the printable single character 
range for HL7 are tested
     */
    @Test
    public void testSplit() {
        System.out.println("split");

        for (byte i = 32; i < 127; i++) {
            String delim = new String(new byte[] {i});
            String composite = "ZZZZ" + delim + delim + delim;
            if ("Z".equals(delim)) {
                composite = "XXXX" + delim + delim + delim;
            }
            System.out.println("Counter at " + String.valueOf(i) + " Delimiter 
is '" + delim + "'");
            String[] result = split(composite, delim);
            for (int j = 0; j < result.length; j++) {
                System.out.println(j + " " + result[j]);
            }
            assertEquals("With 3 trailing delimiters the array result should 
have 4 elements", 4, result.length);
        }
    }

    /**
     * Test of Alternative split using Apache Commons Split version of method
     * All possible delimiter values within the printable single character 
range for HL7 are tested
    */
    @Test
    public void testSplitCommonsSplit() {
        System.out.println("split");

        for (byte i = 32; i < 126; i++) {
            String delim = new String(new byte[] {i});
            String composite = "ZZZZ" + delim + delim + delim;
            if ("Z".equals(delim)) {
                composite = "XXXX" + delim + delim + delim;
            }
            System.out.println("Delimiter is '" + delim + "'");
            String[] result = splitUseCommons(composite, delim);
            for (int j = 0; j < result.length; j++) {
                System.out.println(j + " " + result[j]);
            }
            assertEquals("With 3 trailing delimiters the array result should 
have 4 elements", 4, result.length);
        }
    }

    /**
     * Test of proposed replacement split method, of class
     * ReplaceHapiSplitToCorrectLostTilde.
     */
    @Test
    public void testSplitWithNullsPassed() {
        System.out.println("split");
        String composite = null;
        String delim = null;
        String[] result = split(composite, delim);
        assertEquals("Passing all nulls makes an empty list", 1, result.length);
        assertEquals("The one element array has an empty string content", "", 
result[0]);
    }

    /**
     * Test of proposed replacement split method, of class
     * ReplaceHapiSplitToCorrectLostTilde.
     */
    @Test
    public void testSplitWithNullPassedAsDelimiter() {
        System.out.println("split");
        String composite = "Some content";
        String delim = null;
        String[] result = split(composite, delim);
        assertEquals("Passing null delimiter returns a one element array 
containing the content", 1, result.length);
        assertEquals("The one element array has the original string as 
content", composite, result[0]);
    }

    /**
     * Test of proposed replacement split method
     * All possible delimiter values within the printable single character 
range for HL7 are tested
     */
    @Test
    public void testSplitWithNullPassedAsCompositeAndAnyDelimiter() {
        String composite = null;
        for (byte i = 32; i < 126; i++) {
            String delim = new String(new byte[] {i});
            System.out.println("Delimiter is '" + delim + "'");
            String[] result = split(composite, delim);
            for (int j = 0; j < result.length; j++) {
                System.out.println(j + " '" + result[j] + "'");
            }
            assertEquals("Passing null content produces a one element array 
containing an empty string", 1, result.length);
            assertEquals("The one element array has an empty string content", 
"", result[0]);
        }
    }
}

From: Ian Vowles [mailto:ian.vow...@health.qld.gov.au]
Sent: Tuesday, 8 December 2015 7:20 AM
To: hl7api-devel@lists.sourceforge.net
Subject: Re: [HAPI-devel] Parser dropping 1 empty repeat on an OBX-5 each time 
you parse

Thanks for your reply James.

I ran the test you provided, and it passed as you expected.

Couple of things.

The OBX-5 provided in the example has 9 repeats.  You only had code to test 8. 
If you add the line:

       assertEquals("", obx.getOBX().getObx5_ObservationValue()[8].encode());

Then the test fails.

Alternatively the assertions could be reduced to:

       assertEquals("Count of OBX-5 repeat is 9", 9, 
obx.getOBX().getObx5_ObservationValueReps());

Which also fails.

To check if this might be something particular to OBX-5 entries, given their 
variable nature I extended the test to include a check on another repeating 
field by adding 3 repeat characters to the end of the PID-3 list (making 4 
repeats)

               + "PID|1||10011682^^^CL_MRN 
Pool^^CD:237968946~~~|60683166^^^DONOTSEND^^CD:237968946|CLABTEST^ONE||19500101120000|F||C|34605
 W 12 MILE RD^^FARM 
MILLS^MN^48331-3263^CD:309221^CD:756^^CD:637908|CD:637908|(248)489-6000^CD:170||CD:151|M|CD:3391461|606831661088^^^DONOTSEND^FIN
 NBR^CD:237968946||||CD:654655774|||0|||CD:654897458\r"

and added another assertion:

       assertEquals("Count of PID-3 repeat is 4", 4, ((ORU_R01) 
hapiMsg).getPATIENT_RESULT(0).getPATIENT().getPID().getPid3_PatientIdentifierListReps());

This also fails.

Any thoughts on what is going on?

Thanks
Ian

From: James Agnew [mailto:jamesag...@gmail.com]
Sent: Monday, 7 December 2015 11:13 PM
To: Ian Vowles
Cc: hl7api-devel@lists.sourceforge.net
Subject: Re: [HAPI-devel] Parser dropping 1 empty repeat on an OBX-5 each time 
you parse

Hi Ian,
I turned this into a unit test, and it seems to give me the expected results:

       String msg = 
"MSH|^~\\&|HNAM|CL|CL_RADNET|CL|20110628095233||ORU^R01|Q2301030099T1904270849|P|2.4\r"
               + "PID|1||10011682^^^CL_MRN 
Pool^^CD:237968946|60683166^^^DONOTSEND^^CD:237968946|CLABTEST^ONE||19500101120000|F||C|34605
 W 12 MILE RD^^FARM 
MILLS^MN^48331-3263^CD:309221^CD:756^^CD:637908|CD:637908|(248)489-6000^CD:170||CD:151|M|CD:3391461|606831661088^^^DONOTSEND^FIN
 NBR^CD:237968946||||CD:654655774|||0|||CD:654897458\r"
               + 
"PV1|1|Inpatient|MS^0414^1^CL^^^MMCCN|3|||5332^Rahiman^Abdul|||MED|MS|||1|||5332^Rahiman^Doctor|A||60|||||||||||||||||||CL||Active|||20110329113000\r"
               + 
"ORC|NW|3415770735^HNAM_ORDERID|||RL||||20110628095142|71573^Doctor^Janet^D^^^^^DONOTSEND^^^^COMMUNITY
 DR NBR~70988^Doctor^Janet^D^^^^^DONOTSEND^^^^Personnel Primary 
Identifier~FHJDB2293^Doctor^Janet^D^^^^^DONOTSEND^^^^External 
Identifier~T^Doctor^Janet^D^^^^^DONOTSEND^^^^CD:296695676|||||20110628095231|||CD:2562^Written|71573^Doctor^Janet^D^^^^^DONOTSEND^^^^COMMUNITY
 DR NBR~70988^Doctor^Janet^D^^^^^DONOTSEND^^^^Personnel Primary 
Identifier~FHJDB2293^Doctor^Janet^D^^^^^DONOTSEND^^^^External 
Identifier~T^Doctor^Janet^D^^^^^DONOTSEND^^^^CD:296695676\r"
               + "OBR|1|3415770735^HNAM_ORDERID||CL987^MRA Head w and w/o 
Contrast|||||||||||Rad Type&Rad 
Type|||||00000MR20110001703^HNA_ACCN~8546871^HNA_ACCNID~7515376^HNA_PACSID|CD:232990825|20110628095231||Magnetic
 Resonance Imaging|||1^^0^20110628095100^^RT||||^Dizziness\r"
               + "OBX|1|TX|20725^ROUTINE 
HEMATOLOGY|H7800-4|This~Is~A~Report~~~~~||||||I|||200704021122\r"
               + "NTE|1|P|Special Instructions-\r";

       Parser p = new GenericParser();

       Message hapiMsg;

       // The parse method performs the actual parsing

       hapiMsg = p.parse(msg);

       ORU_R01_OBSERVATION obx = ((ORU_R01) 
hapiMsg).getPATIENT_RESULT(0).getORDER_OBSERVATION(0).getOBSERVATION(0);
       assertEquals("This", 
obx.getOBX().getObx5_ObservationValue()[0].encode());
       assertEquals("Is", obx.getOBX().getObx5_ObservationValue()[1].encode());
       assertEquals("A", obx.getOBX().getObx5_ObservationValue()[2].encode());
       assertEquals("Report", 
obx.getOBX().getObx5_ObservationValue()[3].encode());
       assertEquals("", obx.getOBX().getObx5_ObservationValue()[4].encode());
       assertEquals("", obx.getOBX().getObx5_ObservationValue()[5].encode());
       assertEquals("", obx.getOBX().getObx5_ObservationValue()[6].encode());
       assertEquals("", obx.getOBX().getObx5_ObservationValue()[7].encode());
Cheers,
James

On Mon, Nov 30, 2015 at 10:59 PM, Ian Vowles 
<ian.vow...@health.qld.gov.au<mailto:ian.vow...@health.qld.gov.au>> wrote:
We have been having some trouble with textual OBX-5’s which have repeats used 
to indicate each line of a report when some empty repeats exist to denote empty 
lines for formatting.
Is there a way to overcome this?
The following example demonstrates the problem

Thanks
Ian

package hapiexamples;

import ca.uhn.hl7v2.HL7Exception;
import ca.uhn.hl7v2.app.ApplicationException;
import ca.uhn.hl7v2.model.Message;
import ca.uhn.hl7v2.model.v24.message.ORU_R01;
import ca.uhn.hl7v2.parser.GenericParser;
import ca.uhn.hl7v2.parser.Parser;
import java.io.IOException;

/**
*
* @author VowlesI
*/
public class OruR01RepeatsInObx5 {

    public static void main(String[] args) throws HL7Exception, 
ApplicationException, IOException {
        String msg = 
"MSH|^~\\&|HNAM|CL|CL_RADNET|CL|20110628095233||ORU^R01|Q2301030099T1904270849|P|2.4\r"
                + "PID|1||10011682^^^CL_MRN 
Pool^^CD:237968946|60683166^^^DONOTSEND^^CD:237968946|CLABTEST^ONE||19500101120000|F||C|34605
 W 12 MILE RD^^FARM 
MILLS^MN^48331-3263^CD:309221^CD:756^^CD:637908|CD:637908|(248)489-6000^CD:170||CD:151|M|CD:3391461|606831661088^^^DONOTSEND^FIN
 NBR^CD:237968946||||CD:654655774|||0|||CD:654897458\r"
                + 
"PV1|1|Inpatient|MS^0414^1^CL^^^MMCCN|3|||5332^Rahiman^Abdul|||MED|MS|||1|||5332^Rahiman^Doctor|A||60|||||||||||||||||||CL||Active|||20110329113000\r"
                + 
"ORC|NW|3415770735^HNAM_ORDERID|||RL||||20110628095142|71573^Doctor^Janet^D^^^^^DONOTSEND^^^^COMMUNITY
 DR NBR~70988^Doctor^Janet^D^^^^^DONOTSEND^^^^Personnel Primary 
Identifier~FHJDB2293^Doctor^Janet^D^^^^^DONOTSEND^^^^External 
Identifier~T^Doctor^Janet^D^^^^^DONOTSEND^^^^CD:296695676|||||20110628095231|||CD:2562^Written|71573^Doctor^Janet^D^^^^^DONOTSEND^^^^COMMUNITY
 DR NBR~70988^Doctor^Janet^D^^^^^DONOTSEND^^^^Personnel Primary 
Identifier~FHJDB2293^Doctor^Janet^D^^^^^DONOTSEND^^^^External 
Identifier~T^Doctor^Janet^D^^^^^DONOTSEND^^^^CD:296695676\r"
                + "OBR|1|3415770735^HNAM_ORDERID||CL987^MRA Head w and w/o 
Contrast|||||||||||Rad Type&Rad 
Type|||||00000MR20110001703^HNA_ACCN~8546871^HNA_ACCNID~7515376^HNA_PACSID|CD:232990825|20110628095231||Magnetic
 Resonance Imaging|||1^^0^20110628095100^^RT||||^Dizziness\r"
                + "OBX|1|TX|20725^ROUTINE 
HEMATOLOGY|H7800-4|This~Is~A~Report~~~~~||||||I|||200704021122\r"
                + "NTE|1|P|Special Instructions-\r";

        Parser p = new GenericParser();

        Message hapiMsg;
        // The parse method performs the actual parsing
        hapiMsg = p.parse(msg);
        System.out.println("OBX-5 has " + ((ORU_R01) 
hapiMsg).getPATIENT_RESULT(0).getORDER_OBSERVATION(0).getOBSERVATION(0).getOBX().getObx5_ObservationValueReps());
        hapiMsg.parse(hapiMsg.encode());
        System.out.println("OBX-5 has " + ((ORU_R01) 
hapiMsg).getPATIENT_RESULT(0).getORDER_OBSERVATION(0).getOBSERVATION(0).getOBX().getObx5_ObservationValueReps());
        hapiMsg.parse(hapiMsg.encode());
        System.out.println("OBX-5 has " + ((ORU_R01) 
hapiMsg).getPATIENT_RESULT(0).getORDER_OBSERVATION(0).getOBSERVATION(0).getOBX().getObx5_ObservationValueReps());
        hapiMsg.parse(hapiMsg.encode());
        System.out.println("OBX-5 has " + ((ORU_R01) 
hapiMsg).getPATIENT_RESULT(0).getORDER_OBSERVATION(0).getOBSERVATION(0).getOBX().getObx5_ObservationValueReps());
        hapiMsg.parse(hapiMsg.encode());
        System.out.println("OBX-5 has " + ((ORU_R01) 
hapiMsg).getPATIENT_RESULT(0).getORDER_OBSERVATION(0).getOBSERVATION(0).getOBX().getObx5_ObservationValueReps());
        hapiMsg.parse(hapiMsg.encode());
        System.out.println("OBX-5 has " + ((ORU_R01) 
hapiMsg).getPATIENT_RESULT(0).getORDER_OBSERVATION(0).getOBSERVATION(0).getOBX().getObx5_ObservationValueReps());
        hapiMsg.parse(hapiMsg.encode());
        System.out.println("OBX-5 has " + ((ORU_R01) 
hapiMsg).getPATIENT_RESULT(0).getORDER_OBSERVATION(0).getOBSERVATION(0).getOBX().getObx5_ObservationValueReps());
    }
}

********************************************************************************

This email, including any attachments sent with it, is confidential and for the 
sole use of the intended recipient(s). This confidentiality is not waived or 
lost, if you receive it and you are not the intended recipient(s), or if it is 
transmitted/received in error.

Any unauthorised use, alteration, disclosure, distribution or review of this 
email is strictly prohibited. The information contained in this email, 
including any attachment sent with it, may be subject to a statutory duty of 
confidentiality if it relates to health service matters.

If you are not the intended recipient(s), or if you have received this email in 
error, you are asked to immediately notify the sender by telephone collect on 
Australia +61 1800 198 175<tel:%2B61%201800%20198%20175> or by return email. 
You should also delete this email, and any copies, from your computer system 
network and destroy any hard copies produced.

If not an intended recipient of this email, you must not copy, distribute or 
take any action(s) that relies on it; any form of disclosure, modification, 
distribution and/or publication of this email is also prohibited.

Although Queensland Health takes all reasonable steps to ensure this email does 
not contain malicious software, Queensland Health does not accept 
responsibility for the consequences if any person's computer inadvertently 
suffers any disruption to services, loss of information, harm or is infected 
with a virus, other malicious computer programme or code that may occur as a 
consequence of receiving this email.

Unless stated otherwise, this email represents only the views of the sender and 
not the views of the Queensland Government.

**********************************************************************************

------------------------------------------------------------------------------
Go from Idea to Many App Stores Faster with Intel(R) XDK
Give your users amazing mobile app experiences with Intel(R) XDK.
Use one codebase in this all-in-one HTML5 development environment.
Design, debug & build mobile apps & 2D/3D high-impact games for multiple OSs.
http://pubads.g.doubleclick.net/gampad/clk?id=254741911&iu=/4140
_______________________________________________
Hl7api-devel mailing list
Hl7api-devel@lists.sourceforge.net<mailto:Hl7api-devel@lists.sourceforge.net>
https://lists.sourceforge.net/lists/listinfo/hl7api-devel

------------------------------------------------------------------------------
Site24x7 APM Insight: Get Deep Visibility into Application Performance
APM + Mobile APM + RUM: Monitor 3 App instances at just $35/Month
Monitor end-to-end web transactions and take corrective actions now
Troubleshoot faster and improve end-user experience. Signup Now!
http://pubads.g.doubleclick.net/gampad/clk?id=267308311&iu=/4140
_______________________________________________
Hl7api-devel mailing list
Hl7api-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/hl7api-devel

Reply via email to