Article
0 comment

Playing around with services in grails console

Suppose you have a grails project and have witten a service doing some database magic to pull together data. Now suppose the very unlikely case that it’s not running that smooth than you thought. To expel the black magic you probably would like to use the grails console to play around with your domain classes and services. Using a service is as simple as importing the domain class and using it:

import myproject.Domainclass
def instance=Domainclass.get(3)
println instance.id+"\t"+instance.name

The service classes however are not that accessible to manipulation. You need to request the service bean instance by name from the application context named ctx:

def mcs=ctx.getBean("myCoolService")
def allThings=mcs.getAllThings()

Remember to use the (lowercase) instance name when calling getBean() just as it would be injected into your controller:

class GraphController {
    MyCoolService myCoolService
}

Pulling the strings together you can do more complex tests:

import myproject.Domainclass

def mcs=ctx.getBean("myCoolService")
def instance=Domainclass.get(3)
println instance.id+"\t"+instance.name
println "-----------------------------------------"
def allThings=mcs.getAllThings()
allThings.each { n -> println n.id+"\t"+n.thingstype+"\t\t"+n.name }

Hope that helps. As always: in case of questions or corrections / additions please leave a comment :)

Article
6 comments

Adding assets in Grails 3

When using modern web development technologies, you often come across frameworks or libraries which use additional resources apart from css stylesheets, images and javascript. One such example is Font Awesome, which needs sone font files, located in the /fonts subdirectory of the unzipped package. In Grails 2 lazy coders would put this directory in the /wep-app folder. In Grails 3 you should (!) use the asset pipeline for these files to and here are two ways that work:

  1. Simply put the files into the grails-app/assets/stylesheets folder. This is not a very elegant way nor is it the intended way to use the asset pipeline.
  2. Put the fonts directory parallel to stylesheets, images and javascript into the grails-app/assets/ folder. For the asset pipeline to know the new directory, specify it in the build.gradle file:
    assets {
        minifyJs = true
        minifyCss = true
        includes = ["fonts/*"]
    }
    

    Last thing to do is to patche the font file paths in the font-awesome.css and/or font-awesome.min.css file. Just remove the “../fonts/” part of the url() path, so they all look like this:

    font-family: 'FontAwesome';
    src: url('fontawesome-webfont.eot?v=4.3.0');
    

    Thats all.

This post by David Estes put me on the right track, since the official documentation doesn’t mention Grails 3 issues. Thanks David!

Article
0 comment

Starting with Grails and Neo4J

Since Stefan Armbruster is a bit short of time to update the documentation of the Neo4J plugin for Grails I thought I start a short series of postings describing my progress in using Neo4J as a graph database for a Grails based application. I will use the REST API of Neo4J and not the embedded version.
Like always it starts with additions to grails-app/conf/BuildConfig.groovy. First we need to add the Neo4J maven repository to the repositories section:
[pyg l=”groovy”]repositories {
// …
mavenRepo ‘http://m2.neo4j.org/content/repositories/releases/’
} [/pyg]
Then I set my versions:

[pyg]neo4jVersion=”2.0.3″
neo4jRestVersion=”1.9″[/pyg]

 
Now we need to declare the dependencies:
[pyg]dependencies {
//…
compile “org.neo4j:neo4j-community:$neo4jVersion”
compile group:”org.neo4j”, name:”neo4j-graphviz”, version: neo4jVersion
runtime group:”org.neo4j”, name:”neo4j-shell”, version: neo4jVersion
runtime “org.neo4j:neo4j-rest-graphdb:$neo4jRestVersion”
}[/pyg]

En bloc that looks like this (caveat: this code block also installs the angularjs plugin!):

[pyg]grails.project.dependency.resolver = “maven” // or ivy
grails.project.dependency.resolution = {
// inherit Grails’ default dependencies
inherits(“global”) {
}
log “error”
checksums true
legacyResolve false

repositories {
    inherits true // Whether to inherit repository definitions from plugins

    grailsPlugins()
    grailsHome()
    mavenLocal()
    grailsCentral()
    mavenCentral()
    // uncomment these (or add new ones) to enable
    // remote dependency resolution from public Maven repositories
    //mavenRepo "http://repository.codehaus.org"
    //mavenRepo "http://download.java.net/maven/2/"
    //mavenRepo "http://repository.jboss.com/maven2/"
    mavenRepo 'http://m2.neo4j.org/content/repositories/releases/'
}

neo4jVersion="2.0.3"
neo4jRestVersion="1.9"

dependencies {
    // runtime 'mysql:mysql-connector-java:5.1.29'
    // runtime 'org.postgresql:postgresql:9.3-1101-jdbc41'
    test "org.grails:grails-datastore-test-support:1.0.2-grails-2.4"

    compile "org.neo4j:neo4j-community:$neo4jVersion"
    compile group:"org.neo4j", name:"neo4j-graphviz", version: neo4jVersion
    runtime group:"org.neo4j", name:"neo4j-shell", version: neo4jVersion
    runtime "org.neo4j:neo4j-rest-graphdb:$neo4jRestVersion"
}

plugins {
    // plugins for the build system only
    build ":tomcat:7.0.55"

    // plugins for the compile step
    compile ":scaffolding:2.1.2"
    compile ':cache:1.1.8'
    compile ":asset-pipeline:1.9.9"

    compile ":angularjs:1.0.0"

    // plugins needed at runtime but not for compilation
    runtime ":hibernate4:4.3.6.1" // or ":hibernate:3.6.10.18"
    runtime ":database-migration:1.4.0"
    runtime ":jquery:1.11.1"

}

}[/pyg]

This will get you started with the integration itself. In the next posting I will show, how to access data in the Neo4j database from a controller.