Maybe a silly question but where/how should I define default values for GORM domain class properties? For example when I'm creating a new Company object instance I want default value for property country to be "USA". I guess I could do it in create controller but it looks kinda dirty. Something like:
def create = {
def companyInstance = new Company()
companyInstance.properties = params
companyInstance.accepted = "USA"
...
Put it in the domain class itself
class Company {
String country = "USA"
}
Related
I have tow domain
class Book {
String name
Long id
}
class BookRef {
String name
Long id
String refId
}
In Book table, I stored data like id=1, name='Java'.
Now I can initiate BookRef by getting Book like bellow
def book = Book.get(1)
def bookref = new BookRef()
bookref.id = book.id
bookref.name = book.name
bookref.refId = '1'
bookref.save()
But I want to initiate the bookref object by using the book object like params binding not by binding each individual properties.
For now, I have now used the property to property initialization but in a domain with various properties, it's time-consuming.
How can I do this?
Here you go:
Book book = Book.get(1)
// Option 1
BookRef bookref = new BookRef(book.properties)
bookref.refId = '1'
bookref.save()
// Option 2
BookRef bookref = new BookRef()
bookref.properties = book.properties
bookref.refId = '1'
bookref.save()
Basically, in Grails, any domainInstance.properties gives you a map of all the domain fields.
Please note, this approach is good for primitive types like String, boolean, int, Long etc but Grails might throw an exception when the domain has collections, one-to-many or has-many relationship fields.
i'm making a tables cleaning service that takes the table name and the date field as arguments , here is the service code :
def cleanTables(tableName , dateField) {
def comparisonDate
def numOfRecordsDeleted
use (TimeCategory){
comparisonDate=new Date() -1.year
}
numOfRecordsDeleted=tableName.where { dateField <=comparisonDate }.deleteAll()
log.info("deleted : " +numOfRecordsDeleted)
}
i'm successfully passing to this service the table name but i can't pass the date field , so how to get a specific property from a domain for example a domain named Payments got a property dateCreated , so i pass to my service Payments and dateCreated.
With where queries you have access to criteria query methods such as eq(), or in this case, le(). Those methods take the name of the property as an argument, which is what you need. I tweaked the code a bit because you're actually interacting with domain classes, not tables. Small distinction, until you start working with HQL.
def cleanDomainClass(String domainClassName, String dateField) {
def domainClass = Class.forName("some.package.$domainClassName")
def comparisonDate = use (TimeCategory) { new Date() -1.year }
def numOfRecordsDeleted = domainClass.where { le(dateField, comparisonDate) }.deleteAll()
log.info("deleted : $numOfRecordsDeleted")
}
I wanted to set a value to a field in domain class.
For example,
class Example {
String name
String lastName
}
Now, from response I'm getting domain name, object instance id, field name and value. I have to set the value to the field in domain class.
Here I got the values as
domainName = 'Example'
instanceId = 1
fieldName = 'name'
valueToSet = 'XYZ'
So how should I set value to the field name? May be this is simple but I'm a new with grails and groovy.
Based on the domain name, a new domain instance has to be created at runtime. For this to happen, grailsApplication has to be injected. Here is a sample which can be modeled after in Controller or a Service class:
class SomeService {
def grailsApplication
def someMethod(String domainName, long instanceId,
String fieldName, def valueToSet) {
Class domainClazz = grailsApplication.domainClasses.find {
it.clazz.simpleName == domainName
}.clazz
def domainInstance = domainClazz.get( instanceId )
domainInstance."$fieldName" = valueToSet
domainInstance.save()
}
}
You can populate a Grails domain class by assigning a map to its properties, for example from the params in the controller (using that handy automatic binding). E.g.:
ded example=new Example()
example.properties=params
So you can see from this that the domain object can be treated as a set of properties. Which means you can use strings for keys, so you might have something like:
example['name']='XYZ'
I don't know whether that's what you're asking but I hope it helps.
Say I have something like:
class Foo {
static mapping = {
table 'foo_table'
}
}
How can I get the name of foo_table if I have a reference to an instance of this object?
Import org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsDomainBinder.
To get the table name from the domain class:
def tableName = GrailsDomainBinder.getMapping(Foo).table.name
And to get the table name from an instance of the domain class:
def tableName = GrailsDomainBinder.getMapping(foo.class).table.name
JamesA's answer will work, but only if table name if defined explicitly, like in the question.
If you wish to get a table name whether or not it was specified in mapping, it can be done using SessionFactory:
def tableName = sessionFactory.getClassMetadata(Foo).tableName
I have there domain classes:
Person. (Person.ID, Name,Address)
Designation.(Designation.ID, Title, Band)
SalarySlip (Person.ID, Designation.ID, totalIncome, Tax etc etc.)
In the update method the person controller when I assign a person a designation from a list of designation values I want to insert a new record inside SalarySlip.
Something like:
def update = {
def SalarySlipInstance = new SalarySlip()
SalarySlipInstance.Person.ID = Params.ID //is this correct?
SalarySlipInstance.Designation.ID = ?? //since the value is coming from a list. How can I bind this field?
}
You need to load the Person and Designation objects first:
salarySlipInstance.Person = Person.get(params.person.id)
salarySlipInstance.Designation = Designation.get(params.designation.id)
If in your form you prefix the person and designation id's with person. and designation. it makes it easier to load.