Skip to content

Instantly share code, notes, and snippets.

@alexproca
Created January 10, 2014 19:02
Show Gist options
  • Save alexproca/8360443 to your computer and use it in GitHub Desktop.
Save alexproca/8360443 to your computer and use it in GitHub Desktop.
Count unedifact segments
import org.milyn.smooks.edi.EDIWritable
class UnedifactUtil {
public static int countSegments(final EDIWritable origmessage) {
def isSegmentGroup = { it?.class?.canonicalName?.contains('SegmentGroup') }
def countInDepthSegments
countInDepthSegments = { EDIWritable message ->
def notNullFieldValues = message
.getClass()
.getDeclaredFields()
.collect( { message."$it.name" } )
.grep({ it })
def firstLevelEdifactSegments = notNullFieldValues
.grep({it instanceof EDIWritable})
def inListsEdifactSegments = notNullFieldValues
.grep({it instanceof List})
.flatten()
.grep({ !isSegmentGroup(it) })
def inDepthEdifactSegments = notNullFieldValues
.grep({it instanceof List})
.flatten()
.grep(isSegmentGroup)
.collect(countInDepthSegments)
.flatten()
firstLevelEdifactSegments + inListsEdifactSegments + inDepthEdifactSegments
}
origmessage ? countInDepthSegments(origmessage).size() : 0
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment