Say I have a domain Books.
static hasMany = [reader:Reader]
And Class Reader
String fullName
Now I want to add Readers with fullName: "PersonA", "PersonB", "PersonC" by defualt in the Books domain.
Please tell how do I accomplish this? I am pretty new with Grails.
You can create class named BootStrap in Configuration folder of your App andd add init() method to it. This method will be executed before App starts. Example:
class BootStrap {
def init() {
def readerA = Reader.findOrCreateByFullName("PersonA")
def readerB = Reader.findOrCreateByFullName("PersonB")
def readerC = Reader.findOrCreateByFullName("PersonC")
def readers = [readerA, readerB, readerC]
def book = new Book()
for(reader in readers){
book.addToReader(reader)
}
book.save(flush: true)
}
}
Related
I'm new to Grails. I'm having problem with retrieving data from DB. I have domains with classes, something like...
class Lightbox {
String name = ''
String link = ''
static hasMany = [users: LightboxUserAccount]
}
class LightboxUserAccount {
UserAccount userAccount
static belongsTo = [lightbox: Lightbox]
}
class UserAccount {
String username
...
}
I want to list all "Lightboxes" owned by user with ID=4. I was trying
def my_lb = Lightbox.findAll("from Lightbox as lb where lb.users=:userAccount", [userAccount: springSecurityService.getCurrentUser()])
or
def my_lb = Lightbox.findAllByUsers(4)
None of those work for me. What am I doing wrong? Thx
Try this one:
Lightbox.findAll("from Lightbox as lb where :userAccount in (lb.users)", [userAccount: springSecurityService.getCurrentUser()])
So I did it slightly different, using criteria instead. Take a look if interested
static getAllByUserAccount(UserAccount userAccount) {
def criteria = Lightbox.createCriteria()
def my_lb = criteria.list {
users {
eq('userAccount', userAccount)
}
}
return my_lb
}
It seems to be working. Thanks for responses though
I have the domain classes TestUnit, TestParameter and ParameterRange as shown below.
class TestUnit {
static hasMany = [testParameters : TestParameter]
}
class TestParameter {
static hasMany = [paramRanges : ParameterRange ]
static belongsTo = [testUnit : TestUnit]
}
class ParameterRange {
static belongsTo = [testParam : TestParameter]
}
I want to add the TestUnit object (i.e, testUnitInstance.id) in the ParameterRangeController.
Since i'm new to grails i don't know how to do that could anyone please explain it to me?
What I've tried:
def testUnitId = params.testUnitId
def testUnitInstance = TestUnit.get(testUnitId)
def testParameterInstance = TestParameter.get(params.id)
[parameterRangeInstanceList: testParameterInstance.paramRanges, parameterRangeInstance: new ParameterRange(),testParameterInstance:testParameterInstance, page:"Range", testUnitInstance:testUnitInstance]
You can manipulate more than one domain class in a controller or service, there's no restriction to that.
class ParameterRangeController {
def show() {
//you can get other domain classes...
TestUnit theUnit = TestUnit.get(1)
render view: 'show', model: [theUnit:theUnit, ...]
}
}
Since TestUnit is related to TestParameter, you can also access it like:
ParameterRange range = ParameterRange.get(1)
println range.testParam.testUnit
I suggest you to take a look in the docs about GORM, there's a lot of useful info.
I have a searchable model setup as:
class Tag{
def searchable = true
String name
}
class PersonTag{
static belongsTo = [person: Person]
static searchable = {
tag(component: true)
person(component: true)
}
static PersonTag addTag(String name, Person person){
if(person && person.id){
def tag = Tag.findOrCreate(name)
def t = new PersonTag(tag:tag, person:person)
t.save(flush:true)
return t
}
}
}
class Person{
static searchable = {
name boost: 2.0
tags component:true
}
}
What I am working on is searching "Persons" by tags. When my server starts it indexes everything and all existing tags on people work. If I add a new tag, I can not search for it until a server restart. However if I change a simple property like the persons name, I am able to search for it w/o a restart. All of my changes for tags are going through a PeopleAdminController.
Any suggestions on why searching by tags is not working? I have even tried to manually index/reindex Person, Tag, and PersonTag via the domainInstance.reindex(), as well as in the controller using the searchableService.
I am searching for People in a different controller PeopleController:
def search(){
def result = People.search("${params.q}")
render (view: '/searchable/search.gsp' , model:[searchResult: result])
}
Well I found a way to "fix the issue" but i dont like it(because its a reindexAll()). After I add the tag if I call:
searchableService.reindexAll()
It will work. I dont understand why the following wouldn't work:
static PersonTag addTag(String name, Person person){
if(person && person.id){
def tag = Tag.findOrCreate(name)
def t = new PersonTag(tag:tag, person:person)
t.save(flush:true)
t.index()
tag.reindex()
person.reindex()
return t
}
}
I want to allow users to traverse the domain classes and print out dumps of stuff. My frist problem: assuming the following works just fine:
//this works
class EasyStuffController{
def quickStuff = {
def findAThing = MyDomainClass.findByStuff(params.stuff)
[foundThing:findAThing]
}
}
What is the proper way to write what I am trying to say below:
//this doesn't
class EasyStuffController{ servletContext ->
def quickStuff = {
def classNameString = "MyDomainClass" //or params.whichOne something like that
def domainHandle = grailsApplication.domainClasses.findByFullName(classNameString)
//no such property findByFullName
def findAThing = domainHandle.findByStuff(params.stuff)
[foundThing:findAThing]
}
}
//this also doesn't
class EasyStuffController{ servletContext ->
def quickStuff = {
def classNameString = "MyDomainClass" //or params.whichOne something like that
def domainHandle
grailsApplication.domainClasses.each{
if(it.fullName==classNameString)domainHandle=it
}
def findAThing = domainHandle.findByStuff(params.stuff)
//No signature of method: org.codehaus.groovy.grails.commons.DefaultGrailsDomainClass.list() is applicable
[foundThing:findAThing]
}
}
Those lines above don't work at all. I am trying to give users the ability to choose any domain class and get back the thing with "stuff." Assumption: all domain classes have a Stuff field of the same type.
If you know the full package, you can use this:
String className = "com.foo.bar.MyDomainClass"
Class clazz = grailsApplication.getDomainClass(className).clazz
def findAThing = clazz.findByStuff(params.stuff)
That will also work if you don't use packages.
If you use packages but users will only be providing the class name without the package, and names are unique across all packages, then you can use this:
String className = "MyDomainClass"
Class clazz = grailsApplication.domainClasses.find { it.clazz.simpleName == className }.clazz
def findAThing = clazz.findByStuff(params.stuff)
I use grails-1.3.2 and hbase plugin.
I have some difficulty in creating one-to-Many association with
hbase (i can work with hibernate), so
i decided to try create one-to-Many association with using ArrayList.
Here are my domain classes and controllers:
class Contacts {
String con
static constraints = {}
}
class ContactsController {
def create = {
def contact = new Contacts()
contact.con = params.con
contact.save(flush:true)
}
}
class User {
String firstname
String lastname
// static hasMany = [contact: Contacts]
static ArrayList<Contacts> contact
static constraints = {}
}
class UserController{
def create = {
def user = new User()
user.properties = params
user.save(flush: true)
}
def addContact = {
def user = User.get(params.userID)
def contact = Contacts.get(params.contactID)
user.contact.add(contact)
user.save(flush:true)
}
}
In addContact action user.contact = null, so it can not work.
In user does nor appear contact field.
Can someone help me understand what i have to do for saving ArrayList in db?
I don't know anything about hbase, but the static contact property of the User class looks very suspicious. The fact that this property is static, implies that every user has the same contact list, which seems unlikely to be the desired behaviour.
In a standard GORM domain model - assuming you want each User to have their own contact list - this would be defined
class User {
String firstname
String lastname
static hasMany = [contact: Contacts]
}
Although it looks like we're also defining a static property here, it's actually just the definition of how the Contact and User classes are related (AKA mapping) that is static. The contact property that is dynamically added to the User class is non-static.
Aside
I recommend renaming the Contacts class to Contact and the contact property to contacts. The GORM mapping would then look like this:
class User {
String firstname
String lastname
static hasMany = [contacts: Contact]
}