Search in sources :

Example 1 with Aggregation

use of io.jmix.ui.component.data.aggregation.Aggregation in project jmix by jmix-framework.

the class AbstractTable method getParsedAggregationValue.

@Nullable
protected Object getParsedAggregationValue(String value, Object columnId) throws ParseException {
    Object parsedValue = value;
    for (Column column : getColumns()) {
        if (column.getId().equals(columnId)) {
            AggregationInfo aggregationInfo = column.getAggregation();
            if (aggregationInfo == null || aggregationInfo.getFormatter() != null) {
                return parsedValue;
            }
            MetaPropertyPath propertyPath = aggregationInfo.getPropertyPath();
            Class<?> resultClass;
            Range range = propertyPath != null ? propertyPath.getRange() : null;
            if (range != null && range.isDatatype()) {
                if (aggregationInfo.getType() == AggregationInfo.Type.COUNT) {
                    return parsedValue;
                }
                if (aggregationInfo.getStrategy() == null) {
                    Class<?> rangeJavaClass = propertyPath.getRangeJavaClass();
                    Aggregation aggregation = aggregations.get(rangeJavaClass);
                    resultClass = aggregation.getResultClass();
                } else {
                    resultClass = aggregationInfo.getStrategy().getResultClass();
                }
            } else if (aggregationInfo.getStrategy() == null) {
                return parsedValue;
            } else {
                resultClass = aggregationInfo.getStrategy().getResultClass();
            }
            parsedValue = datatypeRegistry.get(resultClass).parse(value, locale);
            break;
        }
    }
    return parsedValue;
}
Also used : Aggregation(io.jmix.ui.component.data.aggregation.Aggregation) MetaPropertyPath(io.jmix.core.metamodel.model.MetaPropertyPath) Range(io.jmix.core.metamodel.model.Range) Nullable(javax.annotation.Nullable)

Example 2 with Aggregation

use of io.jmix.ui.component.data.aggregation.Aggregation in project jmix by jmix-framework.

the class AggregatableDelegate method doPropertyAggregation.

@SuppressWarnings("unchecked")
protected Object doPropertyAggregation(AggregationInfo aggregationInfo, Collection<K> itemIds) {
    List items;
    if (aggregationInfo.getType() == AggregationInfo.Type.CUSTOM && aggregationInfo.getPropertyPath() == null) {
        // use items in this case;
        items = itemIds.stream().map(itemProvider::apply).collect(Collectors.toList());
    } else {
        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(io.jmix.ui.component.data.aggregation.Aggregation) ArrayList(java.util.ArrayList) List(java.util.List) AggregationStrategy(io.jmix.ui.component.data.aggregation.AggregationStrategy)

Example 3 with Aggregation

use of io.jmix.ui.component.data.aggregation.Aggregation in project jmix by jmix-framework.

the class AggregatableDelegate method doAggregation.

protected Map<AggregationInfo, String> doAggregation(Collection<K> itemIds, AggregationInfo[] aggregationInfos) {
    Map<AggregationInfo, String> aggregationResults = new HashMap<>();
    for (AggregationInfo aggregationInfo : aggregationInfos) {
        final Object value = doPropertyAggregation(aggregationInfo, itemIds);
        String formattedValue;
        if (aggregationInfo.getFormatter() != null) {
            formattedValue = aggregationInfo.getFormatter().apply(value);
        } else {
            // propertyPath could be null in case of custom aggregation
            MetaPropertyPath propertyPath = aggregationInfo.getPropertyPath();
            Range range = propertyPath != null ? propertyPath.getRange() : null;
            if (range != null && 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();
                    }
                    Locale locale = currentAuthentication.getLocale();
                    formattedValue = datatypeRegistry.get(resultClass).format(value, locale);
                } else {
                    formattedValue = value.toString();
                }
            } else {
                if (aggregationInfo.getStrategy() != null) {
                    Class resultClass = aggregationInfo.getStrategy().getResultClass();
                    Locale locale = currentAuthentication.getLocale();
                    formattedValue = datatypeRegistry.get(resultClass).format(value, locale);
                } else {
                    formattedValue = value.toString();
                }
            }
        }
        aggregationResults.put(aggregationInfo, formattedValue);
    }
    return aggregationResults;
}
Also used : Aggregation(io.jmix.ui.component.data.aggregation.Aggregation) Locale(java.util.Locale) HashMap(java.util.HashMap) MetaPropertyPath(io.jmix.core.metamodel.model.MetaPropertyPath) AggregationInfo(io.jmix.ui.component.AggregationInfo) Range(io.jmix.core.metamodel.model.Range)

Aggregations

Aggregation (io.jmix.ui.component.data.aggregation.Aggregation)3 MetaPropertyPath (io.jmix.core.metamodel.model.MetaPropertyPath)2 Range (io.jmix.core.metamodel.model.Range)2 AggregationInfo (io.jmix.ui.component.AggregationInfo)1 AggregationStrategy (io.jmix.ui.component.data.aggregation.AggregationStrategy)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Locale (java.util.Locale)1 Nullable (javax.annotation.Nullable)1