Why not simply:

Structure[] zals = message.getAll("ZAL")
int reps = zals.length

then you can iterate over the zals, cast them to a segment and do your
checks.

cheers
Christian

2015-03-26 1:18 GMT+01:00 Ian Vowles <ian.vow...@health.qld.gov.au>:

> I have been trying to create a method that checks a field for a specific
> value that will work when passed just a Message object with any parsed
> message. It's a bit tricky because it's in a Z-Segment, but the methods all
> look to be there.
>
> Unfortunately, I have hit a somewhat odd behaviour.  My input message is
> an A01 with  a couple of ZAL segments on the end. Here is the code:
>
>         String inputMessage
>                 =
> "MSH|^~\\&|HOMER|irusgch|EGATE-IRUS^prjHbcisRawIn|00050|20150324121258||ADT^A01|20150324121258383268|P|2.3|||AL|AL|AUS|ASCII|ENG\r"
>         .
>         .  bunch of nice A01 segments
>         .
>                 + "ZMR|20140930|095522|1|GC1|\"\"|0001|NIUMM\r"
>                 + "ZAL|1|AL|NOTTRAU1^NOT TRAUMA
> SERVICES^ALERT|||20150324000000||A01 TEST\r"
>                 + "ZAL|2|AL|NOTTRAU^TRAUMA
> SERVICES^ALERT|||20150324000000||A01 TEST\r";
>
>         HapiContext context = new DefaultHapiContext();
>         context.setModelClassFactory(new GenericModelClassFactory());
>         Message message = context.getPipeParser().parse(inputMessage);
>         Terser terser = new Terser(message);
>         SegmentFinder finder = terser.getFinder();
>         Segment zal;
> //        zal = finder.getSegment("ZAL", 2);
> //        zal = finder.getSegment("ZAL", 3);
>         boolean zalFound = true;
>         boolean validZal = false;
>         int zalIndex = 0;
>         while (zalFound && !validZal) {
>             try {
>                 zal = finder.getSegment("ZAL", zalIndex);
>                 String zal3 = terser.get(zal, 3, 0, 1, 1);
>                 validZal = "TRAU".equalsIgnoreCase(zal3);
>                 System.out.println("Counter is = '" + zalIndex + "'
> ZAL-3-1 = '" + zal3 + "'");
>                 zalIndex++;
>                 zalFound = zalIndex < 100;
>             } catch (HL7Exception ex) {
>                 zalFound = false;
>             }
>         }
>         System.out.println("Valid ZAL found = " + zalFound);
>
> When I first started writing, I tried out the line zal =
> finder.getSegment("ZAL", 3); to see if an exception would be thrown, and it
> was, and it even gave me a range for the number of ZAL segments (more on
> that later).
> However, when I ran the code in the loop, before I added an upper bound to
> the count of ZAL segments, it would loop for ever.
>
> Further investigation showed me that if I asked the finder for a segment
> repetition 1 higher than the number present (uncommenting the two
> getSegment lines together does not throw an exception), then an empty
> segment is added and returned, and since I am incrementing through, this
> behaviour continues forever.
>
> So, how can I find out before I start looping, how many repeats of the
> segment exist, so I can set an accurate upper bound, or how can I get a
> 'not found' exception to be thrown should I ask for an index beyond those
> that actually exist?
>
> I did find that using zalFound = !zal.isEmpty(); instead of using a large
> upper bound of the count worked, which is ok, but not really obvious.
>
> Thanks
> Ian
>
>
> ********************************************************************************
> 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 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.
>
> **********************************************************************************
>
>
>
> ------------------------------------------------------------------------------
> Dive into the World of Parallel Programming The Go Parallel Website,
> sponsored
> by Intel and developed in partnership with Slashdot Media, is your hub for
> all
> things parallel software development, from weekly thought leadership blogs
> to
> news, videos, case studies, tutorials and more. Take a look and join the
> conversation now. http://goparallel.sourceforge.net/
> _______________________________________________
> Hl7api-devel mailing list
> Hl7api-devel@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/hl7api-devel
>
------------------------------------------------------------------------------
Dive into the World of Parallel Programming The Go Parallel Website, sponsored
by Intel and developed in partnership with Slashdot Media, is your hub for all
things parallel software development, from weekly thought leadership blogs to
news, videos, case studies, tutorials and more. Take a look and join the 
conversation now. http://goparallel.sourceforge.net/
_______________________________________________
Hl7api-devel mailing list
Hl7api-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/hl7api-devel

Reply via email to