View grails domain class property in another domain class - grails

Hi I am just trying out grails and trying to learn more on domain class. I have two simple domain classes:
Domain Class 1
package grailtest
class Person {
String firstName
String lastName
int age
String email
static constraints = {
}
}
Domain Class 2
package grailtest
class Customer {
String customerId
Person personInCharge
static constraints = {
}
}
When I do a run-app, I can only see
grailtest.Person : 1
as the Person. How can I default it to a particular value, for instance firstName + lastName, to make the application more user friendly?

in the domain override toString method to what you wanted to be display. restart the app

You can use #ToString in case you want an elaborative way of logging or printing in standard out.
import groovy.transform.ToString
#ToString(includeNames=true, includeFields=true)
class Person {
String firstName
String lastName
int age
String email
}
For example,
def person = new Person(firstName: 'Test', lastName: 'hola',
age: 10, email: 'abc#xyz.com')
would give
Person(firstName:Test, lastName:hola, age:10, email:abc#xyz.com)

Find the view where it displays grailstest.Person: 1 and update it to:
${personInstance.firstName} ${personInstance.lastName}
By default this view should be in "views/person"

You put this code in the view .gsp
${personInstance?.firstname} ${personInstance?.lastname}

Related

Creating Domain Relationships in Grails returns unable to resolve class error

Good day to all. I am very new in using grails and I have followed several tutorials for beginners using grails until I come up in creating domain relationships. However, I got stuck with this problem right now. I have 3 domain classes namely, todo, category and user. And as I defined their relationships, it returns me an error saying unable to resolve class. Please see my codes below. Please help. Thank you so much.
Todo.groovy Class
package todoScaff
class Todo {
String name
String note
Date createDate
Date dueDate
Date completedDate
String priority
String status
User owner
Category category
static belongsTo = [User, Category]
static constraints = {
name(blank:false)
createDate()
priority()
status()
note(maxsize:1000, nullable:true)
completedDate(nullable:true)
dueDate(nullable:true)
}
String toString() {
name
}
}
Category.groovy Class
package categoryScaff
class Category {
String name
String description
User user
static belongsTo = User
static hasMany = [todos: Todo]
static constraints = {
name(blank:false)
}
String toString(){
name
}
}
User.groovy Class
package userScaff
class User {
String userName
String fname
String lname
static hasMany = [todos: Todo, categories: Category]
static constraints = {
userName(blank:false, unique:true)
fname(blank:false)
lname(blank:false)
}
String toString(){
"$lname, $fname"
}
}
Since you've placed your domain classes in different packages, you must import the classes at the head of the file.
package categoryScaff
import todoScaff.Todo
import userScaff.User
class Category {
The same needs to happen in your other domain classes that reference classes outside the current package.

Grails: Multiple Relationships between same two Objects

How do I define a relationship like the following:
A person can belong to many projects. A person can be the technical contact for a project or, they can be the business contact for a project or they can be both. If the person gets deleted the project doesn't get deleted. If a project gets deleted the person doesn't get deleted.
class Project {
String name
Person technicalContact
Person businessContact
static constraints = {
}
}
class Person {
String firstName
String lastName
String email
String phone
String department
static constraints = {
}
}
You can have 2 one-to-many in one table like this
class Project {
String name
}
class Person {
String firstName
String lastName
String email
String phone
String department
static hasMany = [technicalContactForProjects: Project ,
businessContactForProjects: Project
]
}
Grails will automatically make 2 relation table from that 2 hasMany, so you can delete its relation without delete the actual person or project.

The user name is stored in an a connected object

I'm using grails and I have the following domain classes:
class User {
transient springSecurityService
String password
static belongsTo = [person: Person]
}
And
class Person {
String name
String emailAddress
....
}
I would like to use the person object's email address as the username in Spring Security.
According to the Spring Security manual, that just takes setting the property of grails.plugins.springsecurity.userLookup.usernamePropertyName to the non-"username" field.
I've tried person.emailAddress, and that doesn't work.
How can I get Spring security to use the Person reference to the User? I can't put the password on the Person class, and I've tried to use inheritance (that brings up other issues).
So I've traced this down to GormUserDetailsService, where this is being called and implimented. The code that is used to find the user is:
def user = User.findWhere((conf.userLookup.usernamePropertyName): username)
if (!user) {
log.warn "User not found: $username"
How would I structure the userNamePropertyName so that I could get it look through the child property?
It's bad idea. Just replace email to User, but if you want to use this field in person, create link to this field.
class User {
transient springSecurityService
String password
String email
static belongsTo = [person: Person]
}
and Person:
class Person {
String name
String emailAddress
....
String getEmail(){
user.email //if relations OneToOne
}
static hasOne = [user:User] //if relations OneToOne
}
You should stay relations OneToOne, because I can't imaginate in what case it should be OneToMany
UPD. Or you can try try this one:
class User extends Person{..}
and in spring security config write just email, but i'm not sure.

How do you organize the fields in grails 2.4.4 including the static mapping relations?

Okay, so I understand how to organize the fields in grails without using the gsp pages by writing the fields in the constraints like so.
Class User{
String firstName;
String nickName;
static constraints = {}
}
this will make first name appear before nick name in the default scaffolding because f comes before n in the alphabet.
Class User{
String firstName;
String nickName;
static constraints = {
nickName()
firstName()
}
}
this makes nick name appear before first name in the CRUD model in scaffolding. It's the order you name the constraints.
Now, how do you make the relations appear in a specific order? For example if I had this
Class User{
String firstName;
String nickName;
static belongsTo = {company:Company}
static constraints = {
}
}
How would I rearrange this order? would it be done in constraints? I know it can be done in gsp page, but how would I do it here?
I'm pretty sure it's the same as regular fields...
i.e. if you want the order of the fields to appear as nickName, firstName, company you'd do this:
Class User {
String firstName
String nickName
static belongsTo = {company: Company}
static constraints = {
nickName()
firstName()
company()
}
}

How to indicate a field in domain class not to be created in the database

I have a domain class like:
class Product {
String name
String number
}
and I only want name field be created in the database as a column, I will generate the number field in the code, I don't want it to be a column of the Product table in the database.
What's the best way to do that?
class Author {
String name
String getUpperCaseName() { name.toUpperCase() }
static transients = ['upperCaseName']
}
See http://www.grails.org/doc/latest/ref/Domain%20Classes/transients.html

Resources