Grails Scripts: validating domain class

The problem

I needed to create scripts that receive the domain class as argument. The problem is that the user can type anything, so we need to validate it.

Validating Domain Classes in Your Script

I looked at the grails scaffolding plugin, because it already implements such validation. If you’re not familiar with, this functionality was inside the core in Grails 2.2.x and they transformed in an independent plugin, allowing faster changes. Now Grails don’t need to release a new version just because some scaffold features changed and the users can benefit from the changes faster. This is a great approach, and I would like to see it more in the core, making him lightweight.

Script that load GrailsApplication

Just seeing the script I realized that we can benefit from the load of the GrailsApplication bean, and use the getDomainClass() method to check if the class is indeed an domain artefact. If this was an Controller instead of script, we could do:

And in an script, it’s the same! :-)

And if you need more information about how Grails bootstrap, look at the linked resources in the gist.

Tagged with: , ,
Posted in Grails

Testing internationalized applications in Grails

So, you take advantage of the internationalization support that Grails have out of the box and now you need to test your application in different languages. What can you do?

Grails tests build a mocked request instance of the type org.codehaus.groovy.grails.plugins.testing.GrailsMockHttpServletRequest, this class extends org.springframework.mock.web.MockHttpServletRequest and have the method addPreferredLocale, with that you can set the java.util.Locale that will be used when your code call g.message.

Here’s an Spock Integration Test example

Tagged with: , ,
Posted in Grails, Internacionalization

Quick note: enable gzip compression in the embedded Tomcat

Nothing much to say here. To improve performance in the development environment I wanted to enable GZIP compression of my javascript files. It turns out that you can change the Tomcat configs in the _Events.groovy script.

With that the size of ExtJS (ext-all-dev.js) drops from 6.5~ MB to 1.4~!

Tagged with: , , ,
Posted in Grails, ExtJS

ExtJS – Tree not showing text

After a real fight with Ext.tree.Panel and Ext.data.TreeStore I figured out the reason why my text wasn’t displayed. In the documentation of TreeStore, it says:

If no Model is specified, an implicit model will be created that implements Ext.data.NodeInterface. The standard Tree fields will also be copied onto the Model for maintaining their state. These fields are listed in the Ext.data.NodeInterface documentation.

My understanding is that, if I set a model in the Store, all fields in Ext.data.NodeInterface will be available. But the real deal is that you need to define them to make your tree work.

Tagged with: , ,
Posted in ExtJS

Inspect Grails Domain Class constraints

Today I needed to check if some constraint was present in my Domain Class.

My first though was to try to inspect the constraints closure. I spent a good time trying to find how to correctly inspect a closure.

It turns out that at runtime you can access your constraints directly, as a Map.

Tagged with: , ,
Posted in Grails

Getting all i18n messages in Javascript

Intro

When building a Single Page Application it’s interesting to have available in JavaScript all your i18n messages.

So, where we start?

Plugins

Grails have the concept of Plugins. If you’re learning how the framework work it’s really interesting to spend some time understanding this topic. The framework itself has a set o plugins that’s already available for you.

If you’re using GGTS / STS, you can use ctrl + shift + T and type *GrailsPlugin*. Sometimes when I want to know how some aspect of Grails works, I try to find the plugin responsible for that.

Finding the Plugin

In this specific case, we have I18NGrailsPlugin. If you look at the source you can see the declaration of the messageSource bean.

So we can extend PluginAwareResourceBundleMessageSource and declare this as our messageSource and declare the Spring Bean:

But doing that we have a problem: all attributes that I18NGrailsPlugin had setup aren’t configured in our class! Without basenames properly set our class don’t know the existence of the properties files.

There are two options to solve our issue:

#1 – Duplicate code

We can duplicate the I18NGrailsPlugin code, setting up the properties of the ExposedKeysMessageSource class. But this is bad, what if the configuration code changes? We need to always check Grails updates to be up-to-date.

#2 – Hook the messageSource configuration

What if I tell you that we can use the configuration made by the i18n plugin, but using our customized class? Certainly better.

And already exists an example, the Tomcat JDBC Pool Plugin. The aim of this plugin is to change the pool implementation used by Grails to the Tomcat JDBC (ships with Tomcat 7), and all this is made in the doWithSpring closure. So, adapting to us:

Note that we don’t need to re-declare messageSource anymore, since we already changed the original Spring Bean configuration.

What’s next?

So now we know all possible i18n keys, that’s great, but how can we transform this in JavaScript? A common pattern of transformation in server data to the client side is the JSON. If you already saw Grails converters you remember that it’s already there! This conversion to JSON and XML is also made by a core Grails plugin, the ConvertersGrailsPlugin. To organize our code, we can do that inside a service:

But note also that I used a new method called getAllProperties. Here’s the updated code of our MessageSource:

Now, all we need is to parse this String in a JavaScript variable. The script can be attached through TagLib, or <r:script> tag or event a controller that returns the content type text/javascript. I will go with the last one to show you what we can do with Grails controllers:

And with that we are ready to use our internationalization in JavaScript :-)

 

Tagged with: , ,
Posted in Grails, Internacionalization

ExtJS4 MVC – Organized application that dynamic loads controllers

I was looking to a good way of loading controllers dynamically in the MVC used by ExtJS4 and find this great video tutorial.

The fiddle provided is not working anymore, so I’ve updated the example, using the latest version available in JSFiddle to ExtJS: http://jsfiddle.net/vkgjU/395/

I always liked the convention over configuration paradigm (Grails use it a lot!) and will defiantly use this approach to my applications.

Tagged with: , , , ,
Posted in ExtJS
Follow

Get every new post delivered to your Inbox.