Advertisement
dmahapatro

MongoDB Polymorphism

Jun 13th, 2013
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 2.90 KB | None | 0 0
  1.     interface Profile {
  2.         Integer getDuration()
  3.     }
  4.    
  5.     import org.bson.types.ObjectId
  6.     class Profile1 implements Profile {
  7.         ObjectId id
  8.         String profileName
  9.         String type
  10.         Date effectiveStartDate
  11.         Date effectiveEndDate
  12.    
  13.         Integer getDuration(){
  14.             effectiveEndDate - effectiveStartDate
  15.         }
  16.    
  17.         static mapWith = "mongo"
  18.     }
  19.    
  20.     import org.bson.types.ObjectId
  21.     class Profile2 implements Profile{
  22.         ObjectId id
  23.         String profileName
  24.         String type
  25.         Date effectiveStartDate
  26.         Date effectiveEndDate
  27.    
  28.         static mapWith = "mongo"
  29.    
  30.         Integer getDuration(){
  31.             effectiveEndDate - effectiveStartDate
  32.         }
  33.     }
  34.     class User {
  35.         ObjectId id
  36.    
  37.         static mapWith = "mongo"
  38.         static embedded = ['profiles']
  39.    
  40.         String email
  41.         List<Profile> profiles
  42.     }
  43.    
  44.     class UserController {
  45.    
  46.         def index() {
  47.             def profile1 = new Profile1(type: 'Individual',
  48.                                         profileName: 'IndividualProfile',
  49.                                         effectiveStartDate: new Date(),
  50.                                         effectiveEndDate: new Date() + 100) as Profile
  51.             def profile2 = new Profile2(type: 'Company',
  52.                                         profileName: 'CompanyProfile',
  53.                                         effectiveStartDate: new Date(),
  54.                                         effectiveEndDate: new Date() + 50) as Profile
  55.    
  56.             println profile1.duration //prints 100
  57.             println profile2.duration //prints 50
  58.    
  59.             def user = new User(profiles: [profile1, profile2], email: 'abc@gmail.com').save(flush: true)
  60.    
  61.             render user as JSON
  62.         }
  63.     }
  64.  
  65.     //db.user.find()
  66.     {
  67.         "_id" : ObjectId("51ba8d55892cb98368b2f1e5"),
  68.         "email" : "abc@gmail.com",
  69.         "profiles" : [{  
  70.                 "effectiveEndDate" : ISODate("2013-09-22T03:26:13.396Z"),  
  71.                 "effectiveStartDate" : ISODate("2013-06-14T03:26:13.396Z"),  
  72.                 "profileName" : "IndividualProfile",    
  73.                 "type" : "Individual"
  74.             },
  75.             {  
  76.                 "effectiveEndDate" : ISODate("2013-08-03T03:26:13.423Z"),      
  77.                 "effectiveStartDate" : ISODate("2013-06-14T03:26:13.423Z"),  
  78.                 "profileName" : "CompanyProfile",
  79.                 "type" : "Company"
  80.             }
  81.         ],
  82.         "version" : 0
  83.     }
  84.  
  85.     //Browser Render:
  86. {
  87.     "class": "com.example.User",
  88.     "id": {
  89.         "class": "org.bson.types.ObjectId",
  90.         "inc": 1756557797,
  91.         "machine": -1993557629,
  92.         "new": false,
  93.         "time": 1371180373000,
  94.         "timeSecond": 1371180373
  95.     },
  96.     "email": "abc@gmail.com"
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement