changeSet(author: "name", id: "my-id") { renameColumn(tableName: "table-name", oldColumnName: "old-name", newColumnName: "new-name") }
Thursday, March 27, 2014
GRAILS Liquibase migration - Rename Column
Tuesday, March 25, 2014
GRAILS / GROOVY -- Merging / combining sublist into single list
Using flatten we can easily merge sublists into a single list.
def sublists = [['a','b','c'],['d','e','f']] println sublists.flatten() // prints [a, b, c, d, e, f]
Tuesday, March 18, 2014
GRAILS spring security authentication success event
There are many times when we would like to do something when a user has successfully logged in, like putting a log entry, resetting failed counters etc. Below is a snippet of code which does something similar. Place this piece of code in your grails Config.groovy file.
grails.plugins.springsecurity.useSecurityEventListener = true grails.plugins.springsecurity.onInteractiveAuthenticationSuccessEvent = { e, appCtx -> println "User " + appCtx.springSecurityService.principal.id + " has successfully logged in" ... ... }
GRAILS Configuration -- Different / External Config Files
External config or extra configuration files could be added your grails config.groovy file. Just replace the file names and place this piece of code right at the top of your config.groovy file.
def customConfigLocations = [] if (new File("${userHome}/.myConfig/global-config.groovy").exists()) customConfigLocations.add("file:${userHome}/.myConfig/global-config.groovy") if (new File("${userHome}/.myConfig/${appName}-config.groovy").exists()) customConfigLocations.add("file:${userHome}/.myConfig/${appName}-config.groovy") if (customConfigLocations.empty) { println("No external configuration available......") }else { grails.config.locations = customConfigLocations println("loading configuration from: :${grails.config.locations}") }
GRAILS forcing file download
Put the below code in your controller to force download a file in grails.
class DownloadController { def downloadFile() { InputStream contentStream try { def file = new File("") response.setHeader "Content-disposition", "attachment; filename=filename-with-extension" response.setHeader("Content-Length", "file-size") response.setContentType("file-mime-type") contentStream = file.newInputStream() response.outputStream << contentStream webRequest.renderView = false } finally { IOUtils.closeQuietly(contentStream) } } }
GRAILS shared / common custom validator
We can define our custom validation logic in config.groovy like below.
The we use it in different classes like below
def dobLowerLimit = Date.parse('MM/dd/yyyy','01/01/1900') grails.gorm.default.constraints = { birthDateValidator( validator: { value, obj -> if (value) { if (value < dobLowerLimit) { return ['past.date.error'] } else if (value > new Date()) { return ['future.date.error'] } } } ) }
The we use it in different classes like below
class Employee { Date birthDate static constraints = { birthDate nullable: false, blank: false, shared: "birthDateValidator" } }
class Customer { Date birthDate static constraints = { birthDate nullable: false, blank: false, shared: "birthDateValidator" } }
GRAILS Injecting services or classes into groovy source
Assuming you have below service class in your grails service(grails-app/services) directory.
And we also have a groovy class under the src(src/groovy) directory and we wanted to inject MyService in this class like below.
Finally, we put our injection logic in resources.groovy(grails-app/conf/spring/resources.groovy)
class MyService { def printSomething() { println "Hello World!!!" } }
And we also have a groovy class under the src(src/groovy) directory and we wanted to inject MyService in this class like below.
class HelloWorld { MyService myService def printHelloWorld() { myService.printSomething(); } }
Finally, we put our injection logic in resources.groovy(grails-app/conf/spring/resources.groovy)
import package.HelloWorld beans = { helloWorld(HelloWorld) { myService = ref("myService") } }
Subscribe to:
Posts (Atom)