Guest User

Untitled

a guest
Feb 20th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. class Product {
  2.  
  3. // the static url to the product
  4. String url
  5.  
  6. // the product's data or path to product
  7. // NOTE: data is being written to the file system
  8. String data
  9.  
  10. // arbitrary name for product
  11. String name
  12.  
  13. // subview that this product belongs to
  14. Subview subview
  15.  
  16. excludedProps = ['data']
  17.  
  18. /**
  19. * Called when this obj is loaded from the DB.
  20. * Loads the data field based on the URL field
  21. */
  22. def onLoad = {
  23. File file = new File(id)
  24. data = file.read
  25. }
  26.  
  27. /**
  28. * Called before the model is stored to the DB. Writes the data contents to
  29. * the file system and updates the URL field
  30. */
  31. def beforeInsert = {
  32. File file = new File(id)
  33. file.write data
  34. data = ""
  35. url = "product/$id"
  36. }
  37.  
  38. def beforeUpdate = {
  39. File file = new File(id)
  40. file.write data
  41. data = ""
  42. url = "product/$id"
  43. }
  44.  
  45. def beforeDelete = {
  46. File file = new File(id)
  47. file.delete
  48. }
  49.  
  50.  
  51. // orm mapping to Subview
  52. static belongsTo = [Subview]
  53.  
  54. static constraints = {
  55. data(blank:true, maxSize:5000)
  56. url(nullable:true)
  57. }
  58. }
Add Comment
Please, Sign In to add comment