我正在一个新的 Grails 2.1.0 应用程序中编写调度功能。我正在从 Ruby on Rails 项目过渡,我的大部分查询策略都源自 Rails 风格。我有以下域类:
时间表.groovy
class Schedule {
// Number of minutes between available appointment slots
int defaultAppointmentInterval
Time officeOpenStart
Time officeOpenEnd
Time lunchStart
Time lunchEnd
static hasMany = [inventorySlots: InventorySlot]
static constraints = {
// long validation rules
}
def boolean isAvailableAt(Date dateTime) {
def isAvailable = true
if (inventorySlots.isEmpty()) {
isAvailable = false
} else if (inventorySlotsSurroundingTime(dateTime).isEmpty()) {
isAvailable = false
}
isAvailable
}
def inventorySlotsSurroundingTime(Date dateTime) {
InventorySlot.surroundingTime(dateTime) {
and {
inventorySlot.schedule = this
}
}
}
}
InventorySlot.groovy
class InventorySlot {
Date startTime
static belongsTo = [schedule: Schedule]
static constraints = {
startTime nullable: false, blank: false
}
static mapping = {
tablePerHierarchy true
schedule lazy: false
}
static namedQueries = {}
def static surroundingTime(Date time) {
[UnboundedInventorySlot.surroundingTime(time), BoundedInventorySlot.surroundingTime(time)].flatten()
}
def endTime() {
return ((BoundedInventorySlot) this).endTime?: (UnboundedInventorySlot (this)).endTime()
}
}
UnboundedInventorySlot.groovy
class UnboundedInventorySlot extends InventorySlot {
static namedQueries = {
// surroundingTime { time ->
// le 'startTime', time
// ge 'startTime', time - 1
// }
}
@Override
def static surroundingTime(Date time) {
findAllByStartTimeLessThanEqualsAndStartTimeGreaterThanEquals(time, time - 1)
}
def Date endTime() {
def endTime
// If the office closing is defined, use that, otherwise use tonight @ 23:59:59
endTime = schedule?.officeOpenEnd?: new DateTime(startTime + 1).withTimeAtStartOfDay().plusSeconds(-1).toDate()
return endTime
}
}
BoundedInventorySlot.groovy
class BoundedInventorySlot extends InventorySlot {
Date endTime
static constraints = {
endTime nullable: false, blank: false, validator: {val, obj ->
if (val.date != obj.startTime.date) { return ["invalid.differentDate", val.date] }
}
}
static namedQueries = {
// surroundingTime { time ->
// le 'startTime', time
// ge 'endTime', time
// }
}
@Override
def static surroundingTime(Date time) {
findAllByStartTimeLessThanEqualsAndEndTimeGreaterThanEquals(time, time)
}
@Override
def Date endTime() {
endTime
}
}
我想做的是实现 Schedule#isAvailableAt(Date) 方法,如下所示:
def boolean isAvailableAt(Date dateTime) {
def isAvailable = true
if (inventorySlots.isEmpty()) {
isAvailable = false
} else if (inventorySlots.surroundingTime(dateTime).isEmpty()) {
isAvailable = false
}
isAvailable
}
其中inventorySlots.surroundingTime()
调用本质上是InventorySlot.surroundingTime()
但不是查询 InventorySlots 的宇宙,它仅对与计划实例关联的实例进行预过滤。这在 Rails 中很常见,但是在 Grails 中对“链式查询”或“集合查询”的任何搜索似乎都没有提供好的文档。谢谢你的帮助。