HibernateStatementCompareField.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package com.primeton.dgs.kernel.core.dao.hibernate;
  2. import java.math.BigDecimal;
  3. import java.math.BigInteger;
  4. import java.util.concurrent.atomic.AtomicInteger;
  5. public class HibernateStatementCompareField extends HibernateStatementField
  6. {
  7. private static final long serialVersionUID = 1L;
  8. private String compare;
  9. private String compareProperty;
  10. private String compareValue;
  11. public static final String AVAILABLE_COMPARE = "eq,ne,gt,ge,lt,le,isnull,isnotnull,isempty,isnotempty";
  12. public String getCompare()
  13. {
  14. return this.compare;
  15. }
  16. public void setCompare(String compare) {
  17. this.compare = compare;
  18. }
  19. public String getCompareProperty() {
  20. return this.compareProperty;
  21. }
  22. public void setCompareProperty(String compareProperty) {
  23. this.compareProperty = compareProperty;
  24. }
  25. public String getCompareValue() {
  26. return this.compareValue;
  27. }
  28. public void setCompareValue(String compareValue) {
  29. this.compareValue = compareValue;
  30. }
  31. public HibernateStatementCompareField()
  32. {
  33. }
  34. public HibernateStatementCompareField(String compare, String compareProperty, String compareValue) {
  35. this.compare = compare;
  36. this.compareProperty = compareProperty;
  37. this.compareValue = compareValue;
  38. }
  39. private static int getCompareOperateCode(String compare)
  40. {
  41. String[] ac = AVAILABLE_COMPARE.split(",");
  42. for (int i = 0; i < ac.length; i++) {
  43. if (ac[i].equalsIgnoreCase(compare)) {
  44. return i;
  45. }
  46. }
  47. return -1;
  48. }
  49. public static boolean isAvailableCompare(String compare)
  50. {
  51. return getCompareOperateCode(compare) >= 0;
  52. }
  53. private static boolean match(String compare, Object value, String compareValue)
  54. throws NumberFormatException
  55. {
  56. if("null".equals(compareValue) && compare.equals("eq")) {
  57. compare = "isnull";
  58. } else if("null".equals(compareValue) && compare.equals("ne")) {
  59. compare = "isnotnull";
  60. }
  61. switch (compare) {
  62. case "isnull":
  63. return value == null;
  64. case "isnotnull":
  65. return value != null;
  66. case "isempty":
  67. return StringUtils.isEmpty("" + (value == null ? "" : String.valueOf(value)));
  68. case "isnotempty":
  69. return StringUtils.isNotEmpty(""+ (value == null ? "" : String.valueOf(value)));
  70. }
  71. if (value == null) {
  72. return false;
  73. }
  74. int reslt = -1;
  75. if ((value instanceof String)) {
  76. reslt = ((String)value).compareTo(compareValue);
  77. } else if ((value instanceof Integer)) {
  78. reslt = ((Integer)value).compareTo(new Integer(compareValue));
  79. } else if ((value instanceof Long)) {
  80. reslt = ((Long)value).compareTo(new Long(compareValue));
  81. } else if ((value instanceof Float)) {
  82. reslt = ((Float)value).compareTo(new Float(compareValue));
  83. } else if ((value instanceof Double)) {
  84. reslt = ((Double)value).compareTo(new Double(compareValue));
  85. } else if ((value instanceof Short)) {
  86. reslt = ((Short)value).compareTo(new Short(compareValue));
  87. } else if ((value instanceof BigInteger)) {
  88. reslt = ((BigInteger)value).compareTo(new BigInteger(compareValue));
  89. } else if ((value instanceof BigDecimal)) {
  90. reslt = ((BigDecimal)value).compareTo(new BigDecimal(compareValue));
  91. } else if ((value instanceof Boolean)) {
  92. int intBool = ((Boolean)value).booleanValue() ? 1 : 0;
  93. int intCompareBool = Boolean.valueOf(compareValue).booleanValue() ? 1 : 0;
  94. reslt = intBool - intCompareBool;
  95. }
  96. int code = getCompareOperateCode(compare);
  97. if (reslt == 0) {
  98. if ((code == 0) || (code == 3) || (code == 5))
  99. return true;
  100. }
  101. else if (reslt > 0) {
  102. if ((code == 1) || (code == 2) || (code == 3))
  103. return true;
  104. }
  105. else if ((reslt < 0) && (
  106. (code == 1) || (code == 4) || (code == 5))) {
  107. return true;
  108. }
  109. return false;
  110. }
  111. @Override
  112. public StringBuffer getRawText(Object vo)
  113. {
  114. return getText(vo, true, new AtomicInteger(0));
  115. }
  116. @Override
  117. public StringBuffer getText(Object vo, AtomicInteger index)
  118. {
  119. return getText(vo, false, index);
  120. }
  121. private StringBuffer getText(Object vo, boolean isRaw, AtomicInteger index)
  122. {
  123. StringBuffer sb = null;
  124. Object actualValue = getBeanProperty(vo, this.compareProperty);
  125. if (match(this.compare, actualValue, this.compareValue)) {
  126. sb = isRaw ? getChildrenRawText(vo) : getChildrenText(vo, index);
  127. }
  128. return sb;
  129. }
  130. }