February 16, 2009

Grails auto domain object maintenance

Grails Object Relational Model GORM defines two little documented conventions: the lastUpdated and dateCreated properties. If a domain object has these properties, the GORM will automatically populate the values when the save method is called.


February 15, 2009

AJAX security in Grails

Grails makes server side response to AJAX calls as easy as a standard action in a controller but what stops your action being called out of context? How can I be sure that the caller is AJAX? I could write a custom filter but I don’t necessarily want all of my actions to be treated the same way.

So in Grails we’d just leverage a bit of built in AOP in the controller and name the action exceptions –

def beforeInterceptor = [action:this.&checkAjax,except:[‘index’,’otheraction’,’logout’]]

private Boolean checkAjax() {
return (request.getHeader(“X-Requested-With”)!=null)
}


Accessing multiple datasources in Grails

Grails has a single dataSource object which (whilst it can take the form of test, dev or prod depending upon the declared environment) can be a bit limiting. You can’t define extra data sources in the config.groovy file but you can add them as resources in grails-app\spring\resources.groovy -

import org.apache.commons.dbcp.BasicDataSource

beans = {
myDatasource1(BasicDataSource) {
driverClassName = “oracle.jdbc.driver.OracleDriver”
url = “jdbc:oracle:thin:@serv1.warwick.ac.uk:1624:dbname”
username = “me”
password = “password”
}
myDatasource2(BasicDataSource) {
driverClassName = “com.microsoft.sqlserver.jdbc.SQLServerDriver”
url = ‘jdbc:sqlserver://serv2.warwick.ac.uk:1433;databaseName=dbname;’
username = “me”
password = “password”
}}

and then we can just inject them into our controller or service just like the default dataSource -

import groovy.sql.Sql

class myController {

def myDatasource1
def myDatasource2

def myAction = {
def connection = Sql.newInstance(myDatasource2)
connection.eachRow(“select col1, col2 from myTable”) {
println it.col1 + ”, ” + it..col2
}

}


Accessing Grails dataSource directly

Grails domain objects take care of the hibernate mapping and the underlying sql but there are times that using groovy sql directly can be useful. Grails allows injection of the default data source object into a controller, job or service.

import groovy.sql.Sql

class myController {

def dataSource

def myAction = {
def connection = Sql.newInstance(dataSource)
connection.eachRow(“select col1, col2 from myTable”) {
println it.col1 + ”, ” + it..col2
}

}


February 14, 2009

Tomorrow at 6:00am

Recently I wanted to set alarm times on objects which were being instantiated at various times during the day. Basically I wanted to set a time property on each object at 6:00 am the following morning.

As the current time could be almost anything from 9-5 a simple hour addition isn’t going very far without some complex logic…

Calendar to the rescue.

def cal = Calendar.instance
todayNow = cal.time
cal.add(Calendar.DATE, +1)
cal.set(Calendar.HOUR_OF_DAY, 6)
cal.set(Calendar.MINUTE, 0)
nextMorning = cal.time


Converting between java.util.Date and java.sql.Date

Giving groovy sql a util.date to work with is a short cut to frustration (especially when using Oracle as it will complain bitterly about an invalid column type should you try to insert a value) I’ve seen examples of simpleDateFormat being used to create strings which are then passed to Oracle’s TO_DATE() but I prefer the idea of working through an intermediate long-

java.util.Date date = new java.util.Date();
long t = date.getTime();
java.sql.Date sqlDate = new java.sql.Date(t);
java.sql.Time sqlTime = new java.sql.Time(t);
java.sql.Timestamp sqlTimestamp = new java.sql.Timestamp(t);

and in groovy :)

def date = new Date()
def t = date.getTime()
def sqlDate = new java.sql.Date(t)
def sqlTime = new java.sql.Time(t)
def sqlTimestamp = new java.sql.Timestamp(t)


December 2021

Mo Tu We Th Fr Sa Su
Nov |  Today  |
      1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31      

Search this blog

Tags

Most recent comments

  • ah, better: nextMorning = new DateMidnight().toDateTime().plusDays(1).plusHours(6); by Chris May on this entry
  • Hmm, you wouldn't want to rely on that for security, since anyone can easily add an X–Requested–With… by Chris May on this entry
  • I quite like Joda–Time for this kind of thing: nextMorning = new DateTime(new DateTime().toDateMidni… by Chris May on this entry
  • Remember to set Calendar.SECOND and Calendar.MILLISECOND to 0 as well – one problem with Calendar is… by Mathew Mannion on this entry

Blog archive

Loading…
RSS2.0 Atom
Not signed in
Sign in

Powered by BlogBuilder
© MMXXI