getArtefact return NULL for the "Domain" - grails

I am new to Grails and was trying to follow with the examples in "Beginning Groovy and Grails"
When I was trying out the examples, I had the following issue:
domainClass = grailsApplication.getArtefact("Domain", domainClassName)
The domainClass was returning NULL. But when I use the following snippet, I get the correct Class.
if (!domainClass) {
def artefacts = grailsApplication.getArtefacts("Domain")
domainClass = artefacts.find {item ->
item.name == domainClassName
}
}
What is wrong with the first part? Is there anything else that I need to take care of. I downloaded the example code of the book and I haven't seen any other specific changes.
Thanks in Advance!!

Use the full path of the domain class i.e prefix the package.

The first code snippet works too, the domainClassName just needs to be fully qualified name ie packagename.domainclassname
def domainClassName = 'org.abc.Sample'
def domainClazz = grailsApplication.getArtefact('Domain', domainClassName)

Related

Common beforeUpdate method for Grails domain

I am trying to use a common beforeUpdate method by writing in BootStrap.groovy.
def init = { servletContext ->
for (domainClass in grailsApplication.domainClasses) {
if(domainClass.clazz.simpleName == domainName){
domainClass.metaClass.beforeUpdate = {
println "i am here "
def dirtyPropertyNames = this.getDirtyPropertyNames()
println(dirtyPropertyNames)
if(dirtyPropertyNames != null && dirtyPropertyNames.size() > 0) {
for (dirtyPropertyName in dirtyPropertyNames) {
def oldValue = (this.getPersistentValue((dirtyPropertyName)))
def newValue = (this."${dirtyPropertyName}")
}
}
}
}
}
}
But I cannot use this.getdirtyPropertyNames() as it gives an error.
groovy.lang.MissingMethodException: No signature of method:
If it's in the domain itself, this.getDirtyPropertyNames() works fine.
I tried using domainClass.getDirtyPropertyNames() too but it still gives an error.
I am using Grails 4.
I am not sure if you are asking how to accomplish what you want, or why you are getting the error you are getting.
If you want to know how to accomplish what you want, I would use an event listener instead of metaprogramming the method. There are lots of examples out there, https://github.com/jeffbrown/gorm-events-demo/blob/261f25652e5fead8563ed83f7903e52dfb37fb40/src/main/groovy/gorm/events/demo/listener/AuditListener.groovy#L22-L26 is one.
If you are asking why you are getting the error you are getting, the reason is this references the instance of BootStrap, not the instance of your domain class. If you really really want to use the dynamic metaprogramming approach (you shouldn't) then you can solve that particular part of the problem by referencing delegate instead of this.
I hope that helps.

Grails: Understanding groovy DomainClass.properties

I'm having a hard time finding information about grails functionality:
DomainClass.properties = params
In my particular case, I have these classes:
class parameterType = {
String name
String desc
static hasMany = [codes : parameterCode]
...
}
class parameterCode = {
String code
String desc
static belongsTo = [parameterType : parameterType]
}
My parameterType/edit.gsp has name, desc and an html table with its list of parameterCodes
At first, I had some variation of the scaffolded controller on the 'update' action. That (I know its wrong but it was a beginners code) it first deleted all the parameterCodes and then reassociated them (or recreated them).
With Ajax I was sending the data in this format:
id=1234&name=paramName&desc=paramDesc&codes[0].code=code1&codes[0].desc=codeDesc1&codes[1].code=code2&codes[1].desc=codeDesc2
And in the controller I had this:
def parameterTypeInstance = ParameterType.get(params.id)
def toDelete = parameterTypeInstance.parameterCodes
parameterTypeInstance.parameterCodes = []
toDelete.each{it.delete(flush: true)}
//And this "magic" line reassociated all the properties in parameterType And Created his parameterCodes in the data base:
parameterTypeInstance.properties = params
I honestly don't how it works, and I just wanted to know if there's a way of doing the same thing without having to previously delete the associated parameterCodes.
Cheers
**Update:**
I just found what I was looking for in these links:
http://www.2paths.com/2009/10/01/one-to-many-relationships-in-grails-forms/
http://omarello.com/2010/08/grails-one-to-many-dynamic-forms/
But I had another error.
These talks about LazyList and decorate(), so I just added the next lines to my ParameterType Class:
def List getExpandableCodeList() {
return LazyList.decorate(codes,FactoryUtils.instantiateFactory(ParameterCode.class))
}
But when I do this in my controller update:
parameterTypeInstance.properties = params
I'm getting this error:
groovy.lang.MissingMethodException: No signature of method: static org.apache.commons.collections.list.LazyList.decorate() is applicable for argument types: (org.hibernate.collection.PersistentSet, org.apache.commons.collections.functors.InstantiateFactory) values: [[cE - EE, cA - AA, cC - CC, cD - DD], org.apache.commons.collections.functors.InstantiateFactory#dd768d]
The data is being recieved in the controller this way:
expandableCodeList[0].desc: AA
expandableCodeList[3].code: cE
expandableCodeList[3].id: 35073
expandableCodeList[1].id: 35076
expandableCodeList[0].code: cA
expandableCodeList[2].code: cD
expandableCodeList[1].desc: CC
expandableCodeList[0].id: 35080
expandableCodeList[3].desc: EE
expandableCodeList[2].id: 35075
Any hints on what I'm doing wrong? should I be sending the data in another format?
Any help would be much appreciated. Thanks.
If I not in error .properties is a method added by groovy to the java.lang.Object , look to this javaodc
There is many way to do what you want to do. Please look to grails documentation on data binding .
For example you can do
bindData(parameterTypeInstance, params, [exclude: parameterTypeInstance.parameterCodes])
look here for more info on bindData
Ok, I'm settings this as an answer:
http://www.2paths.com/2009/10/01/one-to-many-relationships-in-grails-forms/
At last, lazylist and the decorate() method was what I was looking for. Sadly it takes to take the child collection as a list and it carries out a lot of other issues for me. But its a good solution if anyone needs to make a view with the parent and its child objects the "simple" way.

Grails GORM auto update issue

Updated post:
In a Controller if I do this:
def obj = new Test(name:"lol")
obj.save(flush:true)
obj.name = "lol2"
//a singleton service with nothing to do with obj
testService.dostuff()
/*
"obj" gets persisted to the database right here
even before the next println
*/
println "done"
Can anyone please explain me why is this happening with Grails 1.3.7 and not with Grails 2? What is the reason?
I know I could use discard() and basically restructure the code but I am interested in what and why is happening behind the scenes. Thanks!
Old post:
I have a test Grails application. I have one domain class test.Test:
package test
class Test {
String name
static constraints = {}
}
Also I have a service test.TestService:
package test
class TestService {
static scope = "singleton"
static transactional = true
def dostuff() {
println "test service was called"
}
}
And one controller test.TestController:
package test
class TestController {
def testService
def index = {
def obj = new Test(name:"lol")
obj.save(flush:true)
obj.name = "lol2"
testService.dostuff()
println "done"
}
}
So what I do:
Create a domain object
Change one of it's properties
Call a singleton service method
What I would expect:
Nothing gets persisted to the db unless I call obj.save()
What happens instead:
Right after the service call Grails will do an update query to the database.
I have tried the following configuration from this url: http://grails.1312388.n4.nabble.com/Turn-off-autosave-in-gorm-td1378113.html
hibernate.flush.mode="manual"
But it didn't help.
I have tested it with Grails 1.3.7, Grails 2.0.3 does not have this issue.
Could anyone please give me a bit more information on what is exactly going on? It seems like the current session has to be terminated because of the service call and because the object is dirty it is getting automatically persisted to the database after the service call. What I don't understand that even with the manual flush mode configuration in Hibernate does not help.
Thanks in advance!
I'm not sure what about that thread you linked to made you think it would work. They all said it wouldn't work, the ticket created has been closed as won't fix. The solution here is to use discard() as the thread stated.

Grails Blurb Plugin - Unable to resolve class Blurb

I'm working through the book Grails: A Quick-Start Guide and have come upon a problem. The book has me install the Blurb plugin, which seems to work, but states that we will be using it as if it were a domain class and using it a pre-existing controller. The code that I am to add to the controller looks like this
def blurb = Blurb.findByName("custom_${event.id}" )
if (!blurb){
blurb = new Blurb(name:"custom_${event.id}" , content:"" ).save()
}
When I do this I receive the same error in the IDE and the run output
'unable to resolve class Blurb' and I am directed specifically to this line blurb = new Blurb(name:"custom_${event.id}" , content:"" ).save()
Can anyone tell me what might be going wrong? I'm assuming the plugin is installed properly because if I try to access it's controller/action directly 'http://localhost:8080/TekDays/blurb/create' the plugin's provided view renders properly.
Thanks!
--
For reference I am using STS / Grails 1.3.7
Update 2011.05.12 7:45AM CST
I've attached a screenshot showing my project from the STS interface to show how my project is laid out in the event that it is package related as Burt indicated. The issue though is I'm not sure how do to the import statement so perhaps that screenshot will help.
Here is the current code in the Dashboard Controller.
package tekdays
class DashboardController {
...
}
I've tried adding the following lines per Burt's suggestion, but I obviously don't have it right
package tekdays
package my.package <--unexpected token: package
class DashboardController {
I tried changing out my with tekdays and default and both yield the same result.
Am I doing that wrong?
Thanks!
The Blurb class is in the default package, so if your controller is in a package you'll need to use a Groovy trick to access it:
package my.package
import Blurb as Blurb
class MyController {
def action = {
def blurb = Blurb.findByName("custom_${event.id}" )
if (!blurb) {
blurb = new Blurb(name:"custom_${event.id}" , content:"" ).save()
}
}
}

grails findAll() method

I am using the findAll() SQL-like method:
MyDomainClass.findAll("firstname='George' and lastname='kuo' and username='kjlop'"),
but I have got problem:
when value starts with number(for example,when age='2poj') it throws an exception
I use grails 1.3.2 and gorm-hbase 0.2.4 plugin and in my domain classes fields have String type.
Here is the Stack Trace:
expecting token in range: '0'..'9', found 'p'
at org.grails.hbase.ghql.LexerRules.nextToken(LexerRules.java:125)
at org.grails.hbase.finders.QueryStringTokenizer.tokenize(QueryStringTokenizer.groovy:59)
at org.grails.hbase.finders.TokenizerStrategy$tokenize.call(Unknown Source)
//---------
I wonder is there any way in groovy change findAll() method work ?
If anybody know solution please help.
Thanks in advance.
You should be able to run a dynamic finder method on the domain object to achieve what you need.
Example:
MyDomainClass.findAllByFirstnameAndAge('Dan', 25)
This works for all data types and enums.
You can try like the Grails example:
MyDomainClass.findAll("from DomainTable as b where b.firstname=:firstname and b.age=:age", [firstname:'Dan Brown', age: 25]
Notice: I don't know if you mistype it, but '25' is a string, so that it can't be age='25'
EDIT:
I don't know how this doesn't work, but in case you want to find with multiple properties, you should use createCriteria().
def c = MyDomainClass.createCriteria()
def results = c.list {
like("firstName", "George%")
like("age", "25");
}
EDIT2: Sorry, createCriteria is not supported by hbase plugin. Based on your condition, I think it's suitable to try DynamicFinderFilter (with approriate import).
// all books written by Dan Brown or J K Rowling
DynamicFinderFilter filterList = new FinderFilterList(Operator.OR)
DynamicFinderFilter filter1 = new Filter('author', 'Dan Brown')
filterList.addFilter(filter1)
DynamicFinderFilter filter2 = new Filter('author', 'J K Rowling')
filterList.addFilter(filter12)
results = Book.findAll(filterList)
The complete example can be find in the plugin page.

Resources