This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Groovy and the fly likes Griffon Lifecycle Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Languages » Groovy
Bookmark "Griffon Lifecycle" Watch "Griffon Lifecycle" New topic
Author

Griffon Lifecycle

Tom Patterson
Greenhorn

Joined: May 03, 2004
Posts: 3
    
    1
In the Ready.groovy script I would like to load some data into my view. My controller already has a closure to do this. However, I don't know how to reference my controller from the Ready.groovy script! I have been looking for examples of this and have come up empty. Any help would be appreciated!
Tom Patterson
Greenhorn

Joined: May 03, 2004
Posts: 3
    
    1
So no replies to my question. I was motivated to press in and figure it out on my own (With the help of Griffon In Action, the online Griffon Guide, and the Griffon API Documentation). Here it is:

The first thing I needed to know about was "app".

This is described in the following section of Griffon In Action:
5.1.1 Quick tour of injected properties and methods.
"The app property points to the current running application."

So now I know that the app is injected into the Ready.groovy script.

In the same section it says: "... by inspecting app.controllers and checking
for their type."

This is a good clue as to how I can get to my controller.

In section 6.3.1 I found the following: "The object that is used to access the portions is the app property. This property is unique from the other injectable properties in that it’s always available in MVC group members because Griffon injects its value at the metaclass level. It also implements the griffon.core.GriffonApplication interface."

So now I know that app implements griffon.core.GriffonApplication.

Online there is the very good Griffon Guide at:
http://griffon.codehaus.org/guide/1.4.0/guide/

In Chapter 11 (Resource Management) it talks about Resolving Configured Resources. It also has a link to griffon.core.GriffonApplication.

That link takes you to the Griffon API Documentation:
http://griffon.codehaus.org/guide/1.4.0/api/griffon/core/GriffonApplication.html

Of interest to me was the method summary:
Map getControllers()
Returns all currently available controller instances, keyed by group name.

Eureka! I can now get to my controller.

So I printed out the map that was returned by getControllers:
HyperTax
hypertax.HyperTaxController@927e4be

The first line is the key to my one and only controller.
The second line is the value of the controller instance (its reference)

So I still had to do a few more things:

I added the following import statement to Ready.groovy:
import hypertax.HyperTaxController

I then declared my controller:
HyperTaxController controller

I then assigned the value from the map to the controller variable.

Finally I called the controller method that accesses the database and populates the view:
controller.readTaxable()

Here's the complete Ready.groovy script:

import hypertax.HyperTaxController
/*
* This script is executed inside the UI thread, so be sure to call
* long running code in another thread.
*
* You have the following options
* - execOutsideUI { // your code }
* - execFuture { // your code }
* - Thread.start { // your code }
*
* You have the following options to run code again inside the UI thread
* - execInsideUIAsync { // your code }
* - execInsideUISync { // your code }
*/

Map controllerMap
HyperTaxController controller

controllerMap = app.getControllers()
controllerMap.each {entry ->
println entry.key
println entry.value
controller = entry.value
}

controller.readTaxable()

I hope this helps someone else out there that is wrestling with Griffon!

Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9915
    
158

We unfortunately do not have too many experts in this area which sometimes leads to lack of an answer. However, thank you for posting that detailed answer, which has now earned you a cow.

[My Blog] [JavaRanch Journal]
Tom Patterson
Greenhorn

Joined: May 03, 2004
Posts: 3
    
    1
Jaikiran, thank you for the cow!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Griffon Lifecycle
 
Similar Threads
Application deployment guide
User customizable navbar.
You control her!
Automating rollback
Eric.... I need your help