Tuesday, 13 May 2014

Executing SQL queries with GORM

Yesterday at work somebody asked me how to execute a SQL query through GORM. I didn't remember at the moment how exactly how to do it, but I remembered that Hibernate's Session class had a method called executeSQLQuery.

This is a silly example to show how we have done it. Lets say you have a domain class 'MyDomainClass' and inside a service (MyService) you need to execute a given SQL query. It could be like this:

  class MyDomainClass {
      String name
      Integer age
  }

  class MyService {
      Integer getAgeByExecutingSQL() {
         return MyDomainClass.withSession { session ->
            session.executeSQLQuery("select age from my_domain_class").get()
         }
      }
  }

Accessing Hibernate session through withSession method we can access the method I was mentioning above. Then is just a matter of creating a valid SQL and of course keep in mind that the query doesn't return domain class instances but arrays of data.


Wednesday, 30 April 2014

New conditional expressions in Groovy

Many times I've heard someone to say it would be great to have some more control expressions. The good thing in Groovy is that because its syntax you can build your own expressions like if they were already part of the language.

I've created a couple of my own inspired by some other languages out there:

Unless


Unless is a typical expression in other languages like Ruby and sometimes in Groovy it's just annoying to use the not operator. Let's say for example we had this piece of code:

if (!companyIsBankrupt) {
     invest()
}

Wouldn't be nicer to see...

unless(companyIsBankrupt()) {
     invest()
}

Of course it may depend on the context but I think is more readable and clearer than the first attempt. Also this expression not only serves as conditional but because it's been implemented as a method it could return a value, so the latest example could become:
def investment = unless(companyIsBankrupt()) {
     invest()
}

Where


This expression is more complex because it has an internal state and more embedded conditional expressions. It is inspired by the where expression in Haskell.

check(weight: 60) {
    when { weight <= underweight } then { "You're underweight" }
    when { weight <= normal }      then { "You're normal" }
    when { weight <= fat }         then { "You're fat" }
    otherwise { "You have a strange composition" }
    where {
       underweight = 50
       normal = 70
       fat  = 90
    }
}


You can initialize the whole expression with a map and then use the when-then branches to execute the right one taking into account set values within the where clause.

When-then clauses are half way between if-else expressions and switch-case expressions but there is something unique about this type of expression, pre-set values within the scope of the expression and shared among all when-then expressions.

My initial implementation is still a proof of concep and still lacks of some desirable constraints:
  • Initialization values at check() method should be immutable values
  • Values created within the where block should be immutable too
I forgot to mention that the same way unless expression could return a value this check-where expression can do it as well so it won't be any problem to assign the execution of the where clause to a given variable:
def message = check(weight: 60) {
    when { weight <= underweight } then { "You're underweight" }
    when { weight <= normal }      then { "You're normal" }
    when { weight <= fat }         then { "You're fat" }
    otherwise { "You have a strange composition" }
    where {
       underweight = 50
       normal = 70
       fat  = 90
    }
}

Resources


Sunday, 27 April 2014

Searchable plugin: Search by a domain relationship

I don't remember last time I used Searchable Plugin, there must be like a year or so. Anyway I was trying to map a Product/I18nProductDescription domain classes to be able to search by the product description and then return the product id in order to build the link to the product detail.
I was using the default configuration and doing the queries through searchable controller to test my mappings. These were the initial mappings:
class Product {

    static hasMany = [descriptions: I18NDescription]   

}

class I18nDescription {

   static belongsTo = [product: Product]
   static searchable = true

   Locale locale
   String title
   String description

}

The problem was that if you map as "searchable" only I18nDescription, the index doesn't store the relationship, then you won't be able to do "productDescription.product.id" and you'll get a NPE (I guess this is caused because instances are not taken from the hibernate session but from lucene index).

I read the documentation and I found the "component" term.

class Product {

   static hasMany = [descriptions: I18NDescription]   
   static searchable = {
      descriptions component: true
   }

}

class I18nDescription {

   static belongsTo = [product: Product]
   static searchable = {
      product component: true
   }

   Locale locale
   String title
   String description

}


So I mapped my to domain classes as components of each other. But if I stopped there I would get double of results, because both domain classes are pointing to each other right? So I need to "disable" for searching one of them. To do so, one of them should have an option to tell it's not going to have it's one index but is going to be part of another search index.

class Product {

   static hasMany = [descriptions: I18NDescription]   
   static searchable = {
      descriptions component: true
      mapping {
        root false
      }
   }

}

class I18nDescription {

   static belongsTo = [product: Product]
   static searchable = {
      product component: true
      mapping {
        root true
      }
   }

   Locale locale
   String title
   String description

}

That way you can search by title or description and be able to get the reference of the product without having to do anything else.

References

Thursday, 28 November 2013

Testing a command object without a controller

Sometimes you just want to test a command object in a Spock specification without having to use @TestFor with an existing controller class passed as an argument.

Back in time (last week or so XD ) I used to do it this way

import spock.lang.Specification
import grails.test.mixin.TestFor

@TestFor(SomeControllerClass)
class MyCommandObjectSpec extends Specification {
   // ... your stuff
}

But the thing is that Grails wraps all its mocking functionality in "mixins"  and the way you can apply those mixins is through the @TestMixin annotation.

Normally you don't notice this behavior because the annotation @TestFor does that under the hood, but if you had to apply controller mocking behavior in your specification you should be using the @TestMixin annotation with the ControllerUnitTestMixin directly:

import spock.lang.Specification
import grails.test.mixin.TestMixin
import grails.test.mixin.web.ControllerUnitTestMixin

@TestMixin(ControllerUnitTestMixin)
class MyCommandObjectSpec extends Specification {
  //... your stuff
}

Happy testing :)

Tuesday, 19 November 2013

Generating an Inner class using Groovy AST transformations

Oh man! This was hard to find XD!

During the weekend I've been trying to create a simple AST transformation that generates an inner class. I dove into Groovy's documentation and googled during the last two days, but it seemed nobody has struggled with the same issue.

In the end I achieved the goal, and I thought it would be nice to post it in case anyone could have the same problem :)

The transformation takes a method annotated with @MoveToInner and moves it to an inner class. The inner class will have the name passed as parameter to the annotation.

Here is the code:

package com.github.groovy.astextras.local.inner

import org.codehaus.groovy.ast.ASTNode
import org.codehaus.groovy.ast.MethodNode
import org.codehaus.groovy.ast.ClassNode
import org.codehaus.groovy.ast.AnnotationNode

import org.codehaus.groovy.ast.builder.AstBuilder

import org.codehaus.groovy.control.SourceUnit
import org.codehaus.groovy.control.CompilePhase
import org.codehaus.groovy.control.CompilationUnit
import org.codehaus.groovy.control.CompilerConfiguration

import org.codehaus.groovy.transform.ASTTransformation
import org.codehaus.groovy.transform.GroovyASTTransformation

/**
 * This transformation takes the method annotated with @MoveToInner and effectively
 * moves it to an inner class which has the name of the value given as value to the
 * former annotation.
 *
 * This way a code like the following:
 * 
 *   package com.github.groovy.astextras.local.inner
 *
 *   class Something {
 *     @MoveToInner("Foo")
 *     def myMethod() {
 *       return "Hello John"
 *     }
 *   }
 * 
* * Will become... * *
 *   package com.github.groovy.astextras.local.inner
 *
 *   class Something {
 *
 *      class Foo {
 *       def myMethod() {
 *          return "Hello John"
 *       }
 *      }
 *
 *   }
 * 
* * */ @GroovyASTTransformation(phase = CompilePhase.INSTRUCTION_SELECTION) class MoveToInnerAst implements ASTTransformation { static final VALUE = 'value' static final DOLLAR = '$' static final PUBLIC = ClassNode.ACC_PUBLIC void visit(ASTNode[] astNodes, SourceUnit sourceUnit) { if (!checkNodes(astNodes)) return def annotationNode = astNodes[0] def methodNode = astNodes[1] def declaringClass = methodNode.declaringClass def innerClassName = annotationNode.members.getAt(VALUE)?.text def outerClassName = declaringClass.name def outerClassNode = createOuterClass(outerClassName) def innerClassNode = createInnerClass(outerClassName, innerClassName) innerClassNode.addMethod(cloneNode(methodNode)) def compilerConfiguration = sourceUnit.getAST().getUnit().config def compilationUnit = new CompilationUnit(compilerConfiguration).with { addClassNode(outerClassNode) addClassNode(innerClassNode) compile() } } /** * This method checks that the nodes passed as parameters are the ones we * want to visit * * @param astNodes The nodes we may want to process * @return true if the nodes are the ones we were looking for, false otherwise */ def checkNodes(ASTNode[] astNodes) { astNodes && astNodes[0] && astNodes[1] && astNodes[0] instanceof AnnotationNode && astNodes[0].classNode?.name == MoveToInner.class.name && astNodes[1] instanceof MethodNode } /** * This method creates an inner class * * @param qualifiedOuterClassName qualified outer class name (with the name of the package) * @param simpleInnerClassName name of the inner class (without the name of the package) * @return an instance of an InnerClassNode */ def createInnerClass(String qualifiedOuterClassName, String simpleInnerClassName) { def innerClassFullName = qualifiedOuterClassName + DOLLAR + simpleInnerClassName new AstBuilder().buildFromSpec { innerClass(innerClassFullName, PUBLIC) { classNode(qualifiedOuterClassName, PUBLIC) { classNode Object interfaces { classNode GroovyObject } mixins { } } classNode Object interfaces { classNode GroovyObject } mixins { } } }.first() } /** * This method creates an empty class node with the qualified name passed as parameter * * @param qualifiedClassNodeName The qualified name of the ClassNode we want to create * @return a new ClassNode instance */ def createOuterClass(String qualifiedClassNodeName) { new AstBuilder().buildFromSpec { classNode(qualifiedClassNodeName, PUBLIC) { classNode Object interfaces { classNode GroovyObject } mixins { } } }.first() } /** * This method clones the method node passed as parameter * * @param methodNode the MethodNode instance we want to clone from * @return a cloned instance of the node passed as parameter */ def cloneNode(MethodNode methodNode) { new MethodNode( methodNode.name, methodNode.modifiers, methodNode.returnType, methodNode.parameters, methodNode.exceptions, methodNode.code ) } }

The hidden gem of the solution was the following lines:


def compilerConfiguration = sourceUnit.getAST().getUnit().config
        def compilationUnit =
            new CompilationUnit(compilerConfiguration).with {
                addClassNode(outerClassNode)
                addClassNode(innerClassNode)
                compile()
            }

Those lines are responsible to create the .class files for the inner and the outer class. Before using those lines I was unable to make them visible for the rest of the classes. If anybody knows how to do it in any other way, please tell me, I'm all ears.

You can also get the compiler compilation configuration with the shorter expression:

   def compilerConfiguration = classNode.compileUnit.config


 UPDATE (Thanks to Andres Almiray) :

Andres pointed out that the solution was even easier. It was unnecessary to get the compilation unit. The only thing I had to do was:

 void visit(ASTNode[] astNodes, SourceUnit sourceUnit) {

        if (!checkNodes(astNodes)) return

        def annotationNode = astNodes[0]
        def methodNode = astNodes[1]
        def declaringClass = methodNode.declaringClass

        def innerClassName = annotationNode.members.getAt(VALUE)?.text
        def outerClassName = declaringClass.name

        def outerClassNode = createOuterClass(outerClassName)
        def innerClassNode = createInnerClass(outerClassName, innerClassName)

        innerClassNode.addMethod(cloneNode(methodNode))

        methodNode.declaringClass.module.addClass(innerClassNode) 
 }


As I mentioned earlier you can find this code and more AST examples at Github

Friday, 8 November 2013

Exposing Grails URLMappings as JSON

I'm currently working in a REST API. The endpoints are increasing really fast and we haven't decided how to document it yet. Until that happens I was wondering it would be nice to know how to access URLMappings data at runtime.

We've created a controller where the URLMappings structure is exposed. Only thing we have to do is to access to "grailsApplication" and "grailsUrlMappingsHolder" beans.

import grails.converters.JSON

class RootController extends BaseApiController {
   
      def grailsApplication
      def grailsUrlMappingsHolder
  
      def listApiUrls() {
  
          def urlMappings = grailsUrlMappingsHolder.urlMappings.collect { [
  
                  name: it.mappingName?:'',
                  url: it.urlData.logicalUrls.first(),
                  methods: it.parameterValues,
                  parameters: it.constraints.propertyName
 
          ] }
  
  
          def root = [
  
              metadata: grailsApplication.metadata,
              urlMappings: urlMappings
  
          ]
  
          render root as JSON
  
      }
  
  }    

We put all the information in a map and convert it to JSON and....

{
  "metadata": {
    "app.version": "0.1",
    "app.grails.version": "2.2.4",
    "app.name": "yump-api"
  },
  "urlMappings": 
  [
    {
      "name": "",
      "url": "500",
      "methods": {
        "action": "handleValidationException",
        "controller": "exception"
      },
      "parameters": 
      [
      ]
    },
    {
      "name": "",
      "url": "500",
      "methods": {
        "action": "handleApiException",
        "controller": "exception"
      },
      "parameters": 
      [
      ]
    }...


Here we have a basic version of a API documentation with a few lines :)

Monday, 16 September 2013

Groovy and C++ through JNA

Last week I attended a talk at Kaleidos about integrating/extending Python with C++ (Great talk by the way).

Because there's a kind of healthy competition inside Kaleidos.net I'm thinking about coding some C++ again (I probably did my last line of c++ like 10 years ago but WTF let's do this!!! :P)

When talking about integrating C++ with the JVM (as far as I know) there's only two choices JNI and JNA.

I googled for a while and I got to the point where I decided to go for JNA instead of JNI. The reason was the JNI looked more cumbersome than JNA.

First step: Create your C++ code:


Well the first step was to create my C++ code. That's easier said than done. Like I said my C++ is more than rusty. Anyway I came up with the following sample code, don't take it too seriously:

//Algorithms.h

class Algorithms {         
   
     public:
       int getMax(int*,int);
 
 };

//Algorithms.cpp

#include "Algorithms.h"    
   
extern "C" int getMax(int* numbers,int numbersLength) {
   
   Algorithms instance;     
   return instance.getMax(numbers,numbersLength);
 
}
  
int Algorithms::getMax(int* numbers,int numbersLength) {
  
   int max = 0;
  
   for (int i = 0; i < numbersLength ; i++) { 
     int next = numbers[i];
     if (next > max){
       max = next; 
     }
   }
  
   return max;
  
}

The most important detail here is to externalize the code with the 'extern "C"' part.

There's a useful tool called objdump you can use to inspect the resulting library in order to find out which functions are accesible from outside.

By the way, the c++ project is compiled using Gradle cpp plugin;)

Second step: Invoke your code!


Then I wrote the following code in the groovy console:

@Grab( 'net.java.dev.jna:jna:3.4.0' )
import com.sun.jna.Library
import com.sun.jna.Native
 
System.setProperty("jna.library.path","/pathToTheLibraryDir")
 
interface Algorithms extends Library {
    public int getMax(int[] numbers,int numbersLength);
}

def instance = Native.loadLibrary( 'cpp',Algorithms)
def numbers = [1,2,3,4] as int[]
instance.getMax(numbers,numbers.size())


Thinks to take into account.
  • The jna property is one of the three ways of telling JNA where the C++ library is. You can check the other two at the Github JNA page.
  • You can invoke the script without specifying the location of the library if you executed groovyConsole in the same dir.

Thanks to the people of MadridJUG mailing list. They saved me a lot of time.

References