A couple days ago I received the Grails Persistence with GORM and GSQL by Robert Fisher published by APress http://apress.com/book/view/1430219262.
At first I was surprised that it was a total of 156 pages (I was pleased that I didn't have to work my way through yet another 700 page book).
As the title indicates this is focused on one of the greatest assets of Grails, GORM. When working with GORM, you really need examples and options to help you with common data query and dataset organization tasks, this book provides that.
In a nutshell, if you are working on a Grails project, this book will provide you with a concise reference with great code examples of valuable GORM features that you will refer back to.
Showing posts with label grails. Show all posts
Showing posts with label grails. Show all posts
Tuesday, May 5, 2009
Wednesday, August 27, 2008
Acegi and Groovy Class Packages
With all of the tutorials and examples for Grails there is often no reference to packaging your Groovy classes.
So, I decided to convert all of my project into packages including Acegi.
Using IntelliJ 7.04 & JetGroovy plugin v1.6.18593, I was able to refactor most of my classes without issue. Service classes required me to add the imports manually... no problem there. Of course its odd that I had to manually add imports to java.util.*
Acegi classes required to add the package to the class declaration within grails-app/conf/SecurityConfig.groovy
loginUserDomainClass = "com.mycorp.projname.security.User"
authorityDomainClass = "com.mycorp.projname.security.Role"
requestMapClass = "com.mycorp.projname.security.Requestmap"
(thanks to Burt Beckwith)
So, I decided to convert all of my project into packages including Acegi.
Using IntelliJ 7.04 & JetGroovy plugin v1.6.18593, I was able to refactor most of my classes without issue. Service classes required me to add the imports manually... no problem there. Of course its odd that I had to manually add imports to java.util.*
Acegi classes required to add the package to the class declaration within grails-app/conf/SecurityConfig.groovy
loginUserDomainClass = "com.mycorp.projname.security.User"
authorityDomainClass = "com.mycorp.projname.security.Role"
requestMapClass = "com.mycorp.projname.security.Requestmap"
(thanks to Burt Beckwith)
Wednesday, May 14, 2008
Redirecting from index.gsp
To redirect to an initial page from the index.gsp
1. Create a TagLib file
grails-app\taglib\MyAppTagLib.groovy
2. Define a method that routes to your first page
class MyAppTagLib {
def redirectMainPage = {
response.sendRedirect("${request.contextPath}/myController/welcomePage/")
}
}
3. Change your index.gsp to contain only:
<g:redirectMainPage/>
Note: One advantage to this approach is that you have a coding opportunity over simply mapping the "/" url.
For example, if you want to provide features or data based on a specific user's login id.
1. Create a TagLib file
grails-app\taglib\MyAppTagLib.groovy
2. Define a method that routes to your first page
class MyAppTagLib {
def redirectMainPage = {
response.sendRedirect("${request.contextPath}/myController/welcomePage/")
}
}
3. Change your index.gsp to contain only:
<g:redirectMainPage/>
Note: One advantage to this approach is that you have a coding opportunity over simply mapping the "/" url.
For example, if you want to provide features or data based on a specific user's login id.
Thursday, May 8, 2008
Composite Key in Domain Class - round 2
Still tinkering with the composite key issues.
I changed my previous approach a bit, I now define my own getId and setId methods to essentially serialize and deserialize the the key fields strung together
Created a delimiter
static final String delimiter = '~@'
Defined (overriding default) accessors:
def getIdAsMap() {
["fieldOne": fieldOne, "fieldTwo": fieldTwo]
}
def getId() {
fieldOne + delimiter + fieldTwo
}
def setId(String inString) {
def fieldList = new ArrayList()
def items = inString.split(delimiter)
items.each { fieldList.add (it) }
this.setFieldOne (fieldList[0])
this.setFieldTwo (fieldList[1])
getIdAsMap()
}
Its a little clunky but when you can pass around the map, use getIdAsMap(), in other cases the String is available with myclass.id
Inserting new rows requires a little tweak, by default with version: false set, GORM will try to perform a sql update on new records. To fix this, add insert:true to your save method
myClass.save(insert: true)
I changed my previous approach a bit, I now define my own getId and setId methods to essentially serialize and deserialize the the key fields strung together
Created a delimiter
static final String delimiter = '~@'
Defined (overriding default) accessors:
def getIdAsMap() {
["fieldOne": fieldOne, "fieldTwo": fieldTwo]
}
def getId() {
fieldOne + delimiter + fieldTwo
}
def setId(String inString) {
def fieldList = new ArrayList()
def items = inString.split(delimiter)
items.each { fieldList.add (it) }
this.setFieldOne (fieldList[0])
this.setFieldTwo (fieldList[1])
getIdAsMap()
}
Its a little clunky but when you can pass around the map, use getIdAsMap(), in other cases the String is available with myclass.id
Inserting new rows requires a little tweak, by default with version: false set, GORM will try to perform a sql update on new records. To fix this, add insert:true to your save method
myClass.save(insert: true)
UI changes - on the fly?
I love the rapid feedback from Grails, but too much of a good thing...
I was changing some of the field presentation from a <input to <g:select and it was not showing up on the page.
So:
Refreshed the browser page - nope
Shut down the app, at the command prompt typed 'grails clean', restarted - negative
Finally I closed the browser and reopened it, and viola!
I was changing some of the field presentation from a <input to <g:select and it was not showing up on the page.
So:
Refreshed the browser page - nope
Shut down the app, at the command prompt typed 'grails clean', restarted - negative
Finally I closed the browser and reopened it, and viola!
Tuesday, May 6, 2008
Composite Key in Domain Class
Using the default id column in the database works great. However, if you are facing a "legacy" database design and unable to alter the table definition, you have a bit of work to do.
Defining your domain class:
1. The class must implement Serializable
class MyClass implements Serializable {
...
static mapping = {
table "my_table"
version false
id composite: ["myFieldOne","myFieldTwo"]
...
2. The scaffolding will by default setup all communication via the id column. The id column in this case is a serialized value. However, there is an issue with Grails v1.0.2 that does not deserialize the value properly. So, for now you have to work around using the primary key:
3. Add new Method to domain class - define a map of the values
def getPK ( ) {
["myFieldOne":myFieldOne, "myFieldTwo":myFieldTwo]
}
4. List view - Add a link on the page to show details for the current record
<span class="actionButton">
<g:link action="show" params="${myClass.getPK()}">Detail </g:link>
</span>
NOTE: "id=" was changed to "params=" and you are passing in the map returned from myClass.getPK()
5. MyClassController.groovy - modify the retrieval code in the show method
def show = {
def myClass = MyClass.get( new MyClass(params) ) //here are your inbound params
if(!myClass) {
flash.message = "MyClass not found with ${params}"
redirect(action:list)
}
else { return [ myClass: myClass] }
}
Defining your domain class:
1. The class must implement Serializable
class MyClass implements Serializable {
...
static mapping = {
table "my_table"
version false
id composite: ["myFieldOne","myFieldTwo"]
...
2. The scaffolding will by default setup all communication via the id column. The id column in this case is a serialized value. However, there is an issue with Grails v1.0.2 that does not deserialize the value properly. So, for now you have to work around using the primary key:
3. Add new Method to domain class - define a map of the values
def getPK ( ) {
["myFieldOne":myFieldOne, "myFieldTwo":myFieldTwo]
}
4. List view - Add a link on the page to show details for the current record
<span class="actionButton">
<g:link action="show" params="${myClass.getPK()}">Detail </g:link>
</span>
NOTE: "id=" was changed to "params=" and you are passing in the map returned from myClass.getPK()
5. MyClassController.groovy - modify the retrieval code in the show method
def show = {
def myClass = MyClass.get( new MyClass(params) ) //here are your inbound params
if(!myClass) {
flash.message = "MyClass not found with ${params}"
redirect(action:list)
}
else { return [ myClass: myClass] }
}
Tuesday, April 22, 2008
Grails IntelliJ Plugin
The Grails plugin for IntelliJ Idea 7.0 was updated to version 1.5. I was using the previous versions and they were troublesome. The 1.5 release seems to be working well so far.
Plugin Site: http://www.jetbrains.com/idea/features/groovy_grails.html
One point of note, I could not find a way to perform "grails clean" within IntelliJ so I have to have a command prompt open to clean the project now and again when things don't seem to be behaving as expected. Sometimes you have to clean when one of your domain classes changes significantly.
Plugin Site: http://www.jetbrains.com/idea/features/groovy_grails.html
One point of note, I could not find a way to perform "grails clean" within IntelliJ so I have to have a command prompt open to clean the project now and again when things don't seem to be behaving as expected. Sometimes you have to clean when one of your domain classes changes significantly.
New Grails User
A friend of mine, Ken Rimple, recently turned me onto Groovy on Grails. When I first heard of it, I wasn't interested in learning another something on something language/framework pair. But after a quick spin through the tutorial I was hooked.
The best thing about it is that its all based in Java and deploys as a war on your favorite web server. As a matter of fact it builds upon many of the top Java technologies such as Hibernate, Spring, log4J, JUnit, Jetty, HSQL, Quartz, Sitemesh and ANT.
Now that I have my prototype ready, the devil will be in the details to get it finely polished.
The best thing about it is that its all based in Java and deploys as a war on your favorite web server. As a matter of fact it builds upon many of the top Java technologies such as Hibernate, Spring, log4J, JUnit, Jetty, HSQL, Quartz, Sitemesh and ANT.
Now that I have my prototype ready, the devil will be in the details to get it finely polished.
- Ken Rimple's Blog: http://www.rimple.com/tech/
- Groovy Site: http://groovy.codehaus.org/
- Grails Site: http://grails.codehaus.org/
Subscribe to:
Posts (Atom)