I am trying to complete a criteria query in Grails that sometimes has to refer to an unassociated table, therefore using a detached query. Depending on a passed in parameter, I need to perform a different subquery.
I have a Customer domain, which is what I'm creating the criteria based on as there are options available to query against the customer fields, name etc..
It should be possible for the user to query against the customer by the statusof customer orders, this is where my trouble begins. There is no hasMany between customers and orders but there is a join table which can be queried using a detached query.
So there is also an Order and a CustomerOrder join table.
The Order table has a status field, which can contains an Enum with possible values of null, OPEN or CLOSED.
I want to give the user the ability (via a select menu) to choose an order status option of either 'No Orders', 'Open Orders', 'Closed Orders' and 'Open or Closed Orders'
translating this to a detached criteria is proving tricky, if anyone can help that would be great.
to get started I have... but its not complete and I don't think I'm going in the right direction with the if block.
def results = Customer.createCriteria().list {
/* other restrictions omitted */
// now the order status test
if (params.orderStatus) {
if (params.orderStatus == 'NO_ORDERS') {
} else if (params.orderStatus == 'CLOSED_ORDERS') {
} else if (params.orderStatus == 'OPEN_ORDERS') {
} else if (params.orderStatus == 'OPENORCLOSED_ORDERS') {
def sub = DetachedCriteria.forClass(CustomerOrder.class, 'co').with {
and {
order {
eq('status', params.orderStatus)
}
client {
add(Restrictions.eqProperty('this.id', 'id'))
}
}
//setProjection(Property.forName('id'))
}
add(Subqueries.propertyIn('id', sub))
}
}
}