Search in sources :

Example 1 with JmixTableData

use of io.jmix.reports.entity.JmixTableData in project jmix by jmix-framework.

the class JmixTableFormatter method getTableData.

protected JmixTableData getTableData(TemplateTableDescription templateTableDescription, Map<String, List<KeyValueEntity>> transformedData, Map<String, Set<JmixTableData.ColumnInfo>> headerMap, Map<String, List<BandData>> childrenBands) {
    for (TemplateTableBand band : templateTableDescription.getTemplateTableBands()) {
        String bandName = band.getBandName();
        if (bandName.startsWith(HEADER_BAND_PREFIX)) {
            break;
        }
        List<BandData> bandDataList = childrenBands.get(bandName);
        if (bandDataList == null) {
            throw new ReportingException(String.format("Report template has an unknown band [%s]", bandName));
        }
        List<KeyValueEntity> entities = new ArrayList<>();
        Set<JmixTableData.ColumnInfo> headers = new LinkedHashSet<>();
        bandDataList.forEach(bandData -> {
            Map<String, Object> data = bandData.getData();
            final String pkName;
            final boolean pkInFetchPlan;
            if (data instanceof EntityMap) {
                Entity instance = ((EntityMap) data).getInstance();
                pkName = metadataTools.getPrimaryKeyName(metadata.getClass(instance));
                FetchPlan fetchPlan = ((EntityMap) data).getFetchPlan();
                pkInFetchPlan = fetchPlan != null && pkName != null && fetchPlan.containsProperty(pkName);
            } else {
                pkName = null;
                pkInFetchPlan = false;
            }
            KeyValueEntity entityRow = new KeyValueEntity();
            for (TemplateTableColumn column : band.getColumns()) {
                String key = column.getKey();
                Object value = data.get(key);
                if (INSTANCE_NAME_KEY.equals(key)) {
                    return;
                }
                if (checkAddHeader(pkName, pkInFetchPlan, key)) {
                    checkInstanceNameLoaded(value);
                    String transformationKey = transformationKey(key);
                    if (isFormat(bandName, key)) {
                        String formattedValue = getFormattedValue(bandName, key, value);
                        entityRow.setValue(transformationKey, formattedValue);
                    } else {
                        entityRow.setValue(transformationKey, value);
                    }
                }
            }
            if (headers.isEmpty() || headers.size() < data.size()) {
                for (TemplateTableColumn column : band.getColumns()) {
                    String key = column.getKey();
                    Object value = data.get(key);
                    if (INSTANCE_NAME_KEY.equals(key)) {
                        return;
                    }
                    if (checkAddHeader(pkName, pkInFetchPlan, key)) {
                        String transformationKey = transformationKey(key);
                        if (value != null) {
                            Class valueClass = getColumnClass(bandName, key, value);
                            headers.add(new JmixTableData.ColumnInfo(transformationKey, valueClass, column.getCaption(), column.getPosition()));
                        } else {
                            headers.add(new JmixTableData.ColumnInfo(transformationKey, String.class, column.getCaption(), column.getPosition()));
                        }
                    }
                }
            }
            entities.add(entityRow);
        });
        headers.removeIf(header -> containsLowerCaseDuplicate(header, headers));
        transformedData.put(bandName, entities);
        headerMap.put(bandName, headers);
    }
    return new JmixTableData(transformedData, headerMap);
}
Also used : KeyValueEntity(io.jmix.core.entity.KeyValueEntity) ReportingException(io.jmix.reports.exception.ReportingException) JmixTableData(io.jmix.reports.entity.JmixTableData) EntityMap(io.jmix.reports.app.EntityMap) TemplateTableColumn(io.jmix.reports.entity.table.TemplateTableColumn) TemplateTableBand(io.jmix.reports.entity.table.TemplateTableBand) KeyValueEntity(io.jmix.core.entity.KeyValueEntity) BandData(com.haulmont.yarg.structure.BandData)

Example 2 with JmixTableData

use of io.jmix.reports.entity.JmixTableData in project jmix by jmix-framework.

the class JmixTableFormatter method transformData.

protected JmixTableData transformData(BandData rootBand) {
    TemplateTableDescription templateTableDescription = ((ReportTemplate) reportTemplate).getTemplateTableDescription();
    Map<String, List<KeyValueEntity>> transformedData = new LinkedHashMap<>();
    Map<String, Set<JmixTableData.ColumnInfo>> headerMap = new HashMap<>();
    Map<String, List<BandData>> childrenBands = rootBand.getChildrenBands();
    if (templateTableDescription.getTemplateTableBands().size() > 0) {
        return getTableData(templateTableDescription, transformedData, headerMap, childrenBands);
    } else {
        return getTableData(transformedData, headerMap, childrenBands);
    }
}
Also used : TemplateTableDescription(io.jmix.reports.entity.table.TemplateTableDescription) ReportTemplate(io.jmix.reports.entity.ReportTemplate) JmixTableData(io.jmix.reports.entity.JmixTableData)

Example 3 with JmixTableData

use of io.jmix.reports.entity.JmixTableData in project jmix by jmix-framework.

the class JmixTableFormatter method renderDocument.

@Override
public void renderDocument() {
    JmixTableData dto = transformData(rootBand);
    byte[] serializedData = standardSerialization.serialize(dto);
    try {
        IOUtils.write(serializedData, outputStream);
    } catch (IOException e) {
        throw new RuntimeException("An error occurred while rendering chart", e);
    }
}
Also used : JmixTableData(io.jmix.reports.entity.JmixTableData) IOException(java.io.IOException)

Example 4 with JmixTableData

use of io.jmix.reports.entity.JmixTableData in project jmix by jmix-framework.

the class JmixTableFormatter method getTableData.

private JmixTableData getTableData(Map<String, List<KeyValueEntity>> transformedData, Map<String, Set<JmixTableData.ColumnInfo>> headerMap, Map<String, List<BandData>> childrenBands) {
    childrenBands.forEach((bandName, bandDataList) -> {
        if (bandName.startsWith(HEADER_BAND_PREFIX)) {
            return;
        }
        List<KeyValueEntity> entities = new ArrayList<>();
        Set<JmixTableData.ColumnInfo> headers = new HashSet<>();
        Set<String> emptyHeaders = new LinkedHashSet<>();
        bandDataList.forEach(bandData -> {
            Map<String, Object> data = bandData.getData();
            final Entity instance;
            final String pkName;
            final boolean pkInFetchPlan;
            if (data instanceof EntityMap) {
                instance = ((EntityMap) data).getInstance();
                pkName = metadataTools.getPrimaryKeyName(metadata.getClass(instance));
                FetchPlan fetchPlan = ((EntityMap) data).getFetchPlan();
                pkInFetchPlan = fetchPlan != null && pkName != null && fetchPlan.containsProperty(pkName);
            } else {
                instance = null;
                pkName = null;
                pkInFetchPlan = false;
            }
            KeyValueEntity entityRow = new KeyValueEntity();
            data.forEach((name, value) -> {
                if (INSTANCE_NAME_KEY.equals(name)) {
                    return;
                }
                if (checkAddHeader(pkName, pkInFetchPlan, name)) {
                    if (instance != null) {
                        name = messageTools.getPropertyCaption(metadata.getClass(instance), name);
                    }
                    checkInstanceNameLoaded(value);
                    String transformationKey = transformationKey(name);
                    if (isFormat(bandName, name)) {
                        String formattedValue = getFormattedValue(bandData.getName(), name, value);
                        entityRow.setValue(transformationKey, formattedValue);
                    } else {
                        entityRow.setValue(transformationKey, value);
                    }
                }
            });
            if (headers.isEmpty() || headers.size() < data.size()) {
                data.forEach((name, value) -> {
                    if (INSTANCE_NAME_KEY.equals(name)) {
                        return;
                    }
                    if (checkAddHeader(pkName, pkInFetchPlan, name)) {
                        if (instance != null) {
                            name = messageTools.getPropertyCaption(metadata.getClass(instance), name);
                        }
                        if (name != null && value != null) {
                            Class valueClass = getColumnClass(bandData.getName(), name, value);
                            headers.add(new JmixTableData.ColumnInfo(transformationKey(name), valueClass, name));
                        }
                        if (name != null && value == null) {
                            emptyHeaders.add(transformationKey(name));
                        }
                    }
                });
            }
            entities.add(entityRow);
        });
        emptyHeaders.forEach(header -> {
            if (!containsHeader(headers, header))
                headers.add(new JmixTableData.ColumnInfo(header, String.class, header));
        });
        headers.removeIf(header -> containsLowerCaseDuplicate(header, headers));
        transformedData.put(bandName, entities);
        headerMap.put(bandName, headers);
    });
    return new JmixTableData(transformedData, headerMap);
}
Also used : KeyValueEntity(io.jmix.core.entity.KeyValueEntity) JmixTableData(io.jmix.reports.entity.JmixTableData) EntityMap(io.jmix.reports.app.EntityMap) KeyValueEntity(io.jmix.core.entity.KeyValueEntity)

Example 5 with JmixTableData

use of io.jmix.reports.entity.JmixTableData in project jmix by jmix-framework.

the class ShowReportTableScreen method drawTables.

protected void drawTables(JmixTableData dto) {
    Map<String, List<KeyValueEntity>> data = dto.getData();
    Map<String, Set<JmixTableData.ColumnInfo>> headerMap = dto.getHeaders();
    tablesVBoxLayout.removeAll();
    if (data == null || data.isEmpty())
        return;
    data.forEach((dataSetName, keyValueEntities) -> {
        if (CollectionUtils.isNotEmpty(keyValueEntities)) {
            KeyValueCollectionContainer container = createContainer(dataSetName, keyValueEntities, headerMap);
            Table table = createTable(dataSetName, container, headerMap);
            GroupBoxLayout groupBox = uiComponents.create(GroupBoxLayout.class);
            groupBox.setCaption(dataSetName);
            groupBox.add(table);
            groupBox.expand(table);
            tablesVBoxLayout.add(groupBox);
            tablesVBoxLayout.expand(groupBox);
        }
    });
}
Also used : Set(java.util.Set) JmixTableData(io.jmix.reports.entity.JmixTableData) KeyValueCollectionContainer(io.jmix.ui.model.KeyValueCollectionContainer) List(java.util.List)

Aggregations

JmixTableData (io.jmix.reports.entity.JmixTableData)7 KeyValueEntity (io.jmix.core.entity.KeyValueEntity)2 EntityMap (io.jmix.reports.app.EntityMap)2 ReportOutputDocument (com.haulmont.yarg.reporting.ReportOutputDocument)1 BandData (com.haulmont.yarg.structure.BandData)1 Report (io.jmix.reports.entity.Report)1 ReportTemplate (io.jmix.reports.entity.ReportTemplate)1 TemplateTableBand (io.jmix.reports.entity.table.TemplateTableBand)1 TemplateTableColumn (io.jmix.reports.entity.table.TemplateTableColumn)1 TemplateTableDescription (io.jmix.reports.entity.table.TemplateTableDescription)1 ReportingException (io.jmix.reports.exception.ReportingException)1 KeyValueCollectionContainer (io.jmix.ui.model.KeyValueCollectionContainer)1 IOException (java.io.IOException)1 List (java.util.List)1 Set (java.util.Set)1