Search in sources :

Example 1 with FormatModel

use of org.sagacity.sqltoy.config.model.FormatModel in project sagacity-sqltoy by chenrenfei.

the class QueryExecutor method numFmt.

/**
 * @TODO 对结果的数字进行格式化
 * @param format
 * @param roundingMode
 * @param params
 * @return
 */
public QueryExecutor numFmt(String format, RoundingMode roundingMode, String... columns) {
    if (StringUtil.isNotBlank(format) && columns != null && columns.length > 0) {
        for (String column : columns) {
            FormatModel fmt = new FormatModel();
            fmt.setType(2);
            fmt.setColumn(column);
            fmt.setFormat(format);
            fmt.setRoundingMode(roundingMode);
            innerModel.colsFormat.put(column, fmt);
        }
    }
    return this;
}
Also used : FormatModel(org.sagacity.sqltoy.config.model.FormatModel)

Example 2 with FormatModel

use of org.sagacity.sqltoy.config.model.FormatModel in project sagacity-sqltoy by chenrenfei.

the class SqlXMLConfigParse method parseFormat.

/**
 * @todo 解析对结果字段类型为日期、数字格式化处理配置
 * @param sqlToyConfig
 * @param dfElts
 * @param nfElts
 */
private static void parseFormat(SqlToyConfig sqlToyConfig, NodeList dfElts, NodeList nfElts) {
    List<FormatModel> formatModels = new ArrayList<FormatModel>();
    if (dfElts != null && dfElts.getLength() > 0) {
        Element df;
        for (int i = 0; i < dfElts.getLength(); i++) {
            df = (Element) dfElts.item(i);
            String[] columns = trimParams(df.getAttribute("columns").toLowerCase().split("\\,"));
            String format = df.hasAttribute("format") ? df.getAttribute("format") : "yyyy-MM-dd";
            String locale = df.hasAttribute("locale") ? df.getAttribute("locale") : null;
            for (String col : columns) {
                FormatModel formatModel = new FormatModel();
                formatModel.setColumn(col);
                formatModel.setType(1);
                formatModel.setFormat(format);
                formatModel.setLocale(locale);
                formatModels.add(formatModel);
            }
        }
    }
    if (nfElts != null && nfElts.getLength() > 0) {
        Element nf;
        for (int i = 0; i < nfElts.getLength(); i++) {
            nf = (Element) nfElts.item(i);
            String[] columns = trimParams(nf.getAttribute("columns").toLowerCase().split("\\,"));
            String format = nf.hasAttribute("format") ? nf.getAttribute("format") : "capital";
            String roundStr = nf.hasAttribute("roundingMode") ? nf.getAttribute("roundingMode").toUpperCase() : null;
            String locale = nf.hasAttribute("locale") ? nf.getAttribute("locale") : null;
            RoundingMode roundMode = null;
            if (roundStr != null) {
                if (roundStr.equals("HALF_UP")) {
                    roundMode = RoundingMode.HALF_UP;
                } else if (roundStr.equals("HALF_DOWN")) {
                    roundMode = RoundingMode.HALF_DOWN;
                } else if (roundStr.equals("ROUND_DOWN")) {
                    roundMode = RoundingMode.DOWN;
                } else if (roundStr.equals("ROUND_UP")) {
                    roundMode = RoundingMode.UP;
                }
            }
            for (String col : columns) {
                FormatModel formatModel = new FormatModel();
                formatModel.setColumn(col);
                formatModel.setRoundingMode(roundMode);
                formatModel.setType(2);
                formatModel.setFormat(format);
                formatModel.setLocale(locale);
                formatModels.add(formatModel);
            }
        }
    }
    sqlToyConfig.setFormatModels(formatModels);
}
Also used : RoundingMode(java.math.RoundingMode) Element(org.w3c.dom.Element) FormatModel(org.sagacity.sqltoy.config.model.FormatModel) ArrayList(java.util.ArrayList)

Example 3 with FormatModel

use of org.sagacity.sqltoy.config.model.FormatModel in project sagacity-sqltoy by chenrenfei.

the class ResultUtils method formatColumn.

/**
 * @todo 对字段进行格式化
 * @param rows
 * @param formats
 * @param labelIndexMap
 */
private static void formatColumn(List<List> rows, Iterator<FormatModel> formats, LabelIndexModel labelIndexMap) {
    Integer index;
    Object value;
    FormatModel fmt;
    int columnIndex;
    while (formats.hasNext()) {
        fmt = formats.next();
        index = labelIndexMap.get(fmt.getColumn());
        if (index != null) {
            columnIndex = index.intValue();
            for (List row : rows) {
                value = row.get(columnIndex);
                if (value != null) {
                    // 日期格式
                    if (fmt.getType() == 1) {
                        row.set(columnIndex, DateUtil.formatDate(value, fmt.getFormat(), (fmt.getLocale() == null) ? null : new Locale(fmt.getLocale())));
                    } else // 数字格式化
                    {
                        row.set(columnIndex, NumberUtil.format(value, fmt.getFormat(), fmt.getRoundingMode(), (fmt.getLocale() == null) ? null : new Locale(fmt.getLocale())));
                    }
                }
            }
        }
    }
}
Also used : Locale(java.util.Locale) FormatModel(org.sagacity.sqltoy.config.model.FormatModel) ReverseList(org.sagacity.sqltoy.plugins.calculator.ReverseList) List(java.util.List) ArrayList(java.util.ArrayList) UnpivotList(org.sagacity.sqltoy.plugins.calculator.UnpivotList)

Example 4 with FormatModel

use of org.sagacity.sqltoy.config.model.FormatModel in project sagacity-sqltoy by chenrenfei.

the class ResultUtils method calculate.

/**
 * @todo 对查询结果进行计算处理:字段脱敏、格式化、数据旋转、同步环比、分组汇总等
 * @param desensitizeProvider
 * @param sqlToyConfig
 * @param dataSetResult
 * @param pivotCategorySet
 * @param extend
 * @return
 */
public static boolean calculate(DesensitizeProvider desensitizeProvider, SqlToyConfig sqlToyConfig, DataSetResult dataSetResult, List pivotCategorySet, QueryExecutorExtend extend) {
    List items = dataSetResult.getRows();
    // 数据为空直接跳出处理
    if (items == null || items.isEmpty()) {
        return false;
    }
    boolean changedCols = false;
    List<SecureMask> secureMasks = sqlToyConfig.getSecureMasks();
    List<FormatModel> formatModels = sqlToyConfig.getFormatModels();
    List resultProcessors = new ArrayList();
    if (!sqlToyConfig.getResultProcessor().isEmpty()) {
        resultProcessors.addAll(sqlToyConfig.getResultProcessor());
    }
    if (extend != null && !extend.calculators.isEmpty()) {
        resultProcessors.addAll(extend.calculators);
    }
    // 整理列名称跟index的对照map
    LabelIndexModel labelIndexMap = null;
    if (!secureMasks.isEmpty() || !formatModels.isEmpty() || (extend != null && (!extend.secureMask.isEmpty() || !extend.colsFormat.isEmpty())) || !resultProcessors.isEmpty()) {
        labelIndexMap = wrapLabelIndexMap(dataSetResult.getLabelNames());
    }
    // 字段脱敏
    if (!secureMasks.isEmpty()) {
        secureMask(desensitizeProvider, items, secureMasks.iterator(), labelIndexMap);
    }
    // 自动格式化
    if (!formatModels.isEmpty()) {
        formatColumn(items, formatModels.iterator(), labelIndexMap);
    }
    // 扩展脱敏和格式化处理
    if (extend != null) {
        if (!extend.secureMask.isEmpty()) {
            secureMask(desensitizeProvider, items, extend.secureMask.values().iterator(), labelIndexMap);
        }
        if (!extend.colsFormat.isEmpty()) {
            formatColumn(items, extend.colsFormat.values().iterator(), labelIndexMap);
        }
    }
    // 计算
    if (!resultProcessors.isEmpty()) {
        Object processor;
        for (int i = 0; i < resultProcessors.size(); i++) {
            processor = resultProcessors.get(i);
            // 数据旋转(行转列)
            if (processor instanceof PivotModel) {
                items = pivotResult((PivotModel) processor, labelIndexMap, items, pivotCategorySet);
                changedCols = true;
            } else // 列转行
            if (processor instanceof UnpivotModel) {
                items = UnpivotList.process((UnpivotModel) processor, dataSetResult, labelIndexMap, items);
            } else if (processor instanceof SummaryModel) {
                // 数据汇总合计
                GroupSummary.process((SummaryModel) processor, labelIndexMap, items);
            } else if (processor instanceof ColsChainRelativeModel) {
                // 列数据环比
                ColsChainRelative.process((ColsChainRelativeModel) processor, labelIndexMap, items);
                changedCols = true;
            } else if (processor instanceof RowsChainRelativeModel) {
                // 行数据环比
                RowsChainRelative.process((RowsChainRelativeModel) processor, labelIndexMap, items);
            } else if (processor instanceof ReverseModel) {
                // 数据反序
                ReverseList.process((ReverseModel) processor, labelIndexMap, items);
            }
        }
        dataSetResult.setRows(items);
    }
    return changedCols;
}
Also used : PivotModel(org.sagacity.sqltoy.config.model.PivotModel) ColsChainRelativeModel(org.sagacity.sqltoy.config.model.ColsChainRelativeModel) RowsChainRelativeModel(org.sagacity.sqltoy.config.model.RowsChainRelativeModel) FormatModel(org.sagacity.sqltoy.config.model.FormatModel) ArrayList(java.util.ArrayList) SummaryModel(org.sagacity.sqltoy.config.model.SummaryModel) LabelIndexModel(org.sagacity.sqltoy.config.model.LabelIndexModel) UnpivotModel(org.sagacity.sqltoy.config.model.UnpivotModel) ReverseList(org.sagacity.sqltoy.plugins.calculator.ReverseList) List(java.util.List) ArrayList(java.util.ArrayList) UnpivotList(org.sagacity.sqltoy.plugins.calculator.UnpivotList) SecureMask(org.sagacity.sqltoy.config.model.SecureMask) ReverseModel(org.sagacity.sqltoy.config.model.ReverseModel)

Example 5 with FormatModel

use of org.sagacity.sqltoy.config.model.FormatModel in project sagacity-sqltoy by chenrenfei.

the class QueryExecutor method dateFmt.

/**
 * @TODO 结果日期格式化
 * @param format
 * @param params
 * @return
 */
public QueryExecutor dateFmt(String format, String... columns) {
    if (StringUtil.isNotBlank(format) && columns != null && columns.length > 0) {
        for (String column : columns) {
            FormatModel fmt = new FormatModel();
            fmt.setType(1);
            fmt.setColumn(column);
            fmt.setFormat(format);
            innerModel.colsFormat.put(column, fmt);
        }
    }
    return this;
}
Also used : FormatModel(org.sagacity.sqltoy.config.model.FormatModel)

Aggregations

FormatModel (org.sagacity.sqltoy.config.model.FormatModel)5 ArrayList (java.util.ArrayList)3 List (java.util.List)2 ReverseList (org.sagacity.sqltoy.plugins.calculator.ReverseList)2 UnpivotList (org.sagacity.sqltoy.plugins.calculator.UnpivotList)2 RoundingMode (java.math.RoundingMode)1 Locale (java.util.Locale)1 ColsChainRelativeModel (org.sagacity.sqltoy.config.model.ColsChainRelativeModel)1 LabelIndexModel (org.sagacity.sqltoy.config.model.LabelIndexModel)1 PivotModel (org.sagacity.sqltoy.config.model.PivotModel)1 ReverseModel (org.sagacity.sqltoy.config.model.ReverseModel)1 RowsChainRelativeModel (org.sagacity.sqltoy.config.model.RowsChainRelativeModel)1 SecureMask (org.sagacity.sqltoy.config.model.SecureMask)1 SummaryModel (org.sagacity.sqltoy.config.model.SummaryModel)1 UnpivotModel (org.sagacity.sqltoy.config.model.UnpivotModel)1 Element (org.w3c.dom.Element)1