Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GRAILS-3911 - Criteria.list() sort nested properties #15

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,7 @@ public static void populateArgumentsForCriteria(Class<?> targetClass, Criteria c
if (caseArg instanceof Boolean) {
ignoreCase = (Boolean) caseArg;
}
if (ORDER_DESC.equals(order)) {
c.addOrder( ignoreCase ? Order.desc(sort).ignoreCase() : Order.desc(sort));
}
else {
c.addOrder( ignoreCase ? Order.asc(sort).ignoreCase() : Order.asc(sort) );
}
addOrder(c, sort, order, ignoreCase);
}
else {
Mapping m = GrailsDomainBinder.getMapping(targetClass);
Expand All @@ -216,7 +211,26 @@ public static void populateArgumentsForCriteria(Class<?> targetClass, Criteria c
}
}
}


/*
* Add order to criteria, creating necessary subCriteria if nested sort property (ie. sort:'nested.property').
*/
private static void addOrder(Criteria c, String sort, String order, boolean ignoreCase) {
int firstDotPos = sort.indexOf(".");
if (firstDotPos == -1) {
if (ORDER_DESC.equals(order)) {
c.addOrder( ignoreCase ? Order.desc(sort).ignoreCase() : Order.desc(sort));
}
else {
c.addOrder( ignoreCase ? Order.asc(sort).ignoreCase() : Order.asc(sort) );
}
} else {
Criteria subCriteria = c.createCriteria(sort.substring(0,firstDotPos));
String remainingSort = sort.substring(firstDotPos+1);
addOrder(subCriteria, remainingSort, order, ignoreCase); // Recurse on nested sort
}
}

/**
* Configures the criteria instance to cache based on the configured mapping.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package org.codehaus.groovy.grails.orm.hibernate

/**
* Test for GRAILS-3911
*/
class SortWithNestedPropertiesTests extends AbstractGrailsHibernateTests {

def bookClass

protected void onSetUp() {
gcl.parseClass '''
class Book {
Long id
Long version
String title
Author author
String publisher = 'Manning'
static namedQueries = {
manningBooks {
eq('publisher', 'Manning')
}
}

}

class Author {
Long id
Long version
String name
Person person
}

class Person {
Long id
Long version
String name
}
'''
}

protected void setUp() {
super.setUp()

def personClass = ga.getDomainClass('Person').clazz
def authorClass = ga.getDomainClass('Author').clazz
bookClass = ga.getDomainClass('Book').clazz
['C','A','b','a','c','B'].eachWithIndex { name, i ->
def person = personClass.newInstance(id:i, version:1, name:name).save(flush:true)
def author = authorClass.newInstance(id:i, version:1, name:name, person:person).save(flush:true)
bookClass.newInstance(id:i, version:1, title:'foo', author:author).save(flush:true)
}
}

void testListPersistentMethod() {
assertEquals( ['A','a','b','B','C','c'], bookClass.list(sort:'author.name').author.name )
assertEquals( ['A','B','C','a','b','c'], bookClass.list(sort:'author.name', ignoreCase:false).author.name )
}

void testHibernateNamedQueriesBuilder() {
assertEquals( ['A','a','b','B','C','c'], bookClass.manningBooks().list(sort:'author.name').author.name )
}

void testFindAllPersistentMethod() {
assertEquals( ['A','a','b','B','C','c'], bookClass.findAll([sort:'author.name']).author.name )
}

void testFindAllByPersistentMethod() {
assertEquals( ['A','a','b','B','C','c'], bookClass.findAllByPublisher('Manning', [sort:'author.name']).author.name )
}

void testFindByPersistentMethod() {
assertEquals( 'A', bookClass.findByPublisher('Manning', [sort:'author.name']).author.name )
}

void testDeepSort() {
assertEquals( ['A','a','b','B','C','c'], bookClass.list(sort:'author.person.name').author.person.name )
}

}