Guest User

Untitled

a guest
Jun 17th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.29 KB | None | 0 0
  1. import org.springframework.beans.BeanUtils
  2. import java.lang.reflect.Field;
  3. import java.lang.reflect.Method;
  4.  
  5. @Category(Object)
  6. class PropertyUtils {
  7.  
  8. def getPropertyValue (Class clazz, String name, Object ob) {
  9. Method getter = BeanUtils.findDeclaredMethod(clazz, getGetterName(name), null)
  10. try {
  11. if (getter != null) {
  12. return getter.invoke(ob)
  13. }
  14. Field f = clazz.getDeclaredField(name)
  15. if (f != null) {
  16. return f.get(ob)
  17. }
  18. }
  19. catch (Exception ignored) {
  20. // ignored.printStackTrace()
  21. }
  22. return null
  23. }
  24.  
  25. /**
  26. * <p>Get a static property value, which has a public static getter or is just a public static field.</p>
  27. *
  28. * @param clazz The class to check for static property
  29. * @param name The property name
  30. * @return The value if there is one, or null if unset OR there is no such property
  31. */
  32. def getStaticPropertyValue (Class clazz, String name) {
  33. Method getter = BeanUtils.findDeclaredMethod(clazz, getGetterName(name), null)
  34. try {
  35. if (getter != null) {
  36. return getter.invoke(null)
  37. }
  38. Field f = clazz.getDeclaredField(name)
  39. if (f != null) {
  40. return f.get(null)
  41. }
  42. }
  43. catch (Exception ignored) {
  44. // ignored.printStackTrace()
  45. }
  46. return null
  47. }
  48.  
  49. public String getGetterName (String propertyName) {
  50. return "get${Character.toUpperCase(propertyName.charAt(0))}${propertyName.substring(1)}"
  51. }
  52. }
  53.  
  54. @Category(Object)
  55. class ClassUtils {
  56.  
  57. List getSuperClassChain (Class theClass) {
  58. LinkedList classChain = new LinkedList ()
  59. Class clazz = theClass
  60. while (clazz != Object) {
  61. classChain.addFirst( clazz)
  62. clazz = clazz.getSuperclass()
  63. }
  64. return classChain
  65. }
  66.  
  67. }
  68.  
  69. @Category(Object)
  70. class Validations {
  71.  
  72. def validatesPresenceOf (property, options = [:]) {
  73. if (!this."${property}" || this."${property}".length() == 0) {
  74. errors.addPropertyError(property, options['message'] ?: 'Required')
  75. }
  76. }
  77.  
  78. }
  79.  
  80. class ValidationErrors {
  81. def propertyErrors = [:]
  82. def baseErrors = []
  83.  
  84. def addBaseError (message) {
  85. baseErrors << message
  86. }
  87.  
  88. def addToBase (message) {
  89. addBaseError(message)
  90. }
  91.  
  92. def addPropertyError (property, message) {
  93. initializePropertyErrors(property)
  94. propertyErrors[property] << message
  95. }
  96.  
  97. def getErrorsOn (property) {
  98. propertyErrors(property)
  99. }
  100.  
  101. def initializePropertyErrors (property) {
  102. if (propertyErrors[property] == null) {
  103. propertyErrors[property] = []
  104. }
  105. }
  106.  
  107. def hasErrors () {
  108. hasBaseErrors() || hasPropertyErrors()
  109. }
  110.  
  111. def hasBaseErrors () {
  112. baseErrors.size > 0
  113. }
  114.  
  115. def hasPropertyErrors () {
  116. getPropertyMessages().size() > 0
  117. }
  118.  
  119. def reset () {
  120. propertyErrors = [:]
  121. baseErrors = []
  122. }
  123.  
  124. def getFullMessages () {
  125. def result = []
  126. result << getBaseMessages()
  127. result << getPropertyMessages()
  128. result.flatten()
  129. }
  130.  
  131. def getBaseMessages () {
  132. baseErrors
  133. }
  134.  
  135. def getPropertyMessages () {
  136. propertyErrors.keySet().collect { key -> propertyErrors[key].collect { message -> "${key} ${message}"} }.flatten()
  137. }
  138. }
  139.  
  140. @Category(Object)
  141. @Mixin(PropertyUtils)
  142. @Mixin(ClassUtils)
  143. @Mixin(Validations)
  144. class Base {
  145.  
  146. def isValid () {
  147. initializeErrors()
  148. doValidations()
  149. errors.hasErrors()
  150. }
  151.  
  152. def initializeErrors () {
  153. def errors = new ValidationErrors ()
  154. def emc = new ExpandoMetaClass( this.class, false )
  155. emc.getErrors = { -> errors }
  156. emc.initialize()
  157.  
  158. this.metaClass = emc
  159. }
  160.  
  161. def doValidations () {
  162. doValidation('validates')
  163. if (isPersisted()) {
  164. doValidation('validatesOnUpdate')
  165. } else {
  166. doValidation('validatesOnCreate')
  167. }
  168. }
  169.  
  170. def doValidation (validationClosureName) {
  171. println "Performing ${validationClosureName} validation."
  172. getSuperClassChain(this.class).collect { klass ->
  173. getStaticPropertyValue(klass, validationClosureName)
  174. }.findAll {
  175. it != null
  176. }.each {
  177. it.setDelegate(this)
  178. it.call()
  179. }
  180. }
  181.  
  182. def isPersisted () {
  183. this.id != null
  184. }
  185. }
  186.  
  187. @Mixin(Base)
  188. class Person {
  189. def id
  190. def name
  191. static validates = { bean ->
  192. validatesPresenceOf('name')
  193. errors.addToBase("FRACK!")
  194. }
  195. }
  196.  
  197. class Employee extends Person {
  198. def position
  199. static validates = { bean ->
  200. validatesPresenceOf('position')
  201. }
  202. }
  203.  
  204. e = new Employee ()
  205. e.isValid()
  206. println e.errors.getFullMessages()
Add Comment
Please, Sign In to add comment