use of org.sagacity.sqltoy.config.model.GroupMeta in project sagacity-sqltoy by chenrenfei.
the class SqlXMLConfigParse method parseCalculator.
/**
* @todo 解析对sqltoy查询结果的处理逻辑定义
* @param sqlToyConfig
* @param sqlElt
*/
private static void parseCalculator(SqlToyConfig sqlToyConfig, Element sqlElt) {
List elements = sqlElt.elements();
Element elt;
String eltName;
List resultProcessor = new ArrayList();
for (int i = 0; i < elements.size(); i++) {
elt = (Element) elements.get(i);
eltName = elt.getName();
// 旋转(只能进行一次旋转)
if (eltName.equals("pivot")) {
PivotModel pivotModel = new PivotModel();
if (elt.attribute("group-columns") != null)
pivotModel.setGroupCols(trimParams(elt.attributeValue("group-columns").toLowerCase().split(",")));
if (elt.attribute("category-columns") != null)
pivotModel.setCategoryCols(trimParams(elt.attributeValue("category-columns").toLowerCase().split(",")));
if (elt.attribute("category-sql") != null)
pivotModel.setCategorySql(elt.attributeValue("category-sql"));
String[] pivotCols = new String[2];
pivotCols[0] = elt.attributeValue("start-column").toLowerCase();
if (elt.attribute("end-column") != null)
pivotCols[1] = elt.attributeValue("end-column").toLowerCase();
else
pivotCols[1] = pivotCols[0];
if (elt.attribute("default-value") != null) {
String defaultValue = elt.attributeValue("default-value");
if (elt.attribute("default-type") != null) {
String defaultType = elt.attributeValue("default-type");
try {
pivotModel.setDefaultValue(BeanUtil.convertType(defaultValue, defaultType));
} catch (Exception e) {
}
} else
pivotModel.setDefaultValue(defaultValue);
}
pivotModel.setPivotCols(pivotCols);
resultProcessor.add(pivotModel);
} else // 列转行
if (eltName.equals("unpivot")) {
if (elt.attribute("columns") != null && elt.attribute("values-as-column") != null) {
UnpivotModel unpivotModel = new UnpivotModel();
String[] columns = elt.attributeValue("columns").split(",");
String[] realCols = new String[columns.length];
String[] colsAlias = new String[columns.length];
int index = 0;
String[] temp;
for (String column : columns) {
temp = column.split(":");
realCols[index] = temp[0].trim().toLowerCase();
colsAlias[index] = temp[temp.length - 1];
index++;
}
unpivotModel.setColumns(realCols);
unpivotModel.setColsAlias(colsAlias);
// 多列变成行时转成的列名称
unpivotModel.setAsColumn(elt.attributeValue("values-as-column"));
// 变成行的列标题作为的新列名称
if (elt.attribute("labels-as-column") != null)
unpivotModel.setLabelsColumn(elt.attributeValue("labels-as-column"));
// 必须要有2个或以上列
if (index > 1)
resultProcessor.add(unpivotModel);
}
} else // 汇总合计
if (eltName.equals("summary")) {
SummaryModel summaryModel = new SummaryModel();
// 是否逆向汇总
if (elt.attribute("reverse") != null) {
summaryModel.setReverse(Boolean.parseBoolean(elt.attributeValue("reverse")));
summaryModel.setGlobalReverse(summaryModel.isReverse());
}
// 汇总合计涉及的列
if (elt.attribute("columns") != null)
summaryModel.setSummaryCols(elt.attributeValue("columns").toLowerCase());
// 保留小数点位数
if (elt.attribute("radix-size") != null)
summaryModel.setRadixSize(Integer.parseInt(elt.attributeValue("radix-size")));
else
summaryModel.setRadixSize(-1);
// 汇总所在位置
if (elt.attribute("sum-site") != null)
summaryModel.setSumSite(elt.attributeValue("sum-site"));
// sum和average值左右拼接时的连接字符串
if (elt.attribute("link-sign") != null)
summaryModel.setLinkSign(elt.attributeValue("link-sign"));
// 全局汇总
Element globalSummary = elt.element("global");
if (globalSummary != null) {
if (globalSummary.attribute("label-column") != null)
summaryModel.setGlobalLabelColumn(globalSummary.attributeValue("label-column").toLowerCase());
if (globalSummary.attribute("average-label") != null)
summaryModel.setGlobalAverageTitle(globalSummary.attributeValue("average-label"));
// 汇总分组列
if (globalSummary.attribute("group-column") != null)
summaryModel.setGroupColumn(globalSummary.attributeValue("group-column").toLowerCase());
// 全局汇总合计是否逆向
if (globalSummary.attribute("reverse") != null)
summaryModel.setGlobalReverse(Boolean.parseBoolean(globalSummary.attributeValue("reverse")));
if (globalSummary.attribute("sum-label") != null)
summaryModel.setGlobalSumTitle(globalSummary.attributeValue("sum-label"));
}
// 分组汇总
List<Element> groupElts = elt.elements("group");
if (groupElts != null && !groupElts.isEmpty()) {
GroupMeta[] groupMetas = new GroupMeta[groupElts.size()];
int index = 0;
for (Element groupElt : groupElts) {
GroupMeta groupMeta = new GroupMeta();
groupMeta.setGroupColumn(groupElt.attributeValue("group-column").toLowerCase());
if (groupElt.attribute("average-label") != null)
groupMeta.setAverageTitle(groupElt.attributeValue("average-label"));
if (groupElt.attribute("sum-label") != null)
groupMeta.setSumTitle(groupElt.attributeValue("sum-label"));
if (groupElt.attribute("label-column") != null)
groupMeta.setLabelColumn(groupElt.attributeValue("label-column"));
groupMetas[index] = groupMeta;
index++;
}
summaryModel.setGroupMeta(groupMetas);
}
resultProcessor.add(summaryModel);
}
}
// 加入sqltoyModel
if (!resultProcessor.isEmpty()) {
sqlToyConfig.setResultProcessor(resultProcessor);
}
}
use of org.sagacity.sqltoy.config.model.GroupMeta in project sagacity-sqltoy by chenrenfei.
the class ResultUtils method groupSummary.
/**
* @todo 对数据进行汇总合计
* @param summaryModel
* @param labelIndexMap
* @param result
* @return
*/
private static void groupSummary(SummaryModel summaryModel, HashMap<String, Integer> labelIndexMap, List result) {
if (result == null || result.isEmpty())
return;
List<Integer> sumColList = new ArrayList<Integer>();
// 参照列,如按年份进行旋转(columns="1..result.width()-1")
String cols = summaryModel.getSummaryCols().replaceAll("result\\.width\\(\\)", Integer.toString(((List) result.get(0)).size()));
cols = cols.replaceAll("\\$\\{dataWidth\\}", Integer.toString(((List) result.get(0)).size()));
String[] columns = cols.split(",");
String column;
String endColumnStr;
int step;
int stepIndex;
for (int i = 0; i < columns.length; i++) {
column = columns[i].toLowerCase();
// like {1..20?2} ?step 用于数据间隔性汇总
if (column.indexOf("..") != -1) {
step = 1;
String[] beginToEnd = column.split("\\.\\.");
int begin = 0;
int end = 0;
if (CommonUtils.isInteger(beginToEnd[0]))
begin = Integer.parseInt(beginToEnd[0]);
else
begin = (new BigDecimal(ExpressionUtil.calculate(beginToEnd[0]).toString())).intValue();
endColumnStr = beginToEnd[1];
if (CommonUtils.isInteger(endColumnStr))
end = Integer.parseInt(endColumnStr);
else {
stepIndex = endColumnStr.indexOf("?");
if (stepIndex != -1) {
step = Integer.parseInt(endColumnStr.substring(stepIndex + 1).trim());
endColumnStr = endColumnStr.substring(0, stepIndex);
}
end = (new BigDecimal(ExpressionUtil.calculate(endColumnStr).toString())).intValue();
}
for (int j = begin; j <= end; j += step) {
if (!sumColList.contains(j))
sumColList.add(j);
}
} else if (CommonUtils.isInteger(column)) {
if (!sumColList.contains(Integer.parseInt(column)))
sumColList.add(Integer.parseInt(column));
} else {
Integer colIndex;
if (labelIndexMap.containsKey(column))
colIndex = labelIndexMap.get(column);
else
colIndex = (new BigDecimal(ExpressionUtil.calculate(column).toString())).intValue();
if (!sumColList.contains(colIndex))
sumColList.add(colIndex);
}
}
Integer[] summaryCols = new Integer[sumColList.size()];
sumColList.toArray(summaryCols);
boolean hasAverage = false;
if (summaryModel.getGlobalAverageTitle() != null || summaryModel.getSumSite().equals("left") || summaryModel.getSumSite().equals("right"))
hasAverage = true;
Object[][] groupIndexs = null;
if (summaryModel.getGroupMeta() != null) {
groupIndexs = new Object[summaryModel.getGroupMeta().length][5];
GroupMeta groupMeta;
// {{汇总列,汇总标题,平均标题,汇总相对平均的位置(left/right/top/bottom)}}
for (int i = 0; i < groupIndexs.length; i++) {
Object[] group = new Object[5];
groupMeta = summaryModel.getGroupMeta()[i];
group[0] = CommonUtils.isInteger(groupMeta.getGroupColumn()) ? Integer.parseInt(groupMeta.getGroupColumn()) : labelIndexMap.get(groupMeta.getGroupColumn().toLowerCase());
group[1] = groupMeta.getSumTitle();
group[2] = groupMeta.getAverageTitle();
group[3] = summaryModel.getSumSite();
if (groupMeta.getLabelColumn() != null)
group[4] = CommonUtils.isInteger(groupMeta.getLabelColumn()) ? Integer.parseInt(groupMeta.getLabelColumn()) : labelIndexMap.get(groupMeta.getLabelColumn().toLowerCase());
groupIndexs[i] = group;
}
}
int globalLabelIndex = -1;
if (summaryModel.getGlobalLabelColumn() != null) {
if (CommonUtils.isInteger(summaryModel.getGlobalLabelColumn()))
globalLabelIndex = Integer.parseInt(summaryModel.getGlobalLabelColumn());
else
globalLabelIndex = labelIndexMap.get(summaryModel.getGlobalLabelColumn().toLowerCase());
}
// 逆向汇总
if (summaryModel.isReverse()) {
CollectionUtil.groupReverseSummary(result, groupIndexs, summaryCols, globalLabelIndex, summaryModel.getGlobalSumTitle(), hasAverage, summaryModel.getGlobalAverageTitle(), summaryModel.getRadixSize(), summaryModel.getSumSite());
} else {
CollectionUtil.groupSummary(result, groupIndexs, summaryCols, globalLabelIndex, summaryModel.getGlobalSumTitle(), hasAverage, summaryModel.getGlobalAverageTitle(), summaryModel.getRadixSize(), summaryModel.getSumSite(), summaryModel.isGlobalReverse());
}
}
Aggregations