Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- interface Profile {
- Integer getDuration()
- }
- import org.bson.types.ObjectId
- class Profile1 implements Profile {
- ObjectId id
- String profileName
- String type
- Date effectiveStartDate
- Date effectiveEndDate
- Integer getDuration(){
- effectiveEndDate - effectiveStartDate
- }
- static mapWith = "mongo"
- }
- import org.bson.types.ObjectId
- class Profile2 implements Profile{
- ObjectId id
- String profileName
- String type
- Date effectiveStartDate
- Date effectiveEndDate
- static mapWith = "mongo"
- Integer getDuration(){
- effectiveEndDate - effectiveStartDate
- }
- }
- class User {
- ObjectId id
- static mapWith = "mongo"
- static embedded = ['profiles']
- String email
- List<Profile> profiles
- }
- class UserController {
- def index() {
- def profile1 = new Profile1(type: 'Individual',
- profileName: 'IndividualProfile',
- effectiveStartDate: new Date(),
- effectiveEndDate: new Date() + 100) as Profile
- def profile2 = new Profile2(type: 'Company',
- profileName: 'CompanyProfile',
- effectiveStartDate: new Date(),
- effectiveEndDate: new Date() + 50) as Profile
- println profile1.duration //prints 100
- println profile2.duration //prints 50
- def user = new User(profiles: [profile1, profile2], email: 'abc@gmail.com').save(flush: true)
- render user as JSON
- }
- }
- //db.user.find()
- {
- "_id" : ObjectId("51ba8d55892cb98368b2f1e5"),
- "email" : "abc@gmail.com",
- "profiles" : [{
- "effectiveEndDate" : ISODate("2013-09-22T03:26:13.396Z"),
- "effectiveStartDate" : ISODate("2013-06-14T03:26:13.396Z"),
- "profileName" : "IndividualProfile",
- "type" : "Individual"
- },
- {
- "effectiveEndDate" : ISODate("2013-08-03T03:26:13.423Z"),
- "effectiveStartDate" : ISODate("2013-06-14T03:26:13.423Z"),
- "profileName" : "CompanyProfile",
- "type" : "Company"
- }
- ],
- "version" : 0
- }
- //Browser Render:
- {
- "class": "com.example.User",
- "id": {
- "class": "org.bson.types.ObjectId",
- "inc": 1756557797,
- "machine": -1993557629,
- "new": false,
- "time": 1371180373000,
- "timeSecond": 1371180373
- },
- "email": "abc@gmail.com"
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement