2

域字段:

startDate: Date
length: Integer
perpetuity: Boolean
expiryDate: Transient (startDate + length (in years))

域方法 - 这些方法用于在大过滤器表单上进行过滤,以便用户搜索 4 种不同的过期状态。

Date getExpiryDate() {
    if (this.startDate == null || this.length == null) {
        return null
    }

    Timestamp startDate = this.startDate;
    Calendar expirationDate = Calendar.getInstance()
    expirationDate.setTime(startDate)
    expirationDate.add(Calendar.YEAR, this.length)

    return expirationDate.getTime()
}

String getMechExpiredStatus() {
    String isExpiredString = 'Unspecified'
    Date expDate = getExpiryDate()
    if (expDate != null) {
        if (expDate < new Date()) {
            isExpiredString = 'Yes'
        } else {
            isExpiredString =  'No'
        }

    } else if (isPerpetual && startDate != null) {
        isExpiredString =  'Perpetual'
    }

    return isExpiredString
}

构建结果列表

PagedResultList getMechanisms(offset, max, sortColumn, sortOrder, filters, idsOnly = false) {
    def criteria = Mechanism.createCriteria()
    def results = criteria.list(max: max, offset: offset) {
        if (idsOnly) {
            projections {
                property('id')
            }
        }

        if (filters && filters.idFilter) {
            eq('id', filters.idFilter);
        }
    ...
    }

    if (filters && filters.expiredFilter != null && filters.expiredFilter) {
        // Do Work
        // Tried getting results and removed invalid results that do not meet the 
        // expired filter selection
    }

return results

}

我似乎找不到使用瞬态字段“expiryDate”或使用 getMechExpiredStatus 方法编写 createCriteria 的方法。

我尝试使用其他过滤器获取项目列表,然后分别删除对过期过滤器无效但 PagedResultList 没有正确更新的项目;它只返回一个当前的分页结果列表。我也找不到关于 PagedResultList 对象的太多帮助。出于某种原因,删除和添加返回一个布尔值。显然我不太了解这个对象。

如果我从初始条件列表中删除最大值和偏移量,则查询需要长时间才能运行。

有什么想法可以做到这一点吗?

4

2 回答 2

2

我通过在视图中创建过期状态和日期并将其链接到域对象来解决了这个问题。效果很好!

于 2014-08-22T22:13:09.577 回答
1

您不能查询瞬态属性。根据定义,瞬态属性不存在于您的数据源中,因此不能成为数据源执行的查询的一部分。

为了使用瞬态属性完成您想要做的事情,您需要创建自己的结果列表,而不使用数据源提供的限制和偏移功能,过滤结果,然后自己应用偏移和限制。

由于您没有提供您的查询是什么的示例,因此我只能自己编写一个示例:

// this could be any type of query, using list for simplicity of the example
def databasePersons = Person.list()
// assumes anAdult is a boolean transient property of person
// calculated based on age
def filteredPersons = databasePersons.findAll{ it.isAnAdult() }

// get rid of the first X records, the offset
filteredPersons = filteredPersons.drop(offset)

// only take the next X remaining records, the max
filteredPersons = filteredPersons.take(max)
于 2014-08-21T22:34:27.400 回答