Commit b34ad564 authored by 雍欢's avatar 雍欢

treeview支持切换SQL方言

parent 7a83fd3f
...@@ -41,9 +41,6 @@ public class EasySearchManager extends ResourceLoadManager<EasySearchMappingMode ...@@ -41,9 +41,6 @@ public class EasySearchManager extends ResourceLoadManager<EasySearchMappingMode
this.sqlDialect = sqlDialect; this.sqlDialect = sqlDialect;
} }
/**
* 加载配置文件
*/
@Override @Override
public EasySearchMappingModel loadConfigFile(String type) throws NotFoundException { public EasySearchMappingModel loadConfigFile(String type) throws NotFoundException {
List<String> paths = new ArrayList<>(2); List<String> paths = new ArrayList<>(2);
......
...@@ -22,5 +22,8 @@ public interface EasySearchLoadInterface { ...@@ -22,5 +22,8 @@ public interface EasySearchLoadInterface {
String DIALECT_XML_PATH = String.join("/", XML_BASE_DIRECTORY, "%s", "easy-search-%s.xml"); String DIALECT_XML_PATH = String.join("/", XML_BASE_DIRECTORY, "%s", "easy-search-%s.xml");
/**
* 加载配置文件
*/
EasySearchMappingModel loadConfigFile(String path) throws ResourceLoadException; EasySearchMappingModel loadConfigFile(String path) throws ResourceLoadException;
} }
...@@ -81,6 +81,7 @@ public class EasySearchMappingModel implements Serializable, ConfigFileVersion { ...@@ -81,6 +81,7 @@ public class EasySearchMappingModel implements Serializable, ConfigFileVersion {
public QuerySchemeModel getQuerySchemeModel(String name) { public QuerySchemeModel getQuerySchemeModel(String name) {
return querySchemes.stream() return querySchemes.stream()
.map(queryScheme -> queryScheme.get(name)) .map(queryScheme -> queryScheme.get(name))
.filter(Objects::nonNull)
.findFirst() .findFirst()
.get(); .get();
} }
......
package com.huigou.uasp.bmp.common.treeview; package com.huigou.uasp.bmp.common.treeview;
import java.io.IOException; import com.huigou.exception.ApplicationException;
import java.io.InputStream;
import org.springframework.core.io.ClassPathResource;
import com.huigou.exception.NotFoundException; import com.huigou.exception.NotFoundException;
import com.huigou.exception.ResourceLoadException;
import com.huigou.uasp.bmp.common.treeview.domain.TreeViewLoadInterface; import com.huigou.uasp.bmp.common.treeview.domain.TreeViewLoadInterface;
import com.huigou.uasp.bmp.common.treeview.domain.model.TreeViewMappingModel; import com.huigou.uasp.bmp.common.treeview.domain.model.TreeViewMappingModel;
import com.huigou.uasp.bmp.treeview.TreeMappingsDocument; import com.huigou.uasp.bmp.treeview.TreeMappingsDocument;
import com.huigou.util.ResourceLoadManager; import com.huigou.util.ResourceLoadManager;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.xmlbeans.XmlException;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
/** /**
* 树配置管理 * 树配置管理
* @author Gerald
* *
* @author Gerald
*/ */
public class TreeViewManager extends ResourceLoadManager<TreeViewMappingModel> implements TreeViewLoadInterface { public class TreeViewManager extends ResourceLoadManager<TreeViewMappingModel> implements TreeViewLoadInterface {
/** /**
* 加载配置文件 * SQL 方言。
*
* @since 1.1.3
*/
private String sqlDialect;
/**
* 设置SQL方言。
* *
* @Title: loadConfigFile * @since 1.1.3
* @author
* @Description: TODO
* @param @param fileName
* @param @return
* @param @throws Exception
* @return Domain
* @throws
*/ */
public void setSqlDialect(String sqlDialect) {
this.sqlDialect = sqlDialect;
}
@Override
public TreeViewMappingModel loadConfigFile(String type) throws NotFoundException { public TreeViewMappingModel loadConfigFile(String type) throws NotFoundException {
InputStream inputStream = null; List<String> paths = new ArrayList<>(2);
String path = String.format(XML_PATH, type); // 方言xml放在最前面,从而达到优先使用方言xml的目的
if (StringUtils.isNotBlank(sqlDialect)) {
paths.add(String.format(DIALECT_XML_PATH, sqlDialect, type));
}
String defaultPath = String.format(XML_PATH, type);
paths.add(defaultPath);
List<InputStream> xmlStreams = Collections.emptyList();
try { try {
ClassPathResource resource = getResource(path); List<Resource> resources = paths.stream()
inputStream = resource.getInputStream(); .map(ClassPathResource::new)
TreeMappingsDocument doc = TreeMappingsDocument.Factory.parse(inputStream); .filter(Resource::exists)
TreeViewMappingModel mappingModel = new TreeViewMappingModel(doc.getTreeMappings()); .collect(Collectors.toList());
mappingModel.setVersions(resource.lastModified());
mappingModel.setConfigFilePath(path); xmlStreams = resources.stream()
return mappingModel; .map(xml -> {
} catch (Exception e) {
throw new NotFoundException("读取配置文件失败:" + e.getMessage());
} finally {
if (inputStream != null) {
try { try {
inputStream.close(); return xml.getInputStream();
} catch (IOException e) { } catch (IOException ioe) {
e.printStackTrace(); throw new ApplicationException(ioe);
} }
})
.collect(Collectors.toList());
List<TreeMappingsDocument.TreeMappings> mappings = xmlStreams.stream().map(is -> {
try {
return TreeMappingsDocument.Factory.parse(is).getTreeMappings();
} catch (XmlException | IOException e) {
throw new ApplicationException(e);
} }
}).collect(Collectors.toList());
TreeViewMappingModel mappingModel = new TreeViewMappingModel(mappings);
mappingModel.setVersions(maxLastModified(resources));
mappingModel.setConfigFilePaths(paths);
return mappingModel;
} catch (Exception e) {
throw new ResourceLoadException(String.format("读取配置文件[%s]失败:%s", defaultPath, e.getMessage()));
} finally {
xmlStreams.forEach(IOUtils::closeQuietly);
} }
} }
} }
\ No newline at end of file
...@@ -10,10 +10,22 @@ import com.huigou.uasp.bmp.common.treeview.domain.model.TreeViewMappingModel; ...@@ -10,10 +10,22 @@ import com.huigou.uasp.bmp.common.treeview.domain.model.TreeViewMappingModel;
* @author xx * @author xx
*/ */
public interface TreeViewLoadInterface { public interface TreeViewLoadInterface {
/**
* @since 1.1.3
*/
String XML_BASE_DIRECTORY = "config/content/treeview";
/** /**
* 配置文件默认路径 * 配置文件默认路径
*/ */
public final static String XML_PATH = "config/content/treeview/tree-view-%s.xml"; String XML_PATH = String.join("/", XML_BASE_DIRECTORY, "tree-view-%s.xml");
/**
* 方言配置文件路径
*
* @since 1.1.3
*/
String DIALECT_XML_PATH = String.join("/", XML_BASE_DIRECTORY, "%s", "tree-view-%s.xml");
public TreeViewMappingModel loadConfigFile(String path) throws ResourceLoadException; TreeViewMappingModel loadConfigFile(String path) throws ResourceLoadException;
} }
package com.huigou.uasp.bmp.common.treeview.domain.model; package com.huigou.uasp.bmp.common.treeview.domain.model;
import java.io.Serializable; import java.io.Serializable;
import java.util.Collections; import java.util.*;
import java.util.HashMap; import java.util.stream.Collectors;
import java.util.List;
import java.util.Map;
import com.huigou.data.query.XMLParseUtil; import com.huigou.data.query.XMLParseUtil;
import com.huigou.data.query.parser.model.ConditionModel; import com.huigou.data.query.parser.model.ConditionModel;
...@@ -28,17 +26,26 @@ public class TreeViewMappingModel implements Serializable, ConfigFileVersion { ...@@ -28,17 +26,26 @@ public class TreeViewMappingModel implements Serializable, ConfigFileVersion {
private static final long serialVersionUID = 173704882069216812L; private static final long serialVersionUID = 173704882069216812L;
private Map<String, TreeModel> trees;// 包含查询配置文件 /**
* 包含查询配置文件
*/
private final List<Map<String, TreeModel>> trees;
private Long version; private Long version;
private List<String> configFilePaths; private List<String> configFilePaths;
public TreeViewMappingModel(TreeMappings mapping) { public TreeViewMappingModel(TreeMappings mapping) {
trees = new HashMap<>(mapping.getTreeArray().length); this(Arrays.asList(mapping));
for (Tree tree : mapping.getTreeArray()) {
trees.put(tree.getName(), parseTreeModel(tree));
} }
/**
* @since 1.1.3
*/
public TreeViewMappingModel(List<TreeMappings> mappings) {
trees = mappings.stream()
.map(mapping -> Arrays.stream(mapping.getTreeArray()).collect(Collectors.toMap(Tree::getName, this::parseTreeModel)))
.collect(Collectors.toList());
} }
/** /**
...@@ -63,7 +70,8 @@ public class TreeViewMappingModel implements Serializable, ConfigFileVersion { ...@@ -63,7 +70,8 @@ public class TreeViewMappingModel implements Serializable, ConfigFileVersion {
model.setDefaultCondition(dataModel.getDefaultCondition()); model.setDefaultCondition(dataModel.getDefaultCondition());
model.setOrderby(dataModel.getOrderby()); model.setOrderby(dataModel.getOrderby());
model.setOrder(dataModel.getOrder()); model.setOrder(dataModel.getOrder());
for (Condition condition : dataModel.getConditionArray()) {// 根据查询条件定义组合查询语句 for (Condition condition : dataModel.getConditionArray()) {
// 根据查询条件定义组合查询语句
ConditionModel conditionModel = ConditionModel.newInstance(condition); ConditionModel conditionModel = ConditionModel.newInstance(condition);
conditionModel.setFormula(XMLParseUtil.getNodeTextValue(condition)); conditionModel.setFormula(XMLParseUtil.getNodeTextValue(condition));
model.addConditions(conditionModel); model.addConditions(conditionModel);
...@@ -72,7 +80,11 @@ public class TreeViewMappingModel implements Serializable, ConfigFileVersion { ...@@ -72,7 +80,11 @@ public class TreeViewMappingModel implements Serializable, ConfigFileVersion {
} }
public TreeModel getTreeModel(String name) { public TreeModel getTreeModel(String name) {
return trees.get(name); return trees.stream()
.map(queryScheme -> queryScheme.get(name))
.filter(Objects::nonNull)
.findFirst()
.get();
} }
@Override @Override
...@@ -89,6 +101,14 @@ public class TreeViewMappingModel implements Serializable, ConfigFileVersion { ...@@ -89,6 +101,14 @@ public class TreeViewMappingModel implements Serializable, ConfigFileVersion {
return configFilePaths.get(0); return configFilePaths.get(0);
} }
/**
* @deprecated 已被 {@link #setConfigFilePaths(List)} 替代。
*/
@Deprecated
public void setConfigFilePath(String configFilePath) {
this.configFilePaths = Collections.singletonList(configFilePath);
}
public void setConfigFilePaths(List<String> configFilePaths) { public void setConfigFilePaths(List<String> configFilePaths) {
this.configFilePaths = configFilePaths; this.configFilePaths = configFilePaths;
} }
...@@ -97,9 +117,4 @@ public class TreeViewMappingModel implements Serializable, ConfigFileVersion { ...@@ -97,9 +117,4 @@ public class TreeViewMappingModel implements Serializable, ConfigFileVersion {
public List<String> getFilePaths() { public List<String> getFilePaths() {
return configFilePaths; return configFilePaths;
} }
public void setConfigFilePath(String configFilePath) {
this.configFilePaths = Collections.singletonList(configFilePath);
}
} }
...@@ -24,7 +24,9 @@ ...@@ -24,7 +24,9 @@
<property name="sqlDialect" value="${sqlDialect}"/> <property name="sqlDialect" value="${sqlDialect}"/>
</bean> </bean>
<!-- 快捷树查询配置 --> <!-- 快捷树查询配置 -->
<bean id="treeViewManager" class="com.huigou.uasp.bmp.common.treeview.TreeViewManager"/> <bean id="treeViewManager" class="com.huigou.uasp.bmp.common.treeview.TreeViewManager">
<property name="sqlDialect" value="${sqlDialect}"/>
</bean>
<bean id="treeViewApplication" class="com.huigou.uasp.bmp.common.treeview.application.impl.TreeViewApplicationImpl"> <bean id="treeViewApplication" class="com.huigou.uasp.bmp.common.treeview.application.impl.TreeViewApplicationImpl">
<property name="jdbcDao" ref="jdbcDao"/> <property name="jdbcDao" ref="jdbcDao"/>
<property name="permissionBuilder" ref="queryPermissionBuilder"/> <property name="permissionBuilder" ref="queryPermissionBuilder"/>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment