Search in sources :

Example 1 with AggregationStrategy

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

the class AbstractDataGridLoader method loadStrategyClass.

protected void loadStrategyClass(AggregationInfo aggregation, Element aggregationElement) {
    String strategyClass = aggregationElement.attributeValue("strategyClass");
    if (StringUtils.isNotEmpty(strategyClass)) {
        Class<?> aggregationClass = getClassManager().findClass(strategyClass);
        if (aggregationClass == null) {
            throw new GuiDevelopmentException(String.format("Class %s is not found", strategyClass), context);
        }
        try {
            Constructor<?> constructor = aggregationClass.getDeclaredConstructor();
            AggregationStrategy customStrategy = (AggregationStrategy) constructor.newInstance();
            applicationContext.getAutowireCapableBeanFactory().autowireBean(customStrategy);
            aggregation.setStrategy(customStrategy);
        } catch (Exception e) {
            throw new RuntimeException("Unable to instantiate strategy for aggregation", e);
        }
    }
}
Also used : GuiDevelopmentException(io.jmix.ui.GuiDevelopmentException) GuiDevelopmentException(io.jmix.ui.GuiDevelopmentException) AggregationStrategy(io.jmix.ui.component.data.aggregation.AggregationStrategy)

Example 2 with AggregationStrategy

use of io.jmix.ui.component.data.aggregation.AggregationStrategy 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 AggregationStrategy

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

the class AbstractTableLoader method loadAggregation.

@SuppressWarnings("unchecked")
protected void loadAggregation(Table.Column column, Element columnElement) {
    Element aggregationElement = columnElement.element("aggregation");
    if (aggregationElement != null) {
        AggregationInfo aggregation = new AggregationInfo();
        aggregation.setPropertyPath((MetaPropertyPath) column.getId());
        String aggregationType = aggregationElement.attributeValue("type");
        if (StringUtils.isNotEmpty(aggregationType)) {
            aggregation.setType(AggregationInfo.Type.valueOf(aggregationType));
        }
        String aggregationEditable = aggregationElement.attributeValue("editable");
        if (StringUtils.isNotEmpty(aggregationEditable)) {
            aggregation.setEditable(Boolean.parseBoolean(aggregationEditable));
        }
        String valueDescription = aggregationElement.attributeValue("valueDescription");
        if (StringUtils.isNotEmpty(valueDescription)) {
            column.setValueDescription(loadResourceString(valueDescription));
        }
        Formatter formatter = loadFormatter(aggregationElement);
        aggregation.setFormatter(formatter == null ? column.getFormatter() : formatter);
        String strategyClass = aggregationElement.attributeValue("strategyClass");
        if (StringUtils.isNotEmpty(strategyClass)) {
            Class<?> aggregationClass = getClassManager().findClass(strategyClass);
            if (aggregationClass == null) {
                throw new GuiDevelopmentException(String.format("Class %s is not found", strategyClass), context);
            }
            try {
                Constructor<?> constructor = aggregationClass.getDeclaredConstructor();
                AggregationStrategy customStrategy = (AggregationStrategy) constructor.newInstance();
                applicationContext.getAutowireCapableBeanFactory().autowireBean(customStrategy);
                aggregation.setStrategy(customStrategy);
            } catch (Exception e) {
                throw new RuntimeException("Unable to instantiate strategy for aggregation", e);
            }
        }
        if (aggregationType == null && strategyClass == null) {
            throw new GuiDevelopmentException("Incorrect aggregation - type or strategyClass is required", context);
        }
        column.setAggregation(aggregation);
    }
}
Also used : Formatter(io.jmix.ui.component.formatter.Formatter) Element(org.dom4j.Element) GuiDevelopmentException(io.jmix.ui.GuiDevelopmentException) AggregationInfo(io.jmix.ui.component.AggregationInfo) GuiDevelopmentException(io.jmix.ui.GuiDevelopmentException) AggregationStrategy(io.jmix.ui.component.data.aggregation.AggregationStrategy)

Aggregations

AggregationStrategy (io.jmix.ui.component.data.aggregation.AggregationStrategy)3 GuiDevelopmentException (io.jmix.ui.GuiDevelopmentException)2 AggregationInfo (io.jmix.ui.component.AggregationInfo)1 Aggregation (io.jmix.ui.component.data.aggregation.Aggregation)1 Formatter (io.jmix.ui.component.formatter.Formatter)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Element (org.dom4j.Element)1