2013-10-11 65 views
0

我正面臨着Grails模型關聯的問題。這是問題:在Grails模型關聯中需要幫助

  • 訂戶和客戶從PartyRole延伸。
  • 客戶可能擁有多個訂戶,並且訂戶屬於客戶。
  • 一方可能有很多PartyRole。
  • 人和組織從黨延伸。
  • 一個人屬於組織。
  • 一個人有很多個人資料和個人資料屬於Person。

現在我想編輯當前登錄的用戶(用戶),這基本上是組織類型意味着組織屬性如orgName和orgSize。

我可以使用登錄用戶(訂閱者)找到Person(姓氏和姓氏)和個人資料(電子郵件)詳細信息,但無法獲取組織詳細信息。代碼如下。

def profile = { 
    Subscriber loggedinSubscriber = Subscriber.get(springSecurityService.principal.id) 
    if (loggedinSubscriber == null){ 
     redirect(controller: "login" , action:"login"); 
    } 
    else{ 
     println loggedinSubscriber 
     Party person = Person?.get(loggedinSubscriber.party.id) 
     Party org = Organization?.get(loggedinSubscriber.party.id) 
     Profile profile = person?.profile 
     [userInstance: person, authorityList: sortedRoles()] 
    } 
    } 

當我試圖讓組織細節與

Party org = Organization?.get(loggedinSubscriber.party.id) 

我得到空值,但在相同的方式,我可以用已登錄的用戶(用戶)獲取人的細節無一不是從黨的延長。 `

任何想法如何獲得組織細節。

人:

package com.vproc.member 

import com.vproc.enquiry.Enquiry; 
import com.vproc.enquiry.Membership; 
import com.vproc.enquiry.Notification; 
import com.vproc.enquiry.Team; 

class Person extends Party{ 

    String firstName 
    String lastName 

    Profile profile 
    static belongsTo = [Organization] 

    static constraints = { 
     lastName nullable:true 
     firstName blank:false 

    } 

} 

**Organization:** 

package com.vproc.member 

import java.util.Date; 

class Organization extends Party{ 

    String orgName 
    Person contact 
    int orgSize 
    boolean isVendor 

    static constraints = { 
    } 
} 

簡介:

package com.vproc.member 

import java.util.Date; 

import com.vproc.enquiry.Enquiry; 
import com.vproc.enquiry.Membership; 
import com.vproc.enquiry.Team; 

class Profile { 

    String emailAddress // field governed by privacy policy 
    String phoneNumber // field governed by privacy policy 
    Date dateCreated 
    Date lastUpdated 
    boolean isDefaultProfile 
    String status 
    static belongsTo = [person:Person] 
    //ProfilePrivacyLevelEnum privacyLevel = ProfilePrivacyLevelEnum.Private 

    static constraints = { 
    } 
} 

認購人:

package com.vproc.member 

import java.util.Date; 

import com.vproc.common.StatusEnum; 
import com.vproc.enquiry.Discussion; 
import com.vproc.enquiry.Enquiry; 
import com.vproc.enquiry.Membership; 
import com.vproc.enquiry.Notification; 
import com.vproc.enquiry.SharedEnquiry; 
import com.vproc.enquiry.Team; 
import com.vproc.order.Seat; 

class Subscriber extends PartyRole{ 

    transient springSecurityService 

    String username 
    String password 
    boolean enabled 
    boolean accountExpired 
    boolean accountLocked 
    boolean passwordExpired 
    StatusEnum status 
    Date dateCreated 
    Date lastUpdated 
    List<Contact> contacts ; 

    static belongsTo = [ customer: Customer] 
    static hasMany = [seats: Seat, ownedEnquiries: Enquiry,enquiresSharedWith: SharedEnquiry, enquiriesSharedBy: SharedEnquiry , 
     managedTeams: Team , memberships: Membership, contacts: Contact , sharedByContacts: SharedContact, sharedWithContacts: SharedContact, 
      vContacts: VContact, partOf: VContact, sharedbyVContacts: SharedVcontact, sharedWithVcontacts: SharedVcontact, 
      notifications: Notification, discussions: Discussion] 
    static mappedBy = [ managedTeams : "manager" , enquiresSharedWith: "sharedWith" , enquiriesSharedBy: "sharedBy" , 
                sharedByContacts : "sharedBy" , sharedWithContacts : "sharedWith" , 
                vContacts: "forSubscriber" , partOf :"ofContact", 
                sharedbyVContacts: "sharedby" , sharedWithVcontacts :"sharedWith" 
                ] 


    static constraints = { 
     username validator : { val , obj -> 
           if (obj.status != StatusEnum.Pending) 
             val!= null 
           } 
     username unique: true 
     password validator : { val , obj -> 
            if (obj.status != StatusEnum.Pending) 
             val != null 
          } 

     contacts nullable: true 
     notifications nullable : true 
     username nullable: true 
     password nullable: true 

    } 

    static mapping = { 
     password column: '`password`' 
    } 

    Set<Role> getAuthorities() { 
     SubscriberRole.findAllBySubscriber(this).collect { it.role } as Set 
    } 

    def beforeInsert() { 
     encodePassword() 
    } 

    def beforeUpdate() { 
     if (isDirty('password')) { 
      encodePassword() 
     } 
    } 

    protected void encodePassword() { 
     password = springSecurityService.encodePassword(password) 
    } 
} 

黨:

包com.vproc.member

進口java.util.Date;

class Party { 

    Date dateCreated 
    Date lastUpdated 

    static constraints = { 
    } 

    static mapping = { 
     tablePerHierarchy false 
    } 
} 

成員角色:

包com.vproc.member

進口java.util中。日期;

class PartyRole { 

    Party party 

    Date dateCreated 
    Date lastUpdated 
    static constraints = { 
    } 
    static mapping = { 
     tablePerHierarchy false 
    } 
} 

自舉:

類自舉{

DEF初始化= {參數servletContext - >

def userRole = Role.findByAuthority('ROLE_USER') ?: new Role(authority: 'ROLE_USER').save(failOnError: true) 
def adminRole = Role.findByAuthority('ROLE_COMPANY_ADMIN') ?: new Role(authority: 'ROLE_COMPANY_ADMIN').save(failOnError: true) 
def guestRole = Role.findByAuthority('ROLE_GUEST') ?: new Role(authority: 'ROLE_GUEST').save(failOnError: true) 
def csrRole = Role.findByAuthority('ROLE_CSR') ?: new Role(authority: 'ROLE_CSR').save(failOnError: true) 

//PersonRole.create adminUser, adminRole 
def address = new Address(city : 'Pune' , stateCode : 'MH' , countryCode : 'IN' ) 

def adminProfile = Profile.findByEmailAddress('[email protected]')?: new Profile(
    privacyLevel: ProfilePrivacyLevelEnum.Private, 
    emailAddress: "[email protected]" , 
    phoneNumber: "9325507992", 
    status : 'Active' 
    ).save(failOnError: true) 

def adminPerson = Person.findByProfile(adminProfile) ?: new Person(firstName: 'admin' , lastName : 'user' , profile: adminProfile).save(failOnError: true) ; 
def vprocOrganization = Organization.findByOrgName('VPROCURE') ?: new Organization (orgName: 'VPROCURE' , orgSize : 100 , mailingAddress: address, contact: adminPerson).save(failOnError: true) 
def vprocCustomer = Customer.findByParty(vprocOrganization) ?: new Customer (party: vprocOrganization, status: StatusEnum.Active ).save(failOnError: true) ; 
def adminUser = Subscriber.findByUsername('admin') ?: new Subscriber(username : 'admin' , password : 'passw0rd' , enabled: true , party: adminPerson, customer: vprocCustomer , status: StatusEnum.Active).save(failOnError: true) 

if (!adminUser.authorities.contains(adminRole)){ 
     SubscriberRole.create adminUser, adminRole 
    } 

    JSON.registerObjectMarshaller(Date) { 
    return it?.format("MM/dd/yyyy") 
    } 

    def userProfile = Profile.findByEmailAddress('[email protected]') ?: new Profile(
     privacyLevel: ProfilePrivacyLevelEnum.Private, 
     emailAddress: "[email protected]", 
     phoneNumber : "9325507992", 
     status : 'Active' 
     ).save(failOnError: true) 

    def userPerson = Person.findByProfile(userProfile) ?: new Person(firstName: 'plain' , lastName : 'user' , profile: userProfile).save(failOnError: true) ; 
    def plainUser = Subscriber.findByUsername('plainuser') ?: new Subscriber(username: 'plainuser', password : 'passw0rd' , enabled: true , party: userPerson, customer: vprocCustomer , status: StatusEnum.Active).save(failOnError : true) 
    if (!plainUser.authorities.contains(userRole)){ 
     SubscriberRole.create plainUser, userRole 
    } 

/*vprocCustomer.addToSubscribers(amdinUser) 
vprocCustomer.addToSubscribers(plainUser) 
vprocCustomer.save(failOnError : true);*/ 

} 

def destroy = { 
} 

}

+0

你能告訴你的域類? – Eylen

+0

嗨@Eylen,我更新了域類的問題。請看一看。 –

+0

就我所能理解的,你只是將訂閱者與一方進行關聯,不可能使用相同的ID檢索Person和Organization ... 另外,如果您確定要檢索正確的人員實例(你最好檢查一下)你應該可以訪問人員公司easilly,只是做person.company – Eylen

回答

1

剛剛在人域類改變

static belongsTo = [Organization] 

static belongsTo = [organization:Organization] 

,並從person.organization人員實例訪問組織信息