groovy&grails- How to go over array of object? - grails

I have an array of Person object (person.first person.last).
To find out if the person have first name I have to "run" on the array.
I try the following but I got error:
person.eachWithIndex { String persons, i ->
if(persons.first=='')
println(''error)
}
How should I manipulate the object array?

Are you looking for something like:
class Person {
def first
def last
}
def persons = [ new Person(first:'',last:'last'), new Person(first:'john',last:'anon')]
persons.eachWithIndex { person, i ->
if(person.first==''){
println("error at person in position $i")
}
}
Since you doesn't add more details, I don't know what you're looking for when you say manipulate object array so there is some samples:
To manipulate the array objects you can add the statements in the each itself, for example to add a notDefined as a first for person where first=='' you can use:
persons.eachWithIndex { person, i ->
if(person.first==''){
person.first = 'notDefined'
println("first as notDefined for person in position $i")
}
}
To remove the elements where first =='' you can use removeAll method to remove the undesired elements from the array:
persons.removeAll { person ->
!person.first
}
EDIT BASED ON COMMENT:
If you want to remove null elements from your list, you can do it with the expression you use in your comment:
def persons = [ new Person(first:'pepe',last:'last'), null]
persons.removeAll([null])
println persons.size() // prints 1 since null element is removed
However seems that you're not trying to remove null elements instead you're trying to remove elements where all the properties are null, in your case you want to remove: new Person(first:null,last:null). To do so you can try with the follow code:
def persons = [ new Person(first:'pepe',last:'last'), new Person(first:null,last:null)]
persons.removeAll{ person ->
// get all Person properties wich value is not null
// without considering class propertie wich is implicit in all classes
def propsNotNull = person.properties.findAll { k,v -> v != null && k != 'class' }
// if list is empty... means that all properties are null
return propsNotNull.isEmpty()
}
println persons.size() // prints 1
Hope this helps,

Related

Grails namedQuery sort order by multiple columns

Given a namedQuery:
class MyDomainObject {
String someProperty
static namedQueries = {
myNamedQuery {
// some implementation here
}
}
}
I can use it to generate a list, sorted by a single key, like this (documentation for 2.4.3 here):
def resultsList = MyDomainObject.myNamedQuery.list(sort: "someProperty", order: "desc")
How do I order the results by multiple columns? I'd like to be able to define the sort parameters dynamically, not define them in the query.
I'm sure there's a better way, but I ended up creating another named query that I can concatenate onto my chosen one (I could always incorporate into the original query too).
// expects to be passed a List containing a series of Maps
orderByMultipleColumns { List columnsToSortBy ->
columnsToSortBy.each { Map field ->
order("${field.fieldName}", field.fieldOrder)
}
}
// usage:
List orderByList = []
// ...
// within some loop that I use:
orderByList << [fieldName: someValue, fieldOrder: dir] // dir == 'asc' or 'desc'
// ...
MyDomainObject.myNamedQuery().orderByMultipleColumns(orderList).listDistinct(max: length, offset: start)

Groovy removeAll closure not removing objects in Set

I'm using data binding with parent/child relationships in Grails 2.3.7 and am having trouble with deletes. The form has many optional children, and to keep the database tidy I'd like to purge blank (null) values. I've found some nice articles which suggest using removeAll to filter my entries but I can't get remove or removeAll to work!
For example... (Parent has 10 children, 5 are blank)
def update(Parent parent) {
parent.children.getClass() // returns org.hibernate.collection.PersistentSet
parent.children.size() // returns 10
parent.children.findAll{ it.value == null }.size() // returns 5
parent.children.removeAll{ it.value == null } // returns TRUE
parent.children.size() // Still returns 10!!!
}
I've read PersistentSet is finicky about equals() and hashCode() being implemented manually, which I've done in every domain class. What baffles me is how removeAll can return true, indicating the Collection has changed, yet it hasn't. I've been stuck on this for a couple days now so any tips would be appreciated. Thanks.
Update:
I've been experimenting with the Child hashcode and that seems to be the culprit. If I make a bare-bones hashcode based on the id (bad practice) then removeAll works, but if I include the value it stops working again. For example...
// Sample 1: Works with removeAll
int hashCode() {
int hash1 = id.hashCode()
return hash1
}
// Sample 2: Doesn't work with removeAll
int hashCode() {
int hash1 = id.hashCode()
int hash2 = value == null ? 0 : value.hashCode()
return hash1 + hash2
}
// Sample Domain classes (thanks Burt)
class Parent {
static hasMany = [children: Child]
}
class Child {
String name
String value
static constraints = {
value nullable: true
}
}
This behavior is explained by the data binding step updating data, making it dirty. (ie: child.value.isDirty() == true) Here's how I understand it.
First Grails data binding fetches the Parent and children, and the hashcode of each Child is calculated. Next, data updates are applied which makes child.value dirty (if it changed) but the Set's hashcodes remain unchanged. When removeAll finds a match it builds a hashCode with the dirty data, but that hashcode is NOT found in the Set so it can't remove it. Essentially removeAll will only work if ALL of my hashCode variables are clean.
So if the data must be clean to remove it, one solution is to save it twice. Like this...
// Parent Controller
def update(Parent parent) {
parent.children.removeAll{ it.value == null } // Removes CLEAN children with no value
parent.save(flush:true)
parent.refresh() // parent.children is now clean
parent.children.removeAll{ it.value == null } // Removes (formerly dirty) children
parent.save(flush:true) // Success!
}
This works though it's not ideal. First I must allow null values in the database, though they only exist briefly and I don't want them. And second it's kinda inefficient to do two saves. Surely there must be a better way?
hashCode and equals weirdness aren't an issue here - there are no contains calls or something similar that would use the hashCode value and potentially miss the actual data. If you look at the implementation of removeAll you can see that it uses an Iterator to call your closure on every instance and remove any where the closure result is truthy, and return true if at least one was removed. Using this Parent class
class Parent {
static hasMany = [children: Child]
}
and this Child
class Child {
String name
String value
static constraints = {
value nullable: true
}
}
and this code to create test instances:
def parent = new Parent()
5.times {
parent.addToChildren(name: 'c' + it)
}
5.times {
parent.addToChildren(name: 'c2' + it, value: 'asd')
}
parent.save()
it prints 5 for the final size(). So there's probably something else affecting this. You shouldn't have to, but you can create your own removeAll that does the same thing, and if you throw in some println calls you might figure out what's up:
boolean removeAll(collection, Closure remove) {
boolean atLeastOne = false
Iterator iter = collection.iterator()
while (iter.hasNext()) {
def c = iter.next()
if (remove(c)) {
iter.remove()
atLeastOne = true
}
}
atLeastOne
}
Invoke this as
println removeAll(parent.children) { it.value == null }

Need help improving how objects are saved from params in my app

So, I have a form that collects a bunch of radio selections and checkboxes and I need to build a series of objects based on what gets returned, which might look like this:
[thingid:1, 13:30,14:33, 11:26, 12:78, action:save, controller:userThing]
One object is created from the thingid, and the integer value pairs are ids of 2 other objects that are used to create n additional objects, so right now I'm looping through the params with an each() and filtering the non integer pairs with a long if expression, and then saving the object I need :
params.each {
key, value ->
if (key=="submit" | key=="action" | key=="thingid" | key=="controller"){}else{
def prop = ThingProperty.find(){
id == key
}
def level = ThingLevel.find(){
id == value
}
new UserThingScore(userthing: userthing,thingproperty: prop ,thinglevel: level).save(flush:true)
}
}
It works, in that it creates all the necessary objects correctly, but this just seems ridiculous to me, and I know there must be a better way... is there someway I can group form elements so they get returned like this?:
[thingid:1, integerpairs:[13:30,14:33, 11:26, 12:78],action:save,controller:userThing]
An alternative might be:
def userThingList = params.keySet().grep( Integer ).collect { it ->
new UserThingScore( userthing: userthing,
thingproperty: ThingProperty.get( it ),
thinglevel: ThingLevel.get( params[ it ] ) )
}
userThingList*.save()

Find all instances of a type associated with another type

I have some classes that look like this:
class User {
boolean enabled
String username
}
class ExampleClass {
User firstUser
User secondUser
}
My end goal is to find all instances of User where enabled == true OR the instance of User is associated with ExampleClass.
Where this code is running I don't have access to the variable names firstUser or secondUser.
With that said, I need to be able to find all instance of User associated with ExampleClass, disregarding which variable (firstUser or secondUser) the association was made through. How do I do this?
UPDATE:
The best I can come up with is this method in my User domain class. I the example I gave above I have an ExampleClass which has multiple fields of the User type. In fact I have multiple classes with multiple fields of the User type. This is why I get the domain class from the object being passed in instead of just typing ExampleClass.
static List findAllEnabledOrAssociatedWith( Object obj = null ) {
if( obj?.id ) { // Make sure the object in question has been saved to database.
List list = []
obj.domainClass.getPersistentProperties().each {
if( it.getReferencedPropertyType() == User ) {
def propertyName = it.getName()
list += User.executeQuery( "SELECT DISTINCT ${propertyName} FROM ${obj.class.getSimpleName()} obj INNER JOIN obj.${propertyName} ${propertyName} WHERE obj =:obj", [ obj: obj ] )
}
}
list.unique()
return User.executeQuery( "SELECT DISTINCT users FROM User users WHERE users.enabled=true OR users IN(:list)", [ list: list ] )
} else {
return User.executeQuery( "SELECT DISTINCT users FROM User users WHERE users.enabled=true" )
}
}
def sql = '''
from
User u,
ExampleClass ex
where
u.enabled = 1 or (u = ex.firstUser or u = ex.secondUser)'''
def users = User.findAll(sql)

How would I find the text of a node that has a specific value for an attribute in groovy?

I'm using XMLSlurper. My code is below (but does not work). The problem is that it fails when it hits a node that does not have the attribute "id". How do I account for this?
//Parse XML
def page = new XmlSlurper(false,false).parseText(xml)
//Now save the value of the proper node to a property (this fails)
properties[ "finalValue" ] = page.find {
it.attributes().find { it.key.equalsIgnoreCase( 'id' ) }.value == "myNode"
};
I just need to account for nodes without "id" attribute so it doesn't fail. How do I do that?
You could alternatively use the GPath notation, and check if "#id" is empty first.
The following code snippet finds the last element (since the id attribute is "B" and the value is also "bizz", it prints out "bizz" and "B").
def xml = new XmlSlurper().parseText("<foo><bar>bizz</bar><bar id='A'>bazz</bar><bar id='B'>bizz</bar></foo>")
def x = xml.children().find{!it.#id.isEmpty() && it.text()=="bizz"}
println x
println x.#id
Apprently I can get it to work when I simply use depthFirst. So:
properties[ "finalValue" ] = page.depthFirst().find {
it.attributes().find { it.key.equalsIgnoreCase( 'id' ) }.value == "myNode"
};

Resources