HibernateStatementField.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package com.primeton.dgs.kernel.core.dao.hibernate;
  2. import java.io.Serializable;
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.Iterator;
  6. import java.util.List;
  7. import java.util.Map;
  8. import java.util.concurrent.atomic.AtomicInteger;
  9. import java.util.regex.Matcher;
  10. import java.util.regex.Pattern;
  11. import org.apache.commons.beanutils.PropertyUtils;
  12. import com.primeton.dgs.kernel.core.ex.HibernateStatementException;
  13. public abstract class HibernateStatementField implements Serializable {
  14. private static final long serialVersionUID = 1L;
  15. public static final String SPACE = " ";
  16. public static final Pattern PLACEHOLDER = Pattern.compile("#[\\w]+#");
  17. public static final Pattern REPLACEMENT = Pattern.compile("\\$[\\w]+\\$");
  18. protected List<HibernateStatementField> children = new ArrayList();
  19. public List<HibernateStatementField> getChildren() {
  20. return this.children;
  21. }
  22. protected StringBuffer getChildrenRawText(Object vo) {
  23. StringBuffer sb = new StringBuffer();
  24. for (Iterator it = this.children.iterator(); it.hasNext();) {
  25. StringBuffer s = ((HibernateStatementField) it.next())
  26. .getRawText(vo);
  27. if ((s != null) && (s.length() > 0)) {
  28. sb.append(s).append(" ");
  29. }
  30. }
  31. return sb;
  32. }
  33. protected StringBuffer getChildrenText(Object vo, AtomicInteger index) {
  34. StringBuffer sb = new StringBuffer();
  35. for (Iterator it = this.children.iterator(); it.hasNext();) {
  36. StringBuffer s = ((HibernateStatementField) it.next()).getText(vo, index);
  37. if ((s != null) && (s.length() > 0)) {
  38. sb.append(s).append(" ");
  39. }
  40. }
  41. return sb;
  42. }
  43. public abstract StringBuffer getRawText(Object paramObject);
  44. public abstract StringBuffer getText(Object paramObject, AtomicInteger index);
  45. public List<Object> getParam(Object vo) {
  46. StringBuffer sb = getRawText(vo);
  47. if (sb == null) {
  48. return null;
  49. }
  50. List result = new ArrayList();
  51. Matcher m = PLACEHOLDER.matcher(sb);
  52. while (m.find()) {
  53. String placeholder = sb.substring(m.start(), m.end());
  54. String property = placeholder
  55. .substring(1, placeholder.length() - 1);
  56. result.add(getBeanProperty(vo, property));
  57. }
  58. return result;
  59. }
  60. public Map<String, Object> getParamMap(Object vo) {
  61. StringBuffer sb = getRawText(vo);
  62. if (sb == null) {
  63. return null;
  64. }
  65. Map result = new HashMap();
  66. Matcher m = PLACEHOLDER.matcher(sb);
  67. while (m.find()) {
  68. String placeholder = sb.substring(m.start(), m.end());
  69. String property = placeholder
  70. .substring(1, placeholder.length() - 1);
  71. result.put(property, getBeanProperty(vo, property));
  72. }
  73. return result;
  74. }
  75. protected Object getBeanProperty(Object vo, String property) {
  76. if (vo == null)
  77. return null;
  78. try {
  79. return PropertyUtils.getProperty(vo, property);
  80. } catch (NoSuchMethodException e) {
  81. return null;
  82. } catch (Exception e) {
  83. throw new HibernateStatementException("property \"" + property
  84. + "\" is missing!", e);
  85. }
  86. }
  87. public static final boolean isEmpty(Object value) {
  88. if (value == null) {
  89. return true;
  90. }
  91. if ((value instanceof String)) {
  92. return "".equals(((String) value).trim());
  93. }
  94. return false;
  95. }
  96. }