Database Reference
In-Depth Information
----Qtr1
------Jan
------Feb
...
We have created a member processor by assigning a closure to be the value of a vari-
able. We can think of this as giving the closure a name. The important concept to under-
stand is that when we do this, we are then able to pass the closure around like a piece of
data. We also can do the thing we are doing here and use the variable to call the closure
like a function. We could have defined a regular old Java function here, but closures have
a bit more flexibility, and this is after all a chapter about groovy.
In the above code, the member processor outputs information on the member passed
to it, then calls itself twice, the first time passing the member's first child and the second
time passing its next sibling. This effectively executes a depth-first walk of the outline
hierarchy. Beyond this recursion (code that calls itself), there is not much new in this
step. one thing of note is the little trick we are pulling to give the output an outline-like
structure by prepending dashes based on the member's generation.
Groovy's operator overloading feature allows for many interesting additions to Java's standard behavior. For instance, with
Strings, 'Essbrase' - 'r' results in 'Essbase', 'x' * 5 is 'xxxxx', and 'ab' << 'cd' is 'abcd'. In addition Groovy adds a number
of methods to strings, such as padLeft, padRight, and center, several direct type conversions, richer regular expression
support, and useful iterators like eachLine and findAll.
9.3.6 Step Six: XML Output
We have now reached the point of turning our output into xmL format. With groovy's
builder concept, specifically its markupBuilder object, this is an easy task. We replace
the println calls with calls into a builder. Builders are designed to perform a special
behavior when methods are called on them that do not actually exist within the builder
object. A markupBuilder knows how to turn such an arbitrary method call into an xmL
tag. It uses a data map to assign attributes to the tag and a closure to assign the body.
This is probably better understood after looking at some code.
import com.essbase.api.session.IEssbase
doMbrThing = { essCube, mbrName ->
if (mbrName.size() == 0 ) return
essMbr = essCube.getMember(mbrName)
xml.member(name:essMbr.name) {
doMbrThing(essCube, essMbr.firstChildMemberName)
}
doMbrThing(essCube, essMbr.nextSiblingMemberName)
}
writer = new StringWriter()
xml = new groovy.xml.MarkupBuilder(writer)
essHome = IEssbase.Home.create(IEssbase.JAPI_VERSION)
essSvr = essHome.signOn('user', 'pass', false, null,
'http://aps_server:13080/aps/JAPI', 'ess_server')
essApp = essSvr.getApplication('Sample')
xml.application(name: essApp.name) {
essApp.cubes.all.each { essCube ->
xml.cube(name: essCube.name) {
Search WWH ::




Custom Search