Database Reference
In-Depth Information
Those who know Java will be impressed with the simplicity here. We have created no
classes and no main method. groovy allows us to get right down to business and does
that overhead work for us at runtime. This allows us to focus on the code that does the
stuff we want done.
Groovy is an extension of Java that removes many of the more tedious elements of Java programming while remain-
ing highly compatible with Java's native syntax. When used for scripting, the Groovy runtime identifies the code to be
executed, wraps it with a main method and a class declaration, compiles it, executes it, then deletes the files created
during the process. All of this happens automatically behind the scenes, which allows a Groovy developer to concentrate
on coding, working, and thinking as if the program runs directly from its source code.
9.3.2 Step Two: Iteration
now that we can create output, we are going to need to know how to iterate through
a set of data, in other words, to step through it element by element. Essbase's Sample
application has three databases, and we want to run the same code against all of them.
For now, so we can concentrate just on the iteration part, we will hard code their names
and, changing our initial script a bit, say hello to the databases in turn.
['Basic', 'Interntl', 'Xchgrate'].each { println "Hello,
Sample.${it}!" }
This produces:
Hello, Sample.Basic!
Hello, Sample.Interntl!
Hello, Sample.Xchgrate!
Java programmers, and even a few Perl jockeys, will really start to sit up and take
notice here. We have declared no looping variable and used no “for”-type statement to
get the looping done. groovy has allowed us to write a block of code called a closure (the
part between the curly braces) and run it against “each” of the items found in our hard-
coded “list.” no muss, no fuss. Just efficient, workman-like code.
Groovy's iterators, such as each, provide to their closures a variable called it which has a different value for each iteration
of the code. In some cases, a different name for this variable is useful, or more than one variable is needed. These needs
are easily met with named parameter lists.
Groovy allows the quoting of strings with single or double quotation marks. When using double quotes, the string can
be turned into a GString (seriously!) with the $ symbol and a reference to a variable, an expression, or a call to something
that returns a value. The value of the $ expression is substituted into the string at runtime.
9.3.3 Step Three: Connecting to Essbase and Retrieving Databases
We are now ready to start using Java API calls to actually retrieve the list of databases
from Sample itself. We will add a couple of lines of code above our loop to make the con-
nection and a couple more below to tear it down cleanly. Then we will be able to loop on
a real thing retrieved from Essbase.
import com.essbase.api.session.IEssbase
essHome = IEssbase.Home.create(IEssbase.JAPI_VERSION)
essSvr = essHome.signOn('user', 'pass', false, null,
'http://aps_server:13080/aps/JAPI', 'ess_server')
essSvr.getApplication('Sample').cubes.all.each { println it.name }
essSvr.disconnect()
essHome.signOff()
Search WWH ::




Custom Search