Groovy Dynamic List Interaction - grails

I am using an older version of grails (1.1.1) and I am working on a legacy application for a government client.
Here is my question (in psuedo form):
I have a domain that is a Book. It has a sub domain of type Author associated with it (1:many relationship). The Author domain has a firstName and lastName field.
def c = Book.createCriteria()
def booklist = c.listDistinct {
author {
order('lastName', 'asc')
order('firstName', 'asc')
}
}
Let's say I have a list of fields I want to use for an excel export later. This list has both the author domain call and the title of the column I want to use.
Map fields = ['author.lastName' : 'Last Name', 'author.firstName', 'First Name']
How can I dynamically call the following code--
booklist.eachWithIndex(){
key, value ->
println key.fields
}
The intent is that I can create my Map of fields and use a loop to display all data quickly without having to type all of the fields by hand.
Note - The period in the string 'author.lastName' throws an error when trying to output key['author.lastName'] too.

I don't recall the version of Groovy that came with Grails 1.1, but there are a number of language constructs to do things like this. If it's an old version, some things may not be available - so your mileage may vary.
Map keys can be referenced with quotes strings, e.g.
def map = [:]
map."person.name" = "Bob"
The above will have a key of person.name in the map.
Maps can contain anything, including mixed types in Groovy - so you really just need to work around string escapes or other special cases if you are using more complex keys.
You can also use a GString in the above
def map = [:]
def prop = "person.name"
map."${prop}" = "Bob"
You can also get a map of property/value off of a class dynamically by the properties field on it. E.g.:
class Person { String name;String location; }
def bob = new Person(name:'Bob', location:'The City')
def properties = bob.properties
properties.each { println it }

Related

Store phoneNumbers in Grails Domain Classes?

I want to store phone numbers in Grails domain classes. I am not sure what is the best way of doing this. Storing as int does not seems to be a good idea because leading zero is impossible for that.
What is the best way to store and validate phone numbers in Grails domain classes?
I would store phone as a String - nullable and blank too. For display purposes, simply provide your own tag in grails's tablib package.
For example, with a property inside some domain class like this:
String phone
And a taglib class like this:
class MyTagLib {
static defaultEncodeAs = [taglib:'html']
def phone334 = { attrs ->
String phone = attrs.phone
def formatted =
"(".concat(phone.substring(0, 3)).concat(") ")
.concat(phone.substring(3, 6)).concat("-").concat(phone.substring(6))
out << formatted
}
}
and a usage like this inside a gsp:
<g:phone334 phone="${theInstance.phone}" />
Then if phone = '4165557799', the output would be displayed like this: (416) 555-7799.
You can build as many formatters as you want; for example, if your number is 011218213334488 and you need it to look like +(218) 21 333 4488, simply build a formatter for that depending on the length and/or the pattern detected in the input.
You can also build simple validators right there too to make sure for example that all characters are made up of digits and parentheses and dashes, but I don't think taglibs are the right place for that - perform a bit of filtering and validation as suggested in the other posts before getting to displaying what should be correct input material.
You could most probably use matches constraint and store phone numbers as String as there is no predefined constraints for phone numbers. There in matches you can use any regex pattern required according to your needs.
static constraints = {
phone(matches: "^(?:0091|\\+91|0)[7-9][0-9]{9}$")
}
The above regex will work like :-
Begins with 0, +91 or 0091
Followed by a 7-9
Followed by exactly 9 numbers
Must match entire input
You can change it according to your needs.
You can store the phone number as string. To validate the phone number you can use google phone number java library to validate international numbers. Or more easily you can use this grails plugin in your code: https://github.com/ataylor284/grails-phonenumbers . Here is a sample from the plugin home page.
class MyDomain {
String phoneNumber
static constraints = {
phoneNumber(phoneNumber: true)
}
}
Edit:
To validate the number if it is not blank you have to define your custom constraint class which extends PhoneNumberConstraint class.
class CustomPhoneNumberConstraint extends PhoneNumberConstraint{
#Override
protected void processValidate(target, propertyValue, Errors errors) {
//check if phone number is blank
if (propertyValue instanceof String && GrailsStringUtils.isBlank((String)propertyValue)) {
if (!blank) {
super.processValidate(target,propertyValue, errors)
}
}
return true
}
}

Grails data binding - command objects with Lists

Grails 1.3.7
Trouble with data binding Command objects that have List content. Example Command:
class Tracker {
String name
String description
List<Unit> units = new ArrayList()
}
class Unit {
String name
Long unitMax
Long unitMin
}
create GSP for Tracker has the Unit fields. One example:
<g:textField name="units[0].unitMax" value=""/>
TrackerController save method:
def save = { Tracker trackerInstance ->
trackerInstance = trackingService.saveOrUpdateTracker(trackerInstance)
}
But, always java.lang.IndexOutOfBoundsException
Alternatively, if I update controller to:
def save = {
Tracker trackerInstance = new Tracker()
trackerInstance.properties = params
....
Then groovy.lang.ReadOnlyPropertyException: Cannot set readonly property: properties for class: com.redbrickhealth.dto.Tracker
Any ideas?
There seems to be a difference between binding in GORM vs Command objects.
Maybe I need to extend and register a PropertyEditorSupport for Unit?
-Todd
Since Groovy 1.8.7 the List interface has a method called withLazyDefault that can be used instead of apache commons ListUtils:
List<Unit> units = [].withLazyDefault { new Unit() }
This creates a new Unit instance every time units is accessed with a non-existent index.
See the documentation of withLazyDefault for more details. I also wrote a small blog post about this a few days ago.
Grails requires an command with existing list, that will be filled with data from reques.
If you know exact number of units, say 3, you can:
class Tracker {
String name
String description
List<Unit> units = [new Unit(), new Unit(), new Unit()]
}
or use LazyList from apache commons collections
import org.apache.commons.collections.ListUtils
import org.apache.commons.collections.Factory
class Tracker {
String name
String description
List<Unit> units = ListUtils.lazyList([], {new Unit()} as Factory)
}

my own id in GORM

I tried to change the standard 'id' in grails:
calls Book {
String id
String title
static mapping {
id generator:'assigned'
}
}
unfortunately, I soon noticed that this breaks my bootstrap. Instead of
new Book (id:'some ISBN', title:'great book').save(flush:true, failOnError:true)
I had to use
def b = new Book(title:'great book')
b.id = 'some ISBN'
b.save(flush:true, failOnError:true)
otherwise I get an 'ids for this class must be manually assigned before calling save()' error.
but that's ok so far.
I then encountered the same problem in the save action of my bookController. But this time, the workaround didn't do the trick.
Any suggestions?
I known, I can rename the id, but then I will have to change all scaffolded views...
That's a feature of databinding. You don't want submitted data to be able to change managed fields like id and version, so the Map constructor that you're using binds all available properties except those two (it also ignores any value for class, metaClass, and a few others).
So there's a bit of a mismatch here since the value isn't managed by Hibernate/GORM but by you. As you saw the workaround is that you need to create the object in two steps instead of just one.
I can't replicate this problem (used Grails 2.0.RC1). I think it might be as simple as a missing equal sign on your static mapping = { (you just have static mapping {)
Here's the code for a domain object:
class Book {
String id
String name
static mapping = {
id generator:'assigned'
}
}
And inside BootStrap.groovy:
def init = { servletContext ->
new Book(name:"test",id:"123abc").save(failOnError:true)
}
And it works fine for me. I see the id as 123abc.
You need to set the bindable constraint to true for your id prop, e.g.
class Employee {
Long id
String name
static constraints = {
id bindable: true
}
}

alternated default sort in GRAILS

why the following Groovy snippet returns alternating
[Account: 2222 and 2222, Account: 1111 and 1111] or
[Account: 1111 and 1111, Account: 2222 and 2222]
if you run it multiple times within the Groovy Console ?
I thought the sort statement leads to an ALWAYS descending sort order of the list ???
class Account {
long number
String code
String toString() {return "Account: $number and $code"}
static mapping = {
sort number:"desc"
}
}
List items = []
items << new Account(number:1111,code:'1111')
items << new Account(number:2222,code:'2222')
println items.sort()
Thanks in advance
Dominik
You don't define an ordering among your Account instances. The mapping directive is only applicable to GORM mapped classes (IOW: "domain objects"), and will only be used when loading instances of your class from the database AFAIK.
However, you are appending the objects to a plain List, which does not know about GORM properties. In order to sort lists of Account instances reliably in such a context, you will have to specify an explicit ordering, for example:
class Account implements Comparable {
...
def int compareTo(rhs) {
long onum = rhs.number;
return (onum > number)? -1 : ((onum < number)? 1 : 0);
}
...
}
This article has more information about the topic. As to why Groovy sorts the list differently on multiple calls to list.sort: well, I have no idea...
Grails has two main default ways to sort:
Sort when you query:
def airports = Airport.list(sort:'name')
Put a default sorting method on that object:
class Airport {
…
static mapping = {
sort name:"desc"
}
}
The above is taken from the grails documentation.

Setting Grails domain id in Bootstrap.groovy

Is it possible to explicitly set the id of a domain object in Grails' Bootstrap.groovy (or anywhere, for that matter)?
I've tried the following:
new Foo(id: 1234, name: "My Foo").save()
and:
def foo = new Foo()
foo.id = 1234
foo.name = "My Foo"
foo.save()
But in both cases, when I print out the results of Foo.list() at runtime, I see that my object has been given an id of 1, or whatever the next id in the sequence is.
Edit:
This is in Grails 1.0.3, and when I'm running my application in 'dev' with the built-in HSQL database.
Edit:
chanwit has provided one good solution below. However, I was actually looking for a way to set the id without changing my domain's id generation method. This is primarily for testing: I'd like to be able to set certain things to known id values either in my test bootstrap or setUp(), but still be able to use auto_increment or a sequence in production.
Yes, with manually GORM mapping:
class Foo {
String name
static mapping = {
id generator:'assigned'
}
}
and your second snippet (not the first one) will do the job (Id won't be assigned when passing it through constructor).
What I ended up using as a workaround was to not try and retrieve objects by their id. So for the example given in the question, I changed my domain object:
class Foo {
short code /* new field */
String name
static constraints = {
code(unique: true)
name()
}
}
I then used an enum to hold all of the possible values for code (which are static), and would retrieve Foo objects by doing a Foo.findByCode() with the appropriate enum value (instead of using Foo.get() with the id like I wanted to do previously).
It's not the most elegant solution, but it worked for me.
As an alternative, assuming that you're importing data or migrating data from an existing app, for test purposes you could use local maps within the Bootstrap file. Think of it like an import.sql with benefits ;-)
Using this approach:
you wouldn't need to change your domain constraints just for
testing,
you'll have a tested migration path from existing data, and
you'll have a good data slice (or full slice) for future integration tests
Cheers!
def init = { servletContext ->
addFoos()
addBars()
}
def foosByImportId = [:]
private addFoos(){
def pattern = ~/.*\{FooID=(.*), FooCode=(.*), FooName=(.*)}/
new File("import/Foos.txt").eachLine {
def matcher = pattern.matcher(it)
if (!matcher.matches()){
return;
}
String fooId = StringUtils.trimToNull(matcher.group(1))
String fooCode = StringUtils.trimToNull(matcher.group(2))
String fooName = StringUtils.trimToNull(matcher.group(3))
def foo = Foo.findByFooName(fooName) ?: new Foo(fooCode:fooCode,fooName:fooName).save(faileOnError:true)
foosByImportId.putAt(Long.valueOf(fooId), foo) // ids could differ
}
}
private addBars(){
...
String fooId = StringUtils.trimToNull(matcher.group(5))
def foo = foosByImportId[Long.valueOf(fooId)]
...
}

Resources