AggregationStrategy with calculated fields in a GroupTable at group level

Hi, I’m still trying to display a calculated field over two other fields in a table (a percentage) in a grouptable. The sums with aggregation work correctly but this calculation being a percentage on other fields that if they have been added you can not use sum, avg, count, max, min. I think I need to use an aggregation of my own and in the documentation the table shows an example with AggregationStrategy but I can not understand how it works or where to create the class metapropertyPath. Could we have a more clear example of its use?
Thanks and regards.

sumatorios

Hi,
To obtain MetaPropertyPath of an entity attribute you need to obtain MetaClass for that entity first and then just call metaClass.getPropertyPath(attributeName);. MetaClass can be obtained in several ways, e.g.

  1. From a datasource using getMetaClass() method.
  2. Using methods of MetaData interface, such as getClass()
    I’ve prepared the sample project on github.
    Regards,
    Gleb

Hi Gleb
Could you please give some hints how the following code would look like when the CustomerGrade is an Entity instead of Enum? Do I need to create a datasource?


public class CustomerGradeAggregation implements AggregationStrategy<CustomerGrade, String> {
    @Override
    public String aggregate(Collection<CustomerGrade> propertyValues) {
        CustomerGrade mostFrequent = null;
        long max = 0;
        if (CollectionUtils.isNotEmpty(propertyValues)) {
            for (CustomerGrade grade : CustomerGrade.values()) {
                long current = propertyValues.stream()
                        .filter(customerGrade -> customerGrade.equals(grade))
                        .count();

                if (current > max) {
                    mostFrequent = grade;
                    max = current;
                }
            }
        }

        if (mostFrequent != null) {
            Messages messages = AppBeans.get(Messages.NAME);
            return String.format("%s: %d/%d", messages.getMessage(mostFrequent), max, propertyValues.size());
        }

        return null;
    }

    @Override
    public Class<String> getResultClass() {
        return String.class;
    }
}

No, you don’t. AggregationStrategy interface is generic and can work with different types of data. You just need to implement your logic.

Regards,
Gleb