Skip to content

Instantly share code, notes, and snippets.

@robfletcher
Created February 26, 2010 13:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robfletcher/315705 to your computer and use it in GitHub Desktop.
Save robfletcher/315705 to your computer and use it in GitHub Desktop.
PropertyEditor implementation used for binding to Grails domain objects by unique property value
import java.beans.PropertyEditorSupport
import org.apache.commons.lang.StringUtils
class DomainClassLookupPropertyEditor extends PropertyEditorSupport {
Class domainClass
String property
String getAsText() {
value."$property"
}
void setAsText(String text) {
value = domainClass."findBy${StringUtils.capitalize(property)}"(text)
if (!value) {
value = domainClass.newInstance((property): text)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment