Search in sources :

Example 1 with AggregationInfo

use of io.jmix.ui.component.AggregationInfo in project jmix by jmix-framework.

the class AggregatableDelegate method aggregateValues.

public Map<AggregationInfo, Object> aggregateValues(@Nullable AggregationInfo[] aggregationInfos, Collection<K> itemIds) {
    if (aggregationInfos == null || aggregationInfos.length == 0) {
        throw new NullPointerException("Aggregation must be executed at least by one field");
    }
    if (itemProvider == null || itemValueProvider == null) {
        throw new NullPointerException("ItemProvider and ItemValueProvider must be non-nulls");
    }
    Map<AggregationInfo, Object> aggregationResults = new HashMap<>();
    for (AggregationInfo aggregationInfo : aggregationInfos) {
        Object value = doPropertyAggregation(aggregationInfo, itemIds);
        aggregationResults.put(aggregationInfo, value);
    }
    return aggregationResults;
}
Also used : HashMap(java.util.HashMap) AggregationInfo(io.jmix.ui.component.AggregationInfo)

Example 2 with AggregationInfo

use of io.jmix.ui.component.AggregationInfo 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)

Example 3 with AggregationInfo

use of io.jmix.ui.component.AggregationInfo 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

AggregationInfo (io.jmix.ui.component.AggregationInfo)3 HashMap (java.util.HashMap)2 MetaPropertyPath (io.jmix.core.metamodel.model.MetaPropertyPath)1 Range (io.jmix.core.metamodel.model.Range)1 GuiDevelopmentException (io.jmix.ui.GuiDevelopmentException)1 Aggregation (io.jmix.ui.component.data.aggregation.Aggregation)1 AggregationStrategy (io.jmix.ui.component.data.aggregation.AggregationStrategy)1 Formatter (io.jmix.ui.component.formatter.Formatter)1 Locale (java.util.Locale)1 Element (org.dom4j.Element)1