HibernateStatementTextField.java 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package com.primeton.dgs.kernel.core.dao.hibernate;
  2. import java.util.Collection;
  3. import java.util.HashMap;
  4. import java.util.Iterator;
  5. import java.util.concurrent.atomic.AtomicInteger;
  6. import java.util.regex.Matcher;
  7. public class HibernateStatementTextField extends HibernateStatementField
  8. {
  9. private static final long serialVersionUID = 1L;
  10. private String value = "";
  11. public String getValue() {
  12. return this.value;
  13. }
  14. public void setValue(String value) {
  15. this.value = value;
  16. }
  17. public HibernateStatementTextField()
  18. {
  19. }
  20. public HibernateStatementTextField(String value) {
  21. this.value = value;
  22. }
  23. @Override
  24. public StringBuffer getText(Object vo, AtomicInteger index)
  25. {
  26. StringBuffer sb = getRawText(vo);
  27. StringBuffer sbb = new StringBuffer();
  28. Matcher m = PLACEHOLDER.matcher(sb);
  29. while (m.find()) {
  30. // add zhaopx, Hibernate5 需要在 ? 后加一个索引值
  31. String rep = (index.get() < 0 ? "?" : "?"+index);
  32. m.appendReplacement(sbb, rep);
  33. index.incrementAndGet();
  34. }
  35. m.appendTail(sbb);
  36. return sbb;
  37. }
  38. public StringBuffer getRawText(Object vo)
  39. {
  40. StringBuffer sb = new StringBuffer();
  41. Matcher m = REPLACEMENT.matcher(value);
  42. while (m.find()) {
  43. String group = m.group();
  44. String property = group.substring(1, group.length() - 1);
  45. StringBuffer replace = getReplacementText(getBeanProperty(vo, property));
  46. if ((replace != null) && (replace.length() > 0)) {
  47. m.appendReplacement(sb, replace.toString());
  48. }
  49. }
  50. m.appendTail(sb);
  51. return sb;
  52. }
  53. private StringBuffer getReplacementText(Object value)
  54. {
  55. if (value == null) {
  56. return null;
  57. }
  58. if ((value instanceof Object[])) {
  59. return getReplacementTextFromArray((Object[])value);
  60. }
  61. if ((value instanceof Collection)) {
  62. return getReplacementTextFromCollection((Collection)value);
  63. }
  64. return new StringBuffer().append(value);
  65. }
  66. private StringBuffer getReplacementTextFromArray(Object[] array)
  67. {
  68. if ((array == null) || (array.length == 0)) {
  69. return null;
  70. }
  71. StringBuffer sb = new StringBuffer();
  72. for (int i = 0; i < array.length; i++) {
  73. if (i > 0) sb.append(",");
  74. sb.append(array[i]);
  75. }
  76. return sb;
  77. }
  78. private StringBuffer getReplacementTextFromCollection(Collection<?> collection)
  79. {
  80. if ((collection == null) || (collection.isEmpty())) {
  81. return null;
  82. }
  83. StringBuffer sb = new StringBuffer();
  84. int i = 0;
  85. for (Iterator it = collection.iterator(); it.hasNext(); i++) {
  86. if (i > 0) sb.append(",");
  87. sb.append(it.next());
  88. }
  89. return sb;
  90. }
  91. }