Grails has very good support for binding request parameters to a domain object and it's associations. This largely relies on detecting request parameters that end with .id
and automatically loading those from the database.
However, it's not clear how to populate the associations of a command object. Take the following example:
class ProductCommand {
String name
Collection<AttributeTypeCommand> attributeTypes
ProductTypeCommand productType
}
This object has a single-ended association with ProductTypeCommand
and a many-ended association with AttributeTypeCommand
. The list of all attribute types and product types are available from an implementation of this interface
interface ProductAdminService {
Collection<AttributeTypeCommand> listAttributeTypes();
Collection<ProductTypeCommand> getProductTypes();
}
I use this interface to populate the product and attribute type selection lists in a GSP. I also dependency-inject this interface into the command object, and use it to "simulate" attributeTypes
and productType
properties on the command object
class ProductCommand {
ProductAdminService productAdminService
String name
List<Integer> attributeTypeIds = []
Integer productTypeId
void setProductType(ProductTypeCommand productType) {
this.productTypeId = productType.id
}
ProductTypeCommand getProductType() {
productAdminService.productTypes.find {it.id == productTypeId}
}
Collection<AttributeTypeCommand> getAttributeTypes() {
attributeTypeIds.collect {id ->
productAdminService.getAttributeType(id)
}
}
void setAttributeTypes(Collection<AttributeTypeCommand> attributeTypes) {
this.attributeTypeIds = attributeTypes.collect {it.id}
}
}
What actually happens is that the attributeTypeIds
and productTypeId
properties are bound to the relevant request parameters and the getters/setters "simulate" productType
and attributeTypes
properties. Is there a simpler way to populate the associations of a command object?