How to sort complex meta property


I have a Rebate entity that captures: price, quantity and rebateAmount. It also has a meta property: discountPercent, which is calculated as: rebateAmount / (price * quantity).

I would like my screen table to be able to sort on this meta property properly.

I tried creating a CustomSortExpressionProvider like here but the discountPercent property is never passed to this method. Only the related properties are passed (rebateAmount, price and quantity). I was hoping to return the calculation formula as the order by clause. Is there any way to override the screen so the meta property is passed?

As an alternative, is it possible to listen to what column the user is sorting on and if a meta property column, then modify the data collection query to order by the formula?

I’m looking for any solution that will get me the result I need. Any help is appreciated. Thanks.

I can’t say this is the best way to do it but this is how I made it work.

Instead of customizing DefaultJpqlSortExpressionProvider, I customized SortJpqlGenerator, which is called earlier, before the framework determines if it is a meta property. Groovy code follows:

class CustomSortJpqlGenerator extends SortJpqlGenerator {
    protected List<String> getPropertySortExpressions(MetaPropertyPath metaPropertyPath, boolean sortDirectionAsc) {
        Class theClass = metaPropertyPath.getMetaClass().getJavaClass()
        String theProp = metaPropertyPath.toString()
        if(theClass == Rebate.class && theProp == "discountPercent") {
            return "({E}.rebateAmount / ({E}.price * {E}.quantity))"
        return getPropertySortExpressions(metaPropertyPath, sortDirectionAsc)

The class can be overridden by adding this file to the spring.xml file:

<bean id="cuba_SortJpqlGenerator" class=""/>