Wednesday, October 2, 2013

Groovy/Grails and Oracle Spatial (Part 2)

In part one we discussed building out a service class to perform our distance search. Now in part two, we'll build out the rest of the grails framework namely a GSP page to capture user input, a controller to process the form, and a GSP page to render the results as an html table.

Let's start with a controller to test that we can pass user input into our service class SearchByDistance.groovy. First, we'll want to inject the Oracle dataSource into our controller. To do this we need to edit our DataSource.groovy file in the conf folder.

dataSource {
pooled = true
driverClassName = "oracle.jdbc.OracleDriver"
dialect = "org.hibernate.dialect.Oracle10gDialect"
hibernate {
cache.use_second_level_cache = true
cache.use_query_cache = true
cache.provider_class = 'net.sf.ehcache.hibernate.EhCacheProvider'
// environment specific settings
environments {
development {
dataSource {
url = "jdbc:oracle:thin:@myserver:1521:MYINSTANCE"
schema = "MYSCHEMA"
username = "myuser"
password = "mypassword"
// other environments to follow...

Now we can inject this into the controller by simply defining a dataSource. In the controller we also define a list action. The list action grabs user submitted params which we'll print out for debugging purposes, followed by submitting those parameters to our SearchByDistance service. Lastly we'll return a list of our domain object results by default to the list.gsp page.

package osmcreporter

class DailySummaryMappingController {

static allowedMethods = []

def dataSource // the groovy "dataSource" is auto-injected

def index = {
redirect(action: "list", params: params)

def list = {
println "params: " + params.sensor + "," + params.transectid + "," + params.distance
def dsrs = new SearchByDistanceService()
def observations = dsrs.serviceMethod(params.sensor, params.transectid, params.distance)
[ observations: observations] // return the results as model


No comments: