Search in sources :

Example 1 with Aggregation

use of com.haulmont.cuba.gui.data.aggregation.Aggregation in project cuba by cuba-platform.

the class AggregatableDelegate method doAggregation.

protected Map<AggregationInfo, String> doAggregation(Collection<K> itemIds, AggregationInfo[] aggregationInfos) {
    final Map<AggregationInfo, String> aggregationResults = new HashMap<>();
    for (AggregationInfo aggregationInfo : aggregationInfos) {
        final Object value = doPropertyAggregation(aggregationInfo, itemIds);
        String formattedValue;
        if (aggregationInfo.getFormatter() != null) {
            // noinspection unchecked
            formattedValue = aggregationInfo.getFormatter().format(value);
        } else {
            MetaPropertyPath propertyPath = aggregationInfo.getPropertyPath();
            final Range range = propertyPath.getRange();
            if (range.isDatatype()) {
                if (aggregationInfo.getType() != AggregationInfo.Type.COUNT) {
                    Class resultClass;
                    if (aggregationInfo.getStrategy() == null) {
                        Class rangeJavaClass = propertyPath.getRangeJavaClass();
                        Aggregation aggregation = Aggregations.get(rangeJavaClass);
                        resultClass = aggregation.getResultClass();
                    } else {
                        resultClass = aggregationInfo.getStrategy().getResultClass();
                    }
                    UserSessionSource userSessionSource = AppBeans.get(UserSessionSource.NAME);
                    Locale locale = userSessionSource.getLocale();
                    formattedValue = Datatypes.getNN(resultClass).format(value, locale);
                } else {
                    formattedValue = value.toString();
                }
            } else {
                if (aggregationInfo.getStrategy() != null) {
                    Class resultClass = aggregationInfo.getStrategy().getResultClass();
                    UserSessionSource userSessionSource = AppBeans.get(UserSessionSource.NAME);
                    Locale locale = userSessionSource.getLocale();
                    formattedValue = Datatypes.getNN(resultClass).format(value, locale);
                } else {
                    formattedValue = value.toString();
                }
            }
        }
        aggregationResults.put(aggregationInfo, formattedValue);
    }
    return aggregationResults;
}
Also used : Aggregation(com.haulmont.cuba.gui.data.aggregation.Aggregation) UserSessionSource(com.haulmont.cuba.core.global.UserSessionSource) MetaPropertyPath(com.haulmont.chile.core.model.MetaPropertyPath) AggregationInfo(com.haulmont.cuba.gui.components.AggregationInfo) Range(com.haulmont.chile.core.model.Range)

Example 2 with Aggregation

use of com.haulmont.cuba.gui.data.aggregation.Aggregation in project cuba by cuba-platform.

the class AggregatableDelegate method doPropertyAggregation.

@SuppressWarnings("unchecked")
protected Object doPropertyAggregation(AggregationInfo aggregationInfo, Collection<K> itemIds) {
    List items = valuesByProperty(aggregationInfo.getPropertyPath(), itemIds);
    if (aggregationInfo.getStrategy() == null) {
        Class rangeJavaClass = aggregationInfo.getPropertyPath().getRangeJavaClass();
        Aggregation aggregation = Aggregations.get(rangeJavaClass);
        switch(aggregationInfo.getType()) {
            case COUNT:
                return aggregation.count(items);
            case AVG:
                return aggregation.avg(items);
            case MAX:
                return aggregation.max(items);
            case MIN:
                return aggregation.min(items);
            case SUM:
                return aggregation.sum(items);
            default:
                throw new IllegalArgumentException(String.format("Unknown aggregation type: %s", aggregationInfo.getType()));
        }
    } else {
        AggregationStrategy strategy = aggregationInfo.getStrategy();
        return strategy.aggregate(items);
    }
}
Also used : Aggregation(com.haulmont.cuba.gui.data.aggregation.Aggregation) AggregationStrategy(com.haulmont.cuba.gui.data.aggregation.AggregationStrategy)

Aggregations

Aggregation (com.haulmont.cuba.gui.data.aggregation.Aggregation)2 MetaPropertyPath (com.haulmont.chile.core.model.MetaPropertyPath)1 Range (com.haulmont.chile.core.model.Range)1 UserSessionSource (com.haulmont.cuba.core.global.UserSessionSource)1 AggregationInfo (com.haulmont.cuba.gui.components.AggregationInfo)1 AggregationStrategy (com.haulmont.cuba.gui.data.aggregation.AggregationStrategy)1