grails list and createCriteria()

CoffeePeddlerIntern picture CoffeePeddlerIntern · Apr 16, 2013 · Viewed 12.3k times · Source

I have a list function for a grails web app. This list will populate a table base on some search criteria. Im using hibernate createCriteria to try to achieve this.

In this case they can search by 2 different key features. The username and the userNumber.

My only issue is trying to tailor the function so that if it finds a null value, It will prompt the user that one of the the values is null. A username MUST have a userNumber. And if for some reason the userNumber comes up null then it needs to prompt the user. Or if the combination of the username and userNumber is not found, it will also prompt the user.

This is my list function with the search criteria. The println at the bottom are for debugging and shows kind of what im looking to do.

def list(params) {
//TODO STILL NOT WORKING RIGHT  
def userSearchCri = User.createCriteria()

    def userNumber = params.searchUserNumber
    def username = params.searchUsername
    uerSearchCri = User.createCriteria()



    def userList = userSearchCri.list() {

        eq('username', username)
        eq('userNumber', userNumber)
    }

    System.out.println("Search: " + userList.userNumber)
    if(userList.userNumber == "null"){
        System.out.println("OH NO! The Users Number is = " + userList.userNumber + " DO SOMETHING!")
    }


    [userInstanceList: userList]
    } 

Iv tried different combinations of the createCriteria functions. like isNotNull, ilike, ect... im starting to think I cant do this with createCritera. This is the source im using for the createCriteria http://grails.org/doc/2.2.1/ref/Domain%20Classes/createCriteria.html.

If anyone knows how I might be able to achieve my goal, either using the search criteria or some other method. I would greatly appreciate some advice.

Thanks

Answer

dmahapatro picture dmahapatro · Apr 16, 2013

Various key points to look at:

  1. First, Validate the params to see whether userName ans/or userNumber is passed, ans send back a message to user to provide valid information. By this way you are eradicating the option of user input error.
  2. Using Criteria returns back a list of domain objects. You have to iterate over the list to get the userName/userNumber.
  3. Once you have a valid user list you should be able to validate the userName/userNumber accordingly for each user object.

Here is a sample:

def list(params) {
    def userNumber = params.searchUserNumber
    def username = params.searchUsername
    
    //Validate whether params are NULL or valid. Send back error message to user if validation fails
    validateParams()
    uerSearchCri = User.createCriteria()

    def userList = userSearchCri.list() {
        or{
            eq('username', username)
            eq('userNumber', userNumber)
        }
    }

    if(userList){
        userList.each{user->
            if(!user.userName || !user.userNumber){
                //Send error back if needed 
            }
        }
    }
}