Christian, Thank you!
I was able to make this work with the expected results: protected int getSegmentCount(String segment) { int segmentCount = 0; ReadOnlyMessageIterator readOnlyMessageIterator = new ReadOnlyMessageIterator(terser.getFinder().getRoot()); while (readOnlyMessageIterator.hasNext()) { try { Segment s = (Segment) readOnlyMessageIterator.next(); if (StringUtils.equalsIgnoreCase(segment, s.getName())) { segmentCount++; } } catch (ClassCastException e) { //these are not the droids you are looking for } } return segmentCount; } This could be easily modified to iterate over all segments in a message or perform the necessary operations during iteration rather than returning a count. Depending on what class is cast to, it could also be used to locate groups rather than segments. -----Original Message----- From: christian ohr [mailto:christian....@icw.de] Sent: Thursday, October 14, 2010 5:26 AM To: hl7api-devel@lists.sourceforge.net Subject: Re: [HAPI-devel] Counting or iterating over segments for an unkown or generic message type Hi JOnathan, 1. HL7 Message structures are used for grouping messages with the same internal structure (i.e. groups and segments). HAPI classes model message structures rather than messages. As ADT^A08 is defined to have a message structure called ADT_A01, the instantiated HAPI class is ADT_A01. 2. Segments can be nested in groups. The problem is that groups are not visible in pipe-encoded messages, so one has to look at the HL7 specification or simply at the instantiated message structure class - ca.uhn.hl7v2.model.v25.message.ADT_A01 contains a group ADT_A01_INSURANCE which contains the IN1 segments you are looking for. 3. For nested search of segments, have a look at ca.uhn.hl7v2.util.SegmentFinder or one of the Iterator classes in the same package. cheers Christian Jonathan Bartels-2 wrote: > > I have an application that is focused on processing messages segment by > segment, it is largely agnostic to the type of message. For example if we > get a message with an IN1 segment, the application will capture insurance > data. > > > > I am trying to do this with: > > protected int getSegmentCount(String segment) { > > try { > > return getMessage().getAll(segment).length; > > } catch (HL7Exception e) { > > log.warning("Found no segments for:" + segment, e); > > return 0; > > } > > } > > > > However if I send an ADT_A08 message I get: > > ca.uhn.hl7v2.HL7Exception: The structure IN1 does not exist in the group > ca.uhn.hl7v2.model.v25.message.ADT_A01 > > at > ca.uhn.hl7v2.model.AbstractGroup.getAll(AbstractGroup.java:335) > > > > This is odd for two reasons, it’s not an ADT_A01 and despite its message > type this particular sample has two IN1 segments. > > > > Is there a better way to count the number of segments in a message? > > ------------------------------------------------------------------------------ > Beautiful is writing same markup. Internet Explorer 9 supports > standards for HTML5, CSS3, SVG 1.1, ECMAScript5, and DOM L2 & L3. > Spend less time writing and rewriting code and more time creating great > experiences on the web. Be a part of the beta today. > http://p.sf.net/sfu/beautyoftheweb > _______________________________________________ > Hl7api-devel mailing list > Hl7api-devel@lists.sourceforge.net > https://lists.sourceforge.net/lists/listinfo/hl7api-devel > > -- View this message in context: http://old.nabble.com/Counting-or-iterating-over-segments-for-an-unkown-or-generic-message-type-tp29957804p29960699.html Sent from the hl7api-devel mailing list archive at Nabble.com. ------------------------------------------------------------------------------ Beautiful is writing same markup. Internet Explorer 9 supports standards for HTML5, CSS3, SVG 1.1, ECMAScript5, and DOM L2 & L3. Spend less time writing and rewriting code and more time creating great experiences on the web. Be a part of the beta today. http://p.sf.net/sfu/beautyoftheweb _______________________________________________ Hl7api-devel mailing list Hl7api-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/hl7api-devel ------------------------------------------------------------------------------ Beautiful is writing same markup. Internet Explorer 9 supports standards for HTML5, CSS3, SVG 1.1, ECMAScript5, and DOM L2 & L3. Spend less time writing and rewriting code and more time creating great experiences on the web. Be a part of the beta today. http://p.sf.net/sfu/beautyoftheweb _______________________________________________ Hl7api-devel mailing list Hl7api-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/hl7api-devel