|
|
@@ -23,11 +23,13 @@ import org.dom4j.io.SAXReader;
|
|
|
import org.dom4j.io.XMLWriter;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
|
import com.uas.report.core.exception.ReportException;
|
|
|
import com.uas.report.crystal2jasper.Join.JoinType;
|
|
|
import com.uas.report.crystal2jasper.Link.LinkType;
|
|
|
+import com.uas.report.crystal2jasper.Sort.SortType;
|
|
|
import com.uas.report.util.FileUtils;
|
|
|
import com.uas.report.util.ZipUtils;
|
|
|
|
|
|
@@ -337,14 +339,15 @@ public class CrystalToJasper {
|
|
|
// 表名
|
|
|
// <Tablesource alias="FEEPLEASE"
|
|
|
// databaseIdentifier="E_SHINE_DIGIT.FEEPLEASE">
|
|
|
+ Map<String, String> tableNames = new HashMap<>();
|
|
|
List<Element> tableSourceElements = document.selectNodes("//Tablesource");
|
|
|
- List<String> tableNames = new ArrayList<>();
|
|
|
for (Element tableSourceElement : tableSourceElements) {
|
|
|
- String text = tableSourceElement.attribute("databaseIdentifier").getText();
|
|
|
+ String alias = tableSourceElement.attributeValue("alias");
|
|
|
+ String text = tableSourceElement.attributeValue("databaseIdentifier");
|
|
|
// 去除表名前的数据库名
|
|
|
int index = text.lastIndexOf(".");
|
|
|
String tableName = index < 0 ? text : text.substring(index + 1);
|
|
|
- tableNames.add(tableName);
|
|
|
+ tableNames.put(alias, tableName);
|
|
|
|
|
|
// <Column name="FP_V13" type="11" />
|
|
|
List<Element> columnElements = tableSourceElement.elements("Column");
|
|
|
@@ -353,20 +356,20 @@ public class CrystalToJasper {
|
|
|
String columnName = columnElement.attribute("name").getText();
|
|
|
columns.add(columnName);
|
|
|
}
|
|
|
- columnNames.put(tableName, columns);
|
|
|
+ columnNames.put(alias, columns);
|
|
|
}
|
|
|
|
|
|
// 表之间的join关系
|
|
|
// <Join from="FEEPLEASE" to="FEEPLEASEDETAIL" type="loj">
|
|
|
// <Link from="FP_ID" to="FPD_FPID" type="eq"/>
|
|
|
// </Join>
|
|
|
- List<Element> joinElements = document.selectNodes("//Join");
|
|
|
List<Join> joins = new ArrayList<>();
|
|
|
+ List<Element> joinElements = document.selectNodes("//Join");
|
|
|
for (Element joinElement : joinElements) {
|
|
|
- String fromTableName = joinElement.attribute("from").getText();
|
|
|
- String toTableName = joinElement.attribute("to").getText();
|
|
|
+ String fromTableAlias = joinElement.attribute("from").getText();
|
|
|
+ String toTableAlias = joinElement.attribute("to").getText();
|
|
|
String joinType = joinElement.attribute("type").getText();
|
|
|
- Join join = new Join(fromTableName, toTableName, getJoinType(joinType));
|
|
|
+ Join join = new Join(fromTableAlias, toTableAlias, getJoinType(joinType));
|
|
|
|
|
|
List<Element> linkElements = joinElement.elements("Link");
|
|
|
List<Link> links = new ArrayList<>();
|
|
|
@@ -380,11 +383,39 @@ public class CrystalToJasper {
|
|
|
}
|
|
|
join.setLinks(links);
|
|
|
joins.add(join);
|
|
|
- // 有join关系的表需从列表中排除,防止重复
|
|
|
- tableNames.remove(fromTableName);
|
|
|
- tableNames.remove(toTableName);
|
|
|
+ join.setFromTableName(tableNames.get(fromTableAlias));
|
|
|
+ join.setToTableName(tableNames.get(toTableAlias));
|
|
|
+ }
|
|
|
+ // 有join关系的表需从列表中排除,防止重复
|
|
|
+ for (Join join : joins) {
|
|
|
+ tableNames.remove(join.getFromTableAlias());
|
|
|
+ tableNames.remove(join.getToTableAlias());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 排序
|
|
|
+ List<Sort> sorts = new ArrayList<>();
|
|
|
+ // 组排序
|
|
|
+ List<Element> groupFieldElements = document.selectNodes("//AreaPair[@type='GroupAreaPair']/AreaPairProperties");
|
|
|
+ for (Element element : groupFieldElements) {
|
|
|
+ String field = ((Element) element.selectSingleNode("Reference[@name='id']")).attributeValue("value");
|
|
|
+ String sortType = "0";
|
|
|
+ Element sortTypeElement = element.element("SortType");
|
|
|
+ if (sortTypeElement != null) {
|
|
|
+ sortType = sortTypeElement.attributeValue("value");
|
|
|
+ }
|
|
|
+ Sort sort = new Sort(field, getSortType(sortType));
|
|
|
+ sorts.add(sort);
|
|
|
}
|
|
|
- return getQueryString(columnNames, tableNames, joins);
|
|
|
+ // 普通排序字段
|
|
|
+ List<Element> sortElements = document.selectNodes("//SortFields/Field");
|
|
|
+ for (Element element : sortElements) {
|
|
|
+ String field = element.element("Name").attributeValue("value");
|
|
|
+ String sortType = element.element("Operation").attributeValue("value");
|
|
|
+ Sort sort = new Sort(field, getSortType(sortType));
|
|
|
+ sorts.add(sort);
|
|
|
+ }
|
|
|
+
|
|
|
+ return getQueryString(columnNames, tableNames, joins, sorts);
|
|
|
}
|
|
|
|
|
|
private JoinType getJoinType(String type) {
|
|
|
@@ -419,6 +450,16 @@ public class CrystalToJasper {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ private SortType getSortType(String type) {
|
|
|
+ if ("0".equalsIgnoreCase(type)) {
|
|
|
+ return SortType.ASC;
|
|
|
+ } else if ("1".equalsIgnoreCase(type)) {
|
|
|
+ return SortType.DESC;
|
|
|
+ } else {
|
|
|
+ throw new ReportException("无法解析sort类型:" + type);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 根据表名和连接关系获取查询语句
|
|
|
*
|
|
|
@@ -428,11 +469,16 @@ public class CrystalToJasper {
|
|
|
* 表名
|
|
|
* @param joins
|
|
|
* 连接关系
|
|
|
+ * @param sorts
|
|
|
+ * 排序
|
|
|
* @return
|
|
|
*/
|
|
|
- private String getQueryString(Map<String, List<String>> columnNames, List<String> tableNames, List<Join> joins) {
|
|
|
+ private String getQueryString(Map<String, List<String>> columnNames, Map<String, String> tableNames,
|
|
|
+ List<Join> joins, List<Sort> sorts) {
|
|
|
StringBuilder builder = new StringBuilder("select");
|
|
|
int i = 0;
|
|
|
+
|
|
|
+ // 列名
|
|
|
if (columnNames != null) {
|
|
|
Set<Entry<String, List<String>>> entrySet = columnNames.entrySet();
|
|
|
for (Entry<String, List<String>> entry : entrySet) {
|
|
|
@@ -447,23 +493,55 @@ public class CrystalToJasper {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- builder.append(" from ");
|
|
|
+ builder.append("\nfrom ");
|
|
|
|
|
|
+ // 表名
|
|
|
StringBuilder tableNameBuilder = new StringBuilder();
|
|
|
i = 0;
|
|
|
- for (String tableName : tableNames) {
|
|
|
+ for (Entry<String, String> entry : tableNames.entrySet()) {
|
|
|
if (i++ != 0) {
|
|
|
- tableNameBuilder.append(",");
|
|
|
+ tableNameBuilder.append(", ");
|
|
|
}
|
|
|
+ String alias = entry.getKey();
|
|
|
+ String tableName = entry.getValue();
|
|
|
tableNameBuilder.append(tableName);
|
|
|
+ if (!alias.equals(tableName)) {
|
|
|
+ tableNameBuilder.append(" ").append(alias);
|
|
|
+ }
|
|
|
}
|
|
|
+ if (tableNameBuilder.length() > 0 && !CollectionUtils.isEmpty(joins)) {
|
|
|
+ tableNameBuilder.append(", ");
|
|
|
+ }
|
|
|
+ // join
|
|
|
for (Join join : joins) {
|
|
|
- if (!tableNameBuilder.toString().contains(join.getFromTableName())) {
|
|
|
- tableNameBuilder.append(", ").append(join.getFromTableName());
|
|
|
+ String fromTableAlias = join.getFromTableAlias();
|
|
|
+ String fromTableName = join.getFromTableName();
|
|
|
+ try {
|
|
|
+ if (!tableNameBuilder.toString().contains(fromTableName)) {
|
|
|
+ tableNameBuilder.append(fromTableName);
|
|
|
+ }
|
|
|
+ } catch (Throwable e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ if (!fromTableAlias.equals(fromTableName)) {
|
|
|
+ tableNameBuilder.append(" ").append(fromTableAlias);
|
|
|
}
|
|
|
tableNameBuilder.append(" ").append(join.getExpression());
|
|
|
}
|
|
|
builder.append(tableNameBuilder);
|
|
|
+
|
|
|
+ builder.append("\n$P!{WHERE_CONDITION}");
|
|
|
+
|
|
|
+ // 排序
|
|
|
+ i = 0;
|
|
|
+ for (Sort sort : sorts) {
|
|
|
+ if (i++ != 0) {
|
|
|
+ builder.append(", ");
|
|
|
+ } else {
|
|
|
+ builder.append("\norder by ");
|
|
|
+ }
|
|
|
+ builder.append(sort.getExpression());
|
|
|
+ }
|
|
|
return builder.toString();
|
|
|
}
|
|
|
|