Grails Enum Mapping - grails

in Grails, Is there a way to limit the size of the column to which the enum is mapped. In the following example, i would like the column type to be char(2)
enum FooStatus {
BAR('br'), TAR('tr')
final static String id
}
class Foo {
FooStatus status
static constraints = {
status(inList:FooStatus.values()*.id,size:2..2)
}
}
both inList and size do not have any effect when exporting the schema, the column type keeps its default value (varch(255))
Maybe i could do that if i define a new UserType. Any idea ?
Thank you
-ken

I don't think it's directly possible given the way enums are mapped internally in GORM. But changing the code to this works:
enum FooStatus {
BAR('br'),
TAR('tr')
private FooStatus(String id) { this.id = id }
final String id
static FooStatus byId(String id) {
values().find { it.id == id }
}
}
and
class Foo {
String status
FooStatus getFooStatus() { status ? FooStatus.byId(status) : null }
void setFooStatus(FooStatus fooStatus) { status = fooStatus.id }
static transients = ['fooStatus']
static constraints = {
status inList: FooStatus.values()*.id
}
static mapping = {
status sqlType: 'char(2)'
}
}
Adding the transient getter and setter allows you to set or get either the String (id) or enum value.

Grails ships with an undocumented (as far as I can tell anyway) custom Hibernate mapping for enums. The class is org.codehaus.groovy.grails.orm.hibernate.cfg.IdentityEnumType. It won't let you set the column size but does make it easy to change what is stored in the DB for each enum value without having to add transient fields to your model.
import org.codehaus.groovy.grails.orm.hibernate.cfg.IdentityEnumType
class MyDomainClass {
Status status
static mapping = {
status(type: IdentityEnumType)
}
enum Status {
FOO("F"), BAR("B")
String id
Status(String id) { this.id = id }
}
}
You can run an 'alter table' in Bootstrap.groovy to shrink the column:
DataSource dataSource
...
Sql sql = new Sql(dataSource)
sql.execute("alter table my_domain_class change column status status varchar(1) not null")

Even easier (works at least in Grails 2.1.0+)
class DomainClass {
Status status
static mapping = {
status(enumType: "string")
}
}
enum Status {
OPEN ("OPEN"),
CLOSED ("CLOSED"),
...
String name
Status (String name) {
this.name = name
}
}

Since GORM 6.1 identity enum mapping can be enabled with such construct
static mapping = {
myEnum enumType:"identity"
}

Related

Grails cannot clear a hasMany set using cascade

I am trying to clear out the data from the userDeptses set but when I call the clear() method and try to save I get the following error
Caused by: java.lang.UnsupportedOperationException: queued clear cannot be used with orphan delete
I have the cascading set properly, I have even tried using just delete-orphan but still have issues. All relevant classes have equals and hashCode methods implemented: AppSystemUser UserDepts Department
All documentation and articles I have read online say that using the combination of clear() and all-delete-orphan is supposed to work, but not for me. Any help is greatly appreciated.
Grails 3.1.4
Controller:
AppSystemUser user = AppSystemUser.findBySystemUserid(cmd.netid);
user.userDeptses.clear();
userMgmtService.saveAppSystemUser(user);
AppSystemUser:
class AppSystemUser {
String systemUserid
String email
String fullName
Date lastLogin
Boolean active
Set appSystemUserRoles = [];
Set userCollegeses = [];
Set userDeptses = [];
static hasMany = [appSystemUserRoles: AppSystemUserRole,
applicationExtensions: ApplicationExtension,
userCollegeses: UserColleges,
userDeptses: UserDepts]
static mapping = {
version false
fullName column: 'fullName'
lastLogin column: 'lastLogin'
id name: "systemUserid", generator: "assigned"
appSystemUserRoles cascade: "save-update, all-delete-orphan"
userCollegeses cascade: "save-update, all-delete-orphan"
userDeptses cascade: "save-update, all-delete-orphan"
}
....
UserDept:
class UserDepts {
Boolean active
AppSystemUser appSystemUser
Department department
static belongsTo = [AppSystemUser, Department]
static mapping = {
version false
appSystemUser column: "system_userid"
}
....
UserMgmtService:
#Transactional
class UserMgmtService {
def saveAppSystemUser(AppSystemUser user) {
user.save();
}
}
I was not able to find out how to successfully use the clear() method of the Set so I just created a simple workaround that does the trick
def static hibernateSetClear(Set data) {
if(data) {
Iterator i = data.iterator();
while (i.hasNext() && i.next()) {
i.remove();
}
}
}
Just iterate through the Set and remove each item individually. This works perfect and I just call this method instead of clear() whenever I need to clear a Set

Grails - How do I get the value of a domain entity prop in a function of its parent class

I have an abstract class in a groovy file:
Implementation 1
public abstract class Item {
public String testStr;
public String getBigTestStr(){
String s = "___" + this.testStr;
return s;
}
}
Which is inherited by
class Material extends Item {
public String testStr;
static marshalling = {
detail {
includes "bigTestStr"
}
summary {
includes "bigTestStr"
}
}
static mapping = {
table 'materialset'
id column: 'NODEID'
testStr column: 'MATERIALTYPE'
version false
}
}
The idea is that hitting the endpoint for a material will return the return value of Item.bigTestStr(). However, when I trace through Item.bigTestStr(), the debug's variables table shows a value for this.testStr, but is null when it is added to s. See here:
I tried taking the testStr property out of Material
Implementation 2
class Material extends Item {
static marshalling = {
detail {
includes "bigTestStr"
}
summary {
includes "bigTestStr"
}
}
static mapping = {
table 'materialset'
id column: 'NODEID'
testStr column: 'MATERIALTYPE'
version false
}
}
but I still get the same problem.
For both implementations the endpoint returns
{
bigTestStr: ____null
}
How can I get the actual value of Material.testStr to be used by the function in its parent class?
UPDATE
As Emmanuel pointed out, Implementation 2 is the right way to use properties from a parent class. However, this implementation does not seem to work with mapping the parent class' properties to a database column. So the real question is: How can I get Material.testStr to map to a database column?
It looks like your problem is in how you initialized your Material instance. Here's an example:
public abstract class Item {
public String testStr
public String getBigTestStr(){
"___$testStr"
}
}
class MaterialA extends Item {
public String testStr
static marshalling = {
detail {
includes 'bigTestStr'
}
summary {
includes 'bigTestStr'
}
}
static mapping = {
table 'materialset'
id column: 'NODEID'
testStr column: 'MATERIALTYPE'
version false
}
}
class MaterialB extends Item {
static marshalling = {
detail {
includes 'bigTestStr'
}
summary {
includes 'bigTestStr'
}
}
static mapping = {
table 'materialset'
id column: 'NODEID'
testStr column: 'MATERIALTYPE'
version false
}
}
Shown above are three classes Item, MaterialA, and MaterialB. The two material classes simulate your two tests: MaterialA has a testStr property, while MaterialB inherits a property with the same name from Item instead. Here's what happens when instances of both classes are initialized and getBigTestStr() is tested:
new MaterialA(testStr: 'Hello').with {
assert bigTestStr == '___null'
}
new MaterialB(testStr: 'Hello').with {
assert bigTestStr == '___Hello'
}
In short, your second approach, inheriting the property, works. A super class does not (and should not) have access to anything in its subclasses. It doesn't even know about its subclasses. The approach works because initializing testStr in an instance of MaterialB actually initializes the inherited property from Item; which of course is accessible within the Item class.
In your case, Grails is initializing the instances for you using the values stored in the database. So I'd check your database.
Update
Here's an example using a trait rather than an abstract class:
public trait Item {
String testStr
public String getBigTestStr(){
"___$testStr"
}
}
class Material implements Item {
static marshalling = {
detail {
includes 'bigTestStr'
}
summary {
includes 'bigTestStr'
}
}
static mapping = {
table 'materialset'
id column: 'NODEID'
testStr column: 'MATERIALTYPE'
version false
}
}
new Material(testStr: 'Hello').with {
assert bigTestStr == '___Hello'
}
This makes it so that there's no need for an Item table.

In Grails, how do I apply an inList constraint from a 3rd party data source?

Let's say I have the following model:
class Product {
String name
String price
String currency
static constraints = {
currency inList: ['USD', 'EUR']
}
Now we have a new requirement that inList constraint for currency must be pulled from a service:
class CurrencyService {
def getAvailableCurrencies = {
...
}
}
How do I make this work? I tried:
class Product {
def currencyService
...
static constraints = {
currency inList: currencyService.getAvailableCurrencies()
}
}
But I can't access the currencyService instance in the static constraints context. I also tried using static currencyService, but this likewise does not work. Any ideas?
As dmahapatro mentioned above, you could use a custom validator which uses your service:
static constraints = {
currency validator: { value, obj ->
if (!(value in obj.currencyService.getAvailableCurrencies()))
return ['invalid.currency']
}
}

Enum in Grails Domain

I'm trying to use an enum in a Grails 2.1 domain class. I'm generating the controller and views via the grails generate-all <domain class> command, and when I access the view I get the error shown below. What am I missing here?
Error
Failed to convert property value of type java.lang.String to required type
com.domain.ActionEnum for property action; nested exception is
java.lang.IllegalStateException: Cannot convert value of type
[java.lang.String] to required type [com.domain.ActionEnum] for property action:
no matching editors or conversion strategy found
Enum (in /src/groovy)
package com.domain
enum ActionEnum {
PRE_REGISTER(0), PURCHASE(2)
private final int val
public ActionEnum(int val) {
this.val = val
}
int value() { return value }
}
Domain
package com.domain
class Stat {
ActionEnum action
static mapping = {
version false
}
}
View
<g:select name="action"
from="${com.domain.ActionEnum?.values()}"
keys="${com.domain.ActionEnum.values()*.name()}" required=""
value="${xyzInstance?.action?.name()}"/>
EDIT
Now getting error Property action must be a valid number after changing the following.
View
<g:select optionKey='id' name="action"
from="${com.domain.ActionEnum?.values()}"
required=""
value="${xyzInstance?.action}"/> // I tried simply putting a number here
Enum
package com.domain
enum ActionEnum {
PRE_REGISTER(0), PURCHASE(2)
final int id
public ActionEnum(int id) {
this.id = id
}
int value() { return value }
static ActionEnum byId(int id) {
values().find { it.id == id }
}
}
Domain
package com.domain.site
class Stat {
static belongsTo = Game;
Game game
Integer action
static mapping = {
version false
}
static constraints = {
action inList: ActionEnum.values()*.id
}
String toString() {
return "${action}"
}
}
Take a look here ...
Grails Enum Mapping
Grails GORM & Enums
Also you may be hitting this as well. From the docs:
1) Enum types are now mapped using their String value rather than the ordinal value. You can revert to the old behavior by changing your mapping as follows:
static mapping = {
someEnum enumType:"ordinal"
}

Transient property in Grails domain

I have a Grails domain called People, and I want to check that each People has childs or not. Childs are other People objects. Here is my domain structure:
class People implements Serializable {
static constraints = {
name (nullable : false, unique : true)
createdBy (nullable : false)
creationDate (nullable : false)
}
static transients = ['hasChild']
static mapping = {
table 'PEOPLE'
id generator: 'sequence', params : [sequence : 'SEQ_PK_ID']
columns {
id column : 'APEOPLE_ID'
parentPeople column : 'PARENT_PEOPLE_ID'
}
parentPeople lazy : false
}
People parentPeople
String name
String description
Boolean hasChild() {
def childPeoples = People.createCriteria().count {
eq ('parentPeople', People)
}
return (childPeoples > 0)
}
}
But I cannot call people.hasChild() at anywhere. Could you please helpe me on this? Thank you so much!
It's because in eq ('parentPeople', People), Grails can't understand what "People" is (it's a class). You should replace "People" by this. For example:
static transients = ["children"]
def getChildren() {
def childPeoples = People.findAllByParentPeople(this, [sort:'id',order:'asc'])
}
Another way to get the same result is to use Named Queries. It seems more concise and was created specifically for this purpose. I also like it because it fits the pattern of static declarations in a domain model and it's essentially a criteria, which I use throughout my applications. Declaring a transient then writing a closure seems a bit of a work-around when you can declare named queries ... just my opinion.
Try something like this:
static namedQueries = {
getChildren {
projections {
count "parentPeople"
}
}
}

Resources