简体   繁体   中英

Groovy: Accessing properties in Parent class

This has to be something really easy, but am pretty new to Grails & Groovy. I am writing a Grails application in version 2.4.3.

There's a class called UserInfo

class UserInfo extends UserDetails {

    String userID = ""

    static constraints = {
        userID blank:false
    }   
}

Now the UserDetails class:

class UserDetails {

        String userName = ""
        String userType = ""  // 'Admin' or 'Guest' or 'IT' or 'Supervisor'
        ...................
       //Lot more Informations
        ....................
        static constraints = {
            userName blank:false
            userType blank:false
        }   
    }

And now in another controller class i want to get the User Type information.

def getUserData = {

def type = []
int count = 0

UserInfo.each {
            type [i] = it.userType; // Error: No such property: operator for class: UserInfo
            i++;
     }
}

The goal here is to get list of UserTypes ('Admin' or 'Guest' or 'IT' or 'Supervisor') and the total numbers of each user type.

For eg, Admin = 230, Guest = 120, .........

The code above doesn't do that and in fact gives an error. I want to use the data above and plot a X - Y Graph.

Ideally i should have the data in a def in an array model as shown below.

def type = []

type << [totalNum: new Integer("230"), userType: 'Admin']
type << [totalNum: new Integer("120"), userType: 'Guest']
............................................................
............................................................

Once i have populated all the data into a def , then i could use Google Visualization to draw the graph easily.

As of now i still don't know how to extract the data from each instance of UserInfo class and then get it to a def .

Appreciate your help!

您在这里缺少list()方法:

UserInfo.list().each {...}

I don't think it's possible with the syntax you're using. UserInfo.each refers to a colosure over static members of the UserInfo class. However if you want to enumerate things, this can be done with the following syntax.

    class UserInfo {

        enum UserType {
            Admin, Guest, IT
        }

    }

Enumerating over these then becomes

    def getUserData = {
        def types = []
        def count = 0
        UserInfo.UserType.each { 
            types << it
            count++
        }
    }

However, in grails, with your original class, you're just missing a findAll() or list() call for a domain object. This however will produce duplicates if you have more than one of each user type.

    def getUserData = {
        def types = []
        def count = 0
        UserInfo.findAll().each{ UserInfo it -> 
            types << it.userType
            count++
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM