| Grails Version: 3.0.9 | Groovy Version: 2.4.5 | JVM Version: 1.8.0_60
Hi,
I have the following GORM query involving a join between the 'Event' and 'EventCategory' domain objects with page results.
def advancedSearchWithPagedResults(int max, int offset, String search, Date startDate, Date endDate, List myEventCategories) {
// need to convert our list of ints to longs as the field they'll be compared to is a long
def listOfLongs = myEventCategories.collect {
it.toLong()
}
def wildcardSearch = search ? '%' + search + '%' : '%'
def ids = Event.createCriteria().list {
projections {
distinct 'id'
}
maxResults max
firstResult offset
or {
like("eventName", wildcardSearch)
like("address.town", wildcardSearch)
like("address.county", wildcardSearch)
}
and {
if (listOfLongs) {
eventCategories {
'in'("id", listOfLongs)
}
}
between("startDateTime", startDate, endDate)
eq("enabled", true)
}
order("startDateTime", "asc")
}
/* Get the acual events using the list of id's just obtained */
def results = Event.getAll(ids)
return results
}
However, I need to add in / merge the following MySQL query that calculates the distance of each event (in miles) from the supplied latitute and longitude (51.519159, -0.133190) and then filters out any event that is in excess of 25 miles (in the example). The events are also ordered by distance from the supplied lat/long.
SELECT
`event`.*,
( 3959 * acos( cos( radians(51.519159) ) * cos( radians( `event`.address_latitude ) )
* cos( radians(`event`.address_longitude) - radians(-0.133190)) + sin(radians(51.519159))
* sin( radians(`event`.address_latitude)))) AS distance
FROM `event`
WHERE `event`.enabled = 1
HAVING distance < 25
ORDER BY distance;
My question is how to best approach changing the GORM query to incorporate the distance calulations?
Do I need to throw out my GORM query and resort to a native HQL query? I'm hoping not.
Any thoughts would be greatly appreciated.
I'll include relevant parts of the two domain objects for completeness.
class Event implements Taggable {
static hasMany = [courses: Course,
eventCategories: EventCategory,
faqs: FAQ]
static belongsTo = [eventOrganiser: EventOrganiser]
java.util.Date dateCreated
java.util.Date lastUpdated
boolean enabled = true
String eventName
String organisersDescription
@BindingFormat('dd/MM/yyyy HH:mm')
java.util.Date startDateTime
@BindingFormat('dd/MM/yyyy HH:mm')
java.util.Date endDateTime
@BindingFormat('dd/MM/yyyy HH:mm')
java.util.Date entriesOpenDateTime
@BindingFormat('dd/MM/yyyy HH:mm')
java.util.Date entriesCloseDateTime
BigDecimal fromPrice
Address address
Contact primaryContact
static embedded = ['address','primaryContact']
// transient fields
double distanceFromUsersPostcode
....
}
class EventCategory {
static hasMany = [events:Event]
static belongsTo = [Event]
String parentCategoryName
String parentSubCategoryName
String categoryName
String description
int order
}