Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8430829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:41:35+00:00 2026-06-10T05:41:35+00:00

I am writing a scheduling feature in a new Grails 2.1.0 application. I am

  • 0

I am writing a scheduling feature in a new Grails 2.1.0 application. I am transitioning from a Ruby on Rails project, so much of my query strategy derives from Rails style. I have the following domain classes:

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

What I would like to do is to implement the Schedule#isAvailableAt(Date) method as follows:

def boolean isAvailableAt(Date dateTime) {
    def isAvailable = true

    if (inventorySlots.isEmpty()) {
        isAvailable = false
    } else if (inventorySlots.surroundingTime(dateTime).isEmpty()) {
        isAvailable = false
    }

    isAvailable
}

where the inventorySlots.surroundingTime() invocation is essentially InventorySlot.surroundingTime() but instead of querying the universe of InventorySlots, it pre-filters on just the instances associated with the schedule instance. This is very common in Rails, but any searches for “chained query” or “collection query” in Grails doesn’t seem to provide good documentation. Thanks for any help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T05:41:36+00:00Added an answer on June 10, 2026 at 5:41 am

    I can think of two approaches off the top of my head which would work:

    A more complex dynamic finder:

    InventorySlots.findAllByScheduleAndStartTimeLessThanEqualsAndEndTimeGreaterThanEquals(this, time, time -1)
    

    You can chain named queries together, then use any of the autowired finders to run the actual query, so if you uncomment your named query:

    static namedQueries = {
            surroundingTime { time ->
                le 'startTime', time
                ge 'startTime', time - 1
            }
    }
    

    You could simply call:

    InventorySlots.surroundingTime(time).findAllBySchedule(this)

    You might also want to look into where queries in Grails 2 if you are not a fan of the criteria builder syntax. They are more type safe than criteria queries, and can be chained in the same fashion.

    Update: Unfortunately, I’m not familiar with the inner workings of how named queries work with polymorphism, and I presume trouble with that is why you commented that out. I think worst case though, you could build a query on the parent like this one:

    surroundingTime { time ->
                or {
                    and {
                        eq('class', BoundedInventorySlot.name)
                        le 'startTime', time
                        ge 'startTime', time
                    }
                    and {
                        eq('class', UnboundedInventorySlot.name)
                        le 'startTime', time
                        ge 'startTime', time - 1
                    }
                }
            }
    

    ***Update: Could you leverage the spread operator to simplify your task? i.e. Keep this code, but remove the .flatten() and call surroundingTime as a named query or where query.

    def static surroundingTime(Date time) {
            [UnboundedInventorySlot.surroundingTime(time), BoundedInventorySlot.surroundingTime(time)]
        }
    

    Then you could call:
    Schedule.surroundingTime(time)*.findAllBySchedule(this).flatten()

    Not ideal that the caller needs to know to combine the results, but an interesting approach maybe.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Writing my first C# application...never touched the language before and not much of a
I'm writing a job-scheduling app in Ruby for my work (primarily to move files
Writing Junit Tests for my spring application. Because I am new at this I
I'm writing a scheduling application in Java using Quartz. I'm using the CronTrigger, but
I'm in the process of writing a scheduling application on a shared hosting server.
I'm writing up a scheduling application for my wife, and I ran into the
I am writing an application to allow users to schedule one-time long-running tasks from
I'm writing a scheduling application that uses JQuery UI extensively. One of the requirements
I am writing a Scheduling application, and each row in my Database Table is
Writing htaccess that allows me to remove index.php from the URL can confuse search

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.