3

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.

Johnny C.
  • 368
  • 1
  • 5
  • 20

2 Answers2

0

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

Fabiano Taioli
  • 5,270
  • 1
  • 35
  • 49
  • Thank you, @Fabiano, but I was already aware of the getProperties() from java.lang.Object. And I did check the data binding documentation from Grails, unfortunatelly they're not very descriptives when it comes to editing (updating, creating, deleting) a collection of child objects automatically. I was not aware of bindData though, I'll give it a look, but I was looking something like this that I just found: http://www.2paths.com/2009/10/01/one-to-many-relationships-in-grails-forms/ – Johnny C. Feb 28 '13 at 19:24
  • Another thing is that I think that bindData is the same as the default Domain.properties = params assignment but with a set of very useful functionalities that I can't use for what I want now. I think the link I posted in the previous comment is the solution, but I'm getting another error that I don't know if I can post it here or create a new question. Thanks anyway. – Johnny C. Feb 28 '13 at 19:40
0

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.

Johnny C.
  • 368
  • 1
  • 5
  • 20