Search in sources :

Example 1 with GroupTableItems

use of com.haulmont.cuba.gui.components.data.GroupTableItems in project cuba by cuba-platform.

the class ExcelExporter method createGroupRow.

protected int createGroupRow(GroupTable table, List<Table.Column> columns, int rowNumber, GroupInfo groupInfo, int groupNumber) {
    GroupTableItems<Entity> groupTableSource = (GroupTableItems) table.getItems();
    Row row = sheet.createRow(rowNumber);
    Map<Object, Object> aggregations = table.isAggregatable() ? table.getAggregationResults(groupInfo) : Collections.emptyMap();
    int i = 0;
    int initialGroupNumber = groupNumber;
    for (Table.Column column : columns) {
        if (i == initialGroupNumber) {
            Cell cell = row.createCell(i);
            Object val = groupInfo.getValue();
            if (val == null) {
                val = messages.getMessage(getClass(), "excelExporter.empty");
            }
            Collection children = groupTableSource.getGroupItemIds(groupInfo);
            if (children.isEmpty()) {
                return rowNumber;
            }
            Integer groupChildCount = null;
            if (table.isShowItemsCountForGroup()) {
                groupChildCount = children.size();
            }
            Object captionValue = val;
            Element xmlDescriptor = column.getXmlDescriptor();
            if (xmlDescriptor != null && StringUtils.isNotEmpty(xmlDescriptor.attributeValue("captionProperty"))) {
                String captionProperty = xmlDescriptor.attributeValue("captionProperty");
                Object itemId = children.iterator().next();
                Instance item = groupTableSource.getItemNN(itemId);
                captionValue = item.getValueEx(captionProperty);
            }
            GroupTable.GroupCellValueFormatter<Entity> groupCellValueFormatter = table.getGroupCellValueFormatter();
            if (groupCellValueFormatter != null) {
                // disable separate "(N)" printing
                groupChildCount = null;
                List<Entity> groupItems = ((Collection<Object>) groupTableSource.getGroupItemIds(groupInfo)).stream().map(groupTableSource::getItem).collect(Collectors.toList());
                GroupTable.GroupCellContext<Entity> cellContext = new GroupTable.GroupCellContext<>(groupInfo, captionValue, metadataTools.format(captionValue), groupItems);
                captionValue = groupCellValueFormatter.format(cellContext);
            }
            MetaPropertyPath columnId = (MetaPropertyPath) column.getId();
            formatValueCell(cell, captionValue, columnId, groupNumber++, rowNumber, 0, groupChildCount);
        } else {
            AggregationInfo agr = column.getAggregation();
            if (agr != null) {
                Object key = agr.getPropertyPath() != null ? agr.getPropertyPath() : column.getId();
                Object aggregationResult = aggregations.get(key);
                if (aggregationResult != null) {
                    Cell cell = row.createCell(i);
                    formatValueCell(cell, aggregationResult, null, i, rowNumber, 0, null);
                }
            }
        }
        i++;
    }
    int oldRowNumber = rowNumber;
    List<GroupInfo> children = groupTableSource.getChildren(groupInfo);
    if (children.size() > 0) {
        for (GroupInfo child : children) {
            rowNumber = createGroupRow(table, columns, ++rowNumber, child, groupNumber);
        }
    } else {
        Collection<?> itemIds = groupTableSource.getGroupItemIds(groupInfo);
        for (Object itemId : itemIds) {
            createRow(table, columns, groupNumber, ++rowNumber, itemId);
        }
    }
    if (checkIsRowNumberExceed(rowNumber)) {
        sheet.groupRow(oldRowNumber + 1, excelOptions.getMaxRowCount());
    } else {
        sheet.groupRow(oldRowNumber + 1, rowNumber);
    }
    return rowNumber;
}
Also used : Entity(com.haulmont.cuba.core.entity.Entity) Instance(com.haulmont.chile.core.model.Instance) Element(org.dom4j.Element) MetaPropertyPath(com.haulmont.chile.core.model.MetaPropertyPath) GroupTableItems(com.haulmont.cuba.gui.components.data.GroupTableItems) Table(com.haulmont.cuba.gui.components.Table) GroupInfo(com.haulmont.cuba.gui.data.GroupInfo)

Example 2 with GroupTableItems

use of com.haulmont.cuba.gui.components.data.GroupTableItems in project cuba by cuba-platform.

the class WebGroupTable method collectItemIds.

protected void collectItemIds(GroupInfo groupId, final List<Object> itemIds) {
    GroupTableItems<E> groupTableSource = (GroupTableItems<E>) getItems();
    if (groupTableSource.hasChildren(groupId)) {
        final List<GroupInfo> children = groupTableSource.getChildren(groupId);
        for (final GroupInfo child : children) {
            itemIds.add(child);
            collectItemIds(child, itemIds);
        }
    } else {
        itemIds.addAll(groupTableSource.getGroupItemIds(groupId));
    }
}
Also used : GroupInfo(com.haulmont.cuba.gui.data.GroupInfo) GroupTableItems(com.haulmont.cuba.gui.components.data.GroupTableItems)

Example 3 with GroupTableItems

use of com.haulmont.cuba.gui.components.data.GroupTableItems in project cuba by cuba-platform.

the class ExcelExporter method exportTable.

public void exportTable(Table<Entity> table, List<Table.Column> columns, Boolean exportExpanded, @Nullable ExportDisplay display, @Nullable List<String> filterDescription, @Nullable String fileName, ExportMode exportMode) {
    if (display == null) {
        throw new IllegalArgumentException("ExportDisplay is null");
    }
    createWorkbookWithSheet();
    createFonts();
    createFormats();
    int r = 0;
    if (filterDescription != null) {
        for (r = 0; r < filterDescription.size(); r++) {
            String line = filterDescription.get(r);
            Row row = sheet.createRow(r);
            if (r == 0) {
                RichTextString richTextFilterName = excelExportHelper.createRichTextString(line);
                richTextFilterName.applyFont(boldFont);
                row.createCell(0).setCellValue(richTextFilterName);
            } else {
                row.createCell(0).setCellValue(line);
            }
        }
        r++;
    }
    Row row = sheet.createRow(r);
    createAutoColumnSizers(columns.size());
    float maxHeight = sheet.getDefaultRowHeightInPoints();
    CellStyle headerCellStyle = wb.createCellStyle();
    headerCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
    for (Table.Column column : columns) {
        String caption = column.getCaption();
        int countOfReturnSymbols = StringUtils.countMatches(caption, "\n");
        if (countOfReturnSymbols > 0) {
            maxHeight = Math.max(maxHeight, (countOfReturnSymbols + 1) * sheet.getDefaultRowHeightInPoints());
            headerCellStyle.setWrapText(true);
        }
    }
    row.setHeightInPoints(maxHeight);
    for (int c = 0; c < columns.size(); c++) {
        Table.Column column = columns.get(c);
        String caption = column.getCaption();
        Cell cell = row.createCell(c);
        RichTextString richTextString = excelExportHelper.createRichTextString(caption);
        richTextString.applyFont(boldFont);
        cell.setCellValue(richTextString);
        ExcelAutoColumnSizer sizer = new ExcelAutoColumnSizer();
        sizer.notifyCellValue(caption, boldFont);
        sizers[c] = sizer;
        cell.setCellStyle(headerCellStyle);
    }
    TableItems<Entity> tableItems = table.getItems();
    if (exportMode == ExportMode.SELECTED_ROWS && table.getSelected().size() > 0) {
        Set<Entity> selected = table.getSelected();
        List<Entity> ordered = tableItems.getItemIds().stream().map(tableItems::getItem).filter(selected::contains).collect(Collectors.toList());
        for (Entity item : ordered) {
            if (checkIsRowNumberExceed(r)) {
                break;
            }
            createRow(table, columns, 0, ++r, item.getId());
        }
    } else {
        if (table.isAggregatable() && exportAggregation && hasAggregatableColumn(table)) {
            if (table.getAggregationStyle() == Table.AggregationStyle.TOP) {
                r = createAggregatableRow(table, columns, ++r, 1);
            }
        }
        if (table instanceof TreeTable) {
            TreeTable treeTable = (TreeTable) table;
            TreeTableItems treeTableSource = (TreeTableItems) treeTable.getItems();
            for (Object itemId : treeTableSource.getRootItemIds()) {
                if (checkIsRowNumberExceed(r)) {
                    break;
                }
                r = createHierarhicalRow(treeTable, columns, exportExpanded, r, itemId);
            }
        } else if (table instanceof GroupTable && tableItems instanceof GroupTableItems && ((GroupTableItems) tableItems).hasGroups()) {
            GroupTableItems groupTableSource = (GroupTableItems) tableItems;
            for (Object item : groupTableSource.rootGroups()) {
                if (checkIsRowNumberExceed(r)) {
                    break;
                }
                r = createGroupRow((GroupTable) table, columns, ++r, (GroupInfo) item, 0);
            }
        } else {
            for (Object itemId : tableItems.getItemIds()) {
                if (checkIsRowNumberExceed(r)) {
                    break;
                }
                createRow(table, columns, 0, ++r, itemId);
            }
        }
        if (table.isAggregatable() && exportAggregation && hasAggregatableColumn(table)) {
            if (table.getAggregationStyle() == Table.AggregationStyle.BOTTOM) {
                r = createAggregatableRow(table, columns, ++r, 1);
            }
        }
    }
    for (int c = 0; c < columns.size(); c++) {
        sheet.setColumnWidth(c, sizers[c].getWidth() * excelOptions.getColWidthMagic());
    }
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
        wb.write(out);
    } catch (IOException e) {
        throw new RuntimeException("Unable to write document", e);
    }
    if (fileName == null) {
        fileName = messages.getTools().getEntityCaption(((EntityTableItems) tableItems).getEntityMetaClass());
    }
    display.show(new ByteArrayDataProvider(out.toByteArray()), fileName + excelOptions.getExtension(), excelOptions.getExportFormat());
}
Also used : Entity(com.haulmont.cuba.core.entity.Entity) GroupTableItems(com.haulmont.cuba.gui.components.data.GroupTableItems) Table(com.haulmont.cuba.gui.components.Table) EntityTableItems(com.haulmont.cuba.gui.components.data.meta.EntityTableItems) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) TreeTableItems(com.haulmont.cuba.gui.components.data.TreeTableItems)

Example 4 with GroupTableItems

use of com.haulmont.cuba.gui.components.data.GroupTableItems in project cuba by cuba-platform.

the class WebGroupTable method distributeGroupAggregation.

@SuppressWarnings("unchecked")
protected boolean distributeGroupAggregation(AggregationInputValueChangeContext context) {
    if (distributionProvider != null) {
        String value = context.getValue();
        Object columnId = context.getColumnId();
        GroupInfo groupInfo = null;
        try {
            Object parsedValue = getParsedAggregationValue(value, columnId);
            Collection<E> scope = Collections.emptyList();
            if (context.isTotalAggregation()) {
                TableItems<E> tableItems = getItems();
                scope = tableItems == null ? Collections.emptyList() : tableItems.getItems();
            } else if (context instanceof GroupAggregationInputValueChangeContext) {
                Object groupId = ((GroupAggregationInputValueChangeContext) context).getGroupInfo();
                if (groupId instanceof GroupInfo) {
                    groupInfo = (GroupInfo) groupId;
                    scope = ((GroupTableItems) getItems()).getChildItems(groupInfo);
                }
            }
            GroupAggregationDistributionContext<E> aggregationDistribution = new GroupAggregationDistributionContext(getColumnNN(columnId.toString()), parsedValue, scope, groupInfo, context.isTotalAggregation());
            distributionProvider.onDistribution(aggregationDistribution);
        } catch (ValueConversionException e) {
            showParseErrorNotification(e.getLocalizedMessage());
            // rollback to previous value
            return false;
        } catch (ParseException e) {
            showParseErrorNotification(messages.getMainMessage("validationFail"));
            // rollback to previous value
            return false;
        }
    }
    return true;
}
Also used : GroupInfo(com.haulmont.cuba.gui.data.GroupInfo) GroupAggregationInputValueChangeContext(com.haulmont.cuba.web.widgets.CubaGroupTable.GroupAggregationInputValueChangeContext) ParseException(java.text.ParseException) GroupTableItems(com.haulmont.cuba.gui.components.data.GroupTableItems) ValueConversionException(com.haulmont.chile.core.datatypes.ValueConversionException)

Aggregations

GroupTableItems (com.haulmont.cuba.gui.components.data.GroupTableItems)4 GroupInfo (com.haulmont.cuba.gui.data.GroupInfo)3 Entity (com.haulmont.cuba.core.entity.Entity)2 Table (com.haulmont.cuba.gui.components.Table)2 ValueConversionException (com.haulmont.chile.core.datatypes.ValueConversionException)1 Instance (com.haulmont.chile.core.model.Instance)1 MetaPropertyPath (com.haulmont.chile.core.model.MetaPropertyPath)1 TreeTableItems (com.haulmont.cuba.gui.components.data.TreeTableItems)1 EntityTableItems (com.haulmont.cuba.gui.components.data.meta.EntityTableItems)1 GroupAggregationInputValueChangeContext (com.haulmont.cuba.web.widgets.CubaGroupTable.GroupAggregationInputValueChangeContext)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 ParseException (java.text.ParseException)1 Element (org.dom4j.Element)1