Question

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

Was it helpful?

Solution

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 
            }
        }
    }
}

OTHER TIPS

Databases commonly handle null values as special cases that don't match arithmetic operations such as '='/eq. So in your case, I'm going to make some assumptions that you can correct if I'm wrong:

  1. username is a nullable field
  2. userNumber is a nullable field
  3. A user must have at least a username or userNumber (sort of bizarre data model, but let's go with it)

In that case, what you could do is use an or condition in your criteria then test for null values once you get a result:

def userList = userSearchCri.list() {
    or{
      eq('username', username)
      eq('userNumber', userNumber)
    }
}
  • If you get no results then the user is not found.
  • Otherwise test for a null username or userNumber and prompt the user accordingly.

You can have conditions inside a criteria. Also I think you need only one result (username and userNumber are unique?), so you can use get() instead of list()

def user = userSearch.get {
  if(params.username) {
    eq('username',params.username)
  }

  if(params.userNumber) {
    eq('userNumber',params.userNumber)
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top