HibernateStatementSessionFactoryBean.java 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. package com.primeton.dgs.kernel.core.dao.hibernate;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.Serializable;
  6. import java.util.ArrayList;
  7. import java.util.HashMap;
  8. import java.util.Iterator;
  9. import java.util.List;
  10. import java.util.Map;
  11. import java.util.Optional;
  12. import java.util.concurrent.atomic.AtomicInteger;
  13. import org.jdom.Document;
  14. import org.jdom.Element;
  15. import org.jdom.JDOMException;
  16. import org.jdom.Text;
  17. import org.jdom.input.SAXBuilder;
  18. import org.slf4j.Logger;
  19. import org.slf4j.LoggerFactory;
  20. import org.springframework.core.io.Resource;
  21. import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
  22. import org.springframework.core.io.support.ResourcePatternResolver;
  23. import com.primeton.dgs.kernel.core.ex.HibernateStatementException;
  24. import com.primeton.dgs.kernel.core.util.MetadataWebContext;
  25. public class HibernateStatementSessionFactoryBean implements Serializable {
  26. private static final long serialVersionUID = 1L;
  27. private static final String PREFIX = "META-INF/sqlxml/"; // 加载模块的SQL文件的路径前缀
  28. private static final String ENDFIX = "*Sql.xml"; // 文件名的扩展后缀名
  29. private HibernateStatementSessionFactoryBean parent;
  30. private static HibernateStatementSessionFactoryBean instance;
  31. private Map<String, List<HibernateStatementField>> statements;
  32. private boolean ready = false;
  33. private Resource[] statementLocations;
  34. private boolean merge = false;
  35. private static Logger log = LoggerFactory.getLogger(HibernateStatementSessionFactoryBean.class);
  36. public HibernateStatementSessionFactoryBean() {
  37. instance = this;
  38. this.statements = new HashMap();
  39. }
  40. public void setStatementResources(String[] statementResources) {
  41. setStatementResources();
  42. }
  43. public void setStatementLocations(File[] statementLocations) {
  44. setStatementResources();
  45. }
  46. public void setStatementLocations(Resource[] statementLocations) {
  47. this.statementLocations = statementLocations;
  48. }
  49. public void setStatementResources(String statementResources) {
  50. setStatementResources();
  51. }
  52. private void setStatementResources() {
  53. try {
  54. ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
  55. statementLocations = resolver.getResources("classpath*:"
  56. .concat(PREFIX)
  57. .concat(MetadataWebContext.getInstance().getDatabaseName()
  58. .toLowerCase()).concat("/").concat(ENDFIX));
  59. System.setProperty("dataBaseName", MetadataWebContext.getInstance().getDatabaseName());
  60. } catch (IOException e) {
  61. log.error("加载 NativeSQL 异常。", e);
  62. }
  63. }
  64. public Resource[] getStatementLocations() {
  65. return this.statementLocations;
  66. }
  67. public boolean isReady() {
  68. return this.ready;
  69. }
  70. public HibernateStatementSessionFactoryBean getParent() {
  71. return this.parent;
  72. }
  73. public void setParent(HibernateStatementSessionFactoryBean parent) {
  74. this.parent = parent;
  75. }
  76. public boolean isMerge() {
  77. return this.merge;
  78. }
  79. public void setMerge(boolean merge) {
  80. this.merge = merge;
  81. }
  82. public void init() throws IOException {
  83. this.ready = false;
  84. this.statements.clear();
  85. for (int i = 0; i < this.statementLocations.length; i++) {
  86. config(this.statementLocations[i].getInputStream());
  87. log.info("read xml sql template file: {}", statementLocations[i].getURI());
  88. }
  89. if ((isMerge()) && (this.parent != null)) {
  90. doMerge();
  91. }
  92. this.ready = true;
  93. }
  94. private void doMerge() {
  95. if (this.parent != null)
  96. for (Iterator it = this.statements.keySet().iterator(); it
  97. .hasNext();) {
  98. String key = (String) it.next();
  99. if (this.parent.statements.containsKey(key)) {
  100. throw new HibernateStatementException("statement of id=\""
  101. + key + "\" has bean defined!");
  102. }
  103. this.parent.statements.put(key, (List) this.statements.get(key));
  104. }
  105. }
  106. public void config(InputStream in) throws IOException {
  107. SAXBuilder sb = new SAXBuilder();
  108. try {
  109. Document doc = sb.build(in);
  110. Element root = doc.getRootElement();
  111. List statements = root.getChildren("statement");
  112. for (int i = 0; i < statements.size(); i++) {
  113. Element stmt = (Element) statements.get(i);
  114. String id = stmt.getAttributeValue("id");
  115. if ((id == null) || ("".equals(id.trim()))) {
  116. throw new HibernateStatementException(
  117. "statement''s \"id\" should not be empty!");
  118. }
  119. if (this.statements.containsKey(id)) {
  120. throw new HibernateStatementException("statement of id=\""
  121. + id + "\" has bean defined!");
  122. }
  123. List fields = stmt.getContent();
  124. this.statements.put(id, readFields(fields));
  125. }
  126. } catch (JDOMException e) {
  127. throw new HibernateStatementException("statement file is error!", e);
  128. } catch (IOException e) {
  129. throw new HibernateStatementException("statement file not exist!",
  130. e);
  131. } finally {
  132. if (in != null)
  133. in.close();
  134. }
  135. }
  136. private List<HibernateStatementField> readFields(List<?> fields) {
  137. List result = new ArrayList();
  138. for (int j = 0; j < fields.size(); j++) {
  139. if ((fields.get(j) instanceof Text)) {
  140. String text = ((Text) fields.get(j)).getTextTrim();
  141. if (!"".equals(text)) {
  142. result.add(new HibernateStatementTextField(text));
  143. }
  144. } else if ((fields.get(j) instanceof Element)) {
  145. Element field = (Element) fields.get(j);
  146. if ("dynamic-field".equals(field.getName())) {
  147. result.add(readDynamicField(field));
  148. } else if ("compare-field".equals(field.getName())) {
  149. result.add(readCompareField(field));
  150. } else {
  151. throw new HibernateStatementException("\""
  152. + field.getName() + "\" is not supportable!");
  153. }
  154. }
  155. }
  156. return result;
  157. }
  158. private HibernateStatementDynamicField readDynamicField(Element field) {
  159. String property = field.getAttributeValue("property");
  160. String onEmpty = field.getAttributeValue("onEmpty");
  161. if ((property == null) || ("".equals(property))) {
  162. throw new HibernateStatementException(
  163. "\"property\" of dynamic-field is required!");
  164. }
  165. HibernateStatementDynamicField f = new HibernateStatementDynamicField(
  166. property, onEmpty);
  167. List children = readFields(field.getContent());
  168. f.getChildren().addAll(children);
  169. return f;
  170. }
  171. private HibernateStatementCompareField readCompareField(Element field) {
  172. String compare = field.getAttributeValue("compare");
  173. String compareProperty = field.getAttributeValue("compareProperty");
  174. String compareValue = field.getAttributeValue("compareValue");
  175. if ((compare == null) || ("".equals(compare))) {
  176. throw new HibernateStatementException(
  177. "Attribute \"compare\" of compare-field is required!");
  178. }
  179. if ((compareProperty == null) || ("".equals(compareProperty))) {
  180. throw new HibernateStatementException(
  181. "Attribute \"compareProperty\" of compare-field is required!");
  182. }
  183. if ((compareValue == null) || ("".equals(compareValue))) {
  184. throw new HibernateStatementException(
  185. "Attribute \"compareValue\" of compare-field is required!");
  186. }
  187. if (!HibernateStatementCompareField.isAvailableCompare(compare)) {
  188. throw new HibernateStatementException(
  189. "Attribute \"compare\" of compare-field is unavailable, the available value is (eq,ne,gt,ge,lt,le)!");
  190. }
  191. HibernateStatementCompareField f = new HibernateStatementCompareField(
  192. compare, compareProperty, compareValue);
  193. List children = readFields(field.getContent());
  194. f.getChildren().addAll(children);
  195. return f;
  196. }
  197. public void destroy() {
  198. this.statements.clear();
  199. this.ready = false;
  200. }
  201. public void refresh() throws IOException {
  202. destroy();
  203. init();
  204. }
  205. public List<HibernateStatementField> getStatement(String id) {
  206. List<HibernateStatementField> statementFields = this.statements.get(id);
  207. if (statementFields == null) {
  208. if (this.parent == null) {
  209. log.error("statement [id=\"" + id+ "\"]: not existed");
  210. /*throw new HibernateStatementException("statement [id=\"" + id
  211. + "\"]: not existed");*/
  212. return null;
  213. }
  214. return this.parent.getStatement(id);
  215. }
  216. return (List) statementFields;
  217. }
  218. public String getText(String id) {
  219. return getText(id, null);
  220. }
  221. public String getText(String id, Object vo) {
  222. List<?> fileds = getStatement(id);
  223. if(null == fileds){
  224. return null;
  225. }
  226. StringBuffer sb = new StringBuffer();
  227. AtomicInteger index = new AtomicInteger(0);
  228. try {
  229. for (int i = 0; i < fileds.size(); i++) {
  230. StringBuffer s = ((HibernateStatementField) fileds.get(i)).getText(vo, index);
  231. if ((s != null) && (s.length() > 0))
  232. sb.append(s).append(" ");
  233. }
  234. } catch (NumberFormatException e) {
  235. throw new HibernateStatementException("statement [id=\"" + id
  236. + "\"]: cannot format number " + e.getMessage());
  237. } catch (Exception e) {
  238. throw new HibernateStatementException("statement [id=\"" + id
  239. + "\"]: " + e.getMessage(), e);
  240. }
  241. return sb.toString();
  242. }
  243. public static String getStatementText(String id) {
  244. return getStatementText(id, null);
  245. }
  246. public static String getStatementText(String id, Object vo) {
  247. return instance.getText(id, vo);
  248. }
  249. public Object[] getParam(String id, Object vo) {
  250. List fileds = Optional.ofNullable(getStatement(id)).orElse(new ArrayList<>(0));
  251. List result = new ArrayList();
  252. for (int i = 0; i < fileds.size(); i++) {
  253. List list = ((HibernateStatementField) fileds.get(i)).getParam(vo);
  254. if ((list != null) && (!list.isEmpty())) {
  255. result.addAll(list);
  256. }
  257. }
  258. return result.toArray();
  259. }
  260. public Map<String, Object> getParamMap(String id, Object vo) {
  261. if (!this.statements.containsKey(id)) {
  262. throw new HibernateStatementException("statement [id=\"" + id
  263. + "\"]: not existed");
  264. }
  265. Map result = new HashMap();
  266. List fileds = getStatement(id);
  267. for (int i = 0; i < fileds.size(); i++) {
  268. Map map = ((HibernateStatementField) fileds.get(i)).getParamMap(vo);
  269. if ((map != null) && (!map.isEmpty())) {
  270. result.putAll(map);
  271. }
  272. }
  273. return result;
  274. }
  275. public static Object[] getStatementParam(String id, Object vo) {
  276. return instance.getParam(id, vo);
  277. }
  278. public static Map<String, Object> getStatementParamMap(String id, Object vo) {
  279. return instance.getParamMap(id, vo);
  280. }
  281. public static boolean isStateReady() {
  282. return instance.isReady();
  283. }
  284. }