Grails : Problems with Lazy List - grails

I have a web page that allows the user to enter data which will form a Master / Detail relationship when stored to my database. I submit the data to a Grails controller that binds the data in to a Command Object. As I do not know how many "detail" rows will be submitted, I am attempting to use a Lazy List to bind the detail data to. I am failing spectacularly.
My command object looks like :
String title
List testItems = LazyList.decorate(new ArrayList(),
FactoryUtils.instantiateFactory(VocabQuestion.class));
When I submit the form I get the following exception :
| Error 2013-06-04 22:42:54,068 [http-bio-8080-exec-4] ERROR errors.GrailsExceptionResolver - MissingMethodException occurred when processing request: [POST] /*****/vocabulary/save - parameters:
testItems[1].question: Q2
title: Test
testItems[0].answer: A1
testItems[0].question: Q1
testItems[0].vocabulary_test_id:
testItems[1].answer: A2
create: Create
No signature of method: vocabularytest.TestCreationCommand.propertyMissing() is applicable for argument types: () values: []
Possible solutions: propertyMissing(java.lang.String). Stacktrace follows:
Message: No signature of method: vocabularytest.TestCreationCommand.propertyMissing() is applicable for argument types: () values: []
Possible solutions: propertyMissing(java.lang.String)
Line | Method
->> 102 | <init> in vocabularytest.TestCreationCommand
This exception happens very early on in the objects lifecycle, presumeably as Grails tries to bind the data to it.
If I define my Command Object as :
String title
List testItems = [new VocabQuestion()]
and only submit 1 detail record from the form, then everything works as expected.
Where am I going wrong?
EDIT
My VocabQuestion domain class is
package vocabularytest
class VocabQuestion {
static constraints = {
}
static belongsTo = [vocabularyTest: VocabularyTest]
String question
String answer
}

I found an answer ( might not be THE answer but it works )
I used the LazyList syntax that is native in later Groovy versions as follows.
List testItems = [].withLazyDefault {new VocabQuestion()}

Related

Grails - NoSuchMethodException

I just implemented a dynamic dropdown menu based on https://stackoverflow.com/a/3771240/3691484, but now whenever I try to create an object based on the 2nd dropdown list, I get
|
java.lang.NoSuchMethodException: java.util.Set.<init>()
Error |
at java.lang.Class.getConstructor0(Class.java:2810)
Error |
at java.lang.Class.getDeclaredConstructor(Class.java:2053),
basically the same error as https://jira.grails.org/browse/GRAILS-10635. However, the solution listed in the JIRA doesn't seem to applicable to my case, as this is how my domain class is (Tag is the primary select box, subtag dynamically changes based on the tag selected)
class Tag {
static scaffolding = true
String template
String tagName
static hasMany = [subtags: Subtag]
String toString()
{
"${tagName} - ${template}"
}
static constraints = {
template(inList: ["Proposal", "Resume", "Training Manual"])
}
}
Any ideas?
I think this is fixed in recent versions of Grails. If you can still produce it with the latest 2.3.x release, please file a JIRA, but I don't think this is still a problem.

Save List of numbers in domain model error?

I am currently trying to save a List of numbers associated to a domain model in Grails and I keep getting errors. So the scenario is:
I have a Client Domain Model that is shown below and this has a HasMany relationship with a PhoneNumbers Domain model that is also shown below. There is a view where a bunch of numbers are added and then these are stored in a String [] for processing on the controller.
Client Domain:
class Client {
String name
List numbers = new ArrayList()
//This represents a message belonging to a single department
static hasMany = [numbers:PhoneNumbers]
static constraints = {
name(blank:false)
}
}
Phone Numbers Domain:
class PhoneNumbers {
String number
//This represents a message belonging to a single department
static belongsTo = [client:Client]
static constraints = {
number(blank:false)
}
}
As you can see the Client hasMany Phone Numbers and the Phone Numbers belongTo a Client. So in my controller I presumed all's I would need to do is to pass these numbers to the Domain in an Array or List and it would handle the cascade save, my controller function is below:
//This is an array of phone numbers stored in a sesson object as String []
def numbers = session.getAttribute("phoneNumbers")
def numbersConvert = numbers as List
def client = new Client(numbers: numbersConvert, name: params.name)
if (!client.save()) {
client.errors.each{
println(it)
}
}
so the controller above I would expect to take the array of numbers which I have confirmed has values and then convert that to a List. Then save the new Client data and cascade save all the numbers that that client has in the List. However this does not work and I get the error below:
GRAILS-7799: Subtype 'java.lang.String' of reloadable type com.tool.PhoneNumbers is not reloadable: may not see changes reloaded in this hierarchy (please comment on that jira)
| Error 2013-07-30 07:56:50,831 [http-bio-8080-exec-4] ERROR property.BasicPropertyAccessor - IllegalArgumentException in class: com.smstool.PhoneNumbers, getter method of property: id
| Error 2013-07-30 07:56:50,836 [http-bio-8080-exec-4] ERROR errors.GrailsExceptionResolver - IllegalArgumentException occurred when processing
object is not an instance of declaring class. Stacktrace follows:
Message: object is not an instance of declaring class
I did also try another approach to the save the data within the controller as shown below however this did not work either:
numbers.each{
def phoneNumber = new PhoneNumber(number: it).save(flush: true)
client.addToPhoneNumber(phoneNumber).save(flush: true)
}
I presume I am missing something silly and this is probably a really easy thing to do just so tired and need a little help.
Thanks in advance

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 pull object from has many

I am trying to pull all the PostOrder's out where a child instance is present
My domain is as follows:
class PostOrder {
String pOrder
Date dateCreated
Picture picture
Post posts
Video video
static hasMany = [children : Child]
}
and the method i am trying to use to get the objects is:
def getAllInOrder(Child child){
def json = PostOrder.findAllByChildren(child, sort: 'dateCreated', order: 'desc') as JSON
return json
}
I am just getting sql exceptions from this.
Any Ideas?
also these are my exceptions:
util.JDBCExceptionReporter No value specified for parameter 1
errors.GrailsExceptionResolver SQLException occurred when processing request: [GET] /FYP/profile/appPosts - parameters:
child: 1
No value specified for parameter 1. Stacktrace follows:
java.sql.SQLException: No value specified for parameter 1
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:982)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927)
at com.mysql.jdbc.PreparedStatement.checkAllParametersSet(PreparedStatement.java:2595)
at com.mysql.jdbc.PreparedStatement.fillSendPacket(PreparedStatement.java:2571)
at com.mysql.jdbc.PreparedStatement.fillSendPacket(PreparedStatement.java:2497)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2251)
at org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:96)
at org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:96)
at org.grails.datastore.gorm.GormStaticApi$_methodMissing_closure2.doCall(GormStaticApi.groovy:105)
at com.fyp.timeline.ProfileController$$ENmIB8q0.getAllInOrder(ProfileController.groovy:636)
at com.fyp.timeline.ProfileController$$ENmIB8q0.appPosts(ProfileController.groovy:624)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:680)
Try
PostOrder.findAllByChildren(child, [sort: 'dateCreated', order: 'desc'])
Although I don't think this will fix your problem entirely because in this instance, child is expected to be a list. Try this:
PostOrder.all.findAll{it.children.contains(child)}
It seems like there should be a better way to do that, maybe someone else can ring in.

Beginner Groovy

I'm following the code examples in 'The Definitive Guide to Grails' by Graeme Keith Rocher, and have come across a rather unusual stumbling block.
Essentially, 2 domain classes exist - Bookmark & Tag.
Bookmark:
class Bookmark {
static hasMany = [tags:Tag]
URL url
String title
String notes
Date dateCreated = new Date()
}
Tag:
class Tag{
static belongsTo= Bookmark
Bookmark bookmark
String name
}
I'm instructed to launch the Grails Console (is this the same as the groovy console)and create a new object as follows.
def b = new Bookmark(url: new URL('http://grails.org/'), title:'Grails', notes:'Groovy')
This results in:
Result: Bookmark : null
According to the book, GORM automatically provides an implementation of an addTag method. So I code...
b.addTag( new Tag(name: 'grails'))
Only to get whammed with the error message:
Exception thrown: No such property: b for class: ConsoleScript1
groovy.lang.MissingPropertyException: No such property: b for class: ConsoleScript1 at ConsoleScript1.run(ConsoleScript1:2)
The author hasn't accounted for this in the book. I was wondering if anyone could help me out?
Thanks.
Are you reading the 1st edition of the book? If so it's quite outdated. The add* methods have been deprecated since 0.5. It was replaced by addTo* so do this instead:
b.addToTags( new Tag(name: 'grails'))
Assuming your code example shouldn't have Bookmarks defined twice (copy and paste error?) and Tag might look like this:
class Tag {
String name
}
The groovy console is not the same as the grails console. To access the grails console, type grails console in your application directory - you should get a Java GUI app. It's possible that the example will work then because grails add some stuff to the standard Groovy.
Also, your problem isn't the addTag method, but the item b that you defined which cannot be found. Try entering the whole script into the console at once and executing it, instead of executing it line by line.

Resources