Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Mar 19th, 2012  |  syntax: Groovy  |  size: 1.47 KB  |  hits: 46  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. package pl.grooper.offer
  2.  
  3. import pl.grooper.offer.images.OfferMiniature;
  4. import pl.grooper.offer.images.OfferPicture;
  5. import pl.grooper.security.User
  6. import org.springframework.beans.factory.annotation.Value
  7. import org.jsoup.Jsoup;
  8. import org.jsoup.safety.Whitelist;
  9.  
  10. class Offer implements Serializable{
  11.  
  12.         /**
  13.          * name of this offer
  14.          */
  15.         String name
  16.         /**
  17.          * when was this offer created in system
  18.          */
  19.         Date dateCreated
  20.        
  21.         /**
  22.          * when does this offer end
  23.          */
  24.         Date dateExpired
  25.        
  26.         Category category
  27.        
  28.         String description
  29.  
  30.         OfferMiniature miniature
  31.                
  32.         List pictures
  33.  
  34.         static hasMany = [
  35.                 pictures: OfferPicture
  36.         ]
  37.        
  38.         static mapping = {
  39.                 description type:'text'
  40.                 pictures cascade: 'all-delete-orphan'
  41.          }
  42.        
  43.        
  44.        
  45.     static constraints = {
  46.                 name blank: false, maxSize: 50
  47.                 description blank: false, maxSize: 50000
  48.                 pictures nullable: true, maxSize: 3
  49.                 miniature nullable: true
  50.     }
  51.  
  52.         /**
  53.          * owner - user who created this offer
  54.          */
  55.         static belongsTo = [
  56.                 owner : User
  57.         ]
  58.        
  59.         /**
  60.          *
  61.          * @return description in a readable format
  62.          */
  63.     def getDesc() {
  64.         def desc = "${name} ${description}"
  65.         (desc.size() > 50) ? desc.subSequence(0,50) : desc
  66.     }
  67.  
  68.         String toString() {
  69.                 return name
  70.         }
  71.        
  72.         def cleanHtml() {
  73.                 description = Jsoup.clean(description, Whitelist.basic())
  74.         }
  75.        
  76.         def beforeInsert = {
  77.                 cleanHtml()
  78.         }
  79.        
  80.         def beforeUpdate() {
  81.                 if (isDirty('description')) {
  82.                         cleanHtml()
  83.                 }
  84.         }
  85.        
  86. }