Griffon: Introspecting Artifacts - No Fluff Just Stuff

Griffon: Introspecting Artifacts

Posted by: Andres Almiray on November 2, 2010

Here's another peek at some of the features available in Griffon 0.9.1 (last post showed how non-Groovy artifacts can be created). The Artifact API received a full face lift in the latest release, allowing you to query artifacts and their conventions in a similar way as it is done in Grails. If you've looked at Griffon's runtime API you might have noticed a good number of new interfaces, for example These define the basic types of artifacts you may encounter on a vanilla Griffon application. Additional artifacts types may be introduced by plugins/addons. Useful as they are to determine how an artifact works, these interfaces cannot tell you what an artifact may contain, for example you can't inspect a Controller instance in order to find out which actions it exposes. This is where the next set of classes come inEvey artifact instance is backed by its corresponding GriffonClass. This means that a controller instance of type FooController will have a GriffonClass of type GriffonControllerClass that can be used to evaluate the conventions set by the class FooController. Confused? don't be. Let's explore the Artifact API with an example.

Assume for a moment we need to create a form based application. Each form may have a variable number of attributes (for simplicity's sake let's keep them of type String at the moment). Also, each form may have their own set of actions that affect the data somehow. How should we tackle this problem? I heard someone said "code generation!". Yes, that's one way to do it, but you can paint yourself into an ugly corner pretty fast. Let's follow a different path. In the end we want an application that looks like this

When you click the Submit button you should get a dialog like the following one

Alright, let's see how a typical Model would look like

Nothing unusual here, just your standard observable Groovy bean. Next is the Controller. Here we'll find the first hint that some magic will happen under the covers

This Controller exposes two actions: clear and submit. The job of the clear action is to reset the model state; it's done by simply setting each property value to an empty String. How does the Controller knows which properties must be reset? This is where the GriffonClass of the Model comes into play. The submit action uses the same trick (querying the Model's GriffonClass) to find out which properties must be read, then prepares and popups a dialog that displays all the information read from the Model. And now for the last part, let's have a look at the View

The View uses the same trick to access the Model's properties. It follows a similar path to find out which actions can be called from the Controller. The rest of the code is your standard Swing DSL ;-)

Now, what was that about multiple forms? The View as it stands is quite reusable, as it does not have ties to an specific Model or Controller class; you can actually share it with other Models and Controllers. Setting up multiple MVC groups can be done in griffon-app/conf/Application.groovy, assuming we have another form whose model is ExpressoModel then configuring it with this View and Controller is done in the following way

We could have changed the Controller to another class if the clear and submit actions are not suitable for the new Model.

The new Artifact API is the first step on getting scaffolded models and views (perhaps even domain classes) working in Griffon. The rest of the code can be written as a set of plugins. Nick Zhu has written a validation plugin that will surely come in handy in getting the equation right.

Keep on Groovying!
Andres Almiray

About Andres Almiray

Andres is a Java/Groovy developer and a Java Champion with more than 20 years of experience in software design and development. He has been involved in web and desktop application development since the early days of Java. Andres is a true believer in open source and has participated on popular projects like Groovy, Griffon, and DbUnit, as well as starting his own projects (Json-lib, EZMorph, GraphicsBuilder, JideBuilder). Founding member of the Griffon framework and Hackergarten community event. https://ch.linkedin.com/in/aalmiray

Why Attend the NFJS Tour?

  • » Cutting-Edge Technologies
  • » Agile Practices
  • » Peer Exchange

Current Topics:

  • Languages on the JVM: Scala, Groovy, Clojure
  • Enterprise Java
  • Core Java, Java 8
  • Agility
  • Testing: Geb, Spock, Easyb
  • REST
  • NoSQL: MongoDB, Cassandra
  • Hadoop
  • Spring 4
  • Cloud
  • Automation Tools: Gradle, Git, Jenkins, Sonar
  • HTML5, CSS3, AngularJS, jQuery, Usability
  • Mobile Apps - iPhone and Android
  • More...
Learn More »