SpringSource/spring-roo — pull request #56 by vbonamy (2013-08-21 12:43:21+00:00)
Title: Roo 241: Allow user to specify "order by" on dynamic finders

This Pull Request is a *proposal* for solving (at less a part of) the issue ROO-241 - https://jira.springsource.org/browse/ROO-241

# pagination
For the pagination support, the PR #55 is included in this PR.

# jpa and finder
* I added 2 parameters named "sortFieldName" and "sortOrder" to the finders methods.
* These parameters are just added directly on JPA Query, but to avoid JPQL injections, we test that the sortFieldName matches to a known field. This is done with a field fieldNames4OrderClauseFilter (this field can be override by the user/developper if he wants to use multiple fields in ORDER BY clause for example).

```java
public static final List Pet.fieldNames4OrderClauseFilter = java.util.Arrays.asList("sendReminders", "name", "weight", "owner", "type");

public static List Pet.findAllPets(String sortFieldName, String sortOrder) {
String jpaQuery = "SELECT o FROM Pet o";
if (fieldNames4OrderClauseFilter.contains(sortFieldName)) {
jpaQuery = jpaQuery + " ORDER BY " + sortFieldName;
if ("ASC".equalsIgnoreCase(sortOrder) || "DESC".equalsIgnoreCase(sortOrder)) {
jpaQuery = jpaQuery + " " + sortOrder;
}
}
return entityManager().createQuery(jpaQuery, Pet.class).getResultList();
}
```

# web mvc (jsp)

* We can use an url like this : http://localhost:8080/petclinic/pets?page=1&size=10&sortFieldName=name&sortOrder=desc for example

* We have also a new attribute "sort" in the column tag : it indicates if the column can be used to sort/order list (default 'false') - if 'true', a link is set on the header of the column with an url param 'sortFieldName'.

```html

```