Problem with RichUI star rating - grails

I'm devolping a rating system with RichUI plugin for Grails. First I had the following code:
class RatingController {
def springSecurityService
static scaffold = true
def rate = {
def rating = params.rating
def artist = Artist.get( params.id )
def currentUser = currentUser()
currentUser.addToRatings(new Rating(artist:artist, rating:rating)).save()
render(template: "/artist/rate", model: [artist: artist, rating: rating])
}
private currentUser(){
return User.get(springSecurityService.principal.id)
}
}
which worked fine, but the problem with this code is that, if the user updates the rating for one artist it would always create a new Rating instance instead of just update the rating value.
So I came up with the following code:
class RatingController {
def springSecurityService
static scaffold = true
def rate = {
def rating = params.rating
def artist = Artist.get( params.id )
def currentUser = currentUser()
if(! currentUser.ratings.artist.contains(artist)){
currentUser.addToRatings(new Rating(artist:artist, rating:rating)).save()
render(template: "/artist/rate", model: [artist: artist, rating: rating])
}
else{
currentUser.ratings.find{it.artist==artist}.rating = rating
currentUser.save()
render(template: "/artist/rate", model: [artist: artist, rating: rating])
}
}
private currentUser(){
return User.get(springSecurityService.principal.id)
}
}
But with this code, when the rating value is assigned to the new rating (params.rating) in the "else" block, it is assigned to some random number around 50's (like 53). I can not see where is the problem.
A little help would be appreciated.
Thanks very much.

I just found out where the problem was. I had to convert the input value of the rating to type double. So, the following code is working as it was supposed to:
class RatingController {
def springSecurityService
static scaffold = true
def rate = {
def rating = params.rating.toDouble()
def artist = Artist.get( params.id )
def currentUser = currentUser()
if(! currentUser.ratings.artist.contains(artist)){
currentUser.addToRatings(new Rating(artist:artist, rating:rating)).save()
render(template: "/artist/rate", model: [artist: artist, rating: rating])
}
else{
currentUser.ratings.find{it.artist==artist}.rating = rating
currentUser.save()
render(template: "/artist/rate", model: [artist: artist, rating: rating])
}
}
private currentUser(){
return User.get(springSecurityService.principal.id)
}
}

Related

Grails, gorm. Find child by parent and parent by child

For example, I've parent class Author:
class Author {
String name
static hasMany = [
fiction: Book,
nonFiction: Book
]
}
and a child class Book:
class Book {
String title
static belongsTo = [author: Author]
}
I've done some records to Author using:
def fictBook = new Book(title: "IT")
def nonFictBook = new Book(title: "On Writing: A Memoir of the Craft")
def a = new Author(name: "Stephen King")
.addToFiction(fictBook)
.addToNonFiction(nonFictBook)
.save()
How can I found child-class record by parent and parent-class record by child?
In my opinion, this is not the best way to model your data. I would do it like this.
class Author {
String name
static hasMany = [books: Book]
}
class Book {
String title
BookTypes bookType
static belongsTo = [author: Author]
}
enum BookTypes {
FICTION,
NON_FICTION
}
Then, you can do lookups like
def author = Author.get(1)
def nonFictionByAuthor = Book.findAllByAuthorAndBookType(author, BookTypes.NON_FICTION)
You could also just do something like this...
def author = Author.get(1)
def fictionBooks = author.books.findAll { it.bookType == BookTypes.FICTION }
And then inversely:
def fictionBook = Book.findByTitleAndBookType('Title001', BookTypes.FICTION)

Adding multiple childs belonging to a parent at once

If I have two domains as below
class Author {
static hasMany = [books: Book]
String name
}
class Book {
static belongsTo = [author: Author]
String color
}
How can I add multiple books for an author at the same time?
like Below:
def book1 = new Book(color: "white")
def book2 = new Book(color: "black")
def books = []
books << book1
books << book2
def author = new Author(name: "John Doe").addToBooks(books).save()
addToBooks takes either a Book instance or a map that can be used to create a Book instance. This is relatively compact:
def book1 = new Book(color: "white")
def book2 = new Book(color: "black")
def books = []
books << book1
books << book2
def author = new Author(name: "John Doe")
books.each { author.addToBooks(it) }
author.save()

grails domain dirtyPropertyNames for child object

def names = domain.dirtyPropertyNames
for (name in names) {
def originalValue = domain.getPersistentValue(name)
def newValue = domain."$name"
}
But if i have a relation of 1-1 with other domain
how can i access dirtyPropertyNames for that other domain
def dirtyProperties = domain?.otherDomain?.dirtyPropertyNames
for (name in dirtyProperties ) {
def originalValue = domain?.otherDomain?.getPersistentValue(name)
def newValue = domain?.otherDomain?."$name"
}
But i am getting
No such property: dirtyPropertyNames for class: otherDomain
This seems not to be an issue when tested against Grails 2.2.4 and 2.3.0.
How have you tailored the 1:1 relationship?
Here is a sample, hope that helps:
class Book {
String name
String isbn
static hasOne = [author: Author]
}
class Author {
String name
String email
Book book
}
//Save new data
def book = new Book(name: 'Programming Grails', isbn: '123')
book.author = new Author(name: "Burt", email: 'test', book: book)
book.save(flush: true)
//Sanity check
println Book.all
println Author.all
//Check dirty properties of association
def book = Book.get(1)
book.author.name = 'Graeme'
def dirtyProperties = book?.author?.dirtyPropertyNames
for (name in dirtyProperties ) {
println book?.author?.getPersistentValue(name) //Burt
println book?.author?."$name" //Graeme
}
Although, wrt Grails 2.3.0 you can persist a 1-1 relation as done below unlike above:
def author = new Author(name: "Burt", email: 'test')
def book = new Book(author: author, name: 'PG', isbn: '123').save(flush: true)

Deserialize a JSON object with support for embedded associations

Is there an easy way to deserialize a JSON string to a domain class with support of embedded association; belongsTo and hasMany
{
name: "Customer",
contact: {
name: "Contact"
}
}
class Customer {
name
Contact contact
}
class Contact {
String name
static belongsTo = [customer:Customer]
}
in my controller I would like to do the following
def save() {
def customer = new Customer(request.JSON)
customer.save();
}
Now i'm forced to do
def save() {
def contact = new Contact(request.JSON.contact);
def customer = new Customer(request.JSON);
customer.contact = contact;
customer.save();
}
Have you tried using JsonSlurper?
Example usage:
def slurper = new JsonSlurper()
def result = slurper.parseText('{"person":{"name":"Guillaume","age":33,"pets":["dog","cat"]}}')
assert result.person.name == "Guillaume"
assert result.person.age == 33
assert result.person.pets.size() == 2
assert result.person.pets[0] == "dog"
assert result.person.pets[1] == "cat"
Ref: http://groovy.codehaus.org/gapi/groovy/json/JsonSlurper.html
you can try this
Test test
def result = new JsonSlurper().parseTest('yourString')
test = result
Try this will work.

How do I save GORM objects with multiple many-to-one relationships?

Let's say I have the following hierarchy of domain classes.
class School {
String name
static hasMany = [teachers: Teacher, students: Student]
}
class Teacher {
String name
static belongsTo = [school: School]
static hasMany = [students: Student]
}
class Student {
String name
static belongsTo = [school: School, teacher: Teacher]
}
I tried two different ways to save a school, teacher, and student.
Attempt 1:
def school = new School(name: "School").save()
def teacher = new Teacher(name: "Teacher", school: school).save()
def student = new Student(name: "Student", school: school, teacher: teacher).save(flush: true)
It appears to save properly but when I run:
println(school.students*.name)
It prints null.
So I decided to try a different approach.
Attempt 2:
def school = new School(name: "School")
def teacher = new Teacher(name: "Teacher")
def student = new Student(name: "Student")
teacher.addToStudents(student)
school.addToStudents(student)
school.addToTeachers(teacher)
school.save(failOnError: true, flush: true)
Here I tried several combinations of saves and I always got an error about a required field being null. In this case the error was
JdbcSQLException: NULL not allowed for column "TEACHER_ID"
I would greatly appreciate if someone could explain why my attempts failed and what the proper way to go about creating the data is.
def school = new School(name: "School").save(flush: true)
def teacher = new Teacher(name: "Teacher")
school.addToTeachers(teacher)
teacher.save(flush: true)
def student = new Student(name: "Student", teacher: teacher)
school.addToStudents(student)

Resources