在闭包中变量以避免多个if

时间:2014-10-02 16:16:47

标签: grails closures gorm

我需要为以下代码块提供更好的解决方案,以更好地满足DRY原则。有没有办法在每个闭包中处理运算符<,...,> =作为变量?

def query
    if (wvOp == ' ')
        query = Notiz.where {
        notiztext =~ "${params['notiztext']}" &&
        referenz =~ "${params['referenz']}" 
    }
    if (wvOp == '<')
        query = Notiz.where {
        notiztext =~ "${params['notiztext']}" && 
        referenz =~ "${params['referenz']}" &&
        anlagetermin < wiedervorlage
    }
    if (wvOp == '<=')
        query = Notiz.where {
        notiztext =~ "${params['notiztext']}" &&
        referenz =~ "${params['referenz']}" &&
        anlagetermin <= wiedervorlage
    }
    if (wvOp == '=')
        query = Notiz.where {
        notiztext =~ "${params['notiztext']}" &&
        referenz =~ "${params['referenz']}" &&
        anlagetermin == wiedervorlage
    }
    if (wvOp == '>')
        query = Notiz.where {
        notiztext =~ "${params['notiztext']}" &&
        referenz =~ "${params['referenz']}" &&
        anlagetermin > wiedervorlage
    }
    if (wvOp == '>=')
        query = Notiz.where {
        notiztext =~ "${params['notiztext']}" &&
        referenz =~ "${params['referenz']}" &&
        anlagetermin >= wiedervorlage
    }
    query.findAll()

彼得 多特蒙德,德国

2 个答案:

答案 0 :(得分:2)

一种方法是使用基于条件的查询替换where。这些方面的东西:

// translate incoming comparison operator to equivalent criteria method
def opMap = [ '<': 'lt', '<=': 'lte', '=': 'eq', '>': 'gt', '>=': 'gte' ]

def c = Notiz.createCriteria()
def results = c.list {
    and {
        ilike(notiztext, params['notiztext'])
        ilike(referenz, params['referenz'])
        if (wvOp in opMap.keySet()) {
           "${opMap[wvOp]}"(anlagetermin, wiedervorlage)
        }
    }
}

答案 1 :(得分:0)

你可以消除一堆重复。而不是......

def query

if (wvOp == ' ')
    query = Notiz.where {
        notiztext =~ "${params['notiztext']}" &&
        referenz =~ "${params['referenz']}" 
    }
}
if (wvOp == '<')
    query = Notiz.where {
        notiztext =~ "${params['notiztext']}" && 
        referenz =~ "${params['referenz']}" &&
        anlagetermin < wiedervorlage
    }
}
if (wvOp == '<=')
    query = Notiz.where {
        notiztext =~ "${params['notiztext']}" &&
        referenz =~ "${params['referenz']}" &&
        anlagetermin <= wiedervorlage
    }
}
if (wvOp == '=')
    query = Notiz.where {
        notiztext =~ "${params['notiztext']}" &&
        referenz =~ "${params['referenz']}" &&
        anlagetermin == wiedervorlage
    }
}
if (wvOp == '>')
    query = Notiz.where {
        notiztext =~ "${params['notiztext']}" &&
        referenz =~ "${params['referenz']}" &&
        anlagetermin > wiedervorlage
    }
}
if (wvOp == '>=')
    query = Notiz.where {
        notiztext =~ "${params['notiztext']}" &&
        referenz =~ "${params['referenz']}" &&
        anlagetermin >= wiedervorlage
    }
}

你可以这样做......

def query = Notiz.where {

    notiztext =~ "${params['notiztext']}" &&
    referenz =~ "${params['referenz']}" 

    if (wvOp == '<') {
        anlagetermin < wiedervorlage
    } else if (wvOp == '<=')
        anlagetermin <= wiedervorlage
    } else if (wvOp == '=')
        anlagetermin == wiedervorlage
    } else if (wvOp == '>')
        anlagetermin > wiedervorlage
    } else if (wvOp == '>=')
        anlagetermin >= wiedervorlage
    }
}