Search in sources :

Example 1 with EntityMap

use of io.jmix.reports.app.EntityMap 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 EntityMap

use of io.jmix.reports.app.EntityMap 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 3 with EntityMap

use of io.jmix.reports.app.EntityMap in project jmix by jmix-framework.

the class JpqlDataLoader method loadData.

@Override
public List<Map<String, Object>> loadData(ReportQuery reportQuery, BandData parentBand, Map<String, Object> params) {
    String storeName = StoreUtils.getStoreName(reportQuery);
    String query = reportQuery.getScript();
    if (StringUtils.isBlank(query)) {
        return Collections.emptyList();
    }
    try {
        if (Boolean.TRUE.equals(reportQuery.getProcessTemplate())) {
            query = processQueryTemplate(query, parentBand, params);
        }
        List<OutputValue> outputParameters = parseQueryOutputParametersNames(query);
        // replaces [as alias_name], entries except last
        query = query.replaceAll("(?i)" + ALIAS_PATTERN + ",", ",");
        // replaces last [as alias_name] entry
        query = query.replaceAll("(?i)" + ALIAS_PATTERN, " ");
        List queryResult = executeQuery(parentBand, params, storeName, query);
        if (CollectionUtils.isNotEmpty(queryResult) && queryResult.get(0) instanceof Entity) {
            List<Map<String, Object>> wrappedResults = new ArrayList<>();
            for (Object theResult : queryResult) {
                wrappedResults.add(new EntityMap((Entity) theResult, beanFactory));
            }
            return wrappedResults;
        } else {
            return fillOutputData(queryResult, outputParameters);
        }
    } catch (Throwable e) {
        throw new DataLoadingException(String.format("An error occurred while loading data for data set [%s]", reportQuery.getName()), e);
    }
}
Also used : DataLoadingException(com.haulmont.yarg.exception.DataLoadingException) EntityMap(io.jmix.reports.app.EntityMap) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) EntityMap(io.jmix.reports.app.EntityMap) Map(java.util.Map)

Example 4 with EntityMap

use of io.jmix.reports.app.EntityMap in project jmix by jmix-framework.

the class MultiEntityDataLoader method loadData.

@Override
public List<Map<String, Object>> loadData(ReportQuery dataSet, BandData parentBand, Map<String, Object> params) {
    Map<String, Object> additionalParams = dataSet.getAdditionalParams();
    String paramName = (String) additionalParams.get(DataSet.LIST_ENTITIES_PARAM_NAME);
    if (StringUtils.isBlank(paramName)) {
        paramName = DEFAULT_LIST_ENTITIES_PARAM_NAME;
    }
    boolean hasNestedCollection = paramName.contains(NESTED_COLLECTION_SEPARATOR);
    String entityParameterName = StringUtils.substringBefore(paramName, NESTED_COLLECTION_SEPARATOR);
    String nestedCollectionName = StringUtils.substringAfter(paramName, NESTED_COLLECTION_SEPARATOR);
    FetchPlan nestedCollectionFetchPLan = null;
    dataSet = ProxyWrapper.unwrap(dataSet);
    Object entities = null;
    if (params.containsKey(paramName)) {
        entities = params.get(paramName);
    } else if (hasNestedCollection && params.containsKey(entityParameterName)) {
        Entity entity = (Entity) params.get(entityParameterName);
        entity = reloadEntityByDataSetFetchPlan(dataSet, entity);
        if (entity != null) {
            entities = EntityValues.getValueEx(entity, nestedCollectionName);
            if (dataSet instanceof DataSet) {
                FetchPlan entityFetchPlan = getFetchPlan(entity, (DataSet) dataSet);
                if (entityFetchPlan != null && entityFetchPlan.getProperty(nestedCollectionName) != null) {
                    // noinspection ConstantConditions
                    nestedCollectionFetchPLan = entityFetchPlan.getProperty(nestedCollectionName).getFetchPlan();
                }
            }
        }
    }
    if (!(entities instanceof Collection)) {
        if (hasNestedCollection) {
            throw new IllegalStateException(String.format("Input parameters do not contain '%s' parameter, " + "or the entity does not contain nested collection '%s'", entityParameterName, nestedCollectionName));
        } else {
            throw new IllegalStateException(String.format("Input parameters do not contain '%s' parameter or it has type other than collection", paramName));
        }
    }
    Collection<Entity> entitiesList = (Collection) entities;
    params.put(paramName, entitiesList);
    List<Map<String, Object>> resultList = new ArrayList<>();
    for (Entity entity : entitiesList) {
        if (!hasNestedCollection) {
            entity = reloadEntityByDataSetFetchPlan(dataSet, entity);
        }
        if (dataSet instanceof DataSet) {
            if (hasNestedCollection) {
                if (nestedCollectionFetchPLan != null) {
                    resultList.add(new EntityMap(entity, nestedCollectionFetchPLan, beanFactory));
                } else {
                    resultList.add(new EntityMap(entity, beanFactory));
                }
            } else {
                resultList.add(new EntityMap(entity, getFetchPlan(entity, (DataSet) dataSet), beanFactory));
            }
        } else {
            resultList.add(new EntityMap(entity, beanFactory));
        }
    }
    return resultList;
}
Also used : Entity(io.jmix.core.Entity) DataSet(io.jmix.reports.entity.DataSet) ArrayList(java.util.ArrayList) FetchPlan(io.jmix.core.FetchPlan) EntityMap(io.jmix.reports.app.EntityMap) Collection(java.util.Collection) EntityMap(io.jmix.reports.app.EntityMap) Map(java.util.Map)

Example 5 with EntityMap

use of io.jmix.reports.app.EntityMap in project jmix by jmix-framework.

the class SingleEntityDataLoader method loadData.

@Override
public List<Map<String, Object>> loadData(ReportQuery dataSet, BandData parentBand, Map<String, Object> params) {
    Map<String, Object> additionalParams = dataSet.getAdditionalParams();
    String paramName = (String) additionalParams.get(DataSet.ENTITY_PARAM_NAME);
    if (StringUtils.isBlank(paramName)) {
        paramName = DEFAULT_ENTITY_PARAM_NAME;
    }
    Object entity = params.get(paramName);
    if (entity == null) {
        throw new IllegalStateException(String.format("Input parameters do not contain '%s' parameter", paramName));
    }
    dataSet = ProxyWrapper.unwrap(dataSet);
    entity = reloadEntityByDataSetFetchPlan(dataSet, entity);
    params.put(paramName, entity);
    EntityMap result;
    if (dataSet instanceof DataSet) {
        result = new EntityMap((Entity) entity, getFetchPlan((Entity) entity, (DataSet) dataSet), beanFactory);
    } else {
        result = new EntityMap((Entity) entity, beanFactory);
    }
    return Collections.singletonList(result);
}
Also used : Entity(io.jmix.core.Entity) EntityMap(io.jmix.reports.app.EntityMap) DataSet(io.jmix.reports.entity.DataSet)

Aggregations

EntityMap (io.jmix.reports.app.EntityMap)5 Entity (io.jmix.core.Entity)2 KeyValueEntity (io.jmix.core.entity.KeyValueEntity)2 DataSet (io.jmix.reports.entity.DataSet)2 JmixTableData (io.jmix.reports.entity.JmixTableData)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 DataLoadingException (com.haulmont.yarg.exception.DataLoadingException)1 BandData (com.haulmont.yarg.structure.BandData)1 FetchPlan (io.jmix.core.FetchPlan)1 TemplateTableBand (io.jmix.reports.entity.table.TemplateTableBand)1 TemplateTableColumn (io.jmix.reports.entity.table.TemplateTableColumn)1 ReportingException (io.jmix.reports.exception.ReportingException)1 Collection (java.util.Collection)1 List (java.util.List)1