Grails namedQuery sort order by multiple columns - grails

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)

Related

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

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,

Grails Criteria dynamic AND conditions for one-to-many relationship

I have a domain class
class Url {
UUID id
String url
static hasMany = [
indications:UrlIndication
]
...
}
And
class UrlIndication {
UUID id
String name
static belongsTo = Url
...
}
I want to choose urls so that it has all the necessary UrlIndication elements in a given list indicationsId.
For that I use an association and criteria like this one:
indications {
and {
indicationsId.each{
indication->
eq ('id',UUID.fromString(indication as String))
}
}
}
However, all I got is an empty result. Can you suggest any modifications/ other methods so that I can do this? Thanks in advance
Your query returned an empty list because it's the equivalent of the expression (pseudo-code): if 1 = 1 and 1 = 2 and 1 = 3
Such an expression would always be false. in or inList would not work for the reason #innovatism described.
In theory, Criteria's eqAll() or HQL's = ALL would work. But, I don't know for sure because I could not get either one to work.
What will work is to use inList to return a subset of Urls: those which contain at least one of the UrlIndication IDs. Then use Groovy's containsAll() to finish the job.
def ids = indicationsId.collect { UUID.fromString(it as String) }
Url.createCriteria()
.buildCriteria {
indications {
inList 'id', ids
}
}
.setResultTransformer(org.hibernate.Criteria.DISTINCT_ROOT_ENTITY)
.list()
.findAll {
it.indications.id.containsAll(ids)
}
Since the query has the potential to return duplicate Url instances, the ResultTransformer is set to return a unique list.
Finally, findAll() is used along with containsAll() to filter the list further.
Using eqAll (maybe)
Something like the following might work. Something funky is going on with Grails' HibernateCriteriaBuilder that causes the eqAll method to look up properties in the root entity; completely ignoring the sub criteria. So the following uses Hibernate directly. It didn't work for me, but it's as close as I could get. And it gave me a head-ache!
Url.createCriteria().buildCriteria {}
.createCriteria('indications', 'i')
.add(org.hibernate.criterion.Property.forName('i.id').eqAll(org.hibernate.criterion.DetachedCriteria.forClass(UrlIndication)
.add(org.hibernate.criterion.Restrictions.in('id', ids))
.setProjection(org.hibernate.criterion.Property.forName('id'))
))
.setResultTransformer(org.hibernate.Criteria.DISTINCT_ROOT_ENTITY)
.list()
The problem I had is I could not get Restrictions.in to work. Restrictions.eq works fine.
the in clause should do:
indications {
'in' 'id', indicationsId.collect{ UUID.fromString indication.toString() }
}

How to save a collection all at once in Grails

For example I need to retrieve several registers in a table, and edit a field, but it takes too long to save all with a loop, does exist a better way to save?
This how I do it....
class Table
static mapping = {
table "TABLEEX"
id generator:'sequence', params:[sequence:'TABLEEX_SEQ']
}
// identificacion
String data1
String data2
}
And searching the data:
def stuff = Table.createCriteria().list{
eq("data1","1")
}
And editing and saving
stuff.each {
it.data2 = "aaa"
it.save()
}
It isn't clear why you are retrieving the objects to begin with. Is something like this what you are looking for?
Table.executeUpdate("update Table t set t.data2=:newData where t.data1=:oldData", [newData: 'BAR', oldData: 'FOO'])
EDIT
You could also do something like this...
def query = Table.where {
data1 == 'FOO'
}
int total = query.updateAll(data2:'BAR')
Hibernate (the underlying mechanism of gorm, the grails orm) does not support that.
You'll have to iterate over every element and save or implement it yourself (and that will not make it faster).

How do I write a createCriteria in grails which pull only few columns from the table instead of all columns?

How do I write a createCriteria in grails which pull only few columns from the table instead of all columns?
I have a table called Ads. I want to retrieve only columns "Title" , "Price" and "Photo".
def c = Classified.createCriteria()
def records = c.list {
eq('publish_date', '2014-06-06')
}
maxResults(8)
}
Above query retrieves all the records. How to restrict to only few columns?
You can use projections to achieve this - at the simplest
projections {
property('title')
property('price')
property('photo')
}
would cause c.list to return a list of three-element lists, where records[n][0] is the title, records[n][1] is the price etc. If you want to be able to access the properties by name rather than by number then you need to assign aliases and use a result transformer
import org.hibernate.transform.AliasToEntityMapResultTransformer
def c = Classified.createCriteria()
def records = c.list {
eq('publish_date', '2014-06-06')
maxResults(8)
projections {
// first param is the property name, second is the alias definition -
// typically you'd leave them the same but here I make them different
// for demonstration purposes
property('title', 'ttl')
property('price', 'cost')
property('photo', 'picture')
}
resultTransformer(AliasToEntityMapResultTransformer.INSTANCE)
}
Now records will be a list of maps rather than a list of lists, and you can access the projected properties by alias name - records[n].ttl, records[n].cost, etc.
Try this:
def records = Classified.withCriteria {
eq('publish_date', '2014-06-06')
projections {
property('title')
property('price')
property('photo')
}
maxResults(8)
}

How can I avoid redundancy in nearly identically criteria?

Imagine I have the following query:
def result = Test.createCriteria().list(params) {
// image here a lot of assocs, criterias, ...
}
In many cases you need the row count of the query above, e.g. list actions of many controllers.
def resultTotal = Test.createCriteria().list(params) {
// Imagine here a lot of assocs, criterias, ...
// Change to the criteria above is only the projection block
projections { rowCount() }
}
How can I avoid this?
You can:
Extract the Criteria creation to a conditional factory/builder method;
Use named queries;
Use named query parameters (and Groovy code!) to alter the query "on the fly", like:
.
static namedQueries = {
byLocation { Location location ->
if (location) {
... // some more criteria logic
}
}
}
If you aren't paginating the results of the query, you can simply do the following after the first query is invoked:
def resultTotal = result?.size()
For the same set of query being used at many places, I create them as a closure and change its delegate to the criteria in question.
For example :
def query = {
projections{
rowCount()
}
eq('type', myType)
}
def criteria = Customer.createCriteria()
query.delegate = criteria
myType = CustomerType.guest
List records = criteria.list(params) {
query()
}
I use totalCount like this:
def list() {
def myInstanceList = MyInstance.createCriteria().list(params){
eq('name', params.name)
}
[myInstanceList: myInstanceList, myInstanceListTotal: myInstanceList.totalCount]
}

Resources