Re: Building expression from Null strings

From: Eric Schneider (eri..entralparksoftware.com)
Date: Tue Oct 12 2004 - 10:18:55 EDT

  • Next message: Gentry, Michael: "RE: Building expression from Null strings"

    Cyril,

    I'd do something like this....

    Expression qual = null;
    addLikeExpToQualifier(qual, "firstName", getSearchFirstName());
    addLikeExpToQualifier(qual, "lastName", getSearchLastName()));
    addLikeExpToQualifier(qual, "userName", getSearchUserName()));
    addLikeExpToQualifier(qual, "email", getSearchEmail()));
    SelectQuery query = new SelectQuery(Customer.class, qual);

    public void addLikeExpToQualifier(Expression qual, String attribute,
    String searchString){
            if(!StringUtils.isEmpty(searchString)) {
                    if (qual == null) {
                            qual = ExpressionFactory.likeIgnoreCaseExp(attribute, "%" +
    searchString + "%");
                    } else {
                            qual = qual.andExp(ExpressionFactory.likeIgnoreCaseExp(attribute,
    "%" + searchString + "%"));
                    }
            }
    }

    I think that would work, no?

    Cheers,
    e.

    On Oct 12, 2004, at 9:35 AM, Cyril Godefroy wrote:

    > I'm trying to do something around this:
    >
    > Expression qual = generateLikeExp("firstName", getSearchFirstName());
    > qual =qual.andExp(generateLikeExp("lastName",
    > getSearchLastName()));
    > qual =qual.andExp(generateLikeExp("userName",
    > getSearchUserName()));
    > qual =qual.andExp(generateLikeExp("email", getSearchEmail()));
    > SelectQuery query = new SelectQuery(Customer.class, qual);
    > ...
    >
    > public Expression generateLikeExp(String attribute, String
    > searchString){
    > if(!searchString.equals("") && searchString != null){
    > return ExpressionFactory.likeIgnoreCaseExp(attribute, "%"
    > + searchString + "%");
    > } else {
    > return null;
    > }
    > }
    >
    > You see where this leads me... If getFirstName() returns null, then
    > andExp() cannot be called on a null object.
    >
    > Is there an obvious way to go around this problem that I am not seeing?
    >
    > Tia,
    > Cyril



    This archive was generated by hypermail 2.0.0 : Tue Oct 12 2004 - 10:19:05 EDT