Grails and AST transformations – hard start

Recently i tried to use AST transformation in my Burning Image Grails plugin. I want to use them, to mark domain objects that should be extended by some methods and fields that allow to use this object as a container for information about image saved on server.

I follow this "how to" tutorial, but after struggling with code for several hours, its appears (Joshua Burnett was very helpful - thanks) that if you want to use this technique, you should write code as a pure Java classes not Groovy scripts. After that, it run smoothly.

If any one is capable to run AST transformations as groovy scripts (in Grails project), i will be very thankful for "how to".

Mixin is not 100% inheritance

Groovy allows us to simulate multiple inheritance by using @Mixin annotation:

  1.  
  2. class Foo {
  3. def sayFoo(){
  4. "hello from foo"
  5. }
  6. }
  7.  
  8. class Bar {
  9. def sayBar(){
  10. "hello from bar"
  11. }
  12. }
  13.  
  14. @Mixin([Foo, Bar])
  15. class Bas {
  16. }
  17.  
  18. def foo = new Foo()
  19. def bar = new Bar()
  20. def bas = new Bas()
  21.  
  22. assert foo.sayFoo() == bas.sayFoo()
  23. assert bar.sayBar() == bas.sayBar()
  24.  
  25.  

but unfortunately it will only copy methods and properties, and will not provide mixed type, so when you try something like this:

  1.  
  2.  

assertion exception will be throw.

Grails url mapping localization

This is simple "hack" that allows you to internationalize your url in your Grails (1.2.x) application. Lets say that we have application that is provided in two languages: Polish and English. Create ours controller:

  1.  
  2. FooController {
  3. def bar {
  4. }
  5. }
  6.  

In ours locale files (message_pl.properties and message_en.properties) add "locale" param (because grails bug) with code specified for this file (pl, en).

Next step is to create new taglib that will generate internationalized link:

  1.  
  2. class BaseTagLib {
  3. def localizedLink = { attrs, body ->
  4. attrs.mapping = message(code:'locale') + StringUtils.toUpperCaseFristLetter(attrs.mapping)
  5. out << link(attrs, body)
  6. }
  7. }
  8.  

This Taglib doing nothing than add prefix for name of the route. It use util method StringUtils.toUpperCaseFristLetter, that capitalize first letter of provided string:

  1.  
  2. class StringUtils {
  3. static def toUpperCaseFristLetter(String text){
  4. text[0].toUpperCase() + text[1..-1]
  5. }
  6. }
  7.  

Now we can provide url mappings as:

  1.  
  2. class UrlMappings {
  3. static mappings = {
  4. name enGoToFooBar:'/go/to/foo/Bar' {
  5. controller = 'foo'
  6. action = 'bar'
  7. }
  8.  
  9. name plGoToFooBar:'/idz/do/foo/Bar' {
  10. controller = 'foo'
  11. action = 'bar'
  12. }
  13. }
  14. }
  15.  

In ours gsp files we can use it like:

  1.  
  2. <g:localizedLink mapping="goToFooBar"><g:message code="text.goToFooBar" /></g:localizedLink>
  3.  

Setting default locale

If you want to set default locale for application, add this definition of LocaleResolver to spring context configuration:

  1.  
  2. localeResolver(SessionLocaleResolver) {
  3. defaultLocale = new Locale('pl')
  4. Locale.setDefault (defaultLocale)
  5. }
  6.  

Request.locale Grails bug

If you want to use request.locale to determine current locale - forget abut that. Even in Grails 1.2, if you pass lang parameter there will be no change of this object and it will stay as a default (even if i18n properties file will be changed). Quick fix is to add locale entry in message bundles (maybe, hmmm locale) and use it.

Google.ce free from censorship

In recent post, David Drummond said, that after last attacks on Gmail accounts that appears effort to get access to accounts of Chinese human right activists, Google took decisions to stop censoring Chinese internet. Even more, if discussions witch Chinese government will not give any result, they may shut down Google.ce!

Well, Google get point from me, for being not only corporation ...

Powered by WordPress with GimpStyle Theme design by Horacio Bella.
Entries and comments feeds. Valid XHTML and CSS.