tiistai 23. marraskuuta 2010
Regular SQL query result mapped to Grails domain class
Our domain class:
package fi.company.domain
class Product {
String name
int price
}
Service that uses sql
import org.hibernate.SessionFactory
import org.hibernate.Session
import org.hibernate.SQLQuery
class ProductSearchService {
SessionFactory sessionFactory
boolean transactional = false
List<Product> searchProducts(Map params) {
Session session = sessionFactory.getCurrentSession()
SQLQuery sqlQuery = session.createSQLQuery("SELECT * FROM product WHERE ...")
sqlQuery.addEntity(fi.company.domain.Product.class)
return sqlQuery.list()
}
}
maanantai 9. elokuuta 2010
Delete records with Hibernate Criteria Builder
def c = Agency.createCriteria().list {Source: http://stackoverflow.com/questions/2232621/how-do-you-bulk-delete-records-in-grails-gorm
eq("agency", "XXX")
}*.delete()
tiistai 22. kesäkuuta 2010
Check if JSON object is undefined
function(json){
if (json.myObject){
// do stuff with myObject
} else {
// do not do stuff with myObject
}
}
Accessing Taglib from Service
From the service:
class MyService {
def grailsApplication
def method() {
def g = grailsApplication.mainContext.getBean('org.codehaus.groovy.grails.plugins.web.taglib.ApplicationTagLib')
}
}
...now g can be used just as in GSP files.
Mystical message codes for numerical validation constraints
maxSize -> class.prop.maxSize.exceeded
size -> class.prop.size.toosmall & class.prop.size.toobig
min -> class.prop.min.notmet
max -> class.prop.max.exceeded
range -> class.prop.range.invalid (only if validated value is null), class.prop.size.toosmall , class.prop.size.toobig
perjantai 9. huhtikuuta 2010
Hibernate-plugin refuses to install: A workaround
Failed to install plugin [hibernate-1.2.2]. Plugin has missing JAR dependencies.
How to fix this? No clue, but a workaround is to clear the ~/.ivy2 directory.
tiistai 6. huhtikuuta 2010
Using session-scoped services in Grails' taglib
Using services with session scope in tag libraries does not work in Grails, as per version 1.2.2. There's a fix promised in 1.3.0, but while waiting for it to be released, here's a simple workaround:
import org.springframework.context.ApplicationContextAware
import org.springframework.context.ApplicationContext
class DefaultTagLib implements ApplicationContextAware {
ApplicationContext applicationContext
def someTag = {attrs, body ->
applicationContext.documentService.doSomething()
}
}
maanantai 22. maaliskuuta 2010
Setting environment variables for Grails on OSX
The best place to store environment variables on OS X is /etc/launchd.conf. The contents of that file are accessible by console applications, and also (unlike ~/.bashrc and the like) by desktop applications.
Paths should be defined in /etc/paths.
In /etc/launchd.conf:
setenv JAVA_HOME /Library/Java/Home
setenv GRAILS_HOME /Developer/grails-1.2.1
In /etc/paths:
/Developer/grails-1.2.1/bin
perjantai 19. maaliskuuta 2010
Max-width / max-height in IE
IE does not play nice with max-width and max-height. Here's how to handle it...
/* real browsers */
max-width: 500px;
max-height: 500px;
/* IE */
width: expression(this.width > 499 ? "500px" : "auto");
height: expression(this.height > 499 ? "500px" : "auto");
torstai 18. maaliskuuta 2010
Java VM options for Grails usage
When developing large(ish) Grails applications, you probably will need to adjust your heap and permgen sizes. Here be the Options!
-Xmx512m -Xms512m -XX:MaxPermSize=128M
tiistai 9. maaliskuuta 2010
How to protect everything from XSS
For example, if the malicious user changes his/her name to <script src="evilscript.js">giveMeAdminRights(); </script> , using user.name.encodeAsHTML() will prevent the script from being run when the admin views the user's profile.
Also the fieldValue tag will do the same, like this: <g:fieldValue bean="${user}" field="name"/ >
sunnuntai 7. maaliskuuta 2010
The IE 7 z-index problem
Ok, this one is not Grails, but still it took a painful while to get my head around: The IE 7 z-index problem.
lauantai 6. maaliskuuta 2010
ApplicationContext
Accessing application context
import org.springframework.context.ApplicationContextAware
import org.springframework.beans.BeansException
import org.springframework.context.ApplicationContext
class MyServiceOrController
implements ApplicationContextAware {
def applicationContext
def void setApplicationContext(
ApplicationContext appctx) throws BeansException {
applicationContext = appctx
}
}
keskiviikko 3. maaliskuuta 2010
Running Grails commands in specific environment
Every now and then there's a need to run Grails commands from command prompt, in a specific environment. For example, to create a WAR in demo environment:
grails -Dgrails.env=demo war
tiistai 2. maaliskuuta 2010
Importing stuff in GSP files
Sometimes (pretty often, actually) there's a need for importing classes inside GSP files. Here's how the syntax goes:
<%@ page import="fi.company.project.enums.ThingType" %>