How to pass a string in findById(String id) Grails - grails

How to use a String value as a parameter in findById(), It always expecting a Long value. Iam using a domain class named Employee and I configured its id key as string. But still Employee.findById() is expecting a long value as parameter.
Please help me
My Employee.groovy
class Employee {
static hasOne = [user:User, vpn:Vpn, tenrox:Tenrox, machine: Machine, dbuser:DbUser];
static mapping = {
version false
id generator: 'assigned',type: 'string'
}
String name
String designation
String team
Long contact_no
static constraints = {
name(nullable: false,maxSize:100)
designation(nullable: true,maxSize:40)
team(nullable: true,maxSize:40)
contact_no(nullable: true,maxSize:20)
}
}

Declare your id field in the domainclass as String.
class Employee {
static hasOne = [user:User, vpn:Vpn, tenrox:Tenrox, machine: Machine, dbuser:DbUser];
static mapping = {
version false
id generator: 'assigned',type: 'string'
}
String id // Declares id as String
String name
String designation
String team
Long contact_no
static constraints = {
name(nullable: false,maxSize:100)
designation(nullable: true,maxSize:40)
team(nullable: true,maxSize:40)
contact_no(nullable: true,maxSize:20)
}
}

Related

Grails domain hasMany validation

I have a case that validation is done on domain properties but not on the an associated (hasMany) properties.
Is there any configuration I can add to enable the validation on both properties (domain and hasMany).
grails version : 3.1.14
Example:
class Person {
String name;
static hasMany = [location: Location]
static constraints = {
name nullable: true
}
}
class Location {
String address
String city
State state
String zip
static constraints = {
address nullable: true
}
}
According to the documentation the validation should work for has-many associations as you wish: http://docs.grails.org/3.1.14/ref/Domain%20Classes/validate.html
But in my test's it does not work eather.
An other solution is to work with the constraints:
static constraints = {
name nullable: true
location validator: {val, obj ->
val.every { it.validate() } ?: 'invalid'
}
}

Saving related entity in grails

I have a problem with saving form values from two domain classes
One class is
class Ip {
String inetAddress
String dns
String os
String toString(){
"${inetAddress}"
}
Hoster hoster
static constraints = {
....
and the second one is just
class Hoster {
static HOSTER_OPTIONS = ["Name1", "Name2", "Name3"]
String name;
String toString(){
"${name}"
}
List ips = new ArrayList()
static hasMany = [ips : Ip]
static constraints = {
name(unique: true, blank: false, inList: HOSTER_OPTIONS)
}
I have a Controller where I handle the data from a form
def systems = new Ip()
systems.inetAddress = params.ip
systems.dns = params.dns
systems.os = params.os
systems.hoster.name = params.hoster
def result = systems.save(flush: true, failOnError: true)
But I didn't get it managed that the data is saved.
You're not associating correctly your domain classes in the controller:
systems.hoster.name = params.hoster
Instead of setting the name, you need to set the instance that exists in the database:
systems.hoster = Hoster.findByName(params.hoster)

How to access properties of a command object from another command object?

I am currently working on a grails application. I have two command objects (AccountInfoCommand and EducInfoCommand) in a controller. On my EducInfoCommand, I wanted to check if the yearGraduated property is earlier than the set birthDate(a property of AccountInfoCommand) on its validator constraints. How will I do that?
This is my code for my AccountInfoCommand:
class AccountDetailsCommand implements java.io.Serializable {
String username
String password
String confirmPassword
String emailAddress
Date birthDate
}
This is my code for EducInfoCommand:
class EducInfoCommand implements java.io.Serializable {
Integer graduated
EducationLevel educationLevel
String schoolName
String yearGraduated
String honorsReceived
}
static constraints = {
yearGraduated nullable: false, maxSize:4, blank: false, matches: /([0-9]*)/,
validator: {
Date.parse('yyyy',it) <= new Date() ? true : false
}
}
Please help!
Thanks!
You need some reference to which AccountDetails the EducInfo is for. For example, if you added a username field to the EducInfoCommand you could look up the account details from that (assuming there is an AccountDetails gorm object which is similar to the command object):
class EducInfoCommand implements java.io.Serializable {
String yearGraduated
String username
// ...
}
static constraints = {
yearGraduated nullable: false, maxSize:4, blank: false, matches: /([0-9]*)/,
validator: { val, obj ->
Date.parse('yyyy',val) > AccountDetails.findByUsername(obj.username).birthDate
}
}

Assigned domain class id wierdness with Grails

I want to set the id manually on grails 1.3.7
This compiles but the id is always 0
//in bootstrap
def it1 = new ItemType(id:4,name:'feature')
it1.save()
//domanin class
class ItemType {
String name
int id
static constraints = {
id(unique:true,blank:false)
name(blank:false)
}
static mapping = {
id column: 'ItemTypeId', generator:'assigned'
name column: 'Name'
version false
}
}
This compiles and id 4 (as required)
//in bootstrap
def it1 = new ItemType(name:'feature')
it1.id=4
it1.save()
//domanin class
class ItemType {
String name
//int id
static constraints = {
id(unique:true,blank:false)
name(blank:false)
}
static mapping = {
id column: 'ItemTypeId', generator:'assigned'
name column: 'Name'
version false
}
}
So my question is there a way to have id as prop but assigned?
Had the same problem a few days ago: my own id in GORM
It seems that it is a feature :-)

One's more about grails searchable plugin

I have two simple domains:
public class Hotel {
static searchable = true
Source source
City city
HotelType type
long sourceid
float lat
float lon
static hasMany = [hotelTexts:HotelText]
static mapping = {
hotelTexts batchSize:10
}
}
public class HotelText {
static searchable = true
static belongsTo = [hotel:Hotel]
String lang
String name
String description
String address
static mapping = {
batchSize:10
description type:"text"
}
}
I'm totally new in searchable plugin but i believe that it could help me with my problem.
So, the task is to find Hotels by city and then sort result by name. Without sorting it could be easily done with dynamic finders help but...
Summary:
Find hotels by city.
Sort result by hotel name(for given language).
Support pagination.
public class Hotel {
static searchable = {
hotelTexts component: true
}
...
}
public class HotelText {
static searchable = {
name boost: 2.0
}
...
}

Resources